blob: a54c3e0f2ee1bc5ac0492c31659bd2fb7e851dc3 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * INET An implementation of the TCP/IP protocol suite for the LINUX
4 * operating system. INET is implemented using the BSD Socket
5 * interface as the means of communication with the user level.
6 *
7 * Routing netlink socket interface: protocol independent part.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * Fixes:
12 * Vitaly E. Lavrov RTA_OK arithmetics was wrong.
13 */
14
Jakub Kicinskiee5d0322017-06-21 18:25:04 -070015#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/errno.h>
17#include <linux/module.h>
18#include <linux/types.h>
19#include <linux/socket.h>
20#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/timer.h>
22#include <linux/string.h>
23#include <linux/sockios.h>
24#include <linux/net.h>
25#include <linux/fcntl.h>
26#include <linux/mm.h>
27#include <linux/slab.h>
28#include <linux/interrupt.h>
29#include <linux/capability.h>
30#include <linux/skbuff.h>
31#include <linux/init.h>
32#include <linux/security.h>
Stephen Hemminger6756ae42006-03-20 22:23:58 -080033#include <linux/mutex.h>
Thomas Graf18237302006-08-04 23:04:54 -070034#include <linux/if_addr.h>
John Fastabend77162022012-04-15 06:43:56 +000035#include <linux/if_bridge.h>
Jiri Pirkof6f64242014-11-28 14:34:15 +010036#include <linux/if_vlan.h>
Williams, Mitch Aebc08a62010-02-10 01:44:05 +000037#include <linux/pci.h>
John Fastabend77162022012-04-15 06:43:56 +000038#include <linux/etherdevice.h>
Martin KaFai Lau58038692017-06-15 17:29:09 -070039#include <linux/bpf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080041#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include <linux/inet.h>
44#include <linux/netdevice.h>
45#include <net/ip.h>
46#include <net/protocol.h>
47#include <net/arp.h>
48#include <net/route.h>
49#include <net/udp.h>
Daniel Borkmannea697632015-01-05 23:57:47 +010050#include <net/tcp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <net/sock.h>
52#include <net/pkt_sched.h>
Thomas Graf14c0b972006-08-04 03:38:38 -070053#include <net/fib_rules.h>
Thomas Grafe2849862007-03-22 11:48:11 -070054#include <net/rtnetlink.h>
Johannes Berg30ffee82009-07-10 09:51:35 +000055#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Nikolay Aleksandrova428afe2018-11-24 04:34:20 +020057#define RTNL_MAX_TYPE 50
Kees Cookccf8dbc2018-05-30 15:20:52 -070058#define RTNL_SLAVE_MAX_TYPE 36
59
Eric Dumazete0d087a2009-11-07 01:26:17 -080060struct rtnl_link {
Thomas Grafe2849862007-03-22 11:48:11 -070061 rtnl_doit_func doit;
62 rtnl_dumpit_func dumpit;
Florian Westphale4202512017-12-02 21:44:06 +010063 struct module *owner;
Florian Westphal62256f92017-08-09 20:41:52 +020064 unsigned int flags;
Florian Westphaladdf9b92017-12-02 21:44:05 +010065 struct rcu_head rcu;
Thomas Grafe2849862007-03-22 11:48:11 -070066};
67
Stephen Hemminger6756ae42006-03-20 22:23:58 -080068static DEFINE_MUTEX(rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70void rtnl_lock(void)
71{
Stephen Hemminger6756ae42006-03-20 22:23:58 -080072 mutex_lock(&rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
Eric Dumazete0d087a2009-11-07 01:26:17 -080074EXPORT_SYMBOL(rtnl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Kirill Tkhai79ffdfc2018-03-14 22:17:20 +030076int rtnl_lock_killable(void)
77{
78 return mutex_lock_killable(&rtnl_mutex);
79}
80EXPORT_SYMBOL(rtnl_lock_killable);
81
Eric Dumazet1b5c5492016-06-13 20:21:50 -070082static struct sk_buff *defer_kfree_skb_list;
83void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail)
84{
85 if (head && tail) {
86 tail->next = defer_kfree_skb_list;
87 defer_kfree_skb_list = head;
88 }
89}
90EXPORT_SYMBOL(rtnl_kfree_skbs);
91
Stephen Hemminger6756ae42006-03-20 22:23:58 -080092void __rtnl_unlock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Eric Dumazet1b5c5492016-06-13 20:21:50 -070094 struct sk_buff *head = defer_kfree_skb_list;
95
96 defer_kfree_skb_list = NULL;
97
Stephen Hemminger6756ae42006-03-20 22:23:58 -080098 mutex_unlock(&rtnl_mutex);
Eric Dumazet1b5c5492016-06-13 20:21:50 -070099
100 while (head) {
101 struct sk_buff *next = head->next;
102
103 kfree_skb(head);
104 cond_resched();
105 head = next;
106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109void rtnl_unlock(void)
110{
Herbert Xu58ec3b42008-10-07 15:50:03 -0700111 /* This fellow will unlock it for us. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 netdev_run_todo();
113}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800114EXPORT_SYMBOL(rtnl_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800116int rtnl_trylock(void)
117{
118 return mutex_trylock(&rtnl_mutex);
119}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800120EXPORT_SYMBOL(rtnl_trylock);
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800121
Patrick McHardyc9c10142008-04-23 22:10:48 -0700122int rtnl_is_locked(void)
123{
124 return mutex_is_locked(&rtnl_mutex);
125}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800126EXPORT_SYMBOL(rtnl_is_locked);
Patrick McHardyc9c10142008-04-23 22:10:48 -0700127
Vlad Buslov6f995282018-09-24 19:22:49 +0300128bool refcount_dec_and_rtnl_lock(refcount_t *r)
129{
130 return refcount_dec_and_mutex_lock(r, &rtnl_mutex);
131}
132EXPORT_SYMBOL(refcount_dec_and_rtnl_lock);
133
Paul E. McKenneya898def2010-02-22 17:04:49 -0800134#ifdef CONFIG_PROVE_LOCKING
Yaowei Bai0cbf3342015-10-08 21:29:02 +0800135bool lockdep_rtnl_is_held(void)
Paul E. McKenneya898def2010-02-22 17:04:49 -0800136{
137 return lockdep_is_held(&rtnl_mutex);
138}
139EXPORT_SYMBOL(lockdep_rtnl_is_held);
140#endif /* #ifdef CONFIG_PROVE_LOCKING */
141
Florian Westphalb0e9fe12017-12-04 22:42:30 +0100142static struct rtnl_link *__rcu *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
Thomas Grafe2849862007-03-22 11:48:11 -0700143
144static inline int rtm_msgindex(int msgtype)
145{
146 int msgindex = msgtype - RTM_BASE;
147
148 /*
149 * msgindex < 0 implies someone tried to register a netlink
150 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
151 * the message type has not been added to linux/rtnetlink.h
152 */
153 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
154
155 return msgindex;
156}
157
Florian Westphaladdf9b92017-12-02 21:44:05 +0100158static struct rtnl_link *rtnl_get_link(int protocol, int msgtype)
159{
160 struct rtnl_link **tab;
161
162 if (protocol >= ARRAY_SIZE(rtnl_msg_handlers))
163 protocol = PF_UNSPEC;
164
165 tab = rcu_dereference_rtnl(rtnl_msg_handlers[protocol]);
166 if (!tab)
167 tab = rcu_dereference_rtnl(rtnl_msg_handlers[PF_UNSPEC]);
168
169 return tab[msgtype];
170}
171
Florian Westphale4202512017-12-02 21:44:06 +0100172static int rtnl_register_internal(struct module *owner,
173 int protocol, int msgtype,
174 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
175 unsigned int flags)
Thomas Grafe2849862007-03-22 11:48:11 -0700176{
Florian Westphalb0e9fe12017-12-04 22:42:30 +0100177 struct rtnl_link *link, *old;
178 struct rtnl_link __rcu **tab;
Thomas Grafe2849862007-03-22 11:48:11 -0700179 int msgindex;
Florian Westphaladdf9b92017-12-02 21:44:05 +0100180 int ret = -ENOBUFS;
Thomas Grafe2849862007-03-22 11:48:11 -0700181
Patrick McHardy25239ce2010-04-26 16:02:05 +0200182 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700183 msgindex = rtm_msgindex(msgtype);
184
Florian Westphaladdf9b92017-12-02 21:44:05 +0100185 rtnl_lock();
186 tab = rtnl_msg_handlers[protocol];
Thomas Grafe2849862007-03-22 11:48:11 -0700187 if (tab == NULL) {
Florian Westphaladdf9b92017-12-02 21:44:05 +0100188 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(void *), GFP_KERNEL);
189 if (!tab)
190 goto unlock;
Thomas Grafe2849862007-03-22 11:48:11 -0700191
Florian Westphaladdf9b92017-12-02 21:44:05 +0100192 /* ensures we see the 0 stores */
Florian Westphal6853dd42017-08-09 20:41:51 +0200193 rcu_assign_pointer(rtnl_msg_handlers[protocol], tab);
Thomas Grafe2849862007-03-22 11:48:11 -0700194 }
195
Florian Westphaladdf9b92017-12-02 21:44:05 +0100196 old = rtnl_dereference(tab[msgindex]);
197 if (old) {
198 link = kmemdup(old, sizeof(*old), GFP_KERNEL);
199 if (!link)
200 goto unlock;
201 } else {
202 link = kzalloc(sizeof(*link), GFP_KERNEL);
203 if (!link)
204 goto unlock;
205 }
Thomas Grafe2849862007-03-22 11:48:11 -0700206
Florian Westphale4202512017-12-02 21:44:06 +0100207 WARN_ON(link->owner && link->owner != owner);
208 link->owner = owner;
209
Florian Westphaladdf9b92017-12-02 21:44:05 +0100210 WARN_ON(doit && link->doit && link->doit != doit);
211 if (doit)
212 link->doit = doit;
213 WARN_ON(dumpit && link->dumpit && link->dumpit != dumpit);
214 if (dumpit)
215 link->dumpit = dumpit;
216
217 link->flags |= flags;
218
219 /* publish protocol:msgtype */
220 rcu_assign_pointer(tab[msgindex], link);
221 ret = 0;
222 if (old)
223 kfree_rcu(old, rcu);
224unlock:
225 rtnl_unlock();
226 return ret;
Thomas Grafe2849862007-03-22 11:48:11 -0700227}
Florian Westphale4202512017-12-02 21:44:06 +0100228
229/**
230 * rtnl_register_module - Register a rtnetlink message type
231 *
232 * @owner: module registering the hook (THIS_MODULE)
233 * @protocol: Protocol family or PF_UNSPEC
234 * @msgtype: rtnetlink message type
235 * @doit: Function pointer called for each request message
236 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
237 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
238 *
239 * Like rtnl_register, but for use by removable modules.
240 */
241int rtnl_register_module(struct module *owner,
242 int protocol, int msgtype,
243 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
244 unsigned int flags)
245{
246 return rtnl_register_internal(owner, protocol, msgtype,
247 doit, dumpit, flags);
248}
249EXPORT_SYMBOL_GPL(rtnl_register_module);
250
251/**
Florian Westphal16feebc2017-12-02 21:44:08 +0100252 * rtnl_register - Register a rtnetlink message type
Florian Westphale4202512017-12-02 21:44:06 +0100253 * @protocol: Protocol family or PF_UNSPEC
254 * @msgtype: rtnetlink message type
255 * @doit: Function pointer called for each request message
256 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
257 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
258 *
259 * Registers the specified function pointers (at least one of them has
260 * to be non-NULL) to be called whenever a request message for the
261 * specified protocol family and message type is received.
262 *
263 * The special protocol family PF_UNSPEC may be used to define fallback
264 * function pointers for the case when no entry for the specific protocol
265 * family exists.
Thomas Grafe2849862007-03-22 11:48:11 -0700266 */
267void rtnl_register(int protocol, int msgtype,
Greg Rosec7ac8672011-06-10 01:27:09 +0000268 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
Florian Westphalb97bac62017-08-09 20:41:48 +0200269 unsigned int flags)
Thomas Grafe2849862007-03-22 11:48:11 -0700270{
Florian Westphal16feebc2017-12-02 21:44:08 +0100271 int err;
272
273 err = rtnl_register_internal(NULL, protocol, msgtype, doit, dumpit,
274 flags);
275 if (err)
276 pr_err("Unable to register rtnetlink message handler, "
277 "protocol = %d, message type = %d\n", protocol, msgtype);
Thomas Grafe2849862007-03-22 11:48:11 -0700278}
Thomas Grafe2849862007-03-22 11:48:11 -0700279
280/**
281 * rtnl_unregister - Unregister a rtnetlink message type
282 * @protocol: Protocol family or PF_UNSPEC
283 * @msgtype: rtnetlink message type
284 *
285 * Returns 0 on success or a negative error code.
286 */
287int rtnl_unregister(int protocol, int msgtype)
288{
Florian Westphaladdf9b92017-12-02 21:44:05 +0100289 struct rtnl_link **tab, *link;
Thomas Grafe2849862007-03-22 11:48:11 -0700290 int msgindex;
291
Patrick McHardy25239ce2010-04-26 16:02:05 +0200292 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700293 msgindex = rtm_msgindex(msgtype);
294
Florian Westphal6853dd42017-08-09 20:41:51 +0200295 rtnl_lock();
Florian Westphaladdf9b92017-12-02 21:44:05 +0100296 tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
297 if (!tab) {
Florian Westphal6853dd42017-08-09 20:41:51 +0200298 rtnl_unlock();
Thomas Grafe2849862007-03-22 11:48:11 -0700299 return -ENOENT;
Florian Westphal6853dd42017-08-09 20:41:51 +0200300 }
Thomas Grafe2849862007-03-22 11:48:11 -0700301
Florian Westphaladdf9b92017-12-02 21:44:05 +0100302 link = tab[msgindex];
303 rcu_assign_pointer(tab[msgindex], NULL);
Florian Westphal6853dd42017-08-09 20:41:51 +0200304 rtnl_unlock();
Thomas Grafe2849862007-03-22 11:48:11 -0700305
Florian Westphaladdf9b92017-12-02 21:44:05 +0100306 kfree_rcu(link, rcu);
307
Thomas Grafe2849862007-03-22 11:48:11 -0700308 return 0;
309}
Thomas Grafe2849862007-03-22 11:48:11 -0700310EXPORT_SYMBOL_GPL(rtnl_unregister);
311
312/**
313 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
314 * @protocol : Protocol family or PF_UNSPEC
315 *
316 * Identical to calling rtnl_unregster() for all registered message types
317 * of a certain protocol family.
318 */
319void rtnl_unregister_all(int protocol)
320{
Florian Westphaladdf9b92017-12-02 21:44:05 +0100321 struct rtnl_link **tab, *link;
322 int msgindex;
Florian Westphal019a3162017-08-09 20:41:49 +0200323
Patrick McHardy25239ce2010-04-26 16:02:05 +0200324 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700325
Florian Westphal019a3162017-08-09 20:41:49 +0200326 rtnl_lock();
Florian Westphaladdf9b92017-12-02 21:44:05 +0100327 tab = rtnl_msg_handlers[protocol];
Sabrina Dubrocaf707ef62018-08-28 13:40:53 +0200328 if (!tab) {
329 rtnl_unlock();
330 return;
331 }
Florian Westphal6853dd42017-08-09 20:41:51 +0200332 RCU_INIT_POINTER(rtnl_msg_handlers[protocol], NULL);
Florian Westphaladdf9b92017-12-02 21:44:05 +0100333 for (msgindex = 0; msgindex < RTM_NR_MSGTYPES; msgindex++) {
334 link = tab[msgindex];
335 if (!link)
336 continue;
337
338 rcu_assign_pointer(tab[msgindex], NULL);
339 kfree_rcu(link, rcu);
340 }
Florian Westphal019a3162017-08-09 20:41:49 +0200341 rtnl_unlock();
342
Florian Westphal6853dd42017-08-09 20:41:51 +0200343 synchronize_net();
344
Florian Westphaladdf9b92017-12-02 21:44:05 +0100345 kfree(tab);
Thomas Grafe2849862007-03-22 11:48:11 -0700346}
Thomas Grafe2849862007-03-22 11:48:11 -0700347EXPORT_SYMBOL_GPL(rtnl_unregister_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Patrick McHardy38f7b872007-06-13 12:03:51 -0700349static LIST_HEAD(link_ops);
350
Eric Dumazetc63044f2011-12-13 11:38:00 +0000351static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
352{
353 const struct rtnl_link_ops *ops;
354
355 list_for_each_entry(ops, &link_ops, list) {
356 if (!strcmp(ops->kind, kind))
357 return ops;
358 }
359 return NULL;
360}
361
Patrick McHardy38f7b872007-06-13 12:03:51 -0700362/**
363 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
364 * @ops: struct rtnl_link_ops * to register
365 *
366 * The caller must hold the rtnl_mutex. This function should be used
367 * by drivers that create devices during module initialization. It
368 * must be called before registering the devices.
369 *
370 * Returns 0 on success or a negative error code.
371 */
372int __rtnl_link_register(struct rtnl_link_ops *ops)
373{
Eric Dumazetc63044f2011-12-13 11:38:00 +0000374 if (rtnl_link_ops_get(ops->kind))
375 return -EEXIST;
376
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +0200377 /* The check for setup is here because if ops
378 * does not have that filled up, it is not possible
379 * to use the ops for creating device. So do not
380 * fill up dellink as well. That disables rtnl_dellink.
381 */
382 if (ops->setup && !ops->dellink)
Eric Dumazet23289a32009-10-27 07:06:36 +0000383 ops->dellink = unregister_netdevice_queue;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700384
Patrick McHardy38f7b872007-06-13 12:03:51 -0700385 list_add_tail(&ops->list, &link_ops);
386 return 0;
387}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700388EXPORT_SYMBOL_GPL(__rtnl_link_register);
389
390/**
391 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
392 * @ops: struct rtnl_link_ops * to register
393 *
394 * Returns 0 on success or a negative error code.
395 */
396int rtnl_link_register(struct rtnl_link_ops *ops)
397{
398 int err;
399
Kees Cookccf8dbc2018-05-30 15:20:52 -0700400 /* Sanity-check max sizes to avoid stack buffer overflow. */
401 if (WARN_ON(ops->maxtype > RTNL_MAX_TYPE ||
402 ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE))
403 return -EINVAL;
404
Patrick McHardy38f7b872007-06-13 12:03:51 -0700405 rtnl_lock();
406 err = __rtnl_link_register(ops);
407 rtnl_unlock();
408 return err;
409}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700410EXPORT_SYMBOL_GPL(rtnl_link_register);
411
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700412static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
413{
414 struct net_device *dev;
Eric Dumazet23289a32009-10-27 07:06:36 +0000415 LIST_HEAD(list_kill);
416
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700417 for_each_netdev(net, dev) {
Eric Dumazet23289a32009-10-27 07:06:36 +0000418 if (dev->rtnl_link_ops == ops)
419 ops->dellink(dev, &list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700420 }
Eric Dumazet23289a32009-10-27 07:06:36 +0000421 unregister_netdevice_many(&list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700422}
423
Patrick McHardy38f7b872007-06-13 12:03:51 -0700424/**
425 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
426 * @ops: struct rtnl_link_ops * to unregister
427 *
Kirill Tkhai554873e2018-03-30 19:38:37 +0300428 * The caller must hold the rtnl_mutex and guarantee net_namespace_list
429 * integrity (hold pernet_ops_rwsem for writing to close the race
430 * with setup_net() and cleanup_net()).
Patrick McHardy38f7b872007-06-13 12:03:51 -0700431 */
432void __rtnl_link_unregister(struct rtnl_link_ops *ops)
433{
Eric W. Biederman881d9662007-09-17 11:56:21 -0700434 struct net *net;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700435
Eric W. Biederman881d9662007-09-17 11:56:21 -0700436 for_each_net(net) {
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700437 __rtnl_kill_links(net, ops);
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700438 }
Patrick McHardy38f7b872007-06-13 12:03:51 -0700439 list_del(&ops->list);
440}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700441EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
442
Cong Wang200b9162014-05-12 15:11:20 -0700443/* Return with the rtnl_lock held when there are no network
444 * devices unregistering in any network namespace.
445 */
446static void rtnl_lock_unregistering_all(void)
447{
448 struct net *net;
449 bool unregistering;
Peter Zijlstraff960a72014-10-29 17:04:56 +0100450 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Cong Wang200b9162014-05-12 15:11:20 -0700451
Peter Zijlstraff960a72014-10-29 17:04:56 +0100452 add_wait_queue(&netdev_unregistering_wq, &wait);
Cong Wang200b9162014-05-12 15:11:20 -0700453 for (;;) {
Cong Wang200b9162014-05-12 15:11:20 -0700454 unregistering = false;
455 rtnl_lock();
Kirill Tkhaif0b07bb12018-03-29 19:20:32 +0300456 /* We held write locked pernet_ops_rwsem, and parallel
457 * setup_net() and cleanup_net() are not possible.
458 */
Cong Wang200b9162014-05-12 15:11:20 -0700459 for_each_net(net) {
460 if (net->dev_unreg_count > 0) {
461 unregistering = true;
462 break;
463 }
464 }
465 if (!unregistering)
466 break;
467 __rtnl_unlock();
Peter Zijlstraff960a72014-10-29 17:04:56 +0100468
469 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Cong Wang200b9162014-05-12 15:11:20 -0700470 }
Peter Zijlstraff960a72014-10-29 17:04:56 +0100471 remove_wait_queue(&netdev_unregistering_wq, &wait);
Cong Wang200b9162014-05-12 15:11:20 -0700472}
473
Patrick McHardy38f7b872007-06-13 12:03:51 -0700474/**
475 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
476 * @ops: struct rtnl_link_ops * to unregister
477 */
478void rtnl_link_unregister(struct rtnl_link_ops *ops)
479{
Kirill Tkhai8518e9b2018-03-27 18:02:32 +0300480 /* Close the race with setup_net() and cleanup_net() */
Kirill Tkhai4420bf22018-03-27 18:02:23 +0300481 down_write(&pernet_ops_rwsem);
Cong Wang200b9162014-05-12 15:11:20 -0700482 rtnl_lock_unregistering_all();
Patrick McHardy38f7b872007-06-13 12:03:51 -0700483 __rtnl_link_unregister(ops);
484 rtnl_unlock();
Kirill Tkhai4420bf22018-03-27 18:02:23 +0300485 up_write(&pernet_ops_rwsem);
Patrick McHardy38f7b872007-06-13 12:03:51 -0700486}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700487EXPORT_SYMBOL_GPL(rtnl_link_unregister);
488
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100489static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
490{
491 struct net_device *master_dev;
492 const struct rtnl_link_ops *ops;
Florian Westphal8515ae32017-08-10 16:52:59 +0200493 size_t size = 0;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100494
Florian Westphal8515ae32017-08-10 16:52:59 +0200495 rcu_read_lock();
496
497 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100498 if (!master_dev)
Florian Westphal8515ae32017-08-10 16:52:59 +0200499 goto out;
500
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100501 ops = master_dev->rtnl_link_ops;
Fernando Luis Vazquez Cao6049f252014-02-04 19:35:02 +0900502 if (!ops || !ops->get_slave_size)
Florian Westphal8515ae32017-08-10 16:52:59 +0200503 goto out;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100504 /* IFLA_INFO_SLAVE_DATA + nested data */
Florian Westphal8515ae32017-08-10 16:52:59 +0200505 size = nla_total_size(sizeof(struct nlattr)) +
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100506 ops->get_slave_size(master_dev, dev);
Florian Westphal8515ae32017-08-10 16:52:59 +0200507
508out:
509 rcu_read_unlock();
510 return size;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100511}
512
Patrick McHardy38f7b872007-06-13 12:03:51 -0700513static size_t rtnl_link_get_size(const struct net_device *dev)
514{
515 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
516 size_t size;
517
518 if (!ops)
519 return 0;
520
Thomas Graf369cf772010-11-11 15:47:59 +0000521 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
522 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700523
524 if (ops->get_size)
525 /* IFLA_INFO_DATA + nested data */
Thomas Graf369cf772010-11-11 15:47:59 +0000526 size += nla_total_size(sizeof(struct nlattr)) +
Patrick McHardy38f7b872007-06-13 12:03:51 -0700527 ops->get_size(dev);
528
529 if (ops->get_xstats_size)
Thomas Graf369cf772010-11-11 15:47:59 +0000530 /* IFLA_INFO_XSTATS */
531 size += nla_total_size(ops->get_xstats_size(dev));
Patrick McHardy38f7b872007-06-13 12:03:51 -0700532
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100533 size += rtnl_link_get_slave_info_data_size(dev);
534
Patrick McHardy38f7b872007-06-13 12:03:51 -0700535 return size;
536}
537
Thomas Graff8ff1822010-11-16 04:30:14 +0000538static LIST_HEAD(rtnl_af_ops);
539
540static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
541{
542 const struct rtnl_af_ops *ops;
543
Florian Westphal5fa85a02017-10-16 15:44:36 +0200544 list_for_each_entry_rcu(ops, &rtnl_af_ops, list) {
Thomas Graff8ff1822010-11-16 04:30:14 +0000545 if (ops->family == family)
546 return ops;
547 }
548
549 return NULL;
550}
551
552/**
Thomas Graff8ff1822010-11-16 04:30:14 +0000553 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
554 * @ops: struct rtnl_af_ops * to register
555 *
556 * Returns 0 on success or a negative error code.
557 */
stephen hemminger3678a9d2013-12-30 10:41:32 -0800558void rtnl_af_register(struct rtnl_af_ops *ops)
Thomas Graff8ff1822010-11-16 04:30:14 +0000559{
Thomas Graff8ff1822010-11-16 04:30:14 +0000560 rtnl_lock();
Florian Westphal5fa85a02017-10-16 15:44:36 +0200561 list_add_tail_rcu(&ops->list, &rtnl_af_ops);
Thomas Graff8ff1822010-11-16 04:30:14 +0000562 rtnl_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +0000563}
564EXPORT_SYMBOL_GPL(rtnl_af_register);
565
566/**
Thomas Graff8ff1822010-11-16 04:30:14 +0000567 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
568 * @ops: struct rtnl_af_ops * to unregister
569 */
570void rtnl_af_unregister(struct rtnl_af_ops *ops)
571{
572 rtnl_lock();
Florian Westphal5fa85a02017-10-16 15:44:36 +0200573 list_del_rcu(&ops->list);
Thomas Graff8ff1822010-11-16 04:30:14 +0000574 rtnl_unlock();
Florian Westphal5fa85a02017-10-16 15:44:36 +0200575
576 synchronize_rcu();
Thomas Graff8ff1822010-11-16 04:30:14 +0000577}
578EXPORT_SYMBOL_GPL(rtnl_af_unregister);
579
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700580static size_t rtnl_link_get_af_size(const struct net_device *dev,
581 u32 ext_filter_mask)
Thomas Graff8ff1822010-11-16 04:30:14 +0000582{
583 struct rtnl_af_ops *af_ops;
584 size_t size;
585
586 /* IFLA_AF_SPEC */
587 size = nla_total_size(sizeof(struct nlattr));
588
Florian Westphal5fa85a02017-10-16 15:44:36 +0200589 rcu_read_lock();
590 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Thomas Graff8ff1822010-11-16 04:30:14 +0000591 if (af_ops->get_link_af_size) {
592 /* AF_* + nested data */
593 size += nla_total_size(sizeof(struct nlattr)) +
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700594 af_ops->get_link_af_size(dev, ext_filter_mask);
Thomas Graff8ff1822010-11-16 04:30:14 +0000595 }
596 }
Florian Westphal5fa85a02017-10-16 15:44:36 +0200597 rcu_read_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +0000598
599 return size;
600}
601
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100602static bool rtnl_have_link_slave_info(const struct net_device *dev)
603{
604 struct net_device *master_dev;
Florian Westphal4c82a952017-09-26 13:58:43 +0200605 bool ret = false;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100606
Florian Westphal4c82a952017-09-26 13:58:43 +0200607 rcu_read_lock();
608
609 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
Jiri Pirko813f0202014-01-23 19:19:21 +0100610 if (master_dev && master_dev->rtnl_link_ops)
Florian Westphal4c82a952017-09-26 13:58:43 +0200611 ret = true;
612 rcu_read_unlock();
613 return ret;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100614}
615
616static int rtnl_link_slave_info_fill(struct sk_buff *skb,
617 const struct net_device *dev)
618{
619 struct net_device *master_dev;
620 const struct rtnl_link_ops *ops;
621 struct nlattr *slave_data;
622 int err;
623
624 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
625 if (!master_dev)
626 return 0;
627 ops = master_dev->rtnl_link_ops;
628 if (!ops)
629 return 0;
630 if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
631 return -EMSGSIZE;
632 if (ops->fill_slave_info) {
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200633 slave_data = nla_nest_start_noflag(skb, IFLA_INFO_SLAVE_DATA);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100634 if (!slave_data)
635 return -EMSGSIZE;
636 err = ops->fill_slave_info(skb, master_dev, dev);
637 if (err < 0)
638 goto err_cancel_slave_data;
639 nla_nest_end(skb, slave_data);
640 }
641 return 0;
642
643err_cancel_slave_data:
644 nla_nest_cancel(skb, slave_data);
645 return err;
646}
647
648static int rtnl_link_info_fill(struct sk_buff *skb,
649 const struct net_device *dev)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700650{
651 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100652 struct nlattr *data;
653 int err;
654
655 if (!ops)
656 return 0;
657 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
658 return -EMSGSIZE;
659 if (ops->fill_xstats) {
660 err = ops->fill_xstats(skb, dev);
661 if (err < 0)
662 return err;
663 }
664 if (ops->fill_info) {
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200665 data = nla_nest_start_noflag(skb, IFLA_INFO_DATA);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100666 if (data == NULL)
667 return -EMSGSIZE;
668 err = ops->fill_info(skb, dev);
669 if (err < 0)
670 goto err_cancel_data;
671 nla_nest_end(skb, data);
672 }
673 return 0;
674
675err_cancel_data:
676 nla_nest_cancel(skb, data);
677 return err;
678}
679
680static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
681{
682 struct nlattr *linkinfo;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700683 int err = -EMSGSIZE;
684
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200685 linkinfo = nla_nest_start_noflag(skb, IFLA_LINKINFO);
Patrick McHardy38f7b872007-06-13 12:03:51 -0700686 if (linkinfo == NULL)
687 goto out;
688
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100689 err = rtnl_link_info_fill(skb, dev);
690 if (err < 0)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700691 goto err_cancel_link;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100692
693 err = rtnl_link_slave_info_fill(skb, dev);
694 if (err < 0)
695 goto err_cancel_link;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700696
697 nla_nest_end(skb, linkinfo);
698 return 0;
699
Patrick McHardy38f7b872007-06-13 12:03:51 -0700700err_cancel_link:
701 nla_nest_cancel(skb, linkinfo);
702out:
703 return err;
704}
705
Eric Dumazet95c96172012-04-15 05:58:06 +0000706int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800708 struct sock *rtnl = net->rtnl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 int err = 0;
710
Patrick McHardyac6d4392005-08-14 19:29:52 -0700711 NETLINK_CB(skb).dst_group = group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if (echo)
Reshetova, Elena63354792017-06-30 13:07:58 +0300713 refcount_inc(&skb->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
715 if (echo)
716 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
717 return err;
718}
719
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800720int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
Thomas Graf2942e902006-08-15 00:30:25 -0700721{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800722 struct sock *rtnl = net->rtnl;
723
Thomas Graf2942e902006-08-15 00:30:25 -0700724 return nlmsg_unicast(rtnl, skb, pid);
725}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800726EXPORT_SYMBOL(rtnl_unicast);
Thomas Graf2942e902006-08-15 00:30:25 -0700727
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800728void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
729 struct nlmsghdr *nlh, gfp_t flags)
Thomas Graf97676b62006-08-15 00:31:41 -0700730{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800731 struct sock *rtnl = net->rtnl;
Thomas Graf97676b62006-08-15 00:31:41 -0700732 int report = 0;
733
734 if (nlh)
735 report = nlmsg_report(nlh);
736
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800737 nlmsg_notify(rtnl, skb, pid, group, report, flags);
Thomas Graf97676b62006-08-15 00:31:41 -0700738}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800739EXPORT_SYMBOL(rtnl_notify);
Thomas Graf97676b62006-08-15 00:31:41 -0700740
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800741void rtnl_set_sk_err(struct net *net, u32 group, int error)
Thomas Graf97676b62006-08-15 00:31:41 -0700742{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800743 struct sock *rtnl = net->rtnl;
744
Thomas Graf97676b62006-08-15 00:31:41 -0700745 netlink_set_err(rtnl, 0, group, error);
746}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800747EXPORT_SYMBOL(rtnl_set_sk_err);
Thomas Graf97676b62006-08-15 00:31:41 -0700748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
750{
Thomas Graf2d7202b2006-08-22 00:01:27 -0700751 struct nlattr *mx;
752 int i, valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
David Ahernc22a1332019-06-21 16:27:16 -0700754 /* nothing is dumped for dst_default_metrics, so just skip the loop */
755 if (metrics == dst_default_metrics.metrics)
756 return 0;
757
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200758 mx = nla_nest_start_noflag(skb, RTA_METRICS);
Thomas Graf2d7202b2006-08-22 00:01:27 -0700759 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 ||
Andrew Lunneec517cd2020-04-20 00:11:50 +0200832 operstate == IF_OPER_TESTING ||
Stefan Rompfb00055a2006-03-20 17:09:11 -0800833 operstate == IF_OPER_UNKNOWN) &&
Andrew Lunneec517cd2020-04-20 00:11:50 +0200834 !netif_dormant(dev) && !netif_testing(dev))
Stefan Rompfb00055a2006-03-20 17:09:11 -0800835 operstate = IF_OPER_UP;
836 break;
837
Andrew Lunneec517cd2020-04-20 00:11:50 +0200838 case IF_OPER_TESTING:
839 if (operstate == IF_OPER_UP ||
840 operstate == IF_OPER_UNKNOWN)
841 operstate = IF_OPER_TESTING;
842 break;
843
Stefan Rompfb00055a2006-03-20 17:09:11 -0800844 case IF_OPER_DORMANT:
845 if (operstate == IF_OPER_UP ||
846 operstate == IF_OPER_UNKNOWN)
847 operstate = IF_OPER_DORMANT;
848 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700849 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800850
851 if (dev->operstate != operstate) {
852 write_lock_bh(&dev_base_lock);
853 dev->operstate = operstate;
854 write_unlock_bh(&dev_base_lock);
David S. Miller93b2d4a2008-02-17 18:35:07 -0800855 netdev_state_change(dev);
856 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800857}
858
Jiri Bencb1beb682012-07-27 02:58:22 +0000859static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
860{
861 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
862 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
863}
864
Patrick McHardy3729d502010-02-26 06:34:54 +0000865static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
866 const struct ifinfomsg *ifm)
867{
868 unsigned int flags = ifm->ifi_flags;
869
870 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
871 if (ifm->ifi_change)
872 flags = (flags & ifm->ifi_change) |
Jiri Bencb1beb682012-07-27 02:58:22 +0000873 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
Patrick McHardy3729d502010-02-26 06:34:54 +0000874
875 return flags;
876}
877
Thomas Grafb60c5112006-08-04 23:05:34 -0700878static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000879 const struct rtnl_link_stats64 *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
Thomas Grafb60c5112006-08-04 23:05:34 -0700881 a->rx_packets = b->rx_packets;
882 a->tx_packets = b->tx_packets;
883 a->rx_bytes = b->rx_bytes;
884 a->tx_bytes = b->tx_bytes;
885 a->rx_errors = b->rx_errors;
886 a->tx_errors = b->tx_errors;
887 a->rx_dropped = b->rx_dropped;
888 a->tx_dropped = b->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Thomas Grafb60c5112006-08-04 23:05:34 -0700890 a->multicast = b->multicast;
891 a->collisions = b->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Thomas Grafb60c5112006-08-04 23:05:34 -0700893 a->rx_length_errors = b->rx_length_errors;
894 a->rx_over_errors = b->rx_over_errors;
895 a->rx_crc_errors = b->rx_crc_errors;
896 a->rx_frame_errors = b->rx_frame_errors;
897 a->rx_fifo_errors = b->rx_fifo_errors;
898 a->rx_missed_errors = b->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Thomas Grafb60c5112006-08-04 23:05:34 -0700900 a->tx_aborted_errors = b->tx_aborted_errors;
901 a->tx_carrier_errors = b->tx_carrier_errors;
902 a->tx_fifo_errors = b->tx_fifo_errors;
903 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
904 a->tx_window_errors = b->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Thomas Grafb60c5112006-08-04 23:05:34 -0700906 a->rx_compressed = b->rx_compressed;
907 a->tx_compressed = b->tx_compressed;
Jarod Wilson6e7333d2016-02-01 18:51:05 -0500908
909 a->rx_nohandler = b->rx_nohandler;
Jan Engelhardt10708f32010-03-11 09:57:29 +0000910}
911
Chris Wrightc02db8c2010-05-16 01:05:45 -0700912/* All VF info */
Greg Rose115c9b82012-02-21 16:54:48 -0500913static inline int rtnl_vfinfo_size(const struct net_device *dev,
914 u32 ext_filter_mask)
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000915{
Phil Sutter9af15c32017-01-18 14:04:39 +0100916 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
Chris Wrightc02db8c2010-05-16 01:05:45 -0700917 int num_vfs = dev_num_vf(dev->dev.parent);
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100918 size_t size = nla_total_size(0);
Scott Feldman045de012010-05-28 03:42:43 -0700919 size += num_vfs *
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100920 (nla_total_size(0) +
921 nla_total_size(sizeof(struct ifla_vf_mac)) +
Denis Kirjanov75345f882019-06-17 10:53:41 +0200922 nla_total_size(sizeof(struct ifla_vf_broadcast)) +
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100923 nla_total_size(sizeof(struct ifla_vf_vlan)) +
924 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
Moshe Shemesh79aab092016-09-22 12:11:15 +0300925 nla_total_size(MAX_VLAN_LIST_LEN *
926 sizeof(struct ifla_vf_vlan_info)) +
Sucheta Chakrabortyed616682014-05-22 09:59:05 -0400927 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100928 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
Jiri Benc945a3672014-08-08 16:44:32 +0200929 nla_total_size(sizeof(struct ifla_vf_rate)) +
Vlad Zolotarov01a3d792015-03-30 21:35:23 +0300930 nla_total_size(sizeof(struct ifla_vf_link_state)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300931 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100932 nla_total_size(0) + /* nest IFLA_VF_STATS */
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300933 /* IFLA_VF_STATS_RX_PACKETS */
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_TX_PACKETS */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200936 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300937 /* IFLA_VF_STATS_RX_BYTES */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200938 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300939 /* IFLA_VF_STATS_TX_BYTES */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200940 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300941 /* IFLA_VF_STATS_BROADCAST */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200942 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300943 /* IFLA_VF_STATS_MULTICAST */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200944 nla_total_size_64bit(sizeof(__u64)) +
Eugenia Emantayevc5a9f6f2017-07-17 13:47:07 +0300945 /* IFLA_VF_STATS_RX_DROPPED */
946 nla_total_size_64bit(sizeof(__u64)) +
947 /* IFLA_VF_STATS_TX_DROPPED */
948 nla_total_size_64bit(sizeof(__u64)) +
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +0000949 nla_total_size(sizeof(struct ifla_vf_trust)));
Chris Wrightc02db8c2010-05-16 01:05:45 -0700950 return size;
951 } else
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000952 return 0;
953}
954
David Gibsonc53864f2014-04-24 10:22:36 +1000955static size_t rtnl_port_size(const struct net_device *dev,
956 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -0700957{
958 size_t port_size = nla_total_size(4) /* PORT_VF */
959 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
Scott Feldman57b61082010-05-17 22:49:55 -0700960 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
961 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
962 + nla_total_size(1) /* PROT_VDP_REQUEST */
963 + nla_total_size(2); /* PORT_VDP_RESPONSE */
964 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
965 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
966 + port_size;
967 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
968 + port_size;
969
David Gibsonc53864f2014-04-24 10:22:36 +1000970 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
971 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -0700972 return 0;
973 if (dev_num_vf(dev->dev.parent))
974 return port_self_size + vf_ports_size +
975 vf_port_size * dev_num_vf(dev->dev.parent);
976 else
977 return port_self_size;
978}
979
David S. Millerb5cdae32017-04-18 15:36:58 -0400980static size_t rtnl_xdp_size(void)
Brenden Blancod1fdd912016-07-19 12:16:49 -0700981{
Sabrina Dubrocab3cfaa32016-11-15 11:16:35 +0100982 size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */
Martin KaFai Lau58038692017-06-15 17:29:09 -0700983 nla_total_size(1) + /* XDP_ATTACHED */
Jakub Kicinskia25717d2018-07-11 20:36:41 -0700984 nla_total_size(4) + /* XDP_PROG_ID (or 1st mode) */
Jakub Kicinski4f91da22018-07-11 20:36:38 -0700985 nla_total_size(4); /* XDP_<mode>_PROG_ID */
Brenden Blancod1fdd912016-07-19 12:16:49 -0700986
David S. Millerb5cdae32017-04-18 15:36:58 -0400987 return xdp_size;
Brenden Blancod1fdd912016-07-19 12:16:49 -0700988}
989
Jiri Pirko88f4fb02019-09-30 11:48:17 +0200990static size_t rtnl_prop_list_size(const struct net_device *dev)
991{
992 struct netdev_name_node *name_node;
993 size_t size;
994
995 if (list_empty(&dev->name_node->list))
996 return 0;
997 size = nla_total_size(0);
998 list_for_each_entry(name_node, &dev->name_node->list, list)
999 size += nla_total_size(ALTIFNAMSIZ);
1000 return size;
1001}
1002
Roopa Prabhu829eb202020-07-31 17:34:01 -07001003static size_t rtnl_proto_down_size(const struct net_device *dev)
1004{
1005 size_t size = nla_total_size(1);
1006
1007 if (dev->proto_down_reason)
1008 size += nla_total_size(0) + nla_total_size(4);
1009
1010 return size;
1011}
1012
Greg Rose115c9b82012-02-21 16:54:48 -05001013static noinline size_t if_nlmsg_size(const struct net_device *dev,
1014 u32 ext_filter_mask)
Thomas Graf339bf982006-11-10 14:10:15 -08001015{
1016 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
1017 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001018 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
Thomas Graf339bf982006-11-10 14:10:15 -08001019 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
Nicolas Dichtel270cb4d2016-04-26 10:06:16 +02001020 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
Thomas Graf339bf982006-11-10 14:10:15 -08001021 + nla_total_size(sizeof(struct rtnl_link_stats))
David S. Miller35c58452016-04-19 19:49:29 -04001022 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
Thomas Graf339bf982006-11-10 14:10:15 -08001023 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
1024 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
1025 + nla_total_size(4) /* IFLA_TXQLEN */
1026 + nla_total_size(4) /* IFLA_WEIGHT */
1027 + nla_total_size(4) /* IFLA_MTU */
1028 + nla_total_size(4) /* IFLA_LINK */
1029 + nla_total_size(4) /* IFLA_MASTER */
Jiri Pirko9a572472012-12-27 23:49:39 +00001030 + nla_total_size(1) /* IFLA_CARRIER */
Ben Greearedbc0bb2012-03-29 12:51:30 +00001031 + nla_total_size(4) /* IFLA_PROMISCUITY */
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001032 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
1033 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
Tobias Klauser69197562016-11-30 14:30:37 +01001034 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
1035 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
Thomas Graf339bf982006-11-10 14:10:15 -08001036 + nla_total_size(1) /* IFLA_OPERSTATE */
Patrick McHardy38f7b872007-06-13 12:03:51 -07001037 + nla_total_size(1) /* IFLA_LINKMODE */
david decotigny2d3b4792014-03-29 09:48:35 -07001038 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001039 + nla_total_size(4) /* IFLA_LINK_NETNSID */
Serhey Popovychdb833d42017-06-20 14:35:23 +03001040 + nla_total_size(4) /* IFLA_GROUP */
Greg Rose115c9b82012-02-21 16:54:48 -05001041 + nla_total_size(ext_filter_mask
1042 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
1043 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
David Gibsonc53864f2014-04-24 10:22:36 +10001044 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
Thomas Graff8ff1822010-11-16 04:30:14 +00001045 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
Arad, Ronenb1974ed2015-10-19 09:23:28 -07001046 + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
Jiri Pirko82f28412014-11-28 14:34:18 +01001047 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001048 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
Nicolas Dichtelc57c7a92016-03-31 18:10:31 +02001049 + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
David S. Millerb5cdae32017-04-18 15:36:58 -04001050 + rtnl_xdp_size() /* IFLA_XDP */
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001051 + nla_total_size(4) /* IFLA_EVENT */
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02001052 + nla_total_size(4) /* IFLA_NEW_NETNSID */
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01001053 + nla_total_size(4) /* IFLA_NEW_IFINDEX */
Roopa Prabhu829eb202020-07-31 17:34:01 -07001054 + rtnl_proto_down_size(dev) /* proto down */
Christian Brauner7e4a8d52018-09-04 21:53:53 +02001055 + nla_total_size(4) /* IFLA_TARGET_NETNSID */
David Decotignyb2d3bcf2018-01-18 09:59:13 -08001056 + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */
1057 + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */
Stephen Hemminger3e7a50c2018-07-27 13:43:22 -07001058 + nla_total_size(4) /* IFLA_MIN_MTU */
1059 + nla_total_size(4) /* IFLA_MAX_MTU */
Jiri Pirko88f4fb02019-09-30 11:48:17 +02001060 + rtnl_prop_list_size(dev)
Michal Kubecekf74877a2019-12-11 10:58:14 +01001061 + nla_total_size(MAX_ADDR_LEN) /* IFLA_PERM_ADDRESS */
Jiri Benc79e1ad12017-11-02 17:04:38 -02001062 + 0;
Thomas Graf339bf982006-11-10 14:10:15 -08001063}
1064
Scott Feldman57b61082010-05-17 22:49:55 -07001065static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
1066{
1067 struct nlattr *vf_ports;
1068 struct nlattr *vf_port;
1069 int vf;
1070 int err;
1071
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001072 vf_ports = nla_nest_start_noflag(skb, IFLA_VF_PORTS);
Scott Feldman57b61082010-05-17 22:49:55 -07001073 if (!vf_ports)
1074 return -EMSGSIZE;
1075
1076 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001077 vf_port = nla_nest_start_noflag(skb, IFLA_VF_PORT);
Scott Feldman8ca94182010-05-28 03:42:18 -07001078 if (!vf_port)
1079 goto nla_put_failure;
David S. Millera6574342012-04-01 20:12:00 -04001080 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
1081 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001082 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
Scott Feldman8ca94182010-05-28 03:42:18 -07001083 if (err == -EMSGSIZE)
1084 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001085 if (err) {
Scott Feldman57b61082010-05-17 22:49:55 -07001086 nla_nest_cancel(skb, vf_port);
1087 continue;
1088 }
1089 nla_nest_end(skb, vf_port);
1090 }
1091
1092 nla_nest_end(skb, vf_ports);
1093
1094 return 0;
Scott Feldman8ca94182010-05-28 03:42:18 -07001095
1096nla_put_failure:
1097 nla_nest_cancel(skb, vf_ports);
1098 return -EMSGSIZE;
Scott Feldman57b61082010-05-17 22:49:55 -07001099}
1100
1101static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
1102{
1103 struct nlattr *port_self;
1104 int err;
1105
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001106 port_self = nla_nest_start_noflag(skb, IFLA_PORT_SELF);
Scott Feldman57b61082010-05-17 22:49:55 -07001107 if (!port_self)
1108 return -EMSGSIZE;
1109
1110 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
1111 if (err) {
1112 nla_nest_cancel(skb, port_self);
Scott Feldman8ca94182010-05-28 03:42:18 -07001113 return (err == -EMSGSIZE) ? err : 0;
Scott Feldman57b61082010-05-17 22:49:55 -07001114 }
1115
1116 nla_nest_end(skb, port_self);
1117
1118 return 0;
1119}
1120
David Gibsonc53864f2014-04-24 10:22:36 +10001121static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1122 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -07001123{
1124 int err;
1125
David Gibsonc53864f2014-04-24 10:22:36 +10001126 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1127 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -07001128 return 0;
1129
1130 err = rtnl_port_self_fill(skb, dev);
1131 if (err)
1132 return err;
1133
1134 if (dev_num_vf(dev->dev.parent)) {
1135 err = rtnl_vf_ports_fill(skb, dev);
1136 if (err)
1137 return err;
1138 }
1139
1140 return 0;
1141}
1142
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001143static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
1144{
1145 int err;
Jiri Pirko02637fc2014-11-28 14:34:16 +01001146 struct netdev_phys_item_id ppid;
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001147
1148 err = dev_get_phys_port_id(dev, &ppid);
1149 if (err) {
1150 if (err == -EOPNOTSUPP)
1151 return 0;
1152 return err;
1153 }
1154
1155 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1156 return -EMSGSIZE;
1157
1158 return 0;
1159}
1160
David Aherndb24a902015-03-17 20:23:15 -06001161static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1162{
1163 char name[IFNAMSIZ];
1164 int err;
1165
1166 err = dev_get_phys_port_name(dev, name, sizeof(name));
1167 if (err) {
1168 if (err == -EOPNOTSUPP)
1169 return 0;
1170 return err;
1171 }
1172
Michal Schmidt77ef0332017-05-04 16:48:58 +02001173 if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
David Aherndb24a902015-03-17 20:23:15 -06001174 return -EMSGSIZE;
1175
1176 return 0;
1177}
1178
Jiri Pirko82f28412014-11-28 14:34:18 +01001179static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1180{
Florian Fainellibccb3022019-02-06 09:45:46 -08001181 struct netdev_phys_item_id ppid = { };
Jiri Pirko82f28412014-11-28 14:34:18 +01001182 int err;
Jiri Pirko82f28412014-11-28 14:34:18 +01001183
Florian Fainellibccb3022019-02-06 09:45:46 -08001184 err = dev_get_port_parent_id(dev, &ppid, false);
Jiri Pirko82f28412014-11-28 14:34:18 +01001185 if (err) {
1186 if (err == -EOPNOTSUPP)
1187 return 0;
1188 return err;
1189 }
1190
Florian Fainellibccb3022019-02-06 09:45:46 -08001191 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, ppid.id_len, ppid.id))
Jiri Pirko82f28412014-11-28 14:34:18 +01001192 return -EMSGSIZE;
1193
1194 return 0;
1195}
1196
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001197static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1198 struct net_device *dev)
1199{
Roopa Prabhu550bce52016-04-15 20:36:25 -07001200 struct rtnl_link_stats64 *sp;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001201 struct nlattr *attr;
1202
Nicolas Dichtel58414d32016-04-21 18:58:25 +02001203 attr = nla_reserve_64bit(skb, IFLA_STATS64,
1204 sizeof(struct rtnl_link_stats64), IFLA_PAD);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001205 if (!attr)
1206 return -EMSGSIZE;
1207
Roopa Prabhu550bce52016-04-15 20:36:25 -07001208 sp = nla_data(attr);
1209 dev_get_stats(dev, sp);
1210
1211 attr = nla_reserve(skb, IFLA_STATS,
1212 sizeof(struct rtnl_link_stats));
1213 if (!attr)
1214 return -EMSGSIZE;
1215
1216 copy_rtnl_link_stats(nla_data(attr), sp);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001217
1218 return 0;
1219}
1220
1221static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1222 struct net_device *dev,
1223 int vfs_num,
1224 struct nlattr *vfinfo)
1225{
1226 struct ifla_vf_rss_query_en vf_rss_query_en;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001227 struct nlattr *vf, *vfstats, *vfvlanlist;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001228 struct ifla_vf_link_state vf_linkstate;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001229 struct ifla_vf_vlan_info vf_vlan_info;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001230 struct ifla_vf_spoofchk vf_spoofchk;
1231 struct ifla_vf_tx_rate vf_tx_rate;
1232 struct ifla_vf_stats vf_stats;
1233 struct ifla_vf_trust vf_trust;
1234 struct ifla_vf_vlan vf_vlan;
1235 struct ifla_vf_rate vf_rate;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001236 struct ifla_vf_mac vf_mac;
Denis Kirjanov75345f882019-06-17 10:53:41 +02001237 struct ifla_vf_broadcast vf_broadcast;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001238 struct ifla_vf_info ivi;
Danit Goldberg30aad412019-11-06 15:30:07 +02001239 struct ifla_vf_guid node_guid;
1240 struct ifla_vf_guid port_guid;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001241
Mintz, Yuval0eed9cf2017-06-07 21:00:33 +03001242 memset(&ivi, 0, sizeof(ivi));
1243
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001244 /* Not all SR-IOV capable drivers support the
1245 * spoofcheck and "RSS query enable" query. Preset to
1246 * -1 so the user space tool can detect that the driver
1247 * didn't report anything.
1248 */
1249 ivi.spoofchk = -1;
1250 ivi.rss_query_en = -1;
1251 ivi.trusted = -1;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001252 /* The default value for VF link state is "auto"
1253 * IFLA_VF_LINK_STATE_AUTO which equals zero
1254 */
1255 ivi.linkstate = 0;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001256 /* VLAN Protocol by default is 802.1Q */
1257 ivi.vlan_proto = htons(ETH_P_8021Q);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001258 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1259 return 0;
1260
Dan Carpenter775f4f02016-10-13 11:45:28 +03001261 memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
Leon Romanovsky9fbf0822020-01-30 14:59:49 +02001262 memset(&node_guid, 0, sizeof(node_guid));
1263 memset(&port_guid, 0, sizeof(port_guid));
Dan Carpenter775f4f02016-10-13 11:45:28 +03001264
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001265 vf_mac.vf =
1266 vf_vlan.vf =
Moshe Shemesh79aab092016-09-22 12:11:15 +03001267 vf_vlan_info.vf =
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001268 vf_rate.vf =
1269 vf_tx_rate.vf =
1270 vf_spoofchk.vf =
1271 vf_linkstate.vf =
1272 vf_rss_query_en.vf =
Danit Goldberg9aed6ae2019-12-03 17:43:36 +02001273 vf_trust.vf =
1274 node_guid.vf =
1275 port_guid.vf = ivi.vf;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001276
1277 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
Denis Kirjanov75345f882019-06-17 10:53:41 +02001278 memcpy(vf_broadcast.broadcast, dev->broadcast, dev->addr_len);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001279 vf_vlan.vlan = ivi.vlan;
1280 vf_vlan.qos = ivi.qos;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001281 vf_vlan_info.vlan = ivi.vlan;
1282 vf_vlan_info.qos = ivi.qos;
1283 vf_vlan_info.vlan_proto = ivi.vlan_proto;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001284 vf_tx_rate.rate = ivi.max_tx_rate;
1285 vf_rate.min_tx_rate = ivi.min_tx_rate;
1286 vf_rate.max_tx_rate = ivi.max_tx_rate;
1287 vf_spoofchk.setting = ivi.spoofchk;
1288 vf_linkstate.link_state = ivi.linkstate;
1289 vf_rss_query_en.setting = ivi.rss_query_en;
1290 vf_trust.setting = ivi.trusted;
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001291 vf = nla_nest_start_noflag(skb, IFLA_VF_INFO);
Moshe Shemesh79aab092016-09-22 12:11:15 +03001292 if (!vf)
1293 goto nla_put_vfinfo_failure;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001294 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
Denis Kirjanov75345f882019-06-17 10:53:41 +02001295 nla_put(skb, IFLA_VF_BROADCAST, sizeof(vf_broadcast), &vf_broadcast) ||
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001296 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1297 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1298 &vf_rate) ||
1299 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1300 &vf_tx_rate) ||
1301 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1302 &vf_spoofchk) ||
1303 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1304 &vf_linkstate) ||
1305 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1306 sizeof(vf_rss_query_en),
1307 &vf_rss_query_en) ||
1308 nla_put(skb, IFLA_VF_TRUST,
1309 sizeof(vf_trust), &vf_trust))
Moshe Shemesh79aab092016-09-22 12:11:15 +03001310 goto nla_put_vf_failure;
Danit Goldberg30aad412019-11-06 15:30:07 +02001311
Danit Goldberg30aad412019-11-06 15:30:07 +02001312 if (dev->netdev_ops->ndo_get_vf_guid &&
1313 !dev->netdev_ops->ndo_get_vf_guid(dev, vfs_num, &node_guid,
1314 &port_guid)) {
1315 if (nla_put(skb, IFLA_VF_IB_NODE_GUID, sizeof(node_guid),
1316 &node_guid) ||
1317 nla_put(skb, IFLA_VF_IB_PORT_GUID, sizeof(port_guid),
1318 &port_guid))
1319 goto nla_put_vf_failure;
1320 }
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001321 vfvlanlist = nla_nest_start_noflag(skb, IFLA_VF_VLAN_LIST);
Moshe Shemesh79aab092016-09-22 12:11:15 +03001322 if (!vfvlanlist)
1323 goto nla_put_vf_failure;
1324 if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
1325 &vf_vlan_info)) {
1326 nla_nest_cancel(skb, vfvlanlist);
1327 goto nla_put_vf_failure;
1328 }
1329 nla_nest_end(skb, vfvlanlist);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001330 memset(&vf_stats, 0, sizeof(vf_stats));
1331 if (dev->netdev_ops->ndo_get_vf_stats)
1332 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1333 &vf_stats);
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001334 vfstats = nla_nest_start_noflag(skb, IFLA_VF_STATS);
Moshe Shemesh79aab092016-09-22 12:11:15 +03001335 if (!vfstats)
1336 goto nla_put_vf_failure;
Nicolas Dichtel343a6d82016-04-25 10:25:14 +02001337 if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1338 vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1339 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1340 vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1341 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1342 vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1343 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1344 vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1345 nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1346 vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1347 nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
Eugenia Emantayevc5a9f6f2017-07-17 13:47:07 +03001348 vf_stats.multicast, IFLA_VF_STATS_PAD) ||
1349 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED,
1350 vf_stats.rx_dropped, IFLA_VF_STATS_PAD) ||
1351 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED,
1352 vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) {
Moshe Shemesh79aab092016-09-22 12:11:15 +03001353 nla_nest_cancel(skb, vfstats);
1354 goto nla_put_vf_failure;
1355 }
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001356 nla_nest_end(skb, vfstats);
1357 nla_nest_end(skb, vf);
1358 return 0;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001359
1360nla_put_vf_failure:
1361 nla_nest_cancel(skb, vf);
1362nla_put_vfinfo_failure:
1363 nla_nest_cancel(skb, vfinfo);
1364 return -EMSGSIZE;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001365}
1366
Florian Westphal250fc3d2017-09-26 13:58:41 +02001367static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb,
1368 struct net_device *dev,
1369 u32 ext_filter_mask)
1370{
1371 struct nlattr *vfinfo;
1372 int i, num_vfs;
1373
1374 if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0))
1375 return 0;
1376
1377 num_vfs = dev_num_vf(dev->dev.parent);
1378 if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs))
1379 return -EMSGSIZE;
1380
1381 if (!dev->netdev_ops->ndo_get_vf_config)
1382 return 0;
1383
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001384 vfinfo = nla_nest_start_noflag(skb, IFLA_VFINFO_LIST);
Florian Westphal250fc3d2017-09-26 13:58:41 +02001385 if (!vfinfo)
1386 return -EMSGSIZE;
1387
1388 for (i = 0; i < num_vfs; i++) {
1389 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1390 return -EMSGSIZE;
1391 }
1392
1393 nla_nest_end(skb, vfinfo);
1394 return 0;
1395}
1396
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001397static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1398{
Kangjie Lu5f8e4472016-05-03 16:46:24 -04001399 struct rtnl_link_ifmap map;
1400
1401 memset(&map, 0, sizeof(map));
1402 map.mem_start = dev->mem_start;
1403 map.mem_end = dev->mem_end;
1404 map.base_addr = dev->base_addr;
1405 map.irq = dev->irq;
1406 map.dma = dev->dma;
1407 map.port = dev->if_port;
1408
Nicolas Dichtel270cb4d2016-04-26 10:06:16 +02001409 if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001410 return -EMSGSIZE;
1411
1412 return 0;
1413}
1414
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001415static u32 rtnl_xdp_prog_skb(struct net_device *dev)
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001416{
Martin KaFai Lau58038692017-06-15 17:29:09 -07001417 const struct bpf_prog *generic_xdp_prog;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001418
1419 ASSERT_RTNL();
1420
Martin KaFai Lau58038692017-06-15 17:29:09 -07001421 generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001422 if (!generic_xdp_prog)
1423 return 0;
1424 return generic_xdp_prog->aux->id;
1425}
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001426
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001427static u32 rtnl_xdp_prog_drv(struct net_device *dev)
1428{
1429 return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf, XDP_QUERY_PROG);
1430}
Jakub Kicinski118b4aa2017-12-01 15:08:55 -08001431
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001432static u32 rtnl_xdp_prog_hw(struct net_device *dev)
1433{
1434 return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf,
1435 XDP_QUERY_PROG_HW);
1436}
1437
1438static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev,
1439 u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr,
1440 u32 (*get_prog_id)(struct net_device *dev))
1441{
1442 u32 curr_id;
1443 int err;
1444
1445 curr_id = get_prog_id(dev);
1446 if (!curr_id)
1447 return 0;
1448
1449 *prog_id = curr_id;
1450 err = nla_put_u32(skb, attr, curr_id);
1451 if (err)
1452 return err;
1453
1454 if (*mode != XDP_ATTACHED_NONE)
1455 *mode = XDP_ATTACHED_MULTI;
1456 else
1457 *mode = tgt_mode;
1458
1459 return 0;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001460}
1461
Brenden Blancod1fdd912016-07-19 12:16:49 -07001462static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1463{
Brenden Blancod1fdd912016-07-19 12:16:49 -07001464 struct nlattr *xdp;
Martin KaFai Lau58038692017-06-15 17:29:09 -07001465 u32 prog_id;
Brenden Blancod1fdd912016-07-19 12:16:49 -07001466 int err;
Jakub Kicinski4f91da22018-07-11 20:36:38 -07001467 u8 mode;
Brenden Blancod1fdd912016-07-19 12:16:49 -07001468
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001469 xdp = nla_nest_start_noflag(skb, IFLA_XDP);
Brenden Blancod1fdd912016-07-19 12:16:49 -07001470 if (!xdp)
1471 return -EMSGSIZE;
David S. Millerb5cdae32017-04-18 15:36:58 -04001472
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001473 prog_id = 0;
1474 mode = XDP_ATTACHED_NONE;
Jakub Kicinski202aabe2018-07-16 19:08:50 -07001475 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB,
1476 IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb);
1477 if (err)
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001478 goto err_cancel;
Jakub Kicinski202aabe2018-07-16 19:08:50 -07001479 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV,
1480 IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv);
1481 if (err)
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001482 goto err_cancel;
Jakub Kicinski202aabe2018-07-16 19:08:50 -07001483 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW,
1484 IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw);
1485 if (err)
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001486 goto err_cancel;
1487
Jakub Kicinski4f91da22018-07-11 20:36:38 -07001488 err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode);
Brenden Blancod1fdd912016-07-19 12:16:49 -07001489 if (err)
1490 goto err_cancel;
1491
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001492 if (prog_id && mode != XDP_ATTACHED_MULTI) {
Martin KaFai Lau58038692017-06-15 17:29:09 -07001493 err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
1494 if (err)
1495 goto err_cancel;
1496 }
1497
Brenden Blancod1fdd912016-07-19 12:16:49 -07001498 nla_nest_end(skb, xdp);
1499 return 0;
1500
1501err_cancel:
1502 nla_nest_cancel(skb, xdp);
1503 return err;
1504}
1505
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001506static u32 rtnl_get_event(unsigned long event)
1507{
1508 u32 rtnl_event_type = IFLA_EVENT_NONE;
1509
1510 switch (event) {
1511 case NETDEV_REBOOT:
1512 rtnl_event_type = IFLA_EVENT_REBOOT;
1513 break;
1514 case NETDEV_FEAT_CHANGE:
1515 rtnl_event_type = IFLA_EVENT_FEATURES;
1516 break;
1517 case NETDEV_BONDING_FAILOVER:
1518 rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
1519 break;
1520 case NETDEV_NOTIFY_PEERS:
1521 rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
1522 break;
1523 case NETDEV_RESEND_IGMP:
1524 rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
1525 break;
1526 case NETDEV_CHANGEINFODATA:
1527 rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
1528 break;
1529 default:
1530 break;
1531 }
1532
1533 return rtnl_event_type;
1534}
1535
Florian Westphal79110a02017-09-26 13:58:40 +02001536static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
1537{
1538 const struct net_device *upper_dev;
1539 int ret = 0;
1540
1541 rcu_read_lock();
1542
1543 upper_dev = netdev_master_upper_dev_get_rcu(dev);
1544 if (upper_dev)
1545 ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);
1546
1547 rcu_read_unlock();
1548 return ret;
1549}
1550
Sabrina Dubrocafeadc4b2019-05-14 15:12:19 +02001551static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev,
1552 bool force)
Florian Westphal79110a02017-09-26 13:58:40 +02001553{
1554 int ifindex = dev_get_iflink(dev);
1555
Sabrina Dubrocafeadc4b2019-05-14 15:12:19 +02001556 if (force || dev->ifindex != ifindex)
1557 return nla_put_u32(skb, IFLA_LINK, ifindex);
Florian Westphal79110a02017-09-26 13:58:40 +02001558
Sabrina Dubrocafeadc4b2019-05-14 15:12:19 +02001559 return 0;
Florian Westphal79110a02017-09-26 13:58:40 +02001560}
1561
Florian Westphal6c557002017-10-02 23:50:05 +02001562static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
1563 struct net_device *dev)
1564{
1565 char buf[IFALIASZ];
1566 int ret;
1567
1568 ret = dev_get_alias(dev, buf, sizeof(buf));
1569 return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
1570}
1571
Florian Westphalb1e66b92017-09-26 13:58:42 +02001572static int rtnl_fill_link_netnsid(struct sk_buff *skb,
Jiri Benc79e1ad12017-11-02 17:04:38 -02001573 const struct net_device *dev,
Guillaume Naultd4e4fdf2019-10-23 18:39:04 +02001574 struct net *src_net, gfp_t gfp)
Florian Westphalb1e66b92017-09-26 13:58:42 +02001575{
Sabrina Dubrocafeadc4b2019-05-14 15:12:19 +02001576 bool put_iflink = false;
1577
Florian Westphalb1e66b92017-09-26 13:58:42 +02001578 if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
1579 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1580
1581 if (!net_eq(dev_net(dev), link_net)) {
Guillaume Naultd4e4fdf2019-10-23 18:39:04 +02001582 int id = peernet2id_alloc(src_net, link_net, gfp);
Florian Westphalb1e66b92017-09-26 13:58:42 +02001583
1584 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1585 return -EMSGSIZE;
Sabrina Dubrocafeadc4b2019-05-14 15:12:19 +02001586
1587 put_iflink = true;
Florian Westphalb1e66b92017-09-26 13:58:42 +02001588 }
1589 }
1590
Sabrina Dubrocafeadc4b2019-05-14 15:12:19 +02001591 return nla_put_iflink(skb, dev, put_iflink);
Florian Westphalb1e66b92017-09-26 13:58:42 +02001592}
1593
Florian Westphal070cbf5b2017-10-16 15:44:35 +02001594static int rtnl_fill_link_af(struct sk_buff *skb,
1595 const struct net_device *dev,
1596 u32 ext_filter_mask)
1597{
1598 const struct rtnl_af_ops *af_ops;
1599 struct nlattr *af_spec;
1600
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001601 af_spec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
Florian Westphal070cbf5b2017-10-16 15:44:35 +02001602 if (!af_spec)
1603 return -EMSGSIZE;
1604
Florian Westphal5fa85a02017-10-16 15:44:36 +02001605 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Florian Westphal070cbf5b2017-10-16 15:44:35 +02001606 struct nlattr *af;
1607 int err;
1608
1609 if (!af_ops->fill_link_af)
1610 continue;
1611
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001612 af = nla_nest_start_noflag(skb, af_ops->family);
Florian Westphal070cbf5b2017-10-16 15:44:35 +02001613 if (!af)
1614 return -EMSGSIZE;
1615
1616 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1617 /*
1618 * Caller may return ENODATA to indicate that there
1619 * was no data to be dumped. This is not an error, it
1620 * means we should trim the attribute header and
1621 * continue.
1622 */
1623 if (err == -ENODATA)
1624 nla_nest_cancel(skb, af);
1625 else if (err < 0)
1626 return -EMSGSIZE;
1627
1628 nla_nest_end(skb, af);
1629 }
1630
1631 nla_nest_end(skb, af_spec);
1632 return 0;
1633}
1634
Jiri Pirko88f4fb02019-09-30 11:48:17 +02001635static int rtnl_fill_alt_ifnames(struct sk_buff *skb,
1636 const struct net_device *dev)
1637{
1638 struct netdev_name_node *name_node;
1639 int count = 0;
1640
1641 list_for_each_entry(name_node, &dev->name_node->list, list) {
1642 if (nla_put_string(skb, IFLA_ALT_IFNAME, name_node->name))
1643 return -EMSGSIZE;
1644 count++;
1645 }
1646 return count;
1647}
1648
1649static int rtnl_fill_prop_list(struct sk_buff *skb,
1650 const struct net_device *dev)
1651{
1652 struct nlattr *prop_list;
1653 int ret;
1654
1655 prop_list = nla_nest_start(skb, IFLA_PROP_LIST);
1656 if (!prop_list)
1657 return -EMSGSIZE;
1658
1659 ret = rtnl_fill_alt_ifnames(skb, dev);
1660 if (ret <= 0)
1661 goto nest_cancel;
1662
1663 nla_nest_end(skb, prop_list);
1664 return 0;
1665
1666nest_cancel:
1667 nla_nest_cancel(skb, prop_list);
1668 return ret;
1669}
1670
Roopa Prabhu829eb202020-07-31 17:34:01 -07001671static int rtnl_fill_proto_down(struct sk_buff *skb,
1672 const struct net_device *dev)
1673{
1674 struct nlattr *pr;
1675 u32 preason;
1676
1677 if (nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
1678 goto nla_put_failure;
1679
1680 preason = dev->proto_down_reason;
1681 if (!preason)
1682 return 0;
1683
1684 pr = nla_nest_start(skb, IFLA_PROTO_DOWN_REASON);
1685 if (!pr)
1686 return -EMSGSIZE;
1687
1688 if (nla_put_u32(skb, IFLA_PROTO_DOWN_REASON_VALUE, preason)) {
1689 nla_nest_cancel(skb, pr);
1690 goto nla_put_failure;
1691 }
1692
1693 nla_nest_end(skb, pr);
1694 return 0;
1695
1696nla_put_failure:
1697 return -EMSGSIZE;
1698}
1699
Jiri Benc79e1ad12017-11-02 17:04:38 -02001700static int rtnl_fill_ifinfo(struct sk_buff *skb,
1701 struct net_device *dev, struct net *src_net,
Patrick McHardy575c3e22007-05-22 17:00:49 -07001702 int type, u32 pid, u32 seq, u32 change,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001703 unsigned int flags, u32 ext_filter_mask,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01001704 u32 event, int *new_nsid, int new_ifindex,
Guillaume Naultd4e4fdf2019-10-23 18:39:04 +02001705 int tgt_netnsid, gfp_t gfp)
Thomas Grafb60c5112006-08-04 23:05:34 -07001706{
1707 struct ifinfomsg *ifm;
1708 struct nlmsghdr *nlh;
1709
Eric Dumazet2907c352011-05-25 07:34:04 +00001710 ASSERT_RTNL();
Thomas Grafb60c5112006-08-04 23:05:34 -07001711 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1712 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08001713 return -EMSGSIZE;
Thomas Grafb60c5112006-08-04 23:05:34 -07001714
1715 ifm = nlmsg_data(nlh);
1716 ifm->ifi_family = AF_UNSPEC;
1717 ifm->__ifi_pad = 0;
1718 ifm->ifi_type = dev->type;
1719 ifm->ifi_index = dev->ifindex;
1720 ifm->ifi_flags = dev_get_flags(dev);
1721 ifm->ifi_change = change;
1722
Christian Brauner7e4a8d52018-09-04 21:53:53 +02001723 if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid))
Jiri Benc79e1ad12017-11-02 17:04:38 -02001724 goto nla_put_failure;
1725
David S. Millera6574342012-04-01 20:12:00 -04001726 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1727 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1728 nla_put_u8(skb, IFLA_OPERSTATE,
1729 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1730 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1731 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
Stephen Hemminger3e7a50c2018-07-27 13:43:22 -07001732 nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) ||
1733 nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) ||
David S. Millera6574342012-04-01 20:12:00 -04001734 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
Ben Greearedbc0bb2012-03-29 12:51:30 +00001735 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001736 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
Eric Dumazetc70ce022016-03-21 09:55:10 -07001737 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1738 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001739#ifdef CONFIG_RPS
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001740 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001741#endif
Florian Westphal79110a02017-09-26 13:58:40 +02001742 put_master_ifindex(skb, dev) ||
Jiri Pirko9a572472012-12-27 23:49:39 +00001743 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
David S. Millera6574342012-04-01 20:12:00 -04001744 (dev->qdisc &&
1745 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
Florian Westphal6c557002017-10-02 23:50:05 +02001746 nla_put_ifalias(skb, dev) ||
david decotigny2d3b4792014-03-29 09:48:35 -07001747 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
David Decotignyb2d3bcf2018-01-18 09:59:13 -08001748 atomic_read(&dev->carrier_up_count) +
1749 atomic_read(&dev->carrier_down_count)) ||
David Decotignyb2d3bcf2018-01-18 09:59:13 -08001750 nla_put_u32(skb, IFLA_CARRIER_UP_COUNT,
1751 atomic_read(&dev->carrier_up_count)) ||
1752 nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT,
1753 atomic_read(&dev->carrier_down_count)))
David S. Millera6574342012-04-01 20:12:00 -04001754 goto nla_put_failure;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001755
Roopa Prabhu829eb202020-07-31 17:34:01 -07001756 if (rtnl_fill_proto_down(skb, dev))
1757 goto nla_put_failure;
1758
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001759 if (event != IFLA_EVENT_NONE) {
1760 if (nla_put_u32(skb, IFLA_EVENT, event))
1761 goto nla_put_failure;
1762 }
1763
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001764 if (rtnl_fill_link_ifmap(skb, dev))
1765 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
1767 if (dev->addr_len) {
David S. Millera6574342012-04-01 20:12:00 -04001768 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1769 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1770 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 }
1772
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001773 if (rtnl_phys_port_id_fill(skb, dev))
1774 goto nla_put_failure;
1775
David Aherndb24a902015-03-17 20:23:15 -06001776 if (rtnl_phys_port_name_fill(skb, dev))
1777 goto nla_put_failure;
1778
Jiri Pirko82f28412014-11-28 14:34:18 +01001779 if (rtnl_phys_switch_id_fill(skb, dev))
1780 goto nla_put_failure;
1781
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001782 if (rtnl_fill_stats(skb, dev))
Pavel Emelyanov96e74082008-05-21 14:12:46 -07001783 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Florian Westphal250fc3d2017-09-26 13:58:41 +02001785 if (rtnl_fill_vf(skb, dev, ext_filter_mask))
David S. Millera6574342012-04-01 20:12:00 -04001786 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001787
David Gibsonc53864f2014-04-24 10:22:36 +10001788 if (rtnl_port_fill(skb, dev, ext_filter_mask))
Scott Feldman57b61082010-05-17 22:49:55 -07001789 goto nla_put_failure;
1790
Brenden Blancod1fdd912016-07-19 12:16:49 -07001791 if (rtnl_xdp_fill(skb, dev))
1792 goto nla_put_failure;
1793
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001794 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07001795 if (rtnl_link_fill(skb, dev) < 0)
1796 goto nla_put_failure;
1797 }
1798
Guillaume Naultd4e4fdf2019-10-23 18:39:04 +02001799 if (rtnl_fill_link_netnsid(skb, dev, src_net, gfp))
Florian Westphalb1e66b92017-09-26 13:58:42 +02001800 goto nla_put_failure;
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001801
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02001802 if (new_nsid &&
1803 nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0)
1804 goto nla_put_failure;
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01001805 if (new_ifindex &&
1806 nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0)
1807 goto nla_put_failure;
1808
Michal Kubecekf74877a2019-12-11 10:58:14 +01001809 if (memchr_inv(dev->perm_addr, '\0', dev->addr_len) &&
1810 nla_put(skb, IFLA_PERM_ADDRESS, dev->addr_len, dev->perm_addr))
1811 goto nla_put_failure;
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02001812
Florian Westphal5fa85a02017-10-16 15:44:36 +02001813 rcu_read_lock();
Florian Westphal070cbf5b2017-10-16 15:44:35 +02001814 if (rtnl_fill_link_af(skb, dev, ext_filter_mask))
Florian Westphal5fa85a02017-10-16 15:44:36 +02001815 goto nla_put_failure_rcu;
1816 rcu_read_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +00001817
Jiri Pirko88f4fb02019-09-30 11:48:17 +02001818 if (rtnl_fill_prop_list(skb, dev))
1819 goto nla_put_failure;
1820
Johannes Berg053c0952015-01-16 22:09:00 +01001821 nlmsg_end(skb, nlh);
1822 return 0;
Thomas Grafb60c5112006-08-04 23:05:34 -07001823
Florian Westphal5fa85a02017-10-16 15:44:36 +02001824nla_put_failure_rcu:
1825 rcu_read_unlock();
Thomas Grafb60c5112006-08-04 23:05:34 -07001826nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001827 nlmsg_cancel(skb, nlh);
1828 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829}
1830
Jiri Pirkof7b12602014-02-18 20:53:18 +01001831static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1832 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1833 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1834 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1835 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1836 [IFLA_MTU] = { .type = NLA_U32 },
1837 [IFLA_LINK] = { .type = NLA_U32 },
1838 [IFLA_MASTER] = { .type = NLA_U32 },
1839 [IFLA_CARRIER] = { .type = NLA_U8 },
1840 [IFLA_TXQLEN] = { .type = NLA_U32 },
1841 [IFLA_WEIGHT] = { .type = NLA_U32 },
1842 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1843 [IFLA_LINKMODE] = { .type = NLA_U8 },
1844 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1845 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1846 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
Nicolas Dichtel2459b4c2017-10-11 16:24:48 +02001847 /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
1848 * allow 0-length string (needed to remove an alias).
1849 */
1850 [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001851 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1852 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1853 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1854 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1855 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1856 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1857 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1858 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
Stephen Hemminger46e6b992017-12-07 15:40:19 -08001859 [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 },
1860 [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 },
Jiri Pirko02637fc2014-11-28 14:34:16 +01001861 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
david decotigny2d3b4792014-03-29 09:48:35 -07001862 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
Jiri Pirko82f28412014-11-28 14:34:18 +01001863 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
Nicolas Dichtel317f4812015-01-15 15:11:18 +01001864 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001865 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001866 [IFLA_XDP] = { .type = NLA_NESTED },
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001867 [IFLA_EVENT] = { .type = NLA_U32 },
Serhey Popovychdb833d42017-06-20 14:35:23 +03001868 [IFLA_GROUP] = { .type = NLA_U32 },
Christian Brauner7e4a8d52018-09-04 21:53:53 +02001869 [IFLA_TARGET_NETNSID] = { .type = NLA_S32 },
David Decotignyb2d3bcf2018-01-18 09:59:13 -08001870 [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 },
1871 [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 },
Stephen Hemminger3e7a50c2018-07-27 13:43:22 -07001872 [IFLA_MIN_MTU] = { .type = NLA_U32 },
1873 [IFLA_MAX_MTU] = { .type = NLA_U32 },
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02001874 [IFLA_PROP_LIST] = { .type = NLA_NESTED },
1875 [IFLA_ALT_IFNAME] = { .type = NLA_STRING,
1876 .len = ALTIFNAMSIZ - 1 },
Michal Kubecekf74877a2019-12-11 10:58:14 +01001877 [IFLA_PERM_ADDRESS] = { .type = NLA_REJECT },
Roopa Prabhu829eb202020-07-31 17:34:01 -07001878 [IFLA_PROTO_DOWN_REASON] = { .type = NLA_NESTED },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001879};
1880
1881static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1882 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1883 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1884 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1885 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1886};
1887
Jiri Pirkof7b12602014-02-18 20:53:18 +01001888static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
Daniel Borkmann364d5712015-02-05 18:44:04 +01001889 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
Denis Kirjanov75345f882019-06-17 10:53:41 +02001890 [IFLA_VF_BROADCAST] = { .type = NLA_REJECT },
Daniel Borkmann364d5712015-02-05 18:44:04 +01001891 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
Moshe Shemesh79aab092016-09-22 12:11:15 +03001892 [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED },
Daniel Borkmann364d5712015-02-05 18:44:04 +01001893 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1894 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1895 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1896 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001897 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001898 [IFLA_VF_STATS] = { .type = NLA_NESTED },
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00001899 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
Eli Cohencc8e27c2016-03-11 22:58:34 +02001900 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1901 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001902};
1903
Jiri Pirkof7b12602014-02-18 20:53:18 +01001904static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1905 [IFLA_PORT_VF] = { .type = NLA_U32 },
1906 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1907 .len = PORT_PROFILE_MAX },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001908 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1909 .len = PORT_UUID_MAX },
1910 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1911 .len = PORT_UUID_MAX },
1912 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1913 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
Daniel Borkmann025331d2017-02-17 01:56:11 +01001914
1915 /* Unused, but we need to keep it here since user space could
1916 * fill it. It's also broken with regard to NLA_BINARY use in
1917 * combination with structs.
1918 */
1919 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1920 .len = sizeof(struct ifla_port_vsi) },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001921};
1922
Brenden Blancod1fdd912016-07-19 12:16:49 -07001923static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
Toke Høiland-Jørgensen92234c82020-03-25 18:23:26 +01001924 [IFLA_XDP_UNSPEC] = { .strict_start_type = IFLA_XDP_EXPECTED_FD },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001925 [IFLA_XDP_FD] = { .type = NLA_S32 },
Toke Høiland-Jørgensen92234c82020-03-25 18:23:26 +01001926 [IFLA_XDP_EXPECTED_FD] = { .type = NLA_S32 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001927 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 },
Daniel Borkmann85de8572016-11-28 23:16:54 +01001928 [IFLA_XDP_FLAGS] = { .type = NLA_U32 },
Martin KaFai Lau58038692017-06-15 17:29:09 -07001929 [IFLA_XDP_PROG_ID] = { .type = NLA_U32 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001930};
1931
David Aherndc599f72016-02-02 08:17:07 -08001932static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1933{
1934 const struct rtnl_link_ops *ops = NULL;
1935 struct nlattr *linfo[IFLA_INFO_MAX + 1];
1936
Johannes Berg8cb08172019-04-26 14:07:28 +02001937 if (nla_parse_nested_deprecated(linfo, IFLA_INFO_MAX, nla, ifla_info_policy, NULL) < 0)
David Aherndc599f72016-02-02 08:17:07 -08001938 return NULL;
1939
1940 if (linfo[IFLA_INFO_KIND]) {
1941 char kind[MODULE_NAME_LEN];
1942
1943 nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1944 ops = rtnl_link_ops_get(kind);
1945 }
1946
1947 return ops;
1948}
1949
1950static bool link_master_filtered(struct net_device *dev, int master_idx)
1951{
1952 struct net_device *master;
1953
1954 if (!master_idx)
1955 return false;
1956
1957 master = netdev_master_upper_dev_get(dev);
1958 if (!master || master->ifindex != master_idx)
1959 return true;
1960
1961 return false;
1962}
1963
1964static bool link_kind_filtered(const struct net_device *dev,
1965 const struct rtnl_link_ops *kind_ops)
1966{
1967 if (kind_ops && dev->rtnl_link_ops != kind_ops)
1968 return true;
1969
1970 return false;
1971}
1972
1973static bool link_dump_filtered(struct net_device *dev,
1974 int master_idx,
1975 const struct rtnl_link_ops *kind_ops)
1976{
1977 if (link_master_filtered(dev, master_idx) ||
1978 link_kind_filtered(dev, kind_ops))
1979 return true;
1980
1981 return false;
1982}
1983
Christian Braunerc383edc2018-09-04 21:53:47 +02001984/**
1985 * rtnl_get_net_ns_capable - Get netns if sufficiently privileged.
1986 * @sk: netlink socket
1987 * @netnsid: network namespace identifier
1988 *
1989 * Returns the network namespace identified by netnsid on success or an error
1990 * pointer on failure.
1991 */
1992struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid)
Jiri Benc79e1ad12017-11-02 17:04:38 -02001993{
1994 struct net *net;
1995
Andrei Vaginf428fe42018-01-02 23:27:33 -08001996 net = get_net_ns_by_id(sock_net(sk), netnsid);
Jiri Benc79e1ad12017-11-02 17:04:38 -02001997 if (!net)
1998 return ERR_PTR(-EINVAL);
1999
2000 /* For now, the caller is required to have CAP_NET_ADMIN in
2001 * the user namespace owning the target net ns.
2002 */
Andrei Vaginf428fe42018-01-02 23:27:33 -08002003 if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) {
Jiri Benc79e1ad12017-11-02 17:04:38 -02002004 put_net(net);
2005 return ERR_PTR(-EACCES);
2006 }
2007 return net;
2008}
Christian Braunerc383edc2018-09-04 21:53:47 +02002009EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable);
Jiri Benc79e1ad12017-11-02 17:04:38 -02002010
David Ahern905cf0a2018-10-07 20:16:30 -07002011static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh,
2012 bool strict_check, struct nlattr **tb,
2013 struct netlink_ext_ack *extack)
2014{
2015 int hdrlen;
2016
2017 if (strict_check) {
2018 struct ifinfomsg *ifm;
2019
2020 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
2021 NL_SET_ERR_MSG(extack, "Invalid header for link dump");
2022 return -EINVAL;
2023 }
2024
2025 ifm = nlmsg_data(nlh);
2026 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
2027 ifm->ifi_change) {
2028 NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request");
2029 return -EINVAL;
2030 }
2031 if (ifm->ifi_index) {
2032 NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps");
2033 return -EINVAL;
2034 }
2035
Johannes Berg8cb08172019-04-26 14:07:28 +02002036 return nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb,
2037 IFLA_MAX, ifla_policy,
2038 extack);
David Ahern905cf0a2018-10-07 20:16:30 -07002039 }
2040
2041 /* A hack to preserve kernel<->userspace interface.
2042 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
2043 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
2044 * what iproute2 < v3.9.0 used.
2045 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
2046 * attribute, its netlink message is shorter than struct ifinfomsg.
2047 */
2048 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2049 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2050
Johannes Berg8cb08172019-04-26 14:07:28 +02002051 return nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy,
2052 extack);
David Ahern905cf0a2018-10-07 20:16:30 -07002053}
2054
Thomas Grafb60c5112006-08-04 23:05:34 -07002055static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056{
David Ahern905cf0a2018-10-07 20:16:30 -07002057 struct netlink_ext_ack *extack = cb->extack;
2058 const struct nlmsghdr *nlh = cb->nlh;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002059 struct net *net = sock_net(skb->sk);
Jiri Benc79e1ad12017-11-02 17:04:38 -02002060 struct net *tgt_net = net;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002061 int h, s_h;
2062 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 struct net_device *dev;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002064 struct hlist_head *head;
Greg Rose115c9b82012-02-21 16:54:48 -05002065 struct nlattr *tb[IFLA_MAX+1];
2066 u32 ext_filter_mask = 0;
David Aherndc599f72016-02-02 08:17:07 -08002067 const struct rtnl_link_ops *kind_ops = NULL;
2068 unsigned int flags = NLM_F_MULTI;
2069 int master_idx = 0;
Jiri Benc79e1ad12017-11-02 17:04:38 -02002070 int netnsid = -1;
David Ahern905cf0a2018-10-07 20:16:30 -07002071 int err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002073 s_h = cb->args[0];
2074 s_idx = cb->args[1];
2075
David Ahern905cf0a2018-10-07 20:16:30 -07002076 err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack);
2077 if (err < 0) {
2078 if (cb->strict_check)
2079 return err;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02002080
David Ahern905cf0a2018-10-07 20:16:30 -07002081 goto walk_entries;
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00002082 }
Greg Rose115c9b82012-02-21 16:54:48 -05002083
David Ahern905cf0a2018-10-07 20:16:30 -07002084 for (i = 0; i <= IFLA_MAX; ++i) {
2085 if (!tb[i])
2086 continue;
2087
2088 /* new attributes should only be added with strict checking */
2089 switch (i) {
2090 case IFLA_TARGET_NETNSID:
2091 netnsid = nla_get_s32(tb[i]);
2092 tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid);
2093 if (IS_ERR(tgt_net)) {
2094 NL_SET_ERR_MSG(extack, "Invalid target network namespace id");
2095 return PTR_ERR(tgt_net);
2096 }
2097 break;
2098 case IFLA_EXT_MASK:
2099 ext_filter_mask = nla_get_u32(tb[i]);
2100 break;
2101 case IFLA_MASTER:
2102 master_idx = nla_get_u32(tb[i]);
2103 break;
2104 case IFLA_LINKINFO:
2105 kind_ops = linkinfo_to_kind_ops(tb[i]);
2106 break;
2107 default:
2108 if (cb->strict_check) {
2109 NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request");
2110 return -EINVAL;
2111 }
2112 }
2113 }
2114
2115 if (master_idx || kind_ops)
2116 flags |= NLM_F_DUMP_FILTERED;
2117
2118walk_entries:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002119 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
2120 idx = 0;
Jiri Benc79e1ad12017-11-02 17:04:38 -02002121 head = &tgt_net->dev_index_head[h];
Eric Dumazetcac5e652015-02-27 09:42:50 -08002122 hlist_for_each_entry(dev, head, index_hlist) {
David Aherndc599f72016-02-02 08:17:07 -08002123 if (link_dump_filtered(dev, master_idx, kind_ops))
Zhang Shengju3f0ae052016-11-19 23:28:32 +08002124 goto cont;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002125 if (idx < s_idx)
2126 goto cont;
Jiri Benc79e1ad12017-11-02 17:04:38 -02002127 err = rtnl_fill_ifinfo(skb, dev, net,
2128 RTM_NEWLINK,
David Gibson973462b2014-04-24 10:22:35 +10002129 NETLINK_CB(cb->skb).portid,
David Ahern905cf0a2018-10-07 20:16:30 -07002130 nlh->nlmsg_seq, 0, flags,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01002131 ext_filter_mask, 0, NULL, 0,
Guillaume Naultd4e4fdf2019-10-23 18:39:04 +02002132 netnsid, GFP_KERNEL);
David Gibson973462b2014-04-24 10:22:35 +10002133
David Ahernf6c57752017-05-15 23:19:17 -07002134 if (err < 0) {
2135 if (likely(skb->len))
2136 goto out;
2137
2138 goto out_err;
2139 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07002140cont:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002141 idx++;
2142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 }
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002144out:
David Ahernf6c57752017-05-15 23:19:17 -07002145 err = skb->len;
2146out_err:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002147 cb->args[1] = idx;
2148 cb->args[0] = h;
Jakub Sitnickid0225782017-08-09 17:39:12 +02002149 cb->seq = net->dev_base_seq;
2150 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Jiri Benc79e1ad12017-11-02 17:04:38 -02002151 if (netnsid >= 0)
2152 put_net(tgt_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
David Ahernf6c57752017-05-15 23:19:17 -07002154 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155}
2156
Johannes Bergfceb6432017-04-12 14:34:07 +02002157int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
2158 struct netlink_ext_ack *exterr)
Jiri Pirkof7b12602014-02-18 20:53:18 +01002159{
Johannes Berg8cb08172019-04-26 14:07:28 +02002160 return nla_parse_deprecated(tb, IFLA_MAX, head, len, ifla_policy,
2161 exterr);
Jiri Pirkof7b12602014-02-18 20:53:18 +01002162}
2163EXPORT_SYMBOL(rtnl_nla_parse_ifla);
Scott Feldman57b61082010-05-17 22:49:55 -07002164
Eric W. Biederman81adee42009-11-08 00:53:51 -08002165struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
2166{
2167 struct net *net;
2168 /* Examine the link attributes and figure out which
2169 * network namespace we are talking about.
2170 */
2171 if (tb[IFLA_NET_NS_PID])
2172 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
Eric W. Biedermanf0630522011-05-04 17:51:50 -07002173 else if (tb[IFLA_NET_NS_FD])
2174 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
Eric W. Biederman81adee42009-11-08 00:53:51 -08002175 else
2176 net = get_net(src_net);
2177 return net;
2178}
2179EXPORT_SYMBOL(rtnl_link_get_net);
2180
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002181/* Figure out which network namespace we are talking about by
2182 * examining the link attributes in the following order:
2183 *
2184 * 1. IFLA_NET_NS_PID
2185 * 2. IFLA_NET_NS_FD
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002186 * 3. IFLA_TARGET_NETNSID
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002187 */
2188static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net,
2189 struct nlattr *tb[])
2190{
2191 struct net *net;
2192
2193 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])
2194 return rtnl_link_get_net(src_net, tb);
2195
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002196 if (!tb[IFLA_TARGET_NETNSID])
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002197 return get_net(src_net);
2198
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002199 net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID]));
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002200 if (!net)
2201 return ERR_PTR(-EINVAL);
2202
2203 return net;
2204}
2205
2206static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb,
2207 struct net *src_net,
2208 struct nlattr *tb[], int cap)
2209{
2210 struct net *net;
2211
2212 net = rtnl_link_get_net_by_nlattr(src_net, tb);
2213 if (IS_ERR(net))
2214 return net;
2215
2216 if (!netlink_ns_capable(skb, net->user_ns, cap)) {
2217 put_net(net);
2218 return ERR_PTR(-EPERM);
2219 }
2220
2221 return net;
2222}
2223
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002224/* Verify that rtnetlink requests do not pass additional properties
2225 * potentially referring to different network namespaces.
2226 */
2227static int rtnl_ensure_unique_netns(struct nlattr *tb[],
2228 struct netlink_ext_ack *extack,
2229 bool netns_id_only)
2230{
2231
2232 if (netns_id_only) {
2233 if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD])
2234 return 0;
2235
2236 NL_SET_ERR_MSG(extack, "specified netns attribute not supported");
2237 return -EOPNOTSUPP;
2238 }
2239
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002240 if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]))
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002241 goto invalid_attr;
2242
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002243 if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD]))
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002244 goto invalid_attr;
2245
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002246 if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID]))
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002247 goto invalid_attr;
2248
2249 return 0;
2250
2251invalid_attr:
2252 NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified");
2253 return -EINVAL;
2254}
2255
Thomas Graf1840bb12008-02-23 19:54:36 -08002256static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
2257{
2258 if (dev) {
2259 if (tb[IFLA_ADDRESS] &&
2260 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
2261 return -EINVAL;
2262
2263 if (tb[IFLA_BROADCAST] &&
2264 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
2265 return -EINVAL;
2266 }
2267
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002268 if (tb[IFLA_AF_SPEC]) {
2269 struct nlattr *af;
2270 int rem, err;
2271
2272 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2273 const struct rtnl_af_ops *af_ops;
2274
Florian Westphal5fa85a02017-10-16 15:44:36 +02002275 rcu_read_lock();
2276 af_ops = rtnl_af_lookup(nla_type(af));
2277 if (!af_ops) {
2278 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002279 return -EAFNOSUPPORT;
Florian Westphal5fa85a02017-10-16 15:44:36 +02002280 }
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002281
Florian Westphal5fa85a02017-10-16 15:44:36 +02002282 if (!af_ops->set_link_af) {
2283 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002284 return -EOPNOTSUPP;
Florian Westphal5fa85a02017-10-16 15:44:36 +02002285 }
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002286
2287 if (af_ops->validate_link_af) {
Kurt Van Dijck6d3a9a62011-01-26 04:55:24 +00002288 err = af_ops->validate_link_af(dev, af);
Florian Westphal5fa85a02017-10-16 15:44:36 +02002289 if (err < 0) {
2290 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002291 return err;
Florian Westphal5fa85a02017-10-16 15:44:36 +02002292 }
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002293 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02002294
2295 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002296 }
2297 }
2298
Thomas Graf1840bb12008-02-23 19:54:36 -08002299 return 0;
2300}
2301
Eli Cohencc8e27c2016-03-11 22:58:34 +02002302static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
2303 int guid_type)
2304{
2305 const struct net_device_ops *ops = dev->netdev_ops;
2306
2307 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
2308}
2309
2310static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
2311{
2312 if (dev->type != ARPHRD_INFINIBAND)
2313 return -EOPNOTSUPP;
2314
2315 return handle_infiniband_guid(dev, ivt, guid_type);
2316}
2317
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002318static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
Chris Wrightc02db8c2010-05-16 01:05:45 -07002319{
Chris Wrightc02db8c2010-05-16 01:05:45 -07002320 const struct net_device_ops *ops = dev->netdev_ops;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002321 int err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002322
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002323 if (tb[IFLA_VF_MAC]) {
2324 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03002325
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002326 if (ivm->vf >= INT_MAX)
2327 return -EINVAL;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002328 err = -EOPNOTSUPP;
2329 if (ops->ndo_set_vf_mac)
2330 err = ops->ndo_set_vf_mac(dev, ivm->vf,
2331 ivm->mac);
2332 if (err < 0)
2333 return err;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002334 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002335
2336 if (tb[IFLA_VF_VLAN]) {
2337 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
2338
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002339 if (ivv->vf >= INT_MAX)
2340 return -EINVAL;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002341 err = -EOPNOTSUPP;
2342 if (ops->ndo_set_vf_vlan)
2343 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
Moshe Shemesh79aab092016-09-22 12:11:15 +03002344 ivv->qos,
2345 htons(ETH_P_8021Q));
2346 if (err < 0)
2347 return err;
2348 }
2349
2350 if (tb[IFLA_VF_VLAN_LIST]) {
2351 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
2352 struct nlattr *attr;
2353 int rem, len = 0;
2354
2355 err = -EOPNOTSUPP;
2356 if (!ops->ndo_set_vf_vlan)
2357 return err;
2358
2359 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
2360 if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
2361 nla_len(attr) < NLA_HDRLEN) {
2362 return -EINVAL;
2363 }
2364 if (len >= MAX_VLAN_LIST_LEN)
2365 return -EOPNOTSUPP;
2366 ivvl[len] = nla_data(attr);
2367
2368 len++;
2369 }
Arnd Bergmannfa34cd92016-09-30 18:13:49 +02002370 if (len == 0)
2371 return -EINVAL;
2372
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002373 if (ivvl[0]->vf >= INT_MAX)
2374 return -EINVAL;
Moshe Shemesh79aab092016-09-22 12:11:15 +03002375 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
2376 ivvl[0]->qos, ivvl[0]->vlan_proto);
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002377 if (err < 0)
2378 return err;
2379 }
2380
2381 if (tb[IFLA_VF_TX_RATE]) {
2382 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
2383 struct ifla_vf_info ivf;
2384
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002385 if (ivt->vf >= INT_MAX)
2386 return -EINVAL;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002387 err = -EOPNOTSUPP;
2388 if (ops->ndo_get_vf_config)
2389 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
2390 if (err < 0)
2391 return err;
2392
2393 err = -EOPNOTSUPP;
2394 if (ops->ndo_set_vf_rate)
2395 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2396 ivf.min_tx_rate,
2397 ivt->rate);
2398 if (err < 0)
2399 return err;
2400 }
2401
2402 if (tb[IFLA_VF_RATE]) {
2403 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
2404
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002405 if (ivt->vf >= INT_MAX)
2406 return -EINVAL;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002407 err = -EOPNOTSUPP;
2408 if (ops->ndo_set_vf_rate)
2409 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2410 ivt->min_tx_rate,
2411 ivt->max_tx_rate);
2412 if (err < 0)
2413 return err;
2414 }
2415
2416 if (tb[IFLA_VF_SPOOFCHK]) {
2417 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
2418
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002419 if (ivs->vf >= INT_MAX)
2420 return -EINVAL;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002421 err = -EOPNOTSUPP;
2422 if (ops->ndo_set_vf_spoofchk)
2423 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
2424 ivs->setting);
2425 if (err < 0)
2426 return err;
2427 }
2428
2429 if (tb[IFLA_VF_LINK_STATE]) {
2430 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
2431
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002432 if (ivl->vf >= INT_MAX)
2433 return -EINVAL;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002434 err = -EOPNOTSUPP;
2435 if (ops->ndo_set_vf_link_state)
2436 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
2437 ivl->link_state);
2438 if (err < 0)
2439 return err;
2440 }
2441
2442 if (tb[IFLA_VF_RSS_QUERY_EN]) {
2443 struct ifla_vf_rss_query_en *ivrssq_en;
2444
2445 err = -EOPNOTSUPP;
2446 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002447 if (ivrssq_en->vf >= INT_MAX)
2448 return -EINVAL;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002449 if (ops->ndo_set_vf_rss_query_en)
2450 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
2451 ivrssq_en->setting);
2452 if (err < 0)
2453 return err;
2454 }
2455
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00002456 if (tb[IFLA_VF_TRUST]) {
2457 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
2458
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002459 if (ivt->vf >= INT_MAX)
2460 return -EINVAL;
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00002461 err = -EOPNOTSUPP;
2462 if (ops->ndo_set_vf_trust)
2463 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
2464 if (err < 0)
2465 return err;
2466 }
2467
Eli Cohencc8e27c2016-03-11 22:58:34 +02002468 if (tb[IFLA_VF_IB_NODE_GUID]) {
2469 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
2470
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002471 if (ivt->vf >= INT_MAX)
2472 return -EINVAL;
Eli Cohencc8e27c2016-03-11 22:58:34 +02002473 if (!ops->ndo_set_vf_guid)
2474 return -EOPNOTSUPP;
Eli Cohencc8e27c2016-03-11 22:58:34 +02002475 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
2476 }
2477
2478 if (tb[IFLA_VF_IB_PORT_GUID]) {
2479 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
2480
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002481 if (ivt->vf >= INT_MAX)
2482 return -EINVAL;
Eli Cohencc8e27c2016-03-11 22:58:34 +02002483 if (!ops->ndo_set_vf_guid)
2484 return -EOPNOTSUPP;
2485
2486 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
2487 }
2488
Chris Wrightc02db8c2010-05-16 01:05:45 -07002489 return err;
2490}
2491
David Ahern33eaf2a2017-10-04 17:48:46 -07002492static int do_set_master(struct net_device *dev, int ifindex,
2493 struct netlink_ext_ack *extack)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002494{
Jiri Pirko898e5062013-01-03 22:48:52 +00002495 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002496 const struct net_device_ops *ops;
2497 int err;
2498
Jiri Pirko898e5062013-01-03 22:48:52 +00002499 if (upper_dev) {
2500 if (upper_dev->ifindex == ifindex)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002501 return 0;
Jiri Pirko898e5062013-01-03 22:48:52 +00002502 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002503 if (ops->ndo_del_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002504 err = ops->ndo_del_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002505 if (err)
2506 return err;
2507 } else {
2508 return -EOPNOTSUPP;
2509 }
2510 }
2511
2512 if (ifindex) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002513 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
2514 if (!upper_dev)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002515 return -EINVAL;
Jiri Pirko898e5062013-01-03 22:48:52 +00002516 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002517 if (ops->ndo_add_slave) {
David Ahern33eaf2a2017-10-04 17:48:46 -07002518 err = ops->ndo_add_slave(upper_dev, dev, extack);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002519 if (err)
2520 return err;
2521 } else {
2522 return -EOPNOTSUPP;
2523 }
2524 }
2525 return 0;
2526}
2527
Roopa Prabhu829eb202020-07-31 17:34:01 -07002528static const struct nla_policy ifla_proto_down_reason_policy[IFLA_PROTO_DOWN_REASON_VALUE + 1] = {
2529 [IFLA_PROTO_DOWN_REASON_MASK] = { .type = NLA_U32 },
2530 [IFLA_PROTO_DOWN_REASON_VALUE] = { .type = NLA_U32 },
2531};
2532
2533static int do_set_proto_down(struct net_device *dev,
2534 struct nlattr *nl_proto_down,
2535 struct nlattr *nl_proto_down_reason,
2536 struct netlink_ext_ack *extack)
2537{
2538 struct nlattr *pdreason[IFLA_PROTO_DOWN_REASON_MAX + 1];
2539 const struct net_device_ops *ops = dev->netdev_ops;
2540 unsigned long mask = 0;
2541 u32 value;
2542 bool proto_down;
2543 int err;
2544
2545 if (!ops->ndo_change_proto_down) {
2546 NL_SET_ERR_MSG(extack, "Protodown not supported by device");
2547 return -EOPNOTSUPP;
2548 }
2549
2550 if (nl_proto_down_reason) {
2551 err = nla_parse_nested_deprecated(pdreason,
2552 IFLA_PROTO_DOWN_REASON_MAX,
2553 nl_proto_down_reason,
2554 ifla_proto_down_reason_policy,
2555 NULL);
2556 if (err < 0)
2557 return err;
2558
2559 if (!pdreason[IFLA_PROTO_DOWN_REASON_VALUE]) {
2560 NL_SET_ERR_MSG(extack, "Invalid protodown reason value");
2561 return -EINVAL;
2562 }
2563
2564 value = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_VALUE]);
2565
2566 if (pdreason[IFLA_PROTO_DOWN_REASON_MASK])
2567 mask = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_MASK]);
2568
2569 dev_change_proto_down_reason(dev, mask, value);
2570 }
2571
2572 if (nl_proto_down) {
2573 proto_down = nla_get_u8(nl_proto_down);
2574
2575 /* Dont turn off protodown if there are active reasons */
2576 if (!proto_down && dev->proto_down_reason) {
2577 NL_SET_ERR_MSG(extack, "Cannot clear protodown, active reasons");
2578 return -EBUSY;
2579 }
2580 err = dev_change_proto_down(dev,
2581 proto_down);
2582 if (err)
2583 return err;
2584 }
2585
2586 return 0;
2587}
2588
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002589#define DO_SETLINK_MODIFIED 0x01
Nicolas Dichtelba998902014-09-01 16:07:29 +02002590/* notify flag means notify + modified. */
2591#define DO_SETLINK_NOTIFY 0x03
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002592static int do_setlink(const struct sk_buff *skb,
2593 struct net_device *dev, struct ifinfomsg *ifm,
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002594 struct netlink_ext_ack *extack,
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002595 struct nlattr **tb, char *ifname, int status)
Patrick McHardy0157f602007-06-13 12:03:36 -07002596{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002597 const struct net_device_ops *ops = dev->netdev_ops;
Patrick McHardy0157f602007-06-13 12:03:36 -07002598 int err;
2599
Eric Dumazet644c7ee2018-06-05 09:25:19 -07002600 err = validate_linkmsg(dev, tb);
2601 if (err < 0)
2602 return err;
2603
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002604 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) {
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002605 struct net *net = rtnl_link_get_net_capable(skb, dev_net(dev),
2606 tb, CAP_NET_ADMIN);
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002607 if (IS_ERR(net)) {
2608 err = PTR_ERR(net);
2609 goto errout;
2610 }
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002611
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002612 err = dev_change_net_namespace(dev, net, ifname);
2613 put_net(net);
2614 if (err)
2615 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002616 status |= DO_SETLINK_MODIFIED;
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002617 }
2618
Patrick McHardy0157f602007-06-13 12:03:36 -07002619 if (tb[IFLA_MAP]) {
2620 struct rtnl_link_ifmap *u_map;
2621 struct ifmap k_map;
2622
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002623 if (!ops->ndo_set_config) {
Patrick McHardy0157f602007-06-13 12:03:36 -07002624 err = -EOPNOTSUPP;
2625 goto errout;
2626 }
2627
2628 if (!netif_device_present(dev)) {
2629 err = -ENODEV;
2630 goto errout;
2631 }
2632
2633 u_map = nla_data(tb[IFLA_MAP]);
2634 k_map.mem_start = (unsigned long) u_map->mem_start;
2635 k_map.mem_end = (unsigned long) u_map->mem_end;
2636 k_map.base_addr = (unsigned short) u_map->base_addr;
2637 k_map.irq = (unsigned char) u_map->irq;
2638 k_map.dma = (unsigned char) u_map->dma;
2639 k_map.port = (unsigned char) u_map->port;
2640
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002641 err = ops->ndo_set_config(dev, &k_map);
Patrick McHardy0157f602007-06-13 12:03:36 -07002642 if (err < 0)
2643 goto errout;
2644
Nicolas Dichtelba998902014-09-01 16:07:29 +02002645 status |= DO_SETLINK_NOTIFY;
Patrick McHardy0157f602007-06-13 12:03:36 -07002646 }
2647
2648 if (tb[IFLA_ADDRESS]) {
2649 struct sockaddr *sa;
2650 int len;
2651
WANG Cong153711f2017-07-20 11:27:57 -07002652 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2653 sizeof(*sa));
Patrick McHardy0157f602007-06-13 12:03:36 -07002654 sa = kmalloc(len, GFP_KERNEL);
2655 if (!sa) {
2656 err = -ENOMEM;
2657 goto errout;
2658 }
2659 sa->sa_family = dev->type;
2660 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
2661 dev->addr_len);
Petr Machata3a37a962018-12-13 11:54:30 +00002662 err = dev_set_mac_address(dev, sa, extack);
Patrick McHardy0157f602007-06-13 12:03:36 -07002663 kfree(sa);
2664 if (err)
2665 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002666 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002667 }
2668
2669 if (tb[IFLA_MTU]) {
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07002670 err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack);
Patrick McHardy0157f602007-06-13 12:03:36 -07002671 if (err < 0)
2672 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002673 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002674 }
2675
Vlad Dogarucbda10f2011-01-13 23:38:30 +00002676 if (tb[IFLA_GROUP]) {
2677 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Nicolas Dichtelba998902014-09-01 16:07:29 +02002678 status |= DO_SETLINK_NOTIFY;
Vlad Dogarucbda10f2011-01-13 23:38:30 +00002679 }
2680
Patrick McHardy0157f602007-06-13 12:03:36 -07002681 /*
2682 * Interface selected by interface index but interface
2683 * name provided implies that a name change has been
2684 * requested.
2685 */
2686 if (ifm->ifi_index > 0 && ifname[0]) {
2687 err = dev_change_name(dev, ifname);
2688 if (err < 0)
2689 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002690 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002691 }
2692
Stephen Hemminger0b815a12008-09-22 21:28:11 -07002693 if (tb[IFLA_IFALIAS]) {
2694 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2695 nla_len(tb[IFLA_IFALIAS]));
2696 if (err < 0)
2697 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002698 status |= DO_SETLINK_NOTIFY;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07002699 }
2700
Patrick McHardy0157f602007-06-13 12:03:36 -07002701 if (tb[IFLA_BROADCAST]) {
2702 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00002703 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Patrick McHardy0157f602007-06-13 12:03:36 -07002704 }
2705
2706 if (ifm->ifi_flags || ifm->ifi_change) {
Petr Machata567c5e12018-12-06 17:05:42 +00002707 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
2708 extack);
Johannes Berg5f9021c2008-11-16 23:20:31 -08002709 if (err < 0)
2710 goto errout;
Patrick McHardy0157f602007-06-13 12:03:36 -07002711 }
2712
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002713 if (tb[IFLA_MASTER]) {
David Ahern33eaf2a2017-10-04 17:48:46 -07002714 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002715 if (err)
2716 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002717 status |= DO_SETLINK_MODIFIED;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002718 }
2719
Jiri Pirko9a572472012-12-27 23:49:39 +00002720 if (tb[IFLA_CARRIER]) {
2721 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2722 if (err)
2723 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002724 status |= DO_SETLINK_MODIFIED;
Jiri Pirko9a572472012-12-27 23:49:39 +00002725 }
2726
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002727 if (tb[IFLA_TXQLEN]) {
Alexey Dobriyan0cd29502017-05-17 13:30:44 +03002728 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002729
Cong Wang6a643dd2018-01-25 18:26:22 -08002730 err = dev_change_tx_queue_len(dev, value);
2731 if (err)
2732 goto errout;
2733 status |= DO_SETLINK_MODIFIED;
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002734 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002735
Stephen Hemminger46e6b992017-12-07 15:40:19 -08002736 if (tb[IFLA_GSO_MAX_SIZE]) {
2737 u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]);
2738
2739 if (max_size > GSO_MAX_SIZE) {
2740 err = -EINVAL;
2741 goto errout;
2742 }
2743
2744 if (dev->gso_max_size ^ max_size) {
2745 netif_set_gso_max_size(dev, max_size);
2746 status |= DO_SETLINK_MODIFIED;
2747 }
2748 }
2749
2750 if (tb[IFLA_GSO_MAX_SEGS]) {
2751 u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
2752
2753 if (max_segs > GSO_MAX_SEGS) {
2754 err = -EINVAL;
2755 goto errout;
2756 }
2757
2758 if (dev->gso_max_segs ^ max_segs) {
2759 dev->gso_max_segs = max_segs;
2760 status |= DO_SETLINK_MODIFIED;
2761 }
2762 }
2763
Patrick McHardy0157f602007-06-13 12:03:36 -07002764 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002765 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Patrick McHardy0157f602007-06-13 12:03:36 -07002766
2767 if (tb[IFLA_LINKMODE]) {
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002768 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2769
David S. Miller93b2d4a2008-02-17 18:35:07 -08002770 write_lock_bh(&dev_base_lock);
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002771 if (dev->link_mode ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02002772 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002773 dev->link_mode = value;
David S. Miller93b2d4a2008-02-17 18:35:07 -08002774 write_unlock_bh(&dev_base_lock);
Patrick McHardy0157f602007-06-13 12:03:36 -07002775 }
2776
Chris Wrightc02db8c2010-05-16 01:05:45 -07002777 if (tb[IFLA_VFINFO_LIST]) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002778 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
Chris Wrightc02db8c2010-05-16 01:05:45 -07002779 struct nlattr *attr;
2780 int rem;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002781
Chris Wrightc02db8c2010-05-16 01:05:45 -07002782 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002783 if (nla_type(attr) != IFLA_VF_INFO ||
2784 nla_len(attr) < NLA_HDRLEN) {
David Howells253683b2010-05-21 02:25:27 +00002785 err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002786 goto errout;
David Howells253683b2010-05-21 02:25:27 +00002787 }
Johannes Berg8cb08172019-04-26 14:07:28 +02002788 err = nla_parse_nested_deprecated(vfinfo, IFLA_VF_MAX,
2789 attr,
2790 ifla_vf_policy,
2791 NULL);
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002792 if (err < 0)
2793 goto errout;
2794 err = do_setvfinfo(dev, vfinfo);
Chris Wrightc02db8c2010-05-16 01:05:45 -07002795 if (err < 0)
2796 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002797 status |= DO_SETLINK_NOTIFY;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002798 }
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00002799 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002800 err = 0;
2801
Scott Feldman57b61082010-05-17 22:49:55 -07002802 if (tb[IFLA_VF_PORTS]) {
2803 struct nlattr *port[IFLA_PORT_MAX+1];
2804 struct nlattr *attr;
2805 int vf;
2806 int rem;
2807
2808 err = -EOPNOTSUPP;
2809 if (!ops->ndo_set_vf_port)
2810 goto errout;
2811
2812 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
Daniel Borkmann035d2102015-07-13 00:06:02 +02002813 if (nla_type(attr) != IFLA_VF_PORT ||
2814 nla_len(attr) < NLA_HDRLEN) {
2815 err = -EINVAL;
2816 goto errout;
2817 }
Johannes Berg8cb08172019-04-26 14:07:28 +02002818 err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
2819 attr,
2820 ifla_port_policy,
2821 NULL);
Scott Feldman57b61082010-05-17 22:49:55 -07002822 if (err < 0)
2823 goto errout;
2824 if (!port[IFLA_PORT_VF]) {
2825 err = -EOPNOTSUPP;
2826 goto errout;
2827 }
2828 vf = nla_get_u32(port[IFLA_PORT_VF]);
2829 err = ops->ndo_set_vf_port(dev, vf, port);
2830 if (err < 0)
2831 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002832 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07002833 }
2834 }
2835 err = 0;
2836
2837 if (tb[IFLA_PORT_SELF]) {
2838 struct nlattr *port[IFLA_PORT_MAX+1];
2839
Johannes Berg8cb08172019-04-26 14:07:28 +02002840 err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
2841 tb[IFLA_PORT_SELF],
2842 ifla_port_policy, NULL);
Scott Feldman57b61082010-05-17 22:49:55 -07002843 if (err < 0)
2844 goto errout;
2845
2846 err = -EOPNOTSUPP;
2847 if (ops->ndo_set_vf_port)
2848 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
2849 if (err < 0)
2850 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002851 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07002852 }
Thomas Graff8ff1822010-11-16 04:30:14 +00002853
2854 if (tb[IFLA_AF_SPEC]) {
2855 struct nlattr *af;
2856 int rem;
2857
2858 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2859 const struct rtnl_af_ops *af_ops;
2860
Florian Westphal5fa85a02017-10-16 15:44:36 +02002861 rcu_read_lock();
2862
Gustavo A. R. Silva058c8d52017-10-20 19:43:11 -05002863 BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
Thomas Graff8ff1822010-11-16 04:30:14 +00002864
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002865 err = af_ops->set_link_af(dev, af);
Florian Westphal5fa85a02017-10-16 15:44:36 +02002866 if (err < 0) {
2867 rcu_read_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +00002868 goto errout;
Florian Westphal5fa85a02017-10-16 15:44:36 +02002869 }
Thomas Graff8ff1822010-11-16 04:30:14 +00002870
Florian Westphal5fa85a02017-10-16 15:44:36 +02002871 rcu_read_unlock();
Nicolas Dichtelba998902014-09-01 16:07:29 +02002872 status |= DO_SETLINK_NOTIFY;
Thomas Graff8ff1822010-11-16 04:30:14 +00002873 }
2874 }
Scott Feldman57b61082010-05-17 22:49:55 -07002875 err = 0;
2876
Roopa Prabhu829eb202020-07-31 17:34:01 -07002877 if (tb[IFLA_PROTO_DOWN] || tb[IFLA_PROTO_DOWN_REASON]) {
2878 err = do_set_proto_down(dev, tb[IFLA_PROTO_DOWN],
2879 tb[IFLA_PROTO_DOWN_REASON], extack);
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07002880 if (err)
2881 goto errout;
2882 status |= DO_SETLINK_NOTIFY;
2883 }
2884
Brenden Blancod1fdd912016-07-19 12:16:49 -07002885 if (tb[IFLA_XDP]) {
2886 struct nlattr *xdp[IFLA_XDP_MAX + 1];
Daniel Borkmann85de8572016-11-28 23:16:54 +01002887 u32 xdp_flags = 0;
Brenden Blancod1fdd912016-07-19 12:16:49 -07002888
Johannes Berg8cb08172019-04-26 14:07:28 +02002889 err = nla_parse_nested_deprecated(xdp, IFLA_XDP_MAX,
2890 tb[IFLA_XDP],
2891 ifla_xdp_policy, NULL);
Brenden Blancod1fdd912016-07-19 12:16:49 -07002892 if (err < 0)
2893 goto errout;
2894
Martin KaFai Lau58038692017-06-15 17:29:09 -07002895 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
Brenden Blanco262d8622016-07-20 17:22:34 -07002896 err = -EINVAL;
2897 goto errout;
2898 }
Daniel Borkmann85de8572016-11-28 23:16:54 +01002899
2900 if (xdp[IFLA_XDP_FLAGS]) {
2901 xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
2902 if (xdp_flags & ~XDP_FLAGS_MASK) {
2903 err = -EINVAL;
2904 goto errout;
2905 }
Jakub Kicinskiee5d0322017-06-21 18:25:04 -07002906 if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
Daniel Borkmann0489df92017-05-12 01:04:45 +02002907 err = -EINVAL;
2908 goto errout;
2909 }
Daniel Borkmann85de8572016-11-28 23:16:54 +01002910 }
2911
Brenden Blancod1fdd912016-07-19 12:16:49 -07002912 if (xdp[IFLA_XDP_FD]) {
Toke Høiland-Jørgensen92234c82020-03-25 18:23:26 +01002913 int expected_fd = -1;
2914
2915 if (xdp_flags & XDP_FLAGS_REPLACE) {
2916 if (!xdp[IFLA_XDP_EXPECTED_FD]) {
2917 err = -EINVAL;
2918 goto errout;
2919 }
2920 expected_fd =
2921 nla_get_s32(xdp[IFLA_XDP_EXPECTED_FD]);
2922 }
2923
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002924 err = dev_change_xdp_fd(dev, extack,
Daniel Borkmann85de8572016-11-28 23:16:54 +01002925 nla_get_s32(xdp[IFLA_XDP_FD]),
Toke Høiland-Jørgensen92234c82020-03-25 18:23:26 +01002926 expected_fd,
Daniel Borkmann85de8572016-11-28 23:16:54 +01002927 xdp_flags);
Brenden Blancod1fdd912016-07-19 12:16:49 -07002928 if (err)
2929 goto errout;
2930 status |= DO_SETLINK_NOTIFY;
2931 }
2932 }
2933
Patrick McHardy0157f602007-06-13 12:03:36 -07002934errout:
Nicolas Dichtelba998902014-09-01 16:07:29 +02002935 if (status & DO_SETLINK_MODIFIED) {
Xin Long64ff90c2017-10-15 18:13:45 +08002936 if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
Nicolas Dichtelba998902014-09-01 16:07:29 +02002937 netdev_state_change(dev);
2938
2939 if (err < 0)
2940 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",
2941 dev->name);
2942 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002943
Patrick McHardy0157f602007-06-13 12:03:36 -07002944 return err;
2945}
2946
Jiri Pirkocc6090e2019-09-30 11:48:19 +02002947static struct net_device *rtnl_dev_get(struct net *net,
2948 struct nlattr *ifname_attr,
Jiri Pirko76c9ac02019-09-30 11:48:20 +02002949 struct nlattr *altifname_attr,
Jiri Pirkocc6090e2019-09-30 11:48:19 +02002950 char *ifname)
2951{
Jiri Pirko76c9ac02019-09-30 11:48:20 +02002952 char buffer[ALTIFNAMSIZ];
Jiri Pirkocc6090e2019-09-30 11:48:19 +02002953
2954 if (!ifname) {
2955 ifname = buffer;
2956 if (ifname_attr)
2957 nla_strlcpy(ifname, ifname_attr, IFNAMSIZ);
Jiri Pirko76c9ac02019-09-30 11:48:20 +02002958 else if (altifname_attr)
2959 nla_strlcpy(ifname, altifname_attr, ALTIFNAMSIZ);
Jiri Pirkocc6090e2019-09-30 11:48:19 +02002960 else
2961 return NULL;
2962 }
2963
2964 return __dev_get_by_name(net, ifname);
2965}
2966
David Ahernc21ef3e2017-04-16 09:48:24 -07002967static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2968 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002970 struct net *net = sock_net(skb->sk);
Thomas Grafda5e0492006-08-10 21:17:37 -07002971 struct ifinfomsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 struct net_device *dev;
Patrick McHardy0157f602007-06-13 12:03:36 -07002973 int err;
Thomas Grafda5e0492006-08-10 21:17:37 -07002974 struct nlattr *tb[IFLA_MAX+1];
2975 char ifname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976
Johannes Berg8cb08172019-04-26 14:07:28 +02002977 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
2978 ifla_policy, extack);
Thomas Grafda5e0492006-08-10 21:17:37 -07002979 if (err < 0)
2980 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002982 err = rtnl_ensure_unique_netns(tb, extack, false);
2983 if (err < 0)
2984 goto errout;
2985
Thomas Graf5176f912006-08-26 20:13:18 -07002986 if (tb[IFLA_IFNAME])
2987 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
Patrick McHardy78e5b8912006-09-13 20:35:36 -07002988 else
2989 ifname[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 err = -EINVAL;
Thomas Grafda5e0492006-08-10 21:17:37 -07002992 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -07002993 if (ifm->ifi_index > 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00002994 dev = __dev_get_by_index(net, ifm->ifi_index);
Jiri Pirko76c9ac02019-09-30 11:48:20 +02002995 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
2996 dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname);
Thomas Grafda5e0492006-08-10 21:17:37 -07002997 else
2998 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999
Thomas Grafda5e0492006-08-10 21:17:37 -07003000 if (dev == NULL) {
3001 err = -ENODEV;
3002 goto errout;
3003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07003005 err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
Thomas Grafda5e0492006-08-10 21:17:37 -07003006errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 return err;
3008}
3009
WANG Cong66400d52015-03-24 11:53:31 -07003010static int rtnl_group_dellink(const struct net *net, int group)
3011{
3012 struct net_device *dev, *aux;
3013 LIST_HEAD(list_kill);
3014 bool found = false;
3015
3016 if (!group)
3017 return -EPERM;
3018
3019 for_each_netdev(net, dev) {
3020 if (dev->group == group) {
3021 const struct rtnl_link_ops *ops;
3022
3023 found = true;
3024 ops = dev->rtnl_link_ops;
3025 if (!ops || !ops->dellink)
3026 return -EOPNOTSUPP;
3027 }
3028 }
3029
3030 if (!found)
3031 return -ENODEV;
3032
3033 for_each_netdev_safe(net, dev, aux) {
3034 if (dev->group == group) {
3035 const struct rtnl_link_ops *ops;
3036
3037 ops = dev->rtnl_link_ops;
3038 ops->dellink(dev, &list_kill);
3039 }
3040 }
3041 unregister_netdevice_many(&list_kill);
3042
3043 return 0;
3044}
3045
Thomas Graf614732e2015-07-21 10:44:06 +02003046int rtnl_delete_link(struct net_device *dev)
3047{
3048 const struct rtnl_link_ops *ops;
3049 LIST_HEAD(list_kill);
3050
3051 ops = dev->rtnl_link_ops;
3052 if (!ops || !ops->dellink)
3053 return -EOPNOTSUPP;
3054
3055 ops->dellink(dev, &list_kill);
3056 unregister_netdevice_many(&list_kill);
3057
3058 return 0;
3059}
3060EXPORT_SYMBOL_GPL(rtnl_delete_link);
3061
David Ahernc21ef3e2017-04-16 09:48:24 -07003062static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
3063 struct netlink_ext_ack *extack)
Patrick McHardy38f7b872007-06-13 12:03:51 -07003064{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003065 struct net *net = sock_net(skb->sk);
Christian Braunerb61ad682018-01-24 15:26:34 +01003066 struct net *tgt_net = net;
3067 struct net_device *dev = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003068 struct ifinfomsg *ifm;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003069 struct nlattr *tb[IFLA_MAX+1];
3070 int err;
Christian Braunerb61ad682018-01-24 15:26:34 +01003071 int netnsid = -1;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003072
Johannes Berg8cb08172019-04-26 14:07:28 +02003073 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3074 ifla_policy, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07003075 if (err < 0)
3076 return err;
3077
Christian Brauner4ff66ca2018-02-07 13:53:20 +01003078 err = rtnl_ensure_unique_netns(tb, extack, true);
3079 if (err < 0)
3080 return err;
3081
Christian Brauner7e4a8d52018-09-04 21:53:53 +02003082 if (tb[IFLA_TARGET_NETNSID]) {
3083 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
Christian Braunerc383edc2018-09-04 21:53:47 +02003084 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
Christian Braunerb61ad682018-01-24 15:26:34 +01003085 if (IS_ERR(tgt_net))
3086 return PTR_ERR(tgt_net);
3087 }
3088
3089 err = -EINVAL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003090 ifm = nlmsg_data(nlh);
3091 if (ifm->ifi_index > 0)
Christian Braunerb61ad682018-01-24 15:26:34 +01003092 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
Jiri Pirko76c9ac02019-09-30 11:48:20 +02003093 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3094 dev = rtnl_dev_get(net, tb[IFLA_IFNAME],
3095 tb[IFLA_ALT_IFNAME], NULL);
WANG Cong66400d52015-03-24 11:53:31 -07003096 else if (tb[IFLA_GROUP])
Christian Braunerb61ad682018-01-24 15:26:34 +01003097 err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP]));
Patrick McHardy38f7b872007-06-13 12:03:51 -07003098 else
Christian Braunerb61ad682018-01-24 15:26:34 +01003099 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003100
Christian Braunerb61ad682018-01-24 15:26:34 +01003101 if (!dev) {
3102 if (tb[IFLA_IFNAME] || ifm->ifi_index > 0)
3103 err = -ENODEV;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003104
Christian Braunerb61ad682018-01-24 15:26:34 +01003105 goto out;
3106 }
3107
3108 err = rtnl_delete_link(dev);
3109
3110out:
3111 if (netnsid >= 0)
3112 put_net(tgt_net);
3113
3114 return err;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003115}
3116
Patrick McHardy3729d502010-02-26 06:34:54 +00003117int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
3118{
3119 unsigned int old_flags;
3120 int err;
3121
3122 old_flags = dev->flags;
3123 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
Petr Machata6d040322018-12-06 17:05:43 +00003124 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
3125 NULL);
Patrick McHardy3729d502010-02-26 06:34:54 +00003126 if (err < 0)
3127 return err;
3128 }
3129
Roopa Prabhu8d356b82018-07-04 16:46:29 -07003130 if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) {
Roopa Prabhu56a49d72018-09-12 13:21:48 -07003131 __dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags));
Roopa Prabhu8d356b82018-07-04 16:46:29 -07003132 } else {
3133 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
3134 __dev_notify_flags(dev, old_flags, ~0U);
3135 }
Patrick McHardy3729d502010-02-26 06:34:54 +00003136 return 0;
3137}
3138EXPORT_SYMBOL(rtnl_configure_link);
3139
David Ahernd0522f12018-11-06 12:51:14 -08003140struct net_device *rtnl_create_link(struct net *net, const char *ifname,
3141 unsigned char name_assign_type,
3142 const struct rtnl_link_ops *ops,
3143 struct nlattr *tb[],
3144 struct netlink_ext_ack *extack)
Pavel Emelianove7199282007-08-08 22:16:38 -07003145{
Pavel Emelianove7199282007-08-08 22:16:38 -07003146 struct net_device *dev;
Jiri Pirkod40156a2012-07-20 02:28:47 +00003147 unsigned int num_tx_queues = 1;
3148 unsigned int num_rx_queues = 1;
Pavel Emelianove7199282007-08-08 22:16:38 -07003149
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00003150 if (tb[IFLA_NUM_TX_QUEUES])
3151 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
3152 else if (ops->get_num_tx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00003153 num_tx_queues = ops->get_num_tx_queues();
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00003154
3155 if (tb[IFLA_NUM_RX_QUEUES])
3156 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
3157 else if (ops->get_num_rx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00003158 num_rx_queues = ops->get_num_rx_queues();
stephen hemmingerefacb302012-04-10 18:34:43 +00003159
David Ahernd0522f12018-11-06 12:51:14 -08003160 if (num_tx_queues < 1 || num_tx_queues > 4096) {
3161 NL_SET_ERR_MSG(extack, "Invalid number of transmit queues");
Eric Dumazet0e1d6ec2018-10-02 15:47:35 -07003162 return ERR_PTR(-EINVAL);
David Ahernd0522f12018-11-06 12:51:14 -08003163 }
Eric Dumazet0e1d6ec2018-10-02 15:47:35 -07003164
David Ahernd0522f12018-11-06 12:51:14 -08003165 if (num_rx_queues < 1 || num_rx_queues > 4096) {
3166 NL_SET_ERR_MSG(extack, "Invalid number of receive queues");
Eric Dumazet0e1d6ec2018-10-02 15:47:35 -07003167 return ERR_PTR(-EINVAL);
David Ahernd0522f12018-11-06 12:51:14 -08003168 }
Eric Dumazet0e1d6ec2018-10-02 15:47:35 -07003169
Tom Gundersen55177502014-07-14 16:37:25 +02003170 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
Tom Gundersenc835a672014-07-14 16:37:24 +02003171 ops->setup, num_tx_queues, num_rx_queues);
Pavel Emelianove7199282007-08-08 22:16:38 -07003172 if (!dev)
Tobias Klauserd1892e42017-02-20 16:32:06 +01003173 return ERR_PTR(-ENOMEM);
Pavel Emelianove7199282007-08-08 22:16:38 -07003174
Eric W. Biederman81adee42009-11-08 00:53:51 -08003175 dev_net_set(dev, net);
3176 dev->rtnl_link_ops = ops;
Patrick McHardy3729d502010-02-26 06:34:54 +00003177 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
Eric W. Biederman81adee42009-11-08 00:53:51 -08003178
Eric Dumazetd836f5c2020-01-21 22:47:29 -08003179 if (tb[IFLA_MTU]) {
3180 u32 mtu = nla_get_u32(tb[IFLA_MTU]);
3181 int err;
3182
3183 err = dev_validate_mtu(dev, mtu, extack);
3184 if (err) {
3185 free_netdev(dev);
3186 return ERR_PTR(err);
3187 }
3188 dev->mtu = mtu;
3189 }
Jiri Pirko2afb9b52013-01-06 12:41:57 +00003190 if (tb[IFLA_ADDRESS]) {
Pavel Emelianove7199282007-08-08 22:16:38 -07003191 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
3192 nla_len(tb[IFLA_ADDRESS]));
Jiri Pirko2afb9b52013-01-06 12:41:57 +00003193 dev->addr_assign_type = NET_ADDR_SET;
3194 }
Pavel Emelianove7199282007-08-08 22:16:38 -07003195 if (tb[IFLA_BROADCAST])
3196 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
3197 nla_len(tb[IFLA_BROADCAST]));
3198 if (tb[IFLA_TXQLEN])
3199 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
3200 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08003201 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Pavel Emelianove7199282007-08-08 22:16:38 -07003202 if (tb[IFLA_LINKMODE])
3203 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
Patrick McHardyffa934f2011-01-20 03:00:42 +00003204 if (tb[IFLA_GROUP])
3205 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Stephen Hemminger46e6b992017-12-07 15:40:19 -08003206 if (tb[IFLA_GSO_MAX_SIZE])
3207 netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE]));
3208 if (tb[IFLA_GSO_MAX_SEGS])
Stephen Hemmingera0b586f2017-12-08 15:34:13 -08003209 dev->gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
Pavel Emelianove7199282007-08-08 22:16:38 -07003210
3211 return dev;
Pavel Emelianove7199282007-08-08 22:16:38 -07003212}
Eric Dumazete0d087a2009-11-07 01:26:17 -08003213EXPORT_SYMBOL(rtnl_create_link);
Pavel Emelianove7199282007-08-08 22:16:38 -07003214
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07003215static int rtnl_group_changelink(const struct sk_buff *skb,
3216 struct net *net, int group,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00003217 struct ifinfomsg *ifm,
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07003218 struct netlink_ext_ack *extack,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00003219 struct nlattr **tb)
3220{
WANG Congd0795352015-03-23 16:31:09 -07003221 struct net_device *dev, *aux;
Vlad Dogarue7ed8282011-01-13 23:38:31 +00003222 int err;
3223
WANG Congd0795352015-03-23 16:31:09 -07003224 for_each_netdev_safe(net, dev, aux) {
Vlad Dogarue7ed8282011-01-13 23:38:31 +00003225 if (dev->group == group) {
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07003226 err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00003227 if (err < 0)
3228 return err;
3229 }
3230 }
3231
3232 return 0;
3233}
3234
Jakub Kicinskia2939742018-11-27 22:32:31 -08003235static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3236 struct nlattr **attr, struct netlink_ext_ack *extack)
Patrick McHardy38f7b872007-06-13 12:03:51 -07003237{
Jakub Kicinski420d0312018-11-27 22:32:30 -08003238 struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1];
3239 unsigned char name_assign_type = NET_NAME_USER;
3240 struct nlattr *linkinfo[IFLA_INFO_MAX + 1];
3241 const struct rtnl_link_ops *m_ops = NULL;
Jakub Kicinski420d0312018-11-27 22:32:30 -08003242 struct net_device *master_dev = NULL;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003243 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07003244 const struct rtnl_link_ops *ops;
Jakub Kicinski420d0312018-11-27 22:32:30 -08003245 struct nlattr *tb[IFLA_MAX + 1];
3246 struct net *dest_net, *link_net;
3247 struct nlattr **slave_data;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003248 char kind[MODULE_NAME_LEN];
Jakub Kicinski420d0312018-11-27 22:32:30 -08003249 struct net_device *dev;
3250 struct ifinfomsg *ifm;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003251 char ifname[IFNAMSIZ];
Jakub Kicinski420d0312018-11-27 22:32:30 -08003252 struct nlattr **data;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003253 int err;
3254
Johannes Berg95a5afc2008-10-16 15:24:51 -07003255#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07003256replay:
Thomas Graf8072f082007-07-31 14:13:50 -07003257#endif
Johannes Berg8cb08172019-04-26 14:07:28 +02003258 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3259 ifla_policy, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07003260 if (err < 0)
3261 return err;
3262
Christian Brauner4ff66ca2018-02-07 13:53:20 +01003263 err = rtnl_ensure_unique_netns(tb, extack, false);
3264 if (err < 0)
3265 return err;
3266
Patrick McHardy38f7b872007-06-13 12:03:51 -07003267 if (tb[IFLA_IFNAME])
3268 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
3269 else
3270 ifname[0] = '\0';
3271
3272 ifm = nlmsg_data(nlh);
3273 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07003274 dev = __dev_get_by_index(net, ifm->ifi_index);
Jiri Pirko76c9ac02019-09-30 11:48:20 +02003275 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3276 dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname);
Jiri Pirko7af12cb2019-09-30 11:48:18 +02003277 else
3278 dev = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003279
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01003280 if (dev) {
3281 master_dev = netdev_master_upper_dev_get(dev);
3282 if (master_dev)
3283 m_ops = master_dev->rtnl_link_ops;
3284 }
3285
Eric Dumazete0d087a2009-11-07 01:26:17 -08003286 err = validate_linkmsg(dev, tb);
3287 if (err < 0)
Thomas Graf1840bb12008-02-23 19:54:36 -08003288 return err;
3289
Patrick McHardy38f7b872007-06-13 12:03:51 -07003290 if (tb[IFLA_LINKINFO]) {
Johannes Berg8cb08172019-04-26 14:07:28 +02003291 err = nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX,
3292 tb[IFLA_LINKINFO],
3293 ifla_info_policy, NULL);
Patrick McHardy38f7b872007-06-13 12:03:51 -07003294 if (err < 0)
3295 return err;
3296 } else
3297 memset(linkinfo, 0, sizeof(linkinfo));
3298
3299 if (linkinfo[IFLA_INFO_KIND]) {
3300 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
3301 ops = rtnl_link_ops_get(kind);
3302 } else {
3303 kind[0] = '\0';
3304 ops = NULL;
3305 }
3306
Jakub Kicinski420d0312018-11-27 22:32:30 -08003307 data = NULL;
3308 if (ops) {
3309 if (ops->maxtype > RTNL_MAX_TYPE)
3310 return -EINVAL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003311
Jakub Kicinski420d0312018-11-27 22:32:30 -08003312 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
Johannes Berg8cb08172019-04-26 14:07:28 +02003313 err = nla_parse_nested_deprecated(attr, ops->maxtype,
3314 linkinfo[IFLA_INFO_DATA],
3315 ops->policy, extack);
Jakub Kicinski420d0312018-11-27 22:32:30 -08003316 if (err < 0)
3317 return err;
3318 data = attr;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003319 }
Jakub Kicinski420d0312018-11-27 22:32:30 -08003320 if (ops->validate) {
3321 err = ops->validate(tb, data, extack);
3322 if (err < 0)
3323 return err;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01003324 }
Jakub Kicinski420d0312018-11-27 22:32:30 -08003325 }
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01003326
Jakub Kicinski420d0312018-11-27 22:32:30 -08003327 slave_data = NULL;
3328 if (m_ops) {
3329 if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)
3330 return -EINVAL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003331
Jakub Kicinski420d0312018-11-27 22:32:30 -08003332 if (m_ops->slave_maxtype &&
3333 linkinfo[IFLA_INFO_SLAVE_DATA]) {
Johannes Berg8cb08172019-04-26 14:07:28 +02003334 err = nla_parse_nested_deprecated(slave_attr,
3335 m_ops->slave_maxtype,
3336 linkinfo[IFLA_INFO_SLAVE_DATA],
3337 m_ops->slave_policy,
3338 extack);
Jakub Kicinski420d0312018-11-27 22:32:30 -08003339 if (err < 0)
3340 return err;
3341 slave_data = slave_attr;
3342 }
3343 }
3344
3345 if (dev) {
3346 int status = 0;
3347
3348 if (nlh->nlmsg_flags & NLM_F_EXCL)
3349 return -EEXIST;
3350 if (nlh->nlmsg_flags & NLM_F_REPLACE)
3351 return -EOPNOTSUPP;
3352
3353 if (linkinfo[IFLA_INFO_DATA]) {
3354 if (!ops || ops != dev->rtnl_link_ops ||
3355 !ops->changelink)
Patrick McHardy38f7b872007-06-13 12:03:51 -07003356 return -EOPNOTSUPP;
3357
Jakub Kicinski420d0312018-11-27 22:32:30 -08003358 err = ops->changelink(dev, tb, data, extack);
3359 if (err < 0)
3360 return err;
3361 status |= DO_SETLINK_NOTIFY;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003362 }
3363
Jakub Kicinski420d0312018-11-27 22:32:30 -08003364 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
3365 if (!m_ops || !m_ops->slave_changelink)
3366 return -EOPNOTSUPP;
3367
3368 err = m_ops->slave_changelink(master_dev, dev, tb,
3369 slave_data, extack);
3370 if (err < 0)
3371 return err;
3372 status |= DO_SETLINK_NOTIFY;
3373 }
3374
3375 return do_setlink(skb, dev, ifm, extack, tb, ifname, status);
3376 }
3377
3378 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
3379 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
3380 return rtnl_group_changelink(skb, net,
Patrick McHardyffa934f2011-01-20 03:00:42 +00003381 nla_get_u32(tb[IFLA_GROUP]),
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07003382 ifm, extack, tb);
Jakub Kicinski420d0312018-11-27 22:32:30 -08003383 return -ENODEV;
3384 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07003385
Jakub Kicinski420d0312018-11-27 22:32:30 -08003386 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
3387 return -EOPNOTSUPP;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003388
Jakub Kicinski420d0312018-11-27 22:32:30 -08003389 if (!ops) {
Johannes Berg95a5afc2008-10-16 15:24:51 -07003390#ifdef CONFIG_MODULES
Jakub Kicinski420d0312018-11-27 22:32:30 -08003391 if (kind[0]) {
3392 __rtnl_unlock();
3393 request_module("rtnl-link-%s", kind);
3394 rtnl_lock();
3395 ops = rtnl_link_ops_get(kind);
3396 if (ops)
3397 goto replay;
3398 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07003399#endif
Jakub Kicinski420d0312018-11-27 22:32:30 -08003400 NL_SET_ERR_MSG(extack, "Unknown device type");
3401 return -EOPNOTSUPP;
3402 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07003403
Jakub Kicinski420d0312018-11-27 22:32:30 -08003404 if (!ops->setup)
3405 return -EOPNOTSUPP;
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02003406
Jakub Kicinski420d0312018-11-27 22:32:30 -08003407 if (!ifname[0]) {
3408 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
3409 name_assign_type = NET_NAME_ENUM;
3410 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07003411
Jakub Kicinski420d0312018-11-27 22:32:30 -08003412 dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN);
3413 if (IS_ERR(dest_net))
3414 return PTR_ERR(dest_net);
Eric W. Biederman13ad1772011-01-29 14:57:22 +00003415
Jakub Kicinski420d0312018-11-27 22:32:30 -08003416 if (tb[IFLA_LINK_NETNSID]) {
3417 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
Nicolas Dichtel317f4812015-01-15 15:11:18 +01003418
Jakub Kicinski420d0312018-11-27 22:32:30 -08003419 link_net = get_net_ns_by_id(dest_net, id);
3420 if (!link_net) {
3421 NL_SET_ERR_MSG(extack, "Unknown network namespace id");
3422 err = -EINVAL;
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00003423 goto out;
3424 }
Jakub Kicinski420d0312018-11-27 22:32:30 -08003425 err = -EPERM;
3426 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
3427 goto out;
3428 } else {
3429 link_net = NULL;
3430 }
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00003431
Jakub Kicinski420d0312018-11-27 22:32:30 -08003432 dev = rtnl_create_link(link_net ? : dest_net, ifname,
3433 name_assign_type, ops, tb, extack);
3434 if (IS_ERR(dev)) {
3435 err = PTR_ERR(dev);
David S. Miller43638902015-03-10 21:58:32 -04003436 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003437 }
Jakub Kicinski420d0312018-11-27 22:32:30 -08003438
3439 dev->ifindex = ifm->ifi_index;
3440
3441 if (ops->newlink) {
3442 err = ops->newlink(link_net ? : net, dev, tb, data, extack);
3443 /* Drivers should call free_netdev() in ->destructor
3444 * and unregister it on failure after registration
3445 * so that device could be finally freed in rtnl_unlock.
3446 */
3447 if (err < 0) {
3448 /* If device is not registered at all, free it now */
Weilong Chencebb6972020-07-15 20:58:10 +08003449 if (dev->reg_state == NETREG_UNINITIALIZED ||
3450 dev->reg_state == NETREG_UNREGISTERED)
Jakub Kicinski420d0312018-11-27 22:32:30 -08003451 free_netdev(dev);
3452 goto out;
3453 }
3454 } else {
3455 err = register_netdevice(dev);
3456 if (err < 0) {
3457 free_netdev(dev);
3458 goto out;
3459 }
3460 }
3461 err = rtnl_configure_link(dev, ifm);
3462 if (err < 0)
3463 goto out_unregister;
3464 if (link_net) {
3465 err = dev_change_net_namespace(dev, dest_net, ifname);
3466 if (err < 0)
3467 goto out_unregister;
3468 }
3469 if (tb[IFLA_MASTER]) {
3470 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
3471 if (err)
3472 goto out_unregister;
3473 }
3474out:
3475 if (link_net)
3476 put_net(link_net);
3477 put_net(dest_net);
3478 return err;
3479out_unregister:
3480 if (ops->newlink) {
3481 LIST_HEAD(list_kill);
3482
3483 ops->dellink(dev, &list_kill);
3484 unregister_netdevice_many(&list_kill);
3485 } else {
3486 unregister_netdevice(dev);
3487 }
3488 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003489}
3490
Jakub Kicinskia2939742018-11-27 22:32:31 -08003491static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3492 struct netlink_ext_ack *extack)
3493{
3494 struct nlattr **attr;
3495 int ret;
3496
3497 attr = kmalloc_array(RTNL_MAX_TYPE + 1, sizeof(*attr), GFP_KERNEL);
3498 if (!attr)
3499 return -ENOMEM;
3500
3501 ret = __rtnl_newlink(skb, nlh, attr, extack);
3502 kfree(attr);
3503 return ret;
3504}
3505
Jakub Kicinski9b3757b2019-01-18 10:46:16 -08003506static int rtnl_valid_getlink_req(struct sk_buff *skb,
3507 const struct nlmsghdr *nlh,
3508 struct nlattr **tb,
3509 struct netlink_ext_ack *extack)
3510{
3511 struct ifinfomsg *ifm;
3512 int i, err;
3513
3514 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
3515 NL_SET_ERR_MSG(extack, "Invalid header for get link");
3516 return -EINVAL;
3517 }
3518
3519 if (!netlink_strict_get_check(skb))
Johannes Berg8cb08172019-04-26 14:07:28 +02003520 return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3521 ifla_policy, extack);
Jakub Kicinski9b3757b2019-01-18 10:46:16 -08003522
3523 ifm = nlmsg_data(nlh);
3524 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
3525 ifm->ifi_change) {
3526 NL_SET_ERR_MSG(extack, "Invalid values in header for get link request");
3527 return -EINVAL;
3528 }
3529
Johannes Berg8cb08172019-04-26 14:07:28 +02003530 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFLA_MAX,
3531 ifla_policy, extack);
Jakub Kicinski9b3757b2019-01-18 10:46:16 -08003532 if (err)
3533 return err;
3534
3535 for (i = 0; i <= IFLA_MAX; i++) {
3536 if (!tb[i])
3537 continue;
3538
3539 switch (i) {
3540 case IFLA_IFNAME:
Jiri Pirko76c9ac02019-09-30 11:48:20 +02003541 case IFLA_ALT_IFNAME:
Jakub Kicinski9b3757b2019-01-18 10:46:16 -08003542 case IFLA_EXT_MASK:
3543 case IFLA_TARGET_NETNSID:
3544 break;
3545 default:
3546 NL_SET_ERR_MSG(extack, "Unsupported attribute in get link request");
3547 return -EINVAL;
3548 }
3549 }
3550
3551 return 0;
3552}
3553
David Ahernc21ef3e2017-04-16 09:48:24 -07003554static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3555 struct netlink_ext_ack *extack)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003556{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003557 struct net *net = sock_net(skb->sk);
Jiri Benc79e1ad12017-11-02 17:04:38 -02003558 struct net *tgt_net = net;
Thomas Grafb60c5112006-08-04 23:05:34 -07003559 struct ifinfomsg *ifm;
3560 struct nlattr *tb[IFLA_MAX+1];
3561 struct net_device *dev = NULL;
3562 struct sk_buff *nskb;
Jiri Benc79e1ad12017-11-02 17:04:38 -02003563 int netnsid = -1;
Thomas Graf339bf982006-11-10 14:10:15 -08003564 int err;
Greg Rose115c9b82012-02-21 16:54:48 -05003565 u32 ext_filter_mask = 0;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003566
Jakub Kicinski9b3757b2019-01-18 10:46:16 -08003567 err = rtnl_valid_getlink_req(skb, nlh, tb, extack);
Thomas Grafb60c5112006-08-04 23:05:34 -07003568 if (err < 0)
Eric Sesterhenn9918f232006-09-26 23:26:38 -07003569 return err;
Thomas Grafb60c5112006-08-04 23:05:34 -07003570
Christian Brauner4ff66ca2018-02-07 13:53:20 +01003571 err = rtnl_ensure_unique_netns(tb, extack, true);
3572 if (err < 0)
3573 return err;
3574
Christian Brauner7e4a8d52018-09-04 21:53:53 +02003575 if (tb[IFLA_TARGET_NETNSID]) {
3576 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
Christian Braunerc383edc2018-09-04 21:53:47 +02003577 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
Jiri Benc79e1ad12017-11-02 17:04:38 -02003578 if (IS_ERR(tgt_net))
3579 return PTR_ERR(tgt_net);
3580 }
3581
Greg Rose115c9b82012-02-21 16:54:48 -05003582 if (tb[IFLA_EXT_MASK])
3583 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3584
Jiri Benc79e1ad12017-11-02 17:04:38 -02003585 err = -EINVAL;
Thomas Grafb60c5112006-08-04 23:05:34 -07003586 ifm = nlmsg_data(nlh);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003587 if (ifm->ifi_index > 0)
Jiri Benc79e1ad12017-11-02 17:04:38 -02003588 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
Jiri Pirko76c9ac02019-09-30 11:48:20 +02003589 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3590 dev = rtnl_dev_get(tgt_net, tb[IFLA_IFNAME],
3591 tb[IFLA_ALT_IFNAME], NULL);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003592 else
Jiri Benc79e1ad12017-11-02 17:04:38 -02003593 goto out;
Thomas Grafb60c5112006-08-04 23:05:34 -07003594
Jiri Benc79e1ad12017-11-02 17:04:38 -02003595 err = -ENODEV;
Eric Dumazeta3d12892009-10-21 10:59:31 +00003596 if (dev == NULL)
Jiri Benc79e1ad12017-11-02 17:04:38 -02003597 goto out;
Eric Dumazeta3d12892009-10-21 10:59:31 +00003598
Jiri Benc79e1ad12017-11-02 17:04:38 -02003599 err = -ENOBUFS;
Greg Rose115c9b82012-02-21 16:54:48 -05003600 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003601 if (nskb == NULL)
Jiri Benc79e1ad12017-11-02 17:04:38 -02003602 goto out;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003603
Jiri Benc79e1ad12017-11-02 17:04:38 -02003604 err = rtnl_fill_ifinfo(nskb, dev, net,
3605 RTM_NEWLINK, NETLINK_CB(skb).portid,
3606 nlh->nlmsg_seq, 0, 0, ext_filter_mask,
Guillaume Naultd4e4fdf2019-10-23 18:39:04 +02003607 0, NULL, 0, netnsid, GFP_KERNEL);
Patrick McHardy26932562007-01-31 23:16:40 -08003608 if (err < 0) {
3609 /* -EMSGSIZE implies BUG in if_nlmsg_size */
3610 WARN_ON(err == -EMSGSIZE);
3611 kfree_skb(nskb);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003612 } else
Eric W. Biederman15e47302012-09-07 20:12:54 +00003613 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
Jiri Benc79e1ad12017-11-02 17:04:38 -02003614out:
3615 if (netnsid >= 0)
3616 put_net(tgt_net);
Thomas Grafb60c5112006-08-04 23:05:34 -07003617
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003618 return err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003619}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003620
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02003621static int rtnl_alt_ifname(int cmd, struct net_device *dev, struct nlattr *attr,
3622 bool *changed, struct netlink_ext_ack *extack)
3623{
3624 char *alt_ifname;
3625 int err;
3626
3627 err = nla_validate(attr, attr->nla_len, IFLA_MAX, ifla_policy, extack);
3628 if (err)
3629 return err;
3630
Eric Dumazet44bfa9c2020-02-12 20:58:26 -08003631 alt_ifname = nla_strdup(attr, GFP_KERNEL);
3632 if (!alt_ifname)
3633 return -ENOMEM;
3634
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02003635 if (cmd == RTM_NEWLINKPROP) {
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02003636 err = netdev_name_node_alt_create(dev, alt_ifname);
Eric Dumazet44bfa9c2020-02-12 20:58:26 -08003637 if (!err)
3638 alt_ifname = NULL;
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02003639 } else if (cmd == RTM_DELLINKPROP) {
3640 err = netdev_name_node_alt_destroy(dev, alt_ifname);
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02003641 } else {
Eric Dumazet44bfa9c2020-02-12 20:58:26 -08003642 WARN_ON_ONCE(1);
3643 err = -EINVAL;
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02003644 }
3645
Eric Dumazet44bfa9c2020-02-12 20:58:26 -08003646 kfree(alt_ifname);
3647 if (!err)
3648 *changed = true;
3649 return err;
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02003650}
3651
3652static int rtnl_linkprop(int cmd, struct sk_buff *skb, struct nlmsghdr *nlh,
3653 struct netlink_ext_ack *extack)
3654{
3655 struct net *net = sock_net(skb->sk);
3656 struct nlattr *tb[IFLA_MAX + 1];
3657 struct net_device *dev;
3658 struct ifinfomsg *ifm;
3659 bool changed = false;
3660 struct nlattr *attr;
3661 int err, rem;
3662
3663 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
3664 if (err)
3665 return err;
3666
3667 err = rtnl_ensure_unique_netns(tb, extack, true);
3668 if (err)
3669 return err;
3670
3671 ifm = nlmsg_data(nlh);
Jiri Pirkocc6090e2019-09-30 11:48:19 +02003672 if (ifm->ifi_index > 0)
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02003673 dev = __dev_get_by_index(net, ifm->ifi_index);
Jiri Pirko76c9ac02019-09-30 11:48:20 +02003674 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3675 dev = rtnl_dev_get(net, tb[IFLA_IFNAME],
3676 tb[IFLA_ALT_IFNAME], NULL);
Jiri Pirkocc6090e2019-09-30 11:48:19 +02003677 else
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02003678 return -EINVAL;
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02003679
3680 if (!dev)
3681 return -ENODEV;
3682
3683 if (!tb[IFLA_PROP_LIST])
3684 return 0;
3685
3686 nla_for_each_nested(attr, tb[IFLA_PROP_LIST], rem) {
3687 switch (nla_type(attr)) {
3688 case IFLA_ALT_IFNAME:
3689 err = rtnl_alt_ifname(cmd, dev, attr, &changed, extack);
3690 if (err)
3691 return err;
3692 break;
3693 }
3694 }
3695
3696 if (changed)
3697 netdev_state_change(dev);
3698 return 0;
3699}
3700
3701static int rtnl_newlinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
3702 struct netlink_ext_ack *extack)
3703{
3704 return rtnl_linkprop(RTM_NEWLINKPROP, skb, nlh, extack);
3705}
3706
3707static int rtnl_dellinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
3708 struct netlink_ext_ack *extack)
3709{
3710 return rtnl_linkprop(RTM_DELLINKPROP, skb, nlh, extack);
3711}
3712
Greg Rose115c9b82012-02-21 16:54:48 -05003713static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
Greg Rosec7ac8672011-06-10 01:27:09 +00003714{
Greg Rose115c9b82012-02-21 16:54:48 -05003715 struct net *net = sock_net(skb->sk);
3716 struct net_device *dev;
3717 struct nlattr *tb[IFLA_MAX+1];
3718 u32 ext_filter_mask = 0;
3719 u16 min_ifinfo_dump_size = 0;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02003720 int hdrlen;
Greg Rose115c9b82012-02-21 16:54:48 -05003721
Michal Schmidte5eca6d2014-05-28 14:15:19 +02003722 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3723 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
3724 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
3725
Johannes Berg8cb08172019-04-26 14:07:28 +02003726 if (nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00003727 if (tb[IFLA_EXT_MASK])
3728 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3729 }
Greg Rose115c9b82012-02-21 16:54:48 -05003730
3731 if (!ext_filter_mask)
3732 return NLMSG_GOODSIZE;
3733 /*
3734 * traverse the list of net devices and compute the minimum
3735 * buffer size based upon the filter mask.
3736 */
Florian Westphal6853dd42017-08-09 20:41:51 +02003737 rcu_read_lock();
3738 for_each_netdev_rcu(net, dev) {
Greg Rose115c9b82012-02-21 16:54:48 -05003739 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
3740 if_nlmsg_size(dev,
3741 ext_filter_mask));
3742 }
Florian Westphal6853dd42017-08-09 20:41:51 +02003743 rcu_read_unlock();
Greg Rose115c9b82012-02-21 16:54:48 -05003744
Zhang Shengju93af2052016-11-22 14:14:28 +08003745 return nlmsg_total_size(min_ifinfo_dump_size);
Greg Rosec7ac8672011-06-10 01:27:09 +00003746}
3747
Adrian Bunk42bad1d2007-04-26 00:57:41 -07003748static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749{
3750 int idx;
3751 int s_idx = cb->family;
Christian Brauner87ccbb12018-09-04 21:53:51 +02003752 int type = cb->nlh->nlmsg_type - RTM_BASE;
David Ahernc63586d2018-10-24 12:59:02 -07003753 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754
3755 if (s_idx == 0)
3756 s_idx = 1;
Florian Westphal6853dd42017-08-09 20:41:51 +02003757
Patrick McHardy25239ce2010-04-26 16:02:05 +02003758 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
Florian Westphaladdf9b92017-12-02 21:44:05 +01003759 struct rtnl_link **tab;
Florian Westphaladdf9b92017-12-02 21:44:05 +01003760 struct rtnl_link *link;
Florian Westphal6853dd42017-08-09 20:41:51 +02003761 rtnl_dumpit_func dumpit;
3762
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 if (idx < s_idx || idx == PF_PACKET)
3764 continue;
Florian Westphal6853dd42017-08-09 20:41:51 +02003765
Florian Westphaladdf9b92017-12-02 21:44:05 +01003766 if (type < 0 || type >= RTM_NR_MSGTYPES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 continue;
Florian Westphal6853dd42017-08-09 20:41:51 +02003768
Florian Westphaladdf9b92017-12-02 21:44:05 +01003769 tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]);
3770 if (!tab)
3771 continue;
3772
3773 link = tab[type];
3774 if (!link)
3775 continue;
3776
3777 dumpit = link->dumpit;
Florian Westphal6853dd42017-08-09 20:41:51 +02003778 if (!dumpit)
3779 continue;
3780
Nicolas Dichtel04652772013-03-22 06:28:42 +00003781 if (idx > s_idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 memset(&cb->args[0], 0, sizeof(cb->args));
Nicolas Dichtel04652772013-03-22 06:28:42 +00003783 cb->prev_seq = 0;
3784 cb->seq = 0;
3785 }
David Ahernc63586d2018-10-24 12:59:02 -07003786 ret = dumpit(skb, cb);
Alexey Kodanev5e1acb42018-11-02 19:11:04 +03003787 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 break;
3789 }
3790 cb->family = idx;
3791
David Ahernc63586d2018-10-24 12:59:02 -07003792 return skb->len ? : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793}
3794
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003795struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003796 unsigned int change,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003797 u32 event, gfp_t flags, int *new_nsid,
3798 int new_ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003800 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 struct sk_buff *skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003802 int err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00003803 size_t if_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804
Alexei Starovoitov7f294052013-10-23 16:02:42 -07003805 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003806 if (skb == NULL)
3807 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808
Jiri Benc79e1ad12017-11-02 17:04:38 -02003809 err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
3810 type, 0, 0, change, 0, 0, event,
Guillaume Naultd4e4fdf2019-10-23 18:39:04 +02003811 new_nsid, new_ifindex, -1, flags);
Patrick McHardy26932562007-01-31 23:16:40 -08003812 if (err < 0) {
3813 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
3814 WARN_ON(err == -EMSGSIZE);
3815 kfree_skb(skb);
3816 goto errout;
3817 }
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003818 return skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003819errout:
3820 if (err < 0)
Eric W. Biederman4b3da702007-11-19 22:27:40 -08003821 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003822 return NULL;
3823}
3824
3825void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
3826{
3827 struct net *net = dev_net(dev);
3828
3829 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
3830}
3831
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003832static void rtmsg_ifinfo_event(int type, struct net_device *dev,
3833 unsigned int change, u32 event,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003834 gfp_t flags, int *new_nsid, int new_ifindex)
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003835{
3836 struct sk_buff *skb;
3837
Nicolas Dichteled2a80a2015-05-13 14:19:42 +02003838 if (dev->reg_state != NETREG_REGISTERED)
3839 return;
3840
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003841 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid,
3842 new_ifindex);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003843 if (skb)
3844 rtmsg_ifinfo_send(skb, dev, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845}
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003846
3847void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
3848 gfp_t flags)
3849{
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003850 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3851 NULL, 0);
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003852}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003854void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003855 gfp_t flags, int *new_nsid, int new_ifindex)
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003856{
3857 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003858 new_nsid, new_ifindex);
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003859}
3860
John Fastabendd83b0602012-04-15 06:44:08 +00003861static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
3862 struct net_device *dev,
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003863 u8 *addr, u16 vid, u32 pid, u32 seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003864 int type, unsigned int flags,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003865 int nlflags, u16 ndm_state)
John Fastabendd83b0602012-04-15 06:44:08 +00003866{
3867 struct nlmsghdr *nlh;
3868 struct ndmsg *ndm;
3869
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003870 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
John Fastabendd83b0602012-04-15 06:44:08 +00003871 if (!nlh)
3872 return -EMSGSIZE;
3873
3874 ndm = nlmsg_data(nlh);
3875 ndm->ndm_family = AF_BRIDGE;
3876 ndm->ndm_pad1 = 0;
3877 ndm->ndm_pad2 = 0;
3878 ndm->ndm_flags = flags;
3879 ndm->ndm_type = 0;
3880 ndm->ndm_ifindex = dev->ifindex;
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003881 ndm->ndm_state = ndm_state;
John Fastabendd83b0602012-04-15 06:44:08 +00003882
3883 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
3884 goto nla_put_failure;
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003885 if (vid)
3886 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
3887 goto nla_put_failure;
John Fastabendd83b0602012-04-15 06:44:08 +00003888
Johannes Berg053c0952015-01-16 22:09:00 +01003889 nlmsg_end(skb, nlh);
3890 return 0;
John Fastabendd83b0602012-04-15 06:44:08 +00003891
3892nla_put_failure:
3893 nlmsg_cancel(skb, nlh);
3894 return -EMSGSIZE;
3895}
3896
John Fastabend3ff661c2012-04-15 06:44:14 +00003897static inline size_t rtnl_fdb_nlmsg_size(void)
3898{
Sabrina Dubrocaf82ef3e2016-11-18 15:50:39 +01003899 return NLMSG_ALIGN(sizeof(struct ndmsg)) +
3900 nla_total_size(ETH_ALEN) + /* NDA_LLADDR */
3901 nla_total_size(sizeof(u16)) + /* NDA_VLAN */
3902 0;
John Fastabend3ff661c2012-04-15 06:44:14 +00003903}
3904
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003905static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
3906 u16 ndm_state)
John Fastabend3ff661c2012-04-15 06:44:14 +00003907{
3908 struct net *net = dev_net(dev);
3909 struct sk_buff *skb;
3910 int err = -ENOBUFS;
3911
3912 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
3913 if (!skb)
3914 goto errout;
3915
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003916 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003917 0, 0, type, NTF_SELF, 0, ndm_state);
John Fastabend3ff661c2012-04-15 06:44:14 +00003918 if (err < 0) {
3919 kfree_skb(skb);
3920 goto errout;
3921 }
3922
3923 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
3924 return;
3925errout:
3926 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
3927}
3928
Bart Van Asschea9869672019-03-25 09:17:22 -07003929/*
Vlad Yasevich090096b2013-03-06 15:39:42 +00003930 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3931 */
3932int ndo_dflt_fdb_add(struct ndmsg *ndm,
3933 struct nlattr *tb[],
3934 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003935 const unsigned char *addr, u16 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003936 u16 flags)
3937{
3938 int err = -EINVAL;
3939
3940 /* If aging addresses are supported device will need to
3941 * implement its own handler for this.
3942 */
3943 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
3944 pr_info("%s: FDB only supports static addresses\n", dev->name);
3945 return err;
3946 }
3947
Or Gerlitz65891fe2014-12-14 18:19:05 +02003948 if (vid) {
3949 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
3950 return err;
3951 }
3952
Vlad Yasevich090096b2013-03-06 15:39:42 +00003953 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3954 err = dev_uc_add_excl(dev, addr);
3955 else if (is_multicast_ether_addr(addr))
3956 err = dev_mc_add_excl(dev, addr);
3957
3958 /* Only return duplicate errors if NLM_F_EXCL is set */
3959 if (err == -EEXIST && !(flags & NLM_F_EXCL))
3960 err = 0;
3961
3962 return err;
3963}
3964EXPORT_SYMBOL(ndo_dflt_fdb_add);
3965
Florian Westphalb88d12e2017-10-10 17:10:04 +02003966static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
3967 struct netlink_ext_ack *extack)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003968{
3969 u16 vid = 0;
3970
3971 if (vlan_attr) {
3972 if (nla_len(vlan_attr) != sizeof(u16)) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003973 NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
Jiri Pirkof6f64242014-11-28 14:34:15 +01003974 return -EINVAL;
3975 }
3976
3977 vid = nla_get_u16(vlan_attr);
3978
3979 if (!vid || vid >= VLAN_VID_MASK) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003980 NL_SET_ERR_MSG(extack, "invalid vlan id");
Jiri Pirkof6f64242014-11-28 14:34:15 +01003981 return -EINVAL;
3982 }
3983 }
3984 *p_vid = vid;
3985 return 0;
3986}
3987
David Ahernc21ef3e2017-04-16 09:48:24 -07003988static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
3989 struct netlink_ext_ack *extack)
John Fastabend77162022012-04-15 06:43:56 +00003990{
3991 struct net *net = sock_net(skb->sk);
John Fastabend77162022012-04-15 06:43:56 +00003992 struct ndmsg *ndm;
3993 struct nlattr *tb[NDA_MAX+1];
3994 struct net_device *dev;
3995 u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01003996 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00003997 int err;
3998
Johannes Berg8cb08172019-04-26 14:07:28 +02003999 err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
4000 extack);
John Fastabend77162022012-04-15 06:43:56 +00004001 if (err < 0)
4002 return err;
4003
4004 ndm = nlmsg_data(nlh);
4005 if (ndm->ndm_ifindex == 0) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004006 NL_SET_ERR_MSG(extack, "invalid ifindex");
John Fastabend77162022012-04-15 06:43:56 +00004007 return -EINVAL;
4008 }
4009
4010 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
4011 if (dev == NULL) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004012 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabend77162022012-04-15 06:43:56 +00004013 return -ENODEV;
4014 }
4015
4016 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004017 NL_SET_ERR_MSG(extack, "invalid address");
John Fastabend77162022012-04-15 06:43:56 +00004018 return -EINVAL;
4019 }
4020
Ido Schimmelda715772018-10-29 20:36:43 +00004021 if (dev->type != ARPHRD_ETHER) {
4022 NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices");
4023 return -EINVAL;
4024 }
4025
John Fastabend77162022012-04-15 06:43:56 +00004026 addr = nla_data(tb[NDA_LLADDR]);
John Fastabend77162022012-04-15 06:43:56 +00004027
Florian Westphalb88d12e2017-10-10 17:10:04 +02004028 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
Jiri Pirkof6f64242014-11-28 14:34:15 +01004029 if (err)
4030 return err;
4031
John Fastabend77162022012-04-15 06:43:56 +00004032 err = -EOPNOTSUPP;
4033
4034 /* Support fdb on master device the net/bridge default case */
4035 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
Julian Wiedmann2e92a2d2020-02-20 09:00:07 +01004036 netif_is_bridge_port(dev)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00004037 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4038 const struct net_device_ops *ops = br_dev->netdev_ops;
4039
Jiri Pirkof6f64242014-11-28 14:34:15 +01004040 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
Petr Machata87b09842019-01-16 23:06:50 +00004041 nlh->nlmsg_flags, extack);
John Fastabend77162022012-04-15 06:43:56 +00004042 if (err)
4043 goto out;
4044 else
4045 ndm->ndm_flags &= ~NTF_MASTER;
4046 }
4047
4048 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00004049 if ((ndm->ndm_flags & NTF_SELF)) {
4050 if (dev->netdev_ops->ndo_fdb_add)
4051 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
Jiri Pirkof6f64242014-11-28 14:34:15 +01004052 vid,
Petr Machata87b09842019-01-16 23:06:50 +00004053 nlh->nlmsg_flags,
4054 extack);
Vlad Yasevich090096b2013-03-06 15:39:42 +00004055 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01004056 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00004057 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00004058
John Fastabend3ff661c2012-04-15 06:44:14 +00004059 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00004060 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
4061 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00004062 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00004063 }
John Fastabend77162022012-04-15 06:43:56 +00004064 }
4065out:
4066 return err;
4067}
4068
Bart Van Asschea9869672019-03-25 09:17:22 -07004069/*
Vlad Yasevich090096b2013-03-06 15:39:42 +00004070 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
4071 */
4072int ndo_dflt_fdb_del(struct ndmsg *ndm,
4073 struct nlattr *tb[],
4074 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01004075 const unsigned char *addr, u16 vid)
Vlad Yasevich090096b2013-03-06 15:39:42 +00004076{
Alexander Duyckc8a89c42014-07-15 15:15:20 -07004077 int err = -EINVAL;
Vlad Yasevich090096b2013-03-06 15:39:42 +00004078
4079 /* If aging addresses are supported device will need to
4080 * implement its own handler for this.
4081 */
Sridhar Samudrala64535992013-08-08 15:19:48 -07004082 if (!(ndm->ndm_state & NUD_PERMANENT)) {
Vlad Yasevich090096b2013-03-06 15:39:42 +00004083 pr_info("%s: FDB only supports static addresses\n", dev->name);
Alexander Duyckc8a89c42014-07-15 15:15:20 -07004084 return err;
Vlad Yasevich090096b2013-03-06 15:39:42 +00004085 }
4086
4087 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
4088 err = dev_uc_del(dev, addr);
4089 else if (is_multicast_ether_addr(addr))
4090 err = dev_mc_del(dev, addr);
Vlad Yasevich090096b2013-03-06 15:39:42 +00004091
4092 return err;
4093}
4094EXPORT_SYMBOL(ndo_dflt_fdb_del);
4095
David Ahernc21ef3e2017-04-16 09:48:24 -07004096static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
4097 struct netlink_ext_ack *extack)
John Fastabend77162022012-04-15 06:43:56 +00004098{
4099 struct net *net = sock_net(skb->sk);
4100 struct ndmsg *ndm;
Vlad Yasevich1690be62013-02-13 12:00:18 +00004101 struct nlattr *tb[NDA_MAX+1];
John Fastabend77162022012-04-15 06:43:56 +00004102 struct net_device *dev;
John Fastabend77162022012-04-15 06:43:56 +00004103 __u8 *addr;
Colin Ian King7d311802020-04-25 12:28:14 +01004104 int err;
Jiri Pirkof6f64242014-11-28 14:34:15 +01004105 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00004106
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07004107 if (!netlink_capable(skb, CAP_NET_ADMIN))
Vlad Yasevich1690be62013-02-13 12:00:18 +00004108 return -EPERM;
4109
Johannes Berg8cb08172019-04-26 14:07:28 +02004110 err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
4111 extack);
Vlad Yasevich1690be62013-02-13 12:00:18 +00004112 if (err < 0)
4113 return err;
John Fastabend77162022012-04-15 06:43:56 +00004114
4115 ndm = nlmsg_data(nlh);
4116 if (ndm->ndm_ifindex == 0) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004117 NL_SET_ERR_MSG(extack, "invalid ifindex");
John Fastabend77162022012-04-15 06:43:56 +00004118 return -EINVAL;
4119 }
4120
4121 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
4122 if (dev == NULL) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004123 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabend77162022012-04-15 06:43:56 +00004124 return -ENODEV;
4125 }
4126
Vlad Yasevich1690be62013-02-13 12:00:18 +00004127 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004128 NL_SET_ERR_MSG(extack, "invalid address");
John Fastabend77162022012-04-15 06:43:56 +00004129 return -EINVAL;
4130 }
4131
Ido Schimmelda715772018-10-29 20:36:43 +00004132 if (dev->type != ARPHRD_ETHER) {
4133 NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices");
4134 return -EINVAL;
4135 }
4136
Vlad Yasevich1690be62013-02-13 12:00:18 +00004137 addr = nla_data(tb[NDA_LLADDR]);
Vlad Yasevich1690be62013-02-13 12:00:18 +00004138
Florian Westphalb88d12e2017-10-10 17:10:04 +02004139 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
Jiri Pirkof6f64242014-11-28 14:34:15 +01004140 if (err)
4141 return err;
4142
John Fastabend77162022012-04-15 06:43:56 +00004143 err = -EOPNOTSUPP;
4144
4145 /* Support fdb on master device the net/bridge default case */
4146 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
Julian Wiedmann2e92a2d2020-02-20 09:00:07 +01004147 netif_is_bridge_port(dev)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00004148 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4149 const struct net_device_ops *ops = br_dev->netdev_ops;
John Fastabend77162022012-04-15 06:43:56 +00004150
Jiri Pirko898e5062013-01-03 22:48:52 +00004151 if (ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01004152 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00004153
4154 if (err)
4155 goto out;
4156 else
4157 ndm->ndm_flags &= ~NTF_MASTER;
4158 }
4159
4160 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00004161 if (ndm->ndm_flags & NTF_SELF) {
4162 if (dev->netdev_ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01004163 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
4164 vid);
Vlad Yasevich090096b2013-03-06 15:39:42 +00004165 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01004166 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00004167
John Fastabend3ff661c2012-04-15 06:44:14 +00004168 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00004169 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
4170 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00004171 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00004172 }
John Fastabend77162022012-04-15 06:43:56 +00004173 }
4174out:
4175 return err;
4176}
4177
John Fastabendd83b0602012-04-15 06:44:08 +00004178static int nlmsg_populate_fdb(struct sk_buff *skb,
4179 struct netlink_callback *cb,
4180 struct net_device *dev,
4181 int *idx,
4182 struct netdev_hw_addr_list *list)
4183{
4184 struct netdev_hw_addr *ha;
4185 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00004186 u32 portid, seq;
John Fastabendd83b0602012-04-15 06:44:08 +00004187
Eric W. Biederman15e47302012-09-07 20:12:54 +00004188 portid = NETLINK_CB(cb->skb).portid;
John Fastabendd83b0602012-04-15 06:44:08 +00004189 seq = cb->nlh->nlmsg_seq;
4190
4191 list_for_each_entry(ha, &list->list, list) {
Roopa Prabhud2976532016-08-30 21:56:45 -07004192 if (*idx < cb->args[2])
John Fastabendd83b0602012-04-15 06:44:08 +00004193 goto skip;
4194
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00004195 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
John Fastabenda7a558f2012-11-01 16:23:10 +00004196 portid, seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01004197 RTM_NEWNEIGH, NTF_SELF,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00004198 NLM_F_MULTI, NUD_PERMANENT);
John Fastabendd83b0602012-04-15 06:44:08 +00004199 if (err < 0)
4200 return err;
4201skip:
4202 *idx += 1;
4203 }
4204 return 0;
4205}
4206
4207/**
Ben Hutchings2c530402012-07-10 10:55:09 +00004208 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
Bart Van Asschea9869672019-03-25 09:17:22 -07004209 * @skb: socket buffer to store message in
4210 * @cb: netlink callback
John Fastabendd83b0602012-04-15 06:44:08 +00004211 * @dev: netdevice
Bart Van Asschea9869672019-03-25 09:17:22 -07004212 * @filter_dev: ignored
4213 * @idx: the number of FDB table entries dumped is added to *@idx
John Fastabendd83b0602012-04-15 06:44:08 +00004214 *
4215 * Default netdevice operation to dump the existing unicast address list.
John Fastabend91f3e7b2013-03-29 08:18:37 +00004216 * Returns number of addresses from list put in skb.
John Fastabendd83b0602012-04-15 06:44:08 +00004217 */
4218int ndo_dflt_fdb_dump(struct sk_buff *skb,
4219 struct netlink_callback *cb,
4220 struct net_device *dev,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04004221 struct net_device *filter_dev,
Roopa Prabhud2976532016-08-30 21:56:45 -07004222 int *idx)
John Fastabendd83b0602012-04-15 06:44:08 +00004223{
4224 int err;
4225
Eric Dumazet68883892018-12-04 09:40:35 -08004226 if (dev->type != ARPHRD_ETHER)
4227 return -EINVAL;
4228
John Fastabendd83b0602012-04-15 06:44:08 +00004229 netif_addr_lock_bh(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -07004230 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
John Fastabendd83b0602012-04-15 06:44:08 +00004231 if (err)
4232 goto out;
Zhang Shengju2934c9d2016-11-30 16:37:34 +08004233 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
John Fastabendd83b0602012-04-15 06:44:08 +00004234out:
4235 netif_addr_unlock_bh(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -07004236 return err;
John Fastabendd83b0602012-04-15 06:44:08 +00004237}
4238EXPORT_SYMBOL(ndo_dflt_fdb_dump);
4239
David Ahern8c6e1372018-10-07 20:16:44 -07004240static int valid_fdb_dump_strict(const struct nlmsghdr *nlh,
4241 int *br_idx, int *brport_idx,
4242 struct netlink_ext_ack *extack)
4243{
4244 struct nlattr *tb[NDA_MAX + 1];
4245 struct ndmsg *ndm;
4246 int err, i;
4247
4248 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
4249 NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request");
4250 return -EINVAL;
4251 }
4252
4253 ndm = nlmsg_data(nlh);
4254 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
4255 ndm->ndm_flags || ndm->ndm_type) {
Nikolay Aleksandrov8b730182019-10-29 13:59:32 +02004256 NL_SET_ERR_MSG(extack, "Invalid values in header for fdb dump request");
David Ahern8c6e1372018-10-07 20:16:44 -07004257 return -EINVAL;
4258 }
4259
Johannes Berg8cb08172019-04-26 14:07:28 +02004260 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
4261 NDA_MAX, NULL, extack);
David Ahern8c6e1372018-10-07 20:16:44 -07004262 if (err < 0)
4263 return err;
4264
4265 *brport_idx = ndm->ndm_ifindex;
4266 for (i = 0; i <= NDA_MAX; ++i) {
4267 if (!tb[i])
4268 continue;
4269
4270 switch (i) {
4271 case NDA_IFINDEX:
4272 if (nla_len(tb[i]) != sizeof(u32)) {
4273 NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request");
4274 return -EINVAL;
4275 }
4276 *brport_idx = nla_get_u32(tb[NDA_IFINDEX]);
4277 break;
4278 case NDA_MASTER:
4279 if (nla_len(tb[i]) != sizeof(u32)) {
4280 NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request");
4281 return -EINVAL;
4282 }
4283 *br_idx = nla_get_u32(tb[NDA_MASTER]);
4284 break;
4285 default:
4286 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request");
4287 return -EINVAL;
4288 }
4289 }
4290
4291 return 0;
4292}
4293
David Ahern8dfbda12018-10-07 20:16:43 -07004294static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh,
4295 int *br_idx, int *brport_idx,
4296 struct netlink_ext_ack *extack)
4297{
David Ahern8dfbda12018-10-07 20:16:43 -07004298 struct nlattr *tb[IFLA_MAX+1];
4299 int err;
4300
4301 /* A hack to preserve kernel<->userspace interface.
4302 * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0.
4303 * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails.
4304 * So, check for ndmsg with an optional u32 attribute (not used here).
4305 * Fortunately these sizes don't conflict with the size of ifinfomsg
4306 * with an optional attribute.
4307 */
4308 if (nlmsg_len(nlh) != sizeof(struct ndmsg) &&
4309 (nlmsg_len(nlh) != sizeof(struct ndmsg) +
4310 nla_attr_size(sizeof(u32)))) {
David Ahern4565d7e2018-10-08 13:57:24 -07004311 struct ifinfomsg *ifm;
4312
Johannes Berg8cb08172019-04-26 14:07:28 +02004313 err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
4314 tb, IFLA_MAX, ifla_policy,
4315 extack);
David Ahern8dfbda12018-10-07 20:16:43 -07004316 if (err < 0) {
4317 return -EINVAL;
4318 } else if (err == 0) {
4319 if (tb[IFLA_MASTER])
4320 *br_idx = nla_get_u32(tb[IFLA_MASTER]);
4321 }
4322
David Ahern4565d7e2018-10-08 13:57:24 -07004323 ifm = nlmsg_data(nlh);
David Ahern8dfbda12018-10-07 20:16:43 -07004324 *brport_idx = ifm->ifi_index;
4325 }
4326 return 0;
4327}
4328
John Fastabend77162022012-04-15 06:43:56 +00004329static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
4330{
John Fastabend77162022012-04-15 06:43:56 +00004331 struct net_device *dev;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004332 struct net_device *br_dev = NULL;
4333 const struct net_device_ops *ops = NULL;
4334 const struct net_device_ops *cops = NULL;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004335 struct net *net = sock_net(skb->sk);
Roopa Prabhud2976532016-08-30 21:56:45 -07004336 struct hlist_head *head;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004337 int brport_idx = 0;
4338 int br_idx = 0;
Roopa Prabhud2976532016-08-30 21:56:45 -07004339 int h, s_h;
4340 int idx = 0, s_idx;
4341 int err = 0;
4342 int fidx = 0;
John Fastabend77162022012-04-15 06:43:56 +00004343
David Ahern8c6e1372018-10-07 20:16:44 -07004344 if (cb->strict_check)
4345 err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx,
4346 cb->extack);
4347 else
4348 err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx,
4349 cb->extack);
David Ahern8dfbda12018-10-07 20:16:43 -07004350 if (err < 0)
4351 return err;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004352
4353 if (br_idx) {
4354 br_dev = __dev_get_by_index(net, br_idx);
4355 if (!br_dev)
4356 return -ENODEV;
4357
4358 ops = br_dev->netdev_ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004359 }
4360
Roopa Prabhud2976532016-08-30 21:56:45 -07004361 s_h = cb->args[0];
4362 s_idx = cb->args[1];
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004363
Roopa Prabhud2976532016-08-30 21:56:45 -07004364 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4365 idx = 0;
4366 head = &net->dev_index_head[h];
4367 hlist_for_each_entry(dev, head, index_hlist) {
4368
4369 if (brport_idx && (dev->ifindex != brport_idx))
4370 continue;
4371
4372 if (!br_idx) { /* user did not specify a specific bridge */
Julian Wiedmann2e92a2d2020-02-20 09:00:07 +01004373 if (netif_is_bridge_port(dev)) {
Roopa Prabhud2976532016-08-30 21:56:45 -07004374 br_dev = netdev_master_upper_dev_get(dev);
4375 cops = br_dev->netdev_ops;
4376 }
4377 } else {
4378 if (dev != br_dev &&
Julian Wiedmann2e92a2d2020-02-20 09:00:07 +01004379 !netif_is_bridge_port(dev))
Roopa Prabhud2976532016-08-30 21:56:45 -07004380 continue;
4381
4382 if (br_dev != netdev_master_upper_dev_get(dev) &&
4383 !(dev->priv_flags & IFF_EBRIDGE))
4384 continue;
4385 cops = ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004386 }
4387
Roopa Prabhud2976532016-08-30 21:56:45 -07004388 if (idx < s_idx)
4389 goto cont;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004390
Julian Wiedmann2e92a2d2020-02-20 09:00:07 +01004391 if (netif_is_bridge_port(dev)) {
Roopa Prabhud2976532016-08-30 21:56:45 -07004392 if (cops && cops->ndo_fdb_dump) {
4393 err = cops->ndo_fdb_dump(skb, cb,
4394 br_dev, dev,
4395 &fidx);
4396 if (err == -EMSGSIZE)
4397 goto out;
4398 }
4399 }
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004400
Roopa Prabhud2976532016-08-30 21:56:45 -07004401 if (dev->netdev_ops->ndo_fdb_dump)
4402 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
4403 dev, NULL,
4404 &fidx);
4405 else
4406 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
4407 &fidx);
4408 if (err == -EMSGSIZE)
4409 goto out;
4410
4411 cops = NULL;
4412
4413 /* reset fdb offset to 0 for rest of the interfaces */
4414 cb->args[2] = 0;
4415 fidx = 0;
4416cont:
4417 idx++;
John Fastabend77162022012-04-15 06:43:56 +00004418 }
John Fastabend77162022012-04-15 06:43:56 +00004419 }
John Fastabend77162022012-04-15 06:43:56 +00004420
Roopa Prabhud2976532016-08-30 21:56:45 -07004421out:
4422 cb->args[0] = h;
4423 cb->args[1] = idx;
4424 cb->args[2] = fidx;
4425
John Fastabend77162022012-04-15 06:43:56 +00004426 return skb->len;
4427}
4428
Roopa Prabhu5b2f94b2018-12-15 22:35:08 -08004429static int valid_fdb_get_strict(const struct nlmsghdr *nlh,
4430 struct nlattr **tb, u8 *ndm_flags,
4431 int *br_idx, int *brport_idx, u8 **addr,
4432 u16 *vid, struct netlink_ext_ack *extack)
4433{
4434 struct ndmsg *ndm;
4435 int err, i;
4436
4437 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
4438 NL_SET_ERR_MSG(extack, "Invalid header for fdb get request");
4439 return -EINVAL;
4440 }
4441
4442 ndm = nlmsg_data(nlh);
4443 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
4444 ndm->ndm_type) {
4445 NL_SET_ERR_MSG(extack, "Invalid values in header for fdb get request");
4446 return -EINVAL;
4447 }
4448
4449 if (ndm->ndm_flags & ~(NTF_MASTER | NTF_SELF)) {
4450 NL_SET_ERR_MSG(extack, "Invalid flags in header for fdb get request");
4451 return -EINVAL;
4452 }
4453
Johannes Berg8cb08172019-04-26 14:07:28 +02004454 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
4455 NDA_MAX, nda_policy, extack);
Roopa Prabhu5b2f94b2018-12-15 22:35:08 -08004456 if (err < 0)
4457 return err;
4458
4459 *ndm_flags = ndm->ndm_flags;
4460 *brport_idx = ndm->ndm_ifindex;
4461 for (i = 0; i <= NDA_MAX; ++i) {
4462 if (!tb[i])
4463 continue;
4464
4465 switch (i) {
4466 case NDA_MASTER:
4467 *br_idx = nla_get_u32(tb[i]);
4468 break;
4469 case NDA_LLADDR:
4470 if (nla_len(tb[i]) != ETH_ALEN) {
4471 NL_SET_ERR_MSG(extack, "Invalid address in fdb get request");
4472 return -EINVAL;
4473 }
4474 *addr = nla_data(tb[i]);
4475 break;
4476 case NDA_VLAN:
4477 err = fdb_vid_parse(tb[i], vid, extack);
4478 if (err)
4479 return err;
4480 break;
4481 case NDA_VNI:
4482 break;
4483 default:
4484 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb get request");
4485 return -EINVAL;
4486 }
4487 }
4488
4489 return 0;
4490}
4491
4492static int rtnl_fdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh,
4493 struct netlink_ext_ack *extack)
4494{
4495 struct net_device *dev = NULL, *br_dev = NULL;
4496 const struct net_device_ops *ops = NULL;
4497 struct net *net = sock_net(in_skb->sk);
4498 struct nlattr *tb[NDA_MAX + 1];
4499 struct sk_buff *skb;
4500 int brport_idx = 0;
4501 u8 ndm_flags = 0;
4502 int br_idx = 0;
4503 u8 *addr = NULL;
4504 u16 vid = 0;
4505 int err;
4506
4507 err = valid_fdb_get_strict(nlh, tb, &ndm_flags, &br_idx,
4508 &brport_idx, &addr, &vid, extack);
4509 if (err < 0)
4510 return err;
4511
Nikolay Aleksandrovf989d032018-12-30 14:33:20 +02004512 if (!addr) {
4513 NL_SET_ERR_MSG(extack, "Missing lookup address for fdb get request");
4514 return -EINVAL;
4515 }
4516
Roopa Prabhu5b2f94b2018-12-15 22:35:08 -08004517 if (brport_idx) {
4518 dev = __dev_get_by_index(net, brport_idx);
4519 if (!dev) {
4520 NL_SET_ERR_MSG(extack, "Unknown device ifindex");
4521 return -ENODEV;
4522 }
4523 }
4524
4525 if (br_idx) {
4526 if (dev) {
4527 NL_SET_ERR_MSG(extack, "Master and device are mutually exclusive");
4528 return -EINVAL;
4529 }
4530
4531 br_dev = __dev_get_by_index(net, br_idx);
4532 if (!br_dev) {
4533 NL_SET_ERR_MSG(extack, "Invalid master ifindex");
4534 return -EINVAL;
4535 }
4536 ops = br_dev->netdev_ops;
4537 }
4538
4539 if (dev) {
4540 if (!ndm_flags || (ndm_flags & NTF_MASTER)) {
Julian Wiedmann2e92a2d2020-02-20 09:00:07 +01004541 if (!netif_is_bridge_port(dev)) {
Roopa Prabhu5b2f94b2018-12-15 22:35:08 -08004542 NL_SET_ERR_MSG(extack, "Device is not a bridge port");
4543 return -EINVAL;
4544 }
4545 br_dev = netdev_master_upper_dev_get(dev);
4546 if (!br_dev) {
4547 NL_SET_ERR_MSG(extack, "Master of device not found");
4548 return -EINVAL;
4549 }
4550 ops = br_dev->netdev_ops;
4551 } else {
4552 if (!(ndm_flags & NTF_SELF)) {
4553 NL_SET_ERR_MSG(extack, "Missing NTF_SELF");
4554 return -EINVAL;
4555 }
4556 ops = dev->netdev_ops;
4557 }
4558 }
4559
4560 if (!br_dev && !dev) {
4561 NL_SET_ERR_MSG(extack, "No device specified");
4562 return -ENODEV;
4563 }
4564
4565 if (!ops || !ops->ndo_fdb_get) {
4566 NL_SET_ERR_MSG(extack, "Fdb get operation not supported by device");
4567 return -EOPNOTSUPP;
4568 }
4569
4570 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
4571 if (!skb)
4572 return -ENOBUFS;
4573
4574 if (br_dev)
4575 dev = br_dev;
4576 err = ops->ndo_fdb_get(skb, tb, dev, addr, vid,
4577 NETLINK_CB(in_skb).portid,
4578 nlh->nlmsg_seq, extack);
4579 if (err)
4580 goto out;
4581
4582 return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
4583out:
4584 kfree_skb(skb);
4585 return err;
4586}
4587
Scott Feldman2c3c0312014-11-28 14:34:25 +01004588static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
4589 unsigned int attrnum, unsigned int flag)
4590{
4591 if (mask & flag)
4592 return nla_put_u8(skb, attrnum, !!(flags & flag));
4593 return 0;
4594}
4595
John Fastabend815cccb2012-10-24 08:13:09 +00004596int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Scott Feldman2c3c0312014-11-28 14:34:25 +01004597 struct net_device *dev, u16 mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -07004598 u32 flags, u32 mask, int nlflags,
4599 u32 filter_mask,
4600 int (*vlan_fill)(struct sk_buff *skb,
4601 struct net_device *dev,
4602 u32 filter_mask))
John Fastabend815cccb2012-10-24 08:13:09 +00004603{
4604 struct nlmsghdr *nlh;
4605 struct ifinfomsg *ifm;
4606 struct nlattr *br_afspec;
Scott Feldman2c3c0312014-11-28 14:34:25 +01004607 struct nlattr *protinfo;
John Fastabend815cccb2012-10-24 08:13:09 +00004608 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Jiri Pirko898e5062013-01-03 22:48:52 +00004609 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07004610 int err = 0;
John Fastabend815cccb2012-10-24 08:13:09 +00004611
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02004612 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
John Fastabend815cccb2012-10-24 08:13:09 +00004613 if (nlh == NULL)
4614 return -EMSGSIZE;
4615
4616 ifm = nlmsg_data(nlh);
4617 ifm->ifi_family = AF_BRIDGE;
4618 ifm->__ifi_pad = 0;
4619 ifm->ifi_type = dev->type;
4620 ifm->ifi_index = dev->ifindex;
4621 ifm->ifi_flags = dev_get_flags(dev);
4622 ifm->ifi_change = 0;
4623
4624
4625 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4626 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4627 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00004628 (br_dev &&
4629 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
John Fastabend815cccb2012-10-24 08:13:09 +00004630 (dev->addr_len &&
4631 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02004632 (dev->ifindex != dev_get_iflink(dev) &&
4633 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
John Fastabend815cccb2012-10-24 08:13:09 +00004634 goto nla_put_failure;
4635
Michal Kubecekae0be8d2019-04-26 11:13:06 +02004636 br_afspec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
John Fastabend815cccb2012-10-24 08:13:09 +00004637 if (!br_afspec)
4638 goto nla_put_failure;
4639
Roopa Prabhu1d460b92014-12-08 14:04:20 -08004640 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
John Fastabend815cccb2012-10-24 08:13:09 +00004641 nla_nest_cancel(skb, br_afspec);
4642 goto nla_put_failure;
4643 }
Roopa Prabhu1d460b92014-12-08 14:04:20 -08004644
4645 if (mode != BRIDGE_MODE_UNDEF) {
4646 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
4647 nla_nest_cancel(skb, br_afspec);
4648 goto nla_put_failure;
4649 }
4650 }
Scott Feldman7d4f8d82015-06-22 00:27:17 -07004651 if (vlan_fill) {
4652 err = vlan_fill(skb, dev, filter_mask);
4653 if (err) {
4654 nla_nest_cancel(skb, br_afspec);
4655 goto nla_put_failure;
4656 }
4657 }
John Fastabend815cccb2012-10-24 08:13:09 +00004658 nla_nest_end(skb, br_afspec);
4659
Michal Kubecekae0be8d2019-04-26 11:13:06 +02004660 protinfo = nla_nest_start(skb, IFLA_PROTINFO);
Scott Feldman2c3c0312014-11-28 14:34:25 +01004661 if (!protinfo)
4662 goto nla_put_failure;
4663
4664 if (brport_nla_put_flag(skb, flags, mask,
4665 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
4666 brport_nla_put_flag(skb, flags, mask,
4667 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
4668 brport_nla_put_flag(skb, flags, mask,
4669 IFLA_BRPORT_FAST_LEAVE,
4670 BR_MULTICAST_FAST_LEAVE) ||
4671 brport_nla_put_flag(skb, flags, mask,
4672 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
4673 brport_nla_put_flag(skb, flags, mask,
4674 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
4675 brport_nla_put_flag(skb, flags, mask,
4676 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
4677 brport_nla_put_flag(skb, flags, mask,
4678 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
4679 brport_nla_put_flag(skb, flags, mask,
Julian Wiedmann583cb0b2020-02-17 14:45:01 +01004680 IFLA_BRPORT_PROXYARP, BR_PROXYARP) ||
4681 brport_nla_put_flag(skb, flags, mask,
4682 IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD) ||
4683 brport_nla_put_flag(skb, flags, mask,
4684 IFLA_BRPORT_BCAST_FLOOD, BR_BCAST_FLOOD)) {
Scott Feldman2c3c0312014-11-28 14:34:25 +01004685 nla_nest_cancel(skb, protinfo);
4686 goto nla_put_failure;
4687 }
4688
4689 nla_nest_end(skb, protinfo);
4690
Johannes Berg053c0952015-01-16 22:09:00 +01004691 nlmsg_end(skb, nlh);
4692 return 0;
John Fastabend815cccb2012-10-24 08:13:09 +00004693nla_put_failure:
4694 nlmsg_cancel(skb, nlh);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07004695 return err ? err : -EMSGSIZE;
John Fastabend815cccb2012-10-24 08:13:09 +00004696}
Scott Feldman7d4f8d82015-06-22 00:27:17 -07004697EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
John Fastabend815cccb2012-10-24 08:13:09 +00004698
David Ahern2d011be2018-10-07 20:16:31 -07004699static int valid_bridge_getlink_req(const struct nlmsghdr *nlh,
4700 bool strict_check, u32 *filter_mask,
4701 struct netlink_ext_ack *extack)
4702{
4703 struct nlattr *tb[IFLA_MAX+1];
4704 int err, i;
4705
4706 if (strict_check) {
4707 struct ifinfomsg *ifm;
4708
4709 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
4710 NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump");
4711 return -EINVAL;
4712 }
4713
4714 ifm = nlmsg_data(nlh);
4715 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
4716 ifm->ifi_change || ifm->ifi_index) {
4717 NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request");
4718 return -EINVAL;
4719 }
4720
Johannes Berg8cb08172019-04-26 14:07:28 +02004721 err = nlmsg_parse_deprecated_strict(nlh,
4722 sizeof(struct ifinfomsg),
4723 tb, IFLA_MAX, ifla_policy,
4724 extack);
David Ahern2d011be2018-10-07 20:16:31 -07004725 } else {
Johannes Berg8cb08172019-04-26 14:07:28 +02004726 err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
4727 tb, IFLA_MAX, ifla_policy,
4728 extack);
David Ahern2d011be2018-10-07 20:16:31 -07004729 }
4730 if (err < 0)
4731 return err;
4732
4733 /* new attributes should only be added with strict checking */
4734 for (i = 0; i <= IFLA_MAX; ++i) {
4735 if (!tb[i])
4736 continue;
4737
4738 switch (i) {
4739 case IFLA_EXT_MASK:
4740 *filter_mask = nla_get_u32(tb[i]);
4741 break;
4742 default:
4743 if (strict_check) {
4744 NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request");
4745 return -EINVAL;
4746 }
4747 }
4748 }
4749
4750 return 0;
4751}
4752
John Fastabende5a55a82012-10-24 08:12:57 +00004753static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
4754{
David Ahern2d011be2018-10-07 20:16:31 -07004755 const struct nlmsghdr *nlh = cb->nlh;
John Fastabende5a55a82012-10-24 08:12:57 +00004756 struct net *net = sock_net(skb->sk);
4757 struct net_device *dev;
4758 int idx = 0;
4759 u32 portid = NETLINK_CB(cb->skb).portid;
David Ahern2d011be2018-10-07 20:16:31 -07004760 u32 seq = nlh->nlmsg_seq;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00004761 u32 filter_mask = 0;
Roopa Prabhud64f69b2015-09-15 14:44:29 -07004762 int err;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00004763
David Ahern2d011be2018-10-07 20:16:31 -07004764 err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask,
4765 cb->extack);
4766 if (err < 0 && cb->strict_check)
4767 return err;
John Fastabende5a55a82012-10-24 08:12:57 +00004768
4769 rcu_read_lock();
4770 for_each_netdev_rcu(net, dev) {
4771 const struct net_device_ops *ops = dev->netdev_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00004772 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
John Fastabende5a55a82012-10-24 08:12:57 +00004773
Jiri Pirko898e5062013-01-03 22:48:52 +00004774 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07004775 if (idx >= cb->args[0]) {
4776 err = br_dev->netdev_ops->ndo_bridge_getlink(
4777 skb, portid, seq, dev,
4778 filter_mask, NLM_F_MULTI);
David Ahernf6c57752017-05-15 23:19:17 -07004779 if (err < 0 && err != -EOPNOTSUPP) {
4780 if (likely(skb->len))
4781 break;
4782
4783 goto out_err;
4784 }
Roopa Prabhud64f69b2015-09-15 14:44:29 -07004785 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00004786 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00004787 }
4788
4789 if (ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07004790 if (idx >= cb->args[0]) {
4791 err = ops->ndo_bridge_getlink(skb, portid,
4792 seq, dev,
4793 filter_mask,
4794 NLM_F_MULTI);
David Ahernf6c57752017-05-15 23:19:17 -07004795 if (err < 0 && err != -EOPNOTSUPP) {
4796 if (likely(skb->len))
4797 break;
4798
4799 goto out_err;
4800 }
Roopa Prabhud64f69b2015-09-15 14:44:29 -07004801 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00004802 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00004803 }
4804 }
David Ahernf6c57752017-05-15 23:19:17 -07004805 err = skb->len;
4806out_err:
John Fastabende5a55a82012-10-24 08:12:57 +00004807 rcu_read_unlock();
4808 cb->args[0] = idx;
4809
David Ahernf6c57752017-05-15 23:19:17 -07004810 return err;
John Fastabende5a55a82012-10-24 08:12:57 +00004811}
4812
John Fastabend2469ffd2012-10-24 08:13:03 +00004813static inline size_t bridge_nlmsg_size(void)
4814{
4815 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4816 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4817 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4818 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
4819 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
4820 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
4821 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
4822 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
4823 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
4824 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
4825 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
4826}
4827
Roopa Prabhu02dba432015-01-14 20:02:25 -08004828static int rtnl_bridge_notify(struct net_device *dev)
John Fastabend2469ffd2012-10-24 08:13:03 +00004829{
4830 struct net *net = dev_net(dev);
John Fastabend2469ffd2012-10-24 08:13:03 +00004831 struct sk_buff *skb;
4832 int err = -EOPNOTSUPP;
4833
Roopa Prabhu02dba432015-01-14 20:02:25 -08004834 if (!dev->netdev_ops->ndo_bridge_getlink)
4835 return 0;
4836
John Fastabend2469ffd2012-10-24 08:13:03 +00004837 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
4838 if (!skb) {
4839 err = -ENOMEM;
4840 goto errout;
4841 }
4842
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02004843 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
Roopa Prabhu02dba432015-01-14 20:02:25 -08004844 if (err < 0)
4845 goto errout;
John Fastabend2469ffd2012-10-24 08:13:03 +00004846
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08004847 if (!skb->len)
4848 goto errout;
4849
John Fastabend2469ffd2012-10-24 08:13:03 +00004850 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
4851 return 0;
4852errout:
4853 WARN_ON(err == -EMSGSIZE);
4854 kfree_skb(skb);
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08004855 if (err)
4856 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
John Fastabend2469ffd2012-10-24 08:13:03 +00004857 return err;
4858}
4859
David Ahernc21ef3e2017-04-16 09:48:24 -07004860static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
4861 struct netlink_ext_ack *extack)
John Fastabende5a55a82012-10-24 08:12:57 +00004862{
4863 struct net *net = sock_net(skb->sk);
4864 struct ifinfomsg *ifm;
4865 struct net_device *dev;
John Fastabend2469ffd2012-10-24 08:13:03 +00004866 struct nlattr *br_spec, *attr = NULL;
4867 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02004868 u16 flags = 0;
John Fastabendc38e01b2012-11-02 16:32:36 +00004869 bool have_flags = false;
John Fastabende5a55a82012-10-24 08:12:57 +00004870
4871 if (nlmsg_len(nlh) < sizeof(*ifm))
4872 return -EINVAL;
4873
4874 ifm = nlmsg_data(nlh);
4875 if (ifm->ifi_family != AF_BRIDGE)
4876 return -EPFNOSUPPORT;
4877
4878 dev = __dev_get_by_index(net, ifm->ifi_index);
4879 if (!dev) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004880 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabende5a55a82012-10-24 08:12:57 +00004881 return -ENODEV;
4882 }
4883
John Fastabend2469ffd2012-10-24 08:13:03 +00004884 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4885 if (br_spec) {
4886 nla_for_each_nested(attr, br_spec, rem) {
4887 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01004888 if (nla_len(attr) < sizeof(flags))
4889 return -EINVAL;
4890
John Fastabendc38e01b2012-11-02 16:32:36 +00004891 have_flags = true;
John Fastabend2469ffd2012-10-24 08:13:03 +00004892 flags = nla_get_u16(attr);
4893 break;
4894 }
4895 }
4896 }
4897
4898 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00004899 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4900
4901 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
John Fastabend2469ffd2012-10-24 08:13:03 +00004902 err = -EOPNOTSUPP;
4903 goto out;
4904 }
4905
Petr Machata2fd527b2018-12-12 17:02:48 +00004906 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags,
4907 extack);
John Fastabende5a55a82012-10-24 08:12:57 +00004908 if (err)
4909 goto out;
John Fastabend2469ffd2012-10-24 08:13:03 +00004910
4911 flags &= ~BRIDGE_FLAGS_MASTER;
John Fastabende5a55a82012-10-24 08:12:57 +00004912 }
4913
John Fastabend2469ffd2012-10-24 08:13:03 +00004914 if ((flags & BRIDGE_FLAGS_SELF)) {
4915 if (!dev->netdev_ops->ndo_bridge_setlink)
4916 err = -EOPNOTSUPP;
4917 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08004918 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
Petr Machata2fd527b2018-12-12 17:02:48 +00004919 flags,
4920 extack);
Roopa Prabhu02dba432015-01-14 20:02:25 -08004921 if (!err) {
John Fastabend2469ffd2012-10-24 08:13:03 +00004922 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08004923
4924 /* Generate event to notify upper layer of bridge
4925 * change
4926 */
4927 err = rtnl_bridge_notify(dev);
4928 }
John Fastabend2469ffd2012-10-24 08:13:03 +00004929 }
4930
John Fastabendc38e01b2012-11-02 16:32:36 +00004931 if (have_flags)
John Fastabend2469ffd2012-10-24 08:13:03 +00004932 memcpy(nla_data(attr), &flags, sizeof(flags));
John Fastabende5a55a82012-10-24 08:12:57 +00004933out:
4934 return err;
4935}
4936
David Ahernc21ef3e2017-04-16 09:48:24 -07004937static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
4938 struct netlink_ext_ack *extack)
Vlad Yasevich407af322013-02-13 12:00:12 +00004939{
4940 struct net *net = sock_net(skb->sk);
4941 struct ifinfomsg *ifm;
4942 struct net_device *dev;
4943 struct nlattr *br_spec, *attr = NULL;
4944 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02004945 u16 flags = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +00004946 bool have_flags = false;
4947
4948 if (nlmsg_len(nlh) < sizeof(*ifm))
4949 return -EINVAL;
4950
4951 ifm = nlmsg_data(nlh);
4952 if (ifm->ifi_family != AF_BRIDGE)
4953 return -EPFNOSUPPORT;
4954
4955 dev = __dev_get_by_index(net, ifm->ifi_index);
4956 if (!dev) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004957 NL_SET_ERR_MSG(extack, "unknown ifindex");
Vlad Yasevich407af322013-02-13 12:00:12 +00004958 return -ENODEV;
4959 }
4960
4961 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4962 if (br_spec) {
4963 nla_for_each_nested(attr, br_spec, rem) {
4964 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01004965 if (nla_len(attr) < sizeof(flags))
4966 return -EINVAL;
4967
Vlad Yasevich407af322013-02-13 12:00:12 +00004968 have_flags = true;
4969 flags = nla_get_u16(attr);
4970 break;
4971 }
4972 }
4973 }
4974
Vlad Yasevich407af322013-02-13 12:00:12 +00004975 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
4976 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4977
4978 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
4979 err = -EOPNOTSUPP;
4980 goto out;
4981 }
4982
Roopa Prabhuadd511b2015-01-29 22:40:12 -08004983 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00004984 if (err)
4985 goto out;
4986
4987 flags &= ~BRIDGE_FLAGS_MASTER;
4988 }
4989
4990 if ((flags & BRIDGE_FLAGS_SELF)) {
4991 if (!dev->netdev_ops->ndo_bridge_dellink)
4992 err = -EOPNOTSUPP;
4993 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08004994 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
4995 flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00004996
Roopa Prabhu02dba432015-01-14 20:02:25 -08004997 if (!err) {
Vlad Yasevich407af322013-02-13 12:00:12 +00004998 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08004999
5000 /* Generate event to notify upper layer of bridge
5001 * change
5002 */
5003 err = rtnl_bridge_notify(dev);
5004 }
Vlad Yasevich407af322013-02-13 12:00:12 +00005005 }
5006
5007 if (have_flags)
5008 memcpy(nla_data(attr), &flags, sizeof(flags));
Vlad Yasevich407af322013-02-13 12:00:12 +00005009out:
5010 return err;
5011}
5012
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005013static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
5014{
5015 return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
5016 (!idxattr || idxattr == attrid);
5017}
5018
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02005019#define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
5020static int rtnl_get_offload_stats_attr_size(int attr_id)
5021{
5022 switch (attr_id) {
5023 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
5024 return sizeof(struct rtnl_link_stats64);
5025 }
5026
5027 return 0;
5028}
5029
5030static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
5031 int *prividx)
5032{
5033 struct nlattr *attr = NULL;
5034 int attr_id, size;
5035 void *attr_data;
5036 int err;
5037
5038 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
5039 dev->netdev_ops->ndo_get_offload_stats))
5040 return -ENODATA;
5041
5042 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
5043 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
5044 if (attr_id < *prividx)
5045 continue;
5046
5047 size = rtnl_get_offload_stats_attr_size(attr_id);
5048 if (!size)
5049 continue;
5050
Or Gerlitz3df5b3c2016-11-22 23:09:54 +02005051 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02005052 continue;
5053
5054 attr = nla_reserve_64bit(skb, attr_id, size,
5055 IFLA_OFFLOAD_XSTATS_UNSPEC);
5056 if (!attr)
5057 goto nla_put_failure;
5058
5059 attr_data = nla_data(attr);
5060 memset(attr_data, 0, size);
5061 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
5062 attr_data);
5063 if (err)
5064 goto get_offload_stats_failure;
5065 }
5066
5067 if (!attr)
5068 return -ENODATA;
5069
5070 *prividx = 0;
5071 return 0;
5072
5073nla_put_failure:
5074 err = -EMSGSIZE;
5075get_offload_stats_failure:
5076 *prividx = attr_id;
5077 return err;
5078}
5079
5080static int rtnl_get_offload_stats_size(const struct net_device *dev)
5081{
5082 int nla_size = 0;
5083 int attr_id;
5084 int size;
5085
5086 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
5087 dev->netdev_ops->ndo_get_offload_stats))
5088 return 0;
5089
5090 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
5091 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
Or Gerlitz3df5b3c2016-11-22 23:09:54 +02005092 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02005093 continue;
5094 size = rtnl_get_offload_stats_attr_size(attr_id);
5095 nla_size += nla_total_size_64bit(size);
5096 }
5097
5098 if (nla_size != 0)
5099 nla_size += nla_total_size(0);
5100
5101 return nla_size;
5102}
5103
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005104static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
5105 int type, u32 pid, u32 seq, u32 change,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005106 unsigned int flags, unsigned int filter_mask,
5107 int *idxattr, int *prividx)
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005108{
5109 struct if_stats_msg *ifsm;
5110 struct nlmsghdr *nlh;
5111 struct nlattr *attr;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005112 int s_prividx = *prividx;
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02005113 int err;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005114
5115 ASSERT_RTNL();
5116
5117 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
5118 if (!nlh)
5119 return -EMSGSIZE;
5120
5121 ifsm = nlmsg_data(nlh);
Nikolay Aleksandrovce024f42017-10-03 13:20:48 +03005122 ifsm->family = PF_UNSPEC;
5123 ifsm->pad1 = 0;
5124 ifsm->pad2 = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005125 ifsm->ifindex = dev->ifindex;
5126 ifsm->filter_mask = filter_mask;
5127
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005128 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005129 struct rtnl_link_stats64 *sp;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005130
Nicolas Dichtel58414d32016-04-21 18:58:25 +02005131 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
5132 sizeof(struct rtnl_link_stats64),
5133 IFLA_STATS_UNSPEC);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005134 if (!attr)
5135 goto nla_put_failure;
5136
5137 sp = nla_data(attr);
5138 dev_get_stats(dev, sp);
5139 }
5140
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02005141 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
5142 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
5143
5144 if (ops && ops->fill_linkxstats) {
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02005145 *idxattr = IFLA_STATS_LINK_XSTATS;
Michal Kubecekae0be8d2019-04-26 11:13:06 +02005146 attr = nla_nest_start_noflag(skb,
5147 IFLA_STATS_LINK_XSTATS);
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02005148 if (!attr)
5149 goto nla_put_failure;
5150
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02005151 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
5152 nla_nest_end(skb, attr);
5153 if (err)
5154 goto nla_put_failure;
5155 *idxattr = 0;
5156 }
5157 }
5158
5159 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
5160 *idxattr)) {
5161 const struct rtnl_link_ops *ops = NULL;
5162 const struct net_device *master;
5163
5164 master = netdev_master_upper_dev_get(dev);
5165 if (master)
5166 ops = master->rtnl_link_ops;
5167 if (ops && ops->fill_linkxstats) {
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02005168 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
Michal Kubecekae0be8d2019-04-26 11:13:06 +02005169 attr = nla_nest_start_noflag(skb,
5170 IFLA_STATS_LINK_XSTATS_SLAVE);
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02005171 if (!attr)
5172 goto nla_put_failure;
5173
5174 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02005175 nla_nest_end(skb, attr);
5176 if (err)
5177 goto nla_put_failure;
5178 *idxattr = 0;
5179 }
5180 }
5181
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02005182 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
5183 *idxattr)) {
5184 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
Michal Kubecekae0be8d2019-04-26 11:13:06 +02005185 attr = nla_nest_start_noflag(skb,
5186 IFLA_STATS_LINK_OFFLOAD_XSTATS);
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02005187 if (!attr)
5188 goto nla_put_failure;
5189
5190 err = rtnl_get_offload_stats(skb, dev, prividx);
5191 if (err == -ENODATA)
5192 nla_nest_cancel(skb, attr);
5193 else
5194 nla_nest_end(skb, attr);
5195
5196 if (err && err != -ENODATA)
5197 goto nla_put_failure;
5198 *idxattr = 0;
5199 }
5200
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005201 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
5202 struct rtnl_af_ops *af_ops;
5203
5204 *idxattr = IFLA_STATS_AF_SPEC;
Michal Kubecekae0be8d2019-04-26 11:13:06 +02005205 attr = nla_nest_start_noflag(skb, IFLA_STATS_AF_SPEC);
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005206 if (!attr)
5207 goto nla_put_failure;
5208
Florian Westphal5fa85a02017-10-16 15:44:36 +02005209 rcu_read_lock();
5210 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005211 if (af_ops->fill_stats_af) {
5212 struct nlattr *af;
5213 int err;
5214
Michal Kubecekae0be8d2019-04-26 11:13:06 +02005215 af = nla_nest_start_noflag(skb,
5216 af_ops->family);
Florian Westphal5fa85a02017-10-16 15:44:36 +02005217 if (!af) {
5218 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005219 goto nla_put_failure;
Florian Westphal5fa85a02017-10-16 15:44:36 +02005220 }
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005221 err = af_ops->fill_stats_af(skb, dev);
5222
Florian Westphal5fa85a02017-10-16 15:44:36 +02005223 if (err == -ENODATA) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005224 nla_nest_cancel(skb, af);
Florian Westphal5fa85a02017-10-16 15:44:36 +02005225 } else if (err < 0) {
5226 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005227 goto nla_put_failure;
Florian Westphal5fa85a02017-10-16 15:44:36 +02005228 }
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005229
5230 nla_nest_end(skb, af);
5231 }
5232 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02005233 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005234
5235 nla_nest_end(skb, attr);
5236
5237 *idxattr = 0;
5238 }
5239
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005240 nlmsg_end(skb, nlh);
5241
5242 return 0;
5243
5244nla_put_failure:
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005245 /* not a multi message or no progress mean a real error */
5246 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
5247 nlmsg_cancel(skb, nlh);
5248 else
5249 nlmsg_end(skb, nlh);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005250
5251 return -EMSGSIZE;
5252}
5253
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005254static size_t if_nlmsg_stats_size(const struct net_device *dev,
5255 u32 filter_mask)
5256{
5257 size_t size = 0;
5258
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005259 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005260 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
5261
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02005262 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
5263 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02005264 int attr = IFLA_STATS_LINK_XSTATS;
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02005265
5266 if (ops && ops->get_linkxstats_size) {
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02005267 size += nla_total_size(ops->get_linkxstats_size(dev,
5268 attr));
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02005269 /* for IFLA_STATS_LINK_XSTATS */
5270 size += nla_total_size(0);
5271 }
5272 }
5273
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02005274 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
5275 struct net_device *_dev = (struct net_device *)dev;
5276 const struct rtnl_link_ops *ops = NULL;
5277 const struct net_device *master;
5278
5279 /* netdev_master_upper_dev_get can't take const */
5280 master = netdev_master_upper_dev_get(_dev);
5281 if (master)
5282 ops = master->rtnl_link_ops;
5283 if (ops && ops->get_linkxstats_size) {
5284 int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
5285
5286 size += nla_total_size(ops->get_linkxstats_size(dev,
5287 attr));
5288 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
5289 size += nla_total_size(0);
5290 }
5291 }
5292
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02005293 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
5294 size += rtnl_get_offload_stats_size(dev);
5295
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005296 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
5297 struct rtnl_af_ops *af_ops;
5298
5299 /* for IFLA_STATS_AF_SPEC */
5300 size += nla_total_size(0);
5301
Florian Westphal5fa85a02017-10-16 15:44:36 +02005302 rcu_read_lock();
5303 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005304 if (af_ops->get_stats_af_size) {
5305 size += nla_total_size(
5306 af_ops->get_stats_af_size(dev));
5307
5308 /* for AF_* */
5309 size += nla_total_size(0);
5310 }
5311 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02005312 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005313 }
5314
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005315 return size;
5316}
5317
Jakub Kicinski51bc8602019-01-18 10:46:14 -08005318static int rtnl_valid_stats_req(const struct nlmsghdr *nlh, bool strict_check,
5319 bool is_dump, struct netlink_ext_ack *extack)
5320{
5321 struct if_stats_msg *ifsm;
5322
Eric Dumazet69f23a02019-04-14 11:02:05 -07005323 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifsm))) {
Jakub Kicinski51bc8602019-01-18 10:46:14 -08005324 NL_SET_ERR_MSG(extack, "Invalid header for stats dump");
5325 return -EINVAL;
5326 }
5327
5328 if (!strict_check)
5329 return 0;
5330
5331 ifsm = nlmsg_data(nlh);
5332
5333 /* only requests using strict checks can pass data to influence
5334 * the dump. The legacy exception is filter_mask.
5335 */
5336 if (ifsm->pad1 || ifsm->pad2 || (is_dump && ifsm->ifindex)) {
5337 NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request");
5338 return -EINVAL;
5339 }
5340 if (nlmsg_attrlen(nlh, sizeof(*ifsm))) {
5341 NL_SET_ERR_MSG(extack, "Invalid attributes after stats header");
5342 return -EINVAL;
5343 }
Jakub Kicinski6300acb2019-01-18 10:46:15 -08005344 if (ifsm->filter_mask >= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX + 1)) {
5345 NL_SET_ERR_MSG(extack, "Invalid stats requested through filter mask");
5346 return -EINVAL;
5347 }
Jakub Kicinski51bc8602019-01-18 10:46:14 -08005348
5349 return 0;
5350}
5351
David Ahernc21ef3e2017-04-16 09:48:24 -07005352static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
5353 struct netlink_ext_ack *extack)
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005354{
5355 struct net *net = sock_net(skb->sk);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005356 struct net_device *dev = NULL;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005357 int idxattr = 0, prividx = 0;
5358 struct if_stats_msg *ifsm;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005359 struct sk_buff *nskb;
5360 u32 filter_mask;
5361 int err;
5362
Jakub Kicinski51bc8602019-01-18 10:46:14 -08005363 err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb),
5364 false, extack);
5365 if (err)
5366 return err;
Mathias Krause4775cc12016-12-28 17:52:15 +01005367
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005368 ifsm = nlmsg_data(nlh);
5369 if (ifsm->ifindex > 0)
5370 dev = __dev_get_by_index(net, ifsm->ifindex);
5371 else
5372 return -EINVAL;
5373
5374 if (!dev)
5375 return -ENODEV;
5376
5377 filter_mask = ifsm->filter_mask;
5378 if (!filter_mask)
5379 return -EINVAL;
5380
5381 nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
5382 if (!nskb)
5383 return -ENOBUFS;
5384
5385 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
5386 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005387 0, filter_mask, &idxattr, &prividx);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005388 if (err < 0) {
5389 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
5390 WARN_ON(err == -EMSGSIZE);
5391 kfree_skb(nskb);
5392 } else {
5393 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
5394 }
5395
5396 return err;
5397}
5398
5399static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
5400{
David Ahern841891e2018-10-07 20:16:32 -07005401 struct netlink_ext_ack *extack = cb->extack;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005402 int h, s_h, err, s_idx, s_idxattr, s_prividx;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005403 struct net *net = sock_net(skb->sk);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005404 unsigned int flags = NLM_F_MULTI;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005405 struct if_stats_msg *ifsm;
5406 struct hlist_head *head;
5407 struct net_device *dev;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005408 u32 filter_mask = 0;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005409 int idx = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005410
5411 s_h = cb->args[0];
5412 s_idx = cb->args[1];
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005413 s_idxattr = cb->args[2];
5414 s_prividx = cb->args[3];
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005415
5416 cb->seq = net->dev_base_seq;
5417
Jakub Kicinski51bc8602019-01-18 10:46:14 -08005418 err = rtnl_valid_stats_req(cb->nlh, cb->strict_check, true, extack);
5419 if (err)
5420 return err;
Mathias Krause4775cc12016-12-28 17:52:15 +01005421
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005422 ifsm = nlmsg_data(cb->nlh);
5423 filter_mask = ifsm->filter_mask;
David Ahern841891e2018-10-07 20:16:32 -07005424 if (!filter_mask) {
5425 NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump");
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005426 return -EINVAL;
David Ahern841891e2018-10-07 20:16:32 -07005427 }
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005428
5429 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5430 idx = 0;
5431 head = &net->dev_index_head[h];
5432 hlist_for_each_entry(dev, head, index_hlist) {
5433 if (idx < s_idx)
5434 goto cont;
5435 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
5436 NETLINK_CB(cb->skb).portid,
5437 cb->nlh->nlmsg_seq, 0,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005438 flags, filter_mask,
5439 &s_idxattr, &s_prividx);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005440 /* If we ran out of room on the first message,
5441 * we're in trouble
5442 */
5443 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
5444
5445 if (err < 0)
5446 goto out;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005447 s_prividx = 0;
5448 s_idxattr = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005449 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
5450cont:
5451 idx++;
5452 }
5453 }
5454out:
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005455 cb->args[3] = s_prividx;
5456 cb->args[2] = s_idxattr;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005457 cb->args[1] = idx;
5458 cb->args[0] = h;
5459
5460 return skb->len;
5461}
5462
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463/* Process one rtnetlink message. */
5464
Johannes Berg2d4bc932017-04-12 14:34:04 +02005465static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
5466 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09005468 struct net *net = sock_net(skb->sk);
Florian Westphaladdf9b92017-12-02 21:44:05 +01005469 struct rtnl_link *link;
Florian Westphale4202512017-12-02 21:44:06 +01005470 struct module *owner;
Florian Westphal6853dd42017-08-09 20:41:51 +02005471 int err = -EOPNOTSUPP;
Thomas Grafe2849862007-03-22 11:48:11 -07005472 rtnl_doit_func doit;
Florian Westphal62256f92017-08-09 20:41:52 +02005473 unsigned int flags;
Alexander Kuleshov617cfc72016-01-10 21:26:57 +06005474 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 int family;
5476 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479 if (type > RTM_MAX)
Thomas Graf038890f2007-04-05 14:35:52 -07005480 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481
5482 type -= RTM_BASE;
5483
5484 /* All the messages must have at least 1 byte length */
Hong zhi guo573ce262013-03-27 06:47:04 +00005485 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486 return 0;
5487
Hong zhi guo573ce262013-03-27 06:47:04 +00005488 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 kind = type&3;
5490
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07005491 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07005492 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493
Florian Westphal6853dd42017-08-09 20:41:51 +02005494 rcu_read_lock();
David S. Millerb8f3ab42011-01-18 12:40:38 -08005495 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005496 struct sock *rtnl;
Thomas Grafe2849862007-03-22 11:48:11 -07005497 rtnl_dumpit_func dumpit;
Greg Rosec7ac8672011-06-10 01:27:09 +00005498 u16 min_dump_alloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499
Florian Westphaladdf9b92017-12-02 21:44:05 +01005500 link = rtnl_get_link(family, type);
5501 if (!link || !link->dumpit) {
Florian Westphal6853dd42017-08-09 20:41:51 +02005502 family = PF_UNSPEC;
Florian Westphaladdf9b92017-12-02 21:44:05 +01005503 link = rtnl_get_link(family, type);
5504 if (!link || !link->dumpit)
Florian Westphal6853dd42017-08-09 20:41:51 +02005505 goto err_unlock;
5506 }
Florian Westphale4202512017-12-02 21:44:06 +01005507 owner = link->owner;
Florian Westphaladdf9b92017-12-02 21:44:05 +01005508 dumpit = link->dumpit;
Florian Westphale1fa6d22017-08-09 20:41:47 +02005509
Florian Westphal5c2bb9b2017-08-10 16:52:58 +02005510 if (type == RTM_GETLINK - RTM_BASE)
Florian Westphale1fa6d22017-08-09 20:41:47 +02005511 min_dump_alloc = rtnl_calcit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512
Florian Westphale4202512017-12-02 21:44:06 +01005513 err = 0;
5514 /* need to do this before rcu_read_unlock() */
5515 if (!try_module_get(owner))
5516 err = -EPROTONOSUPPORT;
5517
Florian Westphal6853dd42017-08-09 20:41:51 +02005518 rcu_read_unlock();
5519
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005520 rtnl = net->rtnl;
Florian Westphale4202512017-12-02 21:44:06 +01005521 if (err == 0) {
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00005522 struct netlink_dump_control c = {
5523 .dump = dumpit,
5524 .min_dump_alloc = min_dump_alloc,
Florian Westphale4202512017-12-02 21:44:06 +01005525 .module = owner,
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00005526 };
5527 err = netlink_dump_start(rtnl, skb, nlh, &c);
Florian Westphale4202512017-12-02 21:44:06 +01005528 /* netlink_dump_start() will keep a reference on
5529 * module if dump is still in progress.
5530 */
5531 module_put(owner);
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00005532 }
Eric Dumazet2907c352011-05-25 07:34:04 +00005533 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 }
5535
Florian Westphaladdf9b92017-12-02 21:44:05 +01005536 link = rtnl_get_link(family, type);
5537 if (!link || !link->doit) {
Florian Westphal8caa38b2017-08-10 16:53:01 +02005538 family = PF_UNSPEC;
Florian Westphaladdf9b92017-12-02 21:44:05 +01005539 link = rtnl_get_link(PF_UNSPEC, type);
5540 if (!link || !link->doit)
5541 goto out_unlock;
Florian Westphal8caa38b2017-08-10 16:53:01 +02005542 }
5543
Florian Westphale4202512017-12-02 21:44:06 +01005544 owner = link->owner;
5545 if (!try_module_get(owner)) {
5546 err = -EPROTONOSUPPORT;
5547 goto out_unlock;
5548 }
5549
Florian Westphaladdf9b92017-12-02 21:44:05 +01005550 flags = link->flags;
Florian Westphal62256f92017-08-09 20:41:52 +02005551 if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
Florian Westphaladdf9b92017-12-02 21:44:05 +01005552 doit = link->doit;
Florian Westphal62256f92017-08-09 20:41:52 +02005553 rcu_read_unlock();
5554 if (doit)
5555 err = doit(skb, nlh, extack);
Florian Westphale4202512017-12-02 21:44:06 +01005556 module_put(owner);
Florian Westphal62256f92017-08-09 20:41:52 +02005557 return err;
5558 }
Florian Westphal6853dd42017-08-09 20:41:51 +02005559 rcu_read_unlock();
5560
Florian Westphal0cc09022017-08-09 20:41:50 +02005561 rtnl_lock();
Florian Westphaladdf9b92017-12-02 21:44:05 +01005562 link = rtnl_get_link(family, type);
5563 if (link && link->doit)
5564 err = link->doit(skb, nlh, extack);
Florian Westphal0cc09022017-08-09 20:41:50 +02005565 rtnl_unlock();
Florian Westphaladdf9b92017-12-02 21:44:05 +01005566
Florian Westphale4202512017-12-02 21:44:06 +01005567 module_put(owner);
5568
Florian Westphaladdf9b92017-12-02 21:44:05 +01005569 return err;
5570
5571out_unlock:
5572 rcu_read_unlock();
Florian Westphal0cc09022017-08-09 20:41:50 +02005573 return err;
5574
5575err_unlock:
Florian Westphal6853dd42017-08-09 20:41:51 +02005576 rcu_read_unlock();
Florian Westphal0cc09022017-08-09 20:41:50 +02005577 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578}
5579
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07005580static void rtnetlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07005582 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583}
5584
Julien Gomes5f729ea2017-06-20 13:54:16 -07005585static int rtnetlink_bind(struct net *net, int group)
5586{
5587 switch (group) {
5588 case RTNLGRP_IPV4_MROUTE_R:
5589 case RTNLGRP_IPV6_MROUTE_R:
5590 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
5591 return -EPERM;
5592 break;
5593 }
5594 return 0;
5595}
5596
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
5598{
Jiri Pirko351638e2013-05-28 01:30:21 +00005599 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02005600
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601 switch (event) {
Vlad Yasevich5138e862017-04-04 09:23:41 -04005602 case NETDEV_REBOOT:
Xin Long8a212582017-10-15 18:13:41 +08005603 case NETDEV_CHANGEMTU:
David Ahern37536542017-07-19 10:22:40 -07005604 case NETDEV_CHANGEADDR:
Vlad Yasevich5138e862017-04-04 09:23:41 -04005605 case NETDEV_CHANGENAME:
5606 case NETDEV_FEAT_CHANGE:
5607 case NETDEV_BONDING_FAILOVER:
Xin Longe6e66592017-10-15 18:13:43 +08005608 case NETDEV_POST_TYPE_CHANGE:
Vlad Yasevich5138e862017-04-04 09:23:41 -04005609 case NETDEV_NOTIFY_PEERS:
Xin Longdc709f32017-10-15 18:13:44 +08005610 case NETDEV_CHANGEUPPER:
Vlad Yasevich5138e862017-04-04 09:23:41 -04005611 case NETDEV_RESEND_IGMP:
Vlad Yasevich5138e862017-04-04 09:23:41 -04005612 case NETDEV_CHANGEINFODATA:
Xin Longeeda3fb2017-10-24 13:54:19 +08005613 case NETDEV_CHANGELOWERSTATE:
Xin Longebdcf042017-10-15 18:13:42 +08005614 case NETDEV_CHANGE_TX_QUEUE_LEN:
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04005615 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01005616 GFP_KERNEL, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617 break;
5618 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 break;
5620 }
5621 return NOTIFY_DONE;
5622}
5623
5624static struct notifier_block rtnetlink_dev_notifier = {
5625 .notifier_call = rtnetlink_event,
5626};
5627
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005628
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005629static int __net_init rtnetlink_net_init(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005630{
5631 struct sock *sk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00005632 struct netlink_kernel_cfg cfg = {
5633 .groups = RTNLGRP_MAX,
5634 .input = rtnetlink_rcv,
5635 .cb_mutex = &rtnl_mutex,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00005636 .flags = NL_CFG_F_NONROOT_RECV,
Julien Gomes5f729ea2017-06-20 13:54:16 -07005637 .bind = rtnetlink_bind,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00005638 };
5639
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00005640 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005641 if (!sk)
5642 return -ENOMEM;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005643 net->rtnl = sk;
5644 return 0;
5645}
5646
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005647static void __net_exit rtnetlink_net_exit(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005648{
Denis V. Lunev775516b2008-01-18 23:55:19 -08005649 netlink_kernel_release(net->rtnl);
5650 net->rtnl = NULL;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005651}
5652
5653static struct pernet_operations rtnetlink_net_ops = {
5654 .init = rtnetlink_net_init,
5655 .exit = rtnetlink_net_exit,
5656};
5657
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658void __init rtnetlink_init(void)
5659{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005660 if (register_pernet_subsys(&rtnetlink_net_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661 panic("rtnetlink_init: cannot initialize rtnetlink\n");
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005662
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663 register_netdevice_notifier(&rtnetlink_dev_notifier);
Thomas Graf340d17f2007-03-22 11:49:22 -07005664
Greg Rosec7ac8672011-06-10 01:27:09 +00005665 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
Florian Westphalb97bac62017-08-09 20:41:48 +02005666 rtnl_dump_ifinfo, 0);
5667 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
5668 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
5669 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
Thomas Graf687ad8c2007-03-22 11:59:42 -07005670
Florian Westphalb97bac62017-08-09 20:41:48 +02005671 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
5672 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
5673 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
John Fastabend77162022012-04-15 06:43:56 +00005674
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02005675 rtnl_register(PF_UNSPEC, RTM_NEWLINKPROP, rtnl_newlinkprop, NULL, 0);
5676 rtnl_register(PF_UNSPEC, RTM_DELLINKPROP, rtnl_dellinkprop, NULL, 0);
5677
Florian Westphalb97bac62017-08-09 20:41:48 +02005678 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
5679 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
Roopa Prabhu5b2f94b2018-12-15 22:35:08 -08005680 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0);
John Fastabende5a55a82012-10-24 08:12:57 +00005681
Florian Westphalb97bac62017-08-09 20:41:48 +02005682 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
5683 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
5684 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005685
5686 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
Florian Westphalb97bac62017-08-09 20:41:48 +02005687 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688}