blob: 710da8a36729d6a9293b610b029bd33aa38d6514 [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:
Zheng Yongjund467d0b2021-06-02 14:56:23 +080012 * Vitaly E. Lavrov RTA_OK arithmetic was wrong.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 */
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
Nikolay Aleksandrov2dba4072021-01-26 11:35:33 +020058#define RTNL_SLAVE_MAX_TYPE 40
Kees Cookccf8dbc2018-05-30 15:20:52 -070059
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
Jakub Kicinski51e13682020-12-09 18:16:08 -0800142static struct rtnl_link __rcu *__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{
Jakub Kicinski51e13682020-12-09 18:16:08 -0800160 struct rtnl_link __rcu **tab;
Florian Westphaladdf9b92017-12-02 21:44:05 +0100161
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
Jakub Kicinski51e13682020-12-09 18:16:08 -0800169 return rcu_dereference_rtnl(tab[msgtype]);
Florian Westphaladdf9b92017-12-02 21:44:05 +0100170}
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();
Jakub Kicinski51e13682020-12-09 18:16:08 -0800186 tab = rtnl_dereference(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
Zheng Yongjund467d0b2021-06-02 14:56:23 +0800237 * @flags: rtnl_link_flags to modify behaviour of doit/dumpit functions
Florian Westphale4202512017-12-02 21:44:06 +0100238 *
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
Zheng Yongjund467d0b2021-06-02 14:56:23 +0800257 * @flags: rtnl_link_flags to modify behaviour of doit/dumpit functions
Florian Westphale4202512017-12-02 21:44:06 +0100258 *
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{
Jakub Kicinski51e13682020-12-09 18:16:08 -0800289 struct rtnl_link __rcu **tab;
290 struct rtnl_link *link;
Thomas Grafe2849862007-03-22 11:48:11 -0700291 int msgindex;
292
Patrick McHardy25239ce2010-04-26 16:02:05 +0200293 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700294 msgindex = rtm_msgindex(msgtype);
295
Florian Westphal6853dd42017-08-09 20:41:51 +0200296 rtnl_lock();
Florian Westphaladdf9b92017-12-02 21:44:05 +0100297 tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
298 if (!tab) {
Florian Westphal6853dd42017-08-09 20:41:51 +0200299 rtnl_unlock();
Thomas Grafe2849862007-03-22 11:48:11 -0700300 return -ENOENT;
Florian Westphal6853dd42017-08-09 20:41:51 +0200301 }
Thomas Grafe2849862007-03-22 11:48:11 -0700302
Jakub Kicinski51e13682020-12-09 18:16:08 -0800303 link = rtnl_dereference(tab[msgindex]);
Yajun Deng4fc29982021-09-18 14:36:07 +0800304 RCU_INIT_POINTER(tab[msgindex], NULL);
Florian Westphal6853dd42017-08-09 20:41:51 +0200305 rtnl_unlock();
Thomas Grafe2849862007-03-22 11:48:11 -0700306
Florian Westphaladdf9b92017-12-02 21:44:05 +0100307 kfree_rcu(link, rcu);
308
Thomas Grafe2849862007-03-22 11:48:11 -0700309 return 0;
310}
Thomas Grafe2849862007-03-22 11:48:11 -0700311EXPORT_SYMBOL_GPL(rtnl_unregister);
312
313/**
314 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
315 * @protocol : Protocol family or PF_UNSPEC
316 *
317 * Identical to calling rtnl_unregster() for all registered message types
318 * of a certain protocol family.
319 */
320void rtnl_unregister_all(int protocol)
321{
Jakub Kicinski51e13682020-12-09 18:16:08 -0800322 struct rtnl_link __rcu **tab;
323 struct rtnl_link *link;
Florian Westphaladdf9b92017-12-02 21:44:05 +0100324 int msgindex;
Florian Westphal019a3162017-08-09 20:41:49 +0200325
Patrick McHardy25239ce2010-04-26 16:02:05 +0200326 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700327
Florian Westphal019a3162017-08-09 20:41:49 +0200328 rtnl_lock();
Jakub Kicinski51e13682020-12-09 18:16:08 -0800329 tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
Sabrina Dubrocaf707ef62018-08-28 13:40:53 +0200330 if (!tab) {
331 rtnl_unlock();
332 return;
333 }
Florian Westphal6853dd42017-08-09 20:41:51 +0200334 RCU_INIT_POINTER(rtnl_msg_handlers[protocol], NULL);
Florian Westphaladdf9b92017-12-02 21:44:05 +0100335 for (msgindex = 0; msgindex < RTM_NR_MSGTYPES; msgindex++) {
Jakub Kicinski51e13682020-12-09 18:16:08 -0800336 link = rtnl_dereference(tab[msgindex]);
Florian Westphaladdf9b92017-12-02 21:44:05 +0100337 if (!link)
338 continue;
339
Yajun Deng4fc29982021-09-18 14:36:07 +0800340 RCU_INIT_POINTER(tab[msgindex], NULL);
Florian Westphaladdf9b92017-12-02 21:44:05 +0100341 kfree_rcu(link, rcu);
342 }
Florian Westphal019a3162017-08-09 20:41:49 +0200343 rtnl_unlock();
344
Florian Westphal6853dd42017-08-09 20:41:51 +0200345 synchronize_net();
346
Florian Westphaladdf9b92017-12-02 21:44:05 +0100347 kfree(tab);
Thomas Grafe2849862007-03-22 11:48:11 -0700348}
Thomas Grafe2849862007-03-22 11:48:11 -0700349EXPORT_SYMBOL_GPL(rtnl_unregister_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Patrick McHardy38f7b872007-06-13 12:03:51 -0700351static LIST_HEAD(link_ops);
352
Eric Dumazetc63044f2011-12-13 11:38:00 +0000353static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
354{
355 const struct rtnl_link_ops *ops;
356
357 list_for_each_entry(ops, &link_ops, list) {
358 if (!strcmp(ops->kind, kind))
359 return ops;
360 }
361 return NULL;
362}
363
Patrick McHardy38f7b872007-06-13 12:03:51 -0700364/**
365 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
366 * @ops: struct rtnl_link_ops * to register
367 *
368 * The caller must hold the rtnl_mutex. This function should be used
369 * by drivers that create devices during module initialization. It
370 * must be called before registering the devices.
371 *
372 * Returns 0 on success or a negative error code.
373 */
374int __rtnl_link_register(struct rtnl_link_ops *ops)
375{
Eric Dumazetc63044f2011-12-13 11:38:00 +0000376 if (rtnl_link_ops_get(ops->kind))
377 return -EEXIST;
378
Johannes Berg8c713dc2021-06-12 10:20:54 +0200379 /* The check for alloc/setup is here because if ops
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +0200380 * does not have that filled up, it is not possible
381 * to use the ops for creating device. So do not
382 * fill up dellink as well. That disables rtnl_dellink.
383 */
Johannes Berg8c713dc2021-06-12 10:20:54 +0200384 if ((ops->alloc || ops->setup) && !ops->dellink)
Eric Dumazet23289a32009-10-27 07:06:36 +0000385 ops->dellink = unregister_netdevice_queue;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700386
Patrick McHardy38f7b872007-06-13 12:03:51 -0700387 list_add_tail(&ops->list, &link_ops);
388 return 0;
389}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700390EXPORT_SYMBOL_GPL(__rtnl_link_register);
391
392/**
393 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
394 * @ops: struct rtnl_link_ops * to register
395 *
396 * Returns 0 on success or a negative error code.
397 */
398int rtnl_link_register(struct rtnl_link_ops *ops)
399{
400 int err;
401
Kees Cookccf8dbc2018-05-30 15:20:52 -0700402 /* Sanity-check max sizes to avoid stack buffer overflow. */
403 if (WARN_ON(ops->maxtype > RTNL_MAX_TYPE ||
404 ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE))
405 return -EINVAL;
406
Patrick McHardy38f7b872007-06-13 12:03:51 -0700407 rtnl_lock();
408 err = __rtnl_link_register(ops);
409 rtnl_unlock();
410 return err;
411}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700412EXPORT_SYMBOL_GPL(rtnl_link_register);
413
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700414static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
415{
416 struct net_device *dev;
Eric Dumazet23289a32009-10-27 07:06:36 +0000417 LIST_HEAD(list_kill);
418
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700419 for_each_netdev(net, dev) {
Eric Dumazet23289a32009-10-27 07:06:36 +0000420 if (dev->rtnl_link_ops == ops)
421 ops->dellink(dev, &list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700422 }
Eric Dumazet23289a32009-10-27 07:06:36 +0000423 unregister_netdevice_many(&list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700424}
425
Patrick McHardy38f7b872007-06-13 12:03:51 -0700426/**
427 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
428 * @ops: struct rtnl_link_ops * to unregister
429 *
Kirill Tkhai554873e2018-03-30 19:38:37 +0300430 * The caller must hold the rtnl_mutex and guarantee net_namespace_list
431 * integrity (hold pernet_ops_rwsem for writing to close the race
432 * with setup_net() and cleanup_net()).
Patrick McHardy38f7b872007-06-13 12:03:51 -0700433 */
434void __rtnl_link_unregister(struct rtnl_link_ops *ops)
435{
Eric W. Biederman881d9662007-09-17 11:56:21 -0700436 struct net *net;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700437
Eric W. Biederman881d9662007-09-17 11:56:21 -0700438 for_each_net(net) {
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700439 __rtnl_kill_links(net, ops);
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700440 }
Patrick McHardy38f7b872007-06-13 12:03:51 -0700441 list_del(&ops->list);
442}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700443EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
444
Cong Wang200b9162014-05-12 15:11:20 -0700445/* Return with the rtnl_lock held when there are no network
446 * devices unregistering in any network namespace.
447 */
448static void rtnl_lock_unregistering_all(void)
449{
450 struct net *net;
451 bool unregistering;
Peter Zijlstraff960a72014-10-29 17:04:56 +0100452 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Cong Wang200b9162014-05-12 15:11:20 -0700453
Peter Zijlstraff960a72014-10-29 17:04:56 +0100454 add_wait_queue(&netdev_unregistering_wq, &wait);
Cong Wang200b9162014-05-12 15:11:20 -0700455 for (;;) {
Cong Wang200b9162014-05-12 15:11:20 -0700456 unregistering = false;
457 rtnl_lock();
Kirill Tkhaif0b07bb12018-03-29 19:20:32 +0300458 /* We held write locked pernet_ops_rwsem, and parallel
459 * setup_net() and cleanup_net() are not possible.
460 */
Cong Wang200b9162014-05-12 15:11:20 -0700461 for_each_net(net) {
462 if (net->dev_unreg_count > 0) {
463 unregistering = true;
464 break;
465 }
466 }
467 if (!unregistering)
468 break;
469 __rtnl_unlock();
Peter Zijlstraff960a72014-10-29 17:04:56 +0100470
471 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Cong Wang200b9162014-05-12 15:11:20 -0700472 }
Peter Zijlstraff960a72014-10-29 17:04:56 +0100473 remove_wait_queue(&netdev_unregistering_wq, &wait);
Cong Wang200b9162014-05-12 15:11:20 -0700474}
475
Patrick McHardy38f7b872007-06-13 12:03:51 -0700476/**
477 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
478 * @ops: struct rtnl_link_ops * to unregister
479 */
480void rtnl_link_unregister(struct rtnl_link_ops *ops)
481{
Kirill Tkhai8518e9b2018-03-27 18:02:32 +0300482 /* Close the race with setup_net() and cleanup_net() */
Kirill Tkhai4420bf22018-03-27 18:02:23 +0300483 down_write(&pernet_ops_rwsem);
Cong Wang200b9162014-05-12 15:11:20 -0700484 rtnl_lock_unregistering_all();
Patrick McHardy38f7b872007-06-13 12:03:51 -0700485 __rtnl_link_unregister(ops);
486 rtnl_unlock();
Kirill Tkhai4420bf22018-03-27 18:02:23 +0300487 up_write(&pernet_ops_rwsem);
Patrick McHardy38f7b872007-06-13 12:03:51 -0700488}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700489EXPORT_SYMBOL_GPL(rtnl_link_unregister);
490
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100491static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
492{
493 struct net_device *master_dev;
494 const struct rtnl_link_ops *ops;
Florian Westphal8515ae32017-08-10 16:52:59 +0200495 size_t size = 0;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100496
Florian Westphal8515ae32017-08-10 16:52:59 +0200497 rcu_read_lock();
498
499 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100500 if (!master_dev)
Florian Westphal8515ae32017-08-10 16:52:59 +0200501 goto out;
502
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100503 ops = master_dev->rtnl_link_ops;
Fernando Luis Vazquez Cao6049f252014-02-04 19:35:02 +0900504 if (!ops || !ops->get_slave_size)
Florian Westphal8515ae32017-08-10 16:52:59 +0200505 goto out;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100506 /* IFLA_INFO_SLAVE_DATA + nested data */
Florian Westphal8515ae32017-08-10 16:52:59 +0200507 size = nla_total_size(sizeof(struct nlattr)) +
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100508 ops->get_slave_size(master_dev, dev);
Florian Westphal8515ae32017-08-10 16:52:59 +0200509
510out:
511 rcu_read_unlock();
512 return size;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100513}
514
Patrick McHardy38f7b872007-06-13 12:03:51 -0700515static size_t rtnl_link_get_size(const struct net_device *dev)
516{
517 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
518 size_t size;
519
520 if (!ops)
521 return 0;
522
Thomas Graf369cf772010-11-11 15:47:59 +0000523 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
524 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700525
526 if (ops->get_size)
527 /* IFLA_INFO_DATA + nested data */
Thomas Graf369cf772010-11-11 15:47:59 +0000528 size += nla_total_size(sizeof(struct nlattr)) +
Patrick McHardy38f7b872007-06-13 12:03:51 -0700529 ops->get_size(dev);
530
531 if (ops->get_xstats_size)
Thomas Graf369cf772010-11-11 15:47:59 +0000532 /* IFLA_INFO_XSTATS */
533 size += nla_total_size(ops->get_xstats_size(dev));
Patrick McHardy38f7b872007-06-13 12:03:51 -0700534
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100535 size += rtnl_link_get_slave_info_data_size(dev);
536
Patrick McHardy38f7b872007-06-13 12:03:51 -0700537 return size;
538}
539
Thomas Graff8ff1822010-11-16 04:30:14 +0000540static LIST_HEAD(rtnl_af_ops);
541
542static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
543{
544 const struct rtnl_af_ops *ops;
545
Cong Wanga1002432021-05-08 11:00:33 -0700546 ASSERT_RTNL();
547
548 list_for_each_entry(ops, &rtnl_af_ops, list) {
Thomas Graff8ff1822010-11-16 04:30:14 +0000549 if (ops->family == family)
550 return ops;
551 }
552
553 return NULL;
554}
555
556/**
Thomas Graff8ff1822010-11-16 04:30:14 +0000557 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
558 * @ops: struct rtnl_af_ops * to register
559 *
560 * Returns 0 on success or a negative error code.
561 */
stephen hemminger3678a9d2013-12-30 10:41:32 -0800562void rtnl_af_register(struct rtnl_af_ops *ops)
Thomas Graff8ff1822010-11-16 04:30:14 +0000563{
Thomas Graff8ff1822010-11-16 04:30:14 +0000564 rtnl_lock();
Florian Westphal5fa85a02017-10-16 15:44:36 +0200565 list_add_tail_rcu(&ops->list, &rtnl_af_ops);
Thomas Graff8ff1822010-11-16 04:30:14 +0000566 rtnl_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +0000567}
568EXPORT_SYMBOL_GPL(rtnl_af_register);
569
570/**
Thomas Graff8ff1822010-11-16 04:30:14 +0000571 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
572 * @ops: struct rtnl_af_ops * to unregister
573 */
574void rtnl_af_unregister(struct rtnl_af_ops *ops)
575{
576 rtnl_lock();
Florian Westphal5fa85a02017-10-16 15:44:36 +0200577 list_del_rcu(&ops->list);
Thomas Graff8ff1822010-11-16 04:30:14 +0000578 rtnl_unlock();
Florian Westphal5fa85a02017-10-16 15:44:36 +0200579
580 synchronize_rcu();
Thomas Graff8ff1822010-11-16 04:30:14 +0000581}
582EXPORT_SYMBOL_GPL(rtnl_af_unregister);
583
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700584static size_t rtnl_link_get_af_size(const struct net_device *dev,
585 u32 ext_filter_mask)
Thomas Graff8ff1822010-11-16 04:30:14 +0000586{
587 struct rtnl_af_ops *af_ops;
588 size_t size;
589
590 /* IFLA_AF_SPEC */
591 size = nla_total_size(sizeof(struct nlattr));
592
Florian Westphal5fa85a02017-10-16 15:44:36 +0200593 rcu_read_lock();
594 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Thomas Graff8ff1822010-11-16 04:30:14 +0000595 if (af_ops->get_link_af_size) {
596 /* AF_* + nested data */
597 size += nla_total_size(sizeof(struct nlattr)) +
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700598 af_ops->get_link_af_size(dev, ext_filter_mask);
Thomas Graff8ff1822010-11-16 04:30:14 +0000599 }
600 }
Florian Westphal5fa85a02017-10-16 15:44:36 +0200601 rcu_read_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +0000602
603 return size;
604}
605
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100606static bool rtnl_have_link_slave_info(const struct net_device *dev)
607{
608 struct net_device *master_dev;
Florian Westphal4c82a952017-09-26 13:58:43 +0200609 bool ret = false;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100610
Florian Westphal4c82a952017-09-26 13:58:43 +0200611 rcu_read_lock();
612
613 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
Jiri Pirko813f0202014-01-23 19:19:21 +0100614 if (master_dev && master_dev->rtnl_link_ops)
Florian Westphal4c82a952017-09-26 13:58:43 +0200615 ret = true;
616 rcu_read_unlock();
617 return ret;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100618}
619
620static int rtnl_link_slave_info_fill(struct sk_buff *skb,
621 const struct net_device *dev)
622{
623 struct net_device *master_dev;
624 const struct rtnl_link_ops *ops;
625 struct nlattr *slave_data;
626 int err;
627
628 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
629 if (!master_dev)
630 return 0;
631 ops = master_dev->rtnl_link_ops;
632 if (!ops)
633 return 0;
634 if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
635 return -EMSGSIZE;
636 if (ops->fill_slave_info) {
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200637 slave_data = nla_nest_start_noflag(skb, IFLA_INFO_SLAVE_DATA);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100638 if (!slave_data)
639 return -EMSGSIZE;
640 err = ops->fill_slave_info(skb, master_dev, dev);
641 if (err < 0)
642 goto err_cancel_slave_data;
643 nla_nest_end(skb, slave_data);
644 }
645 return 0;
646
647err_cancel_slave_data:
648 nla_nest_cancel(skb, slave_data);
649 return err;
650}
651
652static int rtnl_link_info_fill(struct sk_buff *skb,
653 const struct net_device *dev)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700654{
655 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100656 struct nlattr *data;
657 int err;
658
659 if (!ops)
660 return 0;
661 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
662 return -EMSGSIZE;
663 if (ops->fill_xstats) {
664 err = ops->fill_xstats(skb, dev);
665 if (err < 0)
666 return err;
667 }
668 if (ops->fill_info) {
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200669 data = nla_nest_start_noflag(skb, IFLA_INFO_DATA);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100670 if (data == NULL)
671 return -EMSGSIZE;
672 err = ops->fill_info(skb, dev);
673 if (err < 0)
674 goto err_cancel_data;
675 nla_nest_end(skb, data);
676 }
677 return 0;
678
679err_cancel_data:
680 nla_nest_cancel(skb, data);
681 return err;
682}
683
684static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
685{
686 struct nlattr *linkinfo;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700687 int err = -EMSGSIZE;
688
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200689 linkinfo = nla_nest_start_noflag(skb, IFLA_LINKINFO);
Patrick McHardy38f7b872007-06-13 12:03:51 -0700690 if (linkinfo == NULL)
691 goto out;
692
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100693 err = rtnl_link_info_fill(skb, dev);
694 if (err < 0)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700695 goto err_cancel_link;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100696
697 err = rtnl_link_slave_info_fill(skb, dev);
698 if (err < 0)
699 goto err_cancel_link;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700700
701 nla_nest_end(skb, linkinfo);
702 return 0;
703
Patrick McHardy38f7b872007-06-13 12:03:51 -0700704err_cancel_link:
705 nla_nest_cancel(skb, linkinfo);
706out:
707 return err;
708}
709
Eric Dumazet95c96172012-04-15 05:58:06 +0000710int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800712 struct sock *rtnl = net->rtnl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Yajun Dengcfdf0d92021-07-15 20:12:57 +0800714 return nlmsg_notify(rtnl, skb, pid, group, echo, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
716
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800717int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
Thomas Graf2942e902006-08-15 00:30:25 -0700718{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800719 struct sock *rtnl = net->rtnl;
720
Thomas Graf2942e902006-08-15 00:30:25 -0700721 return nlmsg_unicast(rtnl, skb, pid);
722}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800723EXPORT_SYMBOL(rtnl_unicast);
Thomas Graf2942e902006-08-15 00:30:25 -0700724
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800725void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
726 struct nlmsghdr *nlh, gfp_t flags)
Thomas Graf97676b62006-08-15 00:31:41 -0700727{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800728 struct sock *rtnl = net->rtnl;
Thomas Graf97676b62006-08-15 00:31:41 -0700729
Yajun Dengf9b282b2021-07-27 11:41:41 +0800730 nlmsg_notify(rtnl, skb, pid, group, nlmsg_report(nlh), flags);
Thomas Graf97676b62006-08-15 00:31:41 -0700731}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800732EXPORT_SYMBOL(rtnl_notify);
Thomas Graf97676b62006-08-15 00:31:41 -0700733
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800734void rtnl_set_sk_err(struct net *net, u32 group, int error)
Thomas Graf97676b62006-08-15 00:31:41 -0700735{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800736 struct sock *rtnl = net->rtnl;
737
Thomas Graf97676b62006-08-15 00:31:41 -0700738 netlink_set_err(rtnl, 0, group, error);
739}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800740EXPORT_SYMBOL(rtnl_set_sk_err);
Thomas Graf97676b62006-08-15 00:31:41 -0700741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
743{
Thomas Graf2d7202b2006-08-22 00:01:27 -0700744 struct nlattr *mx;
745 int i, valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
David Ahernc22a1332019-06-21 16:27:16 -0700747 /* nothing is dumped for dst_default_metrics, so just skip the loop */
748 if (metrics == dst_default_metrics.metrics)
749 return 0;
750
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200751 mx = nla_nest_start_noflag(skb, RTA_METRICS);
Thomas Graf2d7202b2006-08-22 00:01:27 -0700752 if (mx == NULL)
753 return -ENOBUFS;
754
755 for (i = 0; i < RTAX_MAX; i++) {
756 if (metrics[i]) {
Daniel Borkmannea697632015-01-05 23:57:47 +0100757 if (i == RTAX_CC_ALGO - 1) {
758 char tmp[TCP_CA_NAME_MAX], *name;
759
760 name = tcp_ca_get_name_by_key(metrics[i], tmp);
761 if (!name)
762 continue;
763 if (nla_put_string(skb, i + 1, name))
764 goto nla_put_failure;
Daniel Borkmannc3a8d942015-08-31 15:58:47 +0200765 } else if (i == RTAX_FEATURES - 1) {
766 u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
767
Phil Sutterf8edcd12016-08-23 13:14:31 +0200768 if (!user_features)
769 continue;
Daniel Borkmannc3a8d942015-08-31 15:58:47 +0200770 BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
771 if (nla_put_u32(skb, i + 1, user_features))
772 goto nla_put_failure;
Daniel Borkmannea697632015-01-05 23:57:47 +0100773 } else {
774 if (nla_put_u32(skb, i + 1, metrics[i]))
775 goto nla_put_failure;
776 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700777 valid++;
Thomas Graf2d7202b2006-08-22 00:01:27 -0700778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
David S. Millera57d27f2006-08-22 22:20:14 -0700781 if (!valid) {
782 nla_nest_cancel(skb, mx);
783 return 0;
784 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700785
786 return nla_nest_end(skb, mx);
787
788nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700789 nla_nest_cancel(skb, mx);
790 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800792EXPORT_SYMBOL(rtnetlink_put_metrics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Thomas Grafe3703b32006-11-27 09:27:07 -0800794int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
David S. Miller87a50692012-07-10 05:06:14 -0700795 long expires, u32 error)
Thomas Grafe3703b32006-11-27 09:27:07 -0800796{
797 struct rta_cacheinfo ci = {
Thomas Grafe3703b32006-11-27 09:27:07 -0800798 .rta_error = error,
799 .rta_id = id,
Thomas Grafe3703b32006-11-27 09:27:07 -0800800 };
801
David Ahern39407462018-04-17 17:33:08 -0700802 if (dst) {
803 ci.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse);
804 ci.rta_used = dst->__use;
805 ci.rta_clntref = atomic_read(&dst->__refcnt);
806 }
Li Wei82539472012-07-29 16:01:30 +0000807 if (expires) {
808 unsigned long clock;
Thomas Grafe3703b32006-11-27 09:27:07 -0800809
Li Wei82539472012-07-29 16:01:30 +0000810 clock = jiffies_to_clock_t(abs(expires));
811 clock = min_t(unsigned long, clock, INT_MAX);
812 ci.rta_expires = (expires > 0) ? clock : -clock;
813 }
Thomas Grafe3703b32006-11-27 09:27:07 -0800814 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
815}
Thomas Grafe3703b32006-11-27 09:27:07 -0800816EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
David S. Miller93b2d4a2008-02-17 18:35:07 -0800818static void set_operstate(struct net_device *dev, unsigned char transition)
Stefan Rompfb00055a2006-03-20 17:09:11 -0800819{
820 unsigned char operstate = dev->operstate;
821
Eric Dumazete0d087a2009-11-07 01:26:17 -0800822 switch (transition) {
Stefan Rompfb00055a2006-03-20 17:09:11 -0800823 case IF_OPER_UP:
824 if ((operstate == IF_OPER_DORMANT ||
Andrew Lunneec517cd2020-04-20 00:11:50 +0200825 operstate == IF_OPER_TESTING ||
Stefan Rompfb00055a2006-03-20 17:09:11 -0800826 operstate == IF_OPER_UNKNOWN) &&
Andrew Lunneec517cd2020-04-20 00:11:50 +0200827 !netif_dormant(dev) && !netif_testing(dev))
Stefan Rompfb00055a2006-03-20 17:09:11 -0800828 operstate = IF_OPER_UP;
829 break;
830
Andrew Lunneec517cd2020-04-20 00:11:50 +0200831 case IF_OPER_TESTING:
832 if (operstate == IF_OPER_UP ||
833 operstate == IF_OPER_UNKNOWN)
834 operstate = IF_OPER_TESTING;
835 break;
836
Stefan Rompfb00055a2006-03-20 17:09:11 -0800837 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) {
Sebastian Andrzej Siewiorfd888e82021-11-26 17:15:29 +0100845 write_lock(&dev_base_lock);
Stefan Rompfb00055a2006-03-20 17:09:11 -0800846 dev->operstate = operstate;
Sebastian Andrzej Siewiorfd888e82021-11-26 17:15:29 +0100847 write_unlock(&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
Roopa Prabhu829eb202020-07-31 17:34:01 -0700996static size_t rtnl_proto_down_size(const struct net_device *dev)
997{
998 size_t size = nla_total_size(1);
999
1000 if (dev->proto_down_reason)
1001 size += nla_total_size(0) + nla_total_size(4);
1002
1003 return size;
1004}
1005
Greg Rose115c9b82012-02-21 16:54:48 -05001006static noinline size_t if_nlmsg_size(const struct net_device *dev,
1007 u32 ext_filter_mask)
Thomas Graf339bf982006-11-10 14:10:15 -08001008{
1009 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
1010 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001011 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
Thomas Graf339bf982006-11-10 14:10:15 -08001012 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
Nicolas Dichtel270cb4d2016-04-26 10:06:16 +02001013 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
Thomas Graf339bf982006-11-10 14:10:15 -08001014 + nla_total_size(sizeof(struct rtnl_link_stats))
David S. Miller35c58452016-04-19 19:49:29 -04001015 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
Thomas Graf339bf982006-11-10 14:10:15 -08001016 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
1017 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
1018 + nla_total_size(4) /* IFLA_TXQLEN */
1019 + nla_total_size(4) /* IFLA_WEIGHT */
1020 + nla_total_size(4) /* IFLA_MTU */
1021 + nla_total_size(4) /* IFLA_LINK */
1022 + nla_total_size(4) /* IFLA_MASTER */
Jiri Pirko9a572472012-12-27 23:49:39 +00001023 + nla_total_size(1) /* IFLA_CARRIER */
Ben Greearedbc0bb2012-03-29 12:51:30 +00001024 + nla_total_size(4) /* IFLA_PROMISCUITY */
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001025 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
1026 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
Tobias Klauser69197562016-11-30 14:30:37 +01001027 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
1028 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
Coco Lieac1b932022-01-05 02:48:38 -08001029 + nla_total_size(4) /* IFLA_GRO_MAX_SIZE */
Thomas Graf339bf982006-11-10 14:10:15 -08001030 + nla_total_size(1) /* IFLA_OPERSTATE */
Patrick McHardy38f7b872007-06-13 12:03:51 -07001031 + nla_total_size(1) /* IFLA_LINKMODE */
david decotigny2d3b4792014-03-29 09:48:35 -07001032 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001033 + nla_total_size(4) /* IFLA_LINK_NETNSID */
Serhey Popovychdb833d42017-06-20 14:35:23 +03001034 + nla_total_size(4) /* IFLA_GROUP */
Greg Rose115c9b82012-02-21 16:54:48 -05001035 + nla_total_size(ext_filter_mask
1036 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
1037 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
David Gibsonc53864f2014-04-24 10:22:36 +10001038 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
Thomas Graff8ff1822010-11-16 04:30:14 +00001039 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
Arad, Ronenb1974ed2015-10-19 09:23:28 -07001040 + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
Jiri Pirko82f28412014-11-28 14:34:18 +01001041 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001042 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
Nicolas Dichtelc57c7a92016-03-31 18:10:31 +02001043 + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
David S. Millerb5cdae32017-04-18 15:36:58 -04001044 + rtnl_xdp_size() /* IFLA_XDP */
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001045 + nla_total_size(4) /* IFLA_EVENT */
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02001046 + nla_total_size(4) /* IFLA_NEW_NETNSID */
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01001047 + nla_total_size(4) /* IFLA_NEW_IFINDEX */
Roopa Prabhu829eb202020-07-31 17:34:01 -07001048 + rtnl_proto_down_size(dev) /* proto down */
Christian Brauner7e4a8d52018-09-04 21:53:53 +02001049 + nla_total_size(4) /* IFLA_TARGET_NETNSID */
David Decotignyb2d3bcf2018-01-18 09:59:13 -08001050 + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */
1051 + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */
Stephen Hemminger3e7a50c2018-07-27 13:43:22 -07001052 + nla_total_size(4) /* IFLA_MIN_MTU */
1053 + nla_total_size(4) /* IFLA_MAX_MTU */
Jiri Pirko88f4fb02019-09-30 11:48:17 +02001054 + rtnl_prop_list_size(dev)
Michal Kubecekf74877a2019-12-11 10:58:14 +01001055 + nla_total_size(MAX_ADDR_LEN) /* IFLA_PERM_ADDRESS */
Jiri Benc79e1ad12017-11-02 17:04:38 -02001056 + 0;
Thomas Graf339bf982006-11-10 14:10:15 -08001057}
1058
Scott Feldman57b61082010-05-17 22:49:55 -07001059static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
1060{
1061 struct nlattr *vf_ports;
1062 struct nlattr *vf_port;
1063 int vf;
1064 int err;
1065
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001066 vf_ports = nla_nest_start_noflag(skb, IFLA_VF_PORTS);
Scott Feldman57b61082010-05-17 22:49:55 -07001067 if (!vf_ports)
1068 return -EMSGSIZE;
1069
1070 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001071 vf_port = nla_nest_start_noflag(skb, IFLA_VF_PORT);
Scott Feldman8ca94182010-05-28 03:42:18 -07001072 if (!vf_port)
1073 goto nla_put_failure;
David S. Millera6574342012-04-01 20:12:00 -04001074 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
1075 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001076 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
Scott Feldman8ca94182010-05-28 03:42:18 -07001077 if (err == -EMSGSIZE)
1078 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001079 if (err) {
Scott Feldman57b61082010-05-17 22:49:55 -07001080 nla_nest_cancel(skb, vf_port);
1081 continue;
1082 }
1083 nla_nest_end(skb, vf_port);
1084 }
1085
1086 nla_nest_end(skb, vf_ports);
1087
1088 return 0;
Scott Feldman8ca94182010-05-28 03:42:18 -07001089
1090nla_put_failure:
1091 nla_nest_cancel(skb, vf_ports);
1092 return -EMSGSIZE;
Scott Feldman57b61082010-05-17 22:49:55 -07001093}
1094
1095static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
1096{
1097 struct nlattr *port_self;
1098 int err;
1099
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001100 port_self = nla_nest_start_noflag(skb, IFLA_PORT_SELF);
Scott Feldman57b61082010-05-17 22:49:55 -07001101 if (!port_self)
1102 return -EMSGSIZE;
1103
1104 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
1105 if (err) {
1106 nla_nest_cancel(skb, port_self);
Scott Feldman8ca94182010-05-28 03:42:18 -07001107 return (err == -EMSGSIZE) ? err : 0;
Scott Feldman57b61082010-05-17 22:49:55 -07001108 }
1109
1110 nla_nest_end(skb, port_self);
1111
1112 return 0;
1113}
1114
David Gibsonc53864f2014-04-24 10:22:36 +10001115static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1116 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -07001117{
1118 int err;
1119
David Gibsonc53864f2014-04-24 10:22:36 +10001120 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1121 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -07001122 return 0;
1123
1124 err = rtnl_port_self_fill(skb, dev);
1125 if (err)
1126 return err;
1127
1128 if (dev_num_vf(dev->dev.parent)) {
1129 err = rtnl_vf_ports_fill(skb, dev);
1130 if (err)
1131 return err;
1132 }
1133
1134 return 0;
1135}
1136
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001137static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
1138{
1139 int err;
Jiri Pirko02637fc2014-11-28 14:34:16 +01001140 struct netdev_phys_item_id ppid;
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001141
1142 err = dev_get_phys_port_id(dev, &ppid);
1143 if (err) {
1144 if (err == -EOPNOTSUPP)
1145 return 0;
1146 return err;
1147 }
1148
1149 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1150 return -EMSGSIZE;
1151
1152 return 0;
1153}
1154
David Aherndb24a902015-03-17 20:23:15 -06001155static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1156{
1157 char name[IFNAMSIZ];
1158 int err;
1159
1160 err = dev_get_phys_port_name(dev, name, sizeof(name));
1161 if (err) {
1162 if (err == -EOPNOTSUPP)
1163 return 0;
1164 return err;
1165 }
1166
Michal Schmidt77ef0332017-05-04 16:48:58 +02001167 if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
David Aherndb24a902015-03-17 20:23:15 -06001168 return -EMSGSIZE;
1169
1170 return 0;
1171}
1172
Jiri Pirko82f28412014-11-28 14:34:18 +01001173static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1174{
Florian Fainellibccb3022019-02-06 09:45:46 -08001175 struct netdev_phys_item_id ppid = { };
Jiri Pirko82f28412014-11-28 14:34:18 +01001176 int err;
Jiri Pirko82f28412014-11-28 14:34:18 +01001177
Florian Fainellibccb3022019-02-06 09:45:46 -08001178 err = dev_get_port_parent_id(dev, &ppid, false);
Jiri Pirko82f28412014-11-28 14:34:18 +01001179 if (err) {
1180 if (err == -EOPNOTSUPP)
1181 return 0;
1182 return err;
1183 }
1184
Florian Fainellibccb3022019-02-06 09:45:46 -08001185 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, ppid.id_len, ppid.id))
Jiri Pirko82f28412014-11-28 14:34:18 +01001186 return -EMSGSIZE;
1187
1188 return 0;
1189}
1190
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001191static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1192 struct net_device *dev)
1193{
Roopa Prabhu550bce52016-04-15 20:36:25 -07001194 struct rtnl_link_stats64 *sp;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001195 struct nlattr *attr;
1196
Nicolas Dichtel58414d32016-04-21 18:58:25 +02001197 attr = nla_reserve_64bit(skb, IFLA_STATS64,
1198 sizeof(struct rtnl_link_stats64), IFLA_PAD);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001199 if (!attr)
1200 return -EMSGSIZE;
1201
Roopa Prabhu550bce52016-04-15 20:36:25 -07001202 sp = nla_data(attr);
1203 dev_get_stats(dev, sp);
1204
1205 attr = nla_reserve(skb, IFLA_STATS,
1206 sizeof(struct rtnl_link_stats));
1207 if (!attr)
1208 return -EMSGSIZE;
1209
1210 copy_rtnl_link_stats(nla_data(attr), sp);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001211
1212 return 0;
1213}
1214
1215static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1216 struct net_device *dev,
1217 int vfs_num,
1218 struct nlattr *vfinfo)
1219{
1220 struct ifla_vf_rss_query_en vf_rss_query_en;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001221 struct nlattr *vf, *vfstats, *vfvlanlist;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001222 struct ifla_vf_link_state vf_linkstate;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001223 struct ifla_vf_vlan_info vf_vlan_info;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001224 struct ifla_vf_spoofchk vf_spoofchk;
1225 struct ifla_vf_tx_rate vf_tx_rate;
1226 struct ifla_vf_stats vf_stats;
1227 struct ifla_vf_trust vf_trust;
1228 struct ifla_vf_vlan vf_vlan;
1229 struct ifla_vf_rate vf_rate;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001230 struct ifla_vf_mac vf_mac;
Denis Kirjanov75345f882019-06-17 10:53:41 +02001231 struct ifla_vf_broadcast vf_broadcast;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001232 struct ifla_vf_info ivi;
Danit Goldberg30aad412019-11-06 15:30:07 +02001233 struct ifla_vf_guid node_guid;
1234 struct ifla_vf_guid port_guid;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001235
Mintz, Yuval0eed9cf2017-06-07 21:00:33 +03001236 memset(&ivi, 0, sizeof(ivi));
1237
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001238 /* Not all SR-IOV capable drivers support the
1239 * spoofcheck and "RSS query enable" query. Preset to
1240 * -1 so the user space tool can detect that the driver
1241 * didn't report anything.
1242 */
1243 ivi.spoofchk = -1;
1244 ivi.rss_query_en = -1;
1245 ivi.trusted = -1;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001246 /* The default value for VF link state is "auto"
1247 * IFLA_VF_LINK_STATE_AUTO which equals zero
1248 */
1249 ivi.linkstate = 0;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001250 /* VLAN Protocol by default is 802.1Q */
1251 ivi.vlan_proto = htons(ETH_P_8021Q);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001252 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1253 return 0;
1254
Dan Carpenter775f4f02016-10-13 11:45:28 +03001255 memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
Leon Romanovsky9fbf0822020-01-30 14:59:49 +02001256 memset(&node_guid, 0, sizeof(node_guid));
1257 memset(&port_guid, 0, sizeof(port_guid));
Dan Carpenter775f4f02016-10-13 11:45:28 +03001258
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001259 vf_mac.vf =
1260 vf_vlan.vf =
Moshe Shemesh79aab092016-09-22 12:11:15 +03001261 vf_vlan_info.vf =
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001262 vf_rate.vf =
1263 vf_tx_rate.vf =
1264 vf_spoofchk.vf =
1265 vf_linkstate.vf =
1266 vf_rss_query_en.vf =
Danit Goldberg9aed6ae2019-12-03 17:43:36 +02001267 vf_trust.vf =
1268 node_guid.vf =
1269 port_guid.vf = ivi.vf;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001270
1271 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
Denis Kirjanov75345f882019-06-17 10:53:41 +02001272 memcpy(vf_broadcast.broadcast, dev->broadcast, dev->addr_len);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001273 vf_vlan.vlan = ivi.vlan;
1274 vf_vlan.qos = ivi.qos;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001275 vf_vlan_info.vlan = ivi.vlan;
1276 vf_vlan_info.qos = ivi.qos;
1277 vf_vlan_info.vlan_proto = ivi.vlan_proto;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001278 vf_tx_rate.rate = ivi.max_tx_rate;
1279 vf_rate.min_tx_rate = ivi.min_tx_rate;
1280 vf_rate.max_tx_rate = ivi.max_tx_rate;
1281 vf_spoofchk.setting = ivi.spoofchk;
1282 vf_linkstate.link_state = ivi.linkstate;
1283 vf_rss_query_en.setting = ivi.rss_query_en;
1284 vf_trust.setting = ivi.trusted;
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001285 vf = nla_nest_start_noflag(skb, IFLA_VF_INFO);
Moshe Shemesh79aab092016-09-22 12:11:15 +03001286 if (!vf)
1287 goto nla_put_vfinfo_failure;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001288 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
Denis Kirjanov75345f882019-06-17 10:53:41 +02001289 nla_put(skb, IFLA_VF_BROADCAST, sizeof(vf_broadcast), &vf_broadcast) ||
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001290 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1291 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1292 &vf_rate) ||
1293 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1294 &vf_tx_rate) ||
1295 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1296 &vf_spoofchk) ||
1297 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1298 &vf_linkstate) ||
1299 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1300 sizeof(vf_rss_query_en),
1301 &vf_rss_query_en) ||
1302 nla_put(skb, IFLA_VF_TRUST,
1303 sizeof(vf_trust), &vf_trust))
Moshe Shemesh79aab092016-09-22 12:11:15 +03001304 goto nla_put_vf_failure;
Danit Goldberg30aad412019-11-06 15:30:07 +02001305
Danit Goldberg30aad412019-11-06 15:30:07 +02001306 if (dev->netdev_ops->ndo_get_vf_guid &&
1307 !dev->netdev_ops->ndo_get_vf_guid(dev, vfs_num, &node_guid,
1308 &port_guid)) {
1309 if (nla_put(skb, IFLA_VF_IB_NODE_GUID, sizeof(node_guid),
1310 &node_guid) ||
1311 nla_put(skb, IFLA_VF_IB_PORT_GUID, sizeof(port_guid),
1312 &port_guid))
1313 goto nla_put_vf_failure;
1314 }
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001315 vfvlanlist = nla_nest_start_noflag(skb, IFLA_VF_VLAN_LIST);
Moshe Shemesh79aab092016-09-22 12:11:15 +03001316 if (!vfvlanlist)
1317 goto nla_put_vf_failure;
1318 if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
1319 &vf_vlan_info)) {
1320 nla_nest_cancel(skb, vfvlanlist);
1321 goto nla_put_vf_failure;
1322 }
1323 nla_nest_end(skb, vfvlanlist);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001324 memset(&vf_stats, 0, sizeof(vf_stats));
1325 if (dev->netdev_ops->ndo_get_vf_stats)
1326 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1327 &vf_stats);
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001328 vfstats = nla_nest_start_noflag(skb, IFLA_VF_STATS);
Moshe Shemesh79aab092016-09-22 12:11:15 +03001329 if (!vfstats)
1330 goto nla_put_vf_failure;
Nicolas Dichtel343a6d82016-04-25 10:25:14 +02001331 if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1332 vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1333 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1334 vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1335 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1336 vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1337 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1338 vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1339 nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1340 vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1341 nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
Eugenia Emantayevc5a9f6f2017-07-17 13:47:07 +03001342 vf_stats.multicast, IFLA_VF_STATS_PAD) ||
1343 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED,
1344 vf_stats.rx_dropped, IFLA_VF_STATS_PAD) ||
1345 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED,
1346 vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) {
Moshe Shemesh79aab092016-09-22 12:11:15 +03001347 nla_nest_cancel(skb, vfstats);
1348 goto nla_put_vf_failure;
1349 }
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001350 nla_nest_end(skb, vfstats);
1351 nla_nest_end(skb, vf);
1352 return 0;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001353
1354nla_put_vf_failure:
1355 nla_nest_cancel(skb, vf);
1356nla_put_vfinfo_failure:
1357 nla_nest_cancel(skb, vfinfo);
1358 return -EMSGSIZE;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001359}
1360
Florian Westphal250fc3d2017-09-26 13:58:41 +02001361static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb,
1362 struct net_device *dev,
1363 u32 ext_filter_mask)
1364{
1365 struct nlattr *vfinfo;
1366 int i, num_vfs;
1367
1368 if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0))
1369 return 0;
1370
1371 num_vfs = dev_num_vf(dev->dev.parent);
1372 if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs))
1373 return -EMSGSIZE;
1374
1375 if (!dev->netdev_ops->ndo_get_vf_config)
1376 return 0;
1377
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001378 vfinfo = nla_nest_start_noflag(skb, IFLA_VFINFO_LIST);
Florian Westphal250fc3d2017-09-26 13:58:41 +02001379 if (!vfinfo)
1380 return -EMSGSIZE;
1381
1382 for (i = 0; i < num_vfs; i++) {
1383 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1384 return -EMSGSIZE;
1385 }
1386
1387 nla_nest_end(skb, vfinfo);
1388 return 0;
1389}
1390
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001391static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1392{
Kangjie Lu5f8e4472016-05-03 16:46:24 -04001393 struct rtnl_link_ifmap map;
1394
1395 memset(&map, 0, sizeof(map));
1396 map.mem_start = dev->mem_start;
1397 map.mem_end = dev->mem_end;
1398 map.base_addr = dev->base_addr;
1399 map.irq = dev->irq;
1400 map.dma = dev->dma;
1401 map.port = dev->if_port;
1402
Nicolas Dichtel270cb4d2016-04-26 10:06:16 +02001403 if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001404 return -EMSGSIZE;
1405
1406 return 0;
1407}
1408
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001409static u32 rtnl_xdp_prog_skb(struct net_device *dev)
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001410{
Martin KaFai Lau58038692017-06-15 17:29:09 -07001411 const struct bpf_prog *generic_xdp_prog;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001412
1413 ASSERT_RTNL();
1414
Martin KaFai Lau58038692017-06-15 17:29:09 -07001415 generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001416 if (!generic_xdp_prog)
1417 return 0;
1418 return generic_xdp_prog->aux->id;
1419}
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001420
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001421static u32 rtnl_xdp_prog_drv(struct net_device *dev)
1422{
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07001423 return dev_xdp_prog_id(dev, XDP_MODE_DRV);
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001424}
Jakub Kicinski118b4aa2017-12-01 15:08:55 -08001425
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001426static u32 rtnl_xdp_prog_hw(struct net_device *dev)
1427{
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07001428 return dev_xdp_prog_id(dev, XDP_MODE_HW);
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001429}
1430
1431static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev,
1432 u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr,
1433 u32 (*get_prog_id)(struct net_device *dev))
1434{
1435 u32 curr_id;
1436 int err;
1437
1438 curr_id = get_prog_id(dev);
1439 if (!curr_id)
1440 return 0;
1441
1442 *prog_id = curr_id;
1443 err = nla_put_u32(skb, attr, curr_id);
1444 if (err)
1445 return err;
1446
1447 if (*mode != XDP_ATTACHED_NONE)
1448 *mode = XDP_ATTACHED_MULTI;
1449 else
1450 *mode = tgt_mode;
1451
1452 return 0;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001453}
1454
Brenden Blancod1fdd912016-07-19 12:16:49 -07001455static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1456{
Brenden Blancod1fdd912016-07-19 12:16:49 -07001457 struct nlattr *xdp;
Martin KaFai Lau58038692017-06-15 17:29:09 -07001458 u32 prog_id;
Brenden Blancod1fdd912016-07-19 12:16:49 -07001459 int err;
Jakub Kicinski4f91da22018-07-11 20:36:38 -07001460 u8 mode;
Brenden Blancod1fdd912016-07-19 12:16:49 -07001461
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001462 xdp = nla_nest_start_noflag(skb, IFLA_XDP);
Brenden Blancod1fdd912016-07-19 12:16:49 -07001463 if (!xdp)
1464 return -EMSGSIZE;
David S. Millerb5cdae32017-04-18 15:36:58 -04001465
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001466 prog_id = 0;
1467 mode = XDP_ATTACHED_NONE;
Jakub Kicinski202aabe2018-07-16 19:08:50 -07001468 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB,
1469 IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb);
1470 if (err)
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001471 goto err_cancel;
Jakub Kicinski202aabe2018-07-16 19:08:50 -07001472 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV,
1473 IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv);
1474 if (err)
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001475 goto err_cancel;
Jakub Kicinski202aabe2018-07-16 19:08:50 -07001476 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW,
1477 IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw);
1478 if (err)
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001479 goto err_cancel;
1480
Jakub Kicinski4f91da22018-07-11 20:36:38 -07001481 err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode);
Brenden Blancod1fdd912016-07-19 12:16:49 -07001482 if (err)
1483 goto err_cancel;
1484
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001485 if (prog_id && mode != XDP_ATTACHED_MULTI) {
Martin KaFai Lau58038692017-06-15 17:29:09 -07001486 err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
1487 if (err)
1488 goto err_cancel;
1489 }
1490
Brenden Blancod1fdd912016-07-19 12:16:49 -07001491 nla_nest_end(skb, xdp);
1492 return 0;
1493
1494err_cancel:
1495 nla_nest_cancel(skb, xdp);
1496 return err;
1497}
1498
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001499static u32 rtnl_get_event(unsigned long event)
1500{
1501 u32 rtnl_event_type = IFLA_EVENT_NONE;
1502
1503 switch (event) {
1504 case NETDEV_REBOOT:
1505 rtnl_event_type = IFLA_EVENT_REBOOT;
1506 break;
1507 case NETDEV_FEAT_CHANGE:
1508 rtnl_event_type = IFLA_EVENT_FEATURES;
1509 break;
1510 case NETDEV_BONDING_FAILOVER:
1511 rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
1512 break;
1513 case NETDEV_NOTIFY_PEERS:
1514 rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
1515 break;
1516 case NETDEV_RESEND_IGMP:
1517 rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
1518 break;
1519 case NETDEV_CHANGEINFODATA:
1520 rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
1521 break;
1522 default:
1523 break;
1524 }
1525
1526 return rtnl_event_type;
1527}
1528
Florian Westphal79110a02017-09-26 13:58:40 +02001529static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
1530{
1531 const struct net_device *upper_dev;
1532 int ret = 0;
1533
1534 rcu_read_lock();
1535
1536 upper_dev = netdev_master_upper_dev_get_rcu(dev);
1537 if (upper_dev)
1538 ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);
1539
1540 rcu_read_unlock();
1541 return ret;
1542}
1543
Sabrina Dubrocafeadc4b2019-05-14 15:12:19 +02001544static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev,
1545 bool force)
Florian Westphal79110a02017-09-26 13:58:40 +02001546{
1547 int ifindex = dev_get_iflink(dev);
1548
Sabrina Dubrocafeadc4b2019-05-14 15:12:19 +02001549 if (force || dev->ifindex != ifindex)
1550 return nla_put_u32(skb, IFLA_LINK, ifindex);
Florian Westphal79110a02017-09-26 13:58:40 +02001551
Sabrina Dubrocafeadc4b2019-05-14 15:12:19 +02001552 return 0;
Florian Westphal79110a02017-09-26 13:58:40 +02001553}
1554
Florian Westphal6c557002017-10-02 23:50:05 +02001555static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
1556 struct net_device *dev)
1557{
1558 char buf[IFALIASZ];
1559 int ret;
1560
1561 ret = dev_get_alias(dev, buf, sizeof(buf));
1562 return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
1563}
1564
Florian Westphalb1e66b92017-09-26 13:58:42 +02001565static int rtnl_fill_link_netnsid(struct sk_buff *skb,
Jiri Benc79e1ad12017-11-02 17:04:38 -02001566 const struct net_device *dev,
Guillaume Naultd4e4fdf2019-10-23 18:39:04 +02001567 struct net *src_net, gfp_t gfp)
Florian Westphalb1e66b92017-09-26 13:58:42 +02001568{
Sabrina Dubrocafeadc4b2019-05-14 15:12:19 +02001569 bool put_iflink = false;
1570
Florian Westphalb1e66b92017-09-26 13:58:42 +02001571 if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
1572 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1573
1574 if (!net_eq(dev_net(dev), link_net)) {
Guillaume Naultd4e4fdf2019-10-23 18:39:04 +02001575 int id = peernet2id_alloc(src_net, link_net, gfp);
Florian Westphalb1e66b92017-09-26 13:58:42 +02001576
1577 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1578 return -EMSGSIZE;
Sabrina Dubrocafeadc4b2019-05-14 15:12:19 +02001579
1580 put_iflink = true;
Florian Westphalb1e66b92017-09-26 13:58:42 +02001581 }
1582 }
1583
Sabrina Dubrocafeadc4b2019-05-14 15:12:19 +02001584 return nla_put_iflink(skb, dev, put_iflink);
Florian Westphalb1e66b92017-09-26 13:58:42 +02001585}
1586
Florian Westphal070cbf5b2017-10-16 15:44:35 +02001587static int rtnl_fill_link_af(struct sk_buff *skb,
1588 const struct net_device *dev,
1589 u32 ext_filter_mask)
1590{
1591 const struct rtnl_af_ops *af_ops;
1592 struct nlattr *af_spec;
1593
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001594 af_spec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
Florian Westphal070cbf5b2017-10-16 15:44:35 +02001595 if (!af_spec)
1596 return -EMSGSIZE;
1597
Florian Westphal5fa85a02017-10-16 15:44:36 +02001598 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Florian Westphal070cbf5b2017-10-16 15:44:35 +02001599 struct nlattr *af;
1600 int err;
1601
1602 if (!af_ops->fill_link_af)
1603 continue;
1604
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001605 af = nla_nest_start_noflag(skb, af_ops->family);
Florian Westphal070cbf5b2017-10-16 15:44:35 +02001606 if (!af)
1607 return -EMSGSIZE;
1608
1609 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1610 /*
1611 * Caller may return ENODATA to indicate that there
1612 * was no data to be dumped. This is not an error, it
1613 * means we should trim the attribute header and
1614 * continue.
1615 */
1616 if (err == -ENODATA)
1617 nla_nest_cancel(skb, af);
1618 else if (err < 0)
1619 return -EMSGSIZE;
1620
1621 nla_nest_end(skb, af);
1622 }
1623
1624 nla_nest_end(skb, af_spec);
1625 return 0;
1626}
1627
Jiri Pirko88f4fb02019-09-30 11:48:17 +02001628static int rtnl_fill_alt_ifnames(struct sk_buff *skb,
1629 const struct net_device *dev)
1630{
1631 struct netdev_name_node *name_node;
1632 int count = 0;
1633
1634 list_for_each_entry(name_node, &dev->name_node->list, list) {
1635 if (nla_put_string(skb, IFLA_ALT_IFNAME, name_node->name))
1636 return -EMSGSIZE;
1637 count++;
1638 }
1639 return count;
1640}
1641
1642static int rtnl_fill_prop_list(struct sk_buff *skb,
1643 const struct net_device *dev)
1644{
1645 struct nlattr *prop_list;
1646 int ret;
1647
1648 prop_list = nla_nest_start(skb, IFLA_PROP_LIST);
1649 if (!prop_list)
1650 return -EMSGSIZE;
1651
1652 ret = rtnl_fill_alt_ifnames(skb, dev);
1653 if (ret <= 0)
1654 goto nest_cancel;
1655
1656 nla_nest_end(skb, prop_list);
1657 return 0;
1658
1659nest_cancel:
1660 nla_nest_cancel(skb, prop_list);
1661 return ret;
1662}
1663
Roopa Prabhu829eb202020-07-31 17:34:01 -07001664static int rtnl_fill_proto_down(struct sk_buff *skb,
1665 const struct net_device *dev)
1666{
1667 struct nlattr *pr;
1668 u32 preason;
1669
1670 if (nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
1671 goto nla_put_failure;
1672
1673 preason = dev->proto_down_reason;
1674 if (!preason)
1675 return 0;
1676
1677 pr = nla_nest_start(skb, IFLA_PROTO_DOWN_REASON);
1678 if (!pr)
1679 return -EMSGSIZE;
1680
1681 if (nla_put_u32(skb, IFLA_PROTO_DOWN_REASON_VALUE, preason)) {
1682 nla_nest_cancel(skb, pr);
1683 goto nla_put_failure;
1684 }
1685
1686 nla_nest_end(skb, pr);
1687 return 0;
1688
1689nla_put_failure:
1690 return -EMSGSIZE;
1691}
1692
Jiri Benc79e1ad12017-11-02 17:04:38 -02001693static int rtnl_fill_ifinfo(struct sk_buff *skb,
1694 struct net_device *dev, struct net *src_net,
Patrick McHardy575c3e22007-05-22 17:00:49 -07001695 int type, u32 pid, u32 seq, u32 change,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001696 unsigned int flags, u32 ext_filter_mask,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01001697 u32 event, int *new_nsid, int new_ifindex,
Guillaume Naultd4e4fdf2019-10-23 18:39:04 +02001698 int tgt_netnsid, gfp_t gfp)
Thomas Grafb60c5112006-08-04 23:05:34 -07001699{
1700 struct ifinfomsg *ifm;
1701 struct nlmsghdr *nlh;
1702
Eric Dumazet2907c352011-05-25 07:34:04 +00001703 ASSERT_RTNL();
Thomas Grafb60c5112006-08-04 23:05:34 -07001704 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1705 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08001706 return -EMSGSIZE;
Thomas Grafb60c5112006-08-04 23:05:34 -07001707
1708 ifm = nlmsg_data(nlh);
1709 ifm->ifi_family = AF_UNSPEC;
1710 ifm->__ifi_pad = 0;
1711 ifm->ifi_type = dev->type;
1712 ifm->ifi_index = dev->ifindex;
1713 ifm->ifi_flags = dev_get_flags(dev);
1714 ifm->ifi_change = change;
1715
Christian Brauner7e4a8d52018-09-04 21:53:53 +02001716 if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid))
Jiri Benc79e1ad12017-11-02 17:04:38 -02001717 goto nla_put_failure;
1718
David S. Millera6574342012-04-01 20:12:00 -04001719 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1720 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1721 nla_put_u8(skb, IFLA_OPERSTATE,
1722 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1723 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1724 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
Stephen Hemminger3e7a50c2018-07-27 13:43:22 -07001725 nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) ||
1726 nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) ||
David S. Millera6574342012-04-01 20:12:00 -04001727 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
Ben Greearedbc0bb2012-03-29 12:51:30 +00001728 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001729 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
Eric Dumazetc70ce022016-03-21 09:55:10 -07001730 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1731 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
Coco Lieac1b932022-01-05 02:48:38 -08001732 nla_put_u32(skb, IFLA_GRO_MAX_SIZE, dev->gro_max_size) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001733#ifdef CONFIG_RPS
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001734 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001735#endif
Florian Westphal79110a02017-09-26 13:58:40 +02001736 put_master_ifindex(skb, dev) ||
Jiri Pirko9a572472012-12-27 23:49:39 +00001737 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
David S. Millera6574342012-04-01 20:12:00 -04001738 (dev->qdisc &&
1739 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
Florian Westphal6c557002017-10-02 23:50:05 +02001740 nla_put_ifalias(skb, dev) ||
david decotigny2d3b4792014-03-29 09:48:35 -07001741 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
David Decotignyb2d3bcf2018-01-18 09:59:13 -08001742 atomic_read(&dev->carrier_up_count) +
1743 atomic_read(&dev->carrier_down_count)) ||
David Decotignyb2d3bcf2018-01-18 09:59:13 -08001744 nla_put_u32(skb, IFLA_CARRIER_UP_COUNT,
1745 atomic_read(&dev->carrier_up_count)) ||
1746 nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT,
1747 atomic_read(&dev->carrier_down_count)))
David S. Millera6574342012-04-01 20:12:00 -04001748 goto nla_put_failure;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001749
Roopa Prabhu829eb202020-07-31 17:34:01 -07001750 if (rtnl_fill_proto_down(skb, dev))
1751 goto nla_put_failure;
1752
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001753 if (event != IFLA_EVENT_NONE) {
1754 if (nla_put_u32(skb, IFLA_EVENT, event))
1755 goto nla_put_failure;
1756 }
1757
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001758 if (rtnl_fill_link_ifmap(skb, dev))
1759 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
1761 if (dev->addr_len) {
David S. Millera6574342012-04-01 20:12:00 -04001762 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1763 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1764 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 }
1766
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001767 if (rtnl_phys_port_id_fill(skb, dev))
1768 goto nla_put_failure;
1769
David Aherndb24a902015-03-17 20:23:15 -06001770 if (rtnl_phys_port_name_fill(skb, dev))
1771 goto nla_put_failure;
1772
Jiri Pirko82f28412014-11-28 14:34:18 +01001773 if (rtnl_phys_switch_id_fill(skb, dev))
1774 goto nla_put_failure;
1775
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001776 if (rtnl_fill_stats(skb, dev))
Pavel Emelyanov96e74082008-05-21 14:12:46 -07001777 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
Florian Westphal250fc3d2017-09-26 13:58:41 +02001779 if (rtnl_fill_vf(skb, dev, ext_filter_mask))
David S. Millera6574342012-04-01 20:12:00 -04001780 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001781
David Gibsonc53864f2014-04-24 10:22:36 +10001782 if (rtnl_port_fill(skb, dev, ext_filter_mask))
Scott Feldman57b61082010-05-17 22:49:55 -07001783 goto nla_put_failure;
1784
Brenden Blancod1fdd912016-07-19 12:16:49 -07001785 if (rtnl_xdp_fill(skb, dev))
1786 goto nla_put_failure;
1787
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001788 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07001789 if (rtnl_link_fill(skb, dev) < 0)
1790 goto nla_put_failure;
1791 }
1792
Guillaume Naultd4e4fdf2019-10-23 18:39:04 +02001793 if (rtnl_fill_link_netnsid(skb, dev, src_net, gfp))
Florian Westphalb1e66b92017-09-26 13:58:42 +02001794 goto nla_put_failure;
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001795
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02001796 if (new_nsid &&
1797 nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0)
1798 goto nla_put_failure;
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01001799 if (new_ifindex &&
1800 nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0)
1801 goto nla_put_failure;
1802
Michal Kubecekf74877a2019-12-11 10:58:14 +01001803 if (memchr_inv(dev->perm_addr, '\0', dev->addr_len) &&
1804 nla_put(skb, IFLA_PERM_ADDRESS, dev->addr_len, dev->perm_addr))
1805 goto nla_put_failure;
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02001806
Florian Westphal5fa85a02017-10-16 15:44:36 +02001807 rcu_read_lock();
Florian Westphal070cbf5b2017-10-16 15:44:35 +02001808 if (rtnl_fill_link_af(skb, dev, ext_filter_mask))
Florian Westphal5fa85a02017-10-16 15:44:36 +02001809 goto nla_put_failure_rcu;
1810 rcu_read_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +00001811
Jiri Pirko88f4fb02019-09-30 11:48:17 +02001812 if (rtnl_fill_prop_list(skb, dev))
1813 goto nla_put_failure;
1814
Johannes Berg00e77ed2021-06-12 10:20:55 +02001815 if (dev->dev.parent &&
1816 nla_put_string(skb, IFLA_PARENT_DEV_NAME,
1817 dev_name(dev->dev.parent)))
1818 goto nla_put_failure;
1819
1820 if (dev->dev.parent && dev->dev.parent->bus &&
1821 nla_put_string(skb, IFLA_PARENT_DEV_BUS_NAME,
1822 dev->dev.parent->bus->name))
1823 goto nla_put_failure;
1824
Johannes Berg053c0952015-01-16 22:09:00 +01001825 nlmsg_end(skb, nlh);
1826 return 0;
Thomas Grafb60c5112006-08-04 23:05:34 -07001827
Florian Westphal5fa85a02017-10-16 15:44:36 +02001828nla_put_failure_rcu:
1829 rcu_read_unlock();
Thomas Grafb60c5112006-08-04 23:05:34 -07001830nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001831 nlmsg_cancel(skb, nlh);
1832 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833}
1834
Jiri Pirkof7b12602014-02-18 20:53:18 +01001835static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1836 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1837 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1838 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1839 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1840 [IFLA_MTU] = { .type = NLA_U32 },
1841 [IFLA_LINK] = { .type = NLA_U32 },
1842 [IFLA_MASTER] = { .type = NLA_U32 },
1843 [IFLA_CARRIER] = { .type = NLA_U8 },
1844 [IFLA_TXQLEN] = { .type = NLA_U32 },
1845 [IFLA_WEIGHT] = { .type = NLA_U32 },
1846 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1847 [IFLA_LINKMODE] = { .type = NLA_U8 },
1848 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1849 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1850 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
Nicolas Dichtel2459b4c2017-10-11 16:24:48 +02001851 /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
1852 * allow 0-length string (needed to remove an alias).
1853 */
1854 [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001855 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1856 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1857 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1858 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1859 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1860 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1861 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1862 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
Stephen Hemminger46e6b992017-12-07 15:40:19 -08001863 [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 },
1864 [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 },
Jiri Pirko02637fc2014-11-28 14:34:16 +01001865 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
david decotigny2d3b4792014-03-29 09:48:35 -07001866 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
Jiri Pirko82f28412014-11-28 14:34:18 +01001867 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
Nicolas Dichtel317f4812015-01-15 15:11:18 +01001868 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001869 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001870 [IFLA_XDP] = { .type = NLA_NESTED },
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001871 [IFLA_EVENT] = { .type = NLA_U32 },
Serhey Popovychdb833d42017-06-20 14:35:23 +03001872 [IFLA_GROUP] = { .type = NLA_U32 },
Christian Brauner7e4a8d52018-09-04 21:53:53 +02001873 [IFLA_TARGET_NETNSID] = { .type = NLA_S32 },
David Decotignyb2d3bcf2018-01-18 09:59:13 -08001874 [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 },
1875 [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 },
Stephen Hemminger3e7a50c2018-07-27 13:43:22 -07001876 [IFLA_MIN_MTU] = { .type = NLA_U32 },
1877 [IFLA_MAX_MTU] = { .type = NLA_U32 },
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02001878 [IFLA_PROP_LIST] = { .type = NLA_NESTED },
1879 [IFLA_ALT_IFNAME] = { .type = NLA_STRING,
1880 .len = ALTIFNAMSIZ - 1 },
Michal Kubecekf74877a2019-12-11 10:58:14 +01001881 [IFLA_PERM_ADDRESS] = { .type = NLA_REJECT },
Roopa Prabhu829eb202020-07-31 17:34:01 -07001882 [IFLA_PROTO_DOWN_REASON] = { .type = NLA_NESTED },
Andrei Vagin7e4a5132021-04-06 23:40:03 -07001883 [IFLA_NEW_IFINDEX] = NLA_POLICY_MIN(NLA_S32, 1),
Johannes Berg88b71052021-06-12 10:20:56 +02001884 [IFLA_PARENT_DEV_NAME] = { .type = NLA_NUL_STRING },
Coco Lieac1b932022-01-05 02:48:38 -08001885 [IFLA_GRO_MAX_SIZE] = { .type = NLA_U32 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001886};
1887
1888static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1889 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1890 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1891 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1892 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1893};
1894
Jiri Pirkof7b12602014-02-18 20:53:18 +01001895static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
Daniel Borkmann364d5712015-02-05 18:44:04 +01001896 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
Denis Kirjanov75345f882019-06-17 10:53:41 +02001897 [IFLA_VF_BROADCAST] = { .type = NLA_REJECT },
Daniel Borkmann364d5712015-02-05 18:44:04 +01001898 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
Moshe Shemesh79aab092016-09-22 12:11:15 +03001899 [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED },
Daniel Borkmann364d5712015-02-05 18:44:04 +01001900 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1901 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1902 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1903 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001904 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001905 [IFLA_VF_STATS] = { .type = NLA_NESTED },
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00001906 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
Eli Cohencc8e27c2016-03-11 22:58:34 +02001907 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1908 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001909};
1910
Jiri Pirkof7b12602014-02-18 20:53:18 +01001911static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1912 [IFLA_PORT_VF] = { .type = NLA_U32 },
1913 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1914 .len = PORT_PROFILE_MAX },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001915 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1916 .len = PORT_UUID_MAX },
1917 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1918 .len = PORT_UUID_MAX },
1919 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1920 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
Daniel Borkmann025331d2017-02-17 01:56:11 +01001921
1922 /* Unused, but we need to keep it here since user space could
1923 * fill it. It's also broken with regard to NLA_BINARY use in
1924 * combination with structs.
1925 */
1926 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1927 .len = sizeof(struct ifla_port_vsi) },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001928};
1929
Brenden Blancod1fdd912016-07-19 12:16:49 -07001930static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
Toke Høiland-Jørgensen92234c82020-03-25 18:23:26 +01001931 [IFLA_XDP_UNSPEC] = { .strict_start_type = IFLA_XDP_EXPECTED_FD },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001932 [IFLA_XDP_FD] = { .type = NLA_S32 },
Toke Høiland-Jørgensen92234c82020-03-25 18:23:26 +01001933 [IFLA_XDP_EXPECTED_FD] = { .type = NLA_S32 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001934 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 },
Daniel Borkmann85de8572016-11-28 23:16:54 +01001935 [IFLA_XDP_FLAGS] = { .type = NLA_U32 },
Martin KaFai Lau58038692017-06-15 17:29:09 -07001936 [IFLA_XDP_PROG_ID] = { .type = NLA_U32 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001937};
1938
David Aherndc599f72016-02-02 08:17:07 -08001939static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1940{
1941 const struct rtnl_link_ops *ops = NULL;
1942 struct nlattr *linfo[IFLA_INFO_MAX + 1];
1943
Johannes Berg8cb08172019-04-26 14:07:28 +02001944 if (nla_parse_nested_deprecated(linfo, IFLA_INFO_MAX, nla, ifla_info_policy, NULL) < 0)
David Aherndc599f72016-02-02 08:17:07 -08001945 return NULL;
1946
1947 if (linfo[IFLA_INFO_KIND]) {
1948 char kind[MODULE_NAME_LEN];
1949
Francis Laniel872f6902020-11-15 18:08:06 +01001950 nla_strscpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
David Aherndc599f72016-02-02 08:17:07 -08001951 ops = rtnl_link_ops_get(kind);
1952 }
1953
1954 return ops;
1955}
1956
1957static bool link_master_filtered(struct net_device *dev, int master_idx)
1958{
1959 struct net_device *master;
1960
1961 if (!master_idx)
1962 return false;
1963
1964 master = netdev_master_upper_dev_get(dev);
Lahav Schlesingerd3432bf2021-08-10 09:06:58 +00001965
1966 /* 0 is already used to denote IFLA_MASTER wasn't passed, therefore need
1967 * another invalid value for ifindex to denote "no master".
1968 */
1969 if (master_idx == -1)
1970 return !!master;
1971
David Aherndc599f72016-02-02 08:17:07 -08001972 if (!master || master->ifindex != master_idx)
1973 return true;
1974
1975 return false;
1976}
1977
1978static bool link_kind_filtered(const struct net_device *dev,
1979 const struct rtnl_link_ops *kind_ops)
1980{
1981 if (kind_ops && dev->rtnl_link_ops != kind_ops)
1982 return true;
1983
1984 return false;
1985}
1986
1987static bool link_dump_filtered(struct net_device *dev,
1988 int master_idx,
1989 const struct rtnl_link_ops *kind_ops)
1990{
1991 if (link_master_filtered(dev, master_idx) ||
1992 link_kind_filtered(dev, kind_ops))
1993 return true;
1994
1995 return false;
1996}
1997
Christian Braunerc383edc2018-09-04 21:53:47 +02001998/**
1999 * rtnl_get_net_ns_capable - Get netns if sufficiently privileged.
2000 * @sk: netlink socket
2001 * @netnsid: network namespace identifier
2002 *
2003 * Returns the network namespace identified by netnsid on success or an error
2004 * pointer on failure.
2005 */
2006struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid)
Jiri Benc79e1ad12017-11-02 17:04:38 -02002007{
2008 struct net *net;
2009
Andrei Vaginf428fe42018-01-02 23:27:33 -08002010 net = get_net_ns_by_id(sock_net(sk), netnsid);
Jiri Benc79e1ad12017-11-02 17:04:38 -02002011 if (!net)
2012 return ERR_PTR(-EINVAL);
2013
2014 /* For now, the caller is required to have CAP_NET_ADMIN in
2015 * the user namespace owning the target net ns.
2016 */
Andrei Vaginf428fe42018-01-02 23:27:33 -08002017 if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) {
Jiri Benc79e1ad12017-11-02 17:04:38 -02002018 put_net(net);
2019 return ERR_PTR(-EACCES);
2020 }
2021 return net;
2022}
Christian Braunerc383edc2018-09-04 21:53:47 +02002023EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable);
Jiri Benc79e1ad12017-11-02 17:04:38 -02002024
David Ahern905cf0a2018-10-07 20:16:30 -07002025static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh,
2026 bool strict_check, struct nlattr **tb,
2027 struct netlink_ext_ack *extack)
2028{
2029 int hdrlen;
2030
2031 if (strict_check) {
2032 struct ifinfomsg *ifm;
2033
2034 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
2035 NL_SET_ERR_MSG(extack, "Invalid header for link dump");
2036 return -EINVAL;
2037 }
2038
2039 ifm = nlmsg_data(nlh);
2040 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
2041 ifm->ifi_change) {
2042 NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request");
2043 return -EINVAL;
2044 }
2045 if (ifm->ifi_index) {
2046 NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps");
2047 return -EINVAL;
2048 }
2049
Johannes Berg8cb08172019-04-26 14:07:28 +02002050 return nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb,
2051 IFLA_MAX, ifla_policy,
2052 extack);
David Ahern905cf0a2018-10-07 20:16:30 -07002053 }
2054
2055 /* A hack to preserve kernel<->userspace interface.
2056 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
2057 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
2058 * what iproute2 < v3.9.0 used.
2059 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
2060 * attribute, its netlink message is shorter than struct ifinfomsg.
2061 */
2062 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2063 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2064
Johannes Berg8cb08172019-04-26 14:07:28 +02002065 return nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy,
2066 extack);
David Ahern905cf0a2018-10-07 20:16:30 -07002067}
2068
Thomas Grafb60c5112006-08-04 23:05:34 -07002069static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070{
David Ahern905cf0a2018-10-07 20:16:30 -07002071 struct netlink_ext_ack *extack = cb->extack;
2072 const struct nlmsghdr *nlh = cb->nlh;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002073 struct net *net = sock_net(skb->sk);
Jiri Benc79e1ad12017-11-02 17:04:38 -02002074 struct net *tgt_net = net;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002075 int h, s_h;
2076 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 struct net_device *dev;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002078 struct hlist_head *head;
Greg Rose115c9b82012-02-21 16:54:48 -05002079 struct nlattr *tb[IFLA_MAX+1];
2080 u32 ext_filter_mask = 0;
David Aherndc599f72016-02-02 08:17:07 -08002081 const struct rtnl_link_ops *kind_ops = NULL;
2082 unsigned int flags = NLM_F_MULTI;
2083 int master_idx = 0;
Jiri Benc79e1ad12017-11-02 17:04:38 -02002084 int netnsid = -1;
David Ahern905cf0a2018-10-07 20:16:30 -07002085 int err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002087 s_h = cb->args[0];
2088 s_idx = cb->args[1];
2089
David Ahern905cf0a2018-10-07 20:16:30 -07002090 err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack);
2091 if (err < 0) {
2092 if (cb->strict_check)
2093 return err;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02002094
David Ahern905cf0a2018-10-07 20:16:30 -07002095 goto walk_entries;
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00002096 }
Greg Rose115c9b82012-02-21 16:54:48 -05002097
David Ahern905cf0a2018-10-07 20:16:30 -07002098 for (i = 0; i <= IFLA_MAX; ++i) {
2099 if (!tb[i])
2100 continue;
2101
2102 /* new attributes should only be added with strict checking */
2103 switch (i) {
2104 case IFLA_TARGET_NETNSID:
2105 netnsid = nla_get_s32(tb[i]);
2106 tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid);
2107 if (IS_ERR(tgt_net)) {
2108 NL_SET_ERR_MSG(extack, "Invalid target network namespace id");
2109 return PTR_ERR(tgt_net);
2110 }
2111 break;
2112 case IFLA_EXT_MASK:
2113 ext_filter_mask = nla_get_u32(tb[i]);
2114 break;
2115 case IFLA_MASTER:
2116 master_idx = nla_get_u32(tb[i]);
2117 break;
2118 case IFLA_LINKINFO:
2119 kind_ops = linkinfo_to_kind_ops(tb[i]);
2120 break;
2121 default:
2122 if (cb->strict_check) {
2123 NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request");
2124 return -EINVAL;
2125 }
2126 }
2127 }
2128
2129 if (master_idx || kind_ops)
2130 flags |= NLM_F_DUMP_FILTERED;
2131
2132walk_entries:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002133 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
2134 idx = 0;
Jiri Benc79e1ad12017-11-02 17:04:38 -02002135 head = &tgt_net->dev_index_head[h];
Eric Dumazetcac5e652015-02-27 09:42:50 -08002136 hlist_for_each_entry(dev, head, index_hlist) {
David Aherndc599f72016-02-02 08:17:07 -08002137 if (link_dump_filtered(dev, master_idx, kind_ops))
Zhang Shengju3f0ae052016-11-19 23:28:32 +08002138 goto cont;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002139 if (idx < s_idx)
2140 goto cont;
Jiri Benc79e1ad12017-11-02 17:04:38 -02002141 err = rtnl_fill_ifinfo(skb, dev, net,
2142 RTM_NEWLINK,
David Gibson973462b2014-04-24 10:22:35 +10002143 NETLINK_CB(cb->skb).portid,
David Ahern905cf0a2018-10-07 20:16:30 -07002144 nlh->nlmsg_seq, 0, flags,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01002145 ext_filter_mask, 0, NULL, 0,
Guillaume Naultd4e4fdf2019-10-23 18:39:04 +02002146 netnsid, GFP_KERNEL);
David Gibson973462b2014-04-24 10:22:35 +10002147
David Ahernf6c57752017-05-15 23:19:17 -07002148 if (err < 0) {
2149 if (likely(skb->len))
2150 goto out;
2151
2152 goto out_err;
2153 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07002154cont:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002155 idx++;
2156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 }
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002158out:
David Ahernf6c57752017-05-15 23:19:17 -07002159 err = skb->len;
2160out_err:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002161 cb->args[1] = idx;
2162 cb->args[0] = h;
zhang kaia9ecb0c2021-03-02 18:16:07 +08002163 cb->seq = tgt_net->dev_base_seq;
Jakub Sitnickid0225782017-08-09 17:39:12 +02002164 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Jiri Benc79e1ad12017-11-02 17:04:38 -02002165 if (netnsid >= 0)
2166 put_net(tgt_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
David Ahernf6c57752017-05-15 23:19:17 -07002168 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169}
2170
Johannes Bergfceb6432017-04-12 14:34:07 +02002171int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
2172 struct netlink_ext_ack *exterr)
Jiri Pirkof7b12602014-02-18 20:53:18 +01002173{
Johannes Berg8cb08172019-04-26 14:07:28 +02002174 return nla_parse_deprecated(tb, IFLA_MAX, head, len, ifla_policy,
2175 exterr);
Jiri Pirkof7b12602014-02-18 20:53:18 +01002176}
2177EXPORT_SYMBOL(rtnl_nla_parse_ifla);
Scott Feldman57b61082010-05-17 22:49:55 -07002178
Eric W. Biederman81adee42009-11-08 00:53:51 -08002179struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
2180{
2181 struct net *net;
2182 /* Examine the link attributes and figure out which
2183 * network namespace we are talking about.
2184 */
2185 if (tb[IFLA_NET_NS_PID])
2186 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
Eric W. Biedermanf0630522011-05-04 17:51:50 -07002187 else if (tb[IFLA_NET_NS_FD])
2188 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
Eric W. Biederman81adee42009-11-08 00:53:51 -08002189 else
2190 net = get_net(src_net);
2191 return net;
2192}
2193EXPORT_SYMBOL(rtnl_link_get_net);
2194
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002195/* Figure out which network namespace we are talking about by
2196 * examining the link attributes in the following order:
2197 *
2198 * 1. IFLA_NET_NS_PID
2199 * 2. IFLA_NET_NS_FD
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002200 * 3. IFLA_TARGET_NETNSID
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002201 */
2202static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net,
2203 struct nlattr *tb[])
2204{
2205 struct net *net;
2206
2207 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])
2208 return rtnl_link_get_net(src_net, tb);
2209
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002210 if (!tb[IFLA_TARGET_NETNSID])
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002211 return get_net(src_net);
2212
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002213 net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID]));
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002214 if (!net)
2215 return ERR_PTR(-EINVAL);
2216
2217 return net;
2218}
2219
2220static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb,
2221 struct net *src_net,
2222 struct nlattr *tb[], int cap)
2223{
2224 struct net *net;
2225
2226 net = rtnl_link_get_net_by_nlattr(src_net, tb);
2227 if (IS_ERR(net))
2228 return net;
2229
2230 if (!netlink_ns_capable(skb, net->user_ns, cap)) {
2231 put_net(net);
2232 return ERR_PTR(-EPERM);
2233 }
2234
2235 return net;
2236}
2237
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002238/* Verify that rtnetlink requests do not pass additional properties
2239 * potentially referring to different network namespaces.
2240 */
2241static int rtnl_ensure_unique_netns(struct nlattr *tb[],
2242 struct netlink_ext_ack *extack,
2243 bool netns_id_only)
2244{
2245
2246 if (netns_id_only) {
2247 if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD])
2248 return 0;
2249
2250 NL_SET_ERR_MSG(extack, "specified netns attribute not supported");
2251 return -EOPNOTSUPP;
2252 }
2253
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002254 if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]))
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002255 goto invalid_attr;
2256
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002257 if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD]))
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002258 goto invalid_attr;
2259
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002260 if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID]))
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002261 goto invalid_attr;
2262
2263 return 0;
2264
2265invalid_attr:
2266 NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified");
2267 return -EINVAL;
2268}
2269
Rocco Yue8679c312021-08-03 20:02:50 +08002270static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[],
2271 struct netlink_ext_ack *extack)
Thomas Graf1840bb12008-02-23 19:54:36 -08002272{
2273 if (dev) {
2274 if (tb[IFLA_ADDRESS] &&
2275 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
2276 return -EINVAL;
2277
2278 if (tb[IFLA_BROADCAST] &&
2279 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
2280 return -EINVAL;
2281 }
2282
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002283 if (tb[IFLA_AF_SPEC]) {
2284 struct nlattr *af;
2285 int rem, err;
2286
2287 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2288 const struct rtnl_af_ops *af_ops;
2289
Florian Westphal5fa85a02017-10-16 15:44:36 +02002290 af_ops = rtnl_af_lookup(nla_type(af));
Cong Wanga1002432021-05-08 11:00:33 -07002291 if (!af_ops)
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002292 return -EAFNOSUPPORT;
2293
Cong Wanga1002432021-05-08 11:00:33 -07002294 if (!af_ops->set_link_af)
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002295 return -EOPNOTSUPP;
2296
2297 if (af_ops->validate_link_af) {
Rocco Yue8679c312021-08-03 20:02:50 +08002298 err = af_ops->validate_link_af(dev, af, extack);
Cong Wanga1002432021-05-08 11:00:33 -07002299 if (err < 0)
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002300 return err;
2301 }
2302 }
2303 }
2304
Coco Lieac1b932022-01-05 02:48:38 -08002305 if (tb[IFLA_GRO_MAX_SIZE]) {
2306 u32 gro_max_size = nla_get_u32(tb[IFLA_GRO_MAX_SIZE]);
2307
2308 if (gro_max_size > GRO_MAX_SIZE) {
2309 NL_SET_ERR_MSG(extack, "too big gro_max_size");
2310 return -EINVAL;
2311 }
2312 }
Thomas Graf1840bb12008-02-23 19:54:36 -08002313 return 0;
2314}
2315
Eli Cohencc8e27c2016-03-11 22:58:34 +02002316static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
2317 int guid_type)
2318{
2319 const struct net_device_ops *ops = dev->netdev_ops;
2320
2321 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
2322}
2323
2324static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
2325{
2326 if (dev->type != ARPHRD_INFINIBAND)
2327 return -EOPNOTSUPP;
2328
2329 return handle_infiniband_guid(dev, ivt, guid_type);
2330}
2331
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002332static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
Chris Wrightc02db8c2010-05-16 01:05:45 -07002333{
Chris Wrightc02db8c2010-05-16 01:05:45 -07002334 const struct net_device_ops *ops = dev->netdev_ops;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002335 int err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002336
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002337 if (tb[IFLA_VF_MAC]) {
2338 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03002339
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002340 if (ivm->vf >= INT_MAX)
2341 return -EINVAL;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002342 err = -EOPNOTSUPP;
2343 if (ops->ndo_set_vf_mac)
2344 err = ops->ndo_set_vf_mac(dev, ivm->vf,
2345 ivm->mac);
2346 if (err < 0)
2347 return err;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002348 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002349
2350 if (tb[IFLA_VF_VLAN]) {
2351 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
2352
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002353 if (ivv->vf >= INT_MAX)
2354 return -EINVAL;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002355 err = -EOPNOTSUPP;
2356 if (ops->ndo_set_vf_vlan)
2357 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
Moshe Shemesh79aab092016-09-22 12:11:15 +03002358 ivv->qos,
2359 htons(ETH_P_8021Q));
2360 if (err < 0)
2361 return err;
2362 }
2363
2364 if (tb[IFLA_VF_VLAN_LIST]) {
2365 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
2366 struct nlattr *attr;
2367 int rem, len = 0;
2368
2369 err = -EOPNOTSUPP;
2370 if (!ops->ndo_set_vf_vlan)
2371 return err;
2372
2373 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
2374 if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
2375 nla_len(attr) < NLA_HDRLEN) {
2376 return -EINVAL;
2377 }
2378 if (len >= MAX_VLAN_LIST_LEN)
2379 return -EOPNOTSUPP;
2380 ivvl[len] = nla_data(attr);
2381
2382 len++;
2383 }
Arnd Bergmannfa34cd92016-09-30 18:13:49 +02002384 if (len == 0)
2385 return -EINVAL;
2386
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002387 if (ivvl[0]->vf >= INT_MAX)
2388 return -EINVAL;
Moshe Shemesh79aab092016-09-22 12:11:15 +03002389 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
2390 ivvl[0]->qos, ivvl[0]->vlan_proto);
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002391 if (err < 0)
2392 return err;
2393 }
2394
2395 if (tb[IFLA_VF_TX_RATE]) {
2396 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
2397 struct ifla_vf_info ivf;
2398
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002399 if (ivt->vf >= INT_MAX)
2400 return -EINVAL;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002401 err = -EOPNOTSUPP;
2402 if (ops->ndo_get_vf_config)
2403 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
2404 if (err < 0)
2405 return err;
2406
2407 err = -EOPNOTSUPP;
2408 if (ops->ndo_set_vf_rate)
2409 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2410 ivf.min_tx_rate,
2411 ivt->rate);
2412 if (err < 0)
2413 return err;
2414 }
2415
2416 if (tb[IFLA_VF_RATE]) {
2417 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
2418
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002419 if (ivt->vf >= INT_MAX)
2420 return -EINVAL;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002421 err = -EOPNOTSUPP;
2422 if (ops->ndo_set_vf_rate)
2423 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2424 ivt->min_tx_rate,
2425 ivt->max_tx_rate);
2426 if (err < 0)
2427 return err;
2428 }
2429
2430 if (tb[IFLA_VF_SPOOFCHK]) {
2431 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
2432
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002433 if (ivs->vf >= INT_MAX)
2434 return -EINVAL;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002435 err = -EOPNOTSUPP;
2436 if (ops->ndo_set_vf_spoofchk)
2437 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
2438 ivs->setting);
2439 if (err < 0)
2440 return err;
2441 }
2442
2443 if (tb[IFLA_VF_LINK_STATE]) {
2444 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
2445
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002446 if (ivl->vf >= INT_MAX)
2447 return -EINVAL;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002448 err = -EOPNOTSUPP;
2449 if (ops->ndo_set_vf_link_state)
2450 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
2451 ivl->link_state);
2452 if (err < 0)
2453 return err;
2454 }
2455
2456 if (tb[IFLA_VF_RSS_QUERY_EN]) {
2457 struct ifla_vf_rss_query_en *ivrssq_en;
2458
2459 err = -EOPNOTSUPP;
2460 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002461 if (ivrssq_en->vf >= INT_MAX)
2462 return -EINVAL;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002463 if (ops->ndo_set_vf_rss_query_en)
2464 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
2465 ivrssq_en->setting);
2466 if (err < 0)
2467 return err;
2468 }
2469
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00002470 if (tb[IFLA_VF_TRUST]) {
2471 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
2472
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002473 if (ivt->vf >= INT_MAX)
2474 return -EINVAL;
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00002475 err = -EOPNOTSUPP;
2476 if (ops->ndo_set_vf_trust)
2477 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
2478 if (err < 0)
2479 return err;
2480 }
2481
Eli Cohencc8e27c2016-03-11 22:58:34 +02002482 if (tb[IFLA_VF_IB_NODE_GUID]) {
2483 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
2484
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002485 if (ivt->vf >= INT_MAX)
2486 return -EINVAL;
Eli Cohencc8e27c2016-03-11 22:58:34 +02002487 if (!ops->ndo_set_vf_guid)
2488 return -EOPNOTSUPP;
Eli Cohencc8e27c2016-03-11 22:58:34 +02002489 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
2490 }
2491
2492 if (tb[IFLA_VF_IB_PORT_GUID]) {
2493 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
2494
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002495 if (ivt->vf >= INT_MAX)
2496 return -EINVAL;
Eli Cohencc8e27c2016-03-11 22:58:34 +02002497 if (!ops->ndo_set_vf_guid)
2498 return -EOPNOTSUPP;
2499
2500 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
2501 }
2502
Chris Wrightc02db8c2010-05-16 01:05:45 -07002503 return err;
2504}
2505
David Ahern33eaf2a2017-10-04 17:48:46 -07002506static int do_set_master(struct net_device *dev, int ifindex,
2507 struct netlink_ext_ack *extack)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002508{
Jiri Pirko898e5062013-01-03 22:48:52 +00002509 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002510 const struct net_device_ops *ops;
2511 int err;
2512
Jiri Pirko898e5062013-01-03 22:48:52 +00002513 if (upper_dev) {
2514 if (upper_dev->ifindex == ifindex)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002515 return 0;
Jiri Pirko898e5062013-01-03 22:48:52 +00002516 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002517 if (ops->ndo_del_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002518 err = ops->ndo_del_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002519 if (err)
2520 return err;
2521 } else {
2522 return -EOPNOTSUPP;
2523 }
2524 }
2525
2526 if (ifindex) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002527 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
2528 if (!upper_dev)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002529 return -EINVAL;
Jiri Pirko898e5062013-01-03 22:48:52 +00002530 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002531 if (ops->ndo_add_slave) {
David Ahern33eaf2a2017-10-04 17:48:46 -07002532 err = ops->ndo_add_slave(upper_dev, dev, extack);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002533 if (err)
2534 return err;
2535 } else {
2536 return -EOPNOTSUPP;
2537 }
2538 }
2539 return 0;
2540}
2541
Roopa Prabhu829eb202020-07-31 17:34:01 -07002542static const struct nla_policy ifla_proto_down_reason_policy[IFLA_PROTO_DOWN_REASON_VALUE + 1] = {
2543 [IFLA_PROTO_DOWN_REASON_MASK] = { .type = NLA_U32 },
2544 [IFLA_PROTO_DOWN_REASON_VALUE] = { .type = NLA_U32 },
2545};
2546
2547static int do_set_proto_down(struct net_device *dev,
2548 struct nlattr *nl_proto_down,
2549 struct nlattr *nl_proto_down_reason,
2550 struct netlink_ext_ack *extack)
2551{
2552 struct nlattr *pdreason[IFLA_PROTO_DOWN_REASON_MAX + 1];
Roopa Prabhu829eb202020-07-31 17:34:01 -07002553 unsigned long mask = 0;
2554 u32 value;
2555 bool proto_down;
2556 int err;
2557
Jakub Kicinski2106efd2021-11-22 17:24:47 -08002558 if (!(dev->priv_flags & IFF_CHANGE_PROTO_DOWN)) {
Roopa Prabhu829eb202020-07-31 17:34:01 -07002559 NL_SET_ERR_MSG(extack, "Protodown not supported by device");
2560 return -EOPNOTSUPP;
2561 }
2562
2563 if (nl_proto_down_reason) {
2564 err = nla_parse_nested_deprecated(pdreason,
2565 IFLA_PROTO_DOWN_REASON_MAX,
2566 nl_proto_down_reason,
2567 ifla_proto_down_reason_policy,
2568 NULL);
2569 if (err < 0)
2570 return err;
2571
2572 if (!pdreason[IFLA_PROTO_DOWN_REASON_VALUE]) {
2573 NL_SET_ERR_MSG(extack, "Invalid protodown reason value");
2574 return -EINVAL;
2575 }
2576
2577 value = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_VALUE]);
2578
2579 if (pdreason[IFLA_PROTO_DOWN_REASON_MASK])
2580 mask = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_MASK]);
2581
2582 dev_change_proto_down_reason(dev, mask, value);
2583 }
2584
2585 if (nl_proto_down) {
2586 proto_down = nla_get_u8(nl_proto_down);
2587
Zheng Yongjund467d0b2021-06-02 14:56:23 +08002588 /* Don't turn off protodown if there are active reasons */
Roopa Prabhu829eb202020-07-31 17:34:01 -07002589 if (!proto_down && dev->proto_down_reason) {
2590 NL_SET_ERR_MSG(extack, "Cannot clear protodown, active reasons");
2591 return -EBUSY;
2592 }
2593 err = dev_change_proto_down(dev,
2594 proto_down);
2595 if (err)
2596 return err;
2597 }
2598
2599 return 0;
2600}
2601
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002602#define DO_SETLINK_MODIFIED 0x01
Nicolas Dichtelba998902014-09-01 16:07:29 +02002603/* notify flag means notify + modified. */
2604#define DO_SETLINK_NOTIFY 0x03
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002605static int do_setlink(const struct sk_buff *skb,
2606 struct net_device *dev, struct ifinfomsg *ifm,
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002607 struct netlink_ext_ack *extack,
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002608 struct nlattr **tb, char *ifname, int status)
Patrick McHardy0157f602007-06-13 12:03:36 -07002609{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002610 const struct net_device_ops *ops = dev->netdev_ops;
Patrick McHardy0157f602007-06-13 12:03:36 -07002611 int err;
2612
Rocco Yue8679c312021-08-03 20:02:50 +08002613 err = validate_linkmsg(dev, tb, extack);
Eric Dumazet644c7ee2018-06-05 09:25:19 -07002614 if (err < 0)
2615 return err;
2616
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002617 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) {
Andrey Ignatov96a6b932021-08-25 17:25:40 -07002618 const char *pat = ifname && ifname[0] ? ifname : NULL;
Andrei Vagineeb85a12021-04-05 00:12:23 -07002619 struct net *net;
2620 int new_ifindex;
2621
2622 net = rtnl_link_get_net_capable(skb, dev_net(dev),
2623 tb, CAP_NET_ADMIN);
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002624 if (IS_ERR(net)) {
2625 err = PTR_ERR(net);
2626 goto errout;
2627 }
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002628
Andrei Vagineeb85a12021-04-05 00:12:23 -07002629 if (tb[IFLA_NEW_IFINDEX])
2630 new_ifindex = nla_get_s32(tb[IFLA_NEW_IFINDEX]);
2631 else
2632 new_ifindex = 0;
2633
Andrey Ignatov96a6b932021-08-25 17:25:40 -07002634 err = __dev_change_net_namespace(dev, net, pat, new_ifindex);
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002635 put_net(net);
2636 if (err)
2637 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002638 status |= DO_SETLINK_MODIFIED;
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002639 }
2640
Patrick McHardy0157f602007-06-13 12:03:36 -07002641 if (tb[IFLA_MAP]) {
2642 struct rtnl_link_ifmap *u_map;
2643 struct ifmap k_map;
2644
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002645 if (!ops->ndo_set_config) {
Patrick McHardy0157f602007-06-13 12:03:36 -07002646 err = -EOPNOTSUPP;
2647 goto errout;
2648 }
2649
2650 if (!netif_device_present(dev)) {
2651 err = -ENODEV;
2652 goto errout;
2653 }
2654
2655 u_map = nla_data(tb[IFLA_MAP]);
2656 k_map.mem_start = (unsigned long) u_map->mem_start;
2657 k_map.mem_end = (unsigned long) u_map->mem_end;
2658 k_map.base_addr = (unsigned short) u_map->base_addr;
2659 k_map.irq = (unsigned char) u_map->irq;
2660 k_map.dma = (unsigned char) u_map->dma;
2661 k_map.port = (unsigned char) u_map->port;
2662
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002663 err = ops->ndo_set_config(dev, &k_map);
Patrick McHardy0157f602007-06-13 12:03:36 -07002664 if (err < 0)
2665 goto errout;
2666
Nicolas Dichtelba998902014-09-01 16:07:29 +02002667 status |= DO_SETLINK_NOTIFY;
Patrick McHardy0157f602007-06-13 12:03:36 -07002668 }
2669
2670 if (tb[IFLA_ADDRESS]) {
2671 struct sockaddr *sa;
2672 int len;
2673
WANG Cong153711f2017-07-20 11:27:57 -07002674 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2675 sizeof(*sa));
Patrick McHardy0157f602007-06-13 12:03:36 -07002676 sa = kmalloc(len, GFP_KERNEL);
2677 if (!sa) {
2678 err = -ENOMEM;
2679 goto errout;
2680 }
2681 sa->sa_family = dev->type;
2682 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
2683 dev->addr_len);
Cong Wang3b23a322021-02-11 11:34:10 -08002684 err = dev_set_mac_address_user(dev, sa, extack);
Patrick McHardy0157f602007-06-13 12:03:36 -07002685 kfree(sa);
2686 if (err)
2687 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002688 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002689 }
2690
2691 if (tb[IFLA_MTU]) {
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07002692 err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack);
Patrick McHardy0157f602007-06-13 12:03:36 -07002693 if (err < 0)
2694 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002695 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002696 }
2697
Vlad Dogarucbda10f2011-01-13 23:38:30 +00002698 if (tb[IFLA_GROUP]) {
2699 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Nicolas Dichtelba998902014-09-01 16:07:29 +02002700 status |= DO_SETLINK_NOTIFY;
Vlad Dogarucbda10f2011-01-13 23:38:30 +00002701 }
2702
Patrick McHardy0157f602007-06-13 12:03:36 -07002703 /*
2704 * Interface selected by interface index but interface
2705 * name provided implies that a name change has been
2706 * requested.
2707 */
2708 if (ifm->ifi_index > 0 && ifname[0]) {
2709 err = dev_change_name(dev, ifname);
2710 if (err < 0)
2711 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002712 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002713 }
2714
Stephen Hemminger0b815a12008-09-22 21:28:11 -07002715 if (tb[IFLA_IFALIAS]) {
2716 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2717 nla_len(tb[IFLA_IFALIAS]));
2718 if (err < 0)
2719 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002720 status |= DO_SETLINK_NOTIFY;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07002721 }
2722
Patrick McHardy0157f602007-06-13 12:03:36 -07002723 if (tb[IFLA_BROADCAST]) {
2724 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00002725 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Patrick McHardy0157f602007-06-13 12:03:36 -07002726 }
2727
2728 if (ifm->ifi_flags || ifm->ifi_change) {
Petr Machata567c5e12018-12-06 17:05:42 +00002729 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
2730 extack);
Johannes Berg5f9021c2008-11-16 23:20:31 -08002731 if (err < 0)
2732 goto errout;
Patrick McHardy0157f602007-06-13 12:03:36 -07002733 }
2734
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002735 if (tb[IFLA_MASTER]) {
David Ahern33eaf2a2017-10-04 17:48:46 -07002736 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002737 if (err)
2738 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002739 status |= DO_SETLINK_MODIFIED;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002740 }
2741
Jiri Pirko9a572472012-12-27 23:49:39 +00002742 if (tb[IFLA_CARRIER]) {
2743 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2744 if (err)
2745 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002746 status |= DO_SETLINK_MODIFIED;
Jiri Pirko9a572472012-12-27 23:49:39 +00002747 }
2748
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002749 if (tb[IFLA_TXQLEN]) {
Alexey Dobriyan0cd29502017-05-17 13:30:44 +03002750 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002751
Cong Wang6a643dd2018-01-25 18:26:22 -08002752 err = dev_change_tx_queue_len(dev, value);
2753 if (err)
2754 goto errout;
2755 status |= DO_SETLINK_MODIFIED;
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002756 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002757
Stephen Hemminger46e6b992017-12-07 15:40:19 -08002758 if (tb[IFLA_GSO_MAX_SIZE]) {
2759 u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]);
2760
2761 if (max_size > GSO_MAX_SIZE) {
2762 err = -EINVAL;
2763 goto errout;
2764 }
2765
2766 if (dev->gso_max_size ^ max_size) {
2767 netif_set_gso_max_size(dev, max_size);
2768 status |= DO_SETLINK_MODIFIED;
2769 }
2770 }
2771
2772 if (tb[IFLA_GSO_MAX_SEGS]) {
2773 u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
2774
2775 if (max_segs > GSO_MAX_SEGS) {
2776 err = -EINVAL;
2777 goto errout;
2778 }
2779
2780 if (dev->gso_max_segs ^ max_segs) {
Eric Dumazet6d872df2021-11-19 07:43:32 -08002781 netif_set_gso_max_segs(dev, max_segs);
Stephen Hemminger46e6b992017-12-07 15:40:19 -08002782 status |= DO_SETLINK_MODIFIED;
2783 }
2784 }
2785
Coco Lieac1b932022-01-05 02:48:38 -08002786 if (tb[IFLA_GRO_MAX_SIZE]) {
2787 u32 gro_max_size = nla_get_u32(tb[IFLA_GRO_MAX_SIZE]);
2788
2789 if (dev->gro_max_size ^ gro_max_size) {
2790 netif_set_gro_max_size(dev, gro_max_size);
2791 status |= DO_SETLINK_MODIFIED;
2792 }
2793 }
2794
Patrick McHardy0157f602007-06-13 12:03:36 -07002795 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002796 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Patrick McHardy0157f602007-06-13 12:03:36 -07002797
2798 if (tb[IFLA_LINKMODE]) {
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002799 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2800
Sebastian Andrzej Siewiorfd888e82021-11-26 17:15:29 +01002801 write_lock(&dev_base_lock);
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002802 if (dev->link_mode ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02002803 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002804 dev->link_mode = value;
Sebastian Andrzej Siewiorfd888e82021-11-26 17:15:29 +01002805 write_unlock(&dev_base_lock);
Patrick McHardy0157f602007-06-13 12:03:36 -07002806 }
2807
Chris Wrightc02db8c2010-05-16 01:05:45 -07002808 if (tb[IFLA_VFINFO_LIST]) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002809 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
Chris Wrightc02db8c2010-05-16 01:05:45 -07002810 struct nlattr *attr;
2811 int rem;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002812
Chris Wrightc02db8c2010-05-16 01:05:45 -07002813 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002814 if (nla_type(attr) != IFLA_VF_INFO ||
2815 nla_len(attr) < NLA_HDRLEN) {
David Howells253683b2010-05-21 02:25:27 +00002816 err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002817 goto errout;
David Howells253683b2010-05-21 02:25:27 +00002818 }
Johannes Berg8cb08172019-04-26 14:07:28 +02002819 err = nla_parse_nested_deprecated(vfinfo, IFLA_VF_MAX,
2820 attr,
2821 ifla_vf_policy,
2822 NULL);
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002823 if (err < 0)
2824 goto errout;
2825 err = do_setvfinfo(dev, vfinfo);
Chris Wrightc02db8c2010-05-16 01:05:45 -07002826 if (err < 0)
2827 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002828 status |= DO_SETLINK_NOTIFY;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002829 }
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00002830 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002831 err = 0;
2832
Scott Feldman57b61082010-05-17 22:49:55 -07002833 if (tb[IFLA_VF_PORTS]) {
2834 struct nlattr *port[IFLA_PORT_MAX+1];
2835 struct nlattr *attr;
2836 int vf;
2837 int rem;
2838
2839 err = -EOPNOTSUPP;
2840 if (!ops->ndo_set_vf_port)
2841 goto errout;
2842
2843 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
Daniel Borkmann035d2102015-07-13 00:06:02 +02002844 if (nla_type(attr) != IFLA_VF_PORT ||
2845 nla_len(attr) < NLA_HDRLEN) {
2846 err = -EINVAL;
2847 goto errout;
2848 }
Johannes Berg8cb08172019-04-26 14:07:28 +02002849 err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
2850 attr,
2851 ifla_port_policy,
2852 NULL);
Scott Feldman57b61082010-05-17 22:49:55 -07002853 if (err < 0)
2854 goto errout;
2855 if (!port[IFLA_PORT_VF]) {
2856 err = -EOPNOTSUPP;
2857 goto errout;
2858 }
2859 vf = nla_get_u32(port[IFLA_PORT_VF]);
2860 err = ops->ndo_set_vf_port(dev, vf, port);
2861 if (err < 0)
2862 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002863 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07002864 }
2865 }
2866 err = 0;
2867
2868 if (tb[IFLA_PORT_SELF]) {
2869 struct nlattr *port[IFLA_PORT_MAX+1];
2870
Johannes Berg8cb08172019-04-26 14:07:28 +02002871 err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
2872 tb[IFLA_PORT_SELF],
2873 ifla_port_policy, NULL);
Scott Feldman57b61082010-05-17 22:49:55 -07002874 if (err < 0)
2875 goto errout;
2876
2877 err = -EOPNOTSUPP;
2878 if (ops->ndo_set_vf_port)
2879 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
2880 if (err < 0)
2881 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002882 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07002883 }
Thomas Graff8ff1822010-11-16 04:30:14 +00002884
2885 if (tb[IFLA_AF_SPEC]) {
2886 struct nlattr *af;
2887 int rem;
2888
2889 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2890 const struct rtnl_af_ops *af_ops;
2891
Gustavo A. R. Silva058c8d52017-10-20 19:43:11 -05002892 BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
Thomas Graff8ff1822010-11-16 04:30:14 +00002893
Stephen Hemminger3583a4e2021-04-07 08:59:12 -07002894 err = af_ops->set_link_af(dev, af, extack);
Cong Wanga1002432021-05-08 11:00:33 -07002895 if (err < 0)
Thomas Graff8ff1822010-11-16 04:30:14 +00002896 goto errout;
2897
Nicolas Dichtelba998902014-09-01 16:07:29 +02002898 status |= DO_SETLINK_NOTIFY;
Thomas Graff8ff1822010-11-16 04:30:14 +00002899 }
2900 }
Scott Feldman57b61082010-05-17 22:49:55 -07002901 err = 0;
2902
Roopa Prabhu829eb202020-07-31 17:34:01 -07002903 if (tb[IFLA_PROTO_DOWN] || tb[IFLA_PROTO_DOWN_REASON]) {
2904 err = do_set_proto_down(dev, tb[IFLA_PROTO_DOWN],
2905 tb[IFLA_PROTO_DOWN_REASON], extack);
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07002906 if (err)
2907 goto errout;
2908 status |= DO_SETLINK_NOTIFY;
2909 }
2910
Brenden Blancod1fdd912016-07-19 12:16:49 -07002911 if (tb[IFLA_XDP]) {
2912 struct nlattr *xdp[IFLA_XDP_MAX + 1];
Daniel Borkmann85de8572016-11-28 23:16:54 +01002913 u32 xdp_flags = 0;
Brenden Blancod1fdd912016-07-19 12:16:49 -07002914
Johannes Berg8cb08172019-04-26 14:07:28 +02002915 err = nla_parse_nested_deprecated(xdp, IFLA_XDP_MAX,
2916 tb[IFLA_XDP],
2917 ifla_xdp_policy, NULL);
Brenden Blancod1fdd912016-07-19 12:16:49 -07002918 if (err < 0)
2919 goto errout;
2920
Martin KaFai Lau58038692017-06-15 17:29:09 -07002921 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
Brenden Blanco262d8622016-07-20 17:22:34 -07002922 err = -EINVAL;
2923 goto errout;
2924 }
Daniel Borkmann85de8572016-11-28 23:16:54 +01002925
2926 if (xdp[IFLA_XDP_FLAGS]) {
2927 xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
2928 if (xdp_flags & ~XDP_FLAGS_MASK) {
2929 err = -EINVAL;
2930 goto errout;
2931 }
Jakub Kicinskiee5d0322017-06-21 18:25:04 -07002932 if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
Daniel Borkmann0489df92017-05-12 01:04:45 +02002933 err = -EINVAL;
2934 goto errout;
2935 }
Daniel Borkmann85de8572016-11-28 23:16:54 +01002936 }
2937
Brenden Blancod1fdd912016-07-19 12:16:49 -07002938 if (xdp[IFLA_XDP_FD]) {
Toke Høiland-Jørgensen92234c82020-03-25 18:23:26 +01002939 int expected_fd = -1;
2940
2941 if (xdp_flags & XDP_FLAGS_REPLACE) {
2942 if (!xdp[IFLA_XDP_EXPECTED_FD]) {
2943 err = -EINVAL;
2944 goto errout;
2945 }
2946 expected_fd =
2947 nla_get_s32(xdp[IFLA_XDP_EXPECTED_FD]);
2948 }
2949
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002950 err = dev_change_xdp_fd(dev, extack,
Daniel Borkmann85de8572016-11-28 23:16:54 +01002951 nla_get_s32(xdp[IFLA_XDP_FD]),
Toke Høiland-Jørgensen92234c82020-03-25 18:23:26 +01002952 expected_fd,
Daniel Borkmann85de8572016-11-28 23:16:54 +01002953 xdp_flags);
Brenden Blancod1fdd912016-07-19 12:16:49 -07002954 if (err)
2955 goto errout;
2956 status |= DO_SETLINK_NOTIFY;
2957 }
2958 }
2959
Patrick McHardy0157f602007-06-13 12:03:36 -07002960errout:
Nicolas Dichtelba998902014-09-01 16:07:29 +02002961 if (status & DO_SETLINK_MODIFIED) {
Xin Long64ff90c2017-10-15 18:13:45 +08002962 if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
Nicolas Dichtelba998902014-09-01 16:07:29 +02002963 netdev_state_change(dev);
2964
2965 if (err < 0)
2966 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",
2967 dev->name);
2968 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002969
Patrick McHardy0157f602007-06-13 12:03:36 -07002970 return err;
2971}
2972
Jiri Pirkocc6090e2019-09-30 11:48:19 +02002973static struct net_device *rtnl_dev_get(struct net *net,
2974 struct nlattr *ifname_attr,
Jiri Pirko76c9ac02019-09-30 11:48:20 +02002975 struct nlattr *altifname_attr,
Jiri Pirkocc6090e2019-09-30 11:48:19 +02002976 char *ifname)
2977{
Jiri Pirko76c9ac02019-09-30 11:48:20 +02002978 char buffer[ALTIFNAMSIZ];
Jiri Pirkocc6090e2019-09-30 11:48:19 +02002979
2980 if (!ifname) {
2981 ifname = buffer;
2982 if (ifname_attr)
Francis Laniel872f6902020-11-15 18:08:06 +01002983 nla_strscpy(ifname, ifname_attr, IFNAMSIZ);
Jiri Pirko76c9ac02019-09-30 11:48:20 +02002984 else if (altifname_attr)
Francis Laniel872f6902020-11-15 18:08:06 +01002985 nla_strscpy(ifname, altifname_attr, ALTIFNAMSIZ);
Jiri Pirkocc6090e2019-09-30 11:48:19 +02002986 else
2987 return NULL;
2988 }
2989
2990 return __dev_get_by_name(net, ifname);
2991}
2992
David Ahernc21ef3e2017-04-16 09:48:24 -07002993static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2994 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002996 struct net *net = sock_net(skb->sk);
Thomas Grafda5e0492006-08-10 21:17:37 -07002997 struct ifinfomsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 struct net_device *dev;
Patrick McHardy0157f602007-06-13 12:03:36 -07002999 int err;
Thomas Grafda5e0492006-08-10 21:17:37 -07003000 struct nlattr *tb[IFLA_MAX+1];
3001 char ifname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
Johannes Berg8cb08172019-04-26 14:07:28 +02003003 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3004 ifla_policy, extack);
Thomas Grafda5e0492006-08-10 21:17:37 -07003005 if (err < 0)
3006 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007
Christian Brauner4ff66ca2018-02-07 13:53:20 +01003008 err = rtnl_ensure_unique_netns(tb, extack, false);
3009 if (err < 0)
3010 goto errout;
3011
Thomas Graf5176f912006-08-26 20:13:18 -07003012 if (tb[IFLA_IFNAME])
Francis Laniel872f6902020-11-15 18:08:06 +01003013 nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
Patrick McHardy78e5b8912006-09-13 20:35:36 -07003014 else
3015 ifname[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 err = -EINVAL;
Thomas Grafda5e0492006-08-10 21:17:37 -07003018 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -07003019 if (ifm->ifi_index > 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00003020 dev = __dev_get_by_index(net, ifm->ifi_index);
Jiri Pirko76c9ac02019-09-30 11:48:20 +02003021 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3022 dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname);
Thomas Grafda5e0492006-08-10 21:17:37 -07003023 else
3024 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025
Thomas Grafda5e0492006-08-10 21:17:37 -07003026 if (dev == NULL) {
3027 err = -ENODEV;
3028 goto errout;
3029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07003031 err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
Thomas Grafda5e0492006-08-10 21:17:37 -07003032errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 return err;
3034}
3035
WANG Cong66400d52015-03-24 11:53:31 -07003036static int rtnl_group_dellink(const struct net *net, int group)
3037{
3038 struct net_device *dev, *aux;
3039 LIST_HEAD(list_kill);
3040 bool found = false;
3041
3042 if (!group)
3043 return -EPERM;
3044
3045 for_each_netdev(net, dev) {
3046 if (dev->group == group) {
3047 const struct rtnl_link_ops *ops;
3048
3049 found = true;
3050 ops = dev->rtnl_link_ops;
3051 if (!ops || !ops->dellink)
3052 return -EOPNOTSUPP;
3053 }
3054 }
3055
3056 if (!found)
3057 return -ENODEV;
3058
3059 for_each_netdev_safe(net, dev, aux) {
3060 if (dev->group == group) {
3061 const struct rtnl_link_ops *ops;
3062
3063 ops = dev->rtnl_link_ops;
3064 ops->dellink(dev, &list_kill);
3065 }
3066 }
3067 unregister_netdevice_many(&list_kill);
3068
3069 return 0;
3070}
3071
Thomas Graf614732e2015-07-21 10:44:06 +02003072int rtnl_delete_link(struct net_device *dev)
3073{
3074 const struct rtnl_link_ops *ops;
3075 LIST_HEAD(list_kill);
3076
3077 ops = dev->rtnl_link_ops;
3078 if (!ops || !ops->dellink)
3079 return -EOPNOTSUPP;
3080
3081 ops->dellink(dev, &list_kill);
3082 unregister_netdevice_many(&list_kill);
3083
3084 return 0;
3085}
3086EXPORT_SYMBOL_GPL(rtnl_delete_link);
3087
David Ahernc21ef3e2017-04-16 09:48:24 -07003088static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
3089 struct netlink_ext_ack *extack)
Patrick McHardy38f7b872007-06-13 12:03:51 -07003090{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003091 struct net *net = sock_net(skb->sk);
Christian Braunerb61ad682018-01-24 15:26:34 +01003092 struct net *tgt_net = net;
3093 struct net_device *dev = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003094 struct ifinfomsg *ifm;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003095 struct nlattr *tb[IFLA_MAX+1];
3096 int err;
Christian Braunerb61ad682018-01-24 15:26:34 +01003097 int netnsid = -1;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003098
Johannes Berg8cb08172019-04-26 14:07:28 +02003099 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3100 ifla_policy, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07003101 if (err < 0)
3102 return err;
3103
Christian Brauner4ff66ca2018-02-07 13:53:20 +01003104 err = rtnl_ensure_unique_netns(tb, extack, true);
3105 if (err < 0)
3106 return err;
3107
Christian Brauner7e4a8d52018-09-04 21:53:53 +02003108 if (tb[IFLA_TARGET_NETNSID]) {
3109 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
Christian Braunerc383edc2018-09-04 21:53:47 +02003110 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
Christian Braunerb61ad682018-01-24 15:26:34 +01003111 if (IS_ERR(tgt_net))
3112 return PTR_ERR(tgt_net);
3113 }
3114
3115 err = -EINVAL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003116 ifm = nlmsg_data(nlh);
3117 if (ifm->ifi_index > 0)
Christian Braunerb61ad682018-01-24 15:26:34 +01003118 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
Jiri Pirko76c9ac02019-09-30 11:48:20 +02003119 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3120 dev = rtnl_dev_get(net, tb[IFLA_IFNAME],
3121 tb[IFLA_ALT_IFNAME], NULL);
WANG Cong66400d52015-03-24 11:53:31 -07003122 else if (tb[IFLA_GROUP])
Christian Braunerb61ad682018-01-24 15:26:34 +01003123 err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP]));
Patrick McHardy38f7b872007-06-13 12:03:51 -07003124 else
Christian Braunerb61ad682018-01-24 15:26:34 +01003125 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003126
Christian Braunerb61ad682018-01-24 15:26:34 +01003127 if (!dev) {
3128 if (tb[IFLA_IFNAME] || ifm->ifi_index > 0)
3129 err = -ENODEV;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003130
Christian Braunerb61ad682018-01-24 15:26:34 +01003131 goto out;
3132 }
3133
3134 err = rtnl_delete_link(dev);
3135
3136out:
3137 if (netnsid >= 0)
3138 put_net(tgt_net);
3139
3140 return err;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003141}
3142
Patrick McHardy3729d502010-02-26 06:34:54 +00003143int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
3144{
3145 unsigned int old_flags;
3146 int err;
3147
3148 old_flags = dev->flags;
3149 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
Petr Machata6d040322018-12-06 17:05:43 +00003150 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
3151 NULL);
Patrick McHardy3729d502010-02-26 06:34:54 +00003152 if (err < 0)
3153 return err;
3154 }
3155
Roopa Prabhu8d356b82018-07-04 16:46:29 -07003156 if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) {
Roopa Prabhu56a49d72018-09-12 13:21:48 -07003157 __dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags));
Roopa Prabhu8d356b82018-07-04 16:46:29 -07003158 } else {
3159 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
3160 __dev_notify_flags(dev, old_flags, ~0U);
3161 }
Patrick McHardy3729d502010-02-26 06:34:54 +00003162 return 0;
3163}
3164EXPORT_SYMBOL(rtnl_configure_link);
3165
David Ahernd0522f12018-11-06 12:51:14 -08003166struct net_device *rtnl_create_link(struct net *net, const char *ifname,
3167 unsigned char name_assign_type,
3168 const struct rtnl_link_ops *ops,
3169 struct nlattr *tb[],
3170 struct netlink_ext_ack *extack)
Pavel Emelianove7199282007-08-08 22:16:38 -07003171{
Pavel Emelianove7199282007-08-08 22:16:38 -07003172 struct net_device *dev;
Jiri Pirkod40156a2012-07-20 02:28:47 +00003173 unsigned int num_tx_queues = 1;
3174 unsigned int num_rx_queues = 1;
Pavel Emelianove7199282007-08-08 22:16:38 -07003175
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00003176 if (tb[IFLA_NUM_TX_QUEUES])
3177 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
3178 else if (ops->get_num_tx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00003179 num_tx_queues = ops->get_num_tx_queues();
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00003180
3181 if (tb[IFLA_NUM_RX_QUEUES])
3182 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
3183 else if (ops->get_num_rx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00003184 num_rx_queues = ops->get_num_rx_queues();
stephen hemmingerefacb302012-04-10 18:34:43 +00003185
David Ahernd0522f12018-11-06 12:51:14 -08003186 if (num_tx_queues < 1 || num_tx_queues > 4096) {
3187 NL_SET_ERR_MSG(extack, "Invalid number of transmit queues");
Eric Dumazet0e1d6ec2018-10-02 15:47:35 -07003188 return ERR_PTR(-EINVAL);
David Ahernd0522f12018-11-06 12:51:14 -08003189 }
Eric Dumazet0e1d6ec2018-10-02 15:47:35 -07003190
David Ahernd0522f12018-11-06 12:51:14 -08003191 if (num_rx_queues < 1 || num_rx_queues > 4096) {
3192 NL_SET_ERR_MSG(extack, "Invalid number of receive queues");
Eric Dumazet0e1d6ec2018-10-02 15:47:35 -07003193 return ERR_PTR(-EINVAL);
David Ahernd0522f12018-11-06 12:51:14 -08003194 }
Eric Dumazet0e1d6ec2018-10-02 15:47:35 -07003195
Johannes Berg8c713dc2021-06-12 10:20:54 +02003196 if (ops->alloc) {
3197 dev = ops->alloc(tb, ifname, name_assign_type,
3198 num_tx_queues, num_rx_queues);
3199 if (IS_ERR(dev))
3200 return dev;
3201 } else {
3202 dev = alloc_netdev_mqs(ops->priv_size, ifname,
3203 name_assign_type, ops->setup,
3204 num_tx_queues, num_rx_queues);
3205 }
3206
Pavel Emelianove7199282007-08-08 22:16:38 -07003207 if (!dev)
Tobias Klauserd1892e42017-02-20 16:32:06 +01003208 return ERR_PTR(-ENOMEM);
Pavel Emelianove7199282007-08-08 22:16:38 -07003209
Eric W. Biederman81adee42009-11-08 00:53:51 -08003210 dev_net_set(dev, net);
3211 dev->rtnl_link_ops = ops;
Patrick McHardy3729d502010-02-26 06:34:54 +00003212 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
Eric W. Biederman81adee42009-11-08 00:53:51 -08003213
Eric Dumazetd836f5c2020-01-21 22:47:29 -08003214 if (tb[IFLA_MTU]) {
3215 u32 mtu = nla_get_u32(tb[IFLA_MTU]);
3216 int err;
3217
3218 err = dev_validate_mtu(dev, mtu, extack);
3219 if (err) {
3220 free_netdev(dev);
3221 return ERR_PTR(err);
3222 }
3223 dev->mtu = mtu;
3224 }
Jiri Pirko2afb9b52013-01-06 12:41:57 +00003225 if (tb[IFLA_ADDRESS]) {
Jakub Kicinskiefd38f72021-10-22 16:20:57 -07003226 __dev_addr_set(dev, nla_data(tb[IFLA_ADDRESS]),
3227 nla_len(tb[IFLA_ADDRESS]));
Jiri Pirko2afb9b52013-01-06 12:41:57 +00003228 dev->addr_assign_type = NET_ADDR_SET;
3229 }
Pavel Emelianove7199282007-08-08 22:16:38 -07003230 if (tb[IFLA_BROADCAST])
3231 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
3232 nla_len(tb[IFLA_BROADCAST]));
3233 if (tb[IFLA_TXQLEN])
3234 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
3235 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08003236 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Pavel Emelianove7199282007-08-08 22:16:38 -07003237 if (tb[IFLA_LINKMODE])
3238 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
Patrick McHardyffa934f2011-01-20 03:00:42 +00003239 if (tb[IFLA_GROUP])
3240 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Stephen Hemminger46e6b992017-12-07 15:40:19 -08003241 if (tb[IFLA_GSO_MAX_SIZE])
3242 netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE]));
3243 if (tb[IFLA_GSO_MAX_SEGS])
Eric Dumazet6d872df2021-11-19 07:43:32 -08003244 netif_set_gso_max_segs(dev, nla_get_u32(tb[IFLA_GSO_MAX_SEGS]));
Coco Lieac1b932022-01-05 02:48:38 -08003245 if (tb[IFLA_GRO_MAX_SIZE])
3246 netif_set_gro_max_size(dev, nla_get_u32(tb[IFLA_GRO_MAX_SIZE]));
Pavel Emelianove7199282007-08-08 22:16:38 -07003247
3248 return dev;
Pavel Emelianove7199282007-08-08 22:16:38 -07003249}
Eric Dumazete0d087a2009-11-07 01:26:17 -08003250EXPORT_SYMBOL(rtnl_create_link);
Pavel Emelianove7199282007-08-08 22:16:38 -07003251
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07003252static int rtnl_group_changelink(const struct sk_buff *skb,
3253 struct net *net, int group,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00003254 struct ifinfomsg *ifm,
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07003255 struct netlink_ext_ack *extack,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00003256 struct nlattr **tb)
3257{
WANG Congd0795352015-03-23 16:31:09 -07003258 struct net_device *dev, *aux;
Vlad Dogarue7ed8282011-01-13 23:38:31 +00003259 int err;
3260
WANG Congd0795352015-03-23 16:31:09 -07003261 for_each_netdev_safe(net, dev, aux) {
Vlad Dogarue7ed8282011-01-13 23:38:31 +00003262 if (dev->group == group) {
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07003263 err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00003264 if (err < 0)
3265 return err;
3266 }
3267 }
3268
3269 return 0;
3270}
3271
Jakub Kicinskia2939742018-11-27 22:32:31 -08003272static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3273 struct nlattr **attr, struct netlink_ext_ack *extack)
Patrick McHardy38f7b872007-06-13 12:03:51 -07003274{
Jakub Kicinski420d0312018-11-27 22:32:30 -08003275 struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1];
3276 unsigned char name_assign_type = NET_NAME_USER;
3277 struct nlattr *linkinfo[IFLA_INFO_MAX + 1];
Eric Dumazetc6f6f242022-01-31 17:21:06 -08003278 const struct rtnl_link_ops *m_ops;
3279 struct net_device *master_dev;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003280 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07003281 const struct rtnl_link_ops *ops;
Jakub Kicinski420d0312018-11-27 22:32:30 -08003282 struct nlattr *tb[IFLA_MAX + 1];
3283 struct net *dest_net, *link_net;
3284 struct nlattr **slave_data;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003285 char kind[MODULE_NAME_LEN];
Jakub Kicinski420d0312018-11-27 22:32:30 -08003286 struct net_device *dev;
3287 struct ifinfomsg *ifm;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003288 char ifname[IFNAMSIZ];
Jakub Kicinski420d0312018-11-27 22:32:30 -08003289 struct nlattr **data;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003290 int err;
3291
Johannes Berg95a5afc2008-10-16 15:24:51 -07003292#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07003293replay:
Thomas Graf8072f082007-07-31 14:13:50 -07003294#endif
Johannes Berg8cb08172019-04-26 14:07:28 +02003295 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3296 ifla_policy, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07003297 if (err < 0)
3298 return err;
3299
Christian Brauner4ff66ca2018-02-07 13:53:20 +01003300 err = rtnl_ensure_unique_netns(tb, extack, false);
3301 if (err < 0)
3302 return err;
3303
Patrick McHardy38f7b872007-06-13 12:03:51 -07003304 if (tb[IFLA_IFNAME])
Francis Laniel872f6902020-11-15 18:08:06 +01003305 nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
Patrick McHardy38f7b872007-06-13 12:03:51 -07003306 else
3307 ifname[0] = '\0';
3308
3309 ifm = nlmsg_data(nlh);
3310 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07003311 dev = __dev_get_by_index(net, ifm->ifi_index);
Jiri Pirko76c9ac02019-09-30 11:48:20 +02003312 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3313 dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname);
Jiri Pirko7af12cb2019-09-30 11:48:18 +02003314 else
3315 dev = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003316
Eric Dumazetc6f6f242022-01-31 17:21:06 -08003317 master_dev = NULL;
3318 m_ops = NULL;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01003319 if (dev) {
3320 master_dev = netdev_master_upper_dev_get(dev);
3321 if (master_dev)
3322 m_ops = master_dev->rtnl_link_ops;
3323 }
3324
Rocco Yue8679c312021-08-03 20:02:50 +08003325 err = validate_linkmsg(dev, tb, extack);
Eric Dumazete0d087a2009-11-07 01:26:17 -08003326 if (err < 0)
Thomas Graf1840bb12008-02-23 19:54:36 -08003327 return err;
3328
Patrick McHardy38f7b872007-06-13 12:03:51 -07003329 if (tb[IFLA_LINKINFO]) {
Johannes Berg8cb08172019-04-26 14:07:28 +02003330 err = nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX,
3331 tb[IFLA_LINKINFO],
3332 ifla_info_policy, NULL);
Patrick McHardy38f7b872007-06-13 12:03:51 -07003333 if (err < 0)
3334 return err;
3335 } else
3336 memset(linkinfo, 0, sizeof(linkinfo));
3337
3338 if (linkinfo[IFLA_INFO_KIND]) {
Francis Laniel872f6902020-11-15 18:08:06 +01003339 nla_strscpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
Patrick McHardy38f7b872007-06-13 12:03:51 -07003340 ops = rtnl_link_ops_get(kind);
3341 } else {
3342 kind[0] = '\0';
3343 ops = NULL;
3344 }
3345
Jakub Kicinski420d0312018-11-27 22:32:30 -08003346 data = NULL;
3347 if (ops) {
3348 if (ops->maxtype > RTNL_MAX_TYPE)
3349 return -EINVAL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003350
Jakub Kicinski420d0312018-11-27 22:32:30 -08003351 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
Johannes Berg8cb08172019-04-26 14:07:28 +02003352 err = nla_parse_nested_deprecated(attr, ops->maxtype,
3353 linkinfo[IFLA_INFO_DATA],
3354 ops->policy, extack);
Jakub Kicinski420d0312018-11-27 22:32:30 -08003355 if (err < 0)
3356 return err;
3357 data = attr;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003358 }
Jakub Kicinski420d0312018-11-27 22:32:30 -08003359 if (ops->validate) {
3360 err = ops->validate(tb, data, extack);
3361 if (err < 0)
3362 return err;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01003363 }
Jakub Kicinski420d0312018-11-27 22:32:30 -08003364 }
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01003365
Jakub Kicinski420d0312018-11-27 22:32:30 -08003366 slave_data = NULL;
3367 if (m_ops) {
3368 if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)
3369 return -EINVAL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003370
Jakub Kicinski420d0312018-11-27 22:32:30 -08003371 if (m_ops->slave_maxtype &&
3372 linkinfo[IFLA_INFO_SLAVE_DATA]) {
Johannes Berg8cb08172019-04-26 14:07:28 +02003373 err = nla_parse_nested_deprecated(slave_attr,
3374 m_ops->slave_maxtype,
3375 linkinfo[IFLA_INFO_SLAVE_DATA],
3376 m_ops->slave_policy,
3377 extack);
Jakub Kicinski420d0312018-11-27 22:32:30 -08003378 if (err < 0)
3379 return err;
3380 slave_data = slave_attr;
3381 }
3382 }
3383
3384 if (dev) {
3385 int status = 0;
3386
3387 if (nlh->nlmsg_flags & NLM_F_EXCL)
3388 return -EEXIST;
3389 if (nlh->nlmsg_flags & NLM_F_REPLACE)
3390 return -EOPNOTSUPP;
3391
3392 if (linkinfo[IFLA_INFO_DATA]) {
3393 if (!ops || ops != dev->rtnl_link_ops ||
3394 !ops->changelink)
Patrick McHardy38f7b872007-06-13 12:03:51 -07003395 return -EOPNOTSUPP;
3396
Jakub Kicinski420d0312018-11-27 22:32:30 -08003397 err = ops->changelink(dev, tb, data, extack);
3398 if (err < 0)
3399 return err;
3400 status |= DO_SETLINK_NOTIFY;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003401 }
3402
Jakub Kicinski420d0312018-11-27 22:32:30 -08003403 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
3404 if (!m_ops || !m_ops->slave_changelink)
3405 return -EOPNOTSUPP;
3406
3407 err = m_ops->slave_changelink(master_dev, dev, tb,
3408 slave_data, extack);
3409 if (err < 0)
3410 return err;
3411 status |= DO_SETLINK_NOTIFY;
3412 }
3413
3414 return do_setlink(skb, dev, ifm, extack, tb, ifname, status);
3415 }
3416
3417 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
3418 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
3419 return rtnl_group_changelink(skb, net,
Patrick McHardyffa934f2011-01-20 03:00:42 +00003420 nla_get_u32(tb[IFLA_GROUP]),
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07003421 ifm, extack, tb);
Jakub Kicinski420d0312018-11-27 22:32:30 -08003422 return -ENODEV;
3423 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07003424
Jakub Kicinski420d0312018-11-27 22:32:30 -08003425 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
3426 return -EOPNOTSUPP;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003427
Jakub Kicinski420d0312018-11-27 22:32:30 -08003428 if (!ops) {
Johannes Berg95a5afc2008-10-16 15:24:51 -07003429#ifdef CONFIG_MODULES
Jakub Kicinski420d0312018-11-27 22:32:30 -08003430 if (kind[0]) {
3431 __rtnl_unlock();
3432 request_module("rtnl-link-%s", kind);
3433 rtnl_lock();
3434 ops = rtnl_link_ops_get(kind);
3435 if (ops)
3436 goto replay;
3437 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07003438#endif
Jakub Kicinski420d0312018-11-27 22:32:30 -08003439 NL_SET_ERR_MSG(extack, "Unknown device type");
3440 return -EOPNOTSUPP;
3441 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07003442
Johannes Berg8c713dc2021-06-12 10:20:54 +02003443 if (!ops->alloc && !ops->setup)
Jakub Kicinski420d0312018-11-27 22:32:30 -08003444 return -EOPNOTSUPP;
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02003445
Jakub Kicinski420d0312018-11-27 22:32:30 -08003446 if (!ifname[0]) {
3447 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
3448 name_assign_type = NET_NAME_ENUM;
3449 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07003450
Jakub Kicinski420d0312018-11-27 22:32:30 -08003451 dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN);
3452 if (IS_ERR(dest_net))
3453 return PTR_ERR(dest_net);
Eric W. Biederman13ad1772011-01-29 14:57:22 +00003454
Jakub Kicinski420d0312018-11-27 22:32:30 -08003455 if (tb[IFLA_LINK_NETNSID]) {
3456 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
Nicolas Dichtel317f4812015-01-15 15:11:18 +01003457
Jakub Kicinski420d0312018-11-27 22:32:30 -08003458 link_net = get_net_ns_by_id(dest_net, id);
3459 if (!link_net) {
3460 NL_SET_ERR_MSG(extack, "Unknown network namespace id");
3461 err = -EINVAL;
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00003462 goto out;
3463 }
Jakub Kicinski420d0312018-11-27 22:32:30 -08003464 err = -EPERM;
3465 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
3466 goto out;
3467 } else {
3468 link_net = NULL;
3469 }
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00003470
Jakub Kicinski420d0312018-11-27 22:32:30 -08003471 dev = rtnl_create_link(link_net ? : dest_net, ifname,
3472 name_assign_type, ops, tb, extack);
3473 if (IS_ERR(dev)) {
3474 err = PTR_ERR(dev);
David S. Miller43638902015-03-10 21:58:32 -04003475 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003476 }
Jakub Kicinski420d0312018-11-27 22:32:30 -08003477
3478 dev->ifindex = ifm->ifi_index;
3479
Jakub Kicinskic269a242021-01-06 10:40:06 -08003480 if (ops->newlink)
Jakub Kicinski420d0312018-11-27 22:32:30 -08003481 err = ops->newlink(link_net ? : net, dev, tb, data, extack);
Jakub Kicinskic269a242021-01-06 10:40:06 -08003482 else
Jakub Kicinski420d0312018-11-27 22:32:30 -08003483 err = register_netdevice(dev);
Jakub Kicinskic269a242021-01-06 10:40:06 -08003484 if (err < 0) {
3485 free_netdev(dev);
3486 goto out;
Jakub Kicinski420d0312018-11-27 22:32:30 -08003487 }
Jakub Kicinskic269a242021-01-06 10:40:06 -08003488
Jakub Kicinski420d0312018-11-27 22:32:30 -08003489 err = rtnl_configure_link(dev, ifm);
3490 if (err < 0)
3491 goto out_unregister;
3492 if (link_net) {
Andrei Vagin0854fa82021-04-06 23:40:51 -07003493 err = dev_change_net_namespace(dev, dest_net, ifname);
Jakub Kicinski420d0312018-11-27 22:32:30 -08003494 if (err < 0)
3495 goto out_unregister;
3496 }
3497 if (tb[IFLA_MASTER]) {
3498 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
3499 if (err)
3500 goto out_unregister;
3501 }
3502out:
3503 if (link_net)
3504 put_net(link_net);
3505 put_net(dest_net);
3506 return err;
3507out_unregister:
3508 if (ops->newlink) {
3509 LIST_HEAD(list_kill);
3510
3511 ops->dellink(dev, &list_kill);
3512 unregister_netdevice_many(&list_kill);
3513 } else {
3514 unregister_netdevice(dev);
3515 }
3516 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003517}
3518
Jakub Kicinskia2939742018-11-27 22:32:31 -08003519static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3520 struct netlink_ext_ack *extack)
3521{
3522 struct nlattr **attr;
3523 int ret;
3524
3525 attr = kmalloc_array(RTNL_MAX_TYPE + 1, sizeof(*attr), GFP_KERNEL);
3526 if (!attr)
3527 return -ENOMEM;
3528
3529 ret = __rtnl_newlink(skb, nlh, attr, extack);
3530 kfree(attr);
3531 return ret;
3532}
3533
Jakub Kicinski9b3757b2019-01-18 10:46:16 -08003534static int rtnl_valid_getlink_req(struct sk_buff *skb,
3535 const struct nlmsghdr *nlh,
3536 struct nlattr **tb,
3537 struct netlink_ext_ack *extack)
3538{
3539 struct ifinfomsg *ifm;
3540 int i, err;
3541
3542 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
3543 NL_SET_ERR_MSG(extack, "Invalid header for get link");
3544 return -EINVAL;
3545 }
3546
3547 if (!netlink_strict_get_check(skb))
Johannes Berg8cb08172019-04-26 14:07:28 +02003548 return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3549 ifla_policy, extack);
Jakub Kicinski9b3757b2019-01-18 10:46:16 -08003550
3551 ifm = nlmsg_data(nlh);
3552 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
3553 ifm->ifi_change) {
3554 NL_SET_ERR_MSG(extack, "Invalid values in header for get link request");
3555 return -EINVAL;
3556 }
3557
Johannes Berg8cb08172019-04-26 14:07:28 +02003558 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFLA_MAX,
3559 ifla_policy, extack);
Jakub Kicinski9b3757b2019-01-18 10:46:16 -08003560 if (err)
3561 return err;
3562
3563 for (i = 0; i <= IFLA_MAX; i++) {
3564 if (!tb[i])
3565 continue;
3566
3567 switch (i) {
3568 case IFLA_IFNAME:
Jiri Pirko76c9ac02019-09-30 11:48:20 +02003569 case IFLA_ALT_IFNAME:
Jakub Kicinski9b3757b2019-01-18 10:46:16 -08003570 case IFLA_EXT_MASK:
3571 case IFLA_TARGET_NETNSID:
3572 break;
3573 default:
3574 NL_SET_ERR_MSG(extack, "Unsupported attribute in get link request");
3575 return -EINVAL;
3576 }
3577 }
3578
3579 return 0;
3580}
3581
David Ahernc21ef3e2017-04-16 09:48:24 -07003582static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3583 struct netlink_ext_ack *extack)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003584{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003585 struct net *net = sock_net(skb->sk);
Jiri Benc79e1ad12017-11-02 17:04:38 -02003586 struct net *tgt_net = net;
Thomas Grafb60c5112006-08-04 23:05:34 -07003587 struct ifinfomsg *ifm;
3588 struct nlattr *tb[IFLA_MAX+1];
3589 struct net_device *dev = NULL;
3590 struct sk_buff *nskb;
Jiri Benc79e1ad12017-11-02 17:04:38 -02003591 int netnsid = -1;
Thomas Graf339bf982006-11-10 14:10:15 -08003592 int err;
Greg Rose115c9b82012-02-21 16:54:48 -05003593 u32 ext_filter_mask = 0;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003594
Jakub Kicinski9b3757b2019-01-18 10:46:16 -08003595 err = rtnl_valid_getlink_req(skb, nlh, tb, extack);
Thomas Grafb60c5112006-08-04 23:05:34 -07003596 if (err < 0)
Eric Sesterhenn9918f232006-09-26 23:26:38 -07003597 return err;
Thomas Grafb60c5112006-08-04 23:05:34 -07003598
Christian Brauner4ff66ca2018-02-07 13:53:20 +01003599 err = rtnl_ensure_unique_netns(tb, extack, true);
3600 if (err < 0)
3601 return err;
3602
Christian Brauner7e4a8d52018-09-04 21:53:53 +02003603 if (tb[IFLA_TARGET_NETNSID]) {
3604 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
Christian Braunerc383edc2018-09-04 21:53:47 +02003605 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
Jiri Benc79e1ad12017-11-02 17:04:38 -02003606 if (IS_ERR(tgt_net))
3607 return PTR_ERR(tgt_net);
3608 }
3609
Greg Rose115c9b82012-02-21 16:54:48 -05003610 if (tb[IFLA_EXT_MASK])
3611 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3612
Jiri Benc79e1ad12017-11-02 17:04:38 -02003613 err = -EINVAL;
Thomas Grafb60c5112006-08-04 23:05:34 -07003614 ifm = nlmsg_data(nlh);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003615 if (ifm->ifi_index > 0)
Jiri Benc79e1ad12017-11-02 17:04:38 -02003616 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
Jiri Pirko76c9ac02019-09-30 11:48:20 +02003617 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3618 dev = rtnl_dev_get(tgt_net, tb[IFLA_IFNAME],
3619 tb[IFLA_ALT_IFNAME], NULL);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003620 else
Jiri Benc79e1ad12017-11-02 17:04:38 -02003621 goto out;
Thomas Grafb60c5112006-08-04 23:05:34 -07003622
Jiri Benc79e1ad12017-11-02 17:04:38 -02003623 err = -ENODEV;
Eric Dumazeta3d12892009-10-21 10:59:31 +00003624 if (dev == NULL)
Jiri Benc79e1ad12017-11-02 17:04:38 -02003625 goto out;
Eric Dumazeta3d12892009-10-21 10:59:31 +00003626
Jiri Benc79e1ad12017-11-02 17:04:38 -02003627 err = -ENOBUFS;
Greg Rose115c9b82012-02-21 16:54:48 -05003628 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003629 if (nskb == NULL)
Jiri Benc79e1ad12017-11-02 17:04:38 -02003630 goto out;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003631
Jiri Benc79e1ad12017-11-02 17:04:38 -02003632 err = rtnl_fill_ifinfo(nskb, dev, net,
3633 RTM_NEWLINK, NETLINK_CB(skb).portid,
3634 nlh->nlmsg_seq, 0, 0, ext_filter_mask,
Guillaume Naultd4e4fdf2019-10-23 18:39:04 +02003635 0, NULL, 0, netnsid, GFP_KERNEL);
Patrick McHardy26932562007-01-31 23:16:40 -08003636 if (err < 0) {
3637 /* -EMSGSIZE implies BUG in if_nlmsg_size */
3638 WARN_ON(err == -EMSGSIZE);
3639 kfree_skb(nskb);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003640 } else
Eric W. Biederman15e47302012-09-07 20:12:54 +00003641 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
Jiri Benc79e1ad12017-11-02 17:04:38 -02003642out:
3643 if (netnsid >= 0)
3644 put_net(tgt_net);
Thomas Grafb60c5112006-08-04 23:05:34 -07003645
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003646 return err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003647}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003648
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02003649static int rtnl_alt_ifname(int cmd, struct net_device *dev, struct nlattr *attr,
3650 bool *changed, struct netlink_ext_ack *extack)
3651{
3652 char *alt_ifname;
3653 int err;
3654
3655 err = nla_validate(attr, attr->nla_len, IFLA_MAX, ifla_policy, extack);
3656 if (err)
3657 return err;
3658
Eric Dumazet44bfa9c2020-02-12 20:58:26 -08003659 alt_ifname = nla_strdup(attr, GFP_KERNEL);
3660 if (!alt_ifname)
3661 return -ENOMEM;
3662
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02003663 if (cmd == RTM_NEWLINKPROP) {
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02003664 err = netdev_name_node_alt_create(dev, alt_ifname);
Eric Dumazet44bfa9c2020-02-12 20:58:26 -08003665 if (!err)
3666 alt_ifname = NULL;
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02003667 } else if (cmd == RTM_DELLINKPROP) {
3668 err = netdev_name_node_alt_destroy(dev, alt_ifname);
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02003669 } else {
Eric Dumazet44bfa9c2020-02-12 20:58:26 -08003670 WARN_ON_ONCE(1);
3671 err = -EINVAL;
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02003672 }
3673
Eric Dumazet44bfa9c2020-02-12 20:58:26 -08003674 kfree(alt_ifname);
3675 if (!err)
3676 *changed = true;
3677 return err;
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02003678}
3679
3680static int rtnl_linkprop(int cmd, struct sk_buff *skb, struct nlmsghdr *nlh,
3681 struct netlink_ext_ack *extack)
3682{
3683 struct net *net = sock_net(skb->sk);
3684 struct nlattr *tb[IFLA_MAX + 1];
3685 struct net_device *dev;
3686 struct ifinfomsg *ifm;
3687 bool changed = false;
3688 struct nlattr *attr;
3689 int err, rem;
3690
3691 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
3692 if (err)
3693 return err;
3694
3695 err = rtnl_ensure_unique_netns(tb, extack, true);
3696 if (err)
3697 return err;
3698
3699 ifm = nlmsg_data(nlh);
Jiri Pirkocc6090e2019-09-30 11:48:19 +02003700 if (ifm->ifi_index > 0)
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02003701 dev = __dev_get_by_index(net, ifm->ifi_index);
Jiri Pirko76c9ac02019-09-30 11:48:20 +02003702 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3703 dev = rtnl_dev_get(net, tb[IFLA_IFNAME],
3704 tb[IFLA_ALT_IFNAME], NULL);
Jiri Pirkocc6090e2019-09-30 11:48:19 +02003705 else
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02003706 return -EINVAL;
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02003707
3708 if (!dev)
3709 return -ENODEV;
3710
3711 if (!tb[IFLA_PROP_LIST])
3712 return 0;
3713
3714 nla_for_each_nested(attr, tb[IFLA_PROP_LIST], rem) {
3715 switch (nla_type(attr)) {
3716 case IFLA_ALT_IFNAME:
3717 err = rtnl_alt_ifname(cmd, dev, attr, &changed, extack);
3718 if (err)
3719 return err;
3720 break;
3721 }
3722 }
3723
3724 if (changed)
3725 netdev_state_change(dev);
3726 return 0;
3727}
3728
3729static int rtnl_newlinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
3730 struct netlink_ext_ack *extack)
3731{
3732 return rtnl_linkprop(RTM_NEWLINKPROP, skb, nlh, extack);
3733}
3734
3735static int rtnl_dellinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
3736 struct netlink_ext_ack *extack)
3737{
3738 return rtnl_linkprop(RTM_DELLINKPROP, skb, nlh, extack);
3739}
3740
Di Zhuebfe3c52020-10-21 10:00:53 +08003741static u32 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
Greg Rosec7ac8672011-06-10 01:27:09 +00003742{
Greg Rose115c9b82012-02-21 16:54:48 -05003743 struct net *net = sock_net(skb->sk);
Di Zhuebfe3c52020-10-21 10:00:53 +08003744 size_t min_ifinfo_dump_size = 0;
Greg Rose115c9b82012-02-21 16:54:48 -05003745 struct nlattr *tb[IFLA_MAX+1];
3746 u32 ext_filter_mask = 0;
Di Zhuebfe3c52020-10-21 10:00:53 +08003747 struct net_device *dev;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02003748 int hdrlen;
Greg Rose115c9b82012-02-21 16:54:48 -05003749
Michal Schmidte5eca6d2014-05-28 14:15:19 +02003750 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3751 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
3752 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
3753
Johannes Berg8cb08172019-04-26 14:07:28 +02003754 if (nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00003755 if (tb[IFLA_EXT_MASK])
3756 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3757 }
Greg Rose115c9b82012-02-21 16:54:48 -05003758
3759 if (!ext_filter_mask)
3760 return NLMSG_GOODSIZE;
3761 /*
3762 * traverse the list of net devices and compute the minimum
3763 * buffer size based upon the filter mask.
3764 */
Florian Westphal6853dd42017-08-09 20:41:51 +02003765 rcu_read_lock();
3766 for_each_netdev_rcu(net, dev) {
Di Zhuebfe3c52020-10-21 10:00:53 +08003767 min_ifinfo_dump_size = max(min_ifinfo_dump_size,
3768 if_nlmsg_size(dev, ext_filter_mask));
Greg Rose115c9b82012-02-21 16:54:48 -05003769 }
Florian Westphal6853dd42017-08-09 20:41:51 +02003770 rcu_read_unlock();
Greg Rose115c9b82012-02-21 16:54:48 -05003771
Zhang Shengju93af2052016-11-22 14:14:28 +08003772 return nlmsg_total_size(min_ifinfo_dump_size);
Greg Rosec7ac8672011-06-10 01:27:09 +00003773}
3774
Adrian Bunk42bad1d2007-04-26 00:57:41 -07003775static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776{
3777 int idx;
3778 int s_idx = cb->family;
Christian Brauner87ccbb12018-09-04 21:53:51 +02003779 int type = cb->nlh->nlmsg_type - RTM_BASE;
David Ahernc63586d2018-10-24 12:59:02 -07003780 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781
3782 if (s_idx == 0)
3783 s_idx = 1;
Florian Westphal6853dd42017-08-09 20:41:51 +02003784
Patrick McHardy25239ce2010-04-26 16:02:05 +02003785 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
Jakub Kicinski51e13682020-12-09 18:16:08 -08003786 struct rtnl_link __rcu **tab;
Florian Westphaladdf9b92017-12-02 21:44:05 +01003787 struct rtnl_link *link;
Florian Westphal6853dd42017-08-09 20:41:51 +02003788 rtnl_dumpit_func dumpit;
3789
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 if (idx < s_idx || idx == PF_PACKET)
3791 continue;
Florian Westphal6853dd42017-08-09 20:41:51 +02003792
Florian Westphaladdf9b92017-12-02 21:44:05 +01003793 if (type < 0 || type >= RTM_NR_MSGTYPES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 continue;
Florian Westphal6853dd42017-08-09 20:41:51 +02003795
Florian Westphaladdf9b92017-12-02 21:44:05 +01003796 tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]);
3797 if (!tab)
3798 continue;
3799
Jakub Kicinski51e13682020-12-09 18:16:08 -08003800 link = rcu_dereference_rtnl(tab[type]);
Florian Westphaladdf9b92017-12-02 21:44:05 +01003801 if (!link)
3802 continue;
3803
3804 dumpit = link->dumpit;
Florian Westphal6853dd42017-08-09 20:41:51 +02003805 if (!dumpit)
3806 continue;
3807
Nicolas Dichtel04652772013-03-22 06:28:42 +00003808 if (idx > s_idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 memset(&cb->args[0], 0, sizeof(cb->args));
Nicolas Dichtel04652772013-03-22 06:28:42 +00003810 cb->prev_seq = 0;
3811 cb->seq = 0;
3812 }
David Ahernc63586d2018-10-24 12:59:02 -07003813 ret = dumpit(skb, cb);
Alexey Kodanev5e1acb42018-11-02 19:11:04 +03003814 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 break;
3816 }
3817 cb->family = idx;
3818
David Ahernc63586d2018-10-24 12:59:02 -07003819 return skb->len ? : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820}
3821
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003822struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003823 unsigned int change,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003824 u32 event, gfp_t flags, int *new_nsid,
3825 int new_ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003827 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 struct sk_buff *skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003829 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830
luo penghao50af5962021-10-21 06:40:20 +00003831 skb = nlmsg_new(if_nlmsg_size(dev, 0), flags);
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003832 if (skb == NULL)
3833 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834
Jiri Benc79e1ad12017-11-02 17:04:38 -02003835 err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
3836 type, 0, 0, change, 0, 0, event,
Guillaume Naultd4e4fdf2019-10-23 18:39:04 +02003837 new_nsid, new_ifindex, -1, flags);
Patrick McHardy26932562007-01-31 23:16:40 -08003838 if (err < 0) {
3839 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
3840 WARN_ON(err == -EMSGSIZE);
3841 kfree_skb(skb);
3842 goto errout;
3843 }
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003844 return skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003845errout:
3846 if (err < 0)
Eric W. Biederman4b3da702007-11-19 22:27:40 -08003847 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003848 return NULL;
3849}
3850
3851void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
3852{
3853 struct net *net = dev_net(dev);
3854
3855 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
3856}
3857
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003858static void rtmsg_ifinfo_event(int type, struct net_device *dev,
3859 unsigned int change, u32 event,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003860 gfp_t flags, int *new_nsid, int new_ifindex)
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003861{
3862 struct sk_buff *skb;
3863
Nicolas Dichteled2a80a2015-05-13 14:19:42 +02003864 if (dev->reg_state != NETREG_REGISTERED)
3865 return;
3866
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003867 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid,
3868 new_ifindex);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003869 if (skb)
3870 rtmsg_ifinfo_send(skb, dev, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871}
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003872
3873void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
3874 gfp_t flags)
3875{
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003876 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3877 NULL, 0);
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003878}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003880void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003881 gfp_t flags, int *new_nsid, int new_ifindex)
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003882{
3883 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003884 new_nsid, new_ifindex);
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003885}
3886
John Fastabendd83b0602012-04-15 06:44:08 +00003887static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
3888 struct net_device *dev,
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003889 u8 *addr, u16 vid, u32 pid, u32 seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003890 int type, unsigned int flags,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003891 int nlflags, u16 ndm_state)
John Fastabendd83b0602012-04-15 06:44:08 +00003892{
3893 struct nlmsghdr *nlh;
3894 struct ndmsg *ndm;
3895
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003896 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
John Fastabendd83b0602012-04-15 06:44:08 +00003897 if (!nlh)
3898 return -EMSGSIZE;
3899
3900 ndm = nlmsg_data(nlh);
3901 ndm->ndm_family = AF_BRIDGE;
3902 ndm->ndm_pad1 = 0;
3903 ndm->ndm_pad2 = 0;
3904 ndm->ndm_flags = flags;
3905 ndm->ndm_type = 0;
3906 ndm->ndm_ifindex = dev->ifindex;
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003907 ndm->ndm_state = ndm_state;
John Fastabendd83b0602012-04-15 06:44:08 +00003908
3909 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
3910 goto nla_put_failure;
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003911 if (vid)
3912 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
3913 goto nla_put_failure;
John Fastabendd83b0602012-04-15 06:44:08 +00003914
Johannes Berg053c0952015-01-16 22:09:00 +01003915 nlmsg_end(skb, nlh);
3916 return 0;
John Fastabendd83b0602012-04-15 06:44:08 +00003917
3918nla_put_failure:
3919 nlmsg_cancel(skb, nlh);
3920 return -EMSGSIZE;
3921}
3922
John Fastabend3ff661c2012-04-15 06:44:14 +00003923static inline size_t rtnl_fdb_nlmsg_size(void)
3924{
Sabrina Dubrocaf82ef3e2016-11-18 15:50:39 +01003925 return NLMSG_ALIGN(sizeof(struct ndmsg)) +
3926 nla_total_size(ETH_ALEN) + /* NDA_LLADDR */
3927 nla_total_size(sizeof(u16)) + /* NDA_VLAN */
3928 0;
John Fastabend3ff661c2012-04-15 06:44:14 +00003929}
3930
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003931static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
3932 u16 ndm_state)
John Fastabend3ff661c2012-04-15 06:44:14 +00003933{
3934 struct net *net = dev_net(dev);
3935 struct sk_buff *skb;
3936 int err = -ENOBUFS;
3937
3938 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
3939 if (!skb)
3940 goto errout;
3941
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003942 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003943 0, 0, type, NTF_SELF, 0, ndm_state);
John Fastabend3ff661c2012-04-15 06:44:14 +00003944 if (err < 0) {
3945 kfree_skb(skb);
3946 goto errout;
3947 }
3948
3949 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
3950 return;
3951errout:
3952 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
3953}
3954
Bart Van Asschea9869672019-03-25 09:17:22 -07003955/*
Vlad Yasevich090096b2013-03-06 15:39:42 +00003956 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3957 */
3958int ndo_dflt_fdb_add(struct ndmsg *ndm,
3959 struct nlattr *tb[],
3960 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003961 const unsigned char *addr, u16 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003962 u16 flags)
3963{
3964 int err = -EINVAL;
3965
3966 /* If aging addresses are supported device will need to
3967 * implement its own handler for this.
3968 */
3969 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
Vladimir Oltean78ecc892021-06-29 03:29:26 +03003970 netdev_info(dev, "default FDB implementation only supports local addresses\n");
Vlad Yasevich090096b2013-03-06 15:39:42 +00003971 return err;
3972 }
3973
Or Gerlitz65891fe2014-12-14 18:19:05 +02003974 if (vid) {
Vladimir Oltean23ac0b42021-06-29 03:29:25 +03003975 netdev_info(dev, "vlans aren't supported yet for dev_uc|mc_add()\n");
Or Gerlitz65891fe2014-12-14 18:19:05 +02003976 return err;
3977 }
3978
Vlad Yasevich090096b2013-03-06 15:39:42 +00003979 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3980 err = dev_uc_add_excl(dev, addr);
3981 else if (is_multicast_ether_addr(addr))
3982 err = dev_mc_add_excl(dev, addr);
3983
3984 /* Only return duplicate errors if NLM_F_EXCL is set */
3985 if (err == -EEXIST && !(flags & NLM_F_EXCL))
3986 err = 0;
3987
3988 return err;
3989}
3990EXPORT_SYMBOL(ndo_dflt_fdb_add);
3991
Florian Westphalb88d12e2017-10-10 17:10:04 +02003992static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
3993 struct netlink_ext_ack *extack)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003994{
3995 u16 vid = 0;
3996
3997 if (vlan_attr) {
3998 if (nla_len(vlan_attr) != sizeof(u16)) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003999 NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
Jiri Pirkof6f64242014-11-28 14:34:15 +01004000 return -EINVAL;
4001 }
4002
4003 vid = nla_get_u16(vlan_attr);
4004
4005 if (!vid || vid >= VLAN_VID_MASK) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004006 NL_SET_ERR_MSG(extack, "invalid vlan id");
Jiri Pirkof6f64242014-11-28 14:34:15 +01004007 return -EINVAL;
4008 }
4009 }
4010 *p_vid = vid;
4011 return 0;
4012}
4013
David Ahernc21ef3e2017-04-16 09:48:24 -07004014static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
4015 struct netlink_ext_ack *extack)
John Fastabend77162022012-04-15 06:43:56 +00004016{
4017 struct net *net = sock_net(skb->sk);
John Fastabend77162022012-04-15 06:43:56 +00004018 struct ndmsg *ndm;
4019 struct nlattr *tb[NDA_MAX+1];
4020 struct net_device *dev;
4021 u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01004022 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00004023 int err;
4024
Johannes Berg8cb08172019-04-26 14:07:28 +02004025 err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
4026 extack);
John Fastabend77162022012-04-15 06:43:56 +00004027 if (err < 0)
4028 return err;
4029
4030 ndm = nlmsg_data(nlh);
4031 if (ndm->ndm_ifindex == 0) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004032 NL_SET_ERR_MSG(extack, "invalid ifindex");
John Fastabend77162022012-04-15 06:43:56 +00004033 return -EINVAL;
4034 }
4035
4036 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
4037 if (dev == NULL) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004038 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabend77162022012-04-15 06:43:56 +00004039 return -ENODEV;
4040 }
4041
4042 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004043 NL_SET_ERR_MSG(extack, "invalid address");
John Fastabend77162022012-04-15 06:43:56 +00004044 return -EINVAL;
4045 }
4046
Ido Schimmelda715772018-10-29 20:36:43 +00004047 if (dev->type != ARPHRD_ETHER) {
4048 NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices");
4049 return -EINVAL;
4050 }
4051
John Fastabend77162022012-04-15 06:43:56 +00004052 addr = nla_data(tb[NDA_LLADDR]);
John Fastabend77162022012-04-15 06:43:56 +00004053
Florian Westphalb88d12e2017-10-10 17:10:04 +02004054 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
Jiri Pirkof6f64242014-11-28 14:34:15 +01004055 if (err)
4056 return err;
4057
John Fastabend77162022012-04-15 06:43:56 +00004058 err = -EOPNOTSUPP;
4059
4060 /* Support fdb on master device the net/bridge default case */
4061 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
Julian Wiedmann2e92a2d2020-02-20 09:00:07 +01004062 netif_is_bridge_port(dev)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00004063 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4064 const struct net_device_ops *ops = br_dev->netdev_ops;
4065
Jiri Pirkof6f64242014-11-28 14:34:15 +01004066 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
Petr Machata87b09842019-01-16 23:06:50 +00004067 nlh->nlmsg_flags, extack);
John Fastabend77162022012-04-15 06:43:56 +00004068 if (err)
4069 goto out;
4070 else
4071 ndm->ndm_flags &= ~NTF_MASTER;
4072 }
4073
4074 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00004075 if ((ndm->ndm_flags & NTF_SELF)) {
4076 if (dev->netdev_ops->ndo_fdb_add)
4077 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
Jiri Pirkof6f64242014-11-28 14:34:15 +01004078 vid,
Petr Machata87b09842019-01-16 23:06:50 +00004079 nlh->nlmsg_flags,
4080 extack);
Vlad Yasevich090096b2013-03-06 15:39:42 +00004081 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01004082 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00004083 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00004084
John Fastabend3ff661c2012-04-15 06:44:14 +00004085 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00004086 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
4087 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00004088 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00004089 }
John Fastabend77162022012-04-15 06:43:56 +00004090 }
4091out:
4092 return err;
4093}
4094
Bart Van Asschea9869672019-03-25 09:17:22 -07004095/*
Vlad Yasevich090096b2013-03-06 15:39:42 +00004096 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
4097 */
4098int ndo_dflt_fdb_del(struct ndmsg *ndm,
4099 struct nlattr *tb[],
4100 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01004101 const unsigned char *addr, u16 vid)
Vlad Yasevich090096b2013-03-06 15:39:42 +00004102{
Alexander Duyckc8a89c42014-07-15 15:15:20 -07004103 int err = -EINVAL;
Vlad Yasevich090096b2013-03-06 15:39:42 +00004104
4105 /* If aging addresses are supported device will need to
4106 * implement its own handler for this.
4107 */
Sridhar Samudrala64535992013-08-08 15:19:48 -07004108 if (!(ndm->ndm_state & NUD_PERMANENT)) {
Vladimir Oltean78ecc892021-06-29 03:29:26 +03004109 netdev_info(dev, "default FDB implementation only supports local addresses\n");
Alexander Duyckc8a89c42014-07-15 15:15:20 -07004110 return err;
Vlad Yasevich090096b2013-03-06 15:39:42 +00004111 }
4112
4113 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
4114 err = dev_uc_del(dev, addr);
4115 else if (is_multicast_ether_addr(addr))
4116 err = dev_mc_del(dev, addr);
Vlad Yasevich090096b2013-03-06 15:39:42 +00004117
4118 return err;
4119}
4120EXPORT_SYMBOL(ndo_dflt_fdb_del);
4121
David Ahernc21ef3e2017-04-16 09:48:24 -07004122static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
4123 struct netlink_ext_ack *extack)
John Fastabend77162022012-04-15 06:43:56 +00004124{
4125 struct net *net = sock_net(skb->sk);
4126 struct ndmsg *ndm;
Vlad Yasevich1690be62013-02-13 12:00:18 +00004127 struct nlattr *tb[NDA_MAX+1];
John Fastabend77162022012-04-15 06:43:56 +00004128 struct net_device *dev;
John Fastabend77162022012-04-15 06:43:56 +00004129 __u8 *addr;
Colin Ian King7d311802020-04-25 12:28:14 +01004130 int err;
Jiri Pirkof6f64242014-11-28 14:34:15 +01004131 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00004132
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07004133 if (!netlink_capable(skb, CAP_NET_ADMIN))
Vlad Yasevich1690be62013-02-13 12:00:18 +00004134 return -EPERM;
4135
Johannes Berg8cb08172019-04-26 14:07:28 +02004136 err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
4137 extack);
Vlad Yasevich1690be62013-02-13 12:00:18 +00004138 if (err < 0)
4139 return err;
John Fastabend77162022012-04-15 06:43:56 +00004140
4141 ndm = nlmsg_data(nlh);
4142 if (ndm->ndm_ifindex == 0) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004143 NL_SET_ERR_MSG(extack, "invalid ifindex");
John Fastabend77162022012-04-15 06:43:56 +00004144 return -EINVAL;
4145 }
4146
4147 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
4148 if (dev == NULL) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004149 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabend77162022012-04-15 06:43:56 +00004150 return -ENODEV;
4151 }
4152
Vlad Yasevich1690be62013-02-13 12:00:18 +00004153 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004154 NL_SET_ERR_MSG(extack, "invalid address");
John Fastabend77162022012-04-15 06:43:56 +00004155 return -EINVAL;
4156 }
4157
Ido Schimmelda715772018-10-29 20:36:43 +00004158 if (dev->type != ARPHRD_ETHER) {
4159 NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices");
4160 return -EINVAL;
4161 }
4162
Vlad Yasevich1690be62013-02-13 12:00:18 +00004163 addr = nla_data(tb[NDA_LLADDR]);
Vlad Yasevich1690be62013-02-13 12:00:18 +00004164
Florian Westphalb88d12e2017-10-10 17:10:04 +02004165 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
Jiri Pirkof6f64242014-11-28 14:34:15 +01004166 if (err)
4167 return err;
4168
John Fastabend77162022012-04-15 06:43:56 +00004169 err = -EOPNOTSUPP;
4170
4171 /* Support fdb on master device the net/bridge default case */
4172 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
Julian Wiedmann2e92a2d2020-02-20 09:00:07 +01004173 netif_is_bridge_port(dev)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00004174 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4175 const struct net_device_ops *ops = br_dev->netdev_ops;
John Fastabend77162022012-04-15 06:43:56 +00004176
Jiri Pirko898e5062013-01-03 22:48:52 +00004177 if (ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01004178 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00004179
4180 if (err)
4181 goto out;
4182 else
4183 ndm->ndm_flags &= ~NTF_MASTER;
4184 }
4185
4186 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00004187 if (ndm->ndm_flags & NTF_SELF) {
4188 if (dev->netdev_ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01004189 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
4190 vid);
Vlad Yasevich090096b2013-03-06 15:39:42 +00004191 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01004192 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00004193
John Fastabend3ff661c2012-04-15 06:44:14 +00004194 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00004195 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
4196 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00004197 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00004198 }
John Fastabend77162022012-04-15 06:43:56 +00004199 }
4200out:
4201 return err;
4202}
4203
John Fastabendd83b0602012-04-15 06:44:08 +00004204static int nlmsg_populate_fdb(struct sk_buff *skb,
4205 struct netlink_callback *cb,
4206 struct net_device *dev,
4207 int *idx,
4208 struct netdev_hw_addr_list *list)
4209{
4210 struct netdev_hw_addr *ha;
4211 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00004212 u32 portid, seq;
John Fastabendd83b0602012-04-15 06:44:08 +00004213
Eric W. Biederman15e47302012-09-07 20:12:54 +00004214 portid = NETLINK_CB(cb->skb).portid;
John Fastabendd83b0602012-04-15 06:44:08 +00004215 seq = cb->nlh->nlmsg_seq;
4216
4217 list_for_each_entry(ha, &list->list, list) {
Roopa Prabhud2976532016-08-30 21:56:45 -07004218 if (*idx < cb->args[2])
John Fastabendd83b0602012-04-15 06:44:08 +00004219 goto skip;
4220
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00004221 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
John Fastabenda7a558f2012-11-01 16:23:10 +00004222 portid, seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01004223 RTM_NEWNEIGH, NTF_SELF,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00004224 NLM_F_MULTI, NUD_PERMANENT);
John Fastabendd83b0602012-04-15 06:44:08 +00004225 if (err < 0)
4226 return err;
4227skip:
4228 *idx += 1;
4229 }
4230 return 0;
4231}
4232
4233/**
Ben Hutchings2c530402012-07-10 10:55:09 +00004234 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
Bart Van Asschea9869672019-03-25 09:17:22 -07004235 * @skb: socket buffer to store message in
4236 * @cb: netlink callback
John Fastabendd83b0602012-04-15 06:44:08 +00004237 * @dev: netdevice
Bart Van Asschea9869672019-03-25 09:17:22 -07004238 * @filter_dev: ignored
4239 * @idx: the number of FDB table entries dumped is added to *@idx
John Fastabendd83b0602012-04-15 06:44:08 +00004240 *
4241 * Default netdevice operation to dump the existing unicast address list.
John Fastabend91f3e7b2013-03-29 08:18:37 +00004242 * Returns number of addresses from list put in skb.
John Fastabendd83b0602012-04-15 06:44:08 +00004243 */
4244int ndo_dflt_fdb_dump(struct sk_buff *skb,
4245 struct netlink_callback *cb,
4246 struct net_device *dev,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04004247 struct net_device *filter_dev,
Roopa Prabhud2976532016-08-30 21:56:45 -07004248 int *idx)
John Fastabendd83b0602012-04-15 06:44:08 +00004249{
4250 int err;
4251
Eric Dumazet68883892018-12-04 09:40:35 -08004252 if (dev->type != ARPHRD_ETHER)
4253 return -EINVAL;
4254
John Fastabendd83b0602012-04-15 06:44:08 +00004255 netif_addr_lock_bh(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -07004256 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
John Fastabendd83b0602012-04-15 06:44:08 +00004257 if (err)
4258 goto out;
Zhang Shengju2934c9d2016-11-30 16:37:34 +08004259 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
John Fastabendd83b0602012-04-15 06:44:08 +00004260out:
4261 netif_addr_unlock_bh(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -07004262 return err;
John Fastabendd83b0602012-04-15 06:44:08 +00004263}
4264EXPORT_SYMBOL(ndo_dflt_fdb_dump);
4265
David Ahern8c6e1372018-10-07 20:16:44 -07004266static int valid_fdb_dump_strict(const struct nlmsghdr *nlh,
4267 int *br_idx, int *brport_idx,
4268 struct netlink_ext_ack *extack)
4269{
4270 struct nlattr *tb[NDA_MAX + 1];
4271 struct ndmsg *ndm;
4272 int err, i;
4273
4274 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
4275 NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request");
4276 return -EINVAL;
4277 }
4278
4279 ndm = nlmsg_data(nlh);
4280 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
4281 ndm->ndm_flags || ndm->ndm_type) {
Nikolay Aleksandrov8b730182019-10-29 13:59:32 +02004282 NL_SET_ERR_MSG(extack, "Invalid values in header for fdb dump request");
David Ahern8c6e1372018-10-07 20:16:44 -07004283 return -EINVAL;
4284 }
4285
Johannes Berg8cb08172019-04-26 14:07:28 +02004286 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
4287 NDA_MAX, NULL, extack);
David Ahern8c6e1372018-10-07 20:16:44 -07004288 if (err < 0)
4289 return err;
4290
4291 *brport_idx = ndm->ndm_ifindex;
4292 for (i = 0; i <= NDA_MAX; ++i) {
4293 if (!tb[i])
4294 continue;
4295
4296 switch (i) {
4297 case NDA_IFINDEX:
4298 if (nla_len(tb[i]) != sizeof(u32)) {
4299 NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request");
4300 return -EINVAL;
4301 }
4302 *brport_idx = nla_get_u32(tb[NDA_IFINDEX]);
4303 break;
4304 case NDA_MASTER:
4305 if (nla_len(tb[i]) != sizeof(u32)) {
4306 NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request");
4307 return -EINVAL;
4308 }
4309 *br_idx = nla_get_u32(tb[NDA_MASTER]);
4310 break;
4311 default:
4312 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request");
4313 return -EINVAL;
4314 }
4315 }
4316
4317 return 0;
4318}
4319
David Ahern8dfbda12018-10-07 20:16:43 -07004320static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh,
4321 int *br_idx, int *brport_idx,
4322 struct netlink_ext_ack *extack)
4323{
David Ahern8dfbda12018-10-07 20:16:43 -07004324 struct nlattr *tb[IFLA_MAX+1];
4325 int err;
4326
4327 /* A hack to preserve kernel<->userspace interface.
4328 * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0.
4329 * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails.
4330 * So, check for ndmsg with an optional u32 attribute (not used here).
4331 * Fortunately these sizes don't conflict with the size of ifinfomsg
4332 * with an optional attribute.
4333 */
4334 if (nlmsg_len(nlh) != sizeof(struct ndmsg) &&
4335 (nlmsg_len(nlh) != sizeof(struct ndmsg) +
4336 nla_attr_size(sizeof(u32)))) {
David Ahern4565d7e2018-10-08 13:57:24 -07004337 struct ifinfomsg *ifm;
4338
Johannes Berg8cb08172019-04-26 14:07:28 +02004339 err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
4340 tb, IFLA_MAX, ifla_policy,
4341 extack);
David Ahern8dfbda12018-10-07 20:16:43 -07004342 if (err < 0) {
4343 return -EINVAL;
4344 } else if (err == 0) {
4345 if (tb[IFLA_MASTER])
4346 *br_idx = nla_get_u32(tb[IFLA_MASTER]);
4347 }
4348
David Ahern4565d7e2018-10-08 13:57:24 -07004349 ifm = nlmsg_data(nlh);
David Ahern8dfbda12018-10-07 20:16:43 -07004350 *brport_idx = ifm->ifi_index;
4351 }
4352 return 0;
4353}
4354
John Fastabend77162022012-04-15 06:43:56 +00004355static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
4356{
John Fastabend77162022012-04-15 06:43:56 +00004357 struct net_device *dev;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004358 struct net_device *br_dev = NULL;
4359 const struct net_device_ops *ops = NULL;
4360 const struct net_device_ops *cops = NULL;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004361 struct net *net = sock_net(skb->sk);
Roopa Prabhud2976532016-08-30 21:56:45 -07004362 struct hlist_head *head;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004363 int brport_idx = 0;
4364 int br_idx = 0;
Roopa Prabhud2976532016-08-30 21:56:45 -07004365 int h, s_h;
4366 int idx = 0, s_idx;
4367 int err = 0;
4368 int fidx = 0;
John Fastabend77162022012-04-15 06:43:56 +00004369
David Ahern8c6e1372018-10-07 20:16:44 -07004370 if (cb->strict_check)
4371 err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx,
4372 cb->extack);
4373 else
4374 err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx,
4375 cb->extack);
David Ahern8dfbda12018-10-07 20:16:43 -07004376 if (err < 0)
4377 return err;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004378
4379 if (br_idx) {
4380 br_dev = __dev_get_by_index(net, br_idx);
4381 if (!br_dev)
4382 return -ENODEV;
4383
4384 ops = br_dev->netdev_ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004385 }
4386
Roopa Prabhud2976532016-08-30 21:56:45 -07004387 s_h = cb->args[0];
4388 s_idx = cb->args[1];
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004389
Roopa Prabhud2976532016-08-30 21:56:45 -07004390 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4391 idx = 0;
4392 head = &net->dev_index_head[h];
4393 hlist_for_each_entry(dev, head, index_hlist) {
4394
4395 if (brport_idx && (dev->ifindex != brport_idx))
4396 continue;
4397
4398 if (!br_idx) { /* user did not specify a specific bridge */
Julian Wiedmann2e92a2d2020-02-20 09:00:07 +01004399 if (netif_is_bridge_port(dev)) {
Roopa Prabhud2976532016-08-30 21:56:45 -07004400 br_dev = netdev_master_upper_dev_get(dev);
4401 cops = br_dev->netdev_ops;
4402 }
4403 } else {
4404 if (dev != br_dev &&
Julian Wiedmann2e92a2d2020-02-20 09:00:07 +01004405 !netif_is_bridge_port(dev))
Roopa Prabhud2976532016-08-30 21:56:45 -07004406 continue;
4407
4408 if (br_dev != netdev_master_upper_dev_get(dev) &&
Kyungrok Chung254ec032021-10-16 20:21:36 +09004409 !netif_is_bridge_master(dev))
Roopa Prabhud2976532016-08-30 21:56:45 -07004410 continue;
4411 cops = ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004412 }
4413
Roopa Prabhud2976532016-08-30 21:56:45 -07004414 if (idx < s_idx)
4415 goto cont;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004416
Julian Wiedmann2e92a2d2020-02-20 09:00:07 +01004417 if (netif_is_bridge_port(dev)) {
Roopa Prabhud2976532016-08-30 21:56:45 -07004418 if (cops && cops->ndo_fdb_dump) {
4419 err = cops->ndo_fdb_dump(skb, cb,
4420 br_dev, dev,
4421 &fidx);
4422 if (err == -EMSGSIZE)
4423 goto out;
4424 }
4425 }
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004426
Roopa Prabhud2976532016-08-30 21:56:45 -07004427 if (dev->netdev_ops->ndo_fdb_dump)
4428 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
4429 dev, NULL,
4430 &fidx);
4431 else
4432 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
4433 &fidx);
4434 if (err == -EMSGSIZE)
4435 goto out;
4436
4437 cops = NULL;
4438
4439 /* reset fdb offset to 0 for rest of the interfaces */
4440 cb->args[2] = 0;
4441 fidx = 0;
4442cont:
4443 idx++;
John Fastabend77162022012-04-15 06:43:56 +00004444 }
John Fastabend77162022012-04-15 06:43:56 +00004445 }
John Fastabend77162022012-04-15 06:43:56 +00004446
Roopa Prabhud2976532016-08-30 21:56:45 -07004447out:
4448 cb->args[0] = h;
4449 cb->args[1] = idx;
4450 cb->args[2] = fidx;
4451
John Fastabend77162022012-04-15 06:43:56 +00004452 return skb->len;
4453}
4454
Roopa Prabhu5b2f94b2018-12-15 22:35:08 -08004455static int valid_fdb_get_strict(const struct nlmsghdr *nlh,
4456 struct nlattr **tb, u8 *ndm_flags,
4457 int *br_idx, int *brport_idx, u8 **addr,
4458 u16 *vid, struct netlink_ext_ack *extack)
4459{
4460 struct ndmsg *ndm;
4461 int err, i;
4462
4463 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
4464 NL_SET_ERR_MSG(extack, "Invalid header for fdb get request");
4465 return -EINVAL;
4466 }
4467
4468 ndm = nlmsg_data(nlh);
4469 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
4470 ndm->ndm_type) {
4471 NL_SET_ERR_MSG(extack, "Invalid values in header for fdb get request");
4472 return -EINVAL;
4473 }
4474
4475 if (ndm->ndm_flags & ~(NTF_MASTER | NTF_SELF)) {
4476 NL_SET_ERR_MSG(extack, "Invalid flags in header for fdb get request");
4477 return -EINVAL;
4478 }
4479
Johannes Berg8cb08172019-04-26 14:07:28 +02004480 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
4481 NDA_MAX, nda_policy, extack);
Roopa Prabhu5b2f94b2018-12-15 22:35:08 -08004482 if (err < 0)
4483 return err;
4484
4485 *ndm_flags = ndm->ndm_flags;
4486 *brport_idx = ndm->ndm_ifindex;
4487 for (i = 0; i <= NDA_MAX; ++i) {
4488 if (!tb[i])
4489 continue;
4490
4491 switch (i) {
4492 case NDA_MASTER:
4493 *br_idx = nla_get_u32(tb[i]);
4494 break;
4495 case NDA_LLADDR:
4496 if (nla_len(tb[i]) != ETH_ALEN) {
4497 NL_SET_ERR_MSG(extack, "Invalid address in fdb get request");
4498 return -EINVAL;
4499 }
4500 *addr = nla_data(tb[i]);
4501 break;
4502 case NDA_VLAN:
4503 err = fdb_vid_parse(tb[i], vid, extack);
4504 if (err)
4505 return err;
4506 break;
4507 case NDA_VNI:
4508 break;
4509 default:
4510 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb get request");
4511 return -EINVAL;
4512 }
4513 }
4514
4515 return 0;
4516}
4517
4518static int rtnl_fdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh,
4519 struct netlink_ext_ack *extack)
4520{
4521 struct net_device *dev = NULL, *br_dev = NULL;
4522 const struct net_device_ops *ops = NULL;
4523 struct net *net = sock_net(in_skb->sk);
4524 struct nlattr *tb[NDA_MAX + 1];
4525 struct sk_buff *skb;
4526 int brport_idx = 0;
4527 u8 ndm_flags = 0;
4528 int br_idx = 0;
4529 u8 *addr = NULL;
4530 u16 vid = 0;
4531 int err;
4532
4533 err = valid_fdb_get_strict(nlh, tb, &ndm_flags, &br_idx,
4534 &brport_idx, &addr, &vid, extack);
4535 if (err < 0)
4536 return err;
4537
Nikolay Aleksandrovf989d032018-12-30 14:33:20 +02004538 if (!addr) {
4539 NL_SET_ERR_MSG(extack, "Missing lookup address for fdb get request");
4540 return -EINVAL;
4541 }
4542
Roopa Prabhu5b2f94b2018-12-15 22:35:08 -08004543 if (brport_idx) {
4544 dev = __dev_get_by_index(net, brport_idx);
4545 if (!dev) {
4546 NL_SET_ERR_MSG(extack, "Unknown device ifindex");
4547 return -ENODEV;
4548 }
4549 }
4550
4551 if (br_idx) {
4552 if (dev) {
4553 NL_SET_ERR_MSG(extack, "Master and device are mutually exclusive");
4554 return -EINVAL;
4555 }
4556
4557 br_dev = __dev_get_by_index(net, br_idx);
4558 if (!br_dev) {
4559 NL_SET_ERR_MSG(extack, "Invalid master ifindex");
4560 return -EINVAL;
4561 }
4562 ops = br_dev->netdev_ops;
4563 }
4564
4565 if (dev) {
4566 if (!ndm_flags || (ndm_flags & NTF_MASTER)) {
Julian Wiedmann2e92a2d2020-02-20 09:00:07 +01004567 if (!netif_is_bridge_port(dev)) {
Roopa Prabhu5b2f94b2018-12-15 22:35:08 -08004568 NL_SET_ERR_MSG(extack, "Device is not a bridge port");
4569 return -EINVAL;
4570 }
4571 br_dev = netdev_master_upper_dev_get(dev);
4572 if (!br_dev) {
4573 NL_SET_ERR_MSG(extack, "Master of device not found");
4574 return -EINVAL;
4575 }
4576 ops = br_dev->netdev_ops;
4577 } else {
4578 if (!(ndm_flags & NTF_SELF)) {
4579 NL_SET_ERR_MSG(extack, "Missing NTF_SELF");
4580 return -EINVAL;
4581 }
4582 ops = dev->netdev_ops;
4583 }
4584 }
4585
4586 if (!br_dev && !dev) {
4587 NL_SET_ERR_MSG(extack, "No device specified");
4588 return -ENODEV;
4589 }
4590
4591 if (!ops || !ops->ndo_fdb_get) {
4592 NL_SET_ERR_MSG(extack, "Fdb get operation not supported by device");
4593 return -EOPNOTSUPP;
4594 }
4595
4596 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
4597 if (!skb)
4598 return -ENOBUFS;
4599
4600 if (br_dev)
4601 dev = br_dev;
4602 err = ops->ndo_fdb_get(skb, tb, dev, addr, vid,
4603 NETLINK_CB(in_skb).portid,
4604 nlh->nlmsg_seq, extack);
4605 if (err)
4606 goto out;
4607
4608 return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
4609out:
4610 kfree_skb(skb);
4611 return err;
4612}
4613
Scott Feldman2c3c0312014-11-28 14:34:25 +01004614static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
4615 unsigned int attrnum, unsigned int flag)
4616{
4617 if (mask & flag)
4618 return nla_put_u8(skb, attrnum, !!(flags & flag));
4619 return 0;
4620}
4621
John Fastabend815cccb2012-10-24 08:13:09 +00004622int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Scott Feldman2c3c0312014-11-28 14:34:25 +01004623 struct net_device *dev, u16 mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -07004624 u32 flags, u32 mask, int nlflags,
4625 u32 filter_mask,
4626 int (*vlan_fill)(struct sk_buff *skb,
4627 struct net_device *dev,
4628 u32 filter_mask))
John Fastabend815cccb2012-10-24 08:13:09 +00004629{
4630 struct nlmsghdr *nlh;
4631 struct ifinfomsg *ifm;
4632 struct nlattr *br_afspec;
Scott Feldman2c3c0312014-11-28 14:34:25 +01004633 struct nlattr *protinfo;
John Fastabend815cccb2012-10-24 08:13:09 +00004634 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Jiri Pirko898e5062013-01-03 22:48:52 +00004635 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07004636 int err = 0;
John Fastabend815cccb2012-10-24 08:13:09 +00004637
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02004638 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
John Fastabend815cccb2012-10-24 08:13:09 +00004639 if (nlh == NULL)
4640 return -EMSGSIZE;
4641
4642 ifm = nlmsg_data(nlh);
4643 ifm->ifi_family = AF_BRIDGE;
4644 ifm->__ifi_pad = 0;
4645 ifm->ifi_type = dev->type;
4646 ifm->ifi_index = dev->ifindex;
4647 ifm->ifi_flags = dev_get_flags(dev);
4648 ifm->ifi_change = 0;
4649
4650
4651 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4652 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4653 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00004654 (br_dev &&
4655 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
John Fastabend815cccb2012-10-24 08:13:09 +00004656 (dev->addr_len &&
4657 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02004658 (dev->ifindex != dev_get_iflink(dev) &&
4659 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
John Fastabend815cccb2012-10-24 08:13:09 +00004660 goto nla_put_failure;
4661
Michal Kubecekae0be8d2019-04-26 11:13:06 +02004662 br_afspec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
John Fastabend815cccb2012-10-24 08:13:09 +00004663 if (!br_afspec)
4664 goto nla_put_failure;
4665
Roopa Prabhu1d460b92014-12-08 14:04:20 -08004666 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
John Fastabend815cccb2012-10-24 08:13:09 +00004667 nla_nest_cancel(skb, br_afspec);
4668 goto nla_put_failure;
4669 }
Roopa Prabhu1d460b92014-12-08 14:04:20 -08004670
4671 if (mode != BRIDGE_MODE_UNDEF) {
4672 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
4673 nla_nest_cancel(skb, br_afspec);
4674 goto nla_put_failure;
4675 }
4676 }
Scott Feldman7d4f8d82015-06-22 00:27:17 -07004677 if (vlan_fill) {
4678 err = vlan_fill(skb, dev, filter_mask);
4679 if (err) {
4680 nla_nest_cancel(skb, br_afspec);
4681 goto nla_put_failure;
4682 }
4683 }
John Fastabend815cccb2012-10-24 08:13:09 +00004684 nla_nest_end(skb, br_afspec);
4685
Michal Kubecekae0be8d2019-04-26 11:13:06 +02004686 protinfo = nla_nest_start(skb, IFLA_PROTINFO);
Scott Feldman2c3c0312014-11-28 14:34:25 +01004687 if (!protinfo)
4688 goto nla_put_failure;
4689
4690 if (brport_nla_put_flag(skb, flags, mask,
4691 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
4692 brport_nla_put_flag(skb, flags, mask,
4693 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
4694 brport_nla_put_flag(skb, flags, mask,
4695 IFLA_BRPORT_FAST_LEAVE,
4696 BR_MULTICAST_FAST_LEAVE) ||
4697 brport_nla_put_flag(skb, flags, mask,
4698 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
4699 brport_nla_put_flag(skb, flags, mask,
4700 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
4701 brport_nla_put_flag(skb, flags, mask,
4702 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
4703 brport_nla_put_flag(skb, flags, mask,
4704 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
4705 brport_nla_put_flag(skb, flags, mask,
Julian Wiedmann583cb0b2020-02-17 14:45:01 +01004706 IFLA_BRPORT_PROXYARP, BR_PROXYARP) ||
4707 brport_nla_put_flag(skb, flags, mask,
4708 IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD) ||
4709 brport_nla_put_flag(skb, flags, mask,
4710 IFLA_BRPORT_BCAST_FLOOD, BR_BCAST_FLOOD)) {
Scott Feldman2c3c0312014-11-28 14:34:25 +01004711 nla_nest_cancel(skb, protinfo);
4712 goto nla_put_failure;
4713 }
4714
4715 nla_nest_end(skb, protinfo);
4716
Johannes Berg053c0952015-01-16 22:09:00 +01004717 nlmsg_end(skb, nlh);
4718 return 0;
John Fastabend815cccb2012-10-24 08:13:09 +00004719nla_put_failure:
4720 nlmsg_cancel(skb, nlh);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07004721 return err ? err : -EMSGSIZE;
John Fastabend815cccb2012-10-24 08:13:09 +00004722}
Scott Feldman7d4f8d82015-06-22 00:27:17 -07004723EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
John Fastabend815cccb2012-10-24 08:13:09 +00004724
David Ahern2d011be2018-10-07 20:16:31 -07004725static int valid_bridge_getlink_req(const struct nlmsghdr *nlh,
4726 bool strict_check, u32 *filter_mask,
4727 struct netlink_ext_ack *extack)
4728{
4729 struct nlattr *tb[IFLA_MAX+1];
4730 int err, i;
4731
4732 if (strict_check) {
4733 struct ifinfomsg *ifm;
4734
4735 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
4736 NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump");
4737 return -EINVAL;
4738 }
4739
4740 ifm = nlmsg_data(nlh);
4741 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
4742 ifm->ifi_change || ifm->ifi_index) {
4743 NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request");
4744 return -EINVAL;
4745 }
4746
Johannes Berg8cb08172019-04-26 14:07:28 +02004747 err = nlmsg_parse_deprecated_strict(nlh,
4748 sizeof(struct ifinfomsg),
4749 tb, IFLA_MAX, ifla_policy,
4750 extack);
David Ahern2d011be2018-10-07 20:16:31 -07004751 } else {
Johannes Berg8cb08172019-04-26 14:07:28 +02004752 err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
4753 tb, IFLA_MAX, ifla_policy,
4754 extack);
David Ahern2d011be2018-10-07 20:16:31 -07004755 }
4756 if (err < 0)
4757 return err;
4758
4759 /* new attributes should only be added with strict checking */
4760 for (i = 0; i <= IFLA_MAX; ++i) {
4761 if (!tb[i])
4762 continue;
4763
4764 switch (i) {
4765 case IFLA_EXT_MASK:
4766 *filter_mask = nla_get_u32(tb[i]);
4767 break;
4768 default:
4769 if (strict_check) {
4770 NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request");
4771 return -EINVAL;
4772 }
4773 }
4774 }
4775
4776 return 0;
4777}
4778
John Fastabende5a55a82012-10-24 08:12:57 +00004779static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
4780{
David Ahern2d011be2018-10-07 20:16:31 -07004781 const struct nlmsghdr *nlh = cb->nlh;
John Fastabende5a55a82012-10-24 08:12:57 +00004782 struct net *net = sock_net(skb->sk);
4783 struct net_device *dev;
4784 int idx = 0;
4785 u32 portid = NETLINK_CB(cb->skb).portid;
David Ahern2d011be2018-10-07 20:16:31 -07004786 u32 seq = nlh->nlmsg_seq;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00004787 u32 filter_mask = 0;
Roopa Prabhud64f69b2015-09-15 14:44:29 -07004788 int err;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00004789
David Ahern2d011be2018-10-07 20:16:31 -07004790 err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask,
4791 cb->extack);
4792 if (err < 0 && cb->strict_check)
4793 return err;
John Fastabende5a55a82012-10-24 08:12:57 +00004794
4795 rcu_read_lock();
4796 for_each_netdev_rcu(net, dev) {
4797 const struct net_device_ops *ops = dev->netdev_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00004798 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
John Fastabende5a55a82012-10-24 08:12:57 +00004799
Jiri Pirko898e5062013-01-03 22:48:52 +00004800 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07004801 if (idx >= cb->args[0]) {
4802 err = br_dev->netdev_ops->ndo_bridge_getlink(
4803 skb, portid, seq, dev,
4804 filter_mask, NLM_F_MULTI);
David Ahernf6c57752017-05-15 23:19:17 -07004805 if (err < 0 && err != -EOPNOTSUPP) {
4806 if (likely(skb->len))
4807 break;
4808
4809 goto out_err;
4810 }
Roopa Prabhud64f69b2015-09-15 14:44:29 -07004811 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00004812 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00004813 }
4814
4815 if (ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07004816 if (idx >= cb->args[0]) {
4817 err = ops->ndo_bridge_getlink(skb, portid,
4818 seq, dev,
4819 filter_mask,
4820 NLM_F_MULTI);
David Ahernf6c57752017-05-15 23:19:17 -07004821 if (err < 0 && err != -EOPNOTSUPP) {
4822 if (likely(skb->len))
4823 break;
4824
4825 goto out_err;
4826 }
Roopa Prabhud64f69b2015-09-15 14:44:29 -07004827 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00004828 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00004829 }
4830 }
David Ahernf6c57752017-05-15 23:19:17 -07004831 err = skb->len;
4832out_err:
John Fastabende5a55a82012-10-24 08:12:57 +00004833 rcu_read_unlock();
4834 cb->args[0] = idx;
4835
David Ahernf6c57752017-05-15 23:19:17 -07004836 return err;
John Fastabende5a55a82012-10-24 08:12:57 +00004837}
4838
John Fastabend2469ffd2012-10-24 08:13:03 +00004839static inline size_t bridge_nlmsg_size(void)
4840{
4841 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4842 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4843 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4844 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
4845 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
4846 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
4847 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
4848 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
4849 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
4850 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
4851 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
4852}
4853
Roopa Prabhu02dba432015-01-14 20:02:25 -08004854static int rtnl_bridge_notify(struct net_device *dev)
John Fastabend2469ffd2012-10-24 08:13:03 +00004855{
4856 struct net *net = dev_net(dev);
John Fastabend2469ffd2012-10-24 08:13:03 +00004857 struct sk_buff *skb;
4858 int err = -EOPNOTSUPP;
4859
Roopa Prabhu02dba432015-01-14 20:02:25 -08004860 if (!dev->netdev_ops->ndo_bridge_getlink)
4861 return 0;
4862
John Fastabend2469ffd2012-10-24 08:13:03 +00004863 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
4864 if (!skb) {
4865 err = -ENOMEM;
4866 goto errout;
4867 }
4868
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02004869 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
Roopa Prabhu02dba432015-01-14 20:02:25 -08004870 if (err < 0)
4871 goto errout;
John Fastabend2469ffd2012-10-24 08:13:03 +00004872
Ido Schimmeld2e381c2021-06-09 14:17:53 +03004873 /* Notification info is only filled for bridge ports, not the bridge
4874 * device itself. Therefore, a zero notification length is valid and
4875 * should not result in an error.
4876 */
4877 if (!skb->len)
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08004878 goto errout;
4879
John Fastabend2469ffd2012-10-24 08:13:03 +00004880 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
4881 return 0;
4882errout:
4883 WARN_ON(err == -EMSGSIZE);
4884 kfree_skb(skb);
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08004885 if (err)
4886 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
John Fastabend2469ffd2012-10-24 08:13:03 +00004887 return err;
4888}
4889
David Ahernc21ef3e2017-04-16 09:48:24 -07004890static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
4891 struct netlink_ext_ack *extack)
John Fastabende5a55a82012-10-24 08:12:57 +00004892{
4893 struct net *net = sock_net(skb->sk);
4894 struct ifinfomsg *ifm;
4895 struct net_device *dev;
John Fastabend2469ffd2012-10-24 08:13:03 +00004896 struct nlattr *br_spec, *attr = NULL;
4897 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02004898 u16 flags = 0;
John Fastabendc38e01b2012-11-02 16:32:36 +00004899 bool have_flags = false;
John Fastabende5a55a82012-10-24 08:12:57 +00004900
4901 if (nlmsg_len(nlh) < sizeof(*ifm))
4902 return -EINVAL;
4903
4904 ifm = nlmsg_data(nlh);
4905 if (ifm->ifi_family != AF_BRIDGE)
4906 return -EPFNOSUPPORT;
4907
4908 dev = __dev_get_by_index(net, ifm->ifi_index);
4909 if (!dev) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004910 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabende5a55a82012-10-24 08:12:57 +00004911 return -ENODEV;
4912 }
4913
John Fastabend2469ffd2012-10-24 08:13:03 +00004914 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4915 if (br_spec) {
4916 nla_for_each_nested(attr, br_spec, rem) {
4917 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01004918 if (nla_len(attr) < sizeof(flags))
4919 return -EINVAL;
4920
John Fastabendc38e01b2012-11-02 16:32:36 +00004921 have_flags = true;
John Fastabend2469ffd2012-10-24 08:13:03 +00004922 flags = nla_get_u16(attr);
4923 break;
4924 }
4925 }
4926 }
4927
4928 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00004929 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4930
4931 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
John Fastabend2469ffd2012-10-24 08:13:03 +00004932 err = -EOPNOTSUPP;
4933 goto out;
4934 }
4935
Petr Machata2fd527b2018-12-12 17:02:48 +00004936 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags,
4937 extack);
John Fastabende5a55a82012-10-24 08:12:57 +00004938 if (err)
4939 goto out;
John Fastabend2469ffd2012-10-24 08:13:03 +00004940
4941 flags &= ~BRIDGE_FLAGS_MASTER;
John Fastabende5a55a82012-10-24 08:12:57 +00004942 }
4943
John Fastabend2469ffd2012-10-24 08:13:03 +00004944 if ((flags & BRIDGE_FLAGS_SELF)) {
4945 if (!dev->netdev_ops->ndo_bridge_setlink)
4946 err = -EOPNOTSUPP;
4947 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08004948 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
Petr Machata2fd527b2018-12-12 17:02:48 +00004949 flags,
4950 extack);
Roopa Prabhu02dba432015-01-14 20:02:25 -08004951 if (!err) {
John Fastabend2469ffd2012-10-24 08:13:03 +00004952 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08004953
4954 /* Generate event to notify upper layer of bridge
4955 * change
4956 */
4957 err = rtnl_bridge_notify(dev);
4958 }
John Fastabend2469ffd2012-10-24 08:13:03 +00004959 }
4960
John Fastabendc38e01b2012-11-02 16:32:36 +00004961 if (have_flags)
John Fastabend2469ffd2012-10-24 08:13:03 +00004962 memcpy(nla_data(attr), &flags, sizeof(flags));
John Fastabende5a55a82012-10-24 08:12:57 +00004963out:
4964 return err;
4965}
4966
David Ahernc21ef3e2017-04-16 09:48:24 -07004967static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
4968 struct netlink_ext_ack *extack)
Vlad Yasevich407af322013-02-13 12:00:12 +00004969{
4970 struct net *net = sock_net(skb->sk);
4971 struct ifinfomsg *ifm;
4972 struct net_device *dev;
4973 struct nlattr *br_spec, *attr = NULL;
4974 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02004975 u16 flags = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +00004976 bool have_flags = false;
4977
4978 if (nlmsg_len(nlh) < sizeof(*ifm))
4979 return -EINVAL;
4980
4981 ifm = nlmsg_data(nlh);
4982 if (ifm->ifi_family != AF_BRIDGE)
4983 return -EPFNOSUPPORT;
4984
4985 dev = __dev_get_by_index(net, ifm->ifi_index);
4986 if (!dev) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004987 NL_SET_ERR_MSG(extack, "unknown ifindex");
Vlad Yasevich407af322013-02-13 12:00:12 +00004988 return -ENODEV;
4989 }
4990
4991 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4992 if (br_spec) {
4993 nla_for_each_nested(attr, br_spec, rem) {
4994 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01004995 if (nla_len(attr) < sizeof(flags))
4996 return -EINVAL;
4997
Vlad Yasevich407af322013-02-13 12:00:12 +00004998 have_flags = true;
4999 flags = nla_get_u16(attr);
5000 break;
5001 }
5002 }
5003 }
5004
Vlad Yasevich407af322013-02-13 12:00:12 +00005005 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
5006 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
5007
5008 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
5009 err = -EOPNOTSUPP;
5010 goto out;
5011 }
5012
Roopa Prabhuadd511b2015-01-29 22:40:12 -08005013 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00005014 if (err)
5015 goto out;
5016
5017 flags &= ~BRIDGE_FLAGS_MASTER;
5018 }
5019
5020 if ((flags & BRIDGE_FLAGS_SELF)) {
5021 if (!dev->netdev_ops->ndo_bridge_dellink)
5022 err = -EOPNOTSUPP;
5023 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08005024 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
5025 flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00005026
Roopa Prabhu02dba432015-01-14 20:02:25 -08005027 if (!err) {
Vlad Yasevich407af322013-02-13 12:00:12 +00005028 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08005029
5030 /* Generate event to notify upper layer of bridge
5031 * change
5032 */
5033 err = rtnl_bridge_notify(dev);
5034 }
Vlad Yasevich407af322013-02-13 12:00:12 +00005035 }
5036
5037 if (have_flags)
5038 memcpy(nla_data(attr), &flags, sizeof(flags));
Vlad Yasevich407af322013-02-13 12:00:12 +00005039out:
5040 return err;
5041}
5042
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005043static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
5044{
5045 return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
5046 (!idxattr || idxattr == attrid);
5047}
5048
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02005049#define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
5050static int rtnl_get_offload_stats_attr_size(int attr_id)
5051{
5052 switch (attr_id) {
5053 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
5054 return sizeof(struct rtnl_link_stats64);
5055 }
5056
5057 return 0;
5058}
5059
5060static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
5061 int *prividx)
5062{
5063 struct nlattr *attr = NULL;
5064 int attr_id, size;
5065 void *attr_data;
5066 int err;
5067
5068 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
5069 dev->netdev_ops->ndo_get_offload_stats))
5070 return -ENODATA;
5071
5072 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
5073 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
5074 if (attr_id < *prividx)
5075 continue;
5076
5077 size = rtnl_get_offload_stats_attr_size(attr_id);
5078 if (!size)
5079 continue;
5080
Or Gerlitz3df5b3c2016-11-22 23:09:54 +02005081 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02005082 continue;
5083
5084 attr = nla_reserve_64bit(skb, attr_id, size,
5085 IFLA_OFFLOAD_XSTATS_UNSPEC);
5086 if (!attr)
5087 goto nla_put_failure;
5088
5089 attr_data = nla_data(attr);
5090 memset(attr_data, 0, size);
5091 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
5092 attr_data);
5093 if (err)
5094 goto get_offload_stats_failure;
5095 }
5096
5097 if (!attr)
5098 return -ENODATA;
5099
5100 *prividx = 0;
5101 return 0;
5102
5103nla_put_failure:
5104 err = -EMSGSIZE;
5105get_offload_stats_failure:
5106 *prividx = attr_id;
5107 return err;
5108}
5109
5110static int rtnl_get_offload_stats_size(const struct net_device *dev)
5111{
5112 int nla_size = 0;
5113 int attr_id;
5114 int size;
5115
5116 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
5117 dev->netdev_ops->ndo_get_offload_stats))
5118 return 0;
5119
5120 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
5121 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
Or Gerlitz3df5b3c2016-11-22 23:09:54 +02005122 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02005123 continue;
5124 size = rtnl_get_offload_stats_attr_size(attr_id);
5125 nla_size += nla_total_size_64bit(size);
5126 }
5127
5128 if (nla_size != 0)
5129 nla_size += nla_total_size(0);
5130
5131 return nla_size;
5132}
5133
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005134static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
5135 int type, u32 pid, u32 seq, u32 change,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005136 unsigned int flags, unsigned int filter_mask,
5137 int *idxattr, int *prividx)
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005138{
5139 struct if_stats_msg *ifsm;
5140 struct nlmsghdr *nlh;
5141 struct nlattr *attr;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005142 int s_prividx = *prividx;
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02005143 int err;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005144
5145 ASSERT_RTNL();
5146
5147 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
5148 if (!nlh)
5149 return -EMSGSIZE;
5150
5151 ifsm = nlmsg_data(nlh);
Nikolay Aleksandrovce024f42017-10-03 13:20:48 +03005152 ifsm->family = PF_UNSPEC;
5153 ifsm->pad1 = 0;
5154 ifsm->pad2 = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005155 ifsm->ifindex = dev->ifindex;
5156 ifsm->filter_mask = filter_mask;
5157
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005158 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005159 struct rtnl_link_stats64 *sp;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005160
Nicolas Dichtel58414d32016-04-21 18:58:25 +02005161 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
5162 sizeof(struct rtnl_link_stats64),
5163 IFLA_STATS_UNSPEC);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005164 if (!attr)
5165 goto nla_put_failure;
5166
5167 sp = nla_data(attr);
5168 dev_get_stats(dev, sp);
5169 }
5170
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02005171 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
5172 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
5173
5174 if (ops && ops->fill_linkxstats) {
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02005175 *idxattr = IFLA_STATS_LINK_XSTATS;
Michal Kubecekae0be8d2019-04-26 11:13:06 +02005176 attr = nla_nest_start_noflag(skb,
5177 IFLA_STATS_LINK_XSTATS);
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02005178 if (!attr)
5179 goto nla_put_failure;
5180
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02005181 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
5182 nla_nest_end(skb, attr);
5183 if (err)
5184 goto nla_put_failure;
5185 *idxattr = 0;
5186 }
5187 }
5188
5189 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
5190 *idxattr)) {
5191 const struct rtnl_link_ops *ops = NULL;
5192 const struct net_device *master;
5193
5194 master = netdev_master_upper_dev_get(dev);
5195 if (master)
5196 ops = master->rtnl_link_ops;
5197 if (ops && ops->fill_linkxstats) {
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02005198 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
Michal Kubecekae0be8d2019-04-26 11:13:06 +02005199 attr = nla_nest_start_noflag(skb,
5200 IFLA_STATS_LINK_XSTATS_SLAVE);
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02005201 if (!attr)
5202 goto nla_put_failure;
5203
5204 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02005205 nla_nest_end(skb, attr);
5206 if (err)
5207 goto nla_put_failure;
5208 *idxattr = 0;
5209 }
5210 }
5211
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02005212 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
5213 *idxattr)) {
5214 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
Michal Kubecekae0be8d2019-04-26 11:13:06 +02005215 attr = nla_nest_start_noflag(skb,
5216 IFLA_STATS_LINK_OFFLOAD_XSTATS);
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02005217 if (!attr)
5218 goto nla_put_failure;
5219
5220 err = rtnl_get_offload_stats(skb, dev, prividx);
5221 if (err == -ENODATA)
5222 nla_nest_cancel(skb, attr);
5223 else
5224 nla_nest_end(skb, attr);
5225
5226 if (err && err != -ENODATA)
5227 goto nla_put_failure;
5228 *idxattr = 0;
5229 }
5230
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005231 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
5232 struct rtnl_af_ops *af_ops;
5233
5234 *idxattr = IFLA_STATS_AF_SPEC;
Michal Kubecekae0be8d2019-04-26 11:13:06 +02005235 attr = nla_nest_start_noflag(skb, IFLA_STATS_AF_SPEC);
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005236 if (!attr)
5237 goto nla_put_failure;
5238
Florian Westphal5fa85a02017-10-16 15:44:36 +02005239 rcu_read_lock();
5240 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005241 if (af_ops->fill_stats_af) {
5242 struct nlattr *af;
5243 int err;
5244
Michal Kubecekae0be8d2019-04-26 11:13:06 +02005245 af = nla_nest_start_noflag(skb,
5246 af_ops->family);
Florian Westphal5fa85a02017-10-16 15:44:36 +02005247 if (!af) {
5248 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005249 goto nla_put_failure;
Florian Westphal5fa85a02017-10-16 15:44:36 +02005250 }
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005251 err = af_ops->fill_stats_af(skb, dev);
5252
Florian Westphal5fa85a02017-10-16 15:44:36 +02005253 if (err == -ENODATA) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005254 nla_nest_cancel(skb, af);
Florian Westphal5fa85a02017-10-16 15:44:36 +02005255 } else if (err < 0) {
5256 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005257 goto nla_put_failure;
Florian Westphal5fa85a02017-10-16 15:44:36 +02005258 }
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005259
5260 nla_nest_end(skb, af);
5261 }
5262 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02005263 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005264
5265 nla_nest_end(skb, attr);
5266
5267 *idxattr = 0;
5268 }
5269
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005270 nlmsg_end(skb, nlh);
5271
5272 return 0;
5273
5274nla_put_failure:
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005275 /* not a multi message or no progress mean a real error */
5276 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
5277 nlmsg_cancel(skb, nlh);
5278 else
5279 nlmsg_end(skb, nlh);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005280
5281 return -EMSGSIZE;
5282}
5283
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005284static size_t if_nlmsg_stats_size(const struct net_device *dev,
5285 u32 filter_mask)
5286{
Eric Dumazetd3436792021-10-05 14:04:17 -07005287 size_t size = NLMSG_ALIGN(sizeof(struct if_stats_msg));
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005288
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005289 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005290 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
5291
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02005292 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
5293 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02005294 int attr = IFLA_STATS_LINK_XSTATS;
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02005295
5296 if (ops && ops->get_linkxstats_size) {
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02005297 size += nla_total_size(ops->get_linkxstats_size(dev,
5298 attr));
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02005299 /* for IFLA_STATS_LINK_XSTATS */
5300 size += nla_total_size(0);
5301 }
5302 }
5303
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02005304 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
5305 struct net_device *_dev = (struct net_device *)dev;
5306 const struct rtnl_link_ops *ops = NULL;
5307 const struct net_device *master;
5308
5309 /* netdev_master_upper_dev_get can't take const */
5310 master = netdev_master_upper_dev_get(_dev);
5311 if (master)
5312 ops = master->rtnl_link_ops;
5313 if (ops && ops->get_linkxstats_size) {
5314 int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
5315
5316 size += nla_total_size(ops->get_linkxstats_size(dev,
5317 attr));
5318 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
5319 size += nla_total_size(0);
5320 }
5321 }
5322
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02005323 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
5324 size += rtnl_get_offload_stats_size(dev);
5325
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005326 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
5327 struct rtnl_af_ops *af_ops;
5328
5329 /* for IFLA_STATS_AF_SPEC */
5330 size += nla_total_size(0);
5331
Florian Westphal5fa85a02017-10-16 15:44:36 +02005332 rcu_read_lock();
5333 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005334 if (af_ops->get_stats_af_size) {
5335 size += nla_total_size(
5336 af_ops->get_stats_af_size(dev));
5337
5338 /* for AF_* */
5339 size += nla_total_size(0);
5340 }
5341 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02005342 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005343 }
5344
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005345 return size;
5346}
5347
Jakub Kicinski51bc8602019-01-18 10:46:14 -08005348static int rtnl_valid_stats_req(const struct nlmsghdr *nlh, bool strict_check,
5349 bool is_dump, struct netlink_ext_ack *extack)
5350{
5351 struct if_stats_msg *ifsm;
5352
Eric Dumazet69f23a02019-04-14 11:02:05 -07005353 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifsm))) {
Jakub Kicinski51bc8602019-01-18 10:46:14 -08005354 NL_SET_ERR_MSG(extack, "Invalid header for stats dump");
5355 return -EINVAL;
5356 }
5357
5358 if (!strict_check)
5359 return 0;
5360
5361 ifsm = nlmsg_data(nlh);
5362
5363 /* only requests using strict checks can pass data to influence
5364 * the dump. The legacy exception is filter_mask.
5365 */
5366 if (ifsm->pad1 || ifsm->pad2 || (is_dump && ifsm->ifindex)) {
5367 NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request");
5368 return -EINVAL;
5369 }
5370 if (nlmsg_attrlen(nlh, sizeof(*ifsm))) {
5371 NL_SET_ERR_MSG(extack, "Invalid attributes after stats header");
5372 return -EINVAL;
5373 }
Jakub Kicinski6300acb2019-01-18 10:46:15 -08005374 if (ifsm->filter_mask >= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX + 1)) {
5375 NL_SET_ERR_MSG(extack, "Invalid stats requested through filter mask");
5376 return -EINVAL;
5377 }
Jakub Kicinski51bc8602019-01-18 10:46:14 -08005378
5379 return 0;
5380}
5381
David Ahernc21ef3e2017-04-16 09:48:24 -07005382static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
5383 struct netlink_ext_ack *extack)
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005384{
5385 struct net *net = sock_net(skb->sk);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005386 struct net_device *dev = NULL;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005387 int idxattr = 0, prividx = 0;
5388 struct if_stats_msg *ifsm;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005389 struct sk_buff *nskb;
5390 u32 filter_mask;
5391 int err;
5392
Jakub Kicinski51bc8602019-01-18 10:46:14 -08005393 err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb),
5394 false, extack);
5395 if (err)
5396 return err;
Mathias Krause4775cc12016-12-28 17:52:15 +01005397
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005398 ifsm = nlmsg_data(nlh);
5399 if (ifsm->ifindex > 0)
5400 dev = __dev_get_by_index(net, ifsm->ifindex);
5401 else
5402 return -EINVAL;
5403
5404 if (!dev)
5405 return -ENODEV;
5406
5407 filter_mask = ifsm->filter_mask;
5408 if (!filter_mask)
5409 return -EINVAL;
5410
5411 nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
5412 if (!nskb)
5413 return -ENOBUFS;
5414
5415 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
5416 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005417 0, filter_mask, &idxattr, &prividx);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005418 if (err < 0) {
5419 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
5420 WARN_ON(err == -EMSGSIZE);
5421 kfree_skb(nskb);
5422 } else {
5423 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
5424 }
5425
5426 return err;
5427}
5428
5429static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
5430{
David Ahern841891e2018-10-07 20:16:32 -07005431 struct netlink_ext_ack *extack = cb->extack;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005432 int h, s_h, err, s_idx, s_idxattr, s_prividx;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005433 struct net *net = sock_net(skb->sk);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005434 unsigned int flags = NLM_F_MULTI;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005435 struct if_stats_msg *ifsm;
5436 struct hlist_head *head;
5437 struct net_device *dev;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005438 u32 filter_mask = 0;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005439 int idx = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005440
5441 s_h = cb->args[0];
5442 s_idx = cb->args[1];
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005443 s_idxattr = cb->args[2];
5444 s_prividx = cb->args[3];
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005445
5446 cb->seq = net->dev_base_seq;
5447
Jakub Kicinski51bc8602019-01-18 10:46:14 -08005448 err = rtnl_valid_stats_req(cb->nlh, cb->strict_check, true, extack);
5449 if (err)
5450 return err;
Mathias Krause4775cc12016-12-28 17:52:15 +01005451
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005452 ifsm = nlmsg_data(cb->nlh);
5453 filter_mask = ifsm->filter_mask;
David Ahern841891e2018-10-07 20:16:32 -07005454 if (!filter_mask) {
5455 NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump");
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005456 return -EINVAL;
David Ahern841891e2018-10-07 20:16:32 -07005457 }
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005458
5459 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5460 idx = 0;
5461 head = &net->dev_index_head[h];
5462 hlist_for_each_entry(dev, head, index_hlist) {
5463 if (idx < s_idx)
5464 goto cont;
5465 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
5466 NETLINK_CB(cb->skb).portid,
5467 cb->nlh->nlmsg_seq, 0,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005468 flags, filter_mask,
5469 &s_idxattr, &s_prividx);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005470 /* If we ran out of room on the first message,
5471 * we're in trouble
5472 */
5473 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
5474
5475 if (err < 0)
5476 goto out;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005477 s_prividx = 0;
5478 s_idxattr = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005479 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
5480cont:
5481 idx++;
5482 }
5483 }
5484out:
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005485 cb->args[3] = s_prividx;
5486 cb->args[2] = s_idxattr;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005487 cb->args[1] = idx;
5488 cb->args[0] = h;
5489
5490 return skb->len;
5491}
5492
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493/* Process one rtnetlink message. */
5494
Johannes Berg2d4bc932017-04-12 14:34:04 +02005495static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
5496 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09005498 struct net *net = sock_net(skb->sk);
Florian Westphaladdf9b92017-12-02 21:44:05 +01005499 struct rtnl_link *link;
Florian Westphale4202512017-12-02 21:44:06 +01005500 struct module *owner;
Florian Westphal6853dd42017-08-09 20:41:51 +02005501 int err = -EOPNOTSUPP;
Thomas Grafe2849862007-03-22 11:48:11 -07005502 rtnl_doit_func doit;
Florian Westphal62256f92017-08-09 20:41:52 +02005503 unsigned int flags;
Alexander Kuleshov617cfc72016-01-10 21:26:57 +06005504 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 int family;
5506 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509 if (type > RTM_MAX)
Thomas Graf038890f2007-04-05 14:35:52 -07005510 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511
5512 type -= RTM_BASE;
5513
5514 /* All the messages must have at least 1 byte length */
Hong zhi guo573ce262013-03-27 06:47:04 +00005515 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 return 0;
5517
Hong zhi guo573ce262013-03-27 06:47:04 +00005518 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 kind = type&3;
5520
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07005521 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07005522 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523
Florian Westphal6853dd42017-08-09 20:41:51 +02005524 rcu_read_lock();
David S. Millerb8f3ab42011-01-18 12:40:38 -08005525 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005526 struct sock *rtnl;
Thomas Grafe2849862007-03-22 11:48:11 -07005527 rtnl_dumpit_func dumpit;
Di Zhuebfe3c52020-10-21 10:00:53 +08005528 u32 min_dump_alloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529
Florian Westphaladdf9b92017-12-02 21:44:05 +01005530 link = rtnl_get_link(family, type);
5531 if (!link || !link->dumpit) {
Florian Westphal6853dd42017-08-09 20:41:51 +02005532 family = PF_UNSPEC;
Florian Westphaladdf9b92017-12-02 21:44:05 +01005533 link = rtnl_get_link(family, type);
5534 if (!link || !link->dumpit)
Florian Westphal6853dd42017-08-09 20:41:51 +02005535 goto err_unlock;
5536 }
Florian Westphale4202512017-12-02 21:44:06 +01005537 owner = link->owner;
Florian Westphaladdf9b92017-12-02 21:44:05 +01005538 dumpit = link->dumpit;
Florian Westphale1fa6d22017-08-09 20:41:47 +02005539
Florian Westphal5c2bb9b2017-08-10 16:52:58 +02005540 if (type == RTM_GETLINK - RTM_BASE)
Florian Westphale1fa6d22017-08-09 20:41:47 +02005541 min_dump_alloc = rtnl_calcit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542
Florian Westphale4202512017-12-02 21:44:06 +01005543 err = 0;
5544 /* need to do this before rcu_read_unlock() */
5545 if (!try_module_get(owner))
5546 err = -EPROTONOSUPPORT;
5547
Florian Westphal6853dd42017-08-09 20:41:51 +02005548 rcu_read_unlock();
5549
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005550 rtnl = net->rtnl;
Florian Westphale4202512017-12-02 21:44:06 +01005551 if (err == 0) {
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00005552 struct netlink_dump_control c = {
5553 .dump = dumpit,
5554 .min_dump_alloc = min_dump_alloc,
Florian Westphale4202512017-12-02 21:44:06 +01005555 .module = owner,
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00005556 };
5557 err = netlink_dump_start(rtnl, skb, nlh, &c);
Florian Westphale4202512017-12-02 21:44:06 +01005558 /* netlink_dump_start() will keep a reference on
5559 * module if dump is still in progress.
5560 */
5561 module_put(owner);
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00005562 }
Eric Dumazet2907c352011-05-25 07:34:04 +00005563 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564 }
5565
Florian Westphaladdf9b92017-12-02 21:44:05 +01005566 link = rtnl_get_link(family, type);
5567 if (!link || !link->doit) {
Florian Westphal8caa38b2017-08-10 16:53:01 +02005568 family = PF_UNSPEC;
Florian Westphaladdf9b92017-12-02 21:44:05 +01005569 link = rtnl_get_link(PF_UNSPEC, type);
5570 if (!link || !link->doit)
5571 goto out_unlock;
Florian Westphal8caa38b2017-08-10 16:53:01 +02005572 }
5573
Florian Westphale4202512017-12-02 21:44:06 +01005574 owner = link->owner;
5575 if (!try_module_get(owner)) {
5576 err = -EPROTONOSUPPORT;
5577 goto out_unlock;
5578 }
5579
Florian Westphaladdf9b92017-12-02 21:44:05 +01005580 flags = link->flags;
Florian Westphal62256f92017-08-09 20:41:52 +02005581 if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
Florian Westphaladdf9b92017-12-02 21:44:05 +01005582 doit = link->doit;
Florian Westphal62256f92017-08-09 20:41:52 +02005583 rcu_read_unlock();
5584 if (doit)
5585 err = doit(skb, nlh, extack);
Florian Westphale4202512017-12-02 21:44:06 +01005586 module_put(owner);
Florian Westphal62256f92017-08-09 20:41:52 +02005587 return err;
5588 }
Florian Westphal6853dd42017-08-09 20:41:51 +02005589 rcu_read_unlock();
5590
Florian Westphal0cc09022017-08-09 20:41:50 +02005591 rtnl_lock();
Florian Westphaladdf9b92017-12-02 21:44:05 +01005592 link = rtnl_get_link(family, type);
5593 if (link && link->doit)
5594 err = link->doit(skb, nlh, extack);
Florian Westphal0cc09022017-08-09 20:41:50 +02005595 rtnl_unlock();
Florian Westphaladdf9b92017-12-02 21:44:05 +01005596
Florian Westphale4202512017-12-02 21:44:06 +01005597 module_put(owner);
5598
Florian Westphaladdf9b92017-12-02 21:44:05 +01005599 return err;
5600
5601out_unlock:
5602 rcu_read_unlock();
Florian Westphal0cc09022017-08-09 20:41:50 +02005603 return err;
5604
5605err_unlock:
Florian Westphal6853dd42017-08-09 20:41:51 +02005606 rcu_read_unlock();
Florian Westphal0cc09022017-08-09 20:41:50 +02005607 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608}
5609
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07005610static void rtnetlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07005612 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613}
5614
Julien Gomes5f729ea2017-06-20 13:54:16 -07005615static int rtnetlink_bind(struct net *net, int group)
5616{
5617 switch (group) {
5618 case RTNLGRP_IPV4_MROUTE_R:
5619 case RTNLGRP_IPV6_MROUTE_R:
5620 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
5621 return -EPERM;
5622 break;
5623 }
5624 return 0;
5625}
5626
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
5628{
Jiri Pirko351638e2013-05-28 01:30:21 +00005629 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02005630
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631 switch (event) {
Vlad Yasevich5138e862017-04-04 09:23:41 -04005632 case NETDEV_REBOOT:
Xin Long8a212582017-10-15 18:13:41 +08005633 case NETDEV_CHANGEMTU:
David Ahern37536542017-07-19 10:22:40 -07005634 case NETDEV_CHANGEADDR:
Vlad Yasevich5138e862017-04-04 09:23:41 -04005635 case NETDEV_CHANGENAME:
5636 case NETDEV_FEAT_CHANGE:
5637 case NETDEV_BONDING_FAILOVER:
Xin Longe6e66592017-10-15 18:13:43 +08005638 case NETDEV_POST_TYPE_CHANGE:
Vlad Yasevich5138e862017-04-04 09:23:41 -04005639 case NETDEV_NOTIFY_PEERS:
Xin Longdc709f32017-10-15 18:13:44 +08005640 case NETDEV_CHANGEUPPER:
Vlad Yasevich5138e862017-04-04 09:23:41 -04005641 case NETDEV_RESEND_IGMP:
Vlad Yasevich5138e862017-04-04 09:23:41 -04005642 case NETDEV_CHANGEINFODATA:
Xin Longeeda3fb2017-10-24 13:54:19 +08005643 case NETDEV_CHANGELOWERSTATE:
Xin Longebdcf042017-10-15 18:13:42 +08005644 case NETDEV_CHANGE_TX_QUEUE_LEN:
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04005645 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01005646 GFP_KERNEL, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 break;
5648 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649 break;
5650 }
5651 return NOTIFY_DONE;
5652}
5653
5654static struct notifier_block rtnetlink_dev_notifier = {
5655 .notifier_call = rtnetlink_event,
5656};
5657
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005658
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005659static int __net_init rtnetlink_net_init(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005660{
5661 struct sock *sk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00005662 struct netlink_kernel_cfg cfg = {
5663 .groups = RTNLGRP_MAX,
5664 .input = rtnetlink_rcv,
5665 .cb_mutex = &rtnl_mutex,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00005666 .flags = NL_CFG_F_NONROOT_RECV,
Julien Gomes5f729ea2017-06-20 13:54:16 -07005667 .bind = rtnetlink_bind,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00005668 };
5669
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00005670 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005671 if (!sk)
5672 return -ENOMEM;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005673 net->rtnl = sk;
5674 return 0;
5675}
5676
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005677static void __net_exit rtnetlink_net_exit(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005678{
Denis V. Lunev775516b2008-01-18 23:55:19 -08005679 netlink_kernel_release(net->rtnl);
5680 net->rtnl = NULL;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005681}
5682
5683static struct pernet_operations rtnetlink_net_ops = {
5684 .init = rtnetlink_net_init,
5685 .exit = rtnetlink_net_exit,
5686};
5687
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688void __init rtnetlink_init(void)
5689{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005690 if (register_pernet_subsys(&rtnetlink_net_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691 panic("rtnetlink_init: cannot initialize rtnetlink\n");
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005692
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693 register_netdevice_notifier(&rtnetlink_dev_notifier);
Thomas Graf340d17f2007-03-22 11:49:22 -07005694
Greg Rosec7ac8672011-06-10 01:27:09 +00005695 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
Florian Westphalb97bac62017-08-09 20:41:48 +02005696 rtnl_dump_ifinfo, 0);
5697 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
5698 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
5699 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
Thomas Graf687ad8c2007-03-22 11:59:42 -07005700
Florian Westphalb97bac62017-08-09 20:41:48 +02005701 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
5702 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
5703 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
John Fastabend77162022012-04-15 06:43:56 +00005704
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02005705 rtnl_register(PF_UNSPEC, RTM_NEWLINKPROP, rtnl_newlinkprop, NULL, 0);
5706 rtnl_register(PF_UNSPEC, RTM_DELLINKPROP, rtnl_dellinkprop, NULL, 0);
5707
Florian Westphalb97bac62017-08-09 20:41:48 +02005708 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
5709 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
Roopa Prabhu5b2f94b2018-12-15 22:35:08 -08005710 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0);
John Fastabende5a55a82012-10-24 08:12:57 +00005711
Florian Westphalb97bac62017-08-09 20:41:48 +02005712 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
5713 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
5714 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005715
5716 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
Florian Westphalb97bac62017-08-09 20:41:48 +02005717 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718}