blob: 68e0682450c60dc7da96bda94517551f72f53f6f [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{
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07001429 return dev_xdp_prog_id(dev, XDP_MODE_DRV);
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001430}
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{
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07001434 return dev_xdp_prog_id(dev, XDP_MODE_HW);
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001435}
1436
1437static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev,
1438 u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr,
1439 u32 (*get_prog_id)(struct net_device *dev))
1440{
1441 u32 curr_id;
1442 int err;
1443
1444 curr_id = get_prog_id(dev);
1445 if (!curr_id)
1446 return 0;
1447
1448 *prog_id = curr_id;
1449 err = nla_put_u32(skb, attr, curr_id);
1450 if (err)
1451 return err;
1452
1453 if (*mode != XDP_ATTACHED_NONE)
1454 *mode = XDP_ATTACHED_MULTI;
1455 else
1456 *mode = tgt_mode;
1457
1458 return 0;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001459}
1460
Brenden Blancod1fdd912016-07-19 12:16:49 -07001461static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1462{
Brenden Blancod1fdd912016-07-19 12:16:49 -07001463 struct nlattr *xdp;
Martin KaFai Lau58038692017-06-15 17:29:09 -07001464 u32 prog_id;
Brenden Blancod1fdd912016-07-19 12:16:49 -07001465 int err;
Jakub Kicinski4f91da22018-07-11 20:36:38 -07001466 u8 mode;
Brenden Blancod1fdd912016-07-19 12:16:49 -07001467
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001468 xdp = nla_nest_start_noflag(skb, IFLA_XDP);
Brenden Blancod1fdd912016-07-19 12:16:49 -07001469 if (!xdp)
1470 return -EMSGSIZE;
David S. Millerb5cdae32017-04-18 15:36:58 -04001471
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001472 prog_id = 0;
1473 mode = XDP_ATTACHED_NONE;
Jakub Kicinski202aabe2018-07-16 19:08:50 -07001474 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB,
1475 IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb);
1476 if (err)
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001477 goto err_cancel;
Jakub Kicinski202aabe2018-07-16 19:08:50 -07001478 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV,
1479 IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv);
1480 if (err)
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001481 goto err_cancel;
Jakub Kicinski202aabe2018-07-16 19:08:50 -07001482 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW,
1483 IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw);
1484 if (err)
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001485 goto err_cancel;
1486
Jakub Kicinski4f91da22018-07-11 20:36:38 -07001487 err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode);
Brenden Blancod1fdd912016-07-19 12:16:49 -07001488 if (err)
1489 goto err_cancel;
1490
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001491 if (prog_id && mode != XDP_ATTACHED_MULTI) {
Martin KaFai Lau58038692017-06-15 17:29:09 -07001492 err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
1493 if (err)
1494 goto err_cancel;
1495 }
1496
Brenden Blancod1fdd912016-07-19 12:16:49 -07001497 nla_nest_end(skb, xdp);
1498 return 0;
1499
1500err_cancel:
1501 nla_nest_cancel(skb, xdp);
1502 return err;
1503}
1504
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001505static u32 rtnl_get_event(unsigned long event)
1506{
1507 u32 rtnl_event_type = IFLA_EVENT_NONE;
1508
1509 switch (event) {
1510 case NETDEV_REBOOT:
1511 rtnl_event_type = IFLA_EVENT_REBOOT;
1512 break;
1513 case NETDEV_FEAT_CHANGE:
1514 rtnl_event_type = IFLA_EVENT_FEATURES;
1515 break;
1516 case NETDEV_BONDING_FAILOVER:
1517 rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
1518 break;
1519 case NETDEV_NOTIFY_PEERS:
1520 rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
1521 break;
1522 case NETDEV_RESEND_IGMP:
1523 rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
1524 break;
1525 case NETDEV_CHANGEINFODATA:
1526 rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
1527 break;
1528 default:
1529 break;
1530 }
1531
1532 return rtnl_event_type;
1533}
1534
Florian Westphal79110a02017-09-26 13:58:40 +02001535static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
1536{
1537 const struct net_device *upper_dev;
1538 int ret = 0;
1539
1540 rcu_read_lock();
1541
1542 upper_dev = netdev_master_upper_dev_get_rcu(dev);
1543 if (upper_dev)
1544 ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);
1545
1546 rcu_read_unlock();
1547 return ret;
1548}
1549
Sabrina Dubrocafeadc4b2019-05-14 15:12:19 +02001550static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev,
1551 bool force)
Florian Westphal79110a02017-09-26 13:58:40 +02001552{
1553 int ifindex = dev_get_iflink(dev);
1554
Sabrina Dubrocafeadc4b2019-05-14 15:12:19 +02001555 if (force || dev->ifindex != ifindex)
1556 return nla_put_u32(skb, IFLA_LINK, ifindex);
Florian Westphal79110a02017-09-26 13:58:40 +02001557
Sabrina Dubrocafeadc4b2019-05-14 15:12:19 +02001558 return 0;
Florian Westphal79110a02017-09-26 13:58:40 +02001559}
1560
Florian Westphal6c557002017-10-02 23:50:05 +02001561static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
1562 struct net_device *dev)
1563{
1564 char buf[IFALIASZ];
1565 int ret;
1566
1567 ret = dev_get_alias(dev, buf, sizeof(buf));
1568 return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
1569}
1570
Florian Westphalb1e66b92017-09-26 13:58:42 +02001571static int rtnl_fill_link_netnsid(struct sk_buff *skb,
Jiri Benc79e1ad12017-11-02 17:04:38 -02001572 const struct net_device *dev,
Guillaume Naultd4e4fdf2019-10-23 18:39:04 +02001573 struct net *src_net, gfp_t gfp)
Florian Westphalb1e66b92017-09-26 13:58:42 +02001574{
Sabrina Dubrocafeadc4b2019-05-14 15:12:19 +02001575 bool put_iflink = false;
1576
Florian Westphalb1e66b92017-09-26 13:58:42 +02001577 if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
1578 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1579
1580 if (!net_eq(dev_net(dev), link_net)) {
Guillaume Naultd4e4fdf2019-10-23 18:39:04 +02001581 int id = peernet2id_alloc(src_net, link_net, gfp);
Florian Westphalb1e66b92017-09-26 13:58:42 +02001582
1583 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1584 return -EMSGSIZE;
Sabrina Dubrocafeadc4b2019-05-14 15:12:19 +02001585
1586 put_iflink = true;
Florian Westphalb1e66b92017-09-26 13:58:42 +02001587 }
1588 }
1589
Sabrina Dubrocafeadc4b2019-05-14 15:12:19 +02001590 return nla_put_iflink(skb, dev, put_iflink);
Florian Westphalb1e66b92017-09-26 13:58:42 +02001591}
1592
Florian Westphal070cbf5b2017-10-16 15:44:35 +02001593static int rtnl_fill_link_af(struct sk_buff *skb,
1594 const struct net_device *dev,
1595 u32 ext_filter_mask)
1596{
1597 const struct rtnl_af_ops *af_ops;
1598 struct nlattr *af_spec;
1599
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001600 af_spec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
Florian Westphal070cbf5b2017-10-16 15:44:35 +02001601 if (!af_spec)
1602 return -EMSGSIZE;
1603
Florian Westphal5fa85a02017-10-16 15:44:36 +02001604 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Florian Westphal070cbf5b2017-10-16 15:44:35 +02001605 struct nlattr *af;
1606 int err;
1607
1608 if (!af_ops->fill_link_af)
1609 continue;
1610
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001611 af = nla_nest_start_noflag(skb, af_ops->family);
Florian Westphal070cbf5b2017-10-16 15:44:35 +02001612 if (!af)
1613 return -EMSGSIZE;
1614
1615 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1616 /*
1617 * Caller may return ENODATA to indicate that there
1618 * was no data to be dumped. This is not an error, it
1619 * means we should trim the attribute header and
1620 * continue.
1621 */
1622 if (err == -ENODATA)
1623 nla_nest_cancel(skb, af);
1624 else if (err < 0)
1625 return -EMSGSIZE;
1626
1627 nla_nest_end(skb, af);
1628 }
1629
1630 nla_nest_end(skb, af_spec);
1631 return 0;
1632}
1633
Jiri Pirko88f4fb02019-09-30 11:48:17 +02001634static int rtnl_fill_alt_ifnames(struct sk_buff *skb,
1635 const struct net_device *dev)
1636{
1637 struct netdev_name_node *name_node;
1638 int count = 0;
1639
1640 list_for_each_entry(name_node, &dev->name_node->list, list) {
1641 if (nla_put_string(skb, IFLA_ALT_IFNAME, name_node->name))
1642 return -EMSGSIZE;
1643 count++;
1644 }
1645 return count;
1646}
1647
1648static int rtnl_fill_prop_list(struct sk_buff *skb,
1649 const struct net_device *dev)
1650{
1651 struct nlattr *prop_list;
1652 int ret;
1653
1654 prop_list = nla_nest_start(skb, IFLA_PROP_LIST);
1655 if (!prop_list)
1656 return -EMSGSIZE;
1657
1658 ret = rtnl_fill_alt_ifnames(skb, dev);
1659 if (ret <= 0)
1660 goto nest_cancel;
1661
1662 nla_nest_end(skb, prop_list);
1663 return 0;
1664
1665nest_cancel:
1666 nla_nest_cancel(skb, prop_list);
1667 return ret;
1668}
1669
Roopa Prabhu829eb202020-07-31 17:34:01 -07001670static int rtnl_fill_proto_down(struct sk_buff *skb,
1671 const struct net_device *dev)
1672{
1673 struct nlattr *pr;
1674 u32 preason;
1675
1676 if (nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
1677 goto nla_put_failure;
1678
1679 preason = dev->proto_down_reason;
1680 if (!preason)
1681 return 0;
1682
1683 pr = nla_nest_start(skb, IFLA_PROTO_DOWN_REASON);
1684 if (!pr)
1685 return -EMSGSIZE;
1686
1687 if (nla_put_u32(skb, IFLA_PROTO_DOWN_REASON_VALUE, preason)) {
1688 nla_nest_cancel(skb, pr);
1689 goto nla_put_failure;
1690 }
1691
1692 nla_nest_end(skb, pr);
1693 return 0;
1694
1695nla_put_failure:
1696 return -EMSGSIZE;
1697}
1698
Jiri Benc79e1ad12017-11-02 17:04:38 -02001699static int rtnl_fill_ifinfo(struct sk_buff *skb,
1700 struct net_device *dev, struct net *src_net,
Patrick McHardy575c3e22007-05-22 17:00:49 -07001701 int type, u32 pid, u32 seq, u32 change,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001702 unsigned int flags, u32 ext_filter_mask,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01001703 u32 event, int *new_nsid, int new_ifindex,
Guillaume Naultd4e4fdf2019-10-23 18:39:04 +02001704 int tgt_netnsid, gfp_t gfp)
Thomas Grafb60c5112006-08-04 23:05:34 -07001705{
1706 struct ifinfomsg *ifm;
1707 struct nlmsghdr *nlh;
1708
Eric Dumazet2907c352011-05-25 07:34:04 +00001709 ASSERT_RTNL();
Thomas Grafb60c5112006-08-04 23:05:34 -07001710 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1711 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08001712 return -EMSGSIZE;
Thomas Grafb60c5112006-08-04 23:05:34 -07001713
1714 ifm = nlmsg_data(nlh);
1715 ifm->ifi_family = AF_UNSPEC;
1716 ifm->__ifi_pad = 0;
1717 ifm->ifi_type = dev->type;
1718 ifm->ifi_index = dev->ifindex;
1719 ifm->ifi_flags = dev_get_flags(dev);
1720 ifm->ifi_change = change;
1721
Christian Brauner7e4a8d52018-09-04 21:53:53 +02001722 if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid))
Jiri Benc79e1ad12017-11-02 17:04:38 -02001723 goto nla_put_failure;
1724
David S. Millera6574342012-04-01 20:12:00 -04001725 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1726 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1727 nla_put_u8(skb, IFLA_OPERSTATE,
1728 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1729 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1730 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
Stephen Hemminger3e7a50c2018-07-27 13:43:22 -07001731 nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) ||
1732 nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) ||
David S. Millera6574342012-04-01 20:12:00 -04001733 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
Ben Greearedbc0bb2012-03-29 12:51:30 +00001734 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001735 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
Eric Dumazetc70ce022016-03-21 09:55:10 -07001736 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1737 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001738#ifdef CONFIG_RPS
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001739 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001740#endif
Florian Westphal79110a02017-09-26 13:58:40 +02001741 put_master_ifindex(skb, dev) ||
Jiri Pirko9a572472012-12-27 23:49:39 +00001742 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
David S. Millera6574342012-04-01 20:12:00 -04001743 (dev->qdisc &&
1744 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
Florian Westphal6c557002017-10-02 23:50:05 +02001745 nla_put_ifalias(skb, dev) ||
david decotigny2d3b4792014-03-29 09:48:35 -07001746 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
David Decotignyb2d3bcf2018-01-18 09:59:13 -08001747 atomic_read(&dev->carrier_up_count) +
1748 atomic_read(&dev->carrier_down_count)) ||
David Decotignyb2d3bcf2018-01-18 09:59:13 -08001749 nla_put_u32(skb, IFLA_CARRIER_UP_COUNT,
1750 atomic_read(&dev->carrier_up_count)) ||
1751 nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT,
1752 atomic_read(&dev->carrier_down_count)))
David S. Millera6574342012-04-01 20:12:00 -04001753 goto nla_put_failure;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001754
Roopa Prabhu829eb202020-07-31 17:34:01 -07001755 if (rtnl_fill_proto_down(skb, dev))
1756 goto nla_put_failure;
1757
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001758 if (event != IFLA_EVENT_NONE) {
1759 if (nla_put_u32(skb, IFLA_EVENT, event))
1760 goto nla_put_failure;
1761 }
1762
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001763 if (rtnl_fill_link_ifmap(skb, dev))
1764 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
1766 if (dev->addr_len) {
David S. Millera6574342012-04-01 20:12:00 -04001767 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1768 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1769 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 }
1771
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001772 if (rtnl_phys_port_id_fill(skb, dev))
1773 goto nla_put_failure;
1774
David Aherndb24a902015-03-17 20:23:15 -06001775 if (rtnl_phys_port_name_fill(skb, dev))
1776 goto nla_put_failure;
1777
Jiri Pirko82f28412014-11-28 14:34:18 +01001778 if (rtnl_phys_switch_id_fill(skb, dev))
1779 goto nla_put_failure;
1780
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001781 if (rtnl_fill_stats(skb, dev))
Pavel Emelyanov96e74082008-05-21 14:12:46 -07001782 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
Florian Westphal250fc3d2017-09-26 13:58:41 +02001784 if (rtnl_fill_vf(skb, dev, ext_filter_mask))
David S. Millera6574342012-04-01 20:12:00 -04001785 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001786
David Gibsonc53864f2014-04-24 10:22:36 +10001787 if (rtnl_port_fill(skb, dev, ext_filter_mask))
Scott Feldman57b61082010-05-17 22:49:55 -07001788 goto nla_put_failure;
1789
Brenden Blancod1fdd912016-07-19 12:16:49 -07001790 if (rtnl_xdp_fill(skb, dev))
1791 goto nla_put_failure;
1792
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001793 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07001794 if (rtnl_link_fill(skb, dev) < 0)
1795 goto nla_put_failure;
1796 }
1797
Guillaume Naultd4e4fdf2019-10-23 18:39:04 +02001798 if (rtnl_fill_link_netnsid(skb, dev, src_net, gfp))
Florian Westphalb1e66b92017-09-26 13:58:42 +02001799 goto nla_put_failure;
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001800
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02001801 if (new_nsid &&
1802 nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0)
1803 goto nla_put_failure;
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01001804 if (new_ifindex &&
1805 nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0)
1806 goto nla_put_failure;
1807
Michal Kubecekf74877a2019-12-11 10:58:14 +01001808 if (memchr_inv(dev->perm_addr, '\0', dev->addr_len) &&
1809 nla_put(skb, IFLA_PERM_ADDRESS, dev->addr_len, dev->perm_addr))
1810 goto nla_put_failure;
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02001811
Florian Westphal5fa85a02017-10-16 15:44:36 +02001812 rcu_read_lock();
Florian Westphal070cbf5b2017-10-16 15:44:35 +02001813 if (rtnl_fill_link_af(skb, dev, ext_filter_mask))
Florian Westphal5fa85a02017-10-16 15:44:36 +02001814 goto nla_put_failure_rcu;
1815 rcu_read_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +00001816
Jiri Pirko88f4fb02019-09-30 11:48:17 +02001817 if (rtnl_fill_prop_list(skb, dev))
1818 goto nla_put_failure;
1819
Johannes Berg053c0952015-01-16 22:09:00 +01001820 nlmsg_end(skb, nlh);
1821 return 0;
Thomas Grafb60c5112006-08-04 23:05:34 -07001822
Florian Westphal5fa85a02017-10-16 15:44:36 +02001823nla_put_failure_rcu:
1824 rcu_read_unlock();
Thomas Grafb60c5112006-08-04 23:05:34 -07001825nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001826 nlmsg_cancel(skb, nlh);
1827 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828}
1829
Jiri Pirkof7b12602014-02-18 20:53:18 +01001830static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1831 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1832 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1833 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1834 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1835 [IFLA_MTU] = { .type = NLA_U32 },
1836 [IFLA_LINK] = { .type = NLA_U32 },
1837 [IFLA_MASTER] = { .type = NLA_U32 },
1838 [IFLA_CARRIER] = { .type = NLA_U8 },
1839 [IFLA_TXQLEN] = { .type = NLA_U32 },
1840 [IFLA_WEIGHT] = { .type = NLA_U32 },
1841 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1842 [IFLA_LINKMODE] = { .type = NLA_U8 },
1843 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1844 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1845 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
Nicolas Dichtel2459b4c2017-10-11 16:24:48 +02001846 /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
1847 * allow 0-length string (needed to remove an alias).
1848 */
1849 [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001850 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1851 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1852 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1853 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1854 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1855 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1856 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1857 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
Stephen Hemminger46e6b992017-12-07 15:40:19 -08001858 [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 },
1859 [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 },
Jiri Pirko02637fc2014-11-28 14:34:16 +01001860 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
david decotigny2d3b4792014-03-29 09:48:35 -07001861 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
Jiri Pirko82f28412014-11-28 14:34:18 +01001862 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
Nicolas Dichtel317f4812015-01-15 15:11:18 +01001863 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001864 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001865 [IFLA_XDP] = { .type = NLA_NESTED },
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001866 [IFLA_EVENT] = { .type = NLA_U32 },
Serhey Popovychdb833d42017-06-20 14:35:23 +03001867 [IFLA_GROUP] = { .type = NLA_U32 },
Christian Brauner7e4a8d52018-09-04 21:53:53 +02001868 [IFLA_TARGET_NETNSID] = { .type = NLA_S32 },
David Decotignyb2d3bcf2018-01-18 09:59:13 -08001869 [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 },
1870 [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 },
Stephen Hemminger3e7a50c2018-07-27 13:43:22 -07001871 [IFLA_MIN_MTU] = { .type = NLA_U32 },
1872 [IFLA_MAX_MTU] = { .type = NLA_U32 },
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02001873 [IFLA_PROP_LIST] = { .type = NLA_NESTED },
1874 [IFLA_ALT_IFNAME] = { .type = NLA_STRING,
1875 .len = ALTIFNAMSIZ - 1 },
Michal Kubecekf74877a2019-12-11 10:58:14 +01001876 [IFLA_PERM_ADDRESS] = { .type = NLA_REJECT },
Roopa Prabhu829eb202020-07-31 17:34:01 -07001877 [IFLA_PROTO_DOWN_REASON] = { .type = NLA_NESTED },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001878};
1879
1880static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1881 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1882 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1883 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1884 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1885};
1886
Jiri Pirkof7b12602014-02-18 20:53:18 +01001887static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
Daniel Borkmann364d5712015-02-05 18:44:04 +01001888 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
Denis Kirjanov75345f882019-06-17 10:53:41 +02001889 [IFLA_VF_BROADCAST] = { .type = NLA_REJECT },
Daniel Borkmann364d5712015-02-05 18:44:04 +01001890 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
Moshe Shemesh79aab092016-09-22 12:11:15 +03001891 [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED },
Daniel Borkmann364d5712015-02-05 18:44:04 +01001892 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1893 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1894 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1895 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001896 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001897 [IFLA_VF_STATS] = { .type = NLA_NESTED },
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00001898 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
Eli Cohencc8e27c2016-03-11 22:58:34 +02001899 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1900 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001901};
1902
Jiri Pirkof7b12602014-02-18 20:53:18 +01001903static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1904 [IFLA_PORT_VF] = { .type = NLA_U32 },
1905 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1906 .len = PORT_PROFILE_MAX },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001907 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1908 .len = PORT_UUID_MAX },
1909 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1910 .len = PORT_UUID_MAX },
1911 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1912 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
Daniel Borkmann025331d2017-02-17 01:56:11 +01001913
1914 /* Unused, but we need to keep it here since user space could
1915 * fill it. It's also broken with regard to NLA_BINARY use in
1916 * combination with structs.
1917 */
1918 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1919 .len = sizeof(struct ifla_port_vsi) },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001920};
1921
Brenden Blancod1fdd912016-07-19 12:16:49 -07001922static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
Toke Høiland-Jørgensen92234c82020-03-25 18:23:26 +01001923 [IFLA_XDP_UNSPEC] = { .strict_start_type = IFLA_XDP_EXPECTED_FD },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001924 [IFLA_XDP_FD] = { .type = NLA_S32 },
Toke Høiland-Jørgensen92234c82020-03-25 18:23:26 +01001925 [IFLA_XDP_EXPECTED_FD] = { .type = NLA_S32 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001926 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 },
Daniel Borkmann85de8572016-11-28 23:16:54 +01001927 [IFLA_XDP_FLAGS] = { .type = NLA_U32 },
Martin KaFai Lau58038692017-06-15 17:29:09 -07001928 [IFLA_XDP_PROG_ID] = { .type = NLA_U32 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001929};
1930
David Aherndc599f72016-02-02 08:17:07 -08001931static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1932{
1933 const struct rtnl_link_ops *ops = NULL;
1934 struct nlattr *linfo[IFLA_INFO_MAX + 1];
1935
Johannes Berg8cb08172019-04-26 14:07:28 +02001936 if (nla_parse_nested_deprecated(linfo, IFLA_INFO_MAX, nla, ifla_info_policy, NULL) < 0)
David Aherndc599f72016-02-02 08:17:07 -08001937 return NULL;
1938
1939 if (linfo[IFLA_INFO_KIND]) {
1940 char kind[MODULE_NAME_LEN];
1941
1942 nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1943 ops = rtnl_link_ops_get(kind);
1944 }
1945
1946 return ops;
1947}
1948
1949static bool link_master_filtered(struct net_device *dev, int master_idx)
1950{
1951 struct net_device *master;
1952
1953 if (!master_idx)
1954 return false;
1955
1956 master = netdev_master_upper_dev_get(dev);
1957 if (!master || master->ifindex != master_idx)
1958 return true;
1959
1960 return false;
1961}
1962
1963static bool link_kind_filtered(const struct net_device *dev,
1964 const struct rtnl_link_ops *kind_ops)
1965{
1966 if (kind_ops && dev->rtnl_link_ops != kind_ops)
1967 return true;
1968
1969 return false;
1970}
1971
1972static bool link_dump_filtered(struct net_device *dev,
1973 int master_idx,
1974 const struct rtnl_link_ops *kind_ops)
1975{
1976 if (link_master_filtered(dev, master_idx) ||
1977 link_kind_filtered(dev, kind_ops))
1978 return true;
1979
1980 return false;
1981}
1982
Christian Braunerc383edc2018-09-04 21:53:47 +02001983/**
1984 * rtnl_get_net_ns_capable - Get netns if sufficiently privileged.
1985 * @sk: netlink socket
1986 * @netnsid: network namespace identifier
1987 *
1988 * Returns the network namespace identified by netnsid on success or an error
1989 * pointer on failure.
1990 */
1991struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid)
Jiri Benc79e1ad12017-11-02 17:04:38 -02001992{
1993 struct net *net;
1994
Andrei Vaginf428fe42018-01-02 23:27:33 -08001995 net = get_net_ns_by_id(sock_net(sk), netnsid);
Jiri Benc79e1ad12017-11-02 17:04:38 -02001996 if (!net)
1997 return ERR_PTR(-EINVAL);
1998
1999 /* For now, the caller is required to have CAP_NET_ADMIN in
2000 * the user namespace owning the target net ns.
2001 */
Andrei Vaginf428fe42018-01-02 23:27:33 -08002002 if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) {
Jiri Benc79e1ad12017-11-02 17:04:38 -02002003 put_net(net);
2004 return ERR_PTR(-EACCES);
2005 }
2006 return net;
2007}
Christian Braunerc383edc2018-09-04 21:53:47 +02002008EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable);
Jiri Benc79e1ad12017-11-02 17:04:38 -02002009
David Ahern905cf0a2018-10-07 20:16:30 -07002010static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh,
2011 bool strict_check, struct nlattr **tb,
2012 struct netlink_ext_ack *extack)
2013{
2014 int hdrlen;
2015
2016 if (strict_check) {
2017 struct ifinfomsg *ifm;
2018
2019 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
2020 NL_SET_ERR_MSG(extack, "Invalid header for link dump");
2021 return -EINVAL;
2022 }
2023
2024 ifm = nlmsg_data(nlh);
2025 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
2026 ifm->ifi_change) {
2027 NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request");
2028 return -EINVAL;
2029 }
2030 if (ifm->ifi_index) {
2031 NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps");
2032 return -EINVAL;
2033 }
2034
Johannes Berg8cb08172019-04-26 14:07:28 +02002035 return nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb,
2036 IFLA_MAX, ifla_policy,
2037 extack);
David Ahern905cf0a2018-10-07 20:16:30 -07002038 }
2039
2040 /* A hack to preserve kernel<->userspace interface.
2041 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
2042 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
2043 * what iproute2 < v3.9.0 used.
2044 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
2045 * attribute, its netlink message is shorter than struct ifinfomsg.
2046 */
2047 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2048 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2049
Johannes Berg8cb08172019-04-26 14:07:28 +02002050 return nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy,
2051 extack);
David Ahern905cf0a2018-10-07 20:16:30 -07002052}
2053
Thomas Grafb60c5112006-08-04 23:05:34 -07002054static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
David Ahern905cf0a2018-10-07 20:16:30 -07002056 struct netlink_ext_ack *extack = cb->extack;
2057 const struct nlmsghdr *nlh = cb->nlh;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002058 struct net *net = sock_net(skb->sk);
Jiri Benc79e1ad12017-11-02 17:04:38 -02002059 struct net *tgt_net = net;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002060 int h, s_h;
2061 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 struct net_device *dev;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002063 struct hlist_head *head;
Greg Rose115c9b82012-02-21 16:54:48 -05002064 struct nlattr *tb[IFLA_MAX+1];
2065 u32 ext_filter_mask = 0;
David Aherndc599f72016-02-02 08:17:07 -08002066 const struct rtnl_link_ops *kind_ops = NULL;
2067 unsigned int flags = NLM_F_MULTI;
2068 int master_idx = 0;
Jiri Benc79e1ad12017-11-02 17:04:38 -02002069 int netnsid = -1;
David Ahern905cf0a2018-10-07 20:16:30 -07002070 int err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002072 s_h = cb->args[0];
2073 s_idx = cb->args[1];
2074
David Ahern905cf0a2018-10-07 20:16:30 -07002075 err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack);
2076 if (err < 0) {
2077 if (cb->strict_check)
2078 return err;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02002079
David Ahern905cf0a2018-10-07 20:16:30 -07002080 goto walk_entries;
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00002081 }
Greg Rose115c9b82012-02-21 16:54:48 -05002082
David Ahern905cf0a2018-10-07 20:16:30 -07002083 for (i = 0; i <= IFLA_MAX; ++i) {
2084 if (!tb[i])
2085 continue;
2086
2087 /* new attributes should only be added with strict checking */
2088 switch (i) {
2089 case IFLA_TARGET_NETNSID:
2090 netnsid = nla_get_s32(tb[i]);
2091 tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid);
2092 if (IS_ERR(tgt_net)) {
2093 NL_SET_ERR_MSG(extack, "Invalid target network namespace id");
2094 return PTR_ERR(tgt_net);
2095 }
2096 break;
2097 case IFLA_EXT_MASK:
2098 ext_filter_mask = nla_get_u32(tb[i]);
2099 break;
2100 case IFLA_MASTER:
2101 master_idx = nla_get_u32(tb[i]);
2102 break;
2103 case IFLA_LINKINFO:
2104 kind_ops = linkinfo_to_kind_ops(tb[i]);
2105 break;
2106 default:
2107 if (cb->strict_check) {
2108 NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request");
2109 return -EINVAL;
2110 }
2111 }
2112 }
2113
2114 if (master_idx || kind_ops)
2115 flags |= NLM_F_DUMP_FILTERED;
2116
2117walk_entries:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002118 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
2119 idx = 0;
Jiri Benc79e1ad12017-11-02 17:04:38 -02002120 head = &tgt_net->dev_index_head[h];
Eric Dumazetcac5e652015-02-27 09:42:50 -08002121 hlist_for_each_entry(dev, head, index_hlist) {
David Aherndc599f72016-02-02 08:17:07 -08002122 if (link_dump_filtered(dev, master_idx, kind_ops))
Zhang Shengju3f0ae052016-11-19 23:28:32 +08002123 goto cont;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002124 if (idx < s_idx)
2125 goto cont;
Jiri Benc79e1ad12017-11-02 17:04:38 -02002126 err = rtnl_fill_ifinfo(skb, dev, net,
2127 RTM_NEWLINK,
David Gibson973462b2014-04-24 10:22:35 +10002128 NETLINK_CB(cb->skb).portid,
David Ahern905cf0a2018-10-07 20:16:30 -07002129 nlh->nlmsg_seq, 0, flags,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01002130 ext_filter_mask, 0, NULL, 0,
Guillaume Naultd4e4fdf2019-10-23 18:39:04 +02002131 netnsid, GFP_KERNEL);
David Gibson973462b2014-04-24 10:22:35 +10002132
David Ahernf6c57752017-05-15 23:19:17 -07002133 if (err < 0) {
2134 if (likely(skb->len))
2135 goto out;
2136
2137 goto out_err;
2138 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07002139cont:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002140 idx++;
2141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 }
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002143out:
David Ahernf6c57752017-05-15 23:19:17 -07002144 err = skb->len;
2145out_err:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002146 cb->args[1] = idx;
2147 cb->args[0] = h;
Jakub Sitnickid0225782017-08-09 17:39:12 +02002148 cb->seq = net->dev_base_seq;
2149 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Jiri Benc79e1ad12017-11-02 17:04:38 -02002150 if (netnsid >= 0)
2151 put_net(tgt_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
David Ahernf6c57752017-05-15 23:19:17 -07002153 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154}
2155
Johannes Bergfceb6432017-04-12 14:34:07 +02002156int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
2157 struct netlink_ext_ack *exterr)
Jiri Pirkof7b12602014-02-18 20:53:18 +01002158{
Johannes Berg8cb08172019-04-26 14:07:28 +02002159 return nla_parse_deprecated(tb, IFLA_MAX, head, len, ifla_policy,
2160 exterr);
Jiri Pirkof7b12602014-02-18 20:53:18 +01002161}
2162EXPORT_SYMBOL(rtnl_nla_parse_ifla);
Scott Feldman57b61082010-05-17 22:49:55 -07002163
Eric W. Biederman81adee42009-11-08 00:53:51 -08002164struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
2165{
2166 struct net *net;
2167 /* Examine the link attributes and figure out which
2168 * network namespace we are talking about.
2169 */
2170 if (tb[IFLA_NET_NS_PID])
2171 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
Eric W. Biedermanf0630522011-05-04 17:51:50 -07002172 else if (tb[IFLA_NET_NS_FD])
2173 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
Eric W. Biederman81adee42009-11-08 00:53:51 -08002174 else
2175 net = get_net(src_net);
2176 return net;
2177}
2178EXPORT_SYMBOL(rtnl_link_get_net);
2179
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002180/* Figure out which network namespace we are talking about by
2181 * examining the link attributes in the following order:
2182 *
2183 * 1. IFLA_NET_NS_PID
2184 * 2. IFLA_NET_NS_FD
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002185 * 3. IFLA_TARGET_NETNSID
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002186 */
2187static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net,
2188 struct nlattr *tb[])
2189{
2190 struct net *net;
2191
2192 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])
2193 return rtnl_link_get_net(src_net, tb);
2194
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002195 if (!tb[IFLA_TARGET_NETNSID])
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002196 return get_net(src_net);
2197
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002198 net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID]));
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002199 if (!net)
2200 return ERR_PTR(-EINVAL);
2201
2202 return net;
2203}
2204
2205static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb,
2206 struct net *src_net,
2207 struct nlattr *tb[], int cap)
2208{
2209 struct net *net;
2210
2211 net = rtnl_link_get_net_by_nlattr(src_net, tb);
2212 if (IS_ERR(net))
2213 return net;
2214
2215 if (!netlink_ns_capable(skb, net->user_ns, cap)) {
2216 put_net(net);
2217 return ERR_PTR(-EPERM);
2218 }
2219
2220 return net;
2221}
2222
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002223/* Verify that rtnetlink requests do not pass additional properties
2224 * potentially referring to different network namespaces.
2225 */
2226static int rtnl_ensure_unique_netns(struct nlattr *tb[],
2227 struct netlink_ext_ack *extack,
2228 bool netns_id_only)
2229{
2230
2231 if (netns_id_only) {
2232 if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD])
2233 return 0;
2234
2235 NL_SET_ERR_MSG(extack, "specified netns attribute not supported");
2236 return -EOPNOTSUPP;
2237 }
2238
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002239 if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]))
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002240 goto invalid_attr;
2241
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002242 if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD]))
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002243 goto invalid_attr;
2244
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002245 if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID]))
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002246 goto invalid_attr;
2247
2248 return 0;
2249
2250invalid_attr:
2251 NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified");
2252 return -EINVAL;
2253}
2254
Thomas Graf1840bb12008-02-23 19:54:36 -08002255static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
2256{
2257 if (dev) {
2258 if (tb[IFLA_ADDRESS] &&
2259 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
2260 return -EINVAL;
2261
2262 if (tb[IFLA_BROADCAST] &&
2263 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
2264 return -EINVAL;
2265 }
2266
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002267 if (tb[IFLA_AF_SPEC]) {
2268 struct nlattr *af;
2269 int rem, err;
2270
2271 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2272 const struct rtnl_af_ops *af_ops;
2273
Florian Westphal5fa85a02017-10-16 15:44:36 +02002274 rcu_read_lock();
2275 af_ops = rtnl_af_lookup(nla_type(af));
2276 if (!af_ops) {
2277 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002278 return -EAFNOSUPPORT;
Florian Westphal5fa85a02017-10-16 15:44:36 +02002279 }
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002280
Florian Westphal5fa85a02017-10-16 15:44:36 +02002281 if (!af_ops->set_link_af) {
2282 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002283 return -EOPNOTSUPP;
Florian Westphal5fa85a02017-10-16 15:44:36 +02002284 }
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002285
2286 if (af_ops->validate_link_af) {
Kurt Van Dijck6d3a9a62011-01-26 04:55:24 +00002287 err = af_ops->validate_link_af(dev, af);
Florian Westphal5fa85a02017-10-16 15:44:36 +02002288 if (err < 0) {
2289 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002290 return err;
Florian Westphal5fa85a02017-10-16 15:44:36 +02002291 }
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002292 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02002293
2294 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002295 }
2296 }
2297
Thomas Graf1840bb12008-02-23 19:54:36 -08002298 return 0;
2299}
2300
Eli Cohencc8e27c2016-03-11 22:58:34 +02002301static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
2302 int guid_type)
2303{
2304 const struct net_device_ops *ops = dev->netdev_ops;
2305
2306 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
2307}
2308
2309static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
2310{
2311 if (dev->type != ARPHRD_INFINIBAND)
2312 return -EOPNOTSUPP;
2313
2314 return handle_infiniband_guid(dev, ivt, guid_type);
2315}
2316
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002317static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
Chris Wrightc02db8c2010-05-16 01:05:45 -07002318{
Chris Wrightc02db8c2010-05-16 01:05:45 -07002319 const struct net_device_ops *ops = dev->netdev_ops;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002320 int err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002321
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002322 if (tb[IFLA_VF_MAC]) {
2323 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03002324
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002325 if (ivm->vf >= INT_MAX)
2326 return -EINVAL;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002327 err = -EOPNOTSUPP;
2328 if (ops->ndo_set_vf_mac)
2329 err = ops->ndo_set_vf_mac(dev, ivm->vf,
2330 ivm->mac);
2331 if (err < 0)
2332 return err;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002333 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002334
2335 if (tb[IFLA_VF_VLAN]) {
2336 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
2337
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002338 if (ivv->vf >= INT_MAX)
2339 return -EINVAL;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002340 err = -EOPNOTSUPP;
2341 if (ops->ndo_set_vf_vlan)
2342 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
Moshe Shemesh79aab092016-09-22 12:11:15 +03002343 ivv->qos,
2344 htons(ETH_P_8021Q));
2345 if (err < 0)
2346 return err;
2347 }
2348
2349 if (tb[IFLA_VF_VLAN_LIST]) {
2350 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
2351 struct nlattr *attr;
2352 int rem, len = 0;
2353
2354 err = -EOPNOTSUPP;
2355 if (!ops->ndo_set_vf_vlan)
2356 return err;
2357
2358 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
2359 if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
2360 nla_len(attr) < NLA_HDRLEN) {
2361 return -EINVAL;
2362 }
2363 if (len >= MAX_VLAN_LIST_LEN)
2364 return -EOPNOTSUPP;
2365 ivvl[len] = nla_data(attr);
2366
2367 len++;
2368 }
Arnd Bergmannfa34cd92016-09-30 18:13:49 +02002369 if (len == 0)
2370 return -EINVAL;
2371
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002372 if (ivvl[0]->vf >= INT_MAX)
2373 return -EINVAL;
Moshe Shemesh79aab092016-09-22 12:11:15 +03002374 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
2375 ivvl[0]->qos, ivvl[0]->vlan_proto);
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002376 if (err < 0)
2377 return err;
2378 }
2379
2380 if (tb[IFLA_VF_TX_RATE]) {
2381 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
2382 struct ifla_vf_info ivf;
2383
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002384 if (ivt->vf >= INT_MAX)
2385 return -EINVAL;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002386 err = -EOPNOTSUPP;
2387 if (ops->ndo_get_vf_config)
2388 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
2389 if (err < 0)
2390 return err;
2391
2392 err = -EOPNOTSUPP;
2393 if (ops->ndo_set_vf_rate)
2394 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2395 ivf.min_tx_rate,
2396 ivt->rate);
2397 if (err < 0)
2398 return err;
2399 }
2400
2401 if (tb[IFLA_VF_RATE]) {
2402 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
2403
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002404 if (ivt->vf >= INT_MAX)
2405 return -EINVAL;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002406 err = -EOPNOTSUPP;
2407 if (ops->ndo_set_vf_rate)
2408 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2409 ivt->min_tx_rate,
2410 ivt->max_tx_rate);
2411 if (err < 0)
2412 return err;
2413 }
2414
2415 if (tb[IFLA_VF_SPOOFCHK]) {
2416 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
2417
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002418 if (ivs->vf >= INT_MAX)
2419 return -EINVAL;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002420 err = -EOPNOTSUPP;
2421 if (ops->ndo_set_vf_spoofchk)
2422 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
2423 ivs->setting);
2424 if (err < 0)
2425 return err;
2426 }
2427
2428 if (tb[IFLA_VF_LINK_STATE]) {
2429 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
2430
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002431 if (ivl->vf >= INT_MAX)
2432 return -EINVAL;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002433 err = -EOPNOTSUPP;
2434 if (ops->ndo_set_vf_link_state)
2435 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
2436 ivl->link_state);
2437 if (err < 0)
2438 return err;
2439 }
2440
2441 if (tb[IFLA_VF_RSS_QUERY_EN]) {
2442 struct ifla_vf_rss_query_en *ivrssq_en;
2443
2444 err = -EOPNOTSUPP;
2445 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002446 if (ivrssq_en->vf >= INT_MAX)
2447 return -EINVAL;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002448 if (ops->ndo_set_vf_rss_query_en)
2449 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
2450 ivrssq_en->setting);
2451 if (err < 0)
2452 return err;
2453 }
2454
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00002455 if (tb[IFLA_VF_TRUST]) {
2456 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
2457
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002458 if (ivt->vf >= INT_MAX)
2459 return -EINVAL;
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00002460 err = -EOPNOTSUPP;
2461 if (ops->ndo_set_vf_trust)
2462 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
2463 if (err < 0)
2464 return err;
2465 }
2466
Eli Cohencc8e27c2016-03-11 22:58:34 +02002467 if (tb[IFLA_VF_IB_NODE_GUID]) {
2468 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
2469
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002470 if (ivt->vf >= INT_MAX)
2471 return -EINVAL;
Eli Cohencc8e27c2016-03-11 22:58:34 +02002472 if (!ops->ndo_set_vf_guid)
2473 return -EOPNOTSUPP;
Eli Cohencc8e27c2016-03-11 22:58:34 +02002474 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
2475 }
2476
2477 if (tb[IFLA_VF_IB_PORT_GUID]) {
2478 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
2479
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002480 if (ivt->vf >= INT_MAX)
2481 return -EINVAL;
Eli Cohencc8e27c2016-03-11 22:58:34 +02002482 if (!ops->ndo_set_vf_guid)
2483 return -EOPNOTSUPP;
2484
2485 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
2486 }
2487
Chris Wrightc02db8c2010-05-16 01:05:45 -07002488 return err;
2489}
2490
David Ahern33eaf2a2017-10-04 17:48:46 -07002491static int do_set_master(struct net_device *dev, int ifindex,
2492 struct netlink_ext_ack *extack)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002493{
Jiri Pirko898e5062013-01-03 22:48:52 +00002494 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002495 const struct net_device_ops *ops;
2496 int err;
2497
Jiri Pirko898e5062013-01-03 22:48:52 +00002498 if (upper_dev) {
2499 if (upper_dev->ifindex == ifindex)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002500 return 0;
Jiri Pirko898e5062013-01-03 22:48:52 +00002501 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002502 if (ops->ndo_del_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002503 err = ops->ndo_del_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002504 if (err)
2505 return err;
2506 } else {
2507 return -EOPNOTSUPP;
2508 }
2509 }
2510
2511 if (ifindex) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002512 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
2513 if (!upper_dev)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002514 return -EINVAL;
Jiri Pirko898e5062013-01-03 22:48:52 +00002515 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002516 if (ops->ndo_add_slave) {
David Ahern33eaf2a2017-10-04 17:48:46 -07002517 err = ops->ndo_add_slave(upper_dev, dev, extack);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002518 if (err)
2519 return err;
2520 } else {
2521 return -EOPNOTSUPP;
2522 }
2523 }
2524 return 0;
2525}
2526
Roopa Prabhu829eb202020-07-31 17:34:01 -07002527static const struct nla_policy ifla_proto_down_reason_policy[IFLA_PROTO_DOWN_REASON_VALUE + 1] = {
2528 [IFLA_PROTO_DOWN_REASON_MASK] = { .type = NLA_U32 },
2529 [IFLA_PROTO_DOWN_REASON_VALUE] = { .type = NLA_U32 },
2530};
2531
2532static int do_set_proto_down(struct net_device *dev,
2533 struct nlattr *nl_proto_down,
2534 struct nlattr *nl_proto_down_reason,
2535 struct netlink_ext_ack *extack)
2536{
2537 struct nlattr *pdreason[IFLA_PROTO_DOWN_REASON_MAX + 1];
2538 const struct net_device_ops *ops = dev->netdev_ops;
2539 unsigned long mask = 0;
2540 u32 value;
2541 bool proto_down;
2542 int err;
2543
2544 if (!ops->ndo_change_proto_down) {
2545 NL_SET_ERR_MSG(extack, "Protodown not supported by device");
2546 return -EOPNOTSUPP;
2547 }
2548
2549 if (nl_proto_down_reason) {
2550 err = nla_parse_nested_deprecated(pdreason,
2551 IFLA_PROTO_DOWN_REASON_MAX,
2552 nl_proto_down_reason,
2553 ifla_proto_down_reason_policy,
2554 NULL);
2555 if (err < 0)
2556 return err;
2557
2558 if (!pdreason[IFLA_PROTO_DOWN_REASON_VALUE]) {
2559 NL_SET_ERR_MSG(extack, "Invalid protodown reason value");
2560 return -EINVAL;
2561 }
2562
2563 value = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_VALUE]);
2564
2565 if (pdreason[IFLA_PROTO_DOWN_REASON_MASK])
2566 mask = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_MASK]);
2567
2568 dev_change_proto_down_reason(dev, mask, value);
2569 }
2570
2571 if (nl_proto_down) {
2572 proto_down = nla_get_u8(nl_proto_down);
2573
2574 /* Dont turn off protodown if there are active reasons */
2575 if (!proto_down && dev->proto_down_reason) {
2576 NL_SET_ERR_MSG(extack, "Cannot clear protodown, active reasons");
2577 return -EBUSY;
2578 }
2579 err = dev_change_proto_down(dev,
2580 proto_down);
2581 if (err)
2582 return err;
2583 }
2584
2585 return 0;
2586}
2587
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002588#define DO_SETLINK_MODIFIED 0x01
Nicolas Dichtelba998902014-09-01 16:07:29 +02002589/* notify flag means notify + modified. */
2590#define DO_SETLINK_NOTIFY 0x03
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002591static int do_setlink(const struct sk_buff *skb,
2592 struct net_device *dev, struct ifinfomsg *ifm,
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002593 struct netlink_ext_ack *extack,
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002594 struct nlattr **tb, char *ifname, int status)
Patrick McHardy0157f602007-06-13 12:03:36 -07002595{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002596 const struct net_device_ops *ops = dev->netdev_ops;
Patrick McHardy0157f602007-06-13 12:03:36 -07002597 int err;
2598
Eric Dumazet644c7ee2018-06-05 09:25:19 -07002599 err = validate_linkmsg(dev, tb);
2600 if (err < 0)
2601 return err;
2602
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002603 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) {
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002604 struct net *net = rtnl_link_get_net_capable(skb, dev_net(dev),
2605 tb, CAP_NET_ADMIN);
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002606 if (IS_ERR(net)) {
2607 err = PTR_ERR(net);
2608 goto errout;
2609 }
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002610
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002611 err = dev_change_net_namespace(dev, net, ifname);
2612 put_net(net);
2613 if (err)
2614 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002615 status |= DO_SETLINK_MODIFIED;
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002616 }
2617
Patrick McHardy0157f602007-06-13 12:03:36 -07002618 if (tb[IFLA_MAP]) {
2619 struct rtnl_link_ifmap *u_map;
2620 struct ifmap k_map;
2621
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002622 if (!ops->ndo_set_config) {
Patrick McHardy0157f602007-06-13 12:03:36 -07002623 err = -EOPNOTSUPP;
2624 goto errout;
2625 }
2626
2627 if (!netif_device_present(dev)) {
2628 err = -ENODEV;
2629 goto errout;
2630 }
2631
2632 u_map = nla_data(tb[IFLA_MAP]);
2633 k_map.mem_start = (unsigned long) u_map->mem_start;
2634 k_map.mem_end = (unsigned long) u_map->mem_end;
2635 k_map.base_addr = (unsigned short) u_map->base_addr;
2636 k_map.irq = (unsigned char) u_map->irq;
2637 k_map.dma = (unsigned char) u_map->dma;
2638 k_map.port = (unsigned char) u_map->port;
2639
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002640 err = ops->ndo_set_config(dev, &k_map);
Patrick McHardy0157f602007-06-13 12:03:36 -07002641 if (err < 0)
2642 goto errout;
2643
Nicolas Dichtelba998902014-09-01 16:07:29 +02002644 status |= DO_SETLINK_NOTIFY;
Patrick McHardy0157f602007-06-13 12:03:36 -07002645 }
2646
2647 if (tb[IFLA_ADDRESS]) {
2648 struct sockaddr *sa;
2649 int len;
2650
WANG Cong153711f2017-07-20 11:27:57 -07002651 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2652 sizeof(*sa));
Patrick McHardy0157f602007-06-13 12:03:36 -07002653 sa = kmalloc(len, GFP_KERNEL);
2654 if (!sa) {
2655 err = -ENOMEM;
2656 goto errout;
2657 }
2658 sa->sa_family = dev->type;
2659 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
2660 dev->addr_len);
Petr Machata3a37a962018-12-13 11:54:30 +00002661 err = dev_set_mac_address(dev, sa, extack);
Patrick McHardy0157f602007-06-13 12:03:36 -07002662 kfree(sa);
2663 if (err)
2664 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002665 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002666 }
2667
2668 if (tb[IFLA_MTU]) {
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07002669 err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack);
Patrick McHardy0157f602007-06-13 12:03:36 -07002670 if (err < 0)
2671 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002672 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002673 }
2674
Vlad Dogarucbda10f2011-01-13 23:38:30 +00002675 if (tb[IFLA_GROUP]) {
2676 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Nicolas Dichtelba998902014-09-01 16:07:29 +02002677 status |= DO_SETLINK_NOTIFY;
Vlad Dogarucbda10f2011-01-13 23:38:30 +00002678 }
2679
Patrick McHardy0157f602007-06-13 12:03:36 -07002680 /*
2681 * Interface selected by interface index but interface
2682 * name provided implies that a name change has been
2683 * requested.
2684 */
2685 if (ifm->ifi_index > 0 && ifname[0]) {
2686 err = dev_change_name(dev, ifname);
2687 if (err < 0)
2688 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002689 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002690 }
2691
Stephen Hemminger0b815a12008-09-22 21:28:11 -07002692 if (tb[IFLA_IFALIAS]) {
2693 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2694 nla_len(tb[IFLA_IFALIAS]));
2695 if (err < 0)
2696 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002697 status |= DO_SETLINK_NOTIFY;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07002698 }
2699
Patrick McHardy0157f602007-06-13 12:03:36 -07002700 if (tb[IFLA_BROADCAST]) {
2701 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00002702 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Patrick McHardy0157f602007-06-13 12:03:36 -07002703 }
2704
2705 if (ifm->ifi_flags || ifm->ifi_change) {
Petr Machata567c5e12018-12-06 17:05:42 +00002706 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
2707 extack);
Johannes Berg5f9021c2008-11-16 23:20:31 -08002708 if (err < 0)
2709 goto errout;
Patrick McHardy0157f602007-06-13 12:03:36 -07002710 }
2711
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002712 if (tb[IFLA_MASTER]) {
David Ahern33eaf2a2017-10-04 17:48:46 -07002713 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002714 if (err)
2715 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002716 status |= DO_SETLINK_MODIFIED;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002717 }
2718
Jiri Pirko9a572472012-12-27 23:49:39 +00002719 if (tb[IFLA_CARRIER]) {
2720 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2721 if (err)
2722 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002723 status |= DO_SETLINK_MODIFIED;
Jiri Pirko9a572472012-12-27 23:49:39 +00002724 }
2725
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002726 if (tb[IFLA_TXQLEN]) {
Alexey Dobriyan0cd29502017-05-17 13:30:44 +03002727 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002728
Cong Wang6a643dd2018-01-25 18:26:22 -08002729 err = dev_change_tx_queue_len(dev, value);
2730 if (err)
2731 goto errout;
2732 status |= DO_SETLINK_MODIFIED;
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002733 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002734
Stephen Hemminger46e6b992017-12-07 15:40:19 -08002735 if (tb[IFLA_GSO_MAX_SIZE]) {
2736 u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]);
2737
2738 if (max_size > GSO_MAX_SIZE) {
2739 err = -EINVAL;
2740 goto errout;
2741 }
2742
2743 if (dev->gso_max_size ^ max_size) {
2744 netif_set_gso_max_size(dev, max_size);
2745 status |= DO_SETLINK_MODIFIED;
2746 }
2747 }
2748
2749 if (tb[IFLA_GSO_MAX_SEGS]) {
2750 u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
2751
2752 if (max_segs > GSO_MAX_SEGS) {
2753 err = -EINVAL;
2754 goto errout;
2755 }
2756
2757 if (dev->gso_max_segs ^ max_segs) {
2758 dev->gso_max_segs = max_segs;
2759 status |= DO_SETLINK_MODIFIED;
2760 }
2761 }
2762
Patrick McHardy0157f602007-06-13 12:03:36 -07002763 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002764 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Patrick McHardy0157f602007-06-13 12:03:36 -07002765
2766 if (tb[IFLA_LINKMODE]) {
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002767 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2768
David S. Miller93b2d4a2008-02-17 18:35:07 -08002769 write_lock_bh(&dev_base_lock);
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002770 if (dev->link_mode ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02002771 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002772 dev->link_mode = value;
David S. Miller93b2d4a2008-02-17 18:35:07 -08002773 write_unlock_bh(&dev_base_lock);
Patrick McHardy0157f602007-06-13 12:03:36 -07002774 }
2775
Chris Wrightc02db8c2010-05-16 01:05:45 -07002776 if (tb[IFLA_VFINFO_LIST]) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002777 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
Chris Wrightc02db8c2010-05-16 01:05:45 -07002778 struct nlattr *attr;
2779 int rem;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002780
Chris Wrightc02db8c2010-05-16 01:05:45 -07002781 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002782 if (nla_type(attr) != IFLA_VF_INFO ||
2783 nla_len(attr) < NLA_HDRLEN) {
David Howells253683b2010-05-21 02:25:27 +00002784 err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002785 goto errout;
David Howells253683b2010-05-21 02:25:27 +00002786 }
Johannes Berg8cb08172019-04-26 14:07:28 +02002787 err = nla_parse_nested_deprecated(vfinfo, IFLA_VF_MAX,
2788 attr,
2789 ifla_vf_policy,
2790 NULL);
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002791 if (err < 0)
2792 goto errout;
2793 err = do_setvfinfo(dev, vfinfo);
Chris Wrightc02db8c2010-05-16 01:05:45 -07002794 if (err < 0)
2795 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002796 status |= DO_SETLINK_NOTIFY;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002797 }
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00002798 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002799 err = 0;
2800
Scott Feldman57b61082010-05-17 22:49:55 -07002801 if (tb[IFLA_VF_PORTS]) {
2802 struct nlattr *port[IFLA_PORT_MAX+1];
2803 struct nlattr *attr;
2804 int vf;
2805 int rem;
2806
2807 err = -EOPNOTSUPP;
2808 if (!ops->ndo_set_vf_port)
2809 goto errout;
2810
2811 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
Daniel Borkmann035d2102015-07-13 00:06:02 +02002812 if (nla_type(attr) != IFLA_VF_PORT ||
2813 nla_len(attr) < NLA_HDRLEN) {
2814 err = -EINVAL;
2815 goto errout;
2816 }
Johannes Berg8cb08172019-04-26 14:07:28 +02002817 err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
2818 attr,
2819 ifla_port_policy,
2820 NULL);
Scott Feldman57b61082010-05-17 22:49:55 -07002821 if (err < 0)
2822 goto errout;
2823 if (!port[IFLA_PORT_VF]) {
2824 err = -EOPNOTSUPP;
2825 goto errout;
2826 }
2827 vf = nla_get_u32(port[IFLA_PORT_VF]);
2828 err = ops->ndo_set_vf_port(dev, vf, port);
2829 if (err < 0)
2830 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002831 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07002832 }
2833 }
2834 err = 0;
2835
2836 if (tb[IFLA_PORT_SELF]) {
2837 struct nlattr *port[IFLA_PORT_MAX+1];
2838
Johannes Berg8cb08172019-04-26 14:07:28 +02002839 err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
2840 tb[IFLA_PORT_SELF],
2841 ifla_port_policy, NULL);
Scott Feldman57b61082010-05-17 22:49:55 -07002842 if (err < 0)
2843 goto errout;
2844
2845 err = -EOPNOTSUPP;
2846 if (ops->ndo_set_vf_port)
2847 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
2848 if (err < 0)
2849 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002850 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07002851 }
Thomas Graff8ff1822010-11-16 04:30:14 +00002852
2853 if (tb[IFLA_AF_SPEC]) {
2854 struct nlattr *af;
2855 int rem;
2856
2857 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2858 const struct rtnl_af_ops *af_ops;
2859
Florian Westphal5fa85a02017-10-16 15:44:36 +02002860 rcu_read_lock();
2861
Gustavo A. R. Silva058c8d52017-10-20 19:43:11 -05002862 BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
Thomas Graff8ff1822010-11-16 04:30:14 +00002863
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002864 err = af_ops->set_link_af(dev, af);
Florian Westphal5fa85a02017-10-16 15:44:36 +02002865 if (err < 0) {
2866 rcu_read_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +00002867 goto errout;
Florian Westphal5fa85a02017-10-16 15:44:36 +02002868 }
Thomas Graff8ff1822010-11-16 04:30:14 +00002869
Florian Westphal5fa85a02017-10-16 15:44:36 +02002870 rcu_read_unlock();
Nicolas Dichtelba998902014-09-01 16:07:29 +02002871 status |= DO_SETLINK_NOTIFY;
Thomas Graff8ff1822010-11-16 04:30:14 +00002872 }
2873 }
Scott Feldman57b61082010-05-17 22:49:55 -07002874 err = 0;
2875
Roopa Prabhu829eb202020-07-31 17:34:01 -07002876 if (tb[IFLA_PROTO_DOWN] || tb[IFLA_PROTO_DOWN_REASON]) {
2877 err = do_set_proto_down(dev, tb[IFLA_PROTO_DOWN],
2878 tb[IFLA_PROTO_DOWN_REASON], extack);
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07002879 if (err)
2880 goto errout;
2881 status |= DO_SETLINK_NOTIFY;
2882 }
2883
Brenden Blancod1fdd912016-07-19 12:16:49 -07002884 if (tb[IFLA_XDP]) {
2885 struct nlattr *xdp[IFLA_XDP_MAX + 1];
Daniel Borkmann85de8572016-11-28 23:16:54 +01002886 u32 xdp_flags = 0;
Brenden Blancod1fdd912016-07-19 12:16:49 -07002887
Johannes Berg8cb08172019-04-26 14:07:28 +02002888 err = nla_parse_nested_deprecated(xdp, IFLA_XDP_MAX,
2889 tb[IFLA_XDP],
2890 ifla_xdp_policy, NULL);
Brenden Blancod1fdd912016-07-19 12:16:49 -07002891 if (err < 0)
2892 goto errout;
2893
Martin KaFai Lau58038692017-06-15 17:29:09 -07002894 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
Brenden Blanco262d8622016-07-20 17:22:34 -07002895 err = -EINVAL;
2896 goto errout;
2897 }
Daniel Borkmann85de8572016-11-28 23:16:54 +01002898
2899 if (xdp[IFLA_XDP_FLAGS]) {
2900 xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
2901 if (xdp_flags & ~XDP_FLAGS_MASK) {
2902 err = -EINVAL;
2903 goto errout;
2904 }
Jakub Kicinskiee5d0322017-06-21 18:25:04 -07002905 if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
Daniel Borkmann0489df92017-05-12 01:04:45 +02002906 err = -EINVAL;
2907 goto errout;
2908 }
Daniel Borkmann85de8572016-11-28 23:16:54 +01002909 }
2910
Brenden Blancod1fdd912016-07-19 12:16:49 -07002911 if (xdp[IFLA_XDP_FD]) {
Toke Høiland-Jørgensen92234c82020-03-25 18:23:26 +01002912 int expected_fd = -1;
2913
2914 if (xdp_flags & XDP_FLAGS_REPLACE) {
2915 if (!xdp[IFLA_XDP_EXPECTED_FD]) {
2916 err = -EINVAL;
2917 goto errout;
2918 }
2919 expected_fd =
2920 nla_get_s32(xdp[IFLA_XDP_EXPECTED_FD]);
2921 }
2922
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002923 err = dev_change_xdp_fd(dev, extack,
Daniel Borkmann85de8572016-11-28 23:16:54 +01002924 nla_get_s32(xdp[IFLA_XDP_FD]),
Toke Høiland-Jørgensen92234c82020-03-25 18:23:26 +01002925 expected_fd,
Daniel Borkmann85de8572016-11-28 23:16:54 +01002926 xdp_flags);
Brenden Blancod1fdd912016-07-19 12:16:49 -07002927 if (err)
2928 goto errout;
2929 status |= DO_SETLINK_NOTIFY;
2930 }
2931 }
2932
Patrick McHardy0157f602007-06-13 12:03:36 -07002933errout:
Nicolas Dichtelba998902014-09-01 16:07:29 +02002934 if (status & DO_SETLINK_MODIFIED) {
Xin Long64ff90c2017-10-15 18:13:45 +08002935 if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
Nicolas Dichtelba998902014-09-01 16:07:29 +02002936 netdev_state_change(dev);
2937
2938 if (err < 0)
2939 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",
2940 dev->name);
2941 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002942
Patrick McHardy0157f602007-06-13 12:03:36 -07002943 return err;
2944}
2945
Jiri Pirkocc6090e2019-09-30 11:48:19 +02002946static struct net_device *rtnl_dev_get(struct net *net,
2947 struct nlattr *ifname_attr,
Jiri Pirko76c9ac02019-09-30 11:48:20 +02002948 struct nlattr *altifname_attr,
Jiri Pirkocc6090e2019-09-30 11:48:19 +02002949 char *ifname)
2950{
Jiri Pirko76c9ac02019-09-30 11:48:20 +02002951 char buffer[ALTIFNAMSIZ];
Jiri Pirkocc6090e2019-09-30 11:48:19 +02002952
2953 if (!ifname) {
2954 ifname = buffer;
2955 if (ifname_attr)
2956 nla_strlcpy(ifname, ifname_attr, IFNAMSIZ);
Jiri Pirko76c9ac02019-09-30 11:48:20 +02002957 else if (altifname_attr)
2958 nla_strlcpy(ifname, altifname_attr, ALTIFNAMSIZ);
Jiri Pirkocc6090e2019-09-30 11:48:19 +02002959 else
2960 return NULL;
2961 }
2962
2963 return __dev_get_by_name(net, ifname);
2964}
2965
David Ahernc21ef3e2017-04-16 09:48:24 -07002966static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2967 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002969 struct net *net = sock_net(skb->sk);
Thomas Grafda5e0492006-08-10 21:17:37 -07002970 struct ifinfomsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 struct net_device *dev;
Patrick McHardy0157f602007-06-13 12:03:36 -07002972 int err;
Thomas Grafda5e0492006-08-10 21:17:37 -07002973 struct nlattr *tb[IFLA_MAX+1];
2974 char ifname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
Johannes Berg8cb08172019-04-26 14:07:28 +02002976 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
2977 ifla_policy, extack);
Thomas Grafda5e0492006-08-10 21:17:37 -07002978 if (err < 0)
2979 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002981 err = rtnl_ensure_unique_netns(tb, extack, false);
2982 if (err < 0)
2983 goto errout;
2984
Thomas Graf5176f912006-08-26 20:13:18 -07002985 if (tb[IFLA_IFNAME])
2986 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
Patrick McHardy78e5b8912006-09-13 20:35:36 -07002987 else
2988 ifname[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 err = -EINVAL;
Thomas Grafda5e0492006-08-10 21:17:37 -07002991 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -07002992 if (ifm->ifi_index > 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00002993 dev = __dev_get_by_index(net, ifm->ifi_index);
Jiri Pirko76c9ac02019-09-30 11:48:20 +02002994 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
2995 dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname);
Thomas Grafda5e0492006-08-10 21:17:37 -07002996 else
2997 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
Thomas Grafda5e0492006-08-10 21:17:37 -07002999 if (dev == NULL) {
3000 err = -ENODEV;
3001 goto errout;
3002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07003004 err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
Thomas Grafda5e0492006-08-10 21:17:37 -07003005errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 return err;
3007}
3008
WANG Cong66400d52015-03-24 11:53:31 -07003009static int rtnl_group_dellink(const struct net *net, int group)
3010{
3011 struct net_device *dev, *aux;
3012 LIST_HEAD(list_kill);
3013 bool found = false;
3014
3015 if (!group)
3016 return -EPERM;
3017
3018 for_each_netdev(net, dev) {
3019 if (dev->group == group) {
3020 const struct rtnl_link_ops *ops;
3021
3022 found = true;
3023 ops = dev->rtnl_link_ops;
3024 if (!ops || !ops->dellink)
3025 return -EOPNOTSUPP;
3026 }
3027 }
3028
3029 if (!found)
3030 return -ENODEV;
3031
3032 for_each_netdev_safe(net, dev, aux) {
3033 if (dev->group == group) {
3034 const struct rtnl_link_ops *ops;
3035
3036 ops = dev->rtnl_link_ops;
3037 ops->dellink(dev, &list_kill);
3038 }
3039 }
3040 unregister_netdevice_many(&list_kill);
3041
3042 return 0;
3043}
3044
Thomas Graf614732e2015-07-21 10:44:06 +02003045int rtnl_delete_link(struct net_device *dev)
3046{
3047 const struct rtnl_link_ops *ops;
3048 LIST_HEAD(list_kill);
3049
3050 ops = dev->rtnl_link_ops;
3051 if (!ops || !ops->dellink)
3052 return -EOPNOTSUPP;
3053
3054 ops->dellink(dev, &list_kill);
3055 unregister_netdevice_many(&list_kill);
3056
3057 return 0;
3058}
3059EXPORT_SYMBOL_GPL(rtnl_delete_link);
3060
David Ahernc21ef3e2017-04-16 09:48:24 -07003061static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
3062 struct netlink_ext_ack *extack)
Patrick McHardy38f7b872007-06-13 12:03:51 -07003063{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003064 struct net *net = sock_net(skb->sk);
Christian Braunerb61ad682018-01-24 15:26:34 +01003065 struct net *tgt_net = net;
3066 struct net_device *dev = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003067 struct ifinfomsg *ifm;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003068 struct nlattr *tb[IFLA_MAX+1];
3069 int err;
Christian Braunerb61ad682018-01-24 15:26:34 +01003070 int netnsid = -1;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003071
Johannes Berg8cb08172019-04-26 14:07:28 +02003072 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3073 ifla_policy, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07003074 if (err < 0)
3075 return err;
3076
Christian Brauner4ff66ca2018-02-07 13:53:20 +01003077 err = rtnl_ensure_unique_netns(tb, extack, true);
3078 if (err < 0)
3079 return err;
3080
Christian Brauner7e4a8d52018-09-04 21:53:53 +02003081 if (tb[IFLA_TARGET_NETNSID]) {
3082 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
Christian Braunerc383edc2018-09-04 21:53:47 +02003083 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
Christian Braunerb61ad682018-01-24 15:26:34 +01003084 if (IS_ERR(tgt_net))
3085 return PTR_ERR(tgt_net);
3086 }
3087
3088 err = -EINVAL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003089 ifm = nlmsg_data(nlh);
3090 if (ifm->ifi_index > 0)
Christian Braunerb61ad682018-01-24 15:26:34 +01003091 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
Jiri Pirko76c9ac02019-09-30 11:48:20 +02003092 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3093 dev = rtnl_dev_get(net, tb[IFLA_IFNAME],
3094 tb[IFLA_ALT_IFNAME], NULL);
WANG Cong66400d52015-03-24 11:53:31 -07003095 else if (tb[IFLA_GROUP])
Christian Braunerb61ad682018-01-24 15:26:34 +01003096 err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP]));
Patrick McHardy38f7b872007-06-13 12:03:51 -07003097 else
Christian Braunerb61ad682018-01-24 15:26:34 +01003098 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003099
Christian Braunerb61ad682018-01-24 15:26:34 +01003100 if (!dev) {
3101 if (tb[IFLA_IFNAME] || ifm->ifi_index > 0)
3102 err = -ENODEV;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003103
Christian Braunerb61ad682018-01-24 15:26:34 +01003104 goto out;
3105 }
3106
3107 err = rtnl_delete_link(dev);
3108
3109out:
3110 if (netnsid >= 0)
3111 put_net(tgt_net);
3112
3113 return err;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003114}
3115
Patrick McHardy3729d502010-02-26 06:34:54 +00003116int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
3117{
3118 unsigned int old_flags;
3119 int err;
3120
3121 old_flags = dev->flags;
3122 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
Petr Machata6d040322018-12-06 17:05:43 +00003123 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
3124 NULL);
Patrick McHardy3729d502010-02-26 06:34:54 +00003125 if (err < 0)
3126 return err;
3127 }
3128
Roopa Prabhu8d356b82018-07-04 16:46:29 -07003129 if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) {
Roopa Prabhu56a49d72018-09-12 13:21:48 -07003130 __dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags));
Roopa Prabhu8d356b82018-07-04 16:46:29 -07003131 } else {
3132 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
3133 __dev_notify_flags(dev, old_flags, ~0U);
3134 }
Patrick McHardy3729d502010-02-26 06:34:54 +00003135 return 0;
3136}
3137EXPORT_SYMBOL(rtnl_configure_link);
3138
David Ahernd0522f12018-11-06 12:51:14 -08003139struct net_device *rtnl_create_link(struct net *net, const char *ifname,
3140 unsigned char name_assign_type,
3141 const struct rtnl_link_ops *ops,
3142 struct nlattr *tb[],
3143 struct netlink_ext_ack *extack)
Pavel Emelianove7199282007-08-08 22:16:38 -07003144{
Pavel Emelianove7199282007-08-08 22:16:38 -07003145 struct net_device *dev;
Jiri Pirkod40156a2012-07-20 02:28:47 +00003146 unsigned int num_tx_queues = 1;
3147 unsigned int num_rx_queues = 1;
Pavel Emelianove7199282007-08-08 22:16:38 -07003148
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00003149 if (tb[IFLA_NUM_TX_QUEUES])
3150 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
3151 else if (ops->get_num_tx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00003152 num_tx_queues = ops->get_num_tx_queues();
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00003153
3154 if (tb[IFLA_NUM_RX_QUEUES])
3155 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
3156 else if (ops->get_num_rx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00003157 num_rx_queues = ops->get_num_rx_queues();
stephen hemmingerefacb302012-04-10 18:34:43 +00003158
David Ahernd0522f12018-11-06 12:51:14 -08003159 if (num_tx_queues < 1 || num_tx_queues > 4096) {
3160 NL_SET_ERR_MSG(extack, "Invalid number of transmit queues");
Eric Dumazet0e1d6ec2018-10-02 15:47:35 -07003161 return ERR_PTR(-EINVAL);
David Ahernd0522f12018-11-06 12:51:14 -08003162 }
Eric Dumazet0e1d6ec2018-10-02 15:47:35 -07003163
David Ahernd0522f12018-11-06 12:51:14 -08003164 if (num_rx_queues < 1 || num_rx_queues > 4096) {
3165 NL_SET_ERR_MSG(extack, "Invalid number of receive queues");
Eric Dumazet0e1d6ec2018-10-02 15:47:35 -07003166 return ERR_PTR(-EINVAL);
David Ahernd0522f12018-11-06 12:51:14 -08003167 }
Eric Dumazet0e1d6ec2018-10-02 15:47:35 -07003168
Tom Gundersen55177502014-07-14 16:37:25 +02003169 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
Tom Gundersenc835a672014-07-14 16:37:24 +02003170 ops->setup, num_tx_queues, num_rx_queues);
Pavel Emelianove7199282007-08-08 22:16:38 -07003171 if (!dev)
Tobias Klauserd1892e42017-02-20 16:32:06 +01003172 return ERR_PTR(-ENOMEM);
Pavel Emelianove7199282007-08-08 22:16:38 -07003173
Eric W. Biederman81adee42009-11-08 00:53:51 -08003174 dev_net_set(dev, net);
3175 dev->rtnl_link_ops = ops;
Patrick McHardy3729d502010-02-26 06:34:54 +00003176 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
Eric W. Biederman81adee42009-11-08 00:53:51 -08003177
Eric Dumazetd836f5c2020-01-21 22:47:29 -08003178 if (tb[IFLA_MTU]) {
3179 u32 mtu = nla_get_u32(tb[IFLA_MTU]);
3180 int err;
3181
3182 err = dev_validate_mtu(dev, mtu, extack);
3183 if (err) {
3184 free_netdev(dev);
3185 return ERR_PTR(err);
3186 }
3187 dev->mtu = mtu;
3188 }
Jiri Pirko2afb9b52013-01-06 12:41:57 +00003189 if (tb[IFLA_ADDRESS]) {
Pavel Emelianove7199282007-08-08 22:16:38 -07003190 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
3191 nla_len(tb[IFLA_ADDRESS]));
Jiri Pirko2afb9b52013-01-06 12:41:57 +00003192 dev->addr_assign_type = NET_ADDR_SET;
3193 }
Pavel Emelianove7199282007-08-08 22:16:38 -07003194 if (tb[IFLA_BROADCAST])
3195 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
3196 nla_len(tb[IFLA_BROADCAST]));
3197 if (tb[IFLA_TXQLEN])
3198 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
3199 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08003200 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Pavel Emelianove7199282007-08-08 22:16:38 -07003201 if (tb[IFLA_LINKMODE])
3202 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
Patrick McHardyffa934f2011-01-20 03:00:42 +00003203 if (tb[IFLA_GROUP])
3204 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Stephen Hemminger46e6b992017-12-07 15:40:19 -08003205 if (tb[IFLA_GSO_MAX_SIZE])
3206 netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE]));
3207 if (tb[IFLA_GSO_MAX_SEGS])
Stephen Hemmingera0b586f2017-12-08 15:34:13 -08003208 dev->gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
Pavel Emelianove7199282007-08-08 22:16:38 -07003209
3210 return dev;
Pavel Emelianove7199282007-08-08 22:16:38 -07003211}
Eric Dumazete0d087a2009-11-07 01:26:17 -08003212EXPORT_SYMBOL(rtnl_create_link);
Pavel Emelianove7199282007-08-08 22:16:38 -07003213
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07003214static int rtnl_group_changelink(const struct sk_buff *skb,
3215 struct net *net, int group,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00003216 struct ifinfomsg *ifm,
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07003217 struct netlink_ext_ack *extack,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00003218 struct nlattr **tb)
3219{
WANG Congd0795352015-03-23 16:31:09 -07003220 struct net_device *dev, *aux;
Vlad Dogarue7ed8282011-01-13 23:38:31 +00003221 int err;
3222
WANG Congd0795352015-03-23 16:31:09 -07003223 for_each_netdev_safe(net, dev, aux) {
Vlad Dogarue7ed8282011-01-13 23:38:31 +00003224 if (dev->group == group) {
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07003225 err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00003226 if (err < 0)
3227 return err;
3228 }
3229 }
3230
3231 return 0;
3232}
3233
Jakub Kicinskia2939742018-11-27 22:32:31 -08003234static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3235 struct nlattr **attr, struct netlink_ext_ack *extack)
Patrick McHardy38f7b872007-06-13 12:03:51 -07003236{
Jakub Kicinski420d0312018-11-27 22:32:30 -08003237 struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1];
3238 unsigned char name_assign_type = NET_NAME_USER;
3239 struct nlattr *linkinfo[IFLA_INFO_MAX + 1];
3240 const struct rtnl_link_ops *m_ops = NULL;
Jakub Kicinski420d0312018-11-27 22:32:30 -08003241 struct net_device *master_dev = NULL;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003242 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07003243 const struct rtnl_link_ops *ops;
Jakub Kicinski420d0312018-11-27 22:32:30 -08003244 struct nlattr *tb[IFLA_MAX + 1];
3245 struct net *dest_net, *link_net;
3246 struct nlattr **slave_data;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003247 char kind[MODULE_NAME_LEN];
Jakub Kicinski420d0312018-11-27 22:32:30 -08003248 struct net_device *dev;
3249 struct ifinfomsg *ifm;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003250 char ifname[IFNAMSIZ];
Jakub Kicinski420d0312018-11-27 22:32:30 -08003251 struct nlattr **data;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003252 int err;
3253
Johannes Berg95a5afc2008-10-16 15:24:51 -07003254#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07003255replay:
Thomas Graf8072f082007-07-31 14:13:50 -07003256#endif
Johannes Berg8cb08172019-04-26 14:07:28 +02003257 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3258 ifla_policy, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07003259 if (err < 0)
3260 return err;
3261
Christian Brauner4ff66ca2018-02-07 13:53:20 +01003262 err = rtnl_ensure_unique_netns(tb, extack, false);
3263 if (err < 0)
3264 return err;
3265
Patrick McHardy38f7b872007-06-13 12:03:51 -07003266 if (tb[IFLA_IFNAME])
3267 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
3268 else
3269 ifname[0] = '\0';
3270
3271 ifm = nlmsg_data(nlh);
3272 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07003273 dev = __dev_get_by_index(net, ifm->ifi_index);
Jiri Pirko76c9ac02019-09-30 11:48:20 +02003274 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3275 dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname);
Jiri Pirko7af12cb2019-09-30 11:48:18 +02003276 else
3277 dev = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003278
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01003279 if (dev) {
3280 master_dev = netdev_master_upper_dev_get(dev);
3281 if (master_dev)
3282 m_ops = master_dev->rtnl_link_ops;
3283 }
3284
Eric Dumazete0d087a2009-11-07 01:26:17 -08003285 err = validate_linkmsg(dev, tb);
3286 if (err < 0)
Thomas Graf1840bb12008-02-23 19:54:36 -08003287 return err;
3288
Patrick McHardy38f7b872007-06-13 12:03:51 -07003289 if (tb[IFLA_LINKINFO]) {
Johannes Berg8cb08172019-04-26 14:07:28 +02003290 err = nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX,
3291 tb[IFLA_LINKINFO],
3292 ifla_info_policy, NULL);
Patrick McHardy38f7b872007-06-13 12:03:51 -07003293 if (err < 0)
3294 return err;
3295 } else
3296 memset(linkinfo, 0, sizeof(linkinfo));
3297
3298 if (linkinfo[IFLA_INFO_KIND]) {
3299 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
3300 ops = rtnl_link_ops_get(kind);
3301 } else {
3302 kind[0] = '\0';
3303 ops = NULL;
3304 }
3305
Jakub Kicinski420d0312018-11-27 22:32:30 -08003306 data = NULL;
3307 if (ops) {
3308 if (ops->maxtype > RTNL_MAX_TYPE)
3309 return -EINVAL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003310
Jakub Kicinski420d0312018-11-27 22:32:30 -08003311 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
Johannes Berg8cb08172019-04-26 14:07:28 +02003312 err = nla_parse_nested_deprecated(attr, ops->maxtype,
3313 linkinfo[IFLA_INFO_DATA],
3314 ops->policy, extack);
Jakub Kicinski420d0312018-11-27 22:32:30 -08003315 if (err < 0)
3316 return err;
3317 data = attr;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003318 }
Jakub Kicinski420d0312018-11-27 22:32:30 -08003319 if (ops->validate) {
3320 err = ops->validate(tb, data, extack);
3321 if (err < 0)
3322 return err;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01003323 }
Jakub Kicinski420d0312018-11-27 22:32:30 -08003324 }
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01003325
Jakub Kicinski420d0312018-11-27 22:32:30 -08003326 slave_data = NULL;
3327 if (m_ops) {
3328 if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)
3329 return -EINVAL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003330
Jakub Kicinski420d0312018-11-27 22:32:30 -08003331 if (m_ops->slave_maxtype &&
3332 linkinfo[IFLA_INFO_SLAVE_DATA]) {
Johannes Berg8cb08172019-04-26 14:07:28 +02003333 err = nla_parse_nested_deprecated(slave_attr,
3334 m_ops->slave_maxtype,
3335 linkinfo[IFLA_INFO_SLAVE_DATA],
3336 m_ops->slave_policy,
3337 extack);
Jakub Kicinski420d0312018-11-27 22:32:30 -08003338 if (err < 0)
3339 return err;
3340 slave_data = slave_attr;
3341 }
3342 }
3343
3344 if (dev) {
3345 int status = 0;
3346
3347 if (nlh->nlmsg_flags & NLM_F_EXCL)
3348 return -EEXIST;
3349 if (nlh->nlmsg_flags & NLM_F_REPLACE)
3350 return -EOPNOTSUPP;
3351
3352 if (linkinfo[IFLA_INFO_DATA]) {
3353 if (!ops || ops != dev->rtnl_link_ops ||
3354 !ops->changelink)
Patrick McHardy38f7b872007-06-13 12:03:51 -07003355 return -EOPNOTSUPP;
3356
Jakub Kicinski420d0312018-11-27 22:32:30 -08003357 err = ops->changelink(dev, tb, data, extack);
3358 if (err < 0)
3359 return err;
3360 status |= DO_SETLINK_NOTIFY;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003361 }
3362
Jakub Kicinski420d0312018-11-27 22:32:30 -08003363 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
3364 if (!m_ops || !m_ops->slave_changelink)
3365 return -EOPNOTSUPP;
3366
3367 err = m_ops->slave_changelink(master_dev, dev, tb,
3368 slave_data, extack);
3369 if (err < 0)
3370 return err;
3371 status |= DO_SETLINK_NOTIFY;
3372 }
3373
3374 return do_setlink(skb, dev, ifm, extack, tb, ifname, status);
3375 }
3376
3377 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
3378 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
3379 return rtnl_group_changelink(skb, net,
Patrick McHardyffa934f2011-01-20 03:00:42 +00003380 nla_get_u32(tb[IFLA_GROUP]),
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07003381 ifm, extack, tb);
Jakub Kicinski420d0312018-11-27 22:32:30 -08003382 return -ENODEV;
3383 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07003384
Jakub Kicinski420d0312018-11-27 22:32:30 -08003385 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
3386 return -EOPNOTSUPP;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003387
Jakub Kicinski420d0312018-11-27 22:32:30 -08003388 if (!ops) {
Johannes Berg95a5afc2008-10-16 15:24:51 -07003389#ifdef CONFIG_MODULES
Jakub Kicinski420d0312018-11-27 22:32:30 -08003390 if (kind[0]) {
3391 __rtnl_unlock();
3392 request_module("rtnl-link-%s", kind);
3393 rtnl_lock();
3394 ops = rtnl_link_ops_get(kind);
3395 if (ops)
3396 goto replay;
3397 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07003398#endif
Jakub Kicinski420d0312018-11-27 22:32:30 -08003399 NL_SET_ERR_MSG(extack, "Unknown device type");
3400 return -EOPNOTSUPP;
3401 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07003402
Jakub Kicinski420d0312018-11-27 22:32:30 -08003403 if (!ops->setup)
3404 return -EOPNOTSUPP;
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02003405
Jakub Kicinski420d0312018-11-27 22:32:30 -08003406 if (!ifname[0]) {
3407 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
3408 name_assign_type = NET_NAME_ENUM;
3409 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07003410
Jakub Kicinski420d0312018-11-27 22:32:30 -08003411 dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN);
3412 if (IS_ERR(dest_net))
3413 return PTR_ERR(dest_net);
Eric W. Biederman13ad1772011-01-29 14:57:22 +00003414
Jakub Kicinski420d0312018-11-27 22:32:30 -08003415 if (tb[IFLA_LINK_NETNSID]) {
3416 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
Nicolas Dichtel317f4812015-01-15 15:11:18 +01003417
Jakub Kicinski420d0312018-11-27 22:32:30 -08003418 link_net = get_net_ns_by_id(dest_net, id);
3419 if (!link_net) {
3420 NL_SET_ERR_MSG(extack, "Unknown network namespace id");
3421 err = -EINVAL;
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00003422 goto out;
3423 }
Jakub Kicinski420d0312018-11-27 22:32:30 -08003424 err = -EPERM;
3425 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
3426 goto out;
3427 } else {
3428 link_net = NULL;
3429 }
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00003430
Jakub Kicinski420d0312018-11-27 22:32:30 -08003431 dev = rtnl_create_link(link_net ? : dest_net, ifname,
3432 name_assign_type, ops, tb, extack);
3433 if (IS_ERR(dev)) {
3434 err = PTR_ERR(dev);
David S. Miller43638902015-03-10 21:58:32 -04003435 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003436 }
Jakub Kicinski420d0312018-11-27 22:32:30 -08003437
3438 dev->ifindex = ifm->ifi_index;
3439
3440 if (ops->newlink) {
3441 err = ops->newlink(link_net ? : net, dev, tb, data, extack);
3442 /* Drivers should call free_netdev() in ->destructor
3443 * and unregister it on failure after registration
3444 * so that device could be finally freed in rtnl_unlock.
3445 */
3446 if (err < 0) {
3447 /* If device is not registered at all, free it now */
Weilong Chencebb6972020-07-15 20:58:10 +08003448 if (dev->reg_state == NETREG_UNINITIALIZED ||
3449 dev->reg_state == NETREG_UNREGISTERED)
Jakub Kicinski420d0312018-11-27 22:32:30 -08003450 free_netdev(dev);
3451 goto out;
3452 }
3453 } else {
3454 err = register_netdevice(dev);
3455 if (err < 0) {
3456 free_netdev(dev);
3457 goto out;
3458 }
3459 }
3460 err = rtnl_configure_link(dev, ifm);
3461 if (err < 0)
3462 goto out_unregister;
3463 if (link_net) {
3464 err = dev_change_net_namespace(dev, dest_net, ifname);
3465 if (err < 0)
3466 goto out_unregister;
3467 }
3468 if (tb[IFLA_MASTER]) {
3469 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
3470 if (err)
3471 goto out_unregister;
3472 }
3473out:
3474 if (link_net)
3475 put_net(link_net);
3476 put_net(dest_net);
3477 return err;
3478out_unregister:
3479 if (ops->newlink) {
3480 LIST_HEAD(list_kill);
3481
3482 ops->dellink(dev, &list_kill);
3483 unregister_netdevice_many(&list_kill);
3484 } else {
3485 unregister_netdevice(dev);
3486 }
3487 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003488}
3489
Jakub Kicinskia2939742018-11-27 22:32:31 -08003490static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3491 struct netlink_ext_ack *extack)
3492{
3493 struct nlattr **attr;
3494 int ret;
3495
3496 attr = kmalloc_array(RTNL_MAX_TYPE + 1, sizeof(*attr), GFP_KERNEL);
3497 if (!attr)
3498 return -ENOMEM;
3499
3500 ret = __rtnl_newlink(skb, nlh, attr, extack);
3501 kfree(attr);
3502 return ret;
3503}
3504
Jakub Kicinski9b3757b2019-01-18 10:46:16 -08003505static int rtnl_valid_getlink_req(struct sk_buff *skb,
3506 const struct nlmsghdr *nlh,
3507 struct nlattr **tb,
3508 struct netlink_ext_ack *extack)
3509{
3510 struct ifinfomsg *ifm;
3511 int i, err;
3512
3513 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
3514 NL_SET_ERR_MSG(extack, "Invalid header for get link");
3515 return -EINVAL;
3516 }
3517
3518 if (!netlink_strict_get_check(skb))
Johannes Berg8cb08172019-04-26 14:07:28 +02003519 return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3520 ifla_policy, extack);
Jakub Kicinski9b3757b2019-01-18 10:46:16 -08003521
3522 ifm = nlmsg_data(nlh);
3523 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
3524 ifm->ifi_change) {
3525 NL_SET_ERR_MSG(extack, "Invalid values in header for get link request");
3526 return -EINVAL;
3527 }
3528
Johannes Berg8cb08172019-04-26 14:07:28 +02003529 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFLA_MAX,
3530 ifla_policy, extack);
Jakub Kicinski9b3757b2019-01-18 10:46:16 -08003531 if (err)
3532 return err;
3533
3534 for (i = 0; i <= IFLA_MAX; i++) {
3535 if (!tb[i])
3536 continue;
3537
3538 switch (i) {
3539 case IFLA_IFNAME:
Jiri Pirko76c9ac02019-09-30 11:48:20 +02003540 case IFLA_ALT_IFNAME:
Jakub Kicinski9b3757b2019-01-18 10:46:16 -08003541 case IFLA_EXT_MASK:
3542 case IFLA_TARGET_NETNSID:
3543 break;
3544 default:
3545 NL_SET_ERR_MSG(extack, "Unsupported attribute in get link request");
3546 return -EINVAL;
3547 }
3548 }
3549
3550 return 0;
3551}
3552
David Ahernc21ef3e2017-04-16 09:48:24 -07003553static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3554 struct netlink_ext_ack *extack)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003555{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003556 struct net *net = sock_net(skb->sk);
Jiri Benc79e1ad12017-11-02 17:04:38 -02003557 struct net *tgt_net = net;
Thomas Grafb60c5112006-08-04 23:05:34 -07003558 struct ifinfomsg *ifm;
3559 struct nlattr *tb[IFLA_MAX+1];
3560 struct net_device *dev = NULL;
3561 struct sk_buff *nskb;
Jiri Benc79e1ad12017-11-02 17:04:38 -02003562 int netnsid = -1;
Thomas Graf339bf982006-11-10 14:10:15 -08003563 int err;
Greg Rose115c9b82012-02-21 16:54:48 -05003564 u32 ext_filter_mask = 0;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003565
Jakub Kicinski9b3757b2019-01-18 10:46:16 -08003566 err = rtnl_valid_getlink_req(skb, nlh, tb, extack);
Thomas Grafb60c5112006-08-04 23:05:34 -07003567 if (err < 0)
Eric Sesterhenn9918f232006-09-26 23:26:38 -07003568 return err;
Thomas Grafb60c5112006-08-04 23:05:34 -07003569
Christian Brauner4ff66ca2018-02-07 13:53:20 +01003570 err = rtnl_ensure_unique_netns(tb, extack, true);
3571 if (err < 0)
3572 return err;
3573
Christian Brauner7e4a8d52018-09-04 21:53:53 +02003574 if (tb[IFLA_TARGET_NETNSID]) {
3575 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
Christian Braunerc383edc2018-09-04 21:53:47 +02003576 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
Jiri Benc79e1ad12017-11-02 17:04:38 -02003577 if (IS_ERR(tgt_net))
3578 return PTR_ERR(tgt_net);
3579 }
3580
Greg Rose115c9b82012-02-21 16:54:48 -05003581 if (tb[IFLA_EXT_MASK])
3582 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3583
Jiri Benc79e1ad12017-11-02 17:04:38 -02003584 err = -EINVAL;
Thomas Grafb60c5112006-08-04 23:05:34 -07003585 ifm = nlmsg_data(nlh);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003586 if (ifm->ifi_index > 0)
Jiri Benc79e1ad12017-11-02 17:04:38 -02003587 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
Jiri Pirko76c9ac02019-09-30 11:48:20 +02003588 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3589 dev = rtnl_dev_get(tgt_net, tb[IFLA_IFNAME],
3590 tb[IFLA_ALT_IFNAME], NULL);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003591 else
Jiri Benc79e1ad12017-11-02 17:04:38 -02003592 goto out;
Thomas Grafb60c5112006-08-04 23:05:34 -07003593
Jiri Benc79e1ad12017-11-02 17:04:38 -02003594 err = -ENODEV;
Eric Dumazeta3d12892009-10-21 10:59:31 +00003595 if (dev == NULL)
Jiri Benc79e1ad12017-11-02 17:04:38 -02003596 goto out;
Eric Dumazeta3d12892009-10-21 10:59:31 +00003597
Jiri Benc79e1ad12017-11-02 17:04:38 -02003598 err = -ENOBUFS;
Greg Rose115c9b82012-02-21 16:54:48 -05003599 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003600 if (nskb == NULL)
Jiri Benc79e1ad12017-11-02 17:04:38 -02003601 goto out;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003602
Jiri Benc79e1ad12017-11-02 17:04:38 -02003603 err = rtnl_fill_ifinfo(nskb, dev, net,
3604 RTM_NEWLINK, NETLINK_CB(skb).portid,
3605 nlh->nlmsg_seq, 0, 0, ext_filter_mask,
Guillaume Naultd4e4fdf2019-10-23 18:39:04 +02003606 0, NULL, 0, netnsid, GFP_KERNEL);
Patrick McHardy26932562007-01-31 23:16:40 -08003607 if (err < 0) {
3608 /* -EMSGSIZE implies BUG in if_nlmsg_size */
3609 WARN_ON(err == -EMSGSIZE);
3610 kfree_skb(nskb);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003611 } else
Eric W. Biederman15e47302012-09-07 20:12:54 +00003612 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
Jiri Benc79e1ad12017-11-02 17:04:38 -02003613out:
3614 if (netnsid >= 0)
3615 put_net(tgt_net);
Thomas Grafb60c5112006-08-04 23:05:34 -07003616
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003617 return err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003618}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003619
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02003620static int rtnl_alt_ifname(int cmd, struct net_device *dev, struct nlattr *attr,
3621 bool *changed, struct netlink_ext_ack *extack)
3622{
3623 char *alt_ifname;
3624 int err;
3625
3626 err = nla_validate(attr, attr->nla_len, IFLA_MAX, ifla_policy, extack);
3627 if (err)
3628 return err;
3629
Eric Dumazet44bfa9c2020-02-12 20:58:26 -08003630 alt_ifname = nla_strdup(attr, GFP_KERNEL);
3631 if (!alt_ifname)
3632 return -ENOMEM;
3633
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02003634 if (cmd == RTM_NEWLINKPROP) {
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02003635 err = netdev_name_node_alt_create(dev, alt_ifname);
Eric Dumazet44bfa9c2020-02-12 20:58:26 -08003636 if (!err)
3637 alt_ifname = NULL;
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02003638 } else if (cmd == RTM_DELLINKPROP) {
3639 err = netdev_name_node_alt_destroy(dev, alt_ifname);
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02003640 } else {
Eric Dumazet44bfa9c2020-02-12 20:58:26 -08003641 WARN_ON_ONCE(1);
3642 err = -EINVAL;
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02003643 }
3644
Eric Dumazet44bfa9c2020-02-12 20:58:26 -08003645 kfree(alt_ifname);
3646 if (!err)
3647 *changed = true;
3648 return err;
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02003649}
3650
3651static int rtnl_linkprop(int cmd, struct sk_buff *skb, struct nlmsghdr *nlh,
3652 struct netlink_ext_ack *extack)
3653{
3654 struct net *net = sock_net(skb->sk);
3655 struct nlattr *tb[IFLA_MAX + 1];
3656 struct net_device *dev;
3657 struct ifinfomsg *ifm;
3658 bool changed = false;
3659 struct nlattr *attr;
3660 int err, rem;
3661
3662 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
3663 if (err)
3664 return err;
3665
3666 err = rtnl_ensure_unique_netns(tb, extack, true);
3667 if (err)
3668 return err;
3669
3670 ifm = nlmsg_data(nlh);
Jiri Pirkocc6090e2019-09-30 11:48:19 +02003671 if (ifm->ifi_index > 0)
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02003672 dev = __dev_get_by_index(net, ifm->ifi_index);
Jiri Pirko76c9ac02019-09-30 11:48:20 +02003673 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3674 dev = rtnl_dev_get(net, tb[IFLA_IFNAME],
3675 tb[IFLA_ALT_IFNAME], NULL);
Jiri Pirkocc6090e2019-09-30 11:48:19 +02003676 else
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02003677 return -EINVAL;
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02003678
3679 if (!dev)
3680 return -ENODEV;
3681
3682 if (!tb[IFLA_PROP_LIST])
3683 return 0;
3684
3685 nla_for_each_nested(attr, tb[IFLA_PROP_LIST], rem) {
3686 switch (nla_type(attr)) {
3687 case IFLA_ALT_IFNAME:
3688 err = rtnl_alt_ifname(cmd, dev, attr, &changed, extack);
3689 if (err)
3690 return err;
3691 break;
3692 }
3693 }
3694
3695 if (changed)
3696 netdev_state_change(dev);
3697 return 0;
3698}
3699
3700static int rtnl_newlinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
3701 struct netlink_ext_ack *extack)
3702{
3703 return rtnl_linkprop(RTM_NEWLINKPROP, skb, nlh, extack);
3704}
3705
3706static int rtnl_dellinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
3707 struct netlink_ext_ack *extack)
3708{
3709 return rtnl_linkprop(RTM_DELLINKPROP, skb, nlh, extack);
3710}
3711
Greg Rose115c9b82012-02-21 16:54:48 -05003712static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
Greg Rosec7ac8672011-06-10 01:27:09 +00003713{
Greg Rose115c9b82012-02-21 16:54:48 -05003714 struct net *net = sock_net(skb->sk);
3715 struct net_device *dev;
3716 struct nlattr *tb[IFLA_MAX+1];
3717 u32 ext_filter_mask = 0;
3718 u16 min_ifinfo_dump_size = 0;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02003719 int hdrlen;
Greg Rose115c9b82012-02-21 16:54:48 -05003720
Michal Schmidte5eca6d2014-05-28 14:15:19 +02003721 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3722 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
3723 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
3724
Johannes Berg8cb08172019-04-26 14:07:28 +02003725 if (nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00003726 if (tb[IFLA_EXT_MASK])
3727 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3728 }
Greg Rose115c9b82012-02-21 16:54:48 -05003729
3730 if (!ext_filter_mask)
3731 return NLMSG_GOODSIZE;
3732 /*
3733 * traverse the list of net devices and compute the minimum
3734 * buffer size based upon the filter mask.
3735 */
Florian Westphal6853dd42017-08-09 20:41:51 +02003736 rcu_read_lock();
3737 for_each_netdev_rcu(net, dev) {
Greg Rose115c9b82012-02-21 16:54:48 -05003738 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
3739 if_nlmsg_size(dev,
3740 ext_filter_mask));
3741 }
Florian Westphal6853dd42017-08-09 20:41:51 +02003742 rcu_read_unlock();
Greg Rose115c9b82012-02-21 16:54:48 -05003743
Zhang Shengju93af2052016-11-22 14:14:28 +08003744 return nlmsg_total_size(min_ifinfo_dump_size);
Greg Rosec7ac8672011-06-10 01:27:09 +00003745}
3746
Adrian Bunk42bad1d2007-04-26 00:57:41 -07003747static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748{
3749 int idx;
3750 int s_idx = cb->family;
Christian Brauner87ccbb12018-09-04 21:53:51 +02003751 int type = cb->nlh->nlmsg_type - RTM_BASE;
David Ahernc63586d2018-10-24 12:59:02 -07003752 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753
3754 if (s_idx == 0)
3755 s_idx = 1;
Florian Westphal6853dd42017-08-09 20:41:51 +02003756
Patrick McHardy25239ce2010-04-26 16:02:05 +02003757 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
Florian Westphaladdf9b92017-12-02 21:44:05 +01003758 struct rtnl_link **tab;
Florian Westphaladdf9b92017-12-02 21:44:05 +01003759 struct rtnl_link *link;
Florian Westphal6853dd42017-08-09 20:41:51 +02003760 rtnl_dumpit_func dumpit;
3761
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 if (idx < s_idx || idx == PF_PACKET)
3763 continue;
Florian Westphal6853dd42017-08-09 20:41:51 +02003764
Florian Westphaladdf9b92017-12-02 21:44:05 +01003765 if (type < 0 || type >= RTM_NR_MSGTYPES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 continue;
Florian Westphal6853dd42017-08-09 20:41:51 +02003767
Florian Westphaladdf9b92017-12-02 21:44:05 +01003768 tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]);
3769 if (!tab)
3770 continue;
3771
3772 link = tab[type];
3773 if (!link)
3774 continue;
3775
3776 dumpit = link->dumpit;
Florian Westphal6853dd42017-08-09 20:41:51 +02003777 if (!dumpit)
3778 continue;
3779
Nicolas Dichtel04652772013-03-22 06:28:42 +00003780 if (idx > s_idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 memset(&cb->args[0], 0, sizeof(cb->args));
Nicolas Dichtel04652772013-03-22 06:28:42 +00003782 cb->prev_seq = 0;
3783 cb->seq = 0;
3784 }
David Ahernc63586d2018-10-24 12:59:02 -07003785 ret = dumpit(skb, cb);
Alexey Kodanev5e1acb42018-11-02 19:11:04 +03003786 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 break;
3788 }
3789 cb->family = idx;
3790
David Ahernc63586d2018-10-24 12:59:02 -07003791 return skb->len ? : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792}
3793
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003794struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003795 unsigned int change,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003796 u32 event, gfp_t flags, int *new_nsid,
3797 int new_ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003799 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 struct sk_buff *skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003801 int err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00003802 size_t if_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803
Alexei Starovoitov7f294052013-10-23 16:02:42 -07003804 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003805 if (skb == NULL)
3806 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807
Jiri Benc79e1ad12017-11-02 17:04:38 -02003808 err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
3809 type, 0, 0, change, 0, 0, event,
Guillaume Naultd4e4fdf2019-10-23 18:39:04 +02003810 new_nsid, new_ifindex, -1, flags);
Patrick McHardy26932562007-01-31 23:16:40 -08003811 if (err < 0) {
3812 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
3813 WARN_ON(err == -EMSGSIZE);
3814 kfree_skb(skb);
3815 goto errout;
3816 }
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003817 return skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003818errout:
3819 if (err < 0)
Eric W. Biederman4b3da702007-11-19 22:27:40 -08003820 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003821 return NULL;
3822}
3823
3824void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
3825{
3826 struct net *net = dev_net(dev);
3827
3828 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
3829}
3830
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003831static void rtmsg_ifinfo_event(int type, struct net_device *dev,
3832 unsigned int change, u32 event,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003833 gfp_t flags, int *new_nsid, int new_ifindex)
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003834{
3835 struct sk_buff *skb;
3836
Nicolas Dichteled2a80a2015-05-13 14:19:42 +02003837 if (dev->reg_state != NETREG_REGISTERED)
3838 return;
3839
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003840 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid,
3841 new_ifindex);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003842 if (skb)
3843 rtmsg_ifinfo_send(skb, dev, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844}
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003845
3846void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
3847 gfp_t flags)
3848{
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003849 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3850 NULL, 0);
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003851}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003853void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003854 gfp_t flags, int *new_nsid, int new_ifindex)
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003855{
3856 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003857 new_nsid, new_ifindex);
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003858}
3859
John Fastabendd83b0602012-04-15 06:44:08 +00003860static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
3861 struct net_device *dev,
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003862 u8 *addr, u16 vid, u32 pid, u32 seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003863 int type, unsigned int flags,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003864 int nlflags, u16 ndm_state)
John Fastabendd83b0602012-04-15 06:44:08 +00003865{
3866 struct nlmsghdr *nlh;
3867 struct ndmsg *ndm;
3868
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003869 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
John Fastabendd83b0602012-04-15 06:44:08 +00003870 if (!nlh)
3871 return -EMSGSIZE;
3872
3873 ndm = nlmsg_data(nlh);
3874 ndm->ndm_family = AF_BRIDGE;
3875 ndm->ndm_pad1 = 0;
3876 ndm->ndm_pad2 = 0;
3877 ndm->ndm_flags = flags;
3878 ndm->ndm_type = 0;
3879 ndm->ndm_ifindex = dev->ifindex;
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003880 ndm->ndm_state = ndm_state;
John Fastabendd83b0602012-04-15 06:44:08 +00003881
3882 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
3883 goto nla_put_failure;
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003884 if (vid)
3885 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
3886 goto nla_put_failure;
John Fastabendd83b0602012-04-15 06:44:08 +00003887
Johannes Berg053c0952015-01-16 22:09:00 +01003888 nlmsg_end(skb, nlh);
3889 return 0;
John Fastabendd83b0602012-04-15 06:44:08 +00003890
3891nla_put_failure:
3892 nlmsg_cancel(skb, nlh);
3893 return -EMSGSIZE;
3894}
3895
John Fastabend3ff661c2012-04-15 06:44:14 +00003896static inline size_t rtnl_fdb_nlmsg_size(void)
3897{
Sabrina Dubrocaf82ef3e2016-11-18 15:50:39 +01003898 return NLMSG_ALIGN(sizeof(struct ndmsg)) +
3899 nla_total_size(ETH_ALEN) + /* NDA_LLADDR */
3900 nla_total_size(sizeof(u16)) + /* NDA_VLAN */
3901 0;
John Fastabend3ff661c2012-04-15 06:44:14 +00003902}
3903
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003904static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
3905 u16 ndm_state)
John Fastabend3ff661c2012-04-15 06:44:14 +00003906{
3907 struct net *net = dev_net(dev);
3908 struct sk_buff *skb;
3909 int err = -ENOBUFS;
3910
3911 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
3912 if (!skb)
3913 goto errout;
3914
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003915 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003916 0, 0, type, NTF_SELF, 0, ndm_state);
John Fastabend3ff661c2012-04-15 06:44:14 +00003917 if (err < 0) {
3918 kfree_skb(skb);
3919 goto errout;
3920 }
3921
3922 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
3923 return;
3924errout:
3925 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
3926}
3927
Bart Van Asschea9869672019-03-25 09:17:22 -07003928/*
Vlad Yasevich090096b2013-03-06 15:39:42 +00003929 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3930 */
3931int ndo_dflt_fdb_add(struct ndmsg *ndm,
3932 struct nlattr *tb[],
3933 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003934 const unsigned char *addr, u16 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003935 u16 flags)
3936{
3937 int err = -EINVAL;
3938
3939 /* If aging addresses are supported device will need to
3940 * implement its own handler for this.
3941 */
3942 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
3943 pr_info("%s: FDB only supports static addresses\n", dev->name);
3944 return err;
3945 }
3946
Or Gerlitz65891fe2014-12-14 18:19:05 +02003947 if (vid) {
3948 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
3949 return err;
3950 }
3951
Vlad Yasevich090096b2013-03-06 15:39:42 +00003952 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3953 err = dev_uc_add_excl(dev, addr);
3954 else if (is_multicast_ether_addr(addr))
3955 err = dev_mc_add_excl(dev, addr);
3956
3957 /* Only return duplicate errors if NLM_F_EXCL is set */
3958 if (err == -EEXIST && !(flags & NLM_F_EXCL))
3959 err = 0;
3960
3961 return err;
3962}
3963EXPORT_SYMBOL(ndo_dflt_fdb_add);
3964
Florian Westphalb88d12e2017-10-10 17:10:04 +02003965static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
3966 struct netlink_ext_ack *extack)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003967{
3968 u16 vid = 0;
3969
3970 if (vlan_attr) {
3971 if (nla_len(vlan_attr) != sizeof(u16)) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003972 NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
Jiri Pirkof6f64242014-11-28 14:34:15 +01003973 return -EINVAL;
3974 }
3975
3976 vid = nla_get_u16(vlan_attr);
3977
3978 if (!vid || vid >= VLAN_VID_MASK) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003979 NL_SET_ERR_MSG(extack, "invalid vlan id");
Jiri Pirkof6f64242014-11-28 14:34:15 +01003980 return -EINVAL;
3981 }
3982 }
3983 *p_vid = vid;
3984 return 0;
3985}
3986
David Ahernc21ef3e2017-04-16 09:48:24 -07003987static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
3988 struct netlink_ext_ack *extack)
John Fastabend77162022012-04-15 06:43:56 +00003989{
3990 struct net *net = sock_net(skb->sk);
John Fastabend77162022012-04-15 06:43:56 +00003991 struct ndmsg *ndm;
3992 struct nlattr *tb[NDA_MAX+1];
3993 struct net_device *dev;
3994 u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01003995 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00003996 int err;
3997
Johannes Berg8cb08172019-04-26 14:07:28 +02003998 err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
3999 extack);
John Fastabend77162022012-04-15 06:43:56 +00004000 if (err < 0)
4001 return err;
4002
4003 ndm = nlmsg_data(nlh);
4004 if (ndm->ndm_ifindex == 0) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004005 NL_SET_ERR_MSG(extack, "invalid ifindex");
John Fastabend77162022012-04-15 06:43:56 +00004006 return -EINVAL;
4007 }
4008
4009 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
4010 if (dev == NULL) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004011 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabend77162022012-04-15 06:43:56 +00004012 return -ENODEV;
4013 }
4014
4015 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004016 NL_SET_ERR_MSG(extack, "invalid address");
John Fastabend77162022012-04-15 06:43:56 +00004017 return -EINVAL;
4018 }
4019
Ido Schimmelda715772018-10-29 20:36:43 +00004020 if (dev->type != ARPHRD_ETHER) {
4021 NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices");
4022 return -EINVAL;
4023 }
4024
John Fastabend77162022012-04-15 06:43:56 +00004025 addr = nla_data(tb[NDA_LLADDR]);
John Fastabend77162022012-04-15 06:43:56 +00004026
Florian Westphalb88d12e2017-10-10 17:10:04 +02004027 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
Jiri Pirkof6f64242014-11-28 14:34:15 +01004028 if (err)
4029 return err;
4030
John Fastabend77162022012-04-15 06:43:56 +00004031 err = -EOPNOTSUPP;
4032
4033 /* Support fdb on master device the net/bridge default case */
4034 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
Julian Wiedmann2e92a2d2020-02-20 09:00:07 +01004035 netif_is_bridge_port(dev)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00004036 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4037 const struct net_device_ops *ops = br_dev->netdev_ops;
4038
Jiri Pirkof6f64242014-11-28 14:34:15 +01004039 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
Petr Machata87b09842019-01-16 23:06:50 +00004040 nlh->nlmsg_flags, extack);
John Fastabend77162022012-04-15 06:43:56 +00004041 if (err)
4042 goto out;
4043 else
4044 ndm->ndm_flags &= ~NTF_MASTER;
4045 }
4046
4047 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00004048 if ((ndm->ndm_flags & NTF_SELF)) {
4049 if (dev->netdev_ops->ndo_fdb_add)
4050 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
Jiri Pirkof6f64242014-11-28 14:34:15 +01004051 vid,
Petr Machata87b09842019-01-16 23:06:50 +00004052 nlh->nlmsg_flags,
4053 extack);
Vlad Yasevich090096b2013-03-06 15:39:42 +00004054 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01004055 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00004056 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00004057
John Fastabend3ff661c2012-04-15 06:44:14 +00004058 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00004059 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
4060 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00004061 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00004062 }
John Fastabend77162022012-04-15 06:43:56 +00004063 }
4064out:
4065 return err;
4066}
4067
Bart Van Asschea9869672019-03-25 09:17:22 -07004068/*
Vlad Yasevich090096b2013-03-06 15:39:42 +00004069 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
4070 */
4071int ndo_dflt_fdb_del(struct ndmsg *ndm,
4072 struct nlattr *tb[],
4073 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01004074 const unsigned char *addr, u16 vid)
Vlad Yasevich090096b2013-03-06 15:39:42 +00004075{
Alexander Duyckc8a89c42014-07-15 15:15:20 -07004076 int err = -EINVAL;
Vlad Yasevich090096b2013-03-06 15:39:42 +00004077
4078 /* If aging addresses are supported device will need to
4079 * implement its own handler for this.
4080 */
Sridhar Samudrala64535992013-08-08 15:19:48 -07004081 if (!(ndm->ndm_state & NUD_PERMANENT)) {
Vlad Yasevich090096b2013-03-06 15:39:42 +00004082 pr_info("%s: FDB only supports static addresses\n", dev->name);
Alexander Duyckc8a89c42014-07-15 15:15:20 -07004083 return err;
Vlad Yasevich090096b2013-03-06 15:39:42 +00004084 }
4085
4086 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
4087 err = dev_uc_del(dev, addr);
4088 else if (is_multicast_ether_addr(addr))
4089 err = dev_mc_del(dev, addr);
Vlad Yasevich090096b2013-03-06 15:39:42 +00004090
4091 return err;
4092}
4093EXPORT_SYMBOL(ndo_dflt_fdb_del);
4094
David Ahernc21ef3e2017-04-16 09:48:24 -07004095static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
4096 struct netlink_ext_ack *extack)
John Fastabend77162022012-04-15 06:43:56 +00004097{
4098 struct net *net = sock_net(skb->sk);
4099 struct ndmsg *ndm;
Vlad Yasevich1690be62013-02-13 12:00:18 +00004100 struct nlattr *tb[NDA_MAX+1];
John Fastabend77162022012-04-15 06:43:56 +00004101 struct net_device *dev;
John Fastabend77162022012-04-15 06:43:56 +00004102 __u8 *addr;
Colin Ian King7d311802020-04-25 12:28:14 +01004103 int err;
Jiri Pirkof6f64242014-11-28 14:34:15 +01004104 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00004105
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07004106 if (!netlink_capable(skb, CAP_NET_ADMIN))
Vlad Yasevich1690be62013-02-13 12:00:18 +00004107 return -EPERM;
4108
Johannes Berg8cb08172019-04-26 14:07:28 +02004109 err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
4110 extack);
Vlad Yasevich1690be62013-02-13 12:00:18 +00004111 if (err < 0)
4112 return err;
John Fastabend77162022012-04-15 06:43:56 +00004113
4114 ndm = nlmsg_data(nlh);
4115 if (ndm->ndm_ifindex == 0) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004116 NL_SET_ERR_MSG(extack, "invalid ifindex");
John Fastabend77162022012-04-15 06:43:56 +00004117 return -EINVAL;
4118 }
4119
4120 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
4121 if (dev == NULL) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004122 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabend77162022012-04-15 06:43:56 +00004123 return -ENODEV;
4124 }
4125
Vlad Yasevich1690be62013-02-13 12:00:18 +00004126 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004127 NL_SET_ERR_MSG(extack, "invalid address");
John Fastabend77162022012-04-15 06:43:56 +00004128 return -EINVAL;
4129 }
4130
Ido Schimmelda715772018-10-29 20:36:43 +00004131 if (dev->type != ARPHRD_ETHER) {
4132 NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices");
4133 return -EINVAL;
4134 }
4135
Vlad Yasevich1690be62013-02-13 12:00:18 +00004136 addr = nla_data(tb[NDA_LLADDR]);
Vlad Yasevich1690be62013-02-13 12:00:18 +00004137
Florian Westphalb88d12e2017-10-10 17:10:04 +02004138 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
Jiri Pirkof6f64242014-11-28 14:34:15 +01004139 if (err)
4140 return err;
4141
John Fastabend77162022012-04-15 06:43:56 +00004142 err = -EOPNOTSUPP;
4143
4144 /* Support fdb on master device the net/bridge default case */
4145 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
Julian Wiedmann2e92a2d2020-02-20 09:00:07 +01004146 netif_is_bridge_port(dev)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00004147 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4148 const struct net_device_ops *ops = br_dev->netdev_ops;
John Fastabend77162022012-04-15 06:43:56 +00004149
Jiri Pirko898e5062013-01-03 22:48:52 +00004150 if (ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01004151 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00004152
4153 if (err)
4154 goto out;
4155 else
4156 ndm->ndm_flags &= ~NTF_MASTER;
4157 }
4158
4159 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00004160 if (ndm->ndm_flags & NTF_SELF) {
4161 if (dev->netdev_ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01004162 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
4163 vid);
Vlad Yasevich090096b2013-03-06 15:39:42 +00004164 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01004165 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00004166
John Fastabend3ff661c2012-04-15 06:44:14 +00004167 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00004168 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
4169 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00004170 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00004171 }
John Fastabend77162022012-04-15 06:43:56 +00004172 }
4173out:
4174 return err;
4175}
4176
John Fastabendd83b0602012-04-15 06:44:08 +00004177static int nlmsg_populate_fdb(struct sk_buff *skb,
4178 struct netlink_callback *cb,
4179 struct net_device *dev,
4180 int *idx,
4181 struct netdev_hw_addr_list *list)
4182{
4183 struct netdev_hw_addr *ha;
4184 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00004185 u32 portid, seq;
John Fastabendd83b0602012-04-15 06:44:08 +00004186
Eric W. Biederman15e47302012-09-07 20:12:54 +00004187 portid = NETLINK_CB(cb->skb).portid;
John Fastabendd83b0602012-04-15 06:44:08 +00004188 seq = cb->nlh->nlmsg_seq;
4189
4190 list_for_each_entry(ha, &list->list, list) {
Roopa Prabhud2976532016-08-30 21:56:45 -07004191 if (*idx < cb->args[2])
John Fastabendd83b0602012-04-15 06:44:08 +00004192 goto skip;
4193
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00004194 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
John Fastabenda7a558f2012-11-01 16:23:10 +00004195 portid, seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01004196 RTM_NEWNEIGH, NTF_SELF,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00004197 NLM_F_MULTI, NUD_PERMANENT);
John Fastabendd83b0602012-04-15 06:44:08 +00004198 if (err < 0)
4199 return err;
4200skip:
4201 *idx += 1;
4202 }
4203 return 0;
4204}
4205
4206/**
Ben Hutchings2c530402012-07-10 10:55:09 +00004207 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
Bart Van Asschea9869672019-03-25 09:17:22 -07004208 * @skb: socket buffer to store message in
4209 * @cb: netlink callback
John Fastabendd83b0602012-04-15 06:44:08 +00004210 * @dev: netdevice
Bart Van Asschea9869672019-03-25 09:17:22 -07004211 * @filter_dev: ignored
4212 * @idx: the number of FDB table entries dumped is added to *@idx
John Fastabendd83b0602012-04-15 06:44:08 +00004213 *
4214 * Default netdevice operation to dump the existing unicast address list.
John Fastabend91f3e7b2013-03-29 08:18:37 +00004215 * Returns number of addresses from list put in skb.
John Fastabendd83b0602012-04-15 06:44:08 +00004216 */
4217int ndo_dflt_fdb_dump(struct sk_buff *skb,
4218 struct netlink_callback *cb,
4219 struct net_device *dev,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04004220 struct net_device *filter_dev,
Roopa Prabhud2976532016-08-30 21:56:45 -07004221 int *idx)
John Fastabendd83b0602012-04-15 06:44:08 +00004222{
4223 int err;
4224
Eric Dumazet68883892018-12-04 09:40:35 -08004225 if (dev->type != ARPHRD_ETHER)
4226 return -EINVAL;
4227
John Fastabendd83b0602012-04-15 06:44:08 +00004228 netif_addr_lock_bh(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -07004229 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
John Fastabendd83b0602012-04-15 06:44:08 +00004230 if (err)
4231 goto out;
Zhang Shengju2934c9d2016-11-30 16:37:34 +08004232 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
John Fastabendd83b0602012-04-15 06:44:08 +00004233out:
4234 netif_addr_unlock_bh(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -07004235 return err;
John Fastabendd83b0602012-04-15 06:44:08 +00004236}
4237EXPORT_SYMBOL(ndo_dflt_fdb_dump);
4238
David Ahern8c6e1372018-10-07 20:16:44 -07004239static int valid_fdb_dump_strict(const struct nlmsghdr *nlh,
4240 int *br_idx, int *brport_idx,
4241 struct netlink_ext_ack *extack)
4242{
4243 struct nlattr *tb[NDA_MAX + 1];
4244 struct ndmsg *ndm;
4245 int err, i;
4246
4247 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
4248 NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request");
4249 return -EINVAL;
4250 }
4251
4252 ndm = nlmsg_data(nlh);
4253 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
4254 ndm->ndm_flags || ndm->ndm_type) {
Nikolay Aleksandrov8b730182019-10-29 13:59:32 +02004255 NL_SET_ERR_MSG(extack, "Invalid values in header for fdb dump request");
David Ahern8c6e1372018-10-07 20:16:44 -07004256 return -EINVAL;
4257 }
4258
Johannes Berg8cb08172019-04-26 14:07:28 +02004259 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
4260 NDA_MAX, NULL, extack);
David Ahern8c6e1372018-10-07 20:16:44 -07004261 if (err < 0)
4262 return err;
4263
4264 *brport_idx = ndm->ndm_ifindex;
4265 for (i = 0; i <= NDA_MAX; ++i) {
4266 if (!tb[i])
4267 continue;
4268
4269 switch (i) {
4270 case NDA_IFINDEX:
4271 if (nla_len(tb[i]) != sizeof(u32)) {
4272 NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request");
4273 return -EINVAL;
4274 }
4275 *brport_idx = nla_get_u32(tb[NDA_IFINDEX]);
4276 break;
4277 case NDA_MASTER:
4278 if (nla_len(tb[i]) != sizeof(u32)) {
4279 NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request");
4280 return -EINVAL;
4281 }
4282 *br_idx = nla_get_u32(tb[NDA_MASTER]);
4283 break;
4284 default:
4285 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request");
4286 return -EINVAL;
4287 }
4288 }
4289
4290 return 0;
4291}
4292
David Ahern8dfbda12018-10-07 20:16:43 -07004293static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh,
4294 int *br_idx, int *brport_idx,
4295 struct netlink_ext_ack *extack)
4296{
David Ahern8dfbda12018-10-07 20:16:43 -07004297 struct nlattr *tb[IFLA_MAX+1];
4298 int err;
4299
4300 /* A hack to preserve kernel<->userspace interface.
4301 * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0.
4302 * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails.
4303 * So, check for ndmsg with an optional u32 attribute (not used here).
4304 * Fortunately these sizes don't conflict with the size of ifinfomsg
4305 * with an optional attribute.
4306 */
4307 if (nlmsg_len(nlh) != sizeof(struct ndmsg) &&
4308 (nlmsg_len(nlh) != sizeof(struct ndmsg) +
4309 nla_attr_size(sizeof(u32)))) {
David Ahern4565d7e2018-10-08 13:57:24 -07004310 struct ifinfomsg *ifm;
4311
Johannes Berg8cb08172019-04-26 14:07:28 +02004312 err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
4313 tb, IFLA_MAX, ifla_policy,
4314 extack);
David Ahern8dfbda12018-10-07 20:16:43 -07004315 if (err < 0) {
4316 return -EINVAL;
4317 } else if (err == 0) {
4318 if (tb[IFLA_MASTER])
4319 *br_idx = nla_get_u32(tb[IFLA_MASTER]);
4320 }
4321
David Ahern4565d7e2018-10-08 13:57:24 -07004322 ifm = nlmsg_data(nlh);
David Ahern8dfbda12018-10-07 20:16:43 -07004323 *brport_idx = ifm->ifi_index;
4324 }
4325 return 0;
4326}
4327
John Fastabend77162022012-04-15 06:43:56 +00004328static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
4329{
John Fastabend77162022012-04-15 06:43:56 +00004330 struct net_device *dev;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004331 struct net_device *br_dev = NULL;
4332 const struct net_device_ops *ops = NULL;
4333 const struct net_device_ops *cops = NULL;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004334 struct net *net = sock_net(skb->sk);
Roopa Prabhud2976532016-08-30 21:56:45 -07004335 struct hlist_head *head;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004336 int brport_idx = 0;
4337 int br_idx = 0;
Roopa Prabhud2976532016-08-30 21:56:45 -07004338 int h, s_h;
4339 int idx = 0, s_idx;
4340 int err = 0;
4341 int fidx = 0;
John Fastabend77162022012-04-15 06:43:56 +00004342
David Ahern8c6e1372018-10-07 20:16:44 -07004343 if (cb->strict_check)
4344 err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx,
4345 cb->extack);
4346 else
4347 err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx,
4348 cb->extack);
David Ahern8dfbda12018-10-07 20:16:43 -07004349 if (err < 0)
4350 return err;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004351
4352 if (br_idx) {
4353 br_dev = __dev_get_by_index(net, br_idx);
4354 if (!br_dev)
4355 return -ENODEV;
4356
4357 ops = br_dev->netdev_ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004358 }
4359
Roopa Prabhud2976532016-08-30 21:56:45 -07004360 s_h = cb->args[0];
4361 s_idx = cb->args[1];
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004362
Roopa Prabhud2976532016-08-30 21:56:45 -07004363 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4364 idx = 0;
4365 head = &net->dev_index_head[h];
4366 hlist_for_each_entry(dev, head, index_hlist) {
4367
4368 if (brport_idx && (dev->ifindex != brport_idx))
4369 continue;
4370
4371 if (!br_idx) { /* user did not specify a specific bridge */
Julian Wiedmann2e92a2d2020-02-20 09:00:07 +01004372 if (netif_is_bridge_port(dev)) {
Roopa Prabhud2976532016-08-30 21:56:45 -07004373 br_dev = netdev_master_upper_dev_get(dev);
4374 cops = br_dev->netdev_ops;
4375 }
4376 } else {
4377 if (dev != br_dev &&
Julian Wiedmann2e92a2d2020-02-20 09:00:07 +01004378 !netif_is_bridge_port(dev))
Roopa Prabhud2976532016-08-30 21:56:45 -07004379 continue;
4380
4381 if (br_dev != netdev_master_upper_dev_get(dev) &&
4382 !(dev->priv_flags & IFF_EBRIDGE))
4383 continue;
4384 cops = ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004385 }
4386
Roopa Prabhud2976532016-08-30 21:56:45 -07004387 if (idx < s_idx)
4388 goto cont;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004389
Julian Wiedmann2e92a2d2020-02-20 09:00:07 +01004390 if (netif_is_bridge_port(dev)) {
Roopa Prabhud2976532016-08-30 21:56:45 -07004391 if (cops && cops->ndo_fdb_dump) {
4392 err = cops->ndo_fdb_dump(skb, cb,
4393 br_dev, dev,
4394 &fidx);
4395 if (err == -EMSGSIZE)
4396 goto out;
4397 }
4398 }
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004399
Roopa Prabhud2976532016-08-30 21:56:45 -07004400 if (dev->netdev_ops->ndo_fdb_dump)
4401 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
4402 dev, NULL,
4403 &fidx);
4404 else
4405 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
4406 &fidx);
4407 if (err == -EMSGSIZE)
4408 goto out;
4409
4410 cops = NULL;
4411
4412 /* reset fdb offset to 0 for rest of the interfaces */
4413 cb->args[2] = 0;
4414 fidx = 0;
4415cont:
4416 idx++;
John Fastabend77162022012-04-15 06:43:56 +00004417 }
John Fastabend77162022012-04-15 06:43:56 +00004418 }
John Fastabend77162022012-04-15 06:43:56 +00004419
Roopa Prabhud2976532016-08-30 21:56:45 -07004420out:
4421 cb->args[0] = h;
4422 cb->args[1] = idx;
4423 cb->args[2] = fidx;
4424
John Fastabend77162022012-04-15 06:43:56 +00004425 return skb->len;
4426}
4427
Roopa Prabhu5b2f94b2018-12-15 22:35:08 -08004428static int valid_fdb_get_strict(const struct nlmsghdr *nlh,
4429 struct nlattr **tb, u8 *ndm_flags,
4430 int *br_idx, int *brport_idx, u8 **addr,
4431 u16 *vid, struct netlink_ext_ack *extack)
4432{
4433 struct ndmsg *ndm;
4434 int err, i;
4435
4436 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
4437 NL_SET_ERR_MSG(extack, "Invalid header for fdb get request");
4438 return -EINVAL;
4439 }
4440
4441 ndm = nlmsg_data(nlh);
4442 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
4443 ndm->ndm_type) {
4444 NL_SET_ERR_MSG(extack, "Invalid values in header for fdb get request");
4445 return -EINVAL;
4446 }
4447
4448 if (ndm->ndm_flags & ~(NTF_MASTER | NTF_SELF)) {
4449 NL_SET_ERR_MSG(extack, "Invalid flags in header for fdb get request");
4450 return -EINVAL;
4451 }
4452
Johannes Berg8cb08172019-04-26 14:07:28 +02004453 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
4454 NDA_MAX, nda_policy, extack);
Roopa Prabhu5b2f94b2018-12-15 22:35:08 -08004455 if (err < 0)
4456 return err;
4457
4458 *ndm_flags = ndm->ndm_flags;
4459 *brport_idx = ndm->ndm_ifindex;
4460 for (i = 0; i <= NDA_MAX; ++i) {
4461 if (!tb[i])
4462 continue;
4463
4464 switch (i) {
4465 case NDA_MASTER:
4466 *br_idx = nla_get_u32(tb[i]);
4467 break;
4468 case NDA_LLADDR:
4469 if (nla_len(tb[i]) != ETH_ALEN) {
4470 NL_SET_ERR_MSG(extack, "Invalid address in fdb get request");
4471 return -EINVAL;
4472 }
4473 *addr = nla_data(tb[i]);
4474 break;
4475 case NDA_VLAN:
4476 err = fdb_vid_parse(tb[i], vid, extack);
4477 if (err)
4478 return err;
4479 break;
4480 case NDA_VNI:
4481 break;
4482 default:
4483 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb get request");
4484 return -EINVAL;
4485 }
4486 }
4487
4488 return 0;
4489}
4490
4491static int rtnl_fdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh,
4492 struct netlink_ext_ack *extack)
4493{
4494 struct net_device *dev = NULL, *br_dev = NULL;
4495 const struct net_device_ops *ops = NULL;
4496 struct net *net = sock_net(in_skb->sk);
4497 struct nlattr *tb[NDA_MAX + 1];
4498 struct sk_buff *skb;
4499 int brport_idx = 0;
4500 u8 ndm_flags = 0;
4501 int br_idx = 0;
4502 u8 *addr = NULL;
4503 u16 vid = 0;
4504 int err;
4505
4506 err = valid_fdb_get_strict(nlh, tb, &ndm_flags, &br_idx,
4507 &brport_idx, &addr, &vid, extack);
4508 if (err < 0)
4509 return err;
4510
Nikolay Aleksandrovf989d032018-12-30 14:33:20 +02004511 if (!addr) {
4512 NL_SET_ERR_MSG(extack, "Missing lookup address for fdb get request");
4513 return -EINVAL;
4514 }
4515
Roopa Prabhu5b2f94b2018-12-15 22:35:08 -08004516 if (brport_idx) {
4517 dev = __dev_get_by_index(net, brport_idx);
4518 if (!dev) {
4519 NL_SET_ERR_MSG(extack, "Unknown device ifindex");
4520 return -ENODEV;
4521 }
4522 }
4523
4524 if (br_idx) {
4525 if (dev) {
4526 NL_SET_ERR_MSG(extack, "Master and device are mutually exclusive");
4527 return -EINVAL;
4528 }
4529
4530 br_dev = __dev_get_by_index(net, br_idx);
4531 if (!br_dev) {
4532 NL_SET_ERR_MSG(extack, "Invalid master ifindex");
4533 return -EINVAL;
4534 }
4535 ops = br_dev->netdev_ops;
4536 }
4537
4538 if (dev) {
4539 if (!ndm_flags || (ndm_flags & NTF_MASTER)) {
Julian Wiedmann2e92a2d2020-02-20 09:00:07 +01004540 if (!netif_is_bridge_port(dev)) {
Roopa Prabhu5b2f94b2018-12-15 22:35:08 -08004541 NL_SET_ERR_MSG(extack, "Device is not a bridge port");
4542 return -EINVAL;
4543 }
4544 br_dev = netdev_master_upper_dev_get(dev);
4545 if (!br_dev) {
4546 NL_SET_ERR_MSG(extack, "Master of device not found");
4547 return -EINVAL;
4548 }
4549 ops = br_dev->netdev_ops;
4550 } else {
4551 if (!(ndm_flags & NTF_SELF)) {
4552 NL_SET_ERR_MSG(extack, "Missing NTF_SELF");
4553 return -EINVAL;
4554 }
4555 ops = dev->netdev_ops;
4556 }
4557 }
4558
4559 if (!br_dev && !dev) {
4560 NL_SET_ERR_MSG(extack, "No device specified");
4561 return -ENODEV;
4562 }
4563
4564 if (!ops || !ops->ndo_fdb_get) {
4565 NL_SET_ERR_MSG(extack, "Fdb get operation not supported by device");
4566 return -EOPNOTSUPP;
4567 }
4568
4569 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
4570 if (!skb)
4571 return -ENOBUFS;
4572
4573 if (br_dev)
4574 dev = br_dev;
4575 err = ops->ndo_fdb_get(skb, tb, dev, addr, vid,
4576 NETLINK_CB(in_skb).portid,
4577 nlh->nlmsg_seq, extack);
4578 if (err)
4579 goto out;
4580
4581 return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
4582out:
4583 kfree_skb(skb);
4584 return err;
4585}
4586
Scott Feldman2c3c0312014-11-28 14:34:25 +01004587static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
4588 unsigned int attrnum, unsigned int flag)
4589{
4590 if (mask & flag)
4591 return nla_put_u8(skb, attrnum, !!(flags & flag));
4592 return 0;
4593}
4594
John Fastabend815cccb2012-10-24 08:13:09 +00004595int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Scott Feldman2c3c0312014-11-28 14:34:25 +01004596 struct net_device *dev, u16 mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -07004597 u32 flags, u32 mask, int nlflags,
4598 u32 filter_mask,
4599 int (*vlan_fill)(struct sk_buff *skb,
4600 struct net_device *dev,
4601 u32 filter_mask))
John Fastabend815cccb2012-10-24 08:13:09 +00004602{
4603 struct nlmsghdr *nlh;
4604 struct ifinfomsg *ifm;
4605 struct nlattr *br_afspec;
Scott Feldman2c3c0312014-11-28 14:34:25 +01004606 struct nlattr *protinfo;
John Fastabend815cccb2012-10-24 08:13:09 +00004607 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Jiri Pirko898e5062013-01-03 22:48:52 +00004608 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07004609 int err = 0;
John Fastabend815cccb2012-10-24 08:13:09 +00004610
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02004611 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
John Fastabend815cccb2012-10-24 08:13:09 +00004612 if (nlh == NULL)
4613 return -EMSGSIZE;
4614
4615 ifm = nlmsg_data(nlh);
4616 ifm->ifi_family = AF_BRIDGE;
4617 ifm->__ifi_pad = 0;
4618 ifm->ifi_type = dev->type;
4619 ifm->ifi_index = dev->ifindex;
4620 ifm->ifi_flags = dev_get_flags(dev);
4621 ifm->ifi_change = 0;
4622
4623
4624 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4625 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4626 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00004627 (br_dev &&
4628 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
John Fastabend815cccb2012-10-24 08:13:09 +00004629 (dev->addr_len &&
4630 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02004631 (dev->ifindex != dev_get_iflink(dev) &&
4632 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
John Fastabend815cccb2012-10-24 08:13:09 +00004633 goto nla_put_failure;
4634
Michal Kubecekae0be8d2019-04-26 11:13:06 +02004635 br_afspec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
John Fastabend815cccb2012-10-24 08:13:09 +00004636 if (!br_afspec)
4637 goto nla_put_failure;
4638
Roopa Prabhu1d460b92014-12-08 14:04:20 -08004639 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
John Fastabend815cccb2012-10-24 08:13:09 +00004640 nla_nest_cancel(skb, br_afspec);
4641 goto nla_put_failure;
4642 }
Roopa Prabhu1d460b92014-12-08 14:04:20 -08004643
4644 if (mode != BRIDGE_MODE_UNDEF) {
4645 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
4646 nla_nest_cancel(skb, br_afspec);
4647 goto nla_put_failure;
4648 }
4649 }
Scott Feldman7d4f8d82015-06-22 00:27:17 -07004650 if (vlan_fill) {
4651 err = vlan_fill(skb, dev, filter_mask);
4652 if (err) {
4653 nla_nest_cancel(skb, br_afspec);
4654 goto nla_put_failure;
4655 }
4656 }
John Fastabend815cccb2012-10-24 08:13:09 +00004657 nla_nest_end(skb, br_afspec);
4658
Michal Kubecekae0be8d2019-04-26 11:13:06 +02004659 protinfo = nla_nest_start(skb, IFLA_PROTINFO);
Scott Feldman2c3c0312014-11-28 14:34:25 +01004660 if (!protinfo)
4661 goto nla_put_failure;
4662
4663 if (brport_nla_put_flag(skb, flags, mask,
4664 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
4665 brport_nla_put_flag(skb, flags, mask,
4666 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
4667 brport_nla_put_flag(skb, flags, mask,
4668 IFLA_BRPORT_FAST_LEAVE,
4669 BR_MULTICAST_FAST_LEAVE) ||
4670 brport_nla_put_flag(skb, flags, mask,
4671 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
4672 brport_nla_put_flag(skb, flags, mask,
4673 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
4674 brport_nla_put_flag(skb, flags, mask,
4675 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
4676 brport_nla_put_flag(skb, flags, mask,
4677 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
4678 brport_nla_put_flag(skb, flags, mask,
Julian Wiedmann583cb0b2020-02-17 14:45:01 +01004679 IFLA_BRPORT_PROXYARP, BR_PROXYARP) ||
4680 brport_nla_put_flag(skb, flags, mask,
4681 IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD) ||
4682 brport_nla_put_flag(skb, flags, mask,
4683 IFLA_BRPORT_BCAST_FLOOD, BR_BCAST_FLOOD)) {
Scott Feldman2c3c0312014-11-28 14:34:25 +01004684 nla_nest_cancel(skb, protinfo);
4685 goto nla_put_failure;
4686 }
4687
4688 nla_nest_end(skb, protinfo);
4689
Johannes Berg053c0952015-01-16 22:09:00 +01004690 nlmsg_end(skb, nlh);
4691 return 0;
John Fastabend815cccb2012-10-24 08:13:09 +00004692nla_put_failure:
4693 nlmsg_cancel(skb, nlh);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07004694 return err ? err : -EMSGSIZE;
John Fastabend815cccb2012-10-24 08:13:09 +00004695}
Scott Feldman7d4f8d82015-06-22 00:27:17 -07004696EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
John Fastabend815cccb2012-10-24 08:13:09 +00004697
David Ahern2d011be2018-10-07 20:16:31 -07004698static int valid_bridge_getlink_req(const struct nlmsghdr *nlh,
4699 bool strict_check, u32 *filter_mask,
4700 struct netlink_ext_ack *extack)
4701{
4702 struct nlattr *tb[IFLA_MAX+1];
4703 int err, i;
4704
4705 if (strict_check) {
4706 struct ifinfomsg *ifm;
4707
4708 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
4709 NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump");
4710 return -EINVAL;
4711 }
4712
4713 ifm = nlmsg_data(nlh);
4714 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
4715 ifm->ifi_change || ifm->ifi_index) {
4716 NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request");
4717 return -EINVAL;
4718 }
4719
Johannes Berg8cb08172019-04-26 14:07:28 +02004720 err = nlmsg_parse_deprecated_strict(nlh,
4721 sizeof(struct ifinfomsg),
4722 tb, IFLA_MAX, ifla_policy,
4723 extack);
David Ahern2d011be2018-10-07 20:16:31 -07004724 } else {
Johannes Berg8cb08172019-04-26 14:07:28 +02004725 err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
4726 tb, IFLA_MAX, ifla_policy,
4727 extack);
David Ahern2d011be2018-10-07 20:16:31 -07004728 }
4729 if (err < 0)
4730 return err;
4731
4732 /* new attributes should only be added with strict checking */
4733 for (i = 0; i <= IFLA_MAX; ++i) {
4734 if (!tb[i])
4735 continue;
4736
4737 switch (i) {
4738 case IFLA_EXT_MASK:
4739 *filter_mask = nla_get_u32(tb[i]);
4740 break;
4741 default:
4742 if (strict_check) {
4743 NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request");
4744 return -EINVAL;
4745 }
4746 }
4747 }
4748
4749 return 0;
4750}
4751
John Fastabende5a55a82012-10-24 08:12:57 +00004752static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
4753{
David Ahern2d011be2018-10-07 20:16:31 -07004754 const struct nlmsghdr *nlh = cb->nlh;
John Fastabende5a55a82012-10-24 08:12:57 +00004755 struct net *net = sock_net(skb->sk);
4756 struct net_device *dev;
4757 int idx = 0;
4758 u32 portid = NETLINK_CB(cb->skb).portid;
David Ahern2d011be2018-10-07 20:16:31 -07004759 u32 seq = nlh->nlmsg_seq;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00004760 u32 filter_mask = 0;
Roopa Prabhud64f69b2015-09-15 14:44:29 -07004761 int err;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00004762
David Ahern2d011be2018-10-07 20:16:31 -07004763 err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask,
4764 cb->extack);
4765 if (err < 0 && cb->strict_check)
4766 return err;
John Fastabende5a55a82012-10-24 08:12:57 +00004767
4768 rcu_read_lock();
4769 for_each_netdev_rcu(net, dev) {
4770 const struct net_device_ops *ops = dev->netdev_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00004771 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
John Fastabende5a55a82012-10-24 08:12:57 +00004772
Jiri Pirko898e5062013-01-03 22:48:52 +00004773 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07004774 if (idx >= cb->args[0]) {
4775 err = br_dev->netdev_ops->ndo_bridge_getlink(
4776 skb, portid, seq, dev,
4777 filter_mask, NLM_F_MULTI);
David Ahernf6c57752017-05-15 23:19:17 -07004778 if (err < 0 && err != -EOPNOTSUPP) {
4779 if (likely(skb->len))
4780 break;
4781
4782 goto out_err;
4783 }
Roopa Prabhud64f69b2015-09-15 14:44:29 -07004784 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00004785 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00004786 }
4787
4788 if (ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07004789 if (idx >= cb->args[0]) {
4790 err = ops->ndo_bridge_getlink(skb, portid,
4791 seq, dev,
4792 filter_mask,
4793 NLM_F_MULTI);
David Ahernf6c57752017-05-15 23:19:17 -07004794 if (err < 0 && err != -EOPNOTSUPP) {
4795 if (likely(skb->len))
4796 break;
4797
4798 goto out_err;
4799 }
Roopa Prabhud64f69b2015-09-15 14:44:29 -07004800 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00004801 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00004802 }
4803 }
David Ahernf6c57752017-05-15 23:19:17 -07004804 err = skb->len;
4805out_err:
John Fastabende5a55a82012-10-24 08:12:57 +00004806 rcu_read_unlock();
4807 cb->args[0] = idx;
4808
David Ahernf6c57752017-05-15 23:19:17 -07004809 return err;
John Fastabende5a55a82012-10-24 08:12:57 +00004810}
4811
John Fastabend2469ffd2012-10-24 08:13:03 +00004812static inline size_t bridge_nlmsg_size(void)
4813{
4814 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4815 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4816 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4817 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
4818 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
4819 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
4820 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
4821 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
4822 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
4823 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
4824 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
4825}
4826
Roopa Prabhu02dba432015-01-14 20:02:25 -08004827static int rtnl_bridge_notify(struct net_device *dev)
John Fastabend2469ffd2012-10-24 08:13:03 +00004828{
4829 struct net *net = dev_net(dev);
John Fastabend2469ffd2012-10-24 08:13:03 +00004830 struct sk_buff *skb;
4831 int err = -EOPNOTSUPP;
4832
Roopa Prabhu02dba432015-01-14 20:02:25 -08004833 if (!dev->netdev_ops->ndo_bridge_getlink)
4834 return 0;
4835
John Fastabend2469ffd2012-10-24 08:13:03 +00004836 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
4837 if (!skb) {
4838 err = -ENOMEM;
4839 goto errout;
4840 }
4841
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02004842 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
Roopa Prabhu02dba432015-01-14 20:02:25 -08004843 if (err < 0)
4844 goto errout;
John Fastabend2469ffd2012-10-24 08:13:03 +00004845
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08004846 if (!skb->len)
4847 goto errout;
4848
John Fastabend2469ffd2012-10-24 08:13:03 +00004849 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
4850 return 0;
4851errout:
4852 WARN_ON(err == -EMSGSIZE);
4853 kfree_skb(skb);
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08004854 if (err)
4855 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
John Fastabend2469ffd2012-10-24 08:13:03 +00004856 return err;
4857}
4858
David Ahernc21ef3e2017-04-16 09:48:24 -07004859static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
4860 struct netlink_ext_ack *extack)
John Fastabende5a55a82012-10-24 08:12:57 +00004861{
4862 struct net *net = sock_net(skb->sk);
4863 struct ifinfomsg *ifm;
4864 struct net_device *dev;
John Fastabend2469ffd2012-10-24 08:13:03 +00004865 struct nlattr *br_spec, *attr = NULL;
4866 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02004867 u16 flags = 0;
John Fastabendc38e01b2012-11-02 16:32:36 +00004868 bool have_flags = false;
John Fastabende5a55a82012-10-24 08:12:57 +00004869
4870 if (nlmsg_len(nlh) < sizeof(*ifm))
4871 return -EINVAL;
4872
4873 ifm = nlmsg_data(nlh);
4874 if (ifm->ifi_family != AF_BRIDGE)
4875 return -EPFNOSUPPORT;
4876
4877 dev = __dev_get_by_index(net, ifm->ifi_index);
4878 if (!dev) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004879 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabende5a55a82012-10-24 08:12:57 +00004880 return -ENODEV;
4881 }
4882
John Fastabend2469ffd2012-10-24 08:13:03 +00004883 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4884 if (br_spec) {
4885 nla_for_each_nested(attr, br_spec, rem) {
4886 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01004887 if (nla_len(attr) < sizeof(flags))
4888 return -EINVAL;
4889
John Fastabendc38e01b2012-11-02 16:32:36 +00004890 have_flags = true;
John Fastabend2469ffd2012-10-24 08:13:03 +00004891 flags = nla_get_u16(attr);
4892 break;
4893 }
4894 }
4895 }
4896
4897 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00004898 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4899
4900 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
John Fastabend2469ffd2012-10-24 08:13:03 +00004901 err = -EOPNOTSUPP;
4902 goto out;
4903 }
4904
Petr Machata2fd527b2018-12-12 17:02:48 +00004905 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags,
4906 extack);
John Fastabende5a55a82012-10-24 08:12:57 +00004907 if (err)
4908 goto out;
John Fastabend2469ffd2012-10-24 08:13:03 +00004909
4910 flags &= ~BRIDGE_FLAGS_MASTER;
John Fastabende5a55a82012-10-24 08:12:57 +00004911 }
4912
John Fastabend2469ffd2012-10-24 08:13:03 +00004913 if ((flags & BRIDGE_FLAGS_SELF)) {
4914 if (!dev->netdev_ops->ndo_bridge_setlink)
4915 err = -EOPNOTSUPP;
4916 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08004917 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
Petr Machata2fd527b2018-12-12 17:02:48 +00004918 flags,
4919 extack);
Roopa Prabhu02dba432015-01-14 20:02:25 -08004920 if (!err) {
John Fastabend2469ffd2012-10-24 08:13:03 +00004921 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08004922
4923 /* Generate event to notify upper layer of bridge
4924 * change
4925 */
4926 err = rtnl_bridge_notify(dev);
4927 }
John Fastabend2469ffd2012-10-24 08:13:03 +00004928 }
4929
John Fastabendc38e01b2012-11-02 16:32:36 +00004930 if (have_flags)
John Fastabend2469ffd2012-10-24 08:13:03 +00004931 memcpy(nla_data(attr), &flags, sizeof(flags));
John Fastabende5a55a82012-10-24 08:12:57 +00004932out:
4933 return err;
4934}
4935
David Ahernc21ef3e2017-04-16 09:48:24 -07004936static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
4937 struct netlink_ext_ack *extack)
Vlad Yasevich407af322013-02-13 12:00:12 +00004938{
4939 struct net *net = sock_net(skb->sk);
4940 struct ifinfomsg *ifm;
4941 struct net_device *dev;
4942 struct nlattr *br_spec, *attr = NULL;
4943 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02004944 u16 flags = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +00004945 bool have_flags = false;
4946
4947 if (nlmsg_len(nlh) < sizeof(*ifm))
4948 return -EINVAL;
4949
4950 ifm = nlmsg_data(nlh);
4951 if (ifm->ifi_family != AF_BRIDGE)
4952 return -EPFNOSUPPORT;
4953
4954 dev = __dev_get_by_index(net, ifm->ifi_index);
4955 if (!dev) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004956 NL_SET_ERR_MSG(extack, "unknown ifindex");
Vlad Yasevich407af322013-02-13 12:00:12 +00004957 return -ENODEV;
4958 }
4959
4960 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4961 if (br_spec) {
4962 nla_for_each_nested(attr, br_spec, rem) {
4963 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01004964 if (nla_len(attr) < sizeof(flags))
4965 return -EINVAL;
4966
Vlad Yasevich407af322013-02-13 12:00:12 +00004967 have_flags = true;
4968 flags = nla_get_u16(attr);
4969 break;
4970 }
4971 }
4972 }
4973
Vlad Yasevich407af322013-02-13 12:00:12 +00004974 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
4975 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4976
4977 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
4978 err = -EOPNOTSUPP;
4979 goto out;
4980 }
4981
Roopa Prabhuadd511b2015-01-29 22:40:12 -08004982 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00004983 if (err)
4984 goto out;
4985
4986 flags &= ~BRIDGE_FLAGS_MASTER;
4987 }
4988
4989 if ((flags & BRIDGE_FLAGS_SELF)) {
4990 if (!dev->netdev_ops->ndo_bridge_dellink)
4991 err = -EOPNOTSUPP;
4992 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08004993 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
4994 flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00004995
Roopa Prabhu02dba432015-01-14 20:02:25 -08004996 if (!err) {
Vlad Yasevich407af322013-02-13 12:00:12 +00004997 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08004998
4999 /* Generate event to notify upper layer of bridge
5000 * change
5001 */
5002 err = rtnl_bridge_notify(dev);
5003 }
Vlad Yasevich407af322013-02-13 12:00:12 +00005004 }
5005
5006 if (have_flags)
5007 memcpy(nla_data(attr), &flags, sizeof(flags));
Vlad Yasevich407af322013-02-13 12:00:12 +00005008out:
5009 return err;
5010}
5011
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005012static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
5013{
5014 return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
5015 (!idxattr || idxattr == attrid);
5016}
5017
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02005018#define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
5019static int rtnl_get_offload_stats_attr_size(int attr_id)
5020{
5021 switch (attr_id) {
5022 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
5023 return sizeof(struct rtnl_link_stats64);
5024 }
5025
5026 return 0;
5027}
5028
5029static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
5030 int *prividx)
5031{
5032 struct nlattr *attr = NULL;
5033 int attr_id, size;
5034 void *attr_data;
5035 int err;
5036
5037 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
5038 dev->netdev_ops->ndo_get_offload_stats))
5039 return -ENODATA;
5040
5041 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
5042 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
5043 if (attr_id < *prividx)
5044 continue;
5045
5046 size = rtnl_get_offload_stats_attr_size(attr_id);
5047 if (!size)
5048 continue;
5049
Or Gerlitz3df5b3c2016-11-22 23:09:54 +02005050 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02005051 continue;
5052
5053 attr = nla_reserve_64bit(skb, attr_id, size,
5054 IFLA_OFFLOAD_XSTATS_UNSPEC);
5055 if (!attr)
5056 goto nla_put_failure;
5057
5058 attr_data = nla_data(attr);
5059 memset(attr_data, 0, size);
5060 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
5061 attr_data);
5062 if (err)
5063 goto get_offload_stats_failure;
5064 }
5065
5066 if (!attr)
5067 return -ENODATA;
5068
5069 *prividx = 0;
5070 return 0;
5071
5072nla_put_failure:
5073 err = -EMSGSIZE;
5074get_offload_stats_failure:
5075 *prividx = attr_id;
5076 return err;
5077}
5078
5079static int rtnl_get_offload_stats_size(const struct net_device *dev)
5080{
5081 int nla_size = 0;
5082 int attr_id;
5083 int size;
5084
5085 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
5086 dev->netdev_ops->ndo_get_offload_stats))
5087 return 0;
5088
5089 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
5090 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
Or Gerlitz3df5b3c2016-11-22 23:09:54 +02005091 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02005092 continue;
5093 size = rtnl_get_offload_stats_attr_size(attr_id);
5094 nla_size += nla_total_size_64bit(size);
5095 }
5096
5097 if (nla_size != 0)
5098 nla_size += nla_total_size(0);
5099
5100 return nla_size;
5101}
5102
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005103static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
5104 int type, u32 pid, u32 seq, u32 change,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005105 unsigned int flags, unsigned int filter_mask,
5106 int *idxattr, int *prividx)
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005107{
5108 struct if_stats_msg *ifsm;
5109 struct nlmsghdr *nlh;
5110 struct nlattr *attr;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005111 int s_prividx = *prividx;
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02005112 int err;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005113
5114 ASSERT_RTNL();
5115
5116 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
5117 if (!nlh)
5118 return -EMSGSIZE;
5119
5120 ifsm = nlmsg_data(nlh);
Nikolay Aleksandrovce024f42017-10-03 13:20:48 +03005121 ifsm->family = PF_UNSPEC;
5122 ifsm->pad1 = 0;
5123 ifsm->pad2 = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005124 ifsm->ifindex = dev->ifindex;
5125 ifsm->filter_mask = filter_mask;
5126
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005127 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005128 struct rtnl_link_stats64 *sp;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005129
Nicolas Dichtel58414d32016-04-21 18:58:25 +02005130 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
5131 sizeof(struct rtnl_link_stats64),
5132 IFLA_STATS_UNSPEC);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005133 if (!attr)
5134 goto nla_put_failure;
5135
5136 sp = nla_data(attr);
5137 dev_get_stats(dev, sp);
5138 }
5139
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02005140 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
5141 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
5142
5143 if (ops && ops->fill_linkxstats) {
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02005144 *idxattr = IFLA_STATS_LINK_XSTATS;
Michal Kubecekae0be8d2019-04-26 11:13:06 +02005145 attr = nla_nest_start_noflag(skb,
5146 IFLA_STATS_LINK_XSTATS);
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02005147 if (!attr)
5148 goto nla_put_failure;
5149
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02005150 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
5151 nla_nest_end(skb, attr);
5152 if (err)
5153 goto nla_put_failure;
5154 *idxattr = 0;
5155 }
5156 }
5157
5158 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
5159 *idxattr)) {
5160 const struct rtnl_link_ops *ops = NULL;
5161 const struct net_device *master;
5162
5163 master = netdev_master_upper_dev_get(dev);
5164 if (master)
5165 ops = master->rtnl_link_ops;
5166 if (ops && ops->fill_linkxstats) {
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02005167 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
Michal Kubecekae0be8d2019-04-26 11:13:06 +02005168 attr = nla_nest_start_noflag(skb,
5169 IFLA_STATS_LINK_XSTATS_SLAVE);
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02005170 if (!attr)
5171 goto nla_put_failure;
5172
5173 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02005174 nla_nest_end(skb, attr);
5175 if (err)
5176 goto nla_put_failure;
5177 *idxattr = 0;
5178 }
5179 }
5180
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02005181 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
5182 *idxattr)) {
5183 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
Michal Kubecekae0be8d2019-04-26 11:13:06 +02005184 attr = nla_nest_start_noflag(skb,
5185 IFLA_STATS_LINK_OFFLOAD_XSTATS);
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02005186 if (!attr)
5187 goto nla_put_failure;
5188
5189 err = rtnl_get_offload_stats(skb, dev, prividx);
5190 if (err == -ENODATA)
5191 nla_nest_cancel(skb, attr);
5192 else
5193 nla_nest_end(skb, attr);
5194
5195 if (err && err != -ENODATA)
5196 goto nla_put_failure;
5197 *idxattr = 0;
5198 }
5199
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005200 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
5201 struct rtnl_af_ops *af_ops;
5202
5203 *idxattr = IFLA_STATS_AF_SPEC;
Michal Kubecekae0be8d2019-04-26 11:13:06 +02005204 attr = nla_nest_start_noflag(skb, IFLA_STATS_AF_SPEC);
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005205 if (!attr)
5206 goto nla_put_failure;
5207
Florian Westphal5fa85a02017-10-16 15:44:36 +02005208 rcu_read_lock();
5209 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005210 if (af_ops->fill_stats_af) {
5211 struct nlattr *af;
5212 int err;
5213
Michal Kubecekae0be8d2019-04-26 11:13:06 +02005214 af = nla_nest_start_noflag(skb,
5215 af_ops->family);
Florian Westphal5fa85a02017-10-16 15:44:36 +02005216 if (!af) {
5217 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005218 goto nla_put_failure;
Florian Westphal5fa85a02017-10-16 15:44:36 +02005219 }
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005220 err = af_ops->fill_stats_af(skb, dev);
5221
Florian Westphal5fa85a02017-10-16 15:44:36 +02005222 if (err == -ENODATA) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005223 nla_nest_cancel(skb, af);
Florian Westphal5fa85a02017-10-16 15:44:36 +02005224 } else if (err < 0) {
5225 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005226 goto nla_put_failure;
Florian Westphal5fa85a02017-10-16 15:44:36 +02005227 }
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005228
5229 nla_nest_end(skb, af);
5230 }
5231 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02005232 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005233
5234 nla_nest_end(skb, attr);
5235
5236 *idxattr = 0;
5237 }
5238
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005239 nlmsg_end(skb, nlh);
5240
5241 return 0;
5242
5243nla_put_failure:
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005244 /* not a multi message or no progress mean a real error */
5245 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
5246 nlmsg_cancel(skb, nlh);
5247 else
5248 nlmsg_end(skb, nlh);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005249
5250 return -EMSGSIZE;
5251}
5252
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005253static size_t if_nlmsg_stats_size(const struct net_device *dev,
5254 u32 filter_mask)
5255{
5256 size_t size = 0;
5257
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005258 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005259 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
5260
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02005261 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
5262 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02005263 int attr = IFLA_STATS_LINK_XSTATS;
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02005264
5265 if (ops && ops->get_linkxstats_size) {
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02005266 size += nla_total_size(ops->get_linkxstats_size(dev,
5267 attr));
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02005268 /* for IFLA_STATS_LINK_XSTATS */
5269 size += nla_total_size(0);
5270 }
5271 }
5272
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02005273 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
5274 struct net_device *_dev = (struct net_device *)dev;
5275 const struct rtnl_link_ops *ops = NULL;
5276 const struct net_device *master;
5277
5278 /* netdev_master_upper_dev_get can't take const */
5279 master = netdev_master_upper_dev_get(_dev);
5280 if (master)
5281 ops = master->rtnl_link_ops;
5282 if (ops && ops->get_linkxstats_size) {
5283 int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
5284
5285 size += nla_total_size(ops->get_linkxstats_size(dev,
5286 attr));
5287 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
5288 size += nla_total_size(0);
5289 }
5290 }
5291
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02005292 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
5293 size += rtnl_get_offload_stats_size(dev);
5294
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005295 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
5296 struct rtnl_af_ops *af_ops;
5297
5298 /* for IFLA_STATS_AF_SPEC */
5299 size += nla_total_size(0);
5300
Florian Westphal5fa85a02017-10-16 15:44:36 +02005301 rcu_read_lock();
5302 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005303 if (af_ops->get_stats_af_size) {
5304 size += nla_total_size(
5305 af_ops->get_stats_af_size(dev));
5306
5307 /* for AF_* */
5308 size += nla_total_size(0);
5309 }
5310 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02005311 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005312 }
5313
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005314 return size;
5315}
5316
Jakub Kicinski51bc8602019-01-18 10:46:14 -08005317static int rtnl_valid_stats_req(const struct nlmsghdr *nlh, bool strict_check,
5318 bool is_dump, struct netlink_ext_ack *extack)
5319{
5320 struct if_stats_msg *ifsm;
5321
Eric Dumazet69f23a02019-04-14 11:02:05 -07005322 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifsm))) {
Jakub Kicinski51bc8602019-01-18 10:46:14 -08005323 NL_SET_ERR_MSG(extack, "Invalid header for stats dump");
5324 return -EINVAL;
5325 }
5326
5327 if (!strict_check)
5328 return 0;
5329
5330 ifsm = nlmsg_data(nlh);
5331
5332 /* only requests using strict checks can pass data to influence
5333 * the dump. The legacy exception is filter_mask.
5334 */
5335 if (ifsm->pad1 || ifsm->pad2 || (is_dump && ifsm->ifindex)) {
5336 NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request");
5337 return -EINVAL;
5338 }
5339 if (nlmsg_attrlen(nlh, sizeof(*ifsm))) {
5340 NL_SET_ERR_MSG(extack, "Invalid attributes after stats header");
5341 return -EINVAL;
5342 }
Jakub Kicinski6300acb2019-01-18 10:46:15 -08005343 if (ifsm->filter_mask >= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX + 1)) {
5344 NL_SET_ERR_MSG(extack, "Invalid stats requested through filter mask");
5345 return -EINVAL;
5346 }
Jakub Kicinski51bc8602019-01-18 10:46:14 -08005347
5348 return 0;
5349}
5350
David Ahernc21ef3e2017-04-16 09:48:24 -07005351static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
5352 struct netlink_ext_ack *extack)
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005353{
5354 struct net *net = sock_net(skb->sk);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005355 struct net_device *dev = NULL;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005356 int idxattr = 0, prividx = 0;
5357 struct if_stats_msg *ifsm;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005358 struct sk_buff *nskb;
5359 u32 filter_mask;
5360 int err;
5361
Jakub Kicinski51bc8602019-01-18 10:46:14 -08005362 err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb),
5363 false, extack);
5364 if (err)
5365 return err;
Mathias Krause4775cc12016-12-28 17:52:15 +01005366
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005367 ifsm = nlmsg_data(nlh);
5368 if (ifsm->ifindex > 0)
5369 dev = __dev_get_by_index(net, ifsm->ifindex);
5370 else
5371 return -EINVAL;
5372
5373 if (!dev)
5374 return -ENODEV;
5375
5376 filter_mask = ifsm->filter_mask;
5377 if (!filter_mask)
5378 return -EINVAL;
5379
5380 nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
5381 if (!nskb)
5382 return -ENOBUFS;
5383
5384 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
5385 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005386 0, filter_mask, &idxattr, &prividx);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005387 if (err < 0) {
5388 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
5389 WARN_ON(err == -EMSGSIZE);
5390 kfree_skb(nskb);
5391 } else {
5392 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
5393 }
5394
5395 return err;
5396}
5397
5398static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
5399{
David Ahern841891e2018-10-07 20:16:32 -07005400 struct netlink_ext_ack *extack = cb->extack;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005401 int h, s_h, err, s_idx, s_idxattr, s_prividx;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005402 struct net *net = sock_net(skb->sk);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005403 unsigned int flags = NLM_F_MULTI;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005404 struct if_stats_msg *ifsm;
5405 struct hlist_head *head;
5406 struct net_device *dev;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005407 u32 filter_mask = 0;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005408 int idx = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005409
5410 s_h = cb->args[0];
5411 s_idx = cb->args[1];
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005412 s_idxattr = cb->args[2];
5413 s_prividx = cb->args[3];
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005414
5415 cb->seq = net->dev_base_seq;
5416
Jakub Kicinski51bc8602019-01-18 10:46:14 -08005417 err = rtnl_valid_stats_req(cb->nlh, cb->strict_check, true, extack);
5418 if (err)
5419 return err;
Mathias Krause4775cc12016-12-28 17:52:15 +01005420
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005421 ifsm = nlmsg_data(cb->nlh);
5422 filter_mask = ifsm->filter_mask;
David Ahern841891e2018-10-07 20:16:32 -07005423 if (!filter_mask) {
5424 NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump");
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005425 return -EINVAL;
David Ahern841891e2018-10-07 20:16:32 -07005426 }
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005427
5428 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5429 idx = 0;
5430 head = &net->dev_index_head[h];
5431 hlist_for_each_entry(dev, head, index_hlist) {
5432 if (idx < s_idx)
5433 goto cont;
5434 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
5435 NETLINK_CB(cb->skb).portid,
5436 cb->nlh->nlmsg_seq, 0,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005437 flags, filter_mask,
5438 &s_idxattr, &s_prividx);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005439 /* If we ran out of room on the first message,
5440 * we're in trouble
5441 */
5442 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
5443
5444 if (err < 0)
5445 goto out;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005446 s_prividx = 0;
5447 s_idxattr = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005448 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
5449cont:
5450 idx++;
5451 }
5452 }
5453out:
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005454 cb->args[3] = s_prividx;
5455 cb->args[2] = s_idxattr;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005456 cb->args[1] = idx;
5457 cb->args[0] = h;
5458
5459 return skb->len;
5460}
5461
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462/* Process one rtnetlink message. */
5463
Johannes Berg2d4bc932017-04-12 14:34:04 +02005464static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
5465 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09005467 struct net *net = sock_net(skb->sk);
Florian Westphaladdf9b92017-12-02 21:44:05 +01005468 struct rtnl_link *link;
Florian Westphale4202512017-12-02 21:44:06 +01005469 struct module *owner;
Florian Westphal6853dd42017-08-09 20:41:51 +02005470 int err = -EOPNOTSUPP;
Thomas Grafe2849862007-03-22 11:48:11 -07005471 rtnl_doit_func doit;
Florian Westphal62256f92017-08-09 20:41:52 +02005472 unsigned int flags;
Alexander Kuleshov617cfc72016-01-10 21:26:57 +06005473 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 int family;
5475 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 if (type > RTM_MAX)
Thomas Graf038890f2007-04-05 14:35:52 -07005479 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480
5481 type -= RTM_BASE;
5482
5483 /* All the messages must have at least 1 byte length */
Hong zhi guo573ce262013-03-27 06:47:04 +00005484 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485 return 0;
5486
Hong zhi guo573ce262013-03-27 06:47:04 +00005487 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 kind = type&3;
5489
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07005490 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07005491 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492
Florian Westphal6853dd42017-08-09 20:41:51 +02005493 rcu_read_lock();
David S. Millerb8f3ab42011-01-18 12:40:38 -08005494 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005495 struct sock *rtnl;
Thomas Grafe2849862007-03-22 11:48:11 -07005496 rtnl_dumpit_func dumpit;
Greg Rosec7ac8672011-06-10 01:27:09 +00005497 u16 min_dump_alloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498
Florian Westphaladdf9b92017-12-02 21:44:05 +01005499 link = rtnl_get_link(family, type);
5500 if (!link || !link->dumpit) {
Florian Westphal6853dd42017-08-09 20:41:51 +02005501 family = PF_UNSPEC;
Florian Westphaladdf9b92017-12-02 21:44:05 +01005502 link = rtnl_get_link(family, type);
5503 if (!link || !link->dumpit)
Florian Westphal6853dd42017-08-09 20:41:51 +02005504 goto err_unlock;
5505 }
Florian Westphale4202512017-12-02 21:44:06 +01005506 owner = link->owner;
Florian Westphaladdf9b92017-12-02 21:44:05 +01005507 dumpit = link->dumpit;
Florian Westphale1fa6d22017-08-09 20:41:47 +02005508
Florian Westphal5c2bb9b2017-08-10 16:52:58 +02005509 if (type == RTM_GETLINK - RTM_BASE)
Florian Westphale1fa6d22017-08-09 20:41:47 +02005510 min_dump_alloc = rtnl_calcit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511
Florian Westphale4202512017-12-02 21:44:06 +01005512 err = 0;
5513 /* need to do this before rcu_read_unlock() */
5514 if (!try_module_get(owner))
5515 err = -EPROTONOSUPPORT;
5516
Florian Westphal6853dd42017-08-09 20:41:51 +02005517 rcu_read_unlock();
5518
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005519 rtnl = net->rtnl;
Florian Westphale4202512017-12-02 21:44:06 +01005520 if (err == 0) {
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00005521 struct netlink_dump_control c = {
5522 .dump = dumpit,
5523 .min_dump_alloc = min_dump_alloc,
Florian Westphale4202512017-12-02 21:44:06 +01005524 .module = owner,
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00005525 };
5526 err = netlink_dump_start(rtnl, skb, nlh, &c);
Florian Westphale4202512017-12-02 21:44:06 +01005527 /* netlink_dump_start() will keep a reference on
5528 * module if dump is still in progress.
5529 */
5530 module_put(owner);
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00005531 }
Eric Dumazet2907c352011-05-25 07:34:04 +00005532 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533 }
5534
Florian Westphaladdf9b92017-12-02 21:44:05 +01005535 link = rtnl_get_link(family, type);
5536 if (!link || !link->doit) {
Florian Westphal8caa38b2017-08-10 16:53:01 +02005537 family = PF_UNSPEC;
Florian Westphaladdf9b92017-12-02 21:44:05 +01005538 link = rtnl_get_link(PF_UNSPEC, type);
5539 if (!link || !link->doit)
5540 goto out_unlock;
Florian Westphal8caa38b2017-08-10 16:53:01 +02005541 }
5542
Florian Westphale4202512017-12-02 21:44:06 +01005543 owner = link->owner;
5544 if (!try_module_get(owner)) {
5545 err = -EPROTONOSUPPORT;
5546 goto out_unlock;
5547 }
5548
Florian Westphaladdf9b92017-12-02 21:44:05 +01005549 flags = link->flags;
Florian Westphal62256f92017-08-09 20:41:52 +02005550 if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
Florian Westphaladdf9b92017-12-02 21:44:05 +01005551 doit = link->doit;
Florian Westphal62256f92017-08-09 20:41:52 +02005552 rcu_read_unlock();
5553 if (doit)
5554 err = doit(skb, nlh, extack);
Florian Westphale4202512017-12-02 21:44:06 +01005555 module_put(owner);
Florian Westphal62256f92017-08-09 20:41:52 +02005556 return err;
5557 }
Florian Westphal6853dd42017-08-09 20:41:51 +02005558 rcu_read_unlock();
5559
Florian Westphal0cc09022017-08-09 20:41:50 +02005560 rtnl_lock();
Florian Westphaladdf9b92017-12-02 21:44:05 +01005561 link = rtnl_get_link(family, type);
5562 if (link && link->doit)
5563 err = link->doit(skb, nlh, extack);
Florian Westphal0cc09022017-08-09 20:41:50 +02005564 rtnl_unlock();
Florian Westphaladdf9b92017-12-02 21:44:05 +01005565
Florian Westphale4202512017-12-02 21:44:06 +01005566 module_put(owner);
5567
Florian Westphaladdf9b92017-12-02 21:44:05 +01005568 return err;
5569
5570out_unlock:
5571 rcu_read_unlock();
Florian Westphal0cc09022017-08-09 20:41:50 +02005572 return err;
5573
5574err_unlock:
Florian Westphal6853dd42017-08-09 20:41:51 +02005575 rcu_read_unlock();
Florian Westphal0cc09022017-08-09 20:41:50 +02005576 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577}
5578
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07005579static void rtnetlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07005581 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582}
5583
Julien Gomes5f729ea2017-06-20 13:54:16 -07005584static int rtnetlink_bind(struct net *net, int group)
5585{
5586 switch (group) {
5587 case RTNLGRP_IPV4_MROUTE_R:
5588 case RTNLGRP_IPV6_MROUTE_R:
5589 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
5590 return -EPERM;
5591 break;
5592 }
5593 return 0;
5594}
5595
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
5597{
Jiri Pirko351638e2013-05-28 01:30:21 +00005598 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02005599
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600 switch (event) {
Vlad Yasevich5138e862017-04-04 09:23:41 -04005601 case NETDEV_REBOOT:
Xin Long8a212582017-10-15 18:13:41 +08005602 case NETDEV_CHANGEMTU:
David Ahern37536542017-07-19 10:22:40 -07005603 case NETDEV_CHANGEADDR:
Vlad Yasevich5138e862017-04-04 09:23:41 -04005604 case NETDEV_CHANGENAME:
5605 case NETDEV_FEAT_CHANGE:
5606 case NETDEV_BONDING_FAILOVER:
Xin Longe6e66592017-10-15 18:13:43 +08005607 case NETDEV_POST_TYPE_CHANGE:
Vlad Yasevich5138e862017-04-04 09:23:41 -04005608 case NETDEV_NOTIFY_PEERS:
Xin Longdc709f32017-10-15 18:13:44 +08005609 case NETDEV_CHANGEUPPER:
Vlad Yasevich5138e862017-04-04 09:23:41 -04005610 case NETDEV_RESEND_IGMP:
Vlad Yasevich5138e862017-04-04 09:23:41 -04005611 case NETDEV_CHANGEINFODATA:
Xin Longeeda3fb2017-10-24 13:54:19 +08005612 case NETDEV_CHANGELOWERSTATE:
Xin Longebdcf042017-10-15 18:13:42 +08005613 case NETDEV_CHANGE_TX_QUEUE_LEN:
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04005614 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01005615 GFP_KERNEL, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 break;
5617 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618 break;
5619 }
5620 return NOTIFY_DONE;
5621}
5622
5623static struct notifier_block rtnetlink_dev_notifier = {
5624 .notifier_call = rtnetlink_event,
5625};
5626
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005627
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005628static int __net_init rtnetlink_net_init(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005629{
5630 struct sock *sk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00005631 struct netlink_kernel_cfg cfg = {
5632 .groups = RTNLGRP_MAX,
5633 .input = rtnetlink_rcv,
5634 .cb_mutex = &rtnl_mutex,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00005635 .flags = NL_CFG_F_NONROOT_RECV,
Julien Gomes5f729ea2017-06-20 13:54:16 -07005636 .bind = rtnetlink_bind,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00005637 };
5638
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00005639 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005640 if (!sk)
5641 return -ENOMEM;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005642 net->rtnl = sk;
5643 return 0;
5644}
5645
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005646static void __net_exit rtnetlink_net_exit(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005647{
Denis V. Lunev775516b2008-01-18 23:55:19 -08005648 netlink_kernel_release(net->rtnl);
5649 net->rtnl = NULL;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005650}
5651
5652static struct pernet_operations rtnetlink_net_ops = {
5653 .init = rtnetlink_net_init,
5654 .exit = rtnetlink_net_exit,
5655};
5656
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657void __init rtnetlink_init(void)
5658{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005659 if (register_pernet_subsys(&rtnetlink_net_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660 panic("rtnetlink_init: cannot initialize rtnetlink\n");
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005661
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662 register_netdevice_notifier(&rtnetlink_dev_notifier);
Thomas Graf340d17f2007-03-22 11:49:22 -07005663
Greg Rosec7ac8672011-06-10 01:27:09 +00005664 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
Florian Westphalb97bac62017-08-09 20:41:48 +02005665 rtnl_dump_ifinfo, 0);
5666 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
5667 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
5668 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
Thomas Graf687ad8c2007-03-22 11:59:42 -07005669
Florian Westphalb97bac62017-08-09 20:41:48 +02005670 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
5671 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
5672 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
John Fastabend77162022012-04-15 06:43:56 +00005673
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02005674 rtnl_register(PF_UNSPEC, RTM_NEWLINKPROP, rtnl_newlinkprop, NULL, 0);
5675 rtnl_register(PF_UNSPEC, RTM_DELLINKPROP, rtnl_dellinkprop, NULL, 0);
5676
Florian Westphalb97bac62017-08-09 20:41:48 +02005677 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
5678 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
Roopa Prabhu5b2f94b2018-12-15 22:35:08 -08005679 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0);
John Fastabende5a55a82012-10-24 08:12:57 +00005680
Florian Westphalb97bac62017-08-09 20:41:48 +02005681 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
5682 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
5683 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005684
5685 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
Florian Westphalb97bac62017-08-09 20:41:48 +02005686 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687}