blob: 09c44bf2e1d28842d77b4ed442ef2c051a25ad21 [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 ||
832 operstate == IF_OPER_UNKNOWN) &&
833 !netif_dormant(dev))
834 operstate = IF_OPER_UP;
835 break;
836
837 case IF_OPER_DORMANT:
838 if (operstate == IF_OPER_UP ||
839 operstate == IF_OPER_UNKNOWN)
840 operstate = IF_OPER_DORMANT;
841 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700842 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800843
844 if (dev->operstate != operstate) {
845 write_lock_bh(&dev_base_lock);
846 dev->operstate = operstate;
847 write_unlock_bh(&dev_base_lock);
David S. Miller93b2d4a2008-02-17 18:35:07 -0800848 netdev_state_change(dev);
849 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800850}
851
Jiri Bencb1beb682012-07-27 02:58:22 +0000852static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
853{
854 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
855 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
856}
857
Patrick McHardy3729d502010-02-26 06:34:54 +0000858static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
859 const struct ifinfomsg *ifm)
860{
861 unsigned int flags = ifm->ifi_flags;
862
863 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
864 if (ifm->ifi_change)
865 flags = (flags & ifm->ifi_change) |
Jiri Bencb1beb682012-07-27 02:58:22 +0000866 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
Patrick McHardy3729d502010-02-26 06:34:54 +0000867
868 return flags;
869}
870
Thomas Grafb60c5112006-08-04 23:05:34 -0700871static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000872 const struct rtnl_link_stats64 *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
Thomas Grafb60c5112006-08-04 23:05:34 -0700874 a->rx_packets = b->rx_packets;
875 a->tx_packets = b->tx_packets;
876 a->rx_bytes = b->rx_bytes;
877 a->tx_bytes = b->tx_bytes;
878 a->rx_errors = b->rx_errors;
879 a->tx_errors = b->tx_errors;
880 a->rx_dropped = b->rx_dropped;
881 a->tx_dropped = b->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Thomas Grafb60c5112006-08-04 23:05:34 -0700883 a->multicast = b->multicast;
884 a->collisions = b->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Thomas Grafb60c5112006-08-04 23:05:34 -0700886 a->rx_length_errors = b->rx_length_errors;
887 a->rx_over_errors = b->rx_over_errors;
888 a->rx_crc_errors = b->rx_crc_errors;
889 a->rx_frame_errors = b->rx_frame_errors;
890 a->rx_fifo_errors = b->rx_fifo_errors;
891 a->rx_missed_errors = b->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Thomas Grafb60c5112006-08-04 23:05:34 -0700893 a->tx_aborted_errors = b->tx_aborted_errors;
894 a->tx_carrier_errors = b->tx_carrier_errors;
895 a->tx_fifo_errors = b->tx_fifo_errors;
896 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
897 a->tx_window_errors = b->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Thomas Grafb60c5112006-08-04 23:05:34 -0700899 a->rx_compressed = b->rx_compressed;
900 a->tx_compressed = b->tx_compressed;
Jarod Wilson6e7333d2016-02-01 18:51:05 -0500901
902 a->rx_nohandler = b->rx_nohandler;
Jan Engelhardt10708f32010-03-11 09:57:29 +0000903}
904
Chris Wrightc02db8c2010-05-16 01:05:45 -0700905/* All VF info */
Greg Rose115c9b82012-02-21 16:54:48 -0500906static inline int rtnl_vfinfo_size(const struct net_device *dev,
907 u32 ext_filter_mask)
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000908{
Phil Sutter9af15c32017-01-18 14:04:39 +0100909 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
Chris Wrightc02db8c2010-05-16 01:05:45 -0700910 int num_vfs = dev_num_vf(dev->dev.parent);
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100911 size_t size = nla_total_size(0);
Scott Feldman045de012010-05-28 03:42:43 -0700912 size += num_vfs *
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100913 (nla_total_size(0) +
914 nla_total_size(sizeof(struct ifla_vf_mac)) +
Denis Kirjanov75345f882019-06-17 10:53:41 +0200915 nla_total_size(sizeof(struct ifla_vf_broadcast)) +
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100916 nla_total_size(sizeof(struct ifla_vf_vlan)) +
917 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
Moshe Shemesh79aab092016-09-22 12:11:15 +0300918 nla_total_size(MAX_VLAN_LIST_LEN *
919 sizeof(struct ifla_vf_vlan_info)) +
Sucheta Chakrabortyed616682014-05-22 09:59:05 -0400920 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100921 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
Jiri Benc945a3672014-08-08 16:44:32 +0200922 nla_total_size(sizeof(struct ifla_vf_rate)) +
Vlad Zolotarov01a3d792015-03-30 21:35:23 +0300923 nla_total_size(sizeof(struct ifla_vf_link_state)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300924 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100925 nla_total_size(0) + /* nest IFLA_VF_STATS */
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300926 /* IFLA_VF_STATS_RX_PACKETS */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200927 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300928 /* IFLA_VF_STATS_TX_PACKETS */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200929 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300930 /* IFLA_VF_STATS_RX_BYTES */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200931 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300932 /* IFLA_VF_STATS_TX_BYTES */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200933 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300934 /* IFLA_VF_STATS_BROADCAST */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200935 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300936 /* IFLA_VF_STATS_MULTICAST */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200937 nla_total_size_64bit(sizeof(__u64)) +
Eugenia Emantayevc5a9f6f2017-07-17 13:47:07 +0300938 /* IFLA_VF_STATS_RX_DROPPED */
939 nla_total_size_64bit(sizeof(__u64)) +
940 /* IFLA_VF_STATS_TX_DROPPED */
941 nla_total_size_64bit(sizeof(__u64)) +
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +0000942 nla_total_size(sizeof(struct ifla_vf_trust)));
Chris Wrightc02db8c2010-05-16 01:05:45 -0700943 return size;
944 } else
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000945 return 0;
946}
947
David Gibsonc53864f2014-04-24 10:22:36 +1000948static size_t rtnl_port_size(const struct net_device *dev,
949 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -0700950{
951 size_t port_size = nla_total_size(4) /* PORT_VF */
952 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
Scott Feldman57b61082010-05-17 22:49:55 -0700953 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
954 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
955 + nla_total_size(1) /* PROT_VDP_REQUEST */
956 + nla_total_size(2); /* PORT_VDP_RESPONSE */
957 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
958 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
959 + port_size;
960 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
961 + port_size;
962
David Gibsonc53864f2014-04-24 10:22:36 +1000963 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
964 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -0700965 return 0;
966 if (dev_num_vf(dev->dev.parent))
967 return port_self_size + vf_ports_size +
968 vf_port_size * dev_num_vf(dev->dev.parent);
969 else
970 return port_self_size;
971}
972
David S. Millerb5cdae32017-04-18 15:36:58 -0400973static size_t rtnl_xdp_size(void)
Brenden Blancod1fdd912016-07-19 12:16:49 -0700974{
Sabrina Dubrocab3cfaa32016-11-15 11:16:35 +0100975 size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */
Martin KaFai Lau58038692017-06-15 17:29:09 -0700976 nla_total_size(1) + /* XDP_ATTACHED */
Jakub Kicinskia25717d2018-07-11 20:36:41 -0700977 nla_total_size(4) + /* XDP_PROG_ID (or 1st mode) */
Jakub Kicinski4f91da22018-07-11 20:36:38 -0700978 nla_total_size(4); /* XDP_<mode>_PROG_ID */
Brenden Blancod1fdd912016-07-19 12:16:49 -0700979
David S. Millerb5cdae32017-04-18 15:36:58 -0400980 return xdp_size;
Brenden Blancod1fdd912016-07-19 12:16:49 -0700981}
982
Jiri Pirko88f4fb02019-09-30 11:48:17 +0200983static size_t rtnl_prop_list_size(const struct net_device *dev)
984{
985 struct netdev_name_node *name_node;
986 size_t size;
987
988 if (list_empty(&dev->name_node->list))
989 return 0;
990 size = nla_total_size(0);
991 list_for_each_entry(name_node, &dev->name_node->list, list)
992 size += nla_total_size(ALTIFNAMSIZ);
993 return size;
994}
995
Greg Rose115c9b82012-02-21 16:54:48 -0500996static noinline size_t if_nlmsg_size(const struct net_device *dev,
997 u32 ext_filter_mask)
Thomas Graf339bf982006-11-10 14:10:15 -0800998{
999 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
1000 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001001 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
Thomas Graf339bf982006-11-10 14:10:15 -08001002 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
Nicolas Dichtel270cb4d2016-04-26 10:06:16 +02001003 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
Thomas Graf339bf982006-11-10 14:10:15 -08001004 + nla_total_size(sizeof(struct rtnl_link_stats))
David S. Miller35c58452016-04-19 19:49:29 -04001005 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
Thomas Graf339bf982006-11-10 14:10:15 -08001006 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
1007 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
1008 + nla_total_size(4) /* IFLA_TXQLEN */
1009 + nla_total_size(4) /* IFLA_WEIGHT */
1010 + nla_total_size(4) /* IFLA_MTU */
1011 + nla_total_size(4) /* IFLA_LINK */
1012 + nla_total_size(4) /* IFLA_MASTER */
Jiri Pirko9a572472012-12-27 23:49:39 +00001013 + nla_total_size(1) /* IFLA_CARRIER */
Ben Greearedbc0bb2012-03-29 12:51:30 +00001014 + nla_total_size(4) /* IFLA_PROMISCUITY */
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001015 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
1016 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
Tobias Klauser69197562016-11-30 14:30:37 +01001017 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
1018 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
Thomas Graf339bf982006-11-10 14:10:15 -08001019 + nla_total_size(1) /* IFLA_OPERSTATE */
Patrick McHardy38f7b872007-06-13 12:03:51 -07001020 + nla_total_size(1) /* IFLA_LINKMODE */
david decotigny2d3b4792014-03-29 09:48:35 -07001021 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001022 + nla_total_size(4) /* IFLA_LINK_NETNSID */
Serhey Popovychdb833d42017-06-20 14:35:23 +03001023 + nla_total_size(4) /* IFLA_GROUP */
Greg Rose115c9b82012-02-21 16:54:48 -05001024 + nla_total_size(ext_filter_mask
1025 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
1026 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
David Gibsonc53864f2014-04-24 10:22:36 +10001027 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
Thomas Graff8ff1822010-11-16 04:30:14 +00001028 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
Arad, Ronenb1974ed2015-10-19 09:23:28 -07001029 + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
Jiri Pirko82f28412014-11-28 14:34:18 +01001030 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001031 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
Nicolas Dichtelc57c7a92016-03-31 18:10:31 +02001032 + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
David S. Millerb5cdae32017-04-18 15:36:58 -04001033 + rtnl_xdp_size() /* IFLA_XDP */
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001034 + nla_total_size(4) /* IFLA_EVENT */
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02001035 + nla_total_size(4) /* IFLA_NEW_NETNSID */
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01001036 + nla_total_size(4) /* IFLA_NEW_IFINDEX */
Colin Ian King03ac7382017-11-06 15:04:54 +00001037 + nla_total_size(1) /* IFLA_PROTO_DOWN */
Christian Brauner7e4a8d52018-09-04 21:53:53 +02001038 + nla_total_size(4) /* IFLA_TARGET_NETNSID */
David Decotignyb2d3bcf2018-01-18 09:59:13 -08001039 + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */
1040 + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */
Stephen Hemminger3e7a50c2018-07-27 13:43:22 -07001041 + nla_total_size(4) /* IFLA_MIN_MTU */
1042 + nla_total_size(4) /* IFLA_MAX_MTU */
Jiri Pirko88f4fb02019-09-30 11:48:17 +02001043 + rtnl_prop_list_size(dev)
Michal Kubecekf74877a2019-12-11 10:58:14 +01001044 + nla_total_size(MAX_ADDR_LEN) /* IFLA_PERM_ADDRESS */
Jiri Benc79e1ad12017-11-02 17:04:38 -02001045 + 0;
Thomas Graf339bf982006-11-10 14:10:15 -08001046}
1047
Scott Feldman57b61082010-05-17 22:49:55 -07001048static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
1049{
1050 struct nlattr *vf_ports;
1051 struct nlattr *vf_port;
1052 int vf;
1053 int err;
1054
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001055 vf_ports = nla_nest_start_noflag(skb, IFLA_VF_PORTS);
Scott Feldman57b61082010-05-17 22:49:55 -07001056 if (!vf_ports)
1057 return -EMSGSIZE;
1058
1059 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001060 vf_port = nla_nest_start_noflag(skb, IFLA_VF_PORT);
Scott Feldman8ca94182010-05-28 03:42:18 -07001061 if (!vf_port)
1062 goto nla_put_failure;
David S. Millera6574342012-04-01 20:12:00 -04001063 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
1064 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001065 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
Scott Feldman8ca94182010-05-28 03:42:18 -07001066 if (err == -EMSGSIZE)
1067 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001068 if (err) {
Scott Feldman57b61082010-05-17 22:49:55 -07001069 nla_nest_cancel(skb, vf_port);
1070 continue;
1071 }
1072 nla_nest_end(skb, vf_port);
1073 }
1074
1075 nla_nest_end(skb, vf_ports);
1076
1077 return 0;
Scott Feldman8ca94182010-05-28 03:42:18 -07001078
1079nla_put_failure:
1080 nla_nest_cancel(skb, vf_ports);
1081 return -EMSGSIZE;
Scott Feldman57b61082010-05-17 22:49:55 -07001082}
1083
1084static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
1085{
1086 struct nlattr *port_self;
1087 int err;
1088
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001089 port_self = nla_nest_start_noflag(skb, IFLA_PORT_SELF);
Scott Feldman57b61082010-05-17 22:49:55 -07001090 if (!port_self)
1091 return -EMSGSIZE;
1092
1093 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
1094 if (err) {
1095 nla_nest_cancel(skb, port_self);
Scott Feldman8ca94182010-05-28 03:42:18 -07001096 return (err == -EMSGSIZE) ? err : 0;
Scott Feldman57b61082010-05-17 22:49:55 -07001097 }
1098
1099 nla_nest_end(skb, port_self);
1100
1101 return 0;
1102}
1103
David Gibsonc53864f2014-04-24 10:22:36 +10001104static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1105 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -07001106{
1107 int err;
1108
David Gibsonc53864f2014-04-24 10:22:36 +10001109 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1110 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -07001111 return 0;
1112
1113 err = rtnl_port_self_fill(skb, dev);
1114 if (err)
1115 return err;
1116
1117 if (dev_num_vf(dev->dev.parent)) {
1118 err = rtnl_vf_ports_fill(skb, dev);
1119 if (err)
1120 return err;
1121 }
1122
1123 return 0;
1124}
1125
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001126static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
1127{
1128 int err;
Jiri Pirko02637fc2014-11-28 14:34:16 +01001129 struct netdev_phys_item_id ppid;
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001130
1131 err = dev_get_phys_port_id(dev, &ppid);
1132 if (err) {
1133 if (err == -EOPNOTSUPP)
1134 return 0;
1135 return err;
1136 }
1137
1138 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1139 return -EMSGSIZE;
1140
1141 return 0;
1142}
1143
David Aherndb24a902015-03-17 20:23:15 -06001144static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1145{
1146 char name[IFNAMSIZ];
1147 int err;
1148
1149 err = dev_get_phys_port_name(dev, name, sizeof(name));
1150 if (err) {
1151 if (err == -EOPNOTSUPP)
1152 return 0;
1153 return err;
1154 }
1155
Michal Schmidt77ef0332017-05-04 16:48:58 +02001156 if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
David Aherndb24a902015-03-17 20:23:15 -06001157 return -EMSGSIZE;
1158
1159 return 0;
1160}
1161
Jiri Pirko82f28412014-11-28 14:34:18 +01001162static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1163{
Florian Fainellibccb3022019-02-06 09:45:46 -08001164 struct netdev_phys_item_id ppid = { };
Jiri Pirko82f28412014-11-28 14:34:18 +01001165 int err;
Jiri Pirko82f28412014-11-28 14:34:18 +01001166
Florian Fainellibccb3022019-02-06 09:45:46 -08001167 err = dev_get_port_parent_id(dev, &ppid, false);
Jiri Pirko82f28412014-11-28 14:34:18 +01001168 if (err) {
1169 if (err == -EOPNOTSUPP)
1170 return 0;
1171 return err;
1172 }
1173
Florian Fainellibccb3022019-02-06 09:45:46 -08001174 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, ppid.id_len, ppid.id))
Jiri Pirko82f28412014-11-28 14:34:18 +01001175 return -EMSGSIZE;
1176
1177 return 0;
1178}
1179
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001180static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1181 struct net_device *dev)
1182{
Roopa Prabhu550bce52016-04-15 20:36:25 -07001183 struct rtnl_link_stats64 *sp;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001184 struct nlattr *attr;
1185
Nicolas Dichtel58414d32016-04-21 18:58:25 +02001186 attr = nla_reserve_64bit(skb, IFLA_STATS64,
1187 sizeof(struct rtnl_link_stats64), IFLA_PAD);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001188 if (!attr)
1189 return -EMSGSIZE;
1190
Roopa Prabhu550bce52016-04-15 20:36:25 -07001191 sp = nla_data(attr);
1192 dev_get_stats(dev, sp);
1193
1194 attr = nla_reserve(skb, IFLA_STATS,
1195 sizeof(struct rtnl_link_stats));
1196 if (!attr)
1197 return -EMSGSIZE;
1198
1199 copy_rtnl_link_stats(nla_data(attr), sp);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001200
1201 return 0;
1202}
1203
1204static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1205 struct net_device *dev,
1206 int vfs_num,
1207 struct nlattr *vfinfo)
1208{
1209 struct ifla_vf_rss_query_en vf_rss_query_en;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001210 struct nlattr *vf, *vfstats, *vfvlanlist;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001211 struct ifla_vf_link_state vf_linkstate;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001212 struct ifla_vf_vlan_info vf_vlan_info;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001213 struct ifla_vf_spoofchk vf_spoofchk;
1214 struct ifla_vf_tx_rate vf_tx_rate;
1215 struct ifla_vf_stats vf_stats;
1216 struct ifla_vf_trust vf_trust;
1217 struct ifla_vf_vlan vf_vlan;
1218 struct ifla_vf_rate vf_rate;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001219 struct ifla_vf_mac vf_mac;
Denis Kirjanov75345f882019-06-17 10:53:41 +02001220 struct ifla_vf_broadcast vf_broadcast;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001221 struct ifla_vf_info ivi;
Danit Goldberg30aad412019-11-06 15:30:07 +02001222 struct ifla_vf_guid node_guid;
1223 struct ifla_vf_guid port_guid;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001224
Mintz, Yuval0eed9cf2017-06-07 21:00:33 +03001225 memset(&ivi, 0, sizeof(ivi));
1226
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001227 /* Not all SR-IOV capable drivers support the
1228 * spoofcheck and "RSS query enable" query. Preset to
1229 * -1 so the user space tool can detect that the driver
1230 * didn't report anything.
1231 */
1232 ivi.spoofchk = -1;
1233 ivi.rss_query_en = -1;
1234 ivi.trusted = -1;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001235 /* The default value for VF link state is "auto"
1236 * IFLA_VF_LINK_STATE_AUTO which equals zero
1237 */
1238 ivi.linkstate = 0;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001239 /* VLAN Protocol by default is 802.1Q */
1240 ivi.vlan_proto = htons(ETH_P_8021Q);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001241 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1242 return 0;
1243
Dan Carpenter775f4f02016-10-13 11:45:28 +03001244 memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
Leon Romanovsky9fbf0822020-01-30 14:59:49 +02001245 memset(&node_guid, 0, sizeof(node_guid));
1246 memset(&port_guid, 0, sizeof(port_guid));
Dan Carpenter775f4f02016-10-13 11:45:28 +03001247
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001248 vf_mac.vf =
1249 vf_vlan.vf =
Moshe Shemesh79aab092016-09-22 12:11:15 +03001250 vf_vlan_info.vf =
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001251 vf_rate.vf =
1252 vf_tx_rate.vf =
1253 vf_spoofchk.vf =
1254 vf_linkstate.vf =
1255 vf_rss_query_en.vf =
Danit Goldberg9aed6ae2019-12-03 17:43:36 +02001256 vf_trust.vf =
1257 node_guid.vf =
1258 port_guid.vf = ivi.vf;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001259
1260 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
Denis Kirjanov75345f882019-06-17 10:53:41 +02001261 memcpy(vf_broadcast.broadcast, dev->broadcast, dev->addr_len);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001262 vf_vlan.vlan = ivi.vlan;
1263 vf_vlan.qos = ivi.qos;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001264 vf_vlan_info.vlan = ivi.vlan;
1265 vf_vlan_info.qos = ivi.qos;
1266 vf_vlan_info.vlan_proto = ivi.vlan_proto;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001267 vf_tx_rate.rate = ivi.max_tx_rate;
1268 vf_rate.min_tx_rate = ivi.min_tx_rate;
1269 vf_rate.max_tx_rate = ivi.max_tx_rate;
1270 vf_spoofchk.setting = ivi.spoofchk;
1271 vf_linkstate.link_state = ivi.linkstate;
1272 vf_rss_query_en.setting = ivi.rss_query_en;
1273 vf_trust.setting = ivi.trusted;
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001274 vf = nla_nest_start_noflag(skb, IFLA_VF_INFO);
Moshe Shemesh79aab092016-09-22 12:11:15 +03001275 if (!vf)
1276 goto nla_put_vfinfo_failure;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001277 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
Denis Kirjanov75345f882019-06-17 10:53:41 +02001278 nla_put(skb, IFLA_VF_BROADCAST, sizeof(vf_broadcast), &vf_broadcast) ||
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001279 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1280 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1281 &vf_rate) ||
1282 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1283 &vf_tx_rate) ||
1284 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1285 &vf_spoofchk) ||
1286 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1287 &vf_linkstate) ||
1288 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1289 sizeof(vf_rss_query_en),
1290 &vf_rss_query_en) ||
1291 nla_put(skb, IFLA_VF_TRUST,
1292 sizeof(vf_trust), &vf_trust))
Moshe Shemesh79aab092016-09-22 12:11:15 +03001293 goto nla_put_vf_failure;
Danit Goldberg30aad412019-11-06 15:30:07 +02001294
Danit Goldberg30aad412019-11-06 15:30:07 +02001295 if (dev->netdev_ops->ndo_get_vf_guid &&
1296 !dev->netdev_ops->ndo_get_vf_guid(dev, vfs_num, &node_guid,
1297 &port_guid)) {
1298 if (nla_put(skb, IFLA_VF_IB_NODE_GUID, sizeof(node_guid),
1299 &node_guid) ||
1300 nla_put(skb, IFLA_VF_IB_PORT_GUID, sizeof(port_guid),
1301 &port_guid))
1302 goto nla_put_vf_failure;
1303 }
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001304 vfvlanlist = nla_nest_start_noflag(skb, IFLA_VF_VLAN_LIST);
Moshe Shemesh79aab092016-09-22 12:11:15 +03001305 if (!vfvlanlist)
1306 goto nla_put_vf_failure;
1307 if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
1308 &vf_vlan_info)) {
1309 nla_nest_cancel(skb, vfvlanlist);
1310 goto nla_put_vf_failure;
1311 }
1312 nla_nest_end(skb, vfvlanlist);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001313 memset(&vf_stats, 0, sizeof(vf_stats));
1314 if (dev->netdev_ops->ndo_get_vf_stats)
1315 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1316 &vf_stats);
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001317 vfstats = nla_nest_start_noflag(skb, IFLA_VF_STATS);
Moshe Shemesh79aab092016-09-22 12:11:15 +03001318 if (!vfstats)
1319 goto nla_put_vf_failure;
Nicolas Dichtel343a6d82016-04-25 10:25:14 +02001320 if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1321 vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1322 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1323 vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1324 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1325 vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1326 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1327 vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1328 nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1329 vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1330 nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
Eugenia Emantayevc5a9f6f2017-07-17 13:47:07 +03001331 vf_stats.multicast, IFLA_VF_STATS_PAD) ||
1332 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED,
1333 vf_stats.rx_dropped, IFLA_VF_STATS_PAD) ||
1334 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED,
1335 vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) {
Moshe Shemesh79aab092016-09-22 12:11:15 +03001336 nla_nest_cancel(skb, vfstats);
1337 goto nla_put_vf_failure;
1338 }
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001339 nla_nest_end(skb, vfstats);
1340 nla_nest_end(skb, vf);
1341 return 0;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001342
1343nla_put_vf_failure:
1344 nla_nest_cancel(skb, vf);
1345nla_put_vfinfo_failure:
1346 nla_nest_cancel(skb, vfinfo);
1347 return -EMSGSIZE;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001348}
1349
Florian Westphal250fc3d2017-09-26 13:58:41 +02001350static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb,
1351 struct net_device *dev,
1352 u32 ext_filter_mask)
1353{
1354 struct nlattr *vfinfo;
1355 int i, num_vfs;
1356
1357 if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0))
1358 return 0;
1359
1360 num_vfs = dev_num_vf(dev->dev.parent);
1361 if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs))
1362 return -EMSGSIZE;
1363
1364 if (!dev->netdev_ops->ndo_get_vf_config)
1365 return 0;
1366
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001367 vfinfo = nla_nest_start_noflag(skb, IFLA_VFINFO_LIST);
Florian Westphal250fc3d2017-09-26 13:58:41 +02001368 if (!vfinfo)
1369 return -EMSGSIZE;
1370
1371 for (i = 0; i < num_vfs; i++) {
1372 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1373 return -EMSGSIZE;
1374 }
1375
1376 nla_nest_end(skb, vfinfo);
1377 return 0;
1378}
1379
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001380static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1381{
Kangjie Lu5f8e4472016-05-03 16:46:24 -04001382 struct rtnl_link_ifmap map;
1383
1384 memset(&map, 0, sizeof(map));
1385 map.mem_start = dev->mem_start;
1386 map.mem_end = dev->mem_end;
1387 map.base_addr = dev->base_addr;
1388 map.irq = dev->irq;
1389 map.dma = dev->dma;
1390 map.port = dev->if_port;
1391
Nicolas Dichtel270cb4d2016-04-26 10:06:16 +02001392 if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001393 return -EMSGSIZE;
1394
1395 return 0;
1396}
1397
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001398static u32 rtnl_xdp_prog_skb(struct net_device *dev)
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001399{
Martin KaFai Lau58038692017-06-15 17:29:09 -07001400 const struct bpf_prog *generic_xdp_prog;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001401
1402 ASSERT_RTNL();
1403
Martin KaFai Lau58038692017-06-15 17:29:09 -07001404 generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001405 if (!generic_xdp_prog)
1406 return 0;
1407 return generic_xdp_prog->aux->id;
1408}
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001409
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001410static u32 rtnl_xdp_prog_drv(struct net_device *dev)
1411{
1412 return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf, XDP_QUERY_PROG);
1413}
Jakub Kicinski118b4aa2017-12-01 15:08:55 -08001414
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001415static u32 rtnl_xdp_prog_hw(struct net_device *dev)
1416{
1417 return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf,
1418 XDP_QUERY_PROG_HW);
1419}
1420
1421static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev,
1422 u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr,
1423 u32 (*get_prog_id)(struct net_device *dev))
1424{
1425 u32 curr_id;
1426 int err;
1427
1428 curr_id = get_prog_id(dev);
1429 if (!curr_id)
1430 return 0;
1431
1432 *prog_id = curr_id;
1433 err = nla_put_u32(skb, attr, curr_id);
1434 if (err)
1435 return err;
1436
1437 if (*mode != XDP_ATTACHED_NONE)
1438 *mode = XDP_ATTACHED_MULTI;
1439 else
1440 *mode = tgt_mode;
1441
1442 return 0;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001443}
1444
Brenden Blancod1fdd912016-07-19 12:16:49 -07001445static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1446{
Brenden Blancod1fdd912016-07-19 12:16:49 -07001447 struct nlattr *xdp;
Martin KaFai Lau58038692017-06-15 17:29:09 -07001448 u32 prog_id;
Brenden Blancod1fdd912016-07-19 12:16:49 -07001449 int err;
Jakub Kicinski4f91da22018-07-11 20:36:38 -07001450 u8 mode;
Brenden Blancod1fdd912016-07-19 12:16:49 -07001451
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001452 xdp = nla_nest_start_noflag(skb, IFLA_XDP);
Brenden Blancod1fdd912016-07-19 12:16:49 -07001453 if (!xdp)
1454 return -EMSGSIZE;
David S. Millerb5cdae32017-04-18 15:36:58 -04001455
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001456 prog_id = 0;
1457 mode = XDP_ATTACHED_NONE;
Jakub Kicinski202aabe2018-07-16 19:08:50 -07001458 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB,
1459 IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb);
1460 if (err)
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001461 goto err_cancel;
Jakub Kicinski202aabe2018-07-16 19:08:50 -07001462 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV,
1463 IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv);
1464 if (err)
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001465 goto err_cancel;
Jakub Kicinski202aabe2018-07-16 19:08:50 -07001466 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW,
1467 IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw);
1468 if (err)
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001469 goto err_cancel;
1470
Jakub Kicinski4f91da22018-07-11 20:36:38 -07001471 err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode);
Brenden Blancod1fdd912016-07-19 12:16:49 -07001472 if (err)
1473 goto err_cancel;
1474
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001475 if (prog_id && mode != XDP_ATTACHED_MULTI) {
Martin KaFai Lau58038692017-06-15 17:29:09 -07001476 err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
1477 if (err)
1478 goto err_cancel;
1479 }
1480
Brenden Blancod1fdd912016-07-19 12:16:49 -07001481 nla_nest_end(skb, xdp);
1482 return 0;
1483
1484err_cancel:
1485 nla_nest_cancel(skb, xdp);
1486 return err;
1487}
1488
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001489static u32 rtnl_get_event(unsigned long event)
1490{
1491 u32 rtnl_event_type = IFLA_EVENT_NONE;
1492
1493 switch (event) {
1494 case NETDEV_REBOOT:
1495 rtnl_event_type = IFLA_EVENT_REBOOT;
1496 break;
1497 case NETDEV_FEAT_CHANGE:
1498 rtnl_event_type = IFLA_EVENT_FEATURES;
1499 break;
1500 case NETDEV_BONDING_FAILOVER:
1501 rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
1502 break;
1503 case NETDEV_NOTIFY_PEERS:
1504 rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
1505 break;
1506 case NETDEV_RESEND_IGMP:
1507 rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
1508 break;
1509 case NETDEV_CHANGEINFODATA:
1510 rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
1511 break;
1512 default:
1513 break;
1514 }
1515
1516 return rtnl_event_type;
1517}
1518
Florian Westphal79110a02017-09-26 13:58:40 +02001519static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
1520{
1521 const struct net_device *upper_dev;
1522 int ret = 0;
1523
1524 rcu_read_lock();
1525
1526 upper_dev = netdev_master_upper_dev_get_rcu(dev);
1527 if (upper_dev)
1528 ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);
1529
1530 rcu_read_unlock();
1531 return ret;
1532}
1533
Sabrina Dubrocafeadc4b2019-05-14 15:12:19 +02001534static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev,
1535 bool force)
Florian Westphal79110a02017-09-26 13:58:40 +02001536{
1537 int ifindex = dev_get_iflink(dev);
1538
Sabrina Dubrocafeadc4b2019-05-14 15:12:19 +02001539 if (force || dev->ifindex != ifindex)
1540 return nla_put_u32(skb, IFLA_LINK, ifindex);
Florian Westphal79110a02017-09-26 13:58:40 +02001541
Sabrina Dubrocafeadc4b2019-05-14 15:12:19 +02001542 return 0;
Florian Westphal79110a02017-09-26 13:58:40 +02001543}
1544
Florian Westphal6c557002017-10-02 23:50:05 +02001545static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
1546 struct net_device *dev)
1547{
1548 char buf[IFALIASZ];
1549 int ret;
1550
1551 ret = dev_get_alias(dev, buf, sizeof(buf));
1552 return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
1553}
1554
Florian Westphalb1e66b92017-09-26 13:58:42 +02001555static int rtnl_fill_link_netnsid(struct sk_buff *skb,
Jiri Benc79e1ad12017-11-02 17:04:38 -02001556 const struct net_device *dev,
Guillaume Naultd4e4fdf2019-10-23 18:39:04 +02001557 struct net *src_net, gfp_t gfp)
Florian Westphalb1e66b92017-09-26 13:58:42 +02001558{
Sabrina Dubrocafeadc4b2019-05-14 15:12:19 +02001559 bool put_iflink = false;
1560
Florian Westphalb1e66b92017-09-26 13:58:42 +02001561 if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
1562 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1563
1564 if (!net_eq(dev_net(dev), link_net)) {
Guillaume Naultd4e4fdf2019-10-23 18:39:04 +02001565 int id = peernet2id_alloc(src_net, link_net, gfp);
Florian Westphalb1e66b92017-09-26 13:58:42 +02001566
1567 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1568 return -EMSGSIZE;
Sabrina Dubrocafeadc4b2019-05-14 15:12:19 +02001569
1570 put_iflink = true;
Florian Westphalb1e66b92017-09-26 13:58:42 +02001571 }
1572 }
1573
Sabrina Dubrocafeadc4b2019-05-14 15:12:19 +02001574 return nla_put_iflink(skb, dev, put_iflink);
Florian Westphalb1e66b92017-09-26 13:58:42 +02001575}
1576
Florian Westphal070cbf5b2017-10-16 15:44:35 +02001577static int rtnl_fill_link_af(struct sk_buff *skb,
1578 const struct net_device *dev,
1579 u32 ext_filter_mask)
1580{
1581 const struct rtnl_af_ops *af_ops;
1582 struct nlattr *af_spec;
1583
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001584 af_spec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
Florian Westphal070cbf5b2017-10-16 15:44:35 +02001585 if (!af_spec)
1586 return -EMSGSIZE;
1587
Florian Westphal5fa85a02017-10-16 15:44:36 +02001588 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Florian Westphal070cbf5b2017-10-16 15:44:35 +02001589 struct nlattr *af;
1590 int err;
1591
1592 if (!af_ops->fill_link_af)
1593 continue;
1594
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001595 af = nla_nest_start_noflag(skb, af_ops->family);
Florian Westphal070cbf5b2017-10-16 15:44:35 +02001596 if (!af)
1597 return -EMSGSIZE;
1598
1599 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1600 /*
1601 * Caller may return ENODATA to indicate that there
1602 * was no data to be dumped. This is not an error, it
1603 * means we should trim the attribute header and
1604 * continue.
1605 */
1606 if (err == -ENODATA)
1607 nla_nest_cancel(skb, af);
1608 else if (err < 0)
1609 return -EMSGSIZE;
1610
1611 nla_nest_end(skb, af);
1612 }
1613
1614 nla_nest_end(skb, af_spec);
1615 return 0;
1616}
1617
Jiri Pirko88f4fb02019-09-30 11:48:17 +02001618static int rtnl_fill_alt_ifnames(struct sk_buff *skb,
1619 const struct net_device *dev)
1620{
1621 struct netdev_name_node *name_node;
1622 int count = 0;
1623
1624 list_for_each_entry(name_node, &dev->name_node->list, list) {
1625 if (nla_put_string(skb, IFLA_ALT_IFNAME, name_node->name))
1626 return -EMSGSIZE;
1627 count++;
1628 }
1629 return count;
1630}
1631
1632static int rtnl_fill_prop_list(struct sk_buff *skb,
1633 const struct net_device *dev)
1634{
1635 struct nlattr *prop_list;
1636 int ret;
1637
1638 prop_list = nla_nest_start(skb, IFLA_PROP_LIST);
1639 if (!prop_list)
1640 return -EMSGSIZE;
1641
1642 ret = rtnl_fill_alt_ifnames(skb, dev);
1643 if (ret <= 0)
1644 goto nest_cancel;
1645
1646 nla_nest_end(skb, prop_list);
1647 return 0;
1648
1649nest_cancel:
1650 nla_nest_cancel(skb, prop_list);
1651 return ret;
1652}
1653
Jiri Benc79e1ad12017-11-02 17:04:38 -02001654static int rtnl_fill_ifinfo(struct sk_buff *skb,
1655 struct net_device *dev, struct net *src_net,
Patrick McHardy575c3e22007-05-22 17:00:49 -07001656 int type, u32 pid, u32 seq, u32 change,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001657 unsigned int flags, u32 ext_filter_mask,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01001658 u32 event, int *new_nsid, int new_ifindex,
Guillaume Naultd4e4fdf2019-10-23 18:39:04 +02001659 int tgt_netnsid, gfp_t gfp)
Thomas Grafb60c5112006-08-04 23:05:34 -07001660{
1661 struct ifinfomsg *ifm;
1662 struct nlmsghdr *nlh;
1663
Eric Dumazet2907c352011-05-25 07:34:04 +00001664 ASSERT_RTNL();
Thomas Grafb60c5112006-08-04 23:05:34 -07001665 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1666 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08001667 return -EMSGSIZE;
Thomas Grafb60c5112006-08-04 23:05:34 -07001668
1669 ifm = nlmsg_data(nlh);
1670 ifm->ifi_family = AF_UNSPEC;
1671 ifm->__ifi_pad = 0;
1672 ifm->ifi_type = dev->type;
1673 ifm->ifi_index = dev->ifindex;
1674 ifm->ifi_flags = dev_get_flags(dev);
1675 ifm->ifi_change = change;
1676
Christian Brauner7e4a8d52018-09-04 21:53:53 +02001677 if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid))
Jiri Benc79e1ad12017-11-02 17:04:38 -02001678 goto nla_put_failure;
1679
David S. Millera6574342012-04-01 20:12:00 -04001680 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1681 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1682 nla_put_u8(skb, IFLA_OPERSTATE,
1683 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1684 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1685 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
Stephen Hemminger3e7a50c2018-07-27 13:43:22 -07001686 nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) ||
1687 nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) ||
David S. Millera6574342012-04-01 20:12:00 -04001688 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
Ben Greearedbc0bb2012-03-29 12:51:30 +00001689 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001690 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
Eric Dumazetc70ce022016-03-21 09:55:10 -07001691 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1692 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001693#ifdef CONFIG_RPS
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001694 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001695#endif
Florian Westphal79110a02017-09-26 13:58:40 +02001696 put_master_ifindex(skb, dev) ||
Jiri Pirko9a572472012-12-27 23:49:39 +00001697 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
David S. Millera6574342012-04-01 20:12:00 -04001698 (dev->qdisc &&
1699 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
Florian Westphal6c557002017-10-02 23:50:05 +02001700 nla_put_ifalias(skb, dev) ||
david decotigny2d3b4792014-03-29 09:48:35 -07001701 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
David Decotignyb2d3bcf2018-01-18 09:59:13 -08001702 atomic_read(&dev->carrier_up_count) +
1703 atomic_read(&dev->carrier_down_count)) ||
1704 nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down) ||
1705 nla_put_u32(skb, IFLA_CARRIER_UP_COUNT,
1706 atomic_read(&dev->carrier_up_count)) ||
1707 nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT,
1708 atomic_read(&dev->carrier_down_count)))
David S. Millera6574342012-04-01 20:12:00 -04001709 goto nla_put_failure;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001710
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001711 if (event != IFLA_EVENT_NONE) {
1712 if (nla_put_u32(skb, IFLA_EVENT, event))
1713 goto nla_put_failure;
1714 }
1715
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001716 if (rtnl_fill_link_ifmap(skb, dev))
1717 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
1719 if (dev->addr_len) {
David S. Millera6574342012-04-01 20:12:00 -04001720 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1721 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1722 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 }
1724
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001725 if (rtnl_phys_port_id_fill(skb, dev))
1726 goto nla_put_failure;
1727
David Aherndb24a902015-03-17 20:23:15 -06001728 if (rtnl_phys_port_name_fill(skb, dev))
1729 goto nla_put_failure;
1730
Jiri Pirko82f28412014-11-28 14:34:18 +01001731 if (rtnl_phys_switch_id_fill(skb, dev))
1732 goto nla_put_failure;
1733
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001734 if (rtnl_fill_stats(skb, dev))
Pavel Emelyanov96e74082008-05-21 14:12:46 -07001735 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Florian Westphal250fc3d2017-09-26 13:58:41 +02001737 if (rtnl_fill_vf(skb, dev, ext_filter_mask))
David S. Millera6574342012-04-01 20:12:00 -04001738 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001739
David Gibsonc53864f2014-04-24 10:22:36 +10001740 if (rtnl_port_fill(skb, dev, ext_filter_mask))
Scott Feldman57b61082010-05-17 22:49:55 -07001741 goto nla_put_failure;
1742
Brenden Blancod1fdd912016-07-19 12:16:49 -07001743 if (rtnl_xdp_fill(skb, dev))
1744 goto nla_put_failure;
1745
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001746 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07001747 if (rtnl_link_fill(skb, dev) < 0)
1748 goto nla_put_failure;
1749 }
1750
Guillaume Naultd4e4fdf2019-10-23 18:39:04 +02001751 if (rtnl_fill_link_netnsid(skb, dev, src_net, gfp))
Florian Westphalb1e66b92017-09-26 13:58:42 +02001752 goto nla_put_failure;
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001753
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02001754 if (new_nsid &&
1755 nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0)
1756 goto nla_put_failure;
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01001757 if (new_ifindex &&
1758 nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0)
1759 goto nla_put_failure;
1760
Michal Kubecekf74877a2019-12-11 10:58:14 +01001761 if (memchr_inv(dev->perm_addr, '\0', dev->addr_len) &&
1762 nla_put(skb, IFLA_PERM_ADDRESS, dev->addr_len, dev->perm_addr))
1763 goto nla_put_failure;
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02001764
Florian Westphal5fa85a02017-10-16 15:44:36 +02001765 rcu_read_lock();
Florian Westphal070cbf5b2017-10-16 15:44:35 +02001766 if (rtnl_fill_link_af(skb, dev, ext_filter_mask))
Florian Westphal5fa85a02017-10-16 15:44:36 +02001767 goto nla_put_failure_rcu;
1768 rcu_read_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +00001769
Jiri Pirko88f4fb02019-09-30 11:48:17 +02001770 if (rtnl_fill_prop_list(skb, dev))
1771 goto nla_put_failure;
1772
Johannes Berg053c0952015-01-16 22:09:00 +01001773 nlmsg_end(skb, nlh);
1774 return 0;
Thomas Grafb60c5112006-08-04 23:05:34 -07001775
Florian Westphal5fa85a02017-10-16 15:44:36 +02001776nla_put_failure_rcu:
1777 rcu_read_unlock();
Thomas Grafb60c5112006-08-04 23:05:34 -07001778nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001779 nlmsg_cancel(skb, nlh);
1780 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781}
1782
Jiri Pirkof7b12602014-02-18 20:53:18 +01001783static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1784 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1785 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1786 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1787 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1788 [IFLA_MTU] = { .type = NLA_U32 },
1789 [IFLA_LINK] = { .type = NLA_U32 },
1790 [IFLA_MASTER] = { .type = NLA_U32 },
1791 [IFLA_CARRIER] = { .type = NLA_U8 },
1792 [IFLA_TXQLEN] = { .type = NLA_U32 },
1793 [IFLA_WEIGHT] = { .type = NLA_U32 },
1794 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1795 [IFLA_LINKMODE] = { .type = NLA_U8 },
1796 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1797 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1798 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
Nicolas Dichtel2459b4c2017-10-11 16:24:48 +02001799 /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
1800 * allow 0-length string (needed to remove an alias).
1801 */
1802 [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001803 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1804 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1805 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1806 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1807 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1808 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1809 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1810 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
Stephen Hemminger46e6b992017-12-07 15:40:19 -08001811 [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 },
1812 [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 },
Jiri Pirko02637fc2014-11-28 14:34:16 +01001813 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
david decotigny2d3b4792014-03-29 09:48:35 -07001814 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
Jiri Pirko82f28412014-11-28 14:34:18 +01001815 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
Nicolas Dichtel317f4812015-01-15 15:11:18 +01001816 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001817 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001818 [IFLA_XDP] = { .type = NLA_NESTED },
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001819 [IFLA_EVENT] = { .type = NLA_U32 },
Serhey Popovychdb833d42017-06-20 14:35:23 +03001820 [IFLA_GROUP] = { .type = NLA_U32 },
Christian Brauner7e4a8d52018-09-04 21:53:53 +02001821 [IFLA_TARGET_NETNSID] = { .type = NLA_S32 },
David Decotignyb2d3bcf2018-01-18 09:59:13 -08001822 [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 },
1823 [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 },
Stephen Hemminger3e7a50c2018-07-27 13:43:22 -07001824 [IFLA_MIN_MTU] = { .type = NLA_U32 },
1825 [IFLA_MAX_MTU] = { .type = NLA_U32 },
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02001826 [IFLA_PROP_LIST] = { .type = NLA_NESTED },
1827 [IFLA_ALT_IFNAME] = { .type = NLA_STRING,
1828 .len = ALTIFNAMSIZ - 1 },
Michal Kubecekf74877a2019-12-11 10:58:14 +01001829 [IFLA_PERM_ADDRESS] = { .type = NLA_REJECT },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001830};
1831
1832static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1833 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1834 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1835 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1836 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1837};
1838
Jiri Pirkof7b12602014-02-18 20:53:18 +01001839static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
Daniel Borkmann364d5712015-02-05 18:44:04 +01001840 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
Denis Kirjanov75345f882019-06-17 10:53:41 +02001841 [IFLA_VF_BROADCAST] = { .type = NLA_REJECT },
Daniel Borkmann364d5712015-02-05 18:44:04 +01001842 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
Moshe Shemesh79aab092016-09-22 12:11:15 +03001843 [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED },
Daniel Borkmann364d5712015-02-05 18:44:04 +01001844 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1845 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1846 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1847 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001848 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001849 [IFLA_VF_STATS] = { .type = NLA_NESTED },
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00001850 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
Eli Cohencc8e27c2016-03-11 22:58:34 +02001851 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1852 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001853};
1854
Jiri Pirkof7b12602014-02-18 20:53:18 +01001855static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1856 [IFLA_PORT_VF] = { .type = NLA_U32 },
1857 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1858 .len = PORT_PROFILE_MAX },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001859 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1860 .len = PORT_UUID_MAX },
1861 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1862 .len = PORT_UUID_MAX },
1863 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1864 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
Daniel Borkmann025331d2017-02-17 01:56:11 +01001865
1866 /* Unused, but we need to keep it here since user space could
1867 * fill it. It's also broken with regard to NLA_BINARY use in
1868 * combination with structs.
1869 */
1870 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1871 .len = sizeof(struct ifla_port_vsi) },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001872};
1873
Brenden Blancod1fdd912016-07-19 12:16:49 -07001874static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
1875 [IFLA_XDP_FD] = { .type = NLA_S32 },
1876 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 },
Daniel Borkmann85de8572016-11-28 23:16:54 +01001877 [IFLA_XDP_FLAGS] = { .type = NLA_U32 },
Martin KaFai Lau58038692017-06-15 17:29:09 -07001878 [IFLA_XDP_PROG_ID] = { .type = NLA_U32 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001879};
1880
David Aherndc599f72016-02-02 08:17:07 -08001881static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1882{
1883 const struct rtnl_link_ops *ops = NULL;
1884 struct nlattr *linfo[IFLA_INFO_MAX + 1];
1885
Johannes Berg8cb08172019-04-26 14:07:28 +02001886 if (nla_parse_nested_deprecated(linfo, IFLA_INFO_MAX, nla, ifla_info_policy, NULL) < 0)
David Aherndc599f72016-02-02 08:17:07 -08001887 return NULL;
1888
1889 if (linfo[IFLA_INFO_KIND]) {
1890 char kind[MODULE_NAME_LEN];
1891
1892 nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1893 ops = rtnl_link_ops_get(kind);
1894 }
1895
1896 return ops;
1897}
1898
1899static bool link_master_filtered(struct net_device *dev, int master_idx)
1900{
1901 struct net_device *master;
1902
1903 if (!master_idx)
1904 return false;
1905
1906 master = netdev_master_upper_dev_get(dev);
1907 if (!master || master->ifindex != master_idx)
1908 return true;
1909
1910 return false;
1911}
1912
1913static bool link_kind_filtered(const struct net_device *dev,
1914 const struct rtnl_link_ops *kind_ops)
1915{
1916 if (kind_ops && dev->rtnl_link_ops != kind_ops)
1917 return true;
1918
1919 return false;
1920}
1921
1922static bool link_dump_filtered(struct net_device *dev,
1923 int master_idx,
1924 const struct rtnl_link_ops *kind_ops)
1925{
1926 if (link_master_filtered(dev, master_idx) ||
1927 link_kind_filtered(dev, kind_ops))
1928 return true;
1929
1930 return false;
1931}
1932
Christian Braunerc383edc2018-09-04 21:53:47 +02001933/**
1934 * rtnl_get_net_ns_capable - Get netns if sufficiently privileged.
1935 * @sk: netlink socket
1936 * @netnsid: network namespace identifier
1937 *
1938 * Returns the network namespace identified by netnsid on success or an error
1939 * pointer on failure.
1940 */
1941struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid)
Jiri Benc79e1ad12017-11-02 17:04:38 -02001942{
1943 struct net *net;
1944
Andrei Vaginf428fe42018-01-02 23:27:33 -08001945 net = get_net_ns_by_id(sock_net(sk), netnsid);
Jiri Benc79e1ad12017-11-02 17:04:38 -02001946 if (!net)
1947 return ERR_PTR(-EINVAL);
1948
1949 /* For now, the caller is required to have CAP_NET_ADMIN in
1950 * the user namespace owning the target net ns.
1951 */
Andrei Vaginf428fe42018-01-02 23:27:33 -08001952 if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) {
Jiri Benc79e1ad12017-11-02 17:04:38 -02001953 put_net(net);
1954 return ERR_PTR(-EACCES);
1955 }
1956 return net;
1957}
Christian Braunerc383edc2018-09-04 21:53:47 +02001958EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable);
Jiri Benc79e1ad12017-11-02 17:04:38 -02001959
David Ahern905cf0a2018-10-07 20:16:30 -07001960static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh,
1961 bool strict_check, struct nlattr **tb,
1962 struct netlink_ext_ack *extack)
1963{
1964 int hdrlen;
1965
1966 if (strict_check) {
1967 struct ifinfomsg *ifm;
1968
1969 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
1970 NL_SET_ERR_MSG(extack, "Invalid header for link dump");
1971 return -EINVAL;
1972 }
1973
1974 ifm = nlmsg_data(nlh);
1975 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
1976 ifm->ifi_change) {
1977 NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request");
1978 return -EINVAL;
1979 }
1980 if (ifm->ifi_index) {
1981 NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps");
1982 return -EINVAL;
1983 }
1984
Johannes Berg8cb08172019-04-26 14:07:28 +02001985 return nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb,
1986 IFLA_MAX, ifla_policy,
1987 extack);
David Ahern905cf0a2018-10-07 20:16:30 -07001988 }
1989
1990 /* A hack to preserve kernel<->userspace interface.
1991 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1992 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1993 * what iproute2 < v3.9.0 used.
1994 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1995 * attribute, its netlink message is shorter than struct ifinfomsg.
1996 */
1997 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
1998 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1999
Johannes Berg8cb08172019-04-26 14:07:28 +02002000 return nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy,
2001 extack);
David Ahern905cf0a2018-10-07 20:16:30 -07002002}
2003
Thomas Grafb60c5112006-08-04 23:05:34 -07002004static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005{
David Ahern905cf0a2018-10-07 20:16:30 -07002006 struct netlink_ext_ack *extack = cb->extack;
2007 const struct nlmsghdr *nlh = cb->nlh;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002008 struct net *net = sock_net(skb->sk);
Jiri Benc79e1ad12017-11-02 17:04:38 -02002009 struct net *tgt_net = net;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002010 int h, s_h;
2011 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 struct net_device *dev;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002013 struct hlist_head *head;
Greg Rose115c9b82012-02-21 16:54:48 -05002014 struct nlattr *tb[IFLA_MAX+1];
2015 u32 ext_filter_mask = 0;
David Aherndc599f72016-02-02 08:17:07 -08002016 const struct rtnl_link_ops *kind_ops = NULL;
2017 unsigned int flags = NLM_F_MULTI;
2018 int master_idx = 0;
Jiri Benc79e1ad12017-11-02 17:04:38 -02002019 int netnsid = -1;
David Ahern905cf0a2018-10-07 20:16:30 -07002020 int err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002022 s_h = cb->args[0];
2023 s_idx = cb->args[1];
2024
David Ahern905cf0a2018-10-07 20:16:30 -07002025 err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack);
2026 if (err < 0) {
2027 if (cb->strict_check)
2028 return err;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02002029
David Ahern905cf0a2018-10-07 20:16:30 -07002030 goto walk_entries;
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00002031 }
Greg Rose115c9b82012-02-21 16:54:48 -05002032
David Ahern905cf0a2018-10-07 20:16:30 -07002033 for (i = 0; i <= IFLA_MAX; ++i) {
2034 if (!tb[i])
2035 continue;
2036
2037 /* new attributes should only be added with strict checking */
2038 switch (i) {
2039 case IFLA_TARGET_NETNSID:
2040 netnsid = nla_get_s32(tb[i]);
2041 tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid);
2042 if (IS_ERR(tgt_net)) {
2043 NL_SET_ERR_MSG(extack, "Invalid target network namespace id");
2044 return PTR_ERR(tgt_net);
2045 }
2046 break;
2047 case IFLA_EXT_MASK:
2048 ext_filter_mask = nla_get_u32(tb[i]);
2049 break;
2050 case IFLA_MASTER:
2051 master_idx = nla_get_u32(tb[i]);
2052 break;
2053 case IFLA_LINKINFO:
2054 kind_ops = linkinfo_to_kind_ops(tb[i]);
2055 break;
2056 default:
2057 if (cb->strict_check) {
2058 NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request");
2059 return -EINVAL;
2060 }
2061 }
2062 }
2063
2064 if (master_idx || kind_ops)
2065 flags |= NLM_F_DUMP_FILTERED;
2066
2067walk_entries:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002068 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
2069 idx = 0;
Jiri Benc79e1ad12017-11-02 17:04:38 -02002070 head = &tgt_net->dev_index_head[h];
Eric Dumazetcac5e652015-02-27 09:42:50 -08002071 hlist_for_each_entry(dev, head, index_hlist) {
David Aherndc599f72016-02-02 08:17:07 -08002072 if (link_dump_filtered(dev, master_idx, kind_ops))
Zhang Shengju3f0ae052016-11-19 23:28:32 +08002073 goto cont;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002074 if (idx < s_idx)
2075 goto cont;
Jiri Benc79e1ad12017-11-02 17:04:38 -02002076 err = rtnl_fill_ifinfo(skb, dev, net,
2077 RTM_NEWLINK,
David Gibson973462b2014-04-24 10:22:35 +10002078 NETLINK_CB(cb->skb).portid,
David Ahern905cf0a2018-10-07 20:16:30 -07002079 nlh->nlmsg_seq, 0, flags,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01002080 ext_filter_mask, 0, NULL, 0,
Guillaume Naultd4e4fdf2019-10-23 18:39:04 +02002081 netnsid, GFP_KERNEL);
David Gibson973462b2014-04-24 10:22:35 +10002082
David Ahernf6c57752017-05-15 23:19:17 -07002083 if (err < 0) {
2084 if (likely(skb->len))
2085 goto out;
2086
2087 goto out_err;
2088 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07002089cont:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002090 idx++;
2091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 }
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002093out:
David Ahernf6c57752017-05-15 23:19:17 -07002094 err = skb->len;
2095out_err:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002096 cb->args[1] = idx;
2097 cb->args[0] = h;
Jakub Sitnickid0225782017-08-09 17:39:12 +02002098 cb->seq = net->dev_base_seq;
2099 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Jiri Benc79e1ad12017-11-02 17:04:38 -02002100 if (netnsid >= 0)
2101 put_net(tgt_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
David Ahernf6c57752017-05-15 23:19:17 -07002103 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104}
2105
Johannes Bergfceb6432017-04-12 14:34:07 +02002106int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
2107 struct netlink_ext_ack *exterr)
Jiri Pirkof7b12602014-02-18 20:53:18 +01002108{
Johannes Berg8cb08172019-04-26 14:07:28 +02002109 return nla_parse_deprecated(tb, IFLA_MAX, head, len, ifla_policy,
2110 exterr);
Jiri Pirkof7b12602014-02-18 20:53:18 +01002111}
2112EXPORT_SYMBOL(rtnl_nla_parse_ifla);
Scott Feldman57b61082010-05-17 22:49:55 -07002113
Eric W. Biederman81adee42009-11-08 00:53:51 -08002114struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
2115{
2116 struct net *net;
2117 /* Examine the link attributes and figure out which
2118 * network namespace we are talking about.
2119 */
2120 if (tb[IFLA_NET_NS_PID])
2121 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
Eric W. Biedermanf0630522011-05-04 17:51:50 -07002122 else if (tb[IFLA_NET_NS_FD])
2123 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
Eric W. Biederman81adee42009-11-08 00:53:51 -08002124 else
2125 net = get_net(src_net);
2126 return net;
2127}
2128EXPORT_SYMBOL(rtnl_link_get_net);
2129
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002130/* Figure out which network namespace we are talking about by
2131 * examining the link attributes in the following order:
2132 *
2133 * 1. IFLA_NET_NS_PID
2134 * 2. IFLA_NET_NS_FD
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002135 * 3. IFLA_TARGET_NETNSID
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002136 */
2137static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net,
2138 struct nlattr *tb[])
2139{
2140 struct net *net;
2141
2142 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])
2143 return rtnl_link_get_net(src_net, tb);
2144
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002145 if (!tb[IFLA_TARGET_NETNSID])
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002146 return get_net(src_net);
2147
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002148 net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID]));
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002149 if (!net)
2150 return ERR_PTR(-EINVAL);
2151
2152 return net;
2153}
2154
2155static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb,
2156 struct net *src_net,
2157 struct nlattr *tb[], int cap)
2158{
2159 struct net *net;
2160
2161 net = rtnl_link_get_net_by_nlattr(src_net, tb);
2162 if (IS_ERR(net))
2163 return net;
2164
2165 if (!netlink_ns_capable(skb, net->user_ns, cap)) {
2166 put_net(net);
2167 return ERR_PTR(-EPERM);
2168 }
2169
2170 return net;
2171}
2172
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002173/* Verify that rtnetlink requests do not pass additional properties
2174 * potentially referring to different network namespaces.
2175 */
2176static int rtnl_ensure_unique_netns(struct nlattr *tb[],
2177 struct netlink_ext_ack *extack,
2178 bool netns_id_only)
2179{
2180
2181 if (netns_id_only) {
2182 if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD])
2183 return 0;
2184
2185 NL_SET_ERR_MSG(extack, "specified netns attribute not supported");
2186 return -EOPNOTSUPP;
2187 }
2188
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002189 if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]))
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002190 goto invalid_attr;
2191
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002192 if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD]))
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002193 goto invalid_attr;
2194
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002195 if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID]))
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002196 goto invalid_attr;
2197
2198 return 0;
2199
2200invalid_attr:
2201 NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified");
2202 return -EINVAL;
2203}
2204
Thomas Graf1840bb12008-02-23 19:54:36 -08002205static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
2206{
2207 if (dev) {
2208 if (tb[IFLA_ADDRESS] &&
2209 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
2210 return -EINVAL;
2211
2212 if (tb[IFLA_BROADCAST] &&
2213 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
2214 return -EINVAL;
2215 }
2216
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002217 if (tb[IFLA_AF_SPEC]) {
2218 struct nlattr *af;
2219 int rem, err;
2220
2221 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2222 const struct rtnl_af_ops *af_ops;
2223
Florian Westphal5fa85a02017-10-16 15:44:36 +02002224 rcu_read_lock();
2225 af_ops = rtnl_af_lookup(nla_type(af));
2226 if (!af_ops) {
2227 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002228 return -EAFNOSUPPORT;
Florian Westphal5fa85a02017-10-16 15:44:36 +02002229 }
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002230
Florian Westphal5fa85a02017-10-16 15:44:36 +02002231 if (!af_ops->set_link_af) {
2232 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002233 return -EOPNOTSUPP;
Florian Westphal5fa85a02017-10-16 15:44:36 +02002234 }
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002235
2236 if (af_ops->validate_link_af) {
Kurt Van Dijck6d3a9a62011-01-26 04:55:24 +00002237 err = af_ops->validate_link_af(dev, af);
Florian Westphal5fa85a02017-10-16 15:44:36 +02002238 if (err < 0) {
2239 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002240 return err;
Florian Westphal5fa85a02017-10-16 15:44:36 +02002241 }
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002242 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02002243
2244 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002245 }
2246 }
2247
Thomas Graf1840bb12008-02-23 19:54:36 -08002248 return 0;
2249}
2250
Eli Cohencc8e27c2016-03-11 22:58:34 +02002251static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
2252 int guid_type)
2253{
2254 const struct net_device_ops *ops = dev->netdev_ops;
2255
2256 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
2257}
2258
2259static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
2260{
2261 if (dev->type != ARPHRD_INFINIBAND)
2262 return -EOPNOTSUPP;
2263
2264 return handle_infiniband_guid(dev, ivt, guid_type);
2265}
2266
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002267static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
Chris Wrightc02db8c2010-05-16 01:05:45 -07002268{
Chris Wrightc02db8c2010-05-16 01:05:45 -07002269 const struct net_device_ops *ops = dev->netdev_ops;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002270 int err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002271
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002272 if (tb[IFLA_VF_MAC]) {
2273 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03002274
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002275 if (ivm->vf >= INT_MAX)
2276 return -EINVAL;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002277 err = -EOPNOTSUPP;
2278 if (ops->ndo_set_vf_mac)
2279 err = ops->ndo_set_vf_mac(dev, ivm->vf,
2280 ivm->mac);
2281 if (err < 0)
2282 return err;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002283 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002284
2285 if (tb[IFLA_VF_VLAN]) {
2286 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
2287
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002288 if (ivv->vf >= INT_MAX)
2289 return -EINVAL;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002290 err = -EOPNOTSUPP;
2291 if (ops->ndo_set_vf_vlan)
2292 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
Moshe Shemesh79aab092016-09-22 12:11:15 +03002293 ivv->qos,
2294 htons(ETH_P_8021Q));
2295 if (err < 0)
2296 return err;
2297 }
2298
2299 if (tb[IFLA_VF_VLAN_LIST]) {
2300 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
2301 struct nlattr *attr;
2302 int rem, len = 0;
2303
2304 err = -EOPNOTSUPP;
2305 if (!ops->ndo_set_vf_vlan)
2306 return err;
2307
2308 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
2309 if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
2310 nla_len(attr) < NLA_HDRLEN) {
2311 return -EINVAL;
2312 }
2313 if (len >= MAX_VLAN_LIST_LEN)
2314 return -EOPNOTSUPP;
2315 ivvl[len] = nla_data(attr);
2316
2317 len++;
2318 }
Arnd Bergmannfa34cd92016-09-30 18:13:49 +02002319 if (len == 0)
2320 return -EINVAL;
2321
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002322 if (ivvl[0]->vf >= INT_MAX)
2323 return -EINVAL;
Moshe Shemesh79aab092016-09-22 12:11:15 +03002324 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
2325 ivvl[0]->qos, ivvl[0]->vlan_proto);
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002326 if (err < 0)
2327 return err;
2328 }
2329
2330 if (tb[IFLA_VF_TX_RATE]) {
2331 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
2332 struct ifla_vf_info ivf;
2333
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002334 if (ivt->vf >= INT_MAX)
2335 return -EINVAL;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002336 err = -EOPNOTSUPP;
2337 if (ops->ndo_get_vf_config)
2338 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
2339 if (err < 0)
2340 return err;
2341
2342 err = -EOPNOTSUPP;
2343 if (ops->ndo_set_vf_rate)
2344 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2345 ivf.min_tx_rate,
2346 ivt->rate);
2347 if (err < 0)
2348 return err;
2349 }
2350
2351 if (tb[IFLA_VF_RATE]) {
2352 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
2353
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002354 if (ivt->vf >= INT_MAX)
2355 return -EINVAL;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002356 err = -EOPNOTSUPP;
2357 if (ops->ndo_set_vf_rate)
2358 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2359 ivt->min_tx_rate,
2360 ivt->max_tx_rate);
2361 if (err < 0)
2362 return err;
2363 }
2364
2365 if (tb[IFLA_VF_SPOOFCHK]) {
2366 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
2367
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002368 if (ivs->vf >= INT_MAX)
2369 return -EINVAL;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002370 err = -EOPNOTSUPP;
2371 if (ops->ndo_set_vf_spoofchk)
2372 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
2373 ivs->setting);
2374 if (err < 0)
2375 return err;
2376 }
2377
2378 if (tb[IFLA_VF_LINK_STATE]) {
2379 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
2380
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002381 if (ivl->vf >= INT_MAX)
2382 return -EINVAL;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002383 err = -EOPNOTSUPP;
2384 if (ops->ndo_set_vf_link_state)
2385 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
2386 ivl->link_state);
2387 if (err < 0)
2388 return err;
2389 }
2390
2391 if (tb[IFLA_VF_RSS_QUERY_EN]) {
2392 struct ifla_vf_rss_query_en *ivrssq_en;
2393
2394 err = -EOPNOTSUPP;
2395 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002396 if (ivrssq_en->vf >= INT_MAX)
2397 return -EINVAL;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002398 if (ops->ndo_set_vf_rss_query_en)
2399 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
2400 ivrssq_en->setting);
2401 if (err < 0)
2402 return err;
2403 }
2404
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00002405 if (tb[IFLA_VF_TRUST]) {
2406 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
2407
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002408 if (ivt->vf >= INT_MAX)
2409 return -EINVAL;
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00002410 err = -EOPNOTSUPP;
2411 if (ops->ndo_set_vf_trust)
2412 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
2413 if (err < 0)
2414 return err;
2415 }
2416
Eli Cohencc8e27c2016-03-11 22:58:34 +02002417 if (tb[IFLA_VF_IB_NODE_GUID]) {
2418 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
2419
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002420 if (ivt->vf >= INT_MAX)
2421 return -EINVAL;
Eli Cohencc8e27c2016-03-11 22:58:34 +02002422 if (!ops->ndo_set_vf_guid)
2423 return -EOPNOTSUPP;
Eli Cohencc8e27c2016-03-11 22:58:34 +02002424 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
2425 }
2426
2427 if (tb[IFLA_VF_IB_PORT_GUID]) {
2428 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
2429
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002430 if (ivt->vf >= INT_MAX)
2431 return -EINVAL;
Eli Cohencc8e27c2016-03-11 22:58:34 +02002432 if (!ops->ndo_set_vf_guid)
2433 return -EOPNOTSUPP;
2434
2435 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
2436 }
2437
Chris Wrightc02db8c2010-05-16 01:05:45 -07002438 return err;
2439}
2440
David Ahern33eaf2a2017-10-04 17:48:46 -07002441static int do_set_master(struct net_device *dev, int ifindex,
2442 struct netlink_ext_ack *extack)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002443{
Jiri Pirko898e5062013-01-03 22:48:52 +00002444 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002445 const struct net_device_ops *ops;
2446 int err;
2447
Jiri Pirko898e5062013-01-03 22:48:52 +00002448 if (upper_dev) {
2449 if (upper_dev->ifindex == ifindex)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002450 return 0;
Jiri Pirko898e5062013-01-03 22:48:52 +00002451 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002452 if (ops->ndo_del_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002453 err = ops->ndo_del_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002454 if (err)
2455 return err;
Taehee Yooab92d682019-10-21 18:47:51 +00002456 netdev_update_lockdep_key(dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002457 } else {
2458 return -EOPNOTSUPP;
2459 }
2460 }
2461
2462 if (ifindex) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002463 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
2464 if (!upper_dev)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002465 return -EINVAL;
Jiri Pirko898e5062013-01-03 22:48:52 +00002466 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002467 if (ops->ndo_add_slave) {
David Ahern33eaf2a2017-10-04 17:48:46 -07002468 err = ops->ndo_add_slave(upper_dev, dev, extack);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002469 if (err)
2470 return err;
2471 } else {
2472 return -EOPNOTSUPP;
2473 }
2474 }
2475 return 0;
2476}
2477
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002478#define DO_SETLINK_MODIFIED 0x01
Nicolas Dichtelba998902014-09-01 16:07:29 +02002479/* notify flag means notify + modified. */
2480#define DO_SETLINK_NOTIFY 0x03
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002481static int do_setlink(const struct sk_buff *skb,
2482 struct net_device *dev, struct ifinfomsg *ifm,
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002483 struct netlink_ext_ack *extack,
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002484 struct nlattr **tb, char *ifname, int status)
Patrick McHardy0157f602007-06-13 12:03:36 -07002485{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002486 const struct net_device_ops *ops = dev->netdev_ops;
Patrick McHardy0157f602007-06-13 12:03:36 -07002487 int err;
2488
Eric Dumazet644c7ee2018-06-05 09:25:19 -07002489 err = validate_linkmsg(dev, tb);
2490 if (err < 0)
2491 return err;
2492
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002493 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) {
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002494 struct net *net = rtnl_link_get_net_capable(skb, dev_net(dev),
2495 tb, CAP_NET_ADMIN);
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002496 if (IS_ERR(net)) {
2497 err = PTR_ERR(net);
2498 goto errout;
2499 }
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002500
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002501 err = dev_change_net_namespace(dev, net, ifname);
2502 put_net(net);
2503 if (err)
2504 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002505 status |= DO_SETLINK_MODIFIED;
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002506 }
2507
Patrick McHardy0157f602007-06-13 12:03:36 -07002508 if (tb[IFLA_MAP]) {
2509 struct rtnl_link_ifmap *u_map;
2510 struct ifmap k_map;
2511
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002512 if (!ops->ndo_set_config) {
Patrick McHardy0157f602007-06-13 12:03:36 -07002513 err = -EOPNOTSUPP;
2514 goto errout;
2515 }
2516
2517 if (!netif_device_present(dev)) {
2518 err = -ENODEV;
2519 goto errout;
2520 }
2521
2522 u_map = nla_data(tb[IFLA_MAP]);
2523 k_map.mem_start = (unsigned long) u_map->mem_start;
2524 k_map.mem_end = (unsigned long) u_map->mem_end;
2525 k_map.base_addr = (unsigned short) u_map->base_addr;
2526 k_map.irq = (unsigned char) u_map->irq;
2527 k_map.dma = (unsigned char) u_map->dma;
2528 k_map.port = (unsigned char) u_map->port;
2529
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002530 err = ops->ndo_set_config(dev, &k_map);
Patrick McHardy0157f602007-06-13 12:03:36 -07002531 if (err < 0)
2532 goto errout;
2533
Nicolas Dichtelba998902014-09-01 16:07:29 +02002534 status |= DO_SETLINK_NOTIFY;
Patrick McHardy0157f602007-06-13 12:03:36 -07002535 }
2536
2537 if (tb[IFLA_ADDRESS]) {
2538 struct sockaddr *sa;
2539 int len;
2540
WANG Cong153711f2017-07-20 11:27:57 -07002541 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2542 sizeof(*sa));
Patrick McHardy0157f602007-06-13 12:03:36 -07002543 sa = kmalloc(len, GFP_KERNEL);
2544 if (!sa) {
2545 err = -ENOMEM;
2546 goto errout;
2547 }
2548 sa->sa_family = dev->type;
2549 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
2550 dev->addr_len);
Petr Machata3a37a962018-12-13 11:54:30 +00002551 err = dev_set_mac_address(dev, sa, extack);
Patrick McHardy0157f602007-06-13 12:03:36 -07002552 kfree(sa);
2553 if (err)
2554 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002555 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002556 }
2557
2558 if (tb[IFLA_MTU]) {
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07002559 err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack);
Patrick McHardy0157f602007-06-13 12:03:36 -07002560 if (err < 0)
2561 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002562 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002563 }
2564
Vlad Dogarucbda10f2011-01-13 23:38:30 +00002565 if (tb[IFLA_GROUP]) {
2566 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Nicolas Dichtelba998902014-09-01 16:07:29 +02002567 status |= DO_SETLINK_NOTIFY;
Vlad Dogarucbda10f2011-01-13 23:38:30 +00002568 }
2569
Patrick McHardy0157f602007-06-13 12:03:36 -07002570 /*
2571 * Interface selected by interface index but interface
2572 * name provided implies that a name change has been
2573 * requested.
2574 */
2575 if (ifm->ifi_index > 0 && ifname[0]) {
2576 err = dev_change_name(dev, ifname);
2577 if (err < 0)
2578 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002579 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002580 }
2581
Stephen Hemminger0b815a12008-09-22 21:28:11 -07002582 if (tb[IFLA_IFALIAS]) {
2583 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2584 nla_len(tb[IFLA_IFALIAS]));
2585 if (err < 0)
2586 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002587 status |= DO_SETLINK_NOTIFY;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07002588 }
2589
Patrick McHardy0157f602007-06-13 12:03:36 -07002590 if (tb[IFLA_BROADCAST]) {
2591 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00002592 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Patrick McHardy0157f602007-06-13 12:03:36 -07002593 }
2594
2595 if (ifm->ifi_flags || ifm->ifi_change) {
Petr Machata567c5e12018-12-06 17:05:42 +00002596 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
2597 extack);
Johannes Berg5f9021c2008-11-16 23:20:31 -08002598 if (err < 0)
2599 goto errout;
Patrick McHardy0157f602007-06-13 12:03:36 -07002600 }
2601
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002602 if (tb[IFLA_MASTER]) {
David Ahern33eaf2a2017-10-04 17:48:46 -07002603 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002604 if (err)
2605 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002606 status |= DO_SETLINK_MODIFIED;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002607 }
2608
Jiri Pirko9a572472012-12-27 23:49:39 +00002609 if (tb[IFLA_CARRIER]) {
2610 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2611 if (err)
2612 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002613 status |= DO_SETLINK_MODIFIED;
Jiri Pirko9a572472012-12-27 23:49:39 +00002614 }
2615
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002616 if (tb[IFLA_TXQLEN]) {
Alexey Dobriyan0cd29502017-05-17 13:30:44 +03002617 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002618
Cong Wang6a643dd2018-01-25 18:26:22 -08002619 err = dev_change_tx_queue_len(dev, value);
2620 if (err)
2621 goto errout;
2622 status |= DO_SETLINK_MODIFIED;
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002623 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002624
Stephen Hemminger46e6b992017-12-07 15:40:19 -08002625 if (tb[IFLA_GSO_MAX_SIZE]) {
2626 u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]);
2627
2628 if (max_size > GSO_MAX_SIZE) {
2629 err = -EINVAL;
2630 goto errout;
2631 }
2632
2633 if (dev->gso_max_size ^ max_size) {
2634 netif_set_gso_max_size(dev, max_size);
2635 status |= DO_SETLINK_MODIFIED;
2636 }
2637 }
2638
2639 if (tb[IFLA_GSO_MAX_SEGS]) {
2640 u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
2641
2642 if (max_segs > GSO_MAX_SEGS) {
2643 err = -EINVAL;
2644 goto errout;
2645 }
2646
2647 if (dev->gso_max_segs ^ max_segs) {
2648 dev->gso_max_segs = max_segs;
2649 status |= DO_SETLINK_MODIFIED;
2650 }
2651 }
2652
Patrick McHardy0157f602007-06-13 12:03:36 -07002653 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002654 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Patrick McHardy0157f602007-06-13 12:03:36 -07002655
2656 if (tb[IFLA_LINKMODE]) {
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002657 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2658
David S. Miller93b2d4a2008-02-17 18:35:07 -08002659 write_lock_bh(&dev_base_lock);
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002660 if (dev->link_mode ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02002661 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002662 dev->link_mode = value;
David S. Miller93b2d4a2008-02-17 18:35:07 -08002663 write_unlock_bh(&dev_base_lock);
Patrick McHardy0157f602007-06-13 12:03:36 -07002664 }
2665
Chris Wrightc02db8c2010-05-16 01:05:45 -07002666 if (tb[IFLA_VFINFO_LIST]) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002667 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
Chris Wrightc02db8c2010-05-16 01:05:45 -07002668 struct nlattr *attr;
2669 int rem;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002670
Chris Wrightc02db8c2010-05-16 01:05:45 -07002671 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002672 if (nla_type(attr) != IFLA_VF_INFO ||
2673 nla_len(attr) < NLA_HDRLEN) {
David Howells253683b2010-05-21 02:25:27 +00002674 err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002675 goto errout;
David Howells253683b2010-05-21 02:25:27 +00002676 }
Johannes Berg8cb08172019-04-26 14:07:28 +02002677 err = nla_parse_nested_deprecated(vfinfo, IFLA_VF_MAX,
2678 attr,
2679 ifla_vf_policy,
2680 NULL);
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002681 if (err < 0)
2682 goto errout;
2683 err = do_setvfinfo(dev, vfinfo);
Chris Wrightc02db8c2010-05-16 01:05:45 -07002684 if (err < 0)
2685 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002686 status |= DO_SETLINK_NOTIFY;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002687 }
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00002688 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002689 err = 0;
2690
Scott Feldman57b61082010-05-17 22:49:55 -07002691 if (tb[IFLA_VF_PORTS]) {
2692 struct nlattr *port[IFLA_PORT_MAX+1];
2693 struct nlattr *attr;
2694 int vf;
2695 int rem;
2696
2697 err = -EOPNOTSUPP;
2698 if (!ops->ndo_set_vf_port)
2699 goto errout;
2700
2701 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
Daniel Borkmann035d2102015-07-13 00:06:02 +02002702 if (nla_type(attr) != IFLA_VF_PORT ||
2703 nla_len(attr) < NLA_HDRLEN) {
2704 err = -EINVAL;
2705 goto errout;
2706 }
Johannes Berg8cb08172019-04-26 14:07:28 +02002707 err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
2708 attr,
2709 ifla_port_policy,
2710 NULL);
Scott Feldman57b61082010-05-17 22:49:55 -07002711 if (err < 0)
2712 goto errout;
2713 if (!port[IFLA_PORT_VF]) {
2714 err = -EOPNOTSUPP;
2715 goto errout;
2716 }
2717 vf = nla_get_u32(port[IFLA_PORT_VF]);
2718 err = ops->ndo_set_vf_port(dev, vf, port);
2719 if (err < 0)
2720 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002721 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07002722 }
2723 }
2724 err = 0;
2725
2726 if (tb[IFLA_PORT_SELF]) {
2727 struct nlattr *port[IFLA_PORT_MAX+1];
2728
Johannes Berg8cb08172019-04-26 14:07:28 +02002729 err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
2730 tb[IFLA_PORT_SELF],
2731 ifla_port_policy, NULL);
Scott Feldman57b61082010-05-17 22:49:55 -07002732 if (err < 0)
2733 goto errout;
2734
2735 err = -EOPNOTSUPP;
2736 if (ops->ndo_set_vf_port)
2737 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
2738 if (err < 0)
2739 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002740 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07002741 }
Thomas Graff8ff1822010-11-16 04:30:14 +00002742
2743 if (tb[IFLA_AF_SPEC]) {
2744 struct nlattr *af;
2745 int rem;
2746
2747 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2748 const struct rtnl_af_ops *af_ops;
2749
Florian Westphal5fa85a02017-10-16 15:44:36 +02002750 rcu_read_lock();
2751
Gustavo A. R. Silva058c8d52017-10-20 19:43:11 -05002752 BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
Thomas Graff8ff1822010-11-16 04:30:14 +00002753
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002754 err = af_ops->set_link_af(dev, af);
Florian Westphal5fa85a02017-10-16 15:44:36 +02002755 if (err < 0) {
2756 rcu_read_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +00002757 goto errout;
Florian Westphal5fa85a02017-10-16 15:44:36 +02002758 }
Thomas Graff8ff1822010-11-16 04:30:14 +00002759
Florian Westphal5fa85a02017-10-16 15:44:36 +02002760 rcu_read_unlock();
Nicolas Dichtelba998902014-09-01 16:07:29 +02002761 status |= DO_SETLINK_NOTIFY;
Thomas Graff8ff1822010-11-16 04:30:14 +00002762 }
2763 }
Scott Feldman57b61082010-05-17 22:49:55 -07002764 err = 0;
2765
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07002766 if (tb[IFLA_PROTO_DOWN]) {
2767 err = dev_change_proto_down(dev,
2768 nla_get_u8(tb[IFLA_PROTO_DOWN]));
2769 if (err)
2770 goto errout;
2771 status |= DO_SETLINK_NOTIFY;
2772 }
2773
Brenden Blancod1fdd912016-07-19 12:16:49 -07002774 if (tb[IFLA_XDP]) {
2775 struct nlattr *xdp[IFLA_XDP_MAX + 1];
Daniel Borkmann85de8572016-11-28 23:16:54 +01002776 u32 xdp_flags = 0;
Brenden Blancod1fdd912016-07-19 12:16:49 -07002777
Johannes Berg8cb08172019-04-26 14:07:28 +02002778 err = nla_parse_nested_deprecated(xdp, IFLA_XDP_MAX,
2779 tb[IFLA_XDP],
2780 ifla_xdp_policy, NULL);
Brenden Blancod1fdd912016-07-19 12:16:49 -07002781 if (err < 0)
2782 goto errout;
2783
Martin KaFai Lau58038692017-06-15 17:29:09 -07002784 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
Brenden Blanco262d8622016-07-20 17:22:34 -07002785 err = -EINVAL;
2786 goto errout;
2787 }
Daniel Borkmann85de8572016-11-28 23:16:54 +01002788
2789 if (xdp[IFLA_XDP_FLAGS]) {
2790 xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
2791 if (xdp_flags & ~XDP_FLAGS_MASK) {
2792 err = -EINVAL;
2793 goto errout;
2794 }
Jakub Kicinskiee5d0322017-06-21 18:25:04 -07002795 if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
Daniel Borkmann0489df92017-05-12 01:04:45 +02002796 err = -EINVAL;
2797 goto errout;
2798 }
Daniel Borkmann85de8572016-11-28 23:16:54 +01002799 }
2800
Brenden Blancod1fdd912016-07-19 12:16:49 -07002801 if (xdp[IFLA_XDP_FD]) {
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002802 err = dev_change_xdp_fd(dev, extack,
Daniel Borkmann85de8572016-11-28 23:16:54 +01002803 nla_get_s32(xdp[IFLA_XDP_FD]),
2804 xdp_flags);
Brenden Blancod1fdd912016-07-19 12:16:49 -07002805 if (err)
2806 goto errout;
2807 status |= DO_SETLINK_NOTIFY;
2808 }
2809 }
2810
Patrick McHardy0157f602007-06-13 12:03:36 -07002811errout:
Nicolas Dichtelba998902014-09-01 16:07:29 +02002812 if (status & DO_SETLINK_MODIFIED) {
Xin Long64ff90c2017-10-15 18:13:45 +08002813 if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
Nicolas Dichtelba998902014-09-01 16:07:29 +02002814 netdev_state_change(dev);
2815
2816 if (err < 0)
2817 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",
2818 dev->name);
2819 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002820
Patrick McHardy0157f602007-06-13 12:03:36 -07002821 return err;
2822}
2823
Jiri Pirkocc6090e2019-09-30 11:48:19 +02002824static struct net_device *rtnl_dev_get(struct net *net,
2825 struct nlattr *ifname_attr,
Jiri Pirko76c9ac02019-09-30 11:48:20 +02002826 struct nlattr *altifname_attr,
Jiri Pirkocc6090e2019-09-30 11:48:19 +02002827 char *ifname)
2828{
Jiri Pirko76c9ac02019-09-30 11:48:20 +02002829 char buffer[ALTIFNAMSIZ];
Jiri Pirkocc6090e2019-09-30 11:48:19 +02002830
2831 if (!ifname) {
2832 ifname = buffer;
2833 if (ifname_attr)
2834 nla_strlcpy(ifname, ifname_attr, IFNAMSIZ);
Jiri Pirko76c9ac02019-09-30 11:48:20 +02002835 else if (altifname_attr)
2836 nla_strlcpy(ifname, altifname_attr, ALTIFNAMSIZ);
Jiri Pirkocc6090e2019-09-30 11:48:19 +02002837 else
2838 return NULL;
2839 }
2840
2841 return __dev_get_by_name(net, ifname);
2842}
2843
David Ahernc21ef3e2017-04-16 09:48:24 -07002844static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2845 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002847 struct net *net = sock_net(skb->sk);
Thomas Grafda5e0492006-08-10 21:17:37 -07002848 struct ifinfomsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 struct net_device *dev;
Patrick McHardy0157f602007-06-13 12:03:36 -07002850 int err;
Thomas Grafda5e0492006-08-10 21:17:37 -07002851 struct nlattr *tb[IFLA_MAX+1];
2852 char ifname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853
Johannes Berg8cb08172019-04-26 14:07:28 +02002854 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
2855 ifla_policy, extack);
Thomas Grafda5e0492006-08-10 21:17:37 -07002856 if (err < 0)
2857 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002859 err = rtnl_ensure_unique_netns(tb, extack, false);
2860 if (err < 0)
2861 goto errout;
2862
Thomas Graf5176f912006-08-26 20:13:18 -07002863 if (tb[IFLA_IFNAME])
2864 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
Patrick McHardy78e5b8912006-09-13 20:35:36 -07002865 else
2866 ifname[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 err = -EINVAL;
Thomas Grafda5e0492006-08-10 21:17:37 -07002869 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -07002870 if (ifm->ifi_index > 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00002871 dev = __dev_get_by_index(net, ifm->ifi_index);
Jiri Pirko76c9ac02019-09-30 11:48:20 +02002872 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
2873 dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname);
Thomas Grafda5e0492006-08-10 21:17:37 -07002874 else
2875 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876
Thomas Grafda5e0492006-08-10 21:17:37 -07002877 if (dev == NULL) {
2878 err = -ENODEV;
2879 goto errout;
2880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002882 err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
Thomas Grafda5e0492006-08-10 21:17:37 -07002883errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 return err;
2885}
2886
WANG Cong66400d52015-03-24 11:53:31 -07002887static int rtnl_group_dellink(const struct net *net, int group)
2888{
2889 struct net_device *dev, *aux;
2890 LIST_HEAD(list_kill);
2891 bool found = false;
2892
2893 if (!group)
2894 return -EPERM;
2895
2896 for_each_netdev(net, dev) {
2897 if (dev->group == group) {
2898 const struct rtnl_link_ops *ops;
2899
2900 found = true;
2901 ops = dev->rtnl_link_ops;
2902 if (!ops || !ops->dellink)
2903 return -EOPNOTSUPP;
2904 }
2905 }
2906
2907 if (!found)
2908 return -ENODEV;
2909
2910 for_each_netdev_safe(net, dev, aux) {
2911 if (dev->group == group) {
2912 const struct rtnl_link_ops *ops;
2913
2914 ops = dev->rtnl_link_ops;
2915 ops->dellink(dev, &list_kill);
2916 }
2917 }
2918 unregister_netdevice_many(&list_kill);
2919
2920 return 0;
2921}
2922
Thomas Graf614732e2015-07-21 10:44:06 +02002923int rtnl_delete_link(struct net_device *dev)
2924{
2925 const struct rtnl_link_ops *ops;
2926 LIST_HEAD(list_kill);
2927
2928 ops = dev->rtnl_link_ops;
2929 if (!ops || !ops->dellink)
2930 return -EOPNOTSUPP;
2931
2932 ops->dellink(dev, &list_kill);
2933 unregister_netdevice_many(&list_kill);
2934
2935 return 0;
2936}
2937EXPORT_SYMBOL_GPL(rtnl_delete_link);
2938
David Ahernc21ef3e2017-04-16 09:48:24 -07002939static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
2940 struct netlink_ext_ack *extack)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002941{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002942 struct net *net = sock_net(skb->sk);
Christian Braunerb61ad682018-01-24 15:26:34 +01002943 struct net *tgt_net = net;
2944 struct net_device *dev = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002945 struct ifinfomsg *ifm;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002946 struct nlattr *tb[IFLA_MAX+1];
2947 int err;
Christian Braunerb61ad682018-01-24 15:26:34 +01002948 int netnsid = -1;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002949
Johannes Berg8cb08172019-04-26 14:07:28 +02002950 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
2951 ifla_policy, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002952 if (err < 0)
2953 return err;
2954
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002955 err = rtnl_ensure_unique_netns(tb, extack, true);
2956 if (err < 0)
2957 return err;
2958
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002959 if (tb[IFLA_TARGET_NETNSID]) {
2960 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
Christian Braunerc383edc2018-09-04 21:53:47 +02002961 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
Christian Braunerb61ad682018-01-24 15:26:34 +01002962 if (IS_ERR(tgt_net))
2963 return PTR_ERR(tgt_net);
2964 }
2965
2966 err = -EINVAL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002967 ifm = nlmsg_data(nlh);
2968 if (ifm->ifi_index > 0)
Christian Braunerb61ad682018-01-24 15:26:34 +01002969 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
Jiri Pirko76c9ac02019-09-30 11:48:20 +02002970 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
2971 dev = rtnl_dev_get(net, tb[IFLA_IFNAME],
2972 tb[IFLA_ALT_IFNAME], NULL);
WANG Cong66400d52015-03-24 11:53:31 -07002973 else if (tb[IFLA_GROUP])
Christian Braunerb61ad682018-01-24 15:26:34 +01002974 err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP]));
Patrick McHardy38f7b872007-06-13 12:03:51 -07002975 else
Christian Braunerb61ad682018-01-24 15:26:34 +01002976 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002977
Christian Braunerb61ad682018-01-24 15:26:34 +01002978 if (!dev) {
2979 if (tb[IFLA_IFNAME] || ifm->ifi_index > 0)
2980 err = -ENODEV;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002981
Christian Braunerb61ad682018-01-24 15:26:34 +01002982 goto out;
2983 }
2984
2985 err = rtnl_delete_link(dev);
2986
2987out:
2988 if (netnsid >= 0)
2989 put_net(tgt_net);
2990
2991 return err;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002992}
2993
Patrick McHardy3729d502010-02-26 06:34:54 +00002994int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
2995{
2996 unsigned int old_flags;
2997 int err;
2998
2999 old_flags = dev->flags;
3000 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
Petr Machata6d040322018-12-06 17:05:43 +00003001 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
3002 NULL);
Patrick McHardy3729d502010-02-26 06:34:54 +00003003 if (err < 0)
3004 return err;
3005 }
3006
Roopa Prabhu8d356b82018-07-04 16:46:29 -07003007 if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) {
Roopa Prabhu56a49d72018-09-12 13:21:48 -07003008 __dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags));
Roopa Prabhu8d356b82018-07-04 16:46:29 -07003009 } else {
3010 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
3011 __dev_notify_flags(dev, old_flags, ~0U);
3012 }
Patrick McHardy3729d502010-02-26 06:34:54 +00003013 return 0;
3014}
3015EXPORT_SYMBOL(rtnl_configure_link);
3016
David Ahernd0522f12018-11-06 12:51:14 -08003017struct net_device *rtnl_create_link(struct net *net, const char *ifname,
3018 unsigned char name_assign_type,
3019 const struct rtnl_link_ops *ops,
3020 struct nlattr *tb[],
3021 struct netlink_ext_ack *extack)
Pavel Emelianove7199282007-08-08 22:16:38 -07003022{
Pavel Emelianove7199282007-08-08 22:16:38 -07003023 struct net_device *dev;
Jiri Pirkod40156a2012-07-20 02:28:47 +00003024 unsigned int num_tx_queues = 1;
3025 unsigned int num_rx_queues = 1;
Pavel Emelianove7199282007-08-08 22:16:38 -07003026
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00003027 if (tb[IFLA_NUM_TX_QUEUES])
3028 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
3029 else if (ops->get_num_tx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00003030 num_tx_queues = ops->get_num_tx_queues();
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00003031
3032 if (tb[IFLA_NUM_RX_QUEUES])
3033 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
3034 else if (ops->get_num_rx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00003035 num_rx_queues = ops->get_num_rx_queues();
stephen hemmingerefacb302012-04-10 18:34:43 +00003036
David Ahernd0522f12018-11-06 12:51:14 -08003037 if (num_tx_queues < 1 || num_tx_queues > 4096) {
3038 NL_SET_ERR_MSG(extack, "Invalid number of transmit queues");
Eric Dumazet0e1d6ec2018-10-02 15:47:35 -07003039 return ERR_PTR(-EINVAL);
David Ahernd0522f12018-11-06 12:51:14 -08003040 }
Eric Dumazet0e1d6ec2018-10-02 15:47:35 -07003041
David Ahernd0522f12018-11-06 12:51:14 -08003042 if (num_rx_queues < 1 || num_rx_queues > 4096) {
3043 NL_SET_ERR_MSG(extack, "Invalid number of receive queues");
Eric Dumazet0e1d6ec2018-10-02 15:47:35 -07003044 return ERR_PTR(-EINVAL);
David Ahernd0522f12018-11-06 12:51:14 -08003045 }
Eric Dumazet0e1d6ec2018-10-02 15:47:35 -07003046
Tom Gundersen55177502014-07-14 16:37:25 +02003047 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
Tom Gundersenc835a672014-07-14 16:37:24 +02003048 ops->setup, num_tx_queues, num_rx_queues);
Pavel Emelianove7199282007-08-08 22:16:38 -07003049 if (!dev)
Tobias Klauserd1892e42017-02-20 16:32:06 +01003050 return ERR_PTR(-ENOMEM);
Pavel Emelianove7199282007-08-08 22:16:38 -07003051
Eric W. Biederman81adee42009-11-08 00:53:51 -08003052 dev_net_set(dev, net);
3053 dev->rtnl_link_ops = ops;
Patrick McHardy3729d502010-02-26 06:34:54 +00003054 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
Eric W. Biederman81adee42009-11-08 00:53:51 -08003055
Eric Dumazetd836f5c2020-01-21 22:47:29 -08003056 if (tb[IFLA_MTU]) {
3057 u32 mtu = nla_get_u32(tb[IFLA_MTU]);
3058 int err;
3059
3060 err = dev_validate_mtu(dev, mtu, extack);
3061 if (err) {
3062 free_netdev(dev);
3063 return ERR_PTR(err);
3064 }
3065 dev->mtu = mtu;
3066 }
Jiri Pirko2afb9b52013-01-06 12:41:57 +00003067 if (tb[IFLA_ADDRESS]) {
Pavel Emelianove7199282007-08-08 22:16:38 -07003068 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
3069 nla_len(tb[IFLA_ADDRESS]));
Jiri Pirko2afb9b52013-01-06 12:41:57 +00003070 dev->addr_assign_type = NET_ADDR_SET;
3071 }
Pavel Emelianove7199282007-08-08 22:16:38 -07003072 if (tb[IFLA_BROADCAST])
3073 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
3074 nla_len(tb[IFLA_BROADCAST]));
3075 if (tb[IFLA_TXQLEN])
3076 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
3077 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08003078 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Pavel Emelianove7199282007-08-08 22:16:38 -07003079 if (tb[IFLA_LINKMODE])
3080 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
Patrick McHardyffa934f2011-01-20 03:00:42 +00003081 if (tb[IFLA_GROUP])
3082 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Stephen Hemminger46e6b992017-12-07 15:40:19 -08003083 if (tb[IFLA_GSO_MAX_SIZE])
3084 netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE]));
3085 if (tb[IFLA_GSO_MAX_SEGS])
Stephen Hemmingera0b586f2017-12-08 15:34:13 -08003086 dev->gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
Pavel Emelianove7199282007-08-08 22:16:38 -07003087
3088 return dev;
Pavel Emelianove7199282007-08-08 22:16:38 -07003089}
Eric Dumazete0d087a2009-11-07 01:26:17 -08003090EXPORT_SYMBOL(rtnl_create_link);
Pavel Emelianove7199282007-08-08 22:16:38 -07003091
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07003092static int rtnl_group_changelink(const struct sk_buff *skb,
3093 struct net *net, int group,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00003094 struct ifinfomsg *ifm,
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07003095 struct netlink_ext_ack *extack,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00003096 struct nlattr **tb)
3097{
WANG Congd0795352015-03-23 16:31:09 -07003098 struct net_device *dev, *aux;
Vlad Dogarue7ed8282011-01-13 23:38:31 +00003099 int err;
3100
WANG Congd0795352015-03-23 16:31:09 -07003101 for_each_netdev_safe(net, dev, aux) {
Vlad Dogarue7ed8282011-01-13 23:38:31 +00003102 if (dev->group == group) {
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07003103 err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00003104 if (err < 0)
3105 return err;
3106 }
3107 }
3108
3109 return 0;
3110}
3111
Jakub Kicinskia2939742018-11-27 22:32:31 -08003112static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3113 struct nlattr **attr, struct netlink_ext_ack *extack)
Patrick McHardy38f7b872007-06-13 12:03:51 -07003114{
Jakub Kicinski420d0312018-11-27 22:32:30 -08003115 struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1];
3116 unsigned char name_assign_type = NET_NAME_USER;
3117 struct nlattr *linkinfo[IFLA_INFO_MAX + 1];
3118 const struct rtnl_link_ops *m_ops = NULL;
Jakub Kicinski420d0312018-11-27 22:32:30 -08003119 struct net_device *master_dev = NULL;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003120 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07003121 const struct rtnl_link_ops *ops;
Jakub Kicinski420d0312018-11-27 22:32:30 -08003122 struct nlattr *tb[IFLA_MAX + 1];
3123 struct net *dest_net, *link_net;
3124 struct nlattr **slave_data;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003125 char kind[MODULE_NAME_LEN];
Jakub Kicinski420d0312018-11-27 22:32:30 -08003126 struct net_device *dev;
3127 struct ifinfomsg *ifm;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003128 char ifname[IFNAMSIZ];
Jakub Kicinski420d0312018-11-27 22:32:30 -08003129 struct nlattr **data;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003130 int err;
3131
Johannes Berg95a5afc2008-10-16 15:24:51 -07003132#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07003133replay:
Thomas Graf8072f082007-07-31 14:13:50 -07003134#endif
Johannes Berg8cb08172019-04-26 14:07:28 +02003135 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3136 ifla_policy, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07003137 if (err < 0)
3138 return err;
3139
Christian Brauner4ff66ca2018-02-07 13:53:20 +01003140 err = rtnl_ensure_unique_netns(tb, extack, false);
3141 if (err < 0)
3142 return err;
3143
Patrick McHardy38f7b872007-06-13 12:03:51 -07003144 if (tb[IFLA_IFNAME])
3145 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
3146 else
3147 ifname[0] = '\0';
3148
3149 ifm = nlmsg_data(nlh);
3150 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07003151 dev = __dev_get_by_index(net, ifm->ifi_index);
Jiri Pirko76c9ac02019-09-30 11:48:20 +02003152 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3153 dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname);
Jiri Pirko7af12cb2019-09-30 11:48:18 +02003154 else
3155 dev = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003156
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01003157 if (dev) {
3158 master_dev = netdev_master_upper_dev_get(dev);
3159 if (master_dev)
3160 m_ops = master_dev->rtnl_link_ops;
3161 }
3162
Eric Dumazete0d087a2009-11-07 01:26:17 -08003163 err = validate_linkmsg(dev, tb);
3164 if (err < 0)
Thomas Graf1840bb12008-02-23 19:54:36 -08003165 return err;
3166
Patrick McHardy38f7b872007-06-13 12:03:51 -07003167 if (tb[IFLA_LINKINFO]) {
Johannes Berg8cb08172019-04-26 14:07:28 +02003168 err = nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX,
3169 tb[IFLA_LINKINFO],
3170 ifla_info_policy, NULL);
Patrick McHardy38f7b872007-06-13 12:03:51 -07003171 if (err < 0)
3172 return err;
3173 } else
3174 memset(linkinfo, 0, sizeof(linkinfo));
3175
3176 if (linkinfo[IFLA_INFO_KIND]) {
3177 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
3178 ops = rtnl_link_ops_get(kind);
3179 } else {
3180 kind[0] = '\0';
3181 ops = NULL;
3182 }
3183
Jakub Kicinski420d0312018-11-27 22:32:30 -08003184 data = NULL;
3185 if (ops) {
3186 if (ops->maxtype > RTNL_MAX_TYPE)
3187 return -EINVAL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003188
Jakub Kicinski420d0312018-11-27 22:32:30 -08003189 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
Johannes Berg8cb08172019-04-26 14:07:28 +02003190 err = nla_parse_nested_deprecated(attr, ops->maxtype,
3191 linkinfo[IFLA_INFO_DATA],
3192 ops->policy, extack);
Jakub Kicinski420d0312018-11-27 22:32:30 -08003193 if (err < 0)
3194 return err;
3195 data = attr;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003196 }
Jakub Kicinski420d0312018-11-27 22:32:30 -08003197 if (ops->validate) {
3198 err = ops->validate(tb, data, extack);
3199 if (err < 0)
3200 return err;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01003201 }
Jakub Kicinski420d0312018-11-27 22:32:30 -08003202 }
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01003203
Jakub Kicinski420d0312018-11-27 22:32:30 -08003204 slave_data = NULL;
3205 if (m_ops) {
3206 if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)
3207 return -EINVAL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003208
Jakub Kicinski420d0312018-11-27 22:32:30 -08003209 if (m_ops->slave_maxtype &&
3210 linkinfo[IFLA_INFO_SLAVE_DATA]) {
Johannes Berg8cb08172019-04-26 14:07:28 +02003211 err = nla_parse_nested_deprecated(slave_attr,
3212 m_ops->slave_maxtype,
3213 linkinfo[IFLA_INFO_SLAVE_DATA],
3214 m_ops->slave_policy,
3215 extack);
Jakub Kicinski420d0312018-11-27 22:32:30 -08003216 if (err < 0)
3217 return err;
3218 slave_data = slave_attr;
3219 }
3220 }
3221
3222 if (dev) {
3223 int status = 0;
3224
3225 if (nlh->nlmsg_flags & NLM_F_EXCL)
3226 return -EEXIST;
3227 if (nlh->nlmsg_flags & NLM_F_REPLACE)
3228 return -EOPNOTSUPP;
3229
3230 if (linkinfo[IFLA_INFO_DATA]) {
3231 if (!ops || ops != dev->rtnl_link_ops ||
3232 !ops->changelink)
Patrick McHardy38f7b872007-06-13 12:03:51 -07003233 return -EOPNOTSUPP;
3234
Jakub Kicinski420d0312018-11-27 22:32:30 -08003235 err = ops->changelink(dev, tb, data, extack);
3236 if (err < 0)
3237 return err;
3238 status |= DO_SETLINK_NOTIFY;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003239 }
3240
Jakub Kicinski420d0312018-11-27 22:32:30 -08003241 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
3242 if (!m_ops || !m_ops->slave_changelink)
3243 return -EOPNOTSUPP;
3244
3245 err = m_ops->slave_changelink(master_dev, dev, tb,
3246 slave_data, extack);
3247 if (err < 0)
3248 return err;
3249 status |= DO_SETLINK_NOTIFY;
3250 }
3251
3252 return do_setlink(skb, dev, ifm, extack, tb, ifname, status);
3253 }
3254
3255 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
3256 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
3257 return rtnl_group_changelink(skb, net,
Patrick McHardyffa934f2011-01-20 03:00:42 +00003258 nla_get_u32(tb[IFLA_GROUP]),
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07003259 ifm, extack, tb);
Jakub Kicinski420d0312018-11-27 22:32:30 -08003260 return -ENODEV;
3261 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07003262
Jakub Kicinski420d0312018-11-27 22:32:30 -08003263 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
3264 return -EOPNOTSUPP;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003265
Jakub Kicinski420d0312018-11-27 22:32:30 -08003266 if (!ops) {
Johannes Berg95a5afc2008-10-16 15:24:51 -07003267#ifdef CONFIG_MODULES
Jakub Kicinski420d0312018-11-27 22:32:30 -08003268 if (kind[0]) {
3269 __rtnl_unlock();
3270 request_module("rtnl-link-%s", kind);
3271 rtnl_lock();
3272 ops = rtnl_link_ops_get(kind);
3273 if (ops)
3274 goto replay;
3275 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07003276#endif
Jakub Kicinski420d0312018-11-27 22:32:30 -08003277 NL_SET_ERR_MSG(extack, "Unknown device type");
3278 return -EOPNOTSUPP;
3279 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07003280
Jakub Kicinski420d0312018-11-27 22:32:30 -08003281 if (!ops->setup)
3282 return -EOPNOTSUPP;
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02003283
Jakub Kicinski420d0312018-11-27 22:32:30 -08003284 if (!ifname[0]) {
3285 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
3286 name_assign_type = NET_NAME_ENUM;
3287 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07003288
Jakub Kicinski420d0312018-11-27 22:32:30 -08003289 dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN);
3290 if (IS_ERR(dest_net))
3291 return PTR_ERR(dest_net);
Eric W. Biederman13ad1772011-01-29 14:57:22 +00003292
Jakub Kicinski420d0312018-11-27 22:32:30 -08003293 if (tb[IFLA_LINK_NETNSID]) {
3294 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
Nicolas Dichtel317f4812015-01-15 15:11:18 +01003295
Jakub Kicinski420d0312018-11-27 22:32:30 -08003296 link_net = get_net_ns_by_id(dest_net, id);
3297 if (!link_net) {
3298 NL_SET_ERR_MSG(extack, "Unknown network namespace id");
3299 err = -EINVAL;
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00003300 goto out;
3301 }
Jakub Kicinski420d0312018-11-27 22:32:30 -08003302 err = -EPERM;
3303 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
3304 goto out;
3305 } else {
3306 link_net = NULL;
3307 }
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00003308
Jakub Kicinski420d0312018-11-27 22:32:30 -08003309 dev = rtnl_create_link(link_net ? : dest_net, ifname,
3310 name_assign_type, ops, tb, extack);
3311 if (IS_ERR(dev)) {
3312 err = PTR_ERR(dev);
David S. Miller43638902015-03-10 21:58:32 -04003313 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003314 }
Jakub Kicinski420d0312018-11-27 22:32:30 -08003315
3316 dev->ifindex = ifm->ifi_index;
3317
3318 if (ops->newlink) {
3319 err = ops->newlink(link_net ? : net, dev, tb, data, extack);
3320 /* Drivers should call free_netdev() in ->destructor
3321 * and unregister it on failure after registration
3322 * so that device could be finally freed in rtnl_unlock.
3323 */
3324 if (err < 0) {
3325 /* If device is not registered at all, free it now */
3326 if (dev->reg_state == NETREG_UNINITIALIZED)
3327 free_netdev(dev);
3328 goto out;
3329 }
3330 } else {
3331 err = register_netdevice(dev);
3332 if (err < 0) {
3333 free_netdev(dev);
3334 goto out;
3335 }
3336 }
3337 err = rtnl_configure_link(dev, ifm);
3338 if (err < 0)
3339 goto out_unregister;
3340 if (link_net) {
3341 err = dev_change_net_namespace(dev, dest_net, ifname);
3342 if (err < 0)
3343 goto out_unregister;
3344 }
3345 if (tb[IFLA_MASTER]) {
3346 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
3347 if (err)
3348 goto out_unregister;
3349 }
3350out:
3351 if (link_net)
3352 put_net(link_net);
3353 put_net(dest_net);
3354 return err;
3355out_unregister:
3356 if (ops->newlink) {
3357 LIST_HEAD(list_kill);
3358
3359 ops->dellink(dev, &list_kill);
3360 unregister_netdevice_many(&list_kill);
3361 } else {
3362 unregister_netdevice(dev);
3363 }
3364 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003365}
3366
Jakub Kicinskia2939742018-11-27 22:32:31 -08003367static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3368 struct netlink_ext_ack *extack)
3369{
3370 struct nlattr **attr;
3371 int ret;
3372
3373 attr = kmalloc_array(RTNL_MAX_TYPE + 1, sizeof(*attr), GFP_KERNEL);
3374 if (!attr)
3375 return -ENOMEM;
3376
3377 ret = __rtnl_newlink(skb, nlh, attr, extack);
3378 kfree(attr);
3379 return ret;
3380}
3381
Jakub Kicinski9b3757b2019-01-18 10:46:16 -08003382static int rtnl_valid_getlink_req(struct sk_buff *skb,
3383 const struct nlmsghdr *nlh,
3384 struct nlattr **tb,
3385 struct netlink_ext_ack *extack)
3386{
3387 struct ifinfomsg *ifm;
3388 int i, err;
3389
3390 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
3391 NL_SET_ERR_MSG(extack, "Invalid header for get link");
3392 return -EINVAL;
3393 }
3394
3395 if (!netlink_strict_get_check(skb))
Johannes Berg8cb08172019-04-26 14:07:28 +02003396 return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3397 ifla_policy, extack);
Jakub Kicinski9b3757b2019-01-18 10:46:16 -08003398
3399 ifm = nlmsg_data(nlh);
3400 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
3401 ifm->ifi_change) {
3402 NL_SET_ERR_MSG(extack, "Invalid values in header for get link request");
3403 return -EINVAL;
3404 }
3405
Johannes Berg8cb08172019-04-26 14:07:28 +02003406 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFLA_MAX,
3407 ifla_policy, extack);
Jakub Kicinski9b3757b2019-01-18 10:46:16 -08003408 if (err)
3409 return err;
3410
3411 for (i = 0; i <= IFLA_MAX; i++) {
3412 if (!tb[i])
3413 continue;
3414
3415 switch (i) {
3416 case IFLA_IFNAME:
Jiri Pirko76c9ac02019-09-30 11:48:20 +02003417 case IFLA_ALT_IFNAME:
Jakub Kicinski9b3757b2019-01-18 10:46:16 -08003418 case IFLA_EXT_MASK:
3419 case IFLA_TARGET_NETNSID:
3420 break;
3421 default:
3422 NL_SET_ERR_MSG(extack, "Unsupported attribute in get link request");
3423 return -EINVAL;
3424 }
3425 }
3426
3427 return 0;
3428}
3429
David Ahernc21ef3e2017-04-16 09:48:24 -07003430static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3431 struct netlink_ext_ack *extack)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003432{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003433 struct net *net = sock_net(skb->sk);
Jiri Benc79e1ad12017-11-02 17:04:38 -02003434 struct net *tgt_net = net;
Thomas Grafb60c5112006-08-04 23:05:34 -07003435 struct ifinfomsg *ifm;
3436 struct nlattr *tb[IFLA_MAX+1];
3437 struct net_device *dev = NULL;
3438 struct sk_buff *nskb;
Jiri Benc79e1ad12017-11-02 17:04:38 -02003439 int netnsid = -1;
Thomas Graf339bf982006-11-10 14:10:15 -08003440 int err;
Greg Rose115c9b82012-02-21 16:54:48 -05003441 u32 ext_filter_mask = 0;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003442
Jakub Kicinski9b3757b2019-01-18 10:46:16 -08003443 err = rtnl_valid_getlink_req(skb, nlh, tb, extack);
Thomas Grafb60c5112006-08-04 23:05:34 -07003444 if (err < 0)
Eric Sesterhenn9918f232006-09-26 23:26:38 -07003445 return err;
Thomas Grafb60c5112006-08-04 23:05:34 -07003446
Christian Brauner4ff66ca2018-02-07 13:53:20 +01003447 err = rtnl_ensure_unique_netns(tb, extack, true);
3448 if (err < 0)
3449 return err;
3450
Christian Brauner7e4a8d52018-09-04 21:53:53 +02003451 if (tb[IFLA_TARGET_NETNSID]) {
3452 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
Christian Braunerc383edc2018-09-04 21:53:47 +02003453 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
Jiri Benc79e1ad12017-11-02 17:04:38 -02003454 if (IS_ERR(tgt_net))
3455 return PTR_ERR(tgt_net);
3456 }
3457
Greg Rose115c9b82012-02-21 16:54:48 -05003458 if (tb[IFLA_EXT_MASK])
3459 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3460
Jiri Benc79e1ad12017-11-02 17:04:38 -02003461 err = -EINVAL;
Thomas Grafb60c5112006-08-04 23:05:34 -07003462 ifm = nlmsg_data(nlh);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003463 if (ifm->ifi_index > 0)
Jiri Benc79e1ad12017-11-02 17:04:38 -02003464 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
Jiri Pirko76c9ac02019-09-30 11:48:20 +02003465 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3466 dev = rtnl_dev_get(tgt_net, tb[IFLA_IFNAME],
3467 tb[IFLA_ALT_IFNAME], NULL);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003468 else
Jiri Benc79e1ad12017-11-02 17:04:38 -02003469 goto out;
Thomas Grafb60c5112006-08-04 23:05:34 -07003470
Jiri Benc79e1ad12017-11-02 17:04:38 -02003471 err = -ENODEV;
Eric Dumazeta3d12892009-10-21 10:59:31 +00003472 if (dev == NULL)
Jiri Benc79e1ad12017-11-02 17:04:38 -02003473 goto out;
Eric Dumazeta3d12892009-10-21 10:59:31 +00003474
Jiri Benc79e1ad12017-11-02 17:04:38 -02003475 err = -ENOBUFS;
Greg Rose115c9b82012-02-21 16:54:48 -05003476 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003477 if (nskb == NULL)
Jiri Benc79e1ad12017-11-02 17:04:38 -02003478 goto out;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003479
Jiri Benc79e1ad12017-11-02 17:04:38 -02003480 err = rtnl_fill_ifinfo(nskb, dev, net,
3481 RTM_NEWLINK, NETLINK_CB(skb).portid,
3482 nlh->nlmsg_seq, 0, 0, ext_filter_mask,
Guillaume Naultd4e4fdf2019-10-23 18:39:04 +02003483 0, NULL, 0, netnsid, GFP_KERNEL);
Patrick McHardy26932562007-01-31 23:16:40 -08003484 if (err < 0) {
3485 /* -EMSGSIZE implies BUG in if_nlmsg_size */
3486 WARN_ON(err == -EMSGSIZE);
3487 kfree_skb(nskb);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003488 } else
Eric W. Biederman15e47302012-09-07 20:12:54 +00003489 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
Jiri Benc79e1ad12017-11-02 17:04:38 -02003490out:
3491 if (netnsid >= 0)
3492 put_net(tgt_net);
Thomas Grafb60c5112006-08-04 23:05:34 -07003493
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003494 return err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003495}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003496
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02003497static int rtnl_alt_ifname(int cmd, struct net_device *dev, struct nlattr *attr,
3498 bool *changed, struct netlink_ext_ack *extack)
3499{
3500 char *alt_ifname;
3501 int err;
3502
3503 err = nla_validate(attr, attr->nla_len, IFLA_MAX, ifla_policy, extack);
3504 if (err)
3505 return err;
3506
3507 alt_ifname = nla_data(attr);
3508 if (cmd == RTM_NEWLINKPROP) {
3509 alt_ifname = kstrdup(alt_ifname, GFP_KERNEL);
3510 if (!alt_ifname)
3511 return -ENOMEM;
3512 err = netdev_name_node_alt_create(dev, alt_ifname);
3513 if (err) {
3514 kfree(alt_ifname);
3515 return err;
3516 }
3517 } else if (cmd == RTM_DELLINKPROP) {
3518 err = netdev_name_node_alt_destroy(dev, alt_ifname);
3519 if (err)
3520 return err;
3521 } else {
3522 WARN_ON(1);
3523 return 0;
3524 }
3525
3526 *changed = true;
3527 return 0;
3528}
3529
3530static int rtnl_linkprop(int cmd, struct sk_buff *skb, struct nlmsghdr *nlh,
3531 struct netlink_ext_ack *extack)
3532{
3533 struct net *net = sock_net(skb->sk);
3534 struct nlattr *tb[IFLA_MAX + 1];
3535 struct net_device *dev;
3536 struct ifinfomsg *ifm;
3537 bool changed = false;
3538 struct nlattr *attr;
3539 int err, rem;
3540
3541 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
3542 if (err)
3543 return err;
3544
3545 err = rtnl_ensure_unique_netns(tb, extack, true);
3546 if (err)
3547 return err;
3548
3549 ifm = nlmsg_data(nlh);
Jiri Pirkocc6090e2019-09-30 11:48:19 +02003550 if (ifm->ifi_index > 0)
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02003551 dev = __dev_get_by_index(net, ifm->ifi_index);
Jiri Pirko76c9ac02019-09-30 11:48:20 +02003552 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3553 dev = rtnl_dev_get(net, tb[IFLA_IFNAME],
3554 tb[IFLA_ALT_IFNAME], NULL);
Jiri Pirkocc6090e2019-09-30 11:48:19 +02003555 else
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02003556 return -EINVAL;
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02003557
3558 if (!dev)
3559 return -ENODEV;
3560
3561 if (!tb[IFLA_PROP_LIST])
3562 return 0;
3563
3564 nla_for_each_nested(attr, tb[IFLA_PROP_LIST], rem) {
3565 switch (nla_type(attr)) {
3566 case IFLA_ALT_IFNAME:
3567 err = rtnl_alt_ifname(cmd, dev, attr, &changed, extack);
3568 if (err)
3569 return err;
3570 break;
3571 }
3572 }
3573
3574 if (changed)
3575 netdev_state_change(dev);
3576 return 0;
3577}
3578
3579static int rtnl_newlinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
3580 struct netlink_ext_ack *extack)
3581{
3582 return rtnl_linkprop(RTM_NEWLINKPROP, skb, nlh, extack);
3583}
3584
3585static int rtnl_dellinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
3586 struct netlink_ext_ack *extack)
3587{
3588 return rtnl_linkprop(RTM_DELLINKPROP, skb, nlh, extack);
3589}
3590
Greg Rose115c9b82012-02-21 16:54:48 -05003591static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
Greg Rosec7ac8672011-06-10 01:27:09 +00003592{
Greg Rose115c9b82012-02-21 16:54:48 -05003593 struct net *net = sock_net(skb->sk);
3594 struct net_device *dev;
3595 struct nlattr *tb[IFLA_MAX+1];
3596 u32 ext_filter_mask = 0;
3597 u16 min_ifinfo_dump_size = 0;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02003598 int hdrlen;
Greg Rose115c9b82012-02-21 16:54:48 -05003599
Michal Schmidte5eca6d2014-05-28 14:15:19 +02003600 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3601 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
3602 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
3603
Johannes Berg8cb08172019-04-26 14:07:28 +02003604 if (nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00003605 if (tb[IFLA_EXT_MASK])
3606 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3607 }
Greg Rose115c9b82012-02-21 16:54:48 -05003608
3609 if (!ext_filter_mask)
3610 return NLMSG_GOODSIZE;
3611 /*
3612 * traverse the list of net devices and compute the minimum
3613 * buffer size based upon the filter mask.
3614 */
Florian Westphal6853dd42017-08-09 20:41:51 +02003615 rcu_read_lock();
3616 for_each_netdev_rcu(net, dev) {
Greg Rose115c9b82012-02-21 16:54:48 -05003617 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
3618 if_nlmsg_size(dev,
3619 ext_filter_mask));
3620 }
Florian Westphal6853dd42017-08-09 20:41:51 +02003621 rcu_read_unlock();
Greg Rose115c9b82012-02-21 16:54:48 -05003622
Zhang Shengju93af2052016-11-22 14:14:28 +08003623 return nlmsg_total_size(min_ifinfo_dump_size);
Greg Rosec7ac8672011-06-10 01:27:09 +00003624}
3625
Adrian Bunk42bad1d2007-04-26 00:57:41 -07003626static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627{
3628 int idx;
3629 int s_idx = cb->family;
Christian Brauner87ccbb12018-09-04 21:53:51 +02003630 int type = cb->nlh->nlmsg_type - RTM_BASE;
David Ahernc63586d2018-10-24 12:59:02 -07003631 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632
3633 if (s_idx == 0)
3634 s_idx = 1;
Florian Westphal6853dd42017-08-09 20:41:51 +02003635
Patrick McHardy25239ce2010-04-26 16:02:05 +02003636 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
Florian Westphaladdf9b92017-12-02 21:44:05 +01003637 struct rtnl_link **tab;
Florian Westphaladdf9b92017-12-02 21:44:05 +01003638 struct rtnl_link *link;
Florian Westphal6853dd42017-08-09 20:41:51 +02003639 rtnl_dumpit_func dumpit;
3640
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 if (idx < s_idx || idx == PF_PACKET)
3642 continue;
Florian Westphal6853dd42017-08-09 20:41:51 +02003643
Florian Westphaladdf9b92017-12-02 21:44:05 +01003644 if (type < 0 || type >= RTM_NR_MSGTYPES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 continue;
Florian Westphal6853dd42017-08-09 20:41:51 +02003646
Florian Westphaladdf9b92017-12-02 21:44:05 +01003647 tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]);
3648 if (!tab)
3649 continue;
3650
3651 link = tab[type];
3652 if (!link)
3653 continue;
3654
3655 dumpit = link->dumpit;
Florian Westphal6853dd42017-08-09 20:41:51 +02003656 if (!dumpit)
3657 continue;
3658
Nicolas Dichtel04652772013-03-22 06:28:42 +00003659 if (idx > s_idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 memset(&cb->args[0], 0, sizeof(cb->args));
Nicolas Dichtel04652772013-03-22 06:28:42 +00003661 cb->prev_seq = 0;
3662 cb->seq = 0;
3663 }
David Ahernc63586d2018-10-24 12:59:02 -07003664 ret = dumpit(skb, cb);
Alexey Kodanev5e1acb42018-11-02 19:11:04 +03003665 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 break;
3667 }
3668 cb->family = idx;
3669
David Ahernc63586d2018-10-24 12:59:02 -07003670 return skb->len ? : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671}
3672
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003673struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003674 unsigned int change,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003675 u32 event, gfp_t flags, int *new_nsid,
3676 int new_ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003678 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 struct sk_buff *skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003680 int err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00003681 size_t if_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682
Alexei Starovoitov7f294052013-10-23 16:02:42 -07003683 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003684 if (skb == NULL)
3685 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686
Jiri Benc79e1ad12017-11-02 17:04:38 -02003687 err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
3688 type, 0, 0, change, 0, 0, event,
Guillaume Naultd4e4fdf2019-10-23 18:39:04 +02003689 new_nsid, new_ifindex, -1, flags);
Patrick McHardy26932562007-01-31 23:16:40 -08003690 if (err < 0) {
3691 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
3692 WARN_ON(err == -EMSGSIZE);
3693 kfree_skb(skb);
3694 goto errout;
3695 }
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003696 return skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003697errout:
3698 if (err < 0)
Eric W. Biederman4b3da702007-11-19 22:27:40 -08003699 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003700 return NULL;
3701}
3702
3703void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
3704{
3705 struct net *net = dev_net(dev);
3706
3707 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
3708}
3709
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003710static void rtmsg_ifinfo_event(int type, struct net_device *dev,
3711 unsigned int change, u32 event,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003712 gfp_t flags, int *new_nsid, int new_ifindex)
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003713{
3714 struct sk_buff *skb;
3715
Nicolas Dichteled2a80a2015-05-13 14:19:42 +02003716 if (dev->reg_state != NETREG_REGISTERED)
3717 return;
3718
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003719 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid,
3720 new_ifindex);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003721 if (skb)
3722 rtmsg_ifinfo_send(skb, dev, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723}
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003724
3725void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
3726 gfp_t flags)
3727{
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003728 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3729 NULL, 0);
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003730}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003732void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003733 gfp_t flags, int *new_nsid, int new_ifindex)
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003734{
3735 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003736 new_nsid, new_ifindex);
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003737}
3738
John Fastabendd83b0602012-04-15 06:44:08 +00003739static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
3740 struct net_device *dev,
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003741 u8 *addr, u16 vid, u32 pid, u32 seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003742 int type, unsigned int flags,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003743 int nlflags, u16 ndm_state)
John Fastabendd83b0602012-04-15 06:44:08 +00003744{
3745 struct nlmsghdr *nlh;
3746 struct ndmsg *ndm;
3747
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003748 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
John Fastabendd83b0602012-04-15 06:44:08 +00003749 if (!nlh)
3750 return -EMSGSIZE;
3751
3752 ndm = nlmsg_data(nlh);
3753 ndm->ndm_family = AF_BRIDGE;
3754 ndm->ndm_pad1 = 0;
3755 ndm->ndm_pad2 = 0;
3756 ndm->ndm_flags = flags;
3757 ndm->ndm_type = 0;
3758 ndm->ndm_ifindex = dev->ifindex;
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003759 ndm->ndm_state = ndm_state;
John Fastabendd83b0602012-04-15 06:44:08 +00003760
3761 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
3762 goto nla_put_failure;
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003763 if (vid)
3764 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
3765 goto nla_put_failure;
John Fastabendd83b0602012-04-15 06:44:08 +00003766
Johannes Berg053c0952015-01-16 22:09:00 +01003767 nlmsg_end(skb, nlh);
3768 return 0;
John Fastabendd83b0602012-04-15 06:44:08 +00003769
3770nla_put_failure:
3771 nlmsg_cancel(skb, nlh);
3772 return -EMSGSIZE;
3773}
3774
John Fastabend3ff661c2012-04-15 06:44:14 +00003775static inline size_t rtnl_fdb_nlmsg_size(void)
3776{
Sabrina Dubrocaf82ef3e2016-11-18 15:50:39 +01003777 return NLMSG_ALIGN(sizeof(struct ndmsg)) +
3778 nla_total_size(ETH_ALEN) + /* NDA_LLADDR */
3779 nla_total_size(sizeof(u16)) + /* NDA_VLAN */
3780 0;
John Fastabend3ff661c2012-04-15 06:44:14 +00003781}
3782
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003783static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
3784 u16 ndm_state)
John Fastabend3ff661c2012-04-15 06:44:14 +00003785{
3786 struct net *net = dev_net(dev);
3787 struct sk_buff *skb;
3788 int err = -ENOBUFS;
3789
3790 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
3791 if (!skb)
3792 goto errout;
3793
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003794 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003795 0, 0, type, NTF_SELF, 0, ndm_state);
John Fastabend3ff661c2012-04-15 06:44:14 +00003796 if (err < 0) {
3797 kfree_skb(skb);
3798 goto errout;
3799 }
3800
3801 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
3802 return;
3803errout:
3804 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
3805}
3806
Bart Van Asschea9869672019-03-25 09:17:22 -07003807/*
Vlad Yasevich090096b2013-03-06 15:39:42 +00003808 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3809 */
3810int ndo_dflt_fdb_add(struct ndmsg *ndm,
3811 struct nlattr *tb[],
3812 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003813 const unsigned char *addr, u16 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003814 u16 flags)
3815{
3816 int err = -EINVAL;
3817
3818 /* If aging addresses are supported device will need to
3819 * implement its own handler for this.
3820 */
3821 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
3822 pr_info("%s: FDB only supports static addresses\n", dev->name);
3823 return err;
3824 }
3825
Or Gerlitz65891fe2014-12-14 18:19:05 +02003826 if (vid) {
3827 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
3828 return err;
3829 }
3830
Vlad Yasevich090096b2013-03-06 15:39:42 +00003831 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3832 err = dev_uc_add_excl(dev, addr);
3833 else if (is_multicast_ether_addr(addr))
3834 err = dev_mc_add_excl(dev, addr);
3835
3836 /* Only return duplicate errors if NLM_F_EXCL is set */
3837 if (err == -EEXIST && !(flags & NLM_F_EXCL))
3838 err = 0;
3839
3840 return err;
3841}
3842EXPORT_SYMBOL(ndo_dflt_fdb_add);
3843
Florian Westphalb88d12e2017-10-10 17:10:04 +02003844static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
3845 struct netlink_ext_ack *extack)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003846{
3847 u16 vid = 0;
3848
3849 if (vlan_attr) {
3850 if (nla_len(vlan_attr) != sizeof(u16)) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003851 NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
Jiri Pirkof6f64242014-11-28 14:34:15 +01003852 return -EINVAL;
3853 }
3854
3855 vid = nla_get_u16(vlan_attr);
3856
3857 if (!vid || vid >= VLAN_VID_MASK) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003858 NL_SET_ERR_MSG(extack, "invalid vlan id");
Jiri Pirkof6f64242014-11-28 14:34:15 +01003859 return -EINVAL;
3860 }
3861 }
3862 *p_vid = vid;
3863 return 0;
3864}
3865
David Ahernc21ef3e2017-04-16 09:48:24 -07003866static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
3867 struct netlink_ext_ack *extack)
John Fastabend77162022012-04-15 06:43:56 +00003868{
3869 struct net *net = sock_net(skb->sk);
John Fastabend77162022012-04-15 06:43:56 +00003870 struct ndmsg *ndm;
3871 struct nlattr *tb[NDA_MAX+1];
3872 struct net_device *dev;
3873 u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01003874 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00003875 int err;
3876
Johannes Berg8cb08172019-04-26 14:07:28 +02003877 err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
3878 extack);
John Fastabend77162022012-04-15 06:43:56 +00003879 if (err < 0)
3880 return err;
3881
3882 ndm = nlmsg_data(nlh);
3883 if (ndm->ndm_ifindex == 0) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003884 NL_SET_ERR_MSG(extack, "invalid ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003885 return -EINVAL;
3886 }
3887
3888 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3889 if (dev == NULL) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003890 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003891 return -ENODEV;
3892 }
3893
3894 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003895 NL_SET_ERR_MSG(extack, "invalid address");
John Fastabend77162022012-04-15 06:43:56 +00003896 return -EINVAL;
3897 }
3898
Ido Schimmelda715772018-10-29 20:36:43 +00003899 if (dev->type != ARPHRD_ETHER) {
3900 NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices");
3901 return -EINVAL;
3902 }
3903
John Fastabend77162022012-04-15 06:43:56 +00003904 addr = nla_data(tb[NDA_LLADDR]);
John Fastabend77162022012-04-15 06:43:56 +00003905
Florian Westphalb88d12e2017-10-10 17:10:04 +02003906 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
Jiri Pirkof6f64242014-11-28 14:34:15 +01003907 if (err)
3908 return err;
3909
John Fastabend77162022012-04-15 06:43:56 +00003910 err = -EOPNOTSUPP;
3911
3912 /* Support fdb on master device the net/bridge default case */
3913 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3914 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003915 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3916 const struct net_device_ops *ops = br_dev->netdev_ops;
3917
Jiri Pirkof6f64242014-11-28 14:34:15 +01003918 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
Petr Machata87b09842019-01-16 23:06:50 +00003919 nlh->nlmsg_flags, extack);
John Fastabend77162022012-04-15 06:43:56 +00003920 if (err)
3921 goto out;
3922 else
3923 ndm->ndm_flags &= ~NTF_MASTER;
3924 }
3925
3926 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00003927 if ((ndm->ndm_flags & NTF_SELF)) {
3928 if (dev->netdev_ops->ndo_fdb_add)
3929 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003930 vid,
Petr Machata87b09842019-01-16 23:06:50 +00003931 nlh->nlmsg_flags,
3932 extack);
Vlad Yasevich090096b2013-03-06 15:39:42 +00003933 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01003934 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003935 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00003936
John Fastabend3ff661c2012-04-15 06:44:14 +00003937 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003938 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
3939 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00003940 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00003941 }
John Fastabend77162022012-04-15 06:43:56 +00003942 }
3943out:
3944 return err;
3945}
3946
Bart Van Asschea9869672019-03-25 09:17:22 -07003947/*
Vlad Yasevich090096b2013-03-06 15:39:42 +00003948 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3949 */
3950int ndo_dflt_fdb_del(struct ndmsg *ndm,
3951 struct nlattr *tb[],
3952 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003953 const unsigned char *addr, u16 vid)
Vlad Yasevich090096b2013-03-06 15:39:42 +00003954{
Alexander Duyckc8a89c42014-07-15 15:15:20 -07003955 int err = -EINVAL;
Vlad Yasevich090096b2013-03-06 15:39:42 +00003956
3957 /* If aging addresses are supported device will need to
3958 * implement its own handler for this.
3959 */
Sridhar Samudrala64535992013-08-08 15:19:48 -07003960 if (!(ndm->ndm_state & NUD_PERMANENT)) {
Vlad Yasevich090096b2013-03-06 15:39:42 +00003961 pr_info("%s: FDB only supports static addresses\n", dev->name);
Alexander Duyckc8a89c42014-07-15 15:15:20 -07003962 return err;
Vlad Yasevich090096b2013-03-06 15:39:42 +00003963 }
3964
3965 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3966 err = dev_uc_del(dev, addr);
3967 else if (is_multicast_ether_addr(addr))
3968 err = dev_mc_del(dev, addr);
Vlad Yasevich090096b2013-03-06 15:39:42 +00003969
3970 return err;
3971}
3972EXPORT_SYMBOL(ndo_dflt_fdb_del);
3973
David Ahernc21ef3e2017-04-16 09:48:24 -07003974static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
3975 struct netlink_ext_ack *extack)
John Fastabend77162022012-04-15 06:43:56 +00003976{
3977 struct net *net = sock_net(skb->sk);
3978 struct ndmsg *ndm;
Vlad Yasevich1690be62013-02-13 12:00:18 +00003979 struct nlattr *tb[NDA_MAX+1];
John Fastabend77162022012-04-15 06:43:56 +00003980 struct net_device *dev;
3981 int err = -EINVAL;
3982 __u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01003983 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00003984
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07003985 if (!netlink_capable(skb, CAP_NET_ADMIN))
Vlad Yasevich1690be62013-02-13 12:00:18 +00003986 return -EPERM;
3987
Johannes Berg8cb08172019-04-26 14:07:28 +02003988 err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
3989 extack);
Vlad Yasevich1690be62013-02-13 12:00:18 +00003990 if (err < 0)
3991 return err;
John Fastabend77162022012-04-15 06:43:56 +00003992
3993 ndm = nlmsg_data(nlh);
3994 if (ndm->ndm_ifindex == 0) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003995 NL_SET_ERR_MSG(extack, "invalid ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003996 return -EINVAL;
3997 }
3998
3999 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
4000 if (dev == NULL) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004001 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabend77162022012-04-15 06:43:56 +00004002 return -ENODEV;
4003 }
4004
Vlad Yasevich1690be62013-02-13 12:00:18 +00004005 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004006 NL_SET_ERR_MSG(extack, "invalid address");
John Fastabend77162022012-04-15 06:43:56 +00004007 return -EINVAL;
4008 }
4009
Ido Schimmelda715772018-10-29 20:36:43 +00004010 if (dev->type != ARPHRD_ETHER) {
4011 NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices");
4012 return -EINVAL;
4013 }
4014
Vlad Yasevich1690be62013-02-13 12:00:18 +00004015 addr = nla_data(tb[NDA_LLADDR]);
Vlad Yasevich1690be62013-02-13 12:00:18 +00004016
Florian Westphalb88d12e2017-10-10 17:10:04 +02004017 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
Jiri Pirkof6f64242014-11-28 14:34:15 +01004018 if (err)
4019 return err;
4020
John Fastabend77162022012-04-15 06:43:56 +00004021 err = -EOPNOTSUPP;
4022
4023 /* Support fdb on master device the net/bridge default case */
4024 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
4025 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00004026 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4027 const struct net_device_ops *ops = br_dev->netdev_ops;
John Fastabend77162022012-04-15 06:43:56 +00004028
Jiri Pirko898e5062013-01-03 22:48:52 +00004029 if (ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01004030 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00004031
4032 if (err)
4033 goto out;
4034 else
4035 ndm->ndm_flags &= ~NTF_MASTER;
4036 }
4037
4038 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00004039 if (ndm->ndm_flags & NTF_SELF) {
4040 if (dev->netdev_ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01004041 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
4042 vid);
Vlad Yasevich090096b2013-03-06 15:39:42 +00004043 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01004044 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00004045
John Fastabend3ff661c2012-04-15 06:44:14 +00004046 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00004047 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
4048 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00004049 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00004050 }
John Fastabend77162022012-04-15 06:43:56 +00004051 }
4052out:
4053 return err;
4054}
4055
John Fastabendd83b0602012-04-15 06:44:08 +00004056static int nlmsg_populate_fdb(struct sk_buff *skb,
4057 struct netlink_callback *cb,
4058 struct net_device *dev,
4059 int *idx,
4060 struct netdev_hw_addr_list *list)
4061{
4062 struct netdev_hw_addr *ha;
4063 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00004064 u32 portid, seq;
John Fastabendd83b0602012-04-15 06:44:08 +00004065
Eric W. Biederman15e47302012-09-07 20:12:54 +00004066 portid = NETLINK_CB(cb->skb).portid;
John Fastabendd83b0602012-04-15 06:44:08 +00004067 seq = cb->nlh->nlmsg_seq;
4068
4069 list_for_each_entry(ha, &list->list, list) {
Roopa Prabhud2976532016-08-30 21:56:45 -07004070 if (*idx < cb->args[2])
John Fastabendd83b0602012-04-15 06:44:08 +00004071 goto skip;
4072
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00004073 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
John Fastabenda7a558f2012-11-01 16:23:10 +00004074 portid, seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01004075 RTM_NEWNEIGH, NTF_SELF,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00004076 NLM_F_MULTI, NUD_PERMANENT);
John Fastabendd83b0602012-04-15 06:44:08 +00004077 if (err < 0)
4078 return err;
4079skip:
4080 *idx += 1;
4081 }
4082 return 0;
4083}
4084
4085/**
Ben Hutchings2c530402012-07-10 10:55:09 +00004086 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
Bart Van Asschea9869672019-03-25 09:17:22 -07004087 * @skb: socket buffer to store message in
4088 * @cb: netlink callback
John Fastabendd83b0602012-04-15 06:44:08 +00004089 * @dev: netdevice
Bart Van Asschea9869672019-03-25 09:17:22 -07004090 * @filter_dev: ignored
4091 * @idx: the number of FDB table entries dumped is added to *@idx
John Fastabendd83b0602012-04-15 06:44:08 +00004092 *
4093 * Default netdevice operation to dump the existing unicast address list.
John Fastabend91f3e7b2013-03-29 08:18:37 +00004094 * Returns number of addresses from list put in skb.
John Fastabendd83b0602012-04-15 06:44:08 +00004095 */
4096int ndo_dflt_fdb_dump(struct sk_buff *skb,
4097 struct netlink_callback *cb,
4098 struct net_device *dev,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04004099 struct net_device *filter_dev,
Roopa Prabhud2976532016-08-30 21:56:45 -07004100 int *idx)
John Fastabendd83b0602012-04-15 06:44:08 +00004101{
4102 int err;
4103
Eric Dumazet68883892018-12-04 09:40:35 -08004104 if (dev->type != ARPHRD_ETHER)
4105 return -EINVAL;
4106
John Fastabendd83b0602012-04-15 06:44:08 +00004107 netif_addr_lock_bh(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -07004108 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
John Fastabendd83b0602012-04-15 06:44:08 +00004109 if (err)
4110 goto out;
Zhang Shengju2934c9d2016-11-30 16:37:34 +08004111 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
John Fastabendd83b0602012-04-15 06:44:08 +00004112out:
4113 netif_addr_unlock_bh(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -07004114 return err;
John Fastabendd83b0602012-04-15 06:44:08 +00004115}
4116EXPORT_SYMBOL(ndo_dflt_fdb_dump);
4117
David Ahern8c6e1372018-10-07 20:16:44 -07004118static int valid_fdb_dump_strict(const struct nlmsghdr *nlh,
4119 int *br_idx, int *brport_idx,
4120 struct netlink_ext_ack *extack)
4121{
4122 struct nlattr *tb[NDA_MAX + 1];
4123 struct ndmsg *ndm;
4124 int err, i;
4125
4126 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
4127 NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request");
4128 return -EINVAL;
4129 }
4130
4131 ndm = nlmsg_data(nlh);
4132 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
4133 ndm->ndm_flags || ndm->ndm_type) {
Nikolay Aleksandrov8b730182019-10-29 13:59:32 +02004134 NL_SET_ERR_MSG(extack, "Invalid values in header for fdb dump request");
David Ahern8c6e1372018-10-07 20:16:44 -07004135 return -EINVAL;
4136 }
4137
Johannes Berg8cb08172019-04-26 14:07:28 +02004138 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
4139 NDA_MAX, NULL, extack);
David Ahern8c6e1372018-10-07 20:16:44 -07004140 if (err < 0)
4141 return err;
4142
4143 *brport_idx = ndm->ndm_ifindex;
4144 for (i = 0; i <= NDA_MAX; ++i) {
4145 if (!tb[i])
4146 continue;
4147
4148 switch (i) {
4149 case NDA_IFINDEX:
4150 if (nla_len(tb[i]) != sizeof(u32)) {
4151 NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request");
4152 return -EINVAL;
4153 }
4154 *brport_idx = nla_get_u32(tb[NDA_IFINDEX]);
4155 break;
4156 case NDA_MASTER:
4157 if (nla_len(tb[i]) != sizeof(u32)) {
4158 NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request");
4159 return -EINVAL;
4160 }
4161 *br_idx = nla_get_u32(tb[NDA_MASTER]);
4162 break;
4163 default:
4164 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request");
4165 return -EINVAL;
4166 }
4167 }
4168
4169 return 0;
4170}
4171
David Ahern8dfbda12018-10-07 20:16:43 -07004172static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh,
4173 int *br_idx, int *brport_idx,
4174 struct netlink_ext_ack *extack)
4175{
David Ahern8dfbda12018-10-07 20:16:43 -07004176 struct nlattr *tb[IFLA_MAX+1];
4177 int err;
4178
4179 /* A hack to preserve kernel<->userspace interface.
4180 * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0.
4181 * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails.
4182 * So, check for ndmsg with an optional u32 attribute (not used here).
4183 * Fortunately these sizes don't conflict with the size of ifinfomsg
4184 * with an optional attribute.
4185 */
4186 if (nlmsg_len(nlh) != sizeof(struct ndmsg) &&
4187 (nlmsg_len(nlh) != sizeof(struct ndmsg) +
4188 nla_attr_size(sizeof(u32)))) {
David Ahern4565d7e2018-10-08 13:57:24 -07004189 struct ifinfomsg *ifm;
4190
Johannes Berg8cb08172019-04-26 14:07:28 +02004191 err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
4192 tb, IFLA_MAX, ifla_policy,
4193 extack);
David Ahern8dfbda12018-10-07 20:16:43 -07004194 if (err < 0) {
4195 return -EINVAL;
4196 } else if (err == 0) {
4197 if (tb[IFLA_MASTER])
4198 *br_idx = nla_get_u32(tb[IFLA_MASTER]);
4199 }
4200
David Ahern4565d7e2018-10-08 13:57:24 -07004201 ifm = nlmsg_data(nlh);
David Ahern8dfbda12018-10-07 20:16:43 -07004202 *brport_idx = ifm->ifi_index;
4203 }
4204 return 0;
4205}
4206
John Fastabend77162022012-04-15 06:43:56 +00004207static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
4208{
John Fastabend77162022012-04-15 06:43:56 +00004209 struct net_device *dev;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004210 struct net_device *br_dev = NULL;
4211 const struct net_device_ops *ops = NULL;
4212 const struct net_device_ops *cops = NULL;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004213 struct net *net = sock_net(skb->sk);
Roopa Prabhud2976532016-08-30 21:56:45 -07004214 struct hlist_head *head;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004215 int brport_idx = 0;
4216 int br_idx = 0;
Roopa Prabhud2976532016-08-30 21:56:45 -07004217 int h, s_h;
4218 int idx = 0, s_idx;
4219 int err = 0;
4220 int fidx = 0;
John Fastabend77162022012-04-15 06:43:56 +00004221
David Ahern8c6e1372018-10-07 20:16:44 -07004222 if (cb->strict_check)
4223 err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx,
4224 cb->extack);
4225 else
4226 err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx,
4227 cb->extack);
David Ahern8dfbda12018-10-07 20:16:43 -07004228 if (err < 0)
4229 return err;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004230
4231 if (br_idx) {
4232 br_dev = __dev_get_by_index(net, br_idx);
4233 if (!br_dev)
4234 return -ENODEV;
4235
4236 ops = br_dev->netdev_ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004237 }
4238
Roopa Prabhud2976532016-08-30 21:56:45 -07004239 s_h = cb->args[0];
4240 s_idx = cb->args[1];
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004241
Roopa Prabhud2976532016-08-30 21:56:45 -07004242 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4243 idx = 0;
4244 head = &net->dev_index_head[h];
4245 hlist_for_each_entry(dev, head, index_hlist) {
4246
4247 if (brport_idx && (dev->ifindex != brport_idx))
4248 continue;
4249
4250 if (!br_idx) { /* user did not specify a specific bridge */
4251 if (dev->priv_flags & IFF_BRIDGE_PORT) {
4252 br_dev = netdev_master_upper_dev_get(dev);
4253 cops = br_dev->netdev_ops;
4254 }
4255 } else {
4256 if (dev != br_dev &&
4257 !(dev->priv_flags & IFF_BRIDGE_PORT))
4258 continue;
4259
4260 if (br_dev != netdev_master_upper_dev_get(dev) &&
4261 !(dev->priv_flags & IFF_EBRIDGE))
4262 continue;
4263 cops = ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004264 }
4265
Roopa Prabhud2976532016-08-30 21:56:45 -07004266 if (idx < s_idx)
4267 goto cont;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004268
Roopa Prabhud2976532016-08-30 21:56:45 -07004269 if (dev->priv_flags & IFF_BRIDGE_PORT) {
4270 if (cops && cops->ndo_fdb_dump) {
4271 err = cops->ndo_fdb_dump(skb, cb,
4272 br_dev, dev,
4273 &fidx);
4274 if (err == -EMSGSIZE)
4275 goto out;
4276 }
4277 }
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004278
Roopa Prabhud2976532016-08-30 21:56:45 -07004279 if (dev->netdev_ops->ndo_fdb_dump)
4280 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
4281 dev, NULL,
4282 &fidx);
4283 else
4284 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
4285 &fidx);
4286 if (err == -EMSGSIZE)
4287 goto out;
4288
4289 cops = NULL;
4290
4291 /* reset fdb offset to 0 for rest of the interfaces */
4292 cb->args[2] = 0;
4293 fidx = 0;
4294cont:
4295 idx++;
John Fastabend77162022012-04-15 06:43:56 +00004296 }
John Fastabend77162022012-04-15 06:43:56 +00004297 }
John Fastabend77162022012-04-15 06:43:56 +00004298
Roopa Prabhud2976532016-08-30 21:56:45 -07004299out:
4300 cb->args[0] = h;
4301 cb->args[1] = idx;
4302 cb->args[2] = fidx;
4303
John Fastabend77162022012-04-15 06:43:56 +00004304 return skb->len;
4305}
4306
Roopa Prabhu5b2f94b2018-12-15 22:35:08 -08004307static int valid_fdb_get_strict(const struct nlmsghdr *nlh,
4308 struct nlattr **tb, u8 *ndm_flags,
4309 int *br_idx, int *brport_idx, u8 **addr,
4310 u16 *vid, struct netlink_ext_ack *extack)
4311{
4312 struct ndmsg *ndm;
4313 int err, i;
4314
4315 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
4316 NL_SET_ERR_MSG(extack, "Invalid header for fdb get request");
4317 return -EINVAL;
4318 }
4319
4320 ndm = nlmsg_data(nlh);
4321 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
4322 ndm->ndm_type) {
4323 NL_SET_ERR_MSG(extack, "Invalid values in header for fdb get request");
4324 return -EINVAL;
4325 }
4326
4327 if (ndm->ndm_flags & ~(NTF_MASTER | NTF_SELF)) {
4328 NL_SET_ERR_MSG(extack, "Invalid flags in header for fdb get request");
4329 return -EINVAL;
4330 }
4331
Johannes Berg8cb08172019-04-26 14:07:28 +02004332 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
4333 NDA_MAX, nda_policy, extack);
Roopa Prabhu5b2f94b2018-12-15 22:35:08 -08004334 if (err < 0)
4335 return err;
4336
4337 *ndm_flags = ndm->ndm_flags;
4338 *brport_idx = ndm->ndm_ifindex;
4339 for (i = 0; i <= NDA_MAX; ++i) {
4340 if (!tb[i])
4341 continue;
4342
4343 switch (i) {
4344 case NDA_MASTER:
4345 *br_idx = nla_get_u32(tb[i]);
4346 break;
4347 case NDA_LLADDR:
4348 if (nla_len(tb[i]) != ETH_ALEN) {
4349 NL_SET_ERR_MSG(extack, "Invalid address in fdb get request");
4350 return -EINVAL;
4351 }
4352 *addr = nla_data(tb[i]);
4353 break;
4354 case NDA_VLAN:
4355 err = fdb_vid_parse(tb[i], vid, extack);
4356 if (err)
4357 return err;
4358 break;
4359 case NDA_VNI:
4360 break;
4361 default:
4362 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb get request");
4363 return -EINVAL;
4364 }
4365 }
4366
4367 return 0;
4368}
4369
4370static int rtnl_fdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh,
4371 struct netlink_ext_ack *extack)
4372{
4373 struct net_device *dev = NULL, *br_dev = NULL;
4374 const struct net_device_ops *ops = NULL;
4375 struct net *net = sock_net(in_skb->sk);
4376 struct nlattr *tb[NDA_MAX + 1];
4377 struct sk_buff *skb;
4378 int brport_idx = 0;
4379 u8 ndm_flags = 0;
4380 int br_idx = 0;
4381 u8 *addr = NULL;
4382 u16 vid = 0;
4383 int err;
4384
4385 err = valid_fdb_get_strict(nlh, tb, &ndm_flags, &br_idx,
4386 &brport_idx, &addr, &vid, extack);
4387 if (err < 0)
4388 return err;
4389
Nikolay Aleksandrovf989d032018-12-30 14:33:20 +02004390 if (!addr) {
4391 NL_SET_ERR_MSG(extack, "Missing lookup address for fdb get request");
4392 return -EINVAL;
4393 }
4394
Roopa Prabhu5b2f94b2018-12-15 22:35:08 -08004395 if (brport_idx) {
4396 dev = __dev_get_by_index(net, brport_idx);
4397 if (!dev) {
4398 NL_SET_ERR_MSG(extack, "Unknown device ifindex");
4399 return -ENODEV;
4400 }
4401 }
4402
4403 if (br_idx) {
4404 if (dev) {
4405 NL_SET_ERR_MSG(extack, "Master and device are mutually exclusive");
4406 return -EINVAL;
4407 }
4408
4409 br_dev = __dev_get_by_index(net, br_idx);
4410 if (!br_dev) {
4411 NL_SET_ERR_MSG(extack, "Invalid master ifindex");
4412 return -EINVAL;
4413 }
4414 ops = br_dev->netdev_ops;
4415 }
4416
4417 if (dev) {
4418 if (!ndm_flags || (ndm_flags & NTF_MASTER)) {
4419 if (!(dev->priv_flags & IFF_BRIDGE_PORT)) {
4420 NL_SET_ERR_MSG(extack, "Device is not a bridge port");
4421 return -EINVAL;
4422 }
4423 br_dev = netdev_master_upper_dev_get(dev);
4424 if (!br_dev) {
4425 NL_SET_ERR_MSG(extack, "Master of device not found");
4426 return -EINVAL;
4427 }
4428 ops = br_dev->netdev_ops;
4429 } else {
4430 if (!(ndm_flags & NTF_SELF)) {
4431 NL_SET_ERR_MSG(extack, "Missing NTF_SELF");
4432 return -EINVAL;
4433 }
4434 ops = dev->netdev_ops;
4435 }
4436 }
4437
4438 if (!br_dev && !dev) {
4439 NL_SET_ERR_MSG(extack, "No device specified");
4440 return -ENODEV;
4441 }
4442
4443 if (!ops || !ops->ndo_fdb_get) {
4444 NL_SET_ERR_MSG(extack, "Fdb get operation not supported by device");
4445 return -EOPNOTSUPP;
4446 }
4447
4448 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
4449 if (!skb)
4450 return -ENOBUFS;
4451
4452 if (br_dev)
4453 dev = br_dev;
4454 err = ops->ndo_fdb_get(skb, tb, dev, addr, vid,
4455 NETLINK_CB(in_skb).portid,
4456 nlh->nlmsg_seq, extack);
4457 if (err)
4458 goto out;
4459
4460 return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
4461out:
4462 kfree_skb(skb);
4463 return err;
4464}
4465
Scott Feldman2c3c0312014-11-28 14:34:25 +01004466static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
4467 unsigned int attrnum, unsigned int flag)
4468{
4469 if (mask & flag)
4470 return nla_put_u8(skb, attrnum, !!(flags & flag));
4471 return 0;
4472}
4473
John Fastabend815cccb2012-10-24 08:13:09 +00004474int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Scott Feldman2c3c0312014-11-28 14:34:25 +01004475 struct net_device *dev, u16 mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -07004476 u32 flags, u32 mask, int nlflags,
4477 u32 filter_mask,
4478 int (*vlan_fill)(struct sk_buff *skb,
4479 struct net_device *dev,
4480 u32 filter_mask))
John Fastabend815cccb2012-10-24 08:13:09 +00004481{
4482 struct nlmsghdr *nlh;
4483 struct ifinfomsg *ifm;
4484 struct nlattr *br_afspec;
Scott Feldman2c3c0312014-11-28 14:34:25 +01004485 struct nlattr *protinfo;
John Fastabend815cccb2012-10-24 08:13:09 +00004486 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Jiri Pirko898e5062013-01-03 22:48:52 +00004487 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07004488 int err = 0;
John Fastabend815cccb2012-10-24 08:13:09 +00004489
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02004490 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
John Fastabend815cccb2012-10-24 08:13:09 +00004491 if (nlh == NULL)
4492 return -EMSGSIZE;
4493
4494 ifm = nlmsg_data(nlh);
4495 ifm->ifi_family = AF_BRIDGE;
4496 ifm->__ifi_pad = 0;
4497 ifm->ifi_type = dev->type;
4498 ifm->ifi_index = dev->ifindex;
4499 ifm->ifi_flags = dev_get_flags(dev);
4500 ifm->ifi_change = 0;
4501
4502
4503 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4504 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4505 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00004506 (br_dev &&
4507 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
John Fastabend815cccb2012-10-24 08:13:09 +00004508 (dev->addr_len &&
4509 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02004510 (dev->ifindex != dev_get_iflink(dev) &&
4511 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
John Fastabend815cccb2012-10-24 08:13:09 +00004512 goto nla_put_failure;
4513
Michal Kubecekae0be8d2019-04-26 11:13:06 +02004514 br_afspec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
John Fastabend815cccb2012-10-24 08:13:09 +00004515 if (!br_afspec)
4516 goto nla_put_failure;
4517
Roopa Prabhu1d460b92014-12-08 14:04:20 -08004518 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
John Fastabend815cccb2012-10-24 08:13:09 +00004519 nla_nest_cancel(skb, br_afspec);
4520 goto nla_put_failure;
4521 }
Roopa Prabhu1d460b92014-12-08 14:04:20 -08004522
4523 if (mode != BRIDGE_MODE_UNDEF) {
4524 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
4525 nla_nest_cancel(skb, br_afspec);
4526 goto nla_put_failure;
4527 }
4528 }
Scott Feldman7d4f8d82015-06-22 00:27:17 -07004529 if (vlan_fill) {
4530 err = vlan_fill(skb, dev, filter_mask);
4531 if (err) {
4532 nla_nest_cancel(skb, br_afspec);
4533 goto nla_put_failure;
4534 }
4535 }
John Fastabend815cccb2012-10-24 08:13:09 +00004536 nla_nest_end(skb, br_afspec);
4537
Michal Kubecekae0be8d2019-04-26 11:13:06 +02004538 protinfo = nla_nest_start(skb, IFLA_PROTINFO);
Scott Feldman2c3c0312014-11-28 14:34:25 +01004539 if (!protinfo)
4540 goto nla_put_failure;
4541
4542 if (brport_nla_put_flag(skb, flags, mask,
4543 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
4544 brport_nla_put_flag(skb, flags, mask,
4545 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
4546 brport_nla_put_flag(skb, flags, mask,
4547 IFLA_BRPORT_FAST_LEAVE,
4548 BR_MULTICAST_FAST_LEAVE) ||
4549 brport_nla_put_flag(skb, flags, mask,
4550 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
4551 brport_nla_put_flag(skb, flags, mask,
4552 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
4553 brport_nla_put_flag(skb, flags, mask,
4554 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
4555 brport_nla_put_flag(skb, flags, mask,
4556 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
4557 brport_nla_put_flag(skb, flags, mask,
4558 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
4559 nla_nest_cancel(skb, protinfo);
4560 goto nla_put_failure;
4561 }
4562
4563 nla_nest_end(skb, protinfo);
4564
Johannes Berg053c0952015-01-16 22:09:00 +01004565 nlmsg_end(skb, nlh);
4566 return 0;
John Fastabend815cccb2012-10-24 08:13:09 +00004567nla_put_failure:
4568 nlmsg_cancel(skb, nlh);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07004569 return err ? err : -EMSGSIZE;
John Fastabend815cccb2012-10-24 08:13:09 +00004570}
Scott Feldman7d4f8d82015-06-22 00:27:17 -07004571EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
John Fastabend815cccb2012-10-24 08:13:09 +00004572
David Ahern2d011be2018-10-07 20:16:31 -07004573static int valid_bridge_getlink_req(const struct nlmsghdr *nlh,
4574 bool strict_check, u32 *filter_mask,
4575 struct netlink_ext_ack *extack)
4576{
4577 struct nlattr *tb[IFLA_MAX+1];
4578 int err, i;
4579
4580 if (strict_check) {
4581 struct ifinfomsg *ifm;
4582
4583 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
4584 NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump");
4585 return -EINVAL;
4586 }
4587
4588 ifm = nlmsg_data(nlh);
4589 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
4590 ifm->ifi_change || ifm->ifi_index) {
4591 NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request");
4592 return -EINVAL;
4593 }
4594
Johannes Berg8cb08172019-04-26 14:07:28 +02004595 err = nlmsg_parse_deprecated_strict(nlh,
4596 sizeof(struct ifinfomsg),
4597 tb, IFLA_MAX, ifla_policy,
4598 extack);
David Ahern2d011be2018-10-07 20:16:31 -07004599 } else {
Johannes Berg8cb08172019-04-26 14:07:28 +02004600 err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
4601 tb, IFLA_MAX, ifla_policy,
4602 extack);
David Ahern2d011be2018-10-07 20:16:31 -07004603 }
4604 if (err < 0)
4605 return err;
4606
4607 /* new attributes should only be added with strict checking */
4608 for (i = 0; i <= IFLA_MAX; ++i) {
4609 if (!tb[i])
4610 continue;
4611
4612 switch (i) {
4613 case IFLA_EXT_MASK:
4614 *filter_mask = nla_get_u32(tb[i]);
4615 break;
4616 default:
4617 if (strict_check) {
4618 NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request");
4619 return -EINVAL;
4620 }
4621 }
4622 }
4623
4624 return 0;
4625}
4626
John Fastabende5a55a82012-10-24 08:12:57 +00004627static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
4628{
David Ahern2d011be2018-10-07 20:16:31 -07004629 const struct nlmsghdr *nlh = cb->nlh;
John Fastabende5a55a82012-10-24 08:12:57 +00004630 struct net *net = sock_net(skb->sk);
4631 struct net_device *dev;
4632 int idx = 0;
4633 u32 portid = NETLINK_CB(cb->skb).portid;
David Ahern2d011be2018-10-07 20:16:31 -07004634 u32 seq = nlh->nlmsg_seq;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00004635 u32 filter_mask = 0;
Roopa Prabhud64f69b2015-09-15 14:44:29 -07004636 int err;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00004637
David Ahern2d011be2018-10-07 20:16:31 -07004638 err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask,
4639 cb->extack);
4640 if (err < 0 && cb->strict_check)
4641 return err;
John Fastabende5a55a82012-10-24 08:12:57 +00004642
4643 rcu_read_lock();
4644 for_each_netdev_rcu(net, dev) {
4645 const struct net_device_ops *ops = dev->netdev_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00004646 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
John Fastabende5a55a82012-10-24 08:12:57 +00004647
Jiri Pirko898e5062013-01-03 22:48:52 +00004648 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07004649 if (idx >= cb->args[0]) {
4650 err = br_dev->netdev_ops->ndo_bridge_getlink(
4651 skb, portid, seq, dev,
4652 filter_mask, NLM_F_MULTI);
David Ahernf6c57752017-05-15 23:19:17 -07004653 if (err < 0 && err != -EOPNOTSUPP) {
4654 if (likely(skb->len))
4655 break;
4656
4657 goto out_err;
4658 }
Roopa Prabhud64f69b2015-09-15 14:44:29 -07004659 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00004660 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00004661 }
4662
4663 if (ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07004664 if (idx >= cb->args[0]) {
4665 err = ops->ndo_bridge_getlink(skb, portid,
4666 seq, dev,
4667 filter_mask,
4668 NLM_F_MULTI);
David Ahernf6c57752017-05-15 23:19:17 -07004669 if (err < 0 && err != -EOPNOTSUPP) {
4670 if (likely(skb->len))
4671 break;
4672
4673 goto out_err;
4674 }
Roopa Prabhud64f69b2015-09-15 14:44:29 -07004675 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00004676 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00004677 }
4678 }
David Ahernf6c57752017-05-15 23:19:17 -07004679 err = skb->len;
4680out_err:
John Fastabende5a55a82012-10-24 08:12:57 +00004681 rcu_read_unlock();
4682 cb->args[0] = idx;
4683
David Ahernf6c57752017-05-15 23:19:17 -07004684 return err;
John Fastabende5a55a82012-10-24 08:12:57 +00004685}
4686
John Fastabend2469ffd2012-10-24 08:13:03 +00004687static inline size_t bridge_nlmsg_size(void)
4688{
4689 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4690 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4691 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4692 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
4693 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
4694 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
4695 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
4696 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
4697 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
4698 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
4699 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
4700}
4701
Roopa Prabhu02dba432015-01-14 20:02:25 -08004702static int rtnl_bridge_notify(struct net_device *dev)
John Fastabend2469ffd2012-10-24 08:13:03 +00004703{
4704 struct net *net = dev_net(dev);
John Fastabend2469ffd2012-10-24 08:13:03 +00004705 struct sk_buff *skb;
4706 int err = -EOPNOTSUPP;
4707
Roopa Prabhu02dba432015-01-14 20:02:25 -08004708 if (!dev->netdev_ops->ndo_bridge_getlink)
4709 return 0;
4710
John Fastabend2469ffd2012-10-24 08:13:03 +00004711 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
4712 if (!skb) {
4713 err = -ENOMEM;
4714 goto errout;
4715 }
4716
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02004717 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
Roopa Prabhu02dba432015-01-14 20:02:25 -08004718 if (err < 0)
4719 goto errout;
John Fastabend2469ffd2012-10-24 08:13:03 +00004720
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08004721 if (!skb->len)
4722 goto errout;
4723
John Fastabend2469ffd2012-10-24 08:13:03 +00004724 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
4725 return 0;
4726errout:
4727 WARN_ON(err == -EMSGSIZE);
4728 kfree_skb(skb);
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08004729 if (err)
4730 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
John Fastabend2469ffd2012-10-24 08:13:03 +00004731 return err;
4732}
4733
David Ahernc21ef3e2017-04-16 09:48:24 -07004734static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
4735 struct netlink_ext_ack *extack)
John Fastabende5a55a82012-10-24 08:12:57 +00004736{
4737 struct net *net = sock_net(skb->sk);
4738 struct ifinfomsg *ifm;
4739 struct net_device *dev;
John Fastabend2469ffd2012-10-24 08:13:03 +00004740 struct nlattr *br_spec, *attr = NULL;
4741 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02004742 u16 flags = 0;
John Fastabendc38e01b2012-11-02 16:32:36 +00004743 bool have_flags = false;
John Fastabende5a55a82012-10-24 08:12:57 +00004744
4745 if (nlmsg_len(nlh) < sizeof(*ifm))
4746 return -EINVAL;
4747
4748 ifm = nlmsg_data(nlh);
4749 if (ifm->ifi_family != AF_BRIDGE)
4750 return -EPFNOSUPPORT;
4751
4752 dev = __dev_get_by_index(net, ifm->ifi_index);
4753 if (!dev) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004754 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabende5a55a82012-10-24 08:12:57 +00004755 return -ENODEV;
4756 }
4757
John Fastabend2469ffd2012-10-24 08:13:03 +00004758 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4759 if (br_spec) {
4760 nla_for_each_nested(attr, br_spec, rem) {
4761 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01004762 if (nla_len(attr) < sizeof(flags))
4763 return -EINVAL;
4764
John Fastabendc38e01b2012-11-02 16:32:36 +00004765 have_flags = true;
John Fastabend2469ffd2012-10-24 08:13:03 +00004766 flags = nla_get_u16(attr);
4767 break;
4768 }
4769 }
4770 }
4771
4772 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00004773 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4774
4775 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
John Fastabend2469ffd2012-10-24 08:13:03 +00004776 err = -EOPNOTSUPP;
4777 goto out;
4778 }
4779
Petr Machata2fd527b2018-12-12 17:02:48 +00004780 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags,
4781 extack);
John Fastabende5a55a82012-10-24 08:12:57 +00004782 if (err)
4783 goto out;
John Fastabend2469ffd2012-10-24 08:13:03 +00004784
4785 flags &= ~BRIDGE_FLAGS_MASTER;
John Fastabende5a55a82012-10-24 08:12:57 +00004786 }
4787
John Fastabend2469ffd2012-10-24 08:13:03 +00004788 if ((flags & BRIDGE_FLAGS_SELF)) {
4789 if (!dev->netdev_ops->ndo_bridge_setlink)
4790 err = -EOPNOTSUPP;
4791 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08004792 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
Petr Machata2fd527b2018-12-12 17:02:48 +00004793 flags,
4794 extack);
Roopa Prabhu02dba432015-01-14 20:02:25 -08004795 if (!err) {
John Fastabend2469ffd2012-10-24 08:13:03 +00004796 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08004797
4798 /* Generate event to notify upper layer of bridge
4799 * change
4800 */
4801 err = rtnl_bridge_notify(dev);
4802 }
John Fastabend2469ffd2012-10-24 08:13:03 +00004803 }
4804
John Fastabendc38e01b2012-11-02 16:32:36 +00004805 if (have_flags)
John Fastabend2469ffd2012-10-24 08:13:03 +00004806 memcpy(nla_data(attr), &flags, sizeof(flags));
John Fastabende5a55a82012-10-24 08:12:57 +00004807out:
4808 return err;
4809}
4810
David Ahernc21ef3e2017-04-16 09:48:24 -07004811static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
4812 struct netlink_ext_ack *extack)
Vlad Yasevich407af322013-02-13 12:00:12 +00004813{
4814 struct net *net = sock_net(skb->sk);
4815 struct ifinfomsg *ifm;
4816 struct net_device *dev;
4817 struct nlattr *br_spec, *attr = NULL;
4818 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02004819 u16 flags = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +00004820 bool have_flags = false;
4821
4822 if (nlmsg_len(nlh) < sizeof(*ifm))
4823 return -EINVAL;
4824
4825 ifm = nlmsg_data(nlh);
4826 if (ifm->ifi_family != AF_BRIDGE)
4827 return -EPFNOSUPPORT;
4828
4829 dev = __dev_get_by_index(net, ifm->ifi_index);
4830 if (!dev) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004831 NL_SET_ERR_MSG(extack, "unknown ifindex");
Vlad Yasevich407af322013-02-13 12:00:12 +00004832 return -ENODEV;
4833 }
4834
4835 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4836 if (br_spec) {
4837 nla_for_each_nested(attr, br_spec, rem) {
4838 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01004839 if (nla_len(attr) < sizeof(flags))
4840 return -EINVAL;
4841
Vlad Yasevich407af322013-02-13 12:00:12 +00004842 have_flags = true;
4843 flags = nla_get_u16(attr);
4844 break;
4845 }
4846 }
4847 }
4848
Vlad Yasevich407af322013-02-13 12:00:12 +00004849 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
4850 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4851
4852 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
4853 err = -EOPNOTSUPP;
4854 goto out;
4855 }
4856
Roopa Prabhuadd511b2015-01-29 22:40:12 -08004857 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00004858 if (err)
4859 goto out;
4860
4861 flags &= ~BRIDGE_FLAGS_MASTER;
4862 }
4863
4864 if ((flags & BRIDGE_FLAGS_SELF)) {
4865 if (!dev->netdev_ops->ndo_bridge_dellink)
4866 err = -EOPNOTSUPP;
4867 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08004868 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
4869 flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00004870
Roopa Prabhu02dba432015-01-14 20:02:25 -08004871 if (!err) {
Vlad Yasevich407af322013-02-13 12:00:12 +00004872 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08004873
4874 /* Generate event to notify upper layer of bridge
4875 * change
4876 */
4877 err = rtnl_bridge_notify(dev);
4878 }
Vlad Yasevich407af322013-02-13 12:00:12 +00004879 }
4880
4881 if (have_flags)
4882 memcpy(nla_data(attr), &flags, sizeof(flags));
Vlad Yasevich407af322013-02-13 12:00:12 +00004883out:
4884 return err;
4885}
4886
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004887static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
4888{
4889 return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
4890 (!idxattr || idxattr == attrid);
4891}
4892
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004893#define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
4894static int rtnl_get_offload_stats_attr_size(int attr_id)
4895{
4896 switch (attr_id) {
4897 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
4898 return sizeof(struct rtnl_link_stats64);
4899 }
4900
4901 return 0;
4902}
4903
4904static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
4905 int *prividx)
4906{
4907 struct nlattr *attr = NULL;
4908 int attr_id, size;
4909 void *attr_data;
4910 int err;
4911
4912 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
4913 dev->netdev_ops->ndo_get_offload_stats))
4914 return -ENODATA;
4915
4916 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
4917 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
4918 if (attr_id < *prividx)
4919 continue;
4920
4921 size = rtnl_get_offload_stats_attr_size(attr_id);
4922 if (!size)
4923 continue;
4924
Or Gerlitz3df5b3c2016-11-22 23:09:54 +02004925 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004926 continue;
4927
4928 attr = nla_reserve_64bit(skb, attr_id, size,
4929 IFLA_OFFLOAD_XSTATS_UNSPEC);
4930 if (!attr)
4931 goto nla_put_failure;
4932
4933 attr_data = nla_data(attr);
4934 memset(attr_data, 0, size);
4935 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
4936 attr_data);
4937 if (err)
4938 goto get_offload_stats_failure;
4939 }
4940
4941 if (!attr)
4942 return -ENODATA;
4943
4944 *prividx = 0;
4945 return 0;
4946
4947nla_put_failure:
4948 err = -EMSGSIZE;
4949get_offload_stats_failure:
4950 *prividx = attr_id;
4951 return err;
4952}
4953
4954static int rtnl_get_offload_stats_size(const struct net_device *dev)
4955{
4956 int nla_size = 0;
4957 int attr_id;
4958 int size;
4959
4960 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
4961 dev->netdev_ops->ndo_get_offload_stats))
4962 return 0;
4963
4964 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
4965 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
Or Gerlitz3df5b3c2016-11-22 23:09:54 +02004966 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004967 continue;
4968 size = rtnl_get_offload_stats_attr_size(attr_id);
4969 nla_size += nla_total_size_64bit(size);
4970 }
4971
4972 if (nla_size != 0)
4973 nla_size += nla_total_size(0);
4974
4975 return nla_size;
4976}
4977
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004978static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
4979 int type, u32 pid, u32 seq, u32 change,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004980 unsigned int flags, unsigned int filter_mask,
4981 int *idxattr, int *prividx)
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004982{
4983 struct if_stats_msg *ifsm;
4984 struct nlmsghdr *nlh;
4985 struct nlattr *attr;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004986 int s_prividx = *prividx;
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004987 int err;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004988
4989 ASSERT_RTNL();
4990
4991 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
4992 if (!nlh)
4993 return -EMSGSIZE;
4994
4995 ifsm = nlmsg_data(nlh);
Nikolay Aleksandrovce024f42017-10-03 13:20:48 +03004996 ifsm->family = PF_UNSPEC;
4997 ifsm->pad1 = 0;
4998 ifsm->pad2 = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004999 ifsm->ifindex = dev->ifindex;
5000 ifsm->filter_mask = filter_mask;
5001
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005002 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005003 struct rtnl_link_stats64 *sp;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005004
Nicolas Dichtel58414d32016-04-21 18:58:25 +02005005 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
5006 sizeof(struct rtnl_link_stats64),
5007 IFLA_STATS_UNSPEC);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005008 if (!attr)
5009 goto nla_put_failure;
5010
5011 sp = nla_data(attr);
5012 dev_get_stats(dev, sp);
5013 }
5014
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02005015 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
5016 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
5017
5018 if (ops && ops->fill_linkxstats) {
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02005019 *idxattr = IFLA_STATS_LINK_XSTATS;
Michal Kubecekae0be8d2019-04-26 11:13:06 +02005020 attr = nla_nest_start_noflag(skb,
5021 IFLA_STATS_LINK_XSTATS);
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02005022 if (!attr)
5023 goto nla_put_failure;
5024
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02005025 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
5026 nla_nest_end(skb, attr);
5027 if (err)
5028 goto nla_put_failure;
5029 *idxattr = 0;
5030 }
5031 }
5032
5033 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
5034 *idxattr)) {
5035 const struct rtnl_link_ops *ops = NULL;
5036 const struct net_device *master;
5037
5038 master = netdev_master_upper_dev_get(dev);
5039 if (master)
5040 ops = master->rtnl_link_ops;
5041 if (ops && ops->fill_linkxstats) {
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02005042 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
Michal Kubecekae0be8d2019-04-26 11:13:06 +02005043 attr = nla_nest_start_noflag(skb,
5044 IFLA_STATS_LINK_XSTATS_SLAVE);
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02005045 if (!attr)
5046 goto nla_put_failure;
5047
5048 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02005049 nla_nest_end(skb, attr);
5050 if (err)
5051 goto nla_put_failure;
5052 *idxattr = 0;
5053 }
5054 }
5055
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02005056 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
5057 *idxattr)) {
5058 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
Michal Kubecekae0be8d2019-04-26 11:13:06 +02005059 attr = nla_nest_start_noflag(skb,
5060 IFLA_STATS_LINK_OFFLOAD_XSTATS);
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02005061 if (!attr)
5062 goto nla_put_failure;
5063
5064 err = rtnl_get_offload_stats(skb, dev, prividx);
5065 if (err == -ENODATA)
5066 nla_nest_cancel(skb, attr);
5067 else
5068 nla_nest_end(skb, attr);
5069
5070 if (err && err != -ENODATA)
5071 goto nla_put_failure;
5072 *idxattr = 0;
5073 }
5074
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005075 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
5076 struct rtnl_af_ops *af_ops;
5077
5078 *idxattr = IFLA_STATS_AF_SPEC;
Michal Kubecekae0be8d2019-04-26 11:13:06 +02005079 attr = nla_nest_start_noflag(skb, IFLA_STATS_AF_SPEC);
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005080 if (!attr)
5081 goto nla_put_failure;
5082
Florian Westphal5fa85a02017-10-16 15:44:36 +02005083 rcu_read_lock();
5084 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005085 if (af_ops->fill_stats_af) {
5086 struct nlattr *af;
5087 int err;
5088
Michal Kubecekae0be8d2019-04-26 11:13:06 +02005089 af = nla_nest_start_noflag(skb,
5090 af_ops->family);
Florian Westphal5fa85a02017-10-16 15:44:36 +02005091 if (!af) {
5092 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005093 goto nla_put_failure;
Florian Westphal5fa85a02017-10-16 15:44:36 +02005094 }
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005095 err = af_ops->fill_stats_af(skb, dev);
5096
Florian Westphal5fa85a02017-10-16 15:44:36 +02005097 if (err == -ENODATA) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005098 nla_nest_cancel(skb, af);
Florian Westphal5fa85a02017-10-16 15:44:36 +02005099 } else if (err < 0) {
5100 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005101 goto nla_put_failure;
Florian Westphal5fa85a02017-10-16 15:44:36 +02005102 }
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005103
5104 nla_nest_end(skb, af);
5105 }
5106 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02005107 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005108
5109 nla_nest_end(skb, attr);
5110
5111 *idxattr = 0;
5112 }
5113
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005114 nlmsg_end(skb, nlh);
5115
5116 return 0;
5117
5118nla_put_failure:
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005119 /* not a multi message or no progress mean a real error */
5120 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
5121 nlmsg_cancel(skb, nlh);
5122 else
5123 nlmsg_end(skb, nlh);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005124
5125 return -EMSGSIZE;
5126}
5127
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005128static size_t if_nlmsg_stats_size(const struct net_device *dev,
5129 u32 filter_mask)
5130{
5131 size_t size = 0;
5132
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005133 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005134 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
5135
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02005136 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
5137 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02005138 int attr = IFLA_STATS_LINK_XSTATS;
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02005139
5140 if (ops && ops->get_linkxstats_size) {
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02005141 size += nla_total_size(ops->get_linkxstats_size(dev,
5142 attr));
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02005143 /* for IFLA_STATS_LINK_XSTATS */
5144 size += nla_total_size(0);
5145 }
5146 }
5147
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02005148 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
5149 struct net_device *_dev = (struct net_device *)dev;
5150 const struct rtnl_link_ops *ops = NULL;
5151 const struct net_device *master;
5152
5153 /* netdev_master_upper_dev_get can't take const */
5154 master = netdev_master_upper_dev_get(_dev);
5155 if (master)
5156 ops = master->rtnl_link_ops;
5157 if (ops && ops->get_linkxstats_size) {
5158 int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
5159
5160 size += nla_total_size(ops->get_linkxstats_size(dev,
5161 attr));
5162 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
5163 size += nla_total_size(0);
5164 }
5165 }
5166
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02005167 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
5168 size += rtnl_get_offload_stats_size(dev);
5169
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005170 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
5171 struct rtnl_af_ops *af_ops;
5172
5173 /* for IFLA_STATS_AF_SPEC */
5174 size += nla_total_size(0);
5175
Florian Westphal5fa85a02017-10-16 15:44:36 +02005176 rcu_read_lock();
5177 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005178 if (af_ops->get_stats_af_size) {
5179 size += nla_total_size(
5180 af_ops->get_stats_af_size(dev));
5181
5182 /* for AF_* */
5183 size += nla_total_size(0);
5184 }
5185 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02005186 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005187 }
5188
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005189 return size;
5190}
5191
Jakub Kicinski51bc8602019-01-18 10:46:14 -08005192static int rtnl_valid_stats_req(const struct nlmsghdr *nlh, bool strict_check,
5193 bool is_dump, struct netlink_ext_ack *extack)
5194{
5195 struct if_stats_msg *ifsm;
5196
Eric Dumazet69f23a02019-04-14 11:02:05 -07005197 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifsm))) {
Jakub Kicinski51bc8602019-01-18 10:46:14 -08005198 NL_SET_ERR_MSG(extack, "Invalid header for stats dump");
5199 return -EINVAL;
5200 }
5201
5202 if (!strict_check)
5203 return 0;
5204
5205 ifsm = nlmsg_data(nlh);
5206
5207 /* only requests using strict checks can pass data to influence
5208 * the dump. The legacy exception is filter_mask.
5209 */
5210 if (ifsm->pad1 || ifsm->pad2 || (is_dump && ifsm->ifindex)) {
5211 NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request");
5212 return -EINVAL;
5213 }
5214 if (nlmsg_attrlen(nlh, sizeof(*ifsm))) {
5215 NL_SET_ERR_MSG(extack, "Invalid attributes after stats header");
5216 return -EINVAL;
5217 }
Jakub Kicinski6300acb2019-01-18 10:46:15 -08005218 if (ifsm->filter_mask >= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX + 1)) {
5219 NL_SET_ERR_MSG(extack, "Invalid stats requested through filter mask");
5220 return -EINVAL;
5221 }
Jakub Kicinski51bc8602019-01-18 10:46:14 -08005222
5223 return 0;
5224}
5225
David Ahernc21ef3e2017-04-16 09:48:24 -07005226static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
5227 struct netlink_ext_ack *extack)
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005228{
5229 struct net *net = sock_net(skb->sk);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005230 struct net_device *dev = NULL;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005231 int idxattr = 0, prividx = 0;
5232 struct if_stats_msg *ifsm;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005233 struct sk_buff *nskb;
5234 u32 filter_mask;
5235 int err;
5236
Jakub Kicinski51bc8602019-01-18 10:46:14 -08005237 err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb),
5238 false, extack);
5239 if (err)
5240 return err;
Mathias Krause4775cc12016-12-28 17:52:15 +01005241
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005242 ifsm = nlmsg_data(nlh);
5243 if (ifsm->ifindex > 0)
5244 dev = __dev_get_by_index(net, ifsm->ifindex);
5245 else
5246 return -EINVAL;
5247
5248 if (!dev)
5249 return -ENODEV;
5250
5251 filter_mask = ifsm->filter_mask;
5252 if (!filter_mask)
5253 return -EINVAL;
5254
5255 nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
5256 if (!nskb)
5257 return -ENOBUFS;
5258
5259 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
5260 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005261 0, filter_mask, &idxattr, &prividx);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005262 if (err < 0) {
5263 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
5264 WARN_ON(err == -EMSGSIZE);
5265 kfree_skb(nskb);
5266 } else {
5267 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
5268 }
5269
5270 return err;
5271}
5272
5273static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
5274{
David Ahern841891e2018-10-07 20:16:32 -07005275 struct netlink_ext_ack *extack = cb->extack;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005276 int h, s_h, err, s_idx, s_idxattr, s_prividx;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005277 struct net *net = sock_net(skb->sk);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005278 unsigned int flags = NLM_F_MULTI;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005279 struct if_stats_msg *ifsm;
5280 struct hlist_head *head;
5281 struct net_device *dev;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005282 u32 filter_mask = 0;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005283 int idx = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005284
5285 s_h = cb->args[0];
5286 s_idx = cb->args[1];
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005287 s_idxattr = cb->args[2];
5288 s_prividx = cb->args[3];
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005289
5290 cb->seq = net->dev_base_seq;
5291
Jakub Kicinski51bc8602019-01-18 10:46:14 -08005292 err = rtnl_valid_stats_req(cb->nlh, cb->strict_check, true, extack);
5293 if (err)
5294 return err;
Mathias Krause4775cc12016-12-28 17:52:15 +01005295
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005296 ifsm = nlmsg_data(cb->nlh);
5297 filter_mask = ifsm->filter_mask;
David Ahern841891e2018-10-07 20:16:32 -07005298 if (!filter_mask) {
5299 NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump");
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005300 return -EINVAL;
David Ahern841891e2018-10-07 20:16:32 -07005301 }
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005302
5303 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5304 idx = 0;
5305 head = &net->dev_index_head[h];
5306 hlist_for_each_entry(dev, head, index_hlist) {
5307 if (idx < s_idx)
5308 goto cont;
5309 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
5310 NETLINK_CB(cb->skb).portid,
5311 cb->nlh->nlmsg_seq, 0,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005312 flags, filter_mask,
5313 &s_idxattr, &s_prividx);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005314 /* If we ran out of room on the first message,
5315 * we're in trouble
5316 */
5317 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
5318
5319 if (err < 0)
5320 goto out;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005321 s_prividx = 0;
5322 s_idxattr = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005323 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
5324cont:
5325 idx++;
5326 }
5327 }
5328out:
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005329 cb->args[3] = s_prividx;
5330 cb->args[2] = s_idxattr;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005331 cb->args[1] = idx;
5332 cb->args[0] = h;
5333
5334 return skb->len;
5335}
5336
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337/* Process one rtnetlink message. */
5338
Johannes Berg2d4bc932017-04-12 14:34:04 +02005339static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
5340 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09005342 struct net *net = sock_net(skb->sk);
Florian Westphaladdf9b92017-12-02 21:44:05 +01005343 struct rtnl_link *link;
Florian Westphale4202512017-12-02 21:44:06 +01005344 struct module *owner;
Florian Westphal6853dd42017-08-09 20:41:51 +02005345 int err = -EOPNOTSUPP;
Thomas Grafe2849862007-03-22 11:48:11 -07005346 rtnl_doit_func doit;
Florian Westphal62256f92017-08-09 20:41:52 +02005347 unsigned int flags;
Alexander Kuleshov617cfc72016-01-10 21:26:57 +06005348 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 int family;
5350 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353 if (type > RTM_MAX)
Thomas Graf038890f2007-04-05 14:35:52 -07005354 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355
5356 type -= RTM_BASE;
5357
5358 /* All the messages must have at least 1 byte length */
Hong zhi guo573ce262013-03-27 06:47:04 +00005359 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 return 0;
5361
Hong zhi guo573ce262013-03-27 06:47:04 +00005362 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 kind = type&3;
5364
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07005365 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07005366 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367
Florian Westphal6853dd42017-08-09 20:41:51 +02005368 rcu_read_lock();
David S. Millerb8f3ab42011-01-18 12:40:38 -08005369 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005370 struct sock *rtnl;
Thomas Grafe2849862007-03-22 11:48:11 -07005371 rtnl_dumpit_func dumpit;
Greg Rosec7ac8672011-06-10 01:27:09 +00005372 u16 min_dump_alloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373
Florian Westphaladdf9b92017-12-02 21:44:05 +01005374 link = rtnl_get_link(family, type);
5375 if (!link || !link->dumpit) {
Florian Westphal6853dd42017-08-09 20:41:51 +02005376 family = PF_UNSPEC;
Florian Westphaladdf9b92017-12-02 21:44:05 +01005377 link = rtnl_get_link(family, type);
5378 if (!link || !link->dumpit)
Florian Westphal6853dd42017-08-09 20:41:51 +02005379 goto err_unlock;
5380 }
Florian Westphale4202512017-12-02 21:44:06 +01005381 owner = link->owner;
Florian Westphaladdf9b92017-12-02 21:44:05 +01005382 dumpit = link->dumpit;
Florian Westphale1fa6d22017-08-09 20:41:47 +02005383
Florian Westphal5c2bb9b2017-08-10 16:52:58 +02005384 if (type == RTM_GETLINK - RTM_BASE)
Florian Westphale1fa6d22017-08-09 20:41:47 +02005385 min_dump_alloc = rtnl_calcit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386
Florian Westphale4202512017-12-02 21:44:06 +01005387 err = 0;
5388 /* need to do this before rcu_read_unlock() */
5389 if (!try_module_get(owner))
5390 err = -EPROTONOSUPPORT;
5391
Florian Westphal6853dd42017-08-09 20:41:51 +02005392 rcu_read_unlock();
5393
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005394 rtnl = net->rtnl;
Florian Westphale4202512017-12-02 21:44:06 +01005395 if (err == 0) {
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00005396 struct netlink_dump_control c = {
5397 .dump = dumpit,
5398 .min_dump_alloc = min_dump_alloc,
Florian Westphale4202512017-12-02 21:44:06 +01005399 .module = owner,
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00005400 };
5401 err = netlink_dump_start(rtnl, skb, nlh, &c);
Florian Westphale4202512017-12-02 21:44:06 +01005402 /* netlink_dump_start() will keep a reference on
5403 * module if dump is still in progress.
5404 */
5405 module_put(owner);
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00005406 }
Eric Dumazet2907c352011-05-25 07:34:04 +00005407 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 }
5409
Florian Westphaladdf9b92017-12-02 21:44:05 +01005410 link = rtnl_get_link(family, type);
5411 if (!link || !link->doit) {
Florian Westphal8caa38b2017-08-10 16:53:01 +02005412 family = PF_UNSPEC;
Florian Westphaladdf9b92017-12-02 21:44:05 +01005413 link = rtnl_get_link(PF_UNSPEC, type);
5414 if (!link || !link->doit)
5415 goto out_unlock;
Florian Westphal8caa38b2017-08-10 16:53:01 +02005416 }
5417
Florian Westphale4202512017-12-02 21:44:06 +01005418 owner = link->owner;
5419 if (!try_module_get(owner)) {
5420 err = -EPROTONOSUPPORT;
5421 goto out_unlock;
5422 }
5423
Florian Westphaladdf9b92017-12-02 21:44:05 +01005424 flags = link->flags;
Florian Westphal62256f92017-08-09 20:41:52 +02005425 if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
Florian Westphaladdf9b92017-12-02 21:44:05 +01005426 doit = link->doit;
Florian Westphal62256f92017-08-09 20:41:52 +02005427 rcu_read_unlock();
5428 if (doit)
5429 err = doit(skb, nlh, extack);
Florian Westphale4202512017-12-02 21:44:06 +01005430 module_put(owner);
Florian Westphal62256f92017-08-09 20:41:52 +02005431 return err;
5432 }
Florian Westphal6853dd42017-08-09 20:41:51 +02005433 rcu_read_unlock();
5434
Florian Westphal0cc09022017-08-09 20:41:50 +02005435 rtnl_lock();
Florian Westphaladdf9b92017-12-02 21:44:05 +01005436 link = rtnl_get_link(family, type);
5437 if (link && link->doit)
5438 err = link->doit(skb, nlh, extack);
Florian Westphal0cc09022017-08-09 20:41:50 +02005439 rtnl_unlock();
Florian Westphaladdf9b92017-12-02 21:44:05 +01005440
Florian Westphale4202512017-12-02 21:44:06 +01005441 module_put(owner);
5442
Florian Westphaladdf9b92017-12-02 21:44:05 +01005443 return err;
5444
5445out_unlock:
5446 rcu_read_unlock();
Florian Westphal0cc09022017-08-09 20:41:50 +02005447 return err;
5448
5449err_unlock:
Florian Westphal6853dd42017-08-09 20:41:51 +02005450 rcu_read_unlock();
Florian Westphal0cc09022017-08-09 20:41:50 +02005451 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452}
5453
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07005454static void rtnetlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07005456 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457}
5458
Julien Gomes5f729ea2017-06-20 13:54:16 -07005459static int rtnetlink_bind(struct net *net, int group)
5460{
5461 switch (group) {
5462 case RTNLGRP_IPV4_MROUTE_R:
5463 case RTNLGRP_IPV6_MROUTE_R:
5464 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
5465 return -EPERM;
5466 break;
5467 }
5468 return 0;
5469}
5470
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
5472{
Jiri Pirko351638e2013-05-28 01:30:21 +00005473 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02005474
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 switch (event) {
Vlad Yasevich5138e862017-04-04 09:23:41 -04005476 case NETDEV_REBOOT:
Xin Long8a212582017-10-15 18:13:41 +08005477 case NETDEV_CHANGEMTU:
David Ahern37536542017-07-19 10:22:40 -07005478 case NETDEV_CHANGEADDR:
Vlad Yasevich5138e862017-04-04 09:23:41 -04005479 case NETDEV_CHANGENAME:
5480 case NETDEV_FEAT_CHANGE:
5481 case NETDEV_BONDING_FAILOVER:
Xin Longe6e66592017-10-15 18:13:43 +08005482 case NETDEV_POST_TYPE_CHANGE:
Vlad Yasevich5138e862017-04-04 09:23:41 -04005483 case NETDEV_NOTIFY_PEERS:
Xin Longdc709f32017-10-15 18:13:44 +08005484 case NETDEV_CHANGEUPPER:
Vlad Yasevich5138e862017-04-04 09:23:41 -04005485 case NETDEV_RESEND_IGMP:
Vlad Yasevich5138e862017-04-04 09:23:41 -04005486 case NETDEV_CHANGEINFODATA:
Xin Longeeda3fb2017-10-24 13:54:19 +08005487 case NETDEV_CHANGELOWERSTATE:
Xin Longebdcf042017-10-15 18:13:42 +08005488 case NETDEV_CHANGE_TX_QUEUE_LEN:
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04005489 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01005490 GFP_KERNEL, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491 break;
5492 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 break;
5494 }
5495 return NOTIFY_DONE;
5496}
5497
5498static struct notifier_block rtnetlink_dev_notifier = {
5499 .notifier_call = rtnetlink_event,
5500};
5501
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005502
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005503static int __net_init rtnetlink_net_init(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005504{
5505 struct sock *sk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00005506 struct netlink_kernel_cfg cfg = {
5507 .groups = RTNLGRP_MAX,
5508 .input = rtnetlink_rcv,
5509 .cb_mutex = &rtnl_mutex,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00005510 .flags = NL_CFG_F_NONROOT_RECV,
Julien Gomes5f729ea2017-06-20 13:54:16 -07005511 .bind = rtnetlink_bind,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00005512 };
5513
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00005514 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005515 if (!sk)
5516 return -ENOMEM;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005517 net->rtnl = sk;
5518 return 0;
5519}
5520
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005521static void __net_exit rtnetlink_net_exit(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005522{
Denis V. Lunev775516b2008-01-18 23:55:19 -08005523 netlink_kernel_release(net->rtnl);
5524 net->rtnl = NULL;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005525}
5526
5527static struct pernet_operations rtnetlink_net_ops = {
5528 .init = rtnetlink_net_init,
5529 .exit = rtnetlink_net_exit,
5530};
5531
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532void __init rtnetlink_init(void)
5533{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005534 if (register_pernet_subsys(&rtnetlink_net_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535 panic("rtnetlink_init: cannot initialize rtnetlink\n");
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005536
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 register_netdevice_notifier(&rtnetlink_dev_notifier);
Thomas Graf340d17f2007-03-22 11:49:22 -07005538
Greg Rosec7ac8672011-06-10 01:27:09 +00005539 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
Florian Westphalb97bac62017-08-09 20:41:48 +02005540 rtnl_dump_ifinfo, 0);
5541 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
5542 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
5543 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
Thomas Graf687ad8c2007-03-22 11:59:42 -07005544
Florian Westphalb97bac62017-08-09 20:41:48 +02005545 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
5546 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
5547 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
John Fastabend77162022012-04-15 06:43:56 +00005548
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02005549 rtnl_register(PF_UNSPEC, RTM_NEWLINKPROP, rtnl_newlinkprop, NULL, 0);
5550 rtnl_register(PF_UNSPEC, RTM_DELLINKPROP, rtnl_dellinkprop, NULL, 0);
5551
Florian Westphalb97bac62017-08-09 20:41:48 +02005552 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
5553 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
Roopa Prabhu5b2f94b2018-12-15 22:35:08 -08005554 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0);
John Fastabende5a55a82012-10-24 08:12:57 +00005555
Florian Westphalb97bac62017-08-09 20:41:48 +02005556 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
5557 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
5558 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005559
5560 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
Florian Westphalb97bac62017-08-09 20:41:48 +02005561 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562}