blob: 564d24c451af7c58e9fab5c3a1178f1026bf86bc [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) {
845 write_lock_bh(&dev_base_lock);
846 dev->operstate = operstate;
847 write_unlock_bh(&dev_base_lock);
David S. Miller93b2d4a2008-02-17 18:35:07 -0800848 netdev_state_change(dev);
849 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800850}
851
Jiri Bencb1beb682012-07-27 02:58:22 +0000852static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
853{
854 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
855 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
856}
857
Patrick McHardy3729d502010-02-26 06:34:54 +0000858static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
859 const struct ifinfomsg *ifm)
860{
861 unsigned int flags = ifm->ifi_flags;
862
863 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
864 if (ifm->ifi_change)
865 flags = (flags & ifm->ifi_change) |
Jiri Bencb1beb682012-07-27 02:58:22 +0000866 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
Patrick McHardy3729d502010-02-26 06:34:54 +0000867
868 return flags;
869}
870
Thomas Grafb60c5112006-08-04 23:05:34 -0700871static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000872 const struct rtnl_link_stats64 *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
Thomas Grafb60c5112006-08-04 23:05:34 -0700874 a->rx_packets = b->rx_packets;
875 a->tx_packets = b->tx_packets;
876 a->rx_bytes = b->rx_bytes;
877 a->tx_bytes = b->tx_bytes;
878 a->rx_errors = b->rx_errors;
879 a->tx_errors = b->tx_errors;
880 a->rx_dropped = b->rx_dropped;
881 a->tx_dropped = b->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Thomas Grafb60c5112006-08-04 23:05:34 -0700883 a->multicast = b->multicast;
884 a->collisions = b->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Thomas Grafb60c5112006-08-04 23:05:34 -0700886 a->rx_length_errors = b->rx_length_errors;
887 a->rx_over_errors = b->rx_over_errors;
888 a->rx_crc_errors = b->rx_crc_errors;
889 a->rx_frame_errors = b->rx_frame_errors;
890 a->rx_fifo_errors = b->rx_fifo_errors;
891 a->rx_missed_errors = b->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Thomas Grafb60c5112006-08-04 23:05:34 -0700893 a->tx_aborted_errors = b->tx_aborted_errors;
894 a->tx_carrier_errors = b->tx_carrier_errors;
895 a->tx_fifo_errors = b->tx_fifo_errors;
896 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
897 a->tx_window_errors = b->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Thomas Grafb60c5112006-08-04 23:05:34 -0700899 a->rx_compressed = b->rx_compressed;
900 a->tx_compressed = b->tx_compressed;
Jarod Wilson6e7333d2016-02-01 18:51:05 -0500901
902 a->rx_nohandler = b->rx_nohandler;
Jan Engelhardt10708f32010-03-11 09:57:29 +0000903}
904
Chris Wrightc02db8c2010-05-16 01:05:45 -0700905/* All VF info */
Greg Rose115c9b82012-02-21 16:54:48 -0500906static inline int rtnl_vfinfo_size(const struct net_device *dev,
907 u32 ext_filter_mask)
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000908{
Phil Sutter9af15c32017-01-18 14:04:39 +0100909 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
Chris Wrightc02db8c2010-05-16 01:05:45 -0700910 int num_vfs = dev_num_vf(dev->dev.parent);
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100911 size_t size = nla_total_size(0);
Scott Feldman045de012010-05-28 03:42:43 -0700912 size += num_vfs *
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100913 (nla_total_size(0) +
914 nla_total_size(sizeof(struct ifla_vf_mac)) +
Denis Kirjanov75345f882019-06-17 10:53:41 +0200915 nla_total_size(sizeof(struct ifla_vf_broadcast)) +
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100916 nla_total_size(sizeof(struct ifla_vf_vlan)) +
917 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
Moshe Shemesh79aab092016-09-22 12:11:15 +0300918 nla_total_size(MAX_VLAN_LIST_LEN *
919 sizeof(struct ifla_vf_vlan_info)) +
Sucheta Chakrabortyed616682014-05-22 09:59:05 -0400920 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100921 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
Jiri Benc945a3672014-08-08 16:44:32 +0200922 nla_total_size(sizeof(struct ifla_vf_rate)) +
Vlad Zolotarov01a3d792015-03-30 21:35:23 +0300923 nla_total_size(sizeof(struct ifla_vf_link_state)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300924 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100925 nla_total_size(0) + /* nest IFLA_VF_STATS */
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300926 /* IFLA_VF_STATS_RX_PACKETS */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200927 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300928 /* IFLA_VF_STATS_TX_PACKETS */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200929 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300930 /* IFLA_VF_STATS_RX_BYTES */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200931 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300932 /* IFLA_VF_STATS_TX_BYTES */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200933 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300934 /* IFLA_VF_STATS_BROADCAST */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200935 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300936 /* IFLA_VF_STATS_MULTICAST */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200937 nla_total_size_64bit(sizeof(__u64)) +
Eugenia Emantayevc5a9f6f2017-07-17 13:47:07 +0300938 /* IFLA_VF_STATS_RX_DROPPED */
939 nla_total_size_64bit(sizeof(__u64)) +
940 /* IFLA_VF_STATS_TX_DROPPED */
941 nla_total_size_64bit(sizeof(__u64)) +
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +0000942 nla_total_size(sizeof(struct ifla_vf_trust)));
Chris Wrightc02db8c2010-05-16 01:05:45 -0700943 return size;
944 } else
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000945 return 0;
946}
947
David Gibsonc53864f2014-04-24 10:22:36 +1000948static size_t rtnl_port_size(const struct net_device *dev,
949 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -0700950{
951 size_t port_size = nla_total_size(4) /* PORT_VF */
952 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
Scott Feldman57b61082010-05-17 22:49:55 -0700953 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
954 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
955 + nla_total_size(1) /* PROT_VDP_REQUEST */
956 + nla_total_size(2); /* PORT_VDP_RESPONSE */
957 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
958 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
959 + port_size;
960 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
961 + port_size;
962
David Gibsonc53864f2014-04-24 10:22:36 +1000963 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
964 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -0700965 return 0;
966 if (dev_num_vf(dev->dev.parent))
967 return port_self_size + vf_ports_size +
968 vf_port_size * dev_num_vf(dev->dev.parent);
969 else
970 return port_self_size;
971}
972
David S. Millerb5cdae32017-04-18 15:36:58 -0400973static size_t rtnl_xdp_size(void)
Brenden Blancod1fdd912016-07-19 12:16:49 -0700974{
Sabrina Dubrocab3cfaa32016-11-15 11:16:35 +0100975 size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */
Martin KaFai Lau58038692017-06-15 17:29:09 -0700976 nla_total_size(1) + /* XDP_ATTACHED */
Jakub Kicinskia25717d2018-07-11 20:36:41 -0700977 nla_total_size(4) + /* XDP_PROG_ID (or 1st mode) */
Jakub Kicinski4f91da22018-07-11 20:36:38 -0700978 nla_total_size(4); /* XDP_<mode>_PROG_ID */
Brenden Blancod1fdd912016-07-19 12:16:49 -0700979
David S. Millerb5cdae32017-04-18 15:36:58 -0400980 return xdp_size;
Brenden Blancod1fdd912016-07-19 12:16:49 -0700981}
982
Jiri Pirko88f4fb02019-09-30 11:48:17 +0200983static size_t rtnl_prop_list_size(const struct net_device *dev)
984{
985 struct netdev_name_node *name_node;
986 size_t size;
987
988 if (list_empty(&dev->name_node->list))
989 return 0;
990 size = nla_total_size(0);
991 list_for_each_entry(name_node, &dev->name_node->list, list)
992 size += nla_total_size(ALTIFNAMSIZ);
993 return size;
994}
995
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 */
Thomas Graf339bf982006-11-10 14:10:15 -08001029 + nla_total_size(1) /* IFLA_OPERSTATE */
Patrick McHardy38f7b872007-06-13 12:03:51 -07001030 + nla_total_size(1) /* IFLA_LINKMODE */
david decotigny2d3b4792014-03-29 09:48:35 -07001031 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001032 + nla_total_size(4) /* IFLA_LINK_NETNSID */
Serhey Popovychdb833d42017-06-20 14:35:23 +03001033 + nla_total_size(4) /* IFLA_GROUP */
Greg Rose115c9b82012-02-21 16:54:48 -05001034 + nla_total_size(ext_filter_mask
1035 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
1036 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
David Gibsonc53864f2014-04-24 10:22:36 +10001037 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
Thomas Graff8ff1822010-11-16 04:30:14 +00001038 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
Arad, Ronenb1974ed2015-10-19 09:23:28 -07001039 + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
Jiri Pirko82f28412014-11-28 14:34:18 +01001040 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001041 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
Nicolas Dichtelc57c7a92016-03-31 18:10:31 +02001042 + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
David S. Millerb5cdae32017-04-18 15:36:58 -04001043 + rtnl_xdp_size() /* IFLA_XDP */
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001044 + nla_total_size(4) /* IFLA_EVENT */
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02001045 + nla_total_size(4) /* IFLA_NEW_NETNSID */
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01001046 + nla_total_size(4) /* IFLA_NEW_IFINDEX */
Roopa Prabhu829eb202020-07-31 17:34:01 -07001047 + rtnl_proto_down_size(dev) /* proto down */
Christian Brauner7e4a8d52018-09-04 21:53:53 +02001048 + nla_total_size(4) /* IFLA_TARGET_NETNSID */
David Decotignyb2d3bcf2018-01-18 09:59:13 -08001049 + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */
1050 + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */
Stephen Hemminger3e7a50c2018-07-27 13:43:22 -07001051 + nla_total_size(4) /* IFLA_MIN_MTU */
1052 + nla_total_size(4) /* IFLA_MAX_MTU */
Jiri Pirko88f4fb02019-09-30 11:48:17 +02001053 + rtnl_prop_list_size(dev)
Michal Kubecekf74877a2019-12-11 10:58:14 +01001054 + nla_total_size(MAX_ADDR_LEN) /* IFLA_PERM_ADDRESS */
Jiri Benc79e1ad12017-11-02 17:04:38 -02001055 + 0;
Thomas Graf339bf982006-11-10 14:10:15 -08001056}
1057
Scott Feldman57b61082010-05-17 22:49:55 -07001058static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
1059{
1060 struct nlattr *vf_ports;
1061 struct nlattr *vf_port;
1062 int vf;
1063 int err;
1064
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001065 vf_ports = nla_nest_start_noflag(skb, IFLA_VF_PORTS);
Scott Feldman57b61082010-05-17 22:49:55 -07001066 if (!vf_ports)
1067 return -EMSGSIZE;
1068
1069 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001070 vf_port = nla_nest_start_noflag(skb, IFLA_VF_PORT);
Scott Feldman8ca94182010-05-28 03:42:18 -07001071 if (!vf_port)
1072 goto nla_put_failure;
David S. Millera6574342012-04-01 20:12:00 -04001073 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
1074 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001075 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
Scott Feldman8ca94182010-05-28 03:42:18 -07001076 if (err == -EMSGSIZE)
1077 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001078 if (err) {
Scott Feldman57b61082010-05-17 22:49:55 -07001079 nla_nest_cancel(skb, vf_port);
1080 continue;
1081 }
1082 nla_nest_end(skb, vf_port);
1083 }
1084
1085 nla_nest_end(skb, vf_ports);
1086
1087 return 0;
Scott Feldman8ca94182010-05-28 03:42:18 -07001088
1089nla_put_failure:
1090 nla_nest_cancel(skb, vf_ports);
1091 return -EMSGSIZE;
Scott Feldman57b61082010-05-17 22:49:55 -07001092}
1093
1094static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
1095{
1096 struct nlattr *port_self;
1097 int err;
1098
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001099 port_self = nla_nest_start_noflag(skb, IFLA_PORT_SELF);
Scott Feldman57b61082010-05-17 22:49:55 -07001100 if (!port_self)
1101 return -EMSGSIZE;
1102
1103 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
1104 if (err) {
1105 nla_nest_cancel(skb, port_self);
Scott Feldman8ca94182010-05-28 03:42:18 -07001106 return (err == -EMSGSIZE) ? err : 0;
Scott Feldman57b61082010-05-17 22:49:55 -07001107 }
1108
1109 nla_nest_end(skb, port_self);
1110
1111 return 0;
1112}
1113
David Gibsonc53864f2014-04-24 10:22:36 +10001114static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1115 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -07001116{
1117 int err;
1118
David Gibsonc53864f2014-04-24 10:22:36 +10001119 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1120 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -07001121 return 0;
1122
1123 err = rtnl_port_self_fill(skb, dev);
1124 if (err)
1125 return err;
1126
1127 if (dev_num_vf(dev->dev.parent)) {
1128 err = rtnl_vf_ports_fill(skb, dev);
1129 if (err)
1130 return err;
1131 }
1132
1133 return 0;
1134}
1135
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001136static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
1137{
1138 int err;
Jiri Pirko02637fc2014-11-28 14:34:16 +01001139 struct netdev_phys_item_id ppid;
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001140
1141 err = dev_get_phys_port_id(dev, &ppid);
1142 if (err) {
1143 if (err == -EOPNOTSUPP)
1144 return 0;
1145 return err;
1146 }
1147
1148 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1149 return -EMSGSIZE;
1150
1151 return 0;
1152}
1153
David Aherndb24a902015-03-17 20:23:15 -06001154static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1155{
1156 char name[IFNAMSIZ];
1157 int err;
1158
1159 err = dev_get_phys_port_name(dev, name, sizeof(name));
1160 if (err) {
1161 if (err == -EOPNOTSUPP)
1162 return 0;
1163 return err;
1164 }
1165
Michal Schmidt77ef0332017-05-04 16:48:58 +02001166 if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
David Aherndb24a902015-03-17 20:23:15 -06001167 return -EMSGSIZE;
1168
1169 return 0;
1170}
1171
Jiri Pirko82f28412014-11-28 14:34:18 +01001172static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1173{
Florian Fainellibccb3022019-02-06 09:45:46 -08001174 struct netdev_phys_item_id ppid = { };
Jiri Pirko82f28412014-11-28 14:34:18 +01001175 int err;
Jiri Pirko82f28412014-11-28 14:34:18 +01001176
Florian Fainellibccb3022019-02-06 09:45:46 -08001177 err = dev_get_port_parent_id(dev, &ppid, false);
Jiri Pirko82f28412014-11-28 14:34:18 +01001178 if (err) {
1179 if (err == -EOPNOTSUPP)
1180 return 0;
1181 return err;
1182 }
1183
Florian Fainellibccb3022019-02-06 09:45:46 -08001184 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, ppid.id_len, ppid.id))
Jiri Pirko82f28412014-11-28 14:34:18 +01001185 return -EMSGSIZE;
1186
1187 return 0;
1188}
1189
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001190static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1191 struct net_device *dev)
1192{
Roopa Prabhu550bce52016-04-15 20:36:25 -07001193 struct rtnl_link_stats64 *sp;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001194 struct nlattr *attr;
1195
Nicolas Dichtel58414d32016-04-21 18:58:25 +02001196 attr = nla_reserve_64bit(skb, IFLA_STATS64,
1197 sizeof(struct rtnl_link_stats64), IFLA_PAD);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001198 if (!attr)
1199 return -EMSGSIZE;
1200
Roopa Prabhu550bce52016-04-15 20:36:25 -07001201 sp = nla_data(attr);
1202 dev_get_stats(dev, sp);
1203
1204 attr = nla_reserve(skb, IFLA_STATS,
1205 sizeof(struct rtnl_link_stats));
1206 if (!attr)
1207 return -EMSGSIZE;
1208
1209 copy_rtnl_link_stats(nla_data(attr), sp);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001210
1211 return 0;
1212}
1213
1214static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1215 struct net_device *dev,
1216 int vfs_num,
1217 struct nlattr *vfinfo)
1218{
1219 struct ifla_vf_rss_query_en vf_rss_query_en;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001220 struct nlattr *vf, *vfstats, *vfvlanlist;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001221 struct ifla_vf_link_state vf_linkstate;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001222 struct ifla_vf_vlan_info vf_vlan_info;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001223 struct ifla_vf_spoofchk vf_spoofchk;
1224 struct ifla_vf_tx_rate vf_tx_rate;
1225 struct ifla_vf_stats vf_stats;
1226 struct ifla_vf_trust vf_trust;
1227 struct ifla_vf_vlan vf_vlan;
1228 struct ifla_vf_rate vf_rate;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001229 struct ifla_vf_mac vf_mac;
Denis Kirjanov75345f882019-06-17 10:53:41 +02001230 struct ifla_vf_broadcast vf_broadcast;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001231 struct ifla_vf_info ivi;
Danit Goldberg30aad412019-11-06 15:30:07 +02001232 struct ifla_vf_guid node_guid;
1233 struct ifla_vf_guid port_guid;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001234
Mintz, Yuval0eed9cf2017-06-07 21:00:33 +03001235 memset(&ivi, 0, sizeof(ivi));
1236
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001237 /* Not all SR-IOV capable drivers support the
1238 * spoofcheck and "RSS query enable" query. Preset to
1239 * -1 so the user space tool can detect that the driver
1240 * didn't report anything.
1241 */
1242 ivi.spoofchk = -1;
1243 ivi.rss_query_en = -1;
1244 ivi.trusted = -1;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001245 /* The default value for VF link state is "auto"
1246 * IFLA_VF_LINK_STATE_AUTO which equals zero
1247 */
1248 ivi.linkstate = 0;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001249 /* VLAN Protocol by default is 802.1Q */
1250 ivi.vlan_proto = htons(ETH_P_8021Q);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001251 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1252 return 0;
1253
Dan Carpenter775f4f02016-10-13 11:45:28 +03001254 memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
Leon Romanovsky9fbf0822020-01-30 14:59:49 +02001255 memset(&node_guid, 0, sizeof(node_guid));
1256 memset(&port_guid, 0, sizeof(port_guid));
Dan Carpenter775f4f02016-10-13 11:45:28 +03001257
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001258 vf_mac.vf =
1259 vf_vlan.vf =
Moshe Shemesh79aab092016-09-22 12:11:15 +03001260 vf_vlan_info.vf =
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001261 vf_rate.vf =
1262 vf_tx_rate.vf =
1263 vf_spoofchk.vf =
1264 vf_linkstate.vf =
1265 vf_rss_query_en.vf =
Danit Goldberg9aed6ae2019-12-03 17:43:36 +02001266 vf_trust.vf =
1267 node_guid.vf =
1268 port_guid.vf = ivi.vf;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001269
1270 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
Denis Kirjanov75345f882019-06-17 10:53:41 +02001271 memcpy(vf_broadcast.broadcast, dev->broadcast, dev->addr_len);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001272 vf_vlan.vlan = ivi.vlan;
1273 vf_vlan.qos = ivi.qos;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001274 vf_vlan_info.vlan = ivi.vlan;
1275 vf_vlan_info.qos = ivi.qos;
1276 vf_vlan_info.vlan_proto = ivi.vlan_proto;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001277 vf_tx_rate.rate = ivi.max_tx_rate;
1278 vf_rate.min_tx_rate = ivi.min_tx_rate;
1279 vf_rate.max_tx_rate = ivi.max_tx_rate;
1280 vf_spoofchk.setting = ivi.spoofchk;
1281 vf_linkstate.link_state = ivi.linkstate;
1282 vf_rss_query_en.setting = ivi.rss_query_en;
1283 vf_trust.setting = ivi.trusted;
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001284 vf = nla_nest_start_noflag(skb, IFLA_VF_INFO);
Moshe Shemesh79aab092016-09-22 12:11:15 +03001285 if (!vf)
1286 goto nla_put_vfinfo_failure;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001287 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
Denis Kirjanov75345f882019-06-17 10:53:41 +02001288 nla_put(skb, IFLA_VF_BROADCAST, sizeof(vf_broadcast), &vf_broadcast) ||
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001289 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1290 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1291 &vf_rate) ||
1292 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1293 &vf_tx_rate) ||
1294 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1295 &vf_spoofchk) ||
1296 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1297 &vf_linkstate) ||
1298 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1299 sizeof(vf_rss_query_en),
1300 &vf_rss_query_en) ||
1301 nla_put(skb, IFLA_VF_TRUST,
1302 sizeof(vf_trust), &vf_trust))
Moshe Shemesh79aab092016-09-22 12:11:15 +03001303 goto nla_put_vf_failure;
Danit Goldberg30aad412019-11-06 15:30:07 +02001304
Danit Goldberg30aad412019-11-06 15:30:07 +02001305 if (dev->netdev_ops->ndo_get_vf_guid &&
1306 !dev->netdev_ops->ndo_get_vf_guid(dev, vfs_num, &node_guid,
1307 &port_guid)) {
1308 if (nla_put(skb, IFLA_VF_IB_NODE_GUID, sizeof(node_guid),
1309 &node_guid) ||
1310 nla_put(skb, IFLA_VF_IB_PORT_GUID, sizeof(port_guid),
1311 &port_guid))
1312 goto nla_put_vf_failure;
1313 }
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001314 vfvlanlist = nla_nest_start_noflag(skb, IFLA_VF_VLAN_LIST);
Moshe Shemesh79aab092016-09-22 12:11:15 +03001315 if (!vfvlanlist)
1316 goto nla_put_vf_failure;
1317 if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
1318 &vf_vlan_info)) {
1319 nla_nest_cancel(skb, vfvlanlist);
1320 goto nla_put_vf_failure;
1321 }
1322 nla_nest_end(skb, vfvlanlist);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001323 memset(&vf_stats, 0, sizeof(vf_stats));
1324 if (dev->netdev_ops->ndo_get_vf_stats)
1325 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1326 &vf_stats);
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001327 vfstats = nla_nest_start_noflag(skb, IFLA_VF_STATS);
Moshe Shemesh79aab092016-09-22 12:11:15 +03001328 if (!vfstats)
1329 goto nla_put_vf_failure;
Nicolas Dichtel343a6d82016-04-25 10:25:14 +02001330 if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1331 vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1332 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1333 vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1334 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1335 vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1336 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1337 vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1338 nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1339 vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1340 nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
Eugenia Emantayevc5a9f6f2017-07-17 13:47:07 +03001341 vf_stats.multicast, IFLA_VF_STATS_PAD) ||
1342 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED,
1343 vf_stats.rx_dropped, IFLA_VF_STATS_PAD) ||
1344 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED,
1345 vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) {
Moshe Shemesh79aab092016-09-22 12:11:15 +03001346 nla_nest_cancel(skb, vfstats);
1347 goto nla_put_vf_failure;
1348 }
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001349 nla_nest_end(skb, vfstats);
1350 nla_nest_end(skb, vf);
1351 return 0;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001352
1353nla_put_vf_failure:
1354 nla_nest_cancel(skb, vf);
1355nla_put_vfinfo_failure:
1356 nla_nest_cancel(skb, vfinfo);
1357 return -EMSGSIZE;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001358}
1359
Florian Westphal250fc3d2017-09-26 13:58:41 +02001360static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb,
1361 struct net_device *dev,
1362 u32 ext_filter_mask)
1363{
1364 struct nlattr *vfinfo;
1365 int i, num_vfs;
1366
1367 if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0))
1368 return 0;
1369
1370 num_vfs = dev_num_vf(dev->dev.parent);
1371 if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs))
1372 return -EMSGSIZE;
1373
1374 if (!dev->netdev_ops->ndo_get_vf_config)
1375 return 0;
1376
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001377 vfinfo = nla_nest_start_noflag(skb, IFLA_VFINFO_LIST);
Florian Westphal250fc3d2017-09-26 13:58:41 +02001378 if (!vfinfo)
1379 return -EMSGSIZE;
1380
1381 for (i = 0; i < num_vfs; i++) {
1382 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1383 return -EMSGSIZE;
1384 }
1385
1386 nla_nest_end(skb, vfinfo);
1387 return 0;
1388}
1389
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001390static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1391{
Kangjie Lu5f8e4472016-05-03 16:46:24 -04001392 struct rtnl_link_ifmap map;
1393
1394 memset(&map, 0, sizeof(map));
1395 map.mem_start = dev->mem_start;
1396 map.mem_end = dev->mem_end;
1397 map.base_addr = dev->base_addr;
1398 map.irq = dev->irq;
1399 map.dma = dev->dma;
1400 map.port = dev->if_port;
1401
Nicolas Dichtel270cb4d2016-04-26 10:06:16 +02001402 if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001403 return -EMSGSIZE;
1404
1405 return 0;
1406}
1407
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001408static u32 rtnl_xdp_prog_skb(struct net_device *dev)
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001409{
Martin KaFai Lau58038692017-06-15 17:29:09 -07001410 const struct bpf_prog *generic_xdp_prog;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001411
1412 ASSERT_RTNL();
1413
Martin KaFai Lau58038692017-06-15 17:29:09 -07001414 generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001415 if (!generic_xdp_prog)
1416 return 0;
1417 return generic_xdp_prog->aux->id;
1418}
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001419
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001420static u32 rtnl_xdp_prog_drv(struct net_device *dev)
1421{
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07001422 return dev_xdp_prog_id(dev, XDP_MODE_DRV);
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001423}
Jakub Kicinski118b4aa2017-12-01 15:08:55 -08001424
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001425static u32 rtnl_xdp_prog_hw(struct net_device *dev)
1426{
Andrii Nakryiko7f0a8382020-07-21 23:45:55 -07001427 return dev_xdp_prog_id(dev, XDP_MODE_HW);
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001428}
1429
1430static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev,
1431 u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr,
1432 u32 (*get_prog_id)(struct net_device *dev))
1433{
1434 u32 curr_id;
1435 int err;
1436
1437 curr_id = get_prog_id(dev);
1438 if (!curr_id)
1439 return 0;
1440
1441 *prog_id = curr_id;
1442 err = nla_put_u32(skb, attr, curr_id);
1443 if (err)
1444 return err;
1445
1446 if (*mode != XDP_ATTACHED_NONE)
1447 *mode = XDP_ATTACHED_MULTI;
1448 else
1449 *mode = tgt_mode;
1450
1451 return 0;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001452}
1453
Brenden Blancod1fdd912016-07-19 12:16:49 -07001454static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1455{
Brenden Blancod1fdd912016-07-19 12:16:49 -07001456 struct nlattr *xdp;
Martin KaFai Lau58038692017-06-15 17:29:09 -07001457 u32 prog_id;
Brenden Blancod1fdd912016-07-19 12:16:49 -07001458 int err;
Jakub Kicinski4f91da22018-07-11 20:36:38 -07001459 u8 mode;
Brenden Blancod1fdd912016-07-19 12:16:49 -07001460
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001461 xdp = nla_nest_start_noflag(skb, IFLA_XDP);
Brenden Blancod1fdd912016-07-19 12:16:49 -07001462 if (!xdp)
1463 return -EMSGSIZE;
David S. Millerb5cdae32017-04-18 15:36:58 -04001464
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001465 prog_id = 0;
1466 mode = XDP_ATTACHED_NONE;
Jakub Kicinski202aabe2018-07-16 19:08:50 -07001467 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB,
1468 IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb);
1469 if (err)
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001470 goto err_cancel;
Jakub Kicinski202aabe2018-07-16 19:08:50 -07001471 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV,
1472 IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv);
1473 if (err)
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001474 goto err_cancel;
Jakub Kicinski202aabe2018-07-16 19:08:50 -07001475 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW,
1476 IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw);
1477 if (err)
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001478 goto err_cancel;
1479
Jakub Kicinski4f91da22018-07-11 20:36:38 -07001480 err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode);
Brenden Blancod1fdd912016-07-19 12:16:49 -07001481 if (err)
1482 goto err_cancel;
1483
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001484 if (prog_id && mode != XDP_ATTACHED_MULTI) {
Martin KaFai Lau58038692017-06-15 17:29:09 -07001485 err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
1486 if (err)
1487 goto err_cancel;
1488 }
1489
Brenden Blancod1fdd912016-07-19 12:16:49 -07001490 nla_nest_end(skb, xdp);
1491 return 0;
1492
1493err_cancel:
1494 nla_nest_cancel(skb, xdp);
1495 return err;
1496}
1497
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001498static u32 rtnl_get_event(unsigned long event)
1499{
1500 u32 rtnl_event_type = IFLA_EVENT_NONE;
1501
1502 switch (event) {
1503 case NETDEV_REBOOT:
1504 rtnl_event_type = IFLA_EVENT_REBOOT;
1505 break;
1506 case NETDEV_FEAT_CHANGE:
1507 rtnl_event_type = IFLA_EVENT_FEATURES;
1508 break;
1509 case NETDEV_BONDING_FAILOVER:
1510 rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
1511 break;
1512 case NETDEV_NOTIFY_PEERS:
1513 rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
1514 break;
1515 case NETDEV_RESEND_IGMP:
1516 rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
1517 break;
1518 case NETDEV_CHANGEINFODATA:
1519 rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
1520 break;
1521 default:
1522 break;
1523 }
1524
1525 return rtnl_event_type;
1526}
1527
Florian Westphal79110a02017-09-26 13:58:40 +02001528static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
1529{
1530 const struct net_device *upper_dev;
1531 int ret = 0;
1532
1533 rcu_read_lock();
1534
1535 upper_dev = netdev_master_upper_dev_get_rcu(dev);
1536 if (upper_dev)
1537 ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);
1538
1539 rcu_read_unlock();
1540 return ret;
1541}
1542
Sabrina Dubrocafeadc4b2019-05-14 15:12:19 +02001543static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev,
1544 bool force)
Florian Westphal79110a02017-09-26 13:58:40 +02001545{
1546 int ifindex = dev_get_iflink(dev);
1547
Sabrina Dubrocafeadc4b2019-05-14 15:12:19 +02001548 if (force || dev->ifindex != ifindex)
1549 return nla_put_u32(skb, IFLA_LINK, ifindex);
Florian Westphal79110a02017-09-26 13:58:40 +02001550
Sabrina Dubrocafeadc4b2019-05-14 15:12:19 +02001551 return 0;
Florian Westphal79110a02017-09-26 13:58:40 +02001552}
1553
Florian Westphal6c557002017-10-02 23:50:05 +02001554static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
1555 struct net_device *dev)
1556{
1557 char buf[IFALIASZ];
1558 int ret;
1559
1560 ret = dev_get_alias(dev, buf, sizeof(buf));
1561 return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
1562}
1563
Florian Westphalb1e66b92017-09-26 13:58:42 +02001564static int rtnl_fill_link_netnsid(struct sk_buff *skb,
Jiri Benc79e1ad12017-11-02 17:04:38 -02001565 const struct net_device *dev,
Guillaume Naultd4e4fdf2019-10-23 18:39:04 +02001566 struct net *src_net, gfp_t gfp)
Florian Westphalb1e66b92017-09-26 13:58:42 +02001567{
Sabrina Dubrocafeadc4b2019-05-14 15:12:19 +02001568 bool put_iflink = false;
1569
Florian Westphalb1e66b92017-09-26 13:58:42 +02001570 if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
1571 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1572
1573 if (!net_eq(dev_net(dev), link_net)) {
Guillaume Naultd4e4fdf2019-10-23 18:39:04 +02001574 int id = peernet2id_alloc(src_net, link_net, gfp);
Florian Westphalb1e66b92017-09-26 13:58:42 +02001575
1576 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1577 return -EMSGSIZE;
Sabrina Dubrocafeadc4b2019-05-14 15:12:19 +02001578
1579 put_iflink = true;
Florian Westphalb1e66b92017-09-26 13:58:42 +02001580 }
1581 }
1582
Sabrina Dubrocafeadc4b2019-05-14 15:12:19 +02001583 return nla_put_iflink(skb, dev, put_iflink);
Florian Westphalb1e66b92017-09-26 13:58:42 +02001584}
1585
Florian Westphal070cbf5b2017-10-16 15:44:35 +02001586static int rtnl_fill_link_af(struct sk_buff *skb,
1587 const struct net_device *dev,
1588 u32 ext_filter_mask)
1589{
1590 const struct rtnl_af_ops *af_ops;
1591 struct nlattr *af_spec;
1592
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001593 af_spec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
Florian Westphal070cbf5b2017-10-16 15:44:35 +02001594 if (!af_spec)
1595 return -EMSGSIZE;
1596
Florian Westphal5fa85a02017-10-16 15:44:36 +02001597 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Florian Westphal070cbf5b2017-10-16 15:44:35 +02001598 struct nlattr *af;
1599 int err;
1600
1601 if (!af_ops->fill_link_af)
1602 continue;
1603
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001604 af = nla_nest_start_noflag(skb, af_ops->family);
Florian Westphal070cbf5b2017-10-16 15:44:35 +02001605 if (!af)
1606 return -EMSGSIZE;
1607
1608 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1609 /*
1610 * Caller may return ENODATA to indicate that there
1611 * was no data to be dumped. This is not an error, it
1612 * means we should trim the attribute header and
1613 * continue.
1614 */
1615 if (err == -ENODATA)
1616 nla_nest_cancel(skb, af);
1617 else if (err < 0)
1618 return -EMSGSIZE;
1619
1620 nla_nest_end(skb, af);
1621 }
1622
1623 nla_nest_end(skb, af_spec);
1624 return 0;
1625}
1626
Jiri Pirko88f4fb02019-09-30 11:48:17 +02001627static int rtnl_fill_alt_ifnames(struct sk_buff *skb,
1628 const struct net_device *dev)
1629{
1630 struct netdev_name_node *name_node;
1631 int count = 0;
1632
1633 list_for_each_entry(name_node, &dev->name_node->list, list) {
1634 if (nla_put_string(skb, IFLA_ALT_IFNAME, name_node->name))
1635 return -EMSGSIZE;
1636 count++;
1637 }
1638 return count;
1639}
1640
1641static int rtnl_fill_prop_list(struct sk_buff *skb,
1642 const struct net_device *dev)
1643{
1644 struct nlattr *prop_list;
1645 int ret;
1646
1647 prop_list = nla_nest_start(skb, IFLA_PROP_LIST);
1648 if (!prop_list)
1649 return -EMSGSIZE;
1650
1651 ret = rtnl_fill_alt_ifnames(skb, dev);
1652 if (ret <= 0)
1653 goto nest_cancel;
1654
1655 nla_nest_end(skb, prop_list);
1656 return 0;
1657
1658nest_cancel:
1659 nla_nest_cancel(skb, prop_list);
1660 return ret;
1661}
1662
Roopa Prabhu829eb202020-07-31 17:34:01 -07001663static int rtnl_fill_proto_down(struct sk_buff *skb,
1664 const struct net_device *dev)
1665{
1666 struct nlattr *pr;
1667 u32 preason;
1668
1669 if (nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
1670 goto nla_put_failure;
1671
1672 preason = dev->proto_down_reason;
1673 if (!preason)
1674 return 0;
1675
1676 pr = nla_nest_start(skb, IFLA_PROTO_DOWN_REASON);
1677 if (!pr)
1678 return -EMSGSIZE;
1679
1680 if (nla_put_u32(skb, IFLA_PROTO_DOWN_REASON_VALUE, preason)) {
1681 nla_nest_cancel(skb, pr);
1682 goto nla_put_failure;
1683 }
1684
1685 nla_nest_end(skb, pr);
1686 return 0;
1687
1688nla_put_failure:
1689 return -EMSGSIZE;
1690}
1691
Jiri Benc79e1ad12017-11-02 17:04:38 -02001692static int rtnl_fill_ifinfo(struct sk_buff *skb,
1693 struct net_device *dev, struct net *src_net,
Patrick McHardy575c3e22007-05-22 17:00:49 -07001694 int type, u32 pid, u32 seq, u32 change,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001695 unsigned int flags, u32 ext_filter_mask,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01001696 u32 event, int *new_nsid, int new_ifindex,
Guillaume Naultd4e4fdf2019-10-23 18:39:04 +02001697 int tgt_netnsid, gfp_t gfp)
Thomas Grafb60c5112006-08-04 23:05:34 -07001698{
1699 struct ifinfomsg *ifm;
1700 struct nlmsghdr *nlh;
1701
Eric Dumazet2907c352011-05-25 07:34:04 +00001702 ASSERT_RTNL();
Thomas Grafb60c5112006-08-04 23:05:34 -07001703 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1704 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08001705 return -EMSGSIZE;
Thomas Grafb60c5112006-08-04 23:05:34 -07001706
1707 ifm = nlmsg_data(nlh);
1708 ifm->ifi_family = AF_UNSPEC;
1709 ifm->__ifi_pad = 0;
1710 ifm->ifi_type = dev->type;
1711 ifm->ifi_index = dev->ifindex;
1712 ifm->ifi_flags = dev_get_flags(dev);
1713 ifm->ifi_change = change;
1714
Christian Brauner7e4a8d52018-09-04 21:53:53 +02001715 if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid))
Jiri Benc79e1ad12017-11-02 17:04:38 -02001716 goto nla_put_failure;
1717
David S. Millera6574342012-04-01 20:12:00 -04001718 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1719 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1720 nla_put_u8(skb, IFLA_OPERSTATE,
1721 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1722 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1723 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
Stephen Hemminger3e7a50c2018-07-27 13:43:22 -07001724 nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) ||
1725 nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) ||
David S. Millera6574342012-04-01 20:12:00 -04001726 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
Ben Greearedbc0bb2012-03-29 12:51:30 +00001727 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001728 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
Eric Dumazetc70ce022016-03-21 09:55:10 -07001729 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1730 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001731#ifdef CONFIG_RPS
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001732 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001733#endif
Florian Westphal79110a02017-09-26 13:58:40 +02001734 put_master_ifindex(skb, dev) ||
Jiri Pirko9a572472012-12-27 23:49:39 +00001735 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
David S. Millera6574342012-04-01 20:12:00 -04001736 (dev->qdisc &&
1737 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
Florian Westphal6c557002017-10-02 23:50:05 +02001738 nla_put_ifalias(skb, dev) ||
david decotigny2d3b4792014-03-29 09:48:35 -07001739 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
David Decotignyb2d3bcf2018-01-18 09:59:13 -08001740 atomic_read(&dev->carrier_up_count) +
1741 atomic_read(&dev->carrier_down_count)) ||
David Decotignyb2d3bcf2018-01-18 09:59:13 -08001742 nla_put_u32(skb, IFLA_CARRIER_UP_COUNT,
1743 atomic_read(&dev->carrier_up_count)) ||
1744 nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT,
1745 atomic_read(&dev->carrier_down_count)))
David S. Millera6574342012-04-01 20:12:00 -04001746 goto nla_put_failure;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001747
Roopa Prabhu829eb202020-07-31 17:34:01 -07001748 if (rtnl_fill_proto_down(skb, dev))
1749 goto nla_put_failure;
1750
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001751 if (event != IFLA_EVENT_NONE) {
1752 if (nla_put_u32(skb, IFLA_EVENT, event))
1753 goto nla_put_failure;
1754 }
1755
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001756 if (rtnl_fill_link_ifmap(skb, dev))
1757 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
1759 if (dev->addr_len) {
David S. Millera6574342012-04-01 20:12:00 -04001760 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1761 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1762 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 }
1764
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001765 if (rtnl_phys_port_id_fill(skb, dev))
1766 goto nla_put_failure;
1767
David Aherndb24a902015-03-17 20:23:15 -06001768 if (rtnl_phys_port_name_fill(skb, dev))
1769 goto nla_put_failure;
1770
Jiri Pirko82f28412014-11-28 14:34:18 +01001771 if (rtnl_phys_switch_id_fill(skb, dev))
1772 goto nla_put_failure;
1773
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001774 if (rtnl_fill_stats(skb, dev))
Pavel Emelyanov96e74082008-05-21 14:12:46 -07001775 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
Florian Westphal250fc3d2017-09-26 13:58:41 +02001777 if (rtnl_fill_vf(skb, dev, ext_filter_mask))
David S. Millera6574342012-04-01 20:12:00 -04001778 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001779
David Gibsonc53864f2014-04-24 10:22:36 +10001780 if (rtnl_port_fill(skb, dev, ext_filter_mask))
Scott Feldman57b61082010-05-17 22:49:55 -07001781 goto nla_put_failure;
1782
Brenden Blancod1fdd912016-07-19 12:16:49 -07001783 if (rtnl_xdp_fill(skb, dev))
1784 goto nla_put_failure;
1785
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001786 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07001787 if (rtnl_link_fill(skb, dev) < 0)
1788 goto nla_put_failure;
1789 }
1790
Guillaume Naultd4e4fdf2019-10-23 18:39:04 +02001791 if (rtnl_fill_link_netnsid(skb, dev, src_net, gfp))
Florian Westphalb1e66b92017-09-26 13:58:42 +02001792 goto nla_put_failure;
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001793
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02001794 if (new_nsid &&
1795 nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0)
1796 goto nla_put_failure;
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01001797 if (new_ifindex &&
1798 nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0)
1799 goto nla_put_failure;
1800
Michal Kubecekf74877a2019-12-11 10:58:14 +01001801 if (memchr_inv(dev->perm_addr, '\0', dev->addr_len) &&
1802 nla_put(skb, IFLA_PERM_ADDRESS, dev->addr_len, dev->perm_addr))
1803 goto nla_put_failure;
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02001804
Florian Westphal5fa85a02017-10-16 15:44:36 +02001805 rcu_read_lock();
Florian Westphal070cbf5b2017-10-16 15:44:35 +02001806 if (rtnl_fill_link_af(skb, dev, ext_filter_mask))
Florian Westphal5fa85a02017-10-16 15:44:36 +02001807 goto nla_put_failure_rcu;
1808 rcu_read_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +00001809
Jiri Pirko88f4fb02019-09-30 11:48:17 +02001810 if (rtnl_fill_prop_list(skb, dev))
1811 goto nla_put_failure;
1812
Johannes Berg00e77ed2021-06-12 10:20:55 +02001813 if (dev->dev.parent &&
1814 nla_put_string(skb, IFLA_PARENT_DEV_NAME,
1815 dev_name(dev->dev.parent)))
1816 goto nla_put_failure;
1817
1818 if (dev->dev.parent && dev->dev.parent->bus &&
1819 nla_put_string(skb, IFLA_PARENT_DEV_BUS_NAME,
1820 dev->dev.parent->bus->name))
1821 goto nla_put_failure;
1822
Johannes Berg053c0952015-01-16 22:09:00 +01001823 nlmsg_end(skb, nlh);
1824 return 0;
Thomas Grafb60c5112006-08-04 23:05:34 -07001825
Florian Westphal5fa85a02017-10-16 15:44:36 +02001826nla_put_failure_rcu:
1827 rcu_read_unlock();
Thomas Grafb60c5112006-08-04 23:05:34 -07001828nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001829 nlmsg_cancel(skb, nlh);
1830 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831}
1832
Jiri Pirkof7b12602014-02-18 20:53:18 +01001833static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1834 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1835 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1836 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1837 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1838 [IFLA_MTU] = { .type = NLA_U32 },
1839 [IFLA_LINK] = { .type = NLA_U32 },
1840 [IFLA_MASTER] = { .type = NLA_U32 },
1841 [IFLA_CARRIER] = { .type = NLA_U8 },
1842 [IFLA_TXQLEN] = { .type = NLA_U32 },
1843 [IFLA_WEIGHT] = { .type = NLA_U32 },
1844 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1845 [IFLA_LINKMODE] = { .type = NLA_U8 },
1846 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1847 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1848 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
Nicolas Dichtel2459b4c2017-10-11 16:24:48 +02001849 /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
1850 * allow 0-length string (needed to remove an alias).
1851 */
1852 [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001853 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1854 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1855 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1856 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1857 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1858 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1859 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1860 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
Stephen Hemminger46e6b992017-12-07 15:40:19 -08001861 [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 },
1862 [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 },
Jiri Pirko02637fc2014-11-28 14:34:16 +01001863 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
david decotigny2d3b4792014-03-29 09:48:35 -07001864 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
Jiri Pirko82f28412014-11-28 14:34:18 +01001865 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
Nicolas Dichtel317f4812015-01-15 15:11:18 +01001866 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001867 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001868 [IFLA_XDP] = { .type = NLA_NESTED },
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001869 [IFLA_EVENT] = { .type = NLA_U32 },
Serhey Popovychdb833d42017-06-20 14:35:23 +03001870 [IFLA_GROUP] = { .type = NLA_U32 },
Christian Brauner7e4a8d52018-09-04 21:53:53 +02001871 [IFLA_TARGET_NETNSID] = { .type = NLA_S32 },
David Decotignyb2d3bcf2018-01-18 09:59:13 -08001872 [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 },
1873 [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 },
Stephen Hemminger3e7a50c2018-07-27 13:43:22 -07001874 [IFLA_MIN_MTU] = { .type = NLA_U32 },
1875 [IFLA_MAX_MTU] = { .type = NLA_U32 },
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02001876 [IFLA_PROP_LIST] = { .type = NLA_NESTED },
1877 [IFLA_ALT_IFNAME] = { .type = NLA_STRING,
1878 .len = ALTIFNAMSIZ - 1 },
Michal Kubecekf74877a2019-12-11 10:58:14 +01001879 [IFLA_PERM_ADDRESS] = { .type = NLA_REJECT },
Roopa Prabhu829eb202020-07-31 17:34:01 -07001880 [IFLA_PROTO_DOWN_REASON] = { .type = NLA_NESTED },
Andrei Vagin7e4a5132021-04-06 23:40:03 -07001881 [IFLA_NEW_IFINDEX] = NLA_POLICY_MIN(NLA_S32, 1),
Johannes Berg88b71052021-06-12 10:20:56 +02001882 [IFLA_PARENT_DEV_NAME] = { .type = NLA_NUL_STRING },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001883};
1884
1885static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1886 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1887 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1888 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1889 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1890};
1891
Jiri Pirkof7b12602014-02-18 20:53:18 +01001892static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
Daniel Borkmann364d5712015-02-05 18:44:04 +01001893 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
Denis Kirjanov75345f882019-06-17 10:53:41 +02001894 [IFLA_VF_BROADCAST] = { .type = NLA_REJECT },
Daniel Borkmann364d5712015-02-05 18:44:04 +01001895 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
Moshe Shemesh79aab092016-09-22 12:11:15 +03001896 [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED },
Daniel Borkmann364d5712015-02-05 18:44:04 +01001897 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1898 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1899 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1900 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001901 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001902 [IFLA_VF_STATS] = { .type = NLA_NESTED },
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00001903 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
Eli Cohencc8e27c2016-03-11 22:58:34 +02001904 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1905 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001906};
1907
Jiri Pirkof7b12602014-02-18 20:53:18 +01001908static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1909 [IFLA_PORT_VF] = { .type = NLA_U32 },
1910 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1911 .len = PORT_PROFILE_MAX },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001912 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1913 .len = PORT_UUID_MAX },
1914 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1915 .len = PORT_UUID_MAX },
1916 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1917 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
Daniel Borkmann025331d2017-02-17 01:56:11 +01001918
1919 /* Unused, but we need to keep it here since user space could
1920 * fill it. It's also broken with regard to NLA_BINARY use in
1921 * combination with structs.
1922 */
1923 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1924 .len = sizeof(struct ifla_port_vsi) },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001925};
1926
Brenden Blancod1fdd912016-07-19 12:16:49 -07001927static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
Toke Høiland-Jørgensen92234c82020-03-25 18:23:26 +01001928 [IFLA_XDP_UNSPEC] = { .strict_start_type = IFLA_XDP_EXPECTED_FD },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001929 [IFLA_XDP_FD] = { .type = NLA_S32 },
Toke Høiland-Jørgensen92234c82020-03-25 18:23:26 +01001930 [IFLA_XDP_EXPECTED_FD] = { .type = NLA_S32 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001931 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 },
Daniel Borkmann85de8572016-11-28 23:16:54 +01001932 [IFLA_XDP_FLAGS] = { .type = NLA_U32 },
Martin KaFai Lau58038692017-06-15 17:29:09 -07001933 [IFLA_XDP_PROG_ID] = { .type = NLA_U32 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001934};
1935
David Aherndc599f72016-02-02 08:17:07 -08001936static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1937{
1938 const struct rtnl_link_ops *ops = NULL;
1939 struct nlattr *linfo[IFLA_INFO_MAX + 1];
1940
Johannes Berg8cb08172019-04-26 14:07:28 +02001941 if (nla_parse_nested_deprecated(linfo, IFLA_INFO_MAX, nla, ifla_info_policy, NULL) < 0)
David Aherndc599f72016-02-02 08:17:07 -08001942 return NULL;
1943
1944 if (linfo[IFLA_INFO_KIND]) {
1945 char kind[MODULE_NAME_LEN];
1946
Francis Laniel872f6902020-11-15 18:08:06 +01001947 nla_strscpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
David Aherndc599f72016-02-02 08:17:07 -08001948 ops = rtnl_link_ops_get(kind);
1949 }
1950
1951 return ops;
1952}
1953
1954static bool link_master_filtered(struct net_device *dev, int master_idx)
1955{
1956 struct net_device *master;
1957
1958 if (!master_idx)
1959 return false;
1960
1961 master = netdev_master_upper_dev_get(dev);
Lahav Schlesingerd3432bf2021-08-10 09:06:58 +00001962
1963 /* 0 is already used to denote IFLA_MASTER wasn't passed, therefore need
1964 * another invalid value for ifindex to denote "no master".
1965 */
1966 if (master_idx == -1)
1967 return !!master;
1968
David Aherndc599f72016-02-02 08:17:07 -08001969 if (!master || master->ifindex != master_idx)
1970 return true;
1971
1972 return false;
1973}
1974
1975static bool link_kind_filtered(const struct net_device *dev,
1976 const struct rtnl_link_ops *kind_ops)
1977{
1978 if (kind_ops && dev->rtnl_link_ops != kind_ops)
1979 return true;
1980
1981 return false;
1982}
1983
1984static bool link_dump_filtered(struct net_device *dev,
1985 int master_idx,
1986 const struct rtnl_link_ops *kind_ops)
1987{
1988 if (link_master_filtered(dev, master_idx) ||
1989 link_kind_filtered(dev, kind_ops))
1990 return true;
1991
1992 return false;
1993}
1994
Christian Braunerc383edc2018-09-04 21:53:47 +02001995/**
1996 * rtnl_get_net_ns_capable - Get netns if sufficiently privileged.
1997 * @sk: netlink socket
1998 * @netnsid: network namespace identifier
1999 *
2000 * Returns the network namespace identified by netnsid on success or an error
2001 * pointer on failure.
2002 */
2003struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid)
Jiri Benc79e1ad12017-11-02 17:04:38 -02002004{
2005 struct net *net;
2006
Andrei Vaginf428fe42018-01-02 23:27:33 -08002007 net = get_net_ns_by_id(sock_net(sk), netnsid);
Jiri Benc79e1ad12017-11-02 17:04:38 -02002008 if (!net)
2009 return ERR_PTR(-EINVAL);
2010
2011 /* For now, the caller is required to have CAP_NET_ADMIN in
2012 * the user namespace owning the target net ns.
2013 */
Andrei Vaginf428fe42018-01-02 23:27:33 -08002014 if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) {
Jiri Benc79e1ad12017-11-02 17:04:38 -02002015 put_net(net);
2016 return ERR_PTR(-EACCES);
2017 }
2018 return net;
2019}
Christian Braunerc383edc2018-09-04 21:53:47 +02002020EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable);
Jiri Benc79e1ad12017-11-02 17:04:38 -02002021
David Ahern905cf0a2018-10-07 20:16:30 -07002022static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh,
2023 bool strict_check, struct nlattr **tb,
2024 struct netlink_ext_ack *extack)
2025{
2026 int hdrlen;
2027
2028 if (strict_check) {
2029 struct ifinfomsg *ifm;
2030
2031 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
2032 NL_SET_ERR_MSG(extack, "Invalid header for link dump");
2033 return -EINVAL;
2034 }
2035
2036 ifm = nlmsg_data(nlh);
2037 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
2038 ifm->ifi_change) {
2039 NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request");
2040 return -EINVAL;
2041 }
2042 if (ifm->ifi_index) {
2043 NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps");
2044 return -EINVAL;
2045 }
2046
Johannes Berg8cb08172019-04-26 14:07:28 +02002047 return nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb,
2048 IFLA_MAX, ifla_policy,
2049 extack);
David Ahern905cf0a2018-10-07 20:16:30 -07002050 }
2051
2052 /* A hack to preserve kernel<->userspace interface.
2053 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
2054 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
2055 * what iproute2 < v3.9.0 used.
2056 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
2057 * attribute, its netlink message is shorter than struct ifinfomsg.
2058 */
2059 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2060 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2061
Johannes Berg8cb08172019-04-26 14:07:28 +02002062 return nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy,
2063 extack);
David Ahern905cf0a2018-10-07 20:16:30 -07002064}
2065
Thomas Grafb60c5112006-08-04 23:05:34 -07002066static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067{
David Ahern905cf0a2018-10-07 20:16:30 -07002068 struct netlink_ext_ack *extack = cb->extack;
2069 const struct nlmsghdr *nlh = cb->nlh;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002070 struct net *net = sock_net(skb->sk);
Jiri Benc79e1ad12017-11-02 17:04:38 -02002071 struct net *tgt_net = net;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002072 int h, s_h;
2073 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 struct net_device *dev;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002075 struct hlist_head *head;
Greg Rose115c9b82012-02-21 16:54:48 -05002076 struct nlattr *tb[IFLA_MAX+1];
2077 u32 ext_filter_mask = 0;
David Aherndc599f72016-02-02 08:17:07 -08002078 const struct rtnl_link_ops *kind_ops = NULL;
2079 unsigned int flags = NLM_F_MULTI;
2080 int master_idx = 0;
Jiri Benc79e1ad12017-11-02 17:04:38 -02002081 int netnsid = -1;
David Ahern905cf0a2018-10-07 20:16:30 -07002082 int err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002084 s_h = cb->args[0];
2085 s_idx = cb->args[1];
2086
David Ahern905cf0a2018-10-07 20:16:30 -07002087 err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack);
2088 if (err < 0) {
2089 if (cb->strict_check)
2090 return err;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02002091
David Ahern905cf0a2018-10-07 20:16:30 -07002092 goto walk_entries;
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00002093 }
Greg Rose115c9b82012-02-21 16:54:48 -05002094
David Ahern905cf0a2018-10-07 20:16:30 -07002095 for (i = 0; i <= IFLA_MAX; ++i) {
2096 if (!tb[i])
2097 continue;
2098
2099 /* new attributes should only be added with strict checking */
2100 switch (i) {
2101 case IFLA_TARGET_NETNSID:
2102 netnsid = nla_get_s32(tb[i]);
2103 tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid);
2104 if (IS_ERR(tgt_net)) {
2105 NL_SET_ERR_MSG(extack, "Invalid target network namespace id");
2106 return PTR_ERR(tgt_net);
2107 }
2108 break;
2109 case IFLA_EXT_MASK:
2110 ext_filter_mask = nla_get_u32(tb[i]);
2111 break;
2112 case IFLA_MASTER:
2113 master_idx = nla_get_u32(tb[i]);
2114 break;
2115 case IFLA_LINKINFO:
2116 kind_ops = linkinfo_to_kind_ops(tb[i]);
2117 break;
2118 default:
2119 if (cb->strict_check) {
2120 NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request");
2121 return -EINVAL;
2122 }
2123 }
2124 }
2125
2126 if (master_idx || kind_ops)
2127 flags |= NLM_F_DUMP_FILTERED;
2128
2129walk_entries:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002130 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
2131 idx = 0;
Jiri Benc79e1ad12017-11-02 17:04:38 -02002132 head = &tgt_net->dev_index_head[h];
Eric Dumazetcac5e652015-02-27 09:42:50 -08002133 hlist_for_each_entry(dev, head, index_hlist) {
David Aherndc599f72016-02-02 08:17:07 -08002134 if (link_dump_filtered(dev, master_idx, kind_ops))
Zhang Shengju3f0ae052016-11-19 23:28:32 +08002135 goto cont;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002136 if (idx < s_idx)
2137 goto cont;
Jiri Benc79e1ad12017-11-02 17:04:38 -02002138 err = rtnl_fill_ifinfo(skb, dev, net,
2139 RTM_NEWLINK,
David Gibson973462b2014-04-24 10:22:35 +10002140 NETLINK_CB(cb->skb).portid,
David Ahern905cf0a2018-10-07 20:16:30 -07002141 nlh->nlmsg_seq, 0, flags,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01002142 ext_filter_mask, 0, NULL, 0,
Guillaume Naultd4e4fdf2019-10-23 18:39:04 +02002143 netnsid, GFP_KERNEL);
David Gibson973462b2014-04-24 10:22:35 +10002144
David Ahernf6c57752017-05-15 23:19:17 -07002145 if (err < 0) {
2146 if (likely(skb->len))
2147 goto out;
2148
2149 goto out_err;
2150 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07002151cont:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002152 idx++;
2153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 }
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002155out:
David Ahernf6c57752017-05-15 23:19:17 -07002156 err = skb->len;
2157out_err:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002158 cb->args[1] = idx;
2159 cb->args[0] = h;
zhang kaia9ecb0c2021-03-02 18:16:07 +08002160 cb->seq = tgt_net->dev_base_seq;
Jakub Sitnickid0225782017-08-09 17:39:12 +02002161 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Jiri Benc79e1ad12017-11-02 17:04:38 -02002162 if (netnsid >= 0)
2163 put_net(tgt_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
David Ahernf6c57752017-05-15 23:19:17 -07002165 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166}
2167
Johannes Bergfceb6432017-04-12 14:34:07 +02002168int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
2169 struct netlink_ext_ack *exterr)
Jiri Pirkof7b12602014-02-18 20:53:18 +01002170{
Johannes Berg8cb08172019-04-26 14:07:28 +02002171 return nla_parse_deprecated(tb, IFLA_MAX, head, len, ifla_policy,
2172 exterr);
Jiri Pirkof7b12602014-02-18 20:53:18 +01002173}
2174EXPORT_SYMBOL(rtnl_nla_parse_ifla);
Scott Feldman57b61082010-05-17 22:49:55 -07002175
Eric W. Biederman81adee42009-11-08 00:53:51 -08002176struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
2177{
2178 struct net *net;
2179 /* Examine the link attributes and figure out which
2180 * network namespace we are talking about.
2181 */
2182 if (tb[IFLA_NET_NS_PID])
2183 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
Eric W. Biedermanf0630522011-05-04 17:51:50 -07002184 else if (tb[IFLA_NET_NS_FD])
2185 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
Eric W. Biederman81adee42009-11-08 00:53:51 -08002186 else
2187 net = get_net(src_net);
2188 return net;
2189}
2190EXPORT_SYMBOL(rtnl_link_get_net);
2191
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002192/* Figure out which network namespace we are talking about by
2193 * examining the link attributes in the following order:
2194 *
2195 * 1. IFLA_NET_NS_PID
2196 * 2. IFLA_NET_NS_FD
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002197 * 3. IFLA_TARGET_NETNSID
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002198 */
2199static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net,
2200 struct nlattr *tb[])
2201{
2202 struct net *net;
2203
2204 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])
2205 return rtnl_link_get_net(src_net, tb);
2206
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002207 if (!tb[IFLA_TARGET_NETNSID])
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002208 return get_net(src_net);
2209
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002210 net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID]));
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002211 if (!net)
2212 return ERR_PTR(-EINVAL);
2213
2214 return net;
2215}
2216
2217static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb,
2218 struct net *src_net,
2219 struct nlattr *tb[], int cap)
2220{
2221 struct net *net;
2222
2223 net = rtnl_link_get_net_by_nlattr(src_net, tb);
2224 if (IS_ERR(net))
2225 return net;
2226
2227 if (!netlink_ns_capable(skb, net->user_ns, cap)) {
2228 put_net(net);
2229 return ERR_PTR(-EPERM);
2230 }
2231
2232 return net;
2233}
2234
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002235/* Verify that rtnetlink requests do not pass additional properties
2236 * potentially referring to different network namespaces.
2237 */
2238static int rtnl_ensure_unique_netns(struct nlattr *tb[],
2239 struct netlink_ext_ack *extack,
2240 bool netns_id_only)
2241{
2242
2243 if (netns_id_only) {
2244 if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD])
2245 return 0;
2246
2247 NL_SET_ERR_MSG(extack, "specified netns attribute not supported");
2248 return -EOPNOTSUPP;
2249 }
2250
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002251 if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]))
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002252 goto invalid_attr;
2253
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002254 if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || 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_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID]))
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002258 goto invalid_attr;
2259
2260 return 0;
2261
2262invalid_attr:
2263 NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified");
2264 return -EINVAL;
2265}
2266
Rocco Yue8679c312021-08-03 20:02:50 +08002267static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[],
2268 struct netlink_ext_ack *extack)
Thomas Graf1840bb12008-02-23 19:54:36 -08002269{
2270 if (dev) {
2271 if (tb[IFLA_ADDRESS] &&
2272 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
2273 return -EINVAL;
2274
2275 if (tb[IFLA_BROADCAST] &&
2276 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
2277 return -EINVAL;
2278 }
2279
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002280 if (tb[IFLA_AF_SPEC]) {
2281 struct nlattr *af;
2282 int rem, err;
2283
2284 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2285 const struct rtnl_af_ops *af_ops;
2286
Florian Westphal5fa85a02017-10-16 15:44:36 +02002287 af_ops = rtnl_af_lookup(nla_type(af));
Cong Wanga1002432021-05-08 11:00:33 -07002288 if (!af_ops)
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002289 return -EAFNOSUPPORT;
2290
Cong Wanga1002432021-05-08 11:00:33 -07002291 if (!af_ops->set_link_af)
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002292 return -EOPNOTSUPP;
2293
2294 if (af_ops->validate_link_af) {
Rocco Yue8679c312021-08-03 20:02:50 +08002295 err = af_ops->validate_link_af(dev, af, extack);
Cong Wanga1002432021-05-08 11:00:33 -07002296 if (err < 0)
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002297 return err;
2298 }
2299 }
2300 }
2301
Thomas Graf1840bb12008-02-23 19:54:36 -08002302 return 0;
2303}
2304
Eli Cohencc8e27c2016-03-11 22:58:34 +02002305static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
2306 int guid_type)
2307{
2308 const struct net_device_ops *ops = dev->netdev_ops;
2309
2310 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
2311}
2312
2313static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
2314{
2315 if (dev->type != ARPHRD_INFINIBAND)
2316 return -EOPNOTSUPP;
2317
2318 return handle_infiniband_guid(dev, ivt, guid_type);
2319}
2320
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002321static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
Chris Wrightc02db8c2010-05-16 01:05:45 -07002322{
Chris Wrightc02db8c2010-05-16 01:05:45 -07002323 const struct net_device_ops *ops = dev->netdev_ops;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002324 int err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002325
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002326 if (tb[IFLA_VF_MAC]) {
2327 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03002328
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002329 if (ivm->vf >= INT_MAX)
2330 return -EINVAL;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002331 err = -EOPNOTSUPP;
2332 if (ops->ndo_set_vf_mac)
2333 err = ops->ndo_set_vf_mac(dev, ivm->vf,
2334 ivm->mac);
2335 if (err < 0)
2336 return err;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002337 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002338
2339 if (tb[IFLA_VF_VLAN]) {
2340 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
2341
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002342 if (ivv->vf >= INT_MAX)
2343 return -EINVAL;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002344 err = -EOPNOTSUPP;
2345 if (ops->ndo_set_vf_vlan)
2346 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
Moshe Shemesh79aab092016-09-22 12:11:15 +03002347 ivv->qos,
2348 htons(ETH_P_8021Q));
2349 if (err < 0)
2350 return err;
2351 }
2352
2353 if (tb[IFLA_VF_VLAN_LIST]) {
2354 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
2355 struct nlattr *attr;
2356 int rem, len = 0;
2357
2358 err = -EOPNOTSUPP;
2359 if (!ops->ndo_set_vf_vlan)
2360 return err;
2361
2362 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
2363 if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
2364 nla_len(attr) < NLA_HDRLEN) {
2365 return -EINVAL;
2366 }
2367 if (len >= MAX_VLAN_LIST_LEN)
2368 return -EOPNOTSUPP;
2369 ivvl[len] = nla_data(attr);
2370
2371 len++;
2372 }
Arnd Bergmannfa34cd92016-09-30 18:13:49 +02002373 if (len == 0)
2374 return -EINVAL;
2375
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002376 if (ivvl[0]->vf >= INT_MAX)
2377 return -EINVAL;
Moshe Shemesh79aab092016-09-22 12:11:15 +03002378 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
2379 ivvl[0]->qos, ivvl[0]->vlan_proto);
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002380 if (err < 0)
2381 return err;
2382 }
2383
2384 if (tb[IFLA_VF_TX_RATE]) {
2385 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
2386 struct ifla_vf_info ivf;
2387
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002388 if (ivt->vf >= INT_MAX)
2389 return -EINVAL;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002390 err = -EOPNOTSUPP;
2391 if (ops->ndo_get_vf_config)
2392 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
2393 if (err < 0)
2394 return err;
2395
2396 err = -EOPNOTSUPP;
2397 if (ops->ndo_set_vf_rate)
2398 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2399 ivf.min_tx_rate,
2400 ivt->rate);
2401 if (err < 0)
2402 return err;
2403 }
2404
2405 if (tb[IFLA_VF_RATE]) {
2406 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
2407
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002408 if (ivt->vf >= INT_MAX)
2409 return -EINVAL;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002410 err = -EOPNOTSUPP;
2411 if (ops->ndo_set_vf_rate)
2412 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2413 ivt->min_tx_rate,
2414 ivt->max_tx_rate);
2415 if (err < 0)
2416 return err;
2417 }
2418
2419 if (tb[IFLA_VF_SPOOFCHK]) {
2420 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
2421
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002422 if (ivs->vf >= INT_MAX)
2423 return -EINVAL;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002424 err = -EOPNOTSUPP;
2425 if (ops->ndo_set_vf_spoofchk)
2426 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
2427 ivs->setting);
2428 if (err < 0)
2429 return err;
2430 }
2431
2432 if (tb[IFLA_VF_LINK_STATE]) {
2433 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
2434
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002435 if (ivl->vf >= INT_MAX)
2436 return -EINVAL;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002437 err = -EOPNOTSUPP;
2438 if (ops->ndo_set_vf_link_state)
2439 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
2440 ivl->link_state);
2441 if (err < 0)
2442 return err;
2443 }
2444
2445 if (tb[IFLA_VF_RSS_QUERY_EN]) {
2446 struct ifla_vf_rss_query_en *ivrssq_en;
2447
2448 err = -EOPNOTSUPP;
2449 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002450 if (ivrssq_en->vf >= INT_MAX)
2451 return -EINVAL;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002452 if (ops->ndo_set_vf_rss_query_en)
2453 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
2454 ivrssq_en->setting);
2455 if (err < 0)
2456 return err;
2457 }
2458
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00002459 if (tb[IFLA_VF_TRUST]) {
2460 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
2461
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002462 if (ivt->vf >= INT_MAX)
2463 return -EINVAL;
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00002464 err = -EOPNOTSUPP;
2465 if (ops->ndo_set_vf_trust)
2466 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
2467 if (err < 0)
2468 return err;
2469 }
2470
Eli Cohencc8e27c2016-03-11 22:58:34 +02002471 if (tb[IFLA_VF_IB_NODE_GUID]) {
2472 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
2473
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002474 if (ivt->vf >= INT_MAX)
2475 return -EINVAL;
Eli Cohencc8e27c2016-03-11 22:58:34 +02002476 if (!ops->ndo_set_vf_guid)
2477 return -EOPNOTSUPP;
Eli Cohencc8e27c2016-03-11 22:58:34 +02002478 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
2479 }
2480
2481 if (tb[IFLA_VF_IB_PORT_GUID]) {
2482 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
2483
Dan Carpenterff08ddb2019-11-20 15:34:38 +03002484 if (ivt->vf >= INT_MAX)
2485 return -EINVAL;
Eli Cohencc8e27c2016-03-11 22:58:34 +02002486 if (!ops->ndo_set_vf_guid)
2487 return -EOPNOTSUPP;
2488
2489 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
2490 }
2491
Chris Wrightc02db8c2010-05-16 01:05:45 -07002492 return err;
2493}
2494
David Ahern33eaf2a2017-10-04 17:48:46 -07002495static int do_set_master(struct net_device *dev, int ifindex,
2496 struct netlink_ext_ack *extack)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002497{
Jiri Pirko898e5062013-01-03 22:48:52 +00002498 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002499 const struct net_device_ops *ops;
2500 int err;
2501
Jiri Pirko898e5062013-01-03 22:48:52 +00002502 if (upper_dev) {
2503 if (upper_dev->ifindex == ifindex)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002504 return 0;
Jiri Pirko898e5062013-01-03 22:48:52 +00002505 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002506 if (ops->ndo_del_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002507 err = ops->ndo_del_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002508 if (err)
2509 return err;
2510 } else {
2511 return -EOPNOTSUPP;
2512 }
2513 }
2514
2515 if (ifindex) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002516 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
2517 if (!upper_dev)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002518 return -EINVAL;
Jiri Pirko898e5062013-01-03 22:48:52 +00002519 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002520 if (ops->ndo_add_slave) {
David Ahern33eaf2a2017-10-04 17:48:46 -07002521 err = ops->ndo_add_slave(upper_dev, dev, extack);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002522 if (err)
2523 return err;
2524 } else {
2525 return -EOPNOTSUPP;
2526 }
2527 }
2528 return 0;
2529}
2530
Roopa Prabhu829eb202020-07-31 17:34:01 -07002531static const struct nla_policy ifla_proto_down_reason_policy[IFLA_PROTO_DOWN_REASON_VALUE + 1] = {
2532 [IFLA_PROTO_DOWN_REASON_MASK] = { .type = NLA_U32 },
2533 [IFLA_PROTO_DOWN_REASON_VALUE] = { .type = NLA_U32 },
2534};
2535
2536static int do_set_proto_down(struct net_device *dev,
2537 struct nlattr *nl_proto_down,
2538 struct nlattr *nl_proto_down_reason,
2539 struct netlink_ext_ack *extack)
2540{
2541 struct nlattr *pdreason[IFLA_PROTO_DOWN_REASON_MAX + 1];
2542 const struct net_device_ops *ops = dev->netdev_ops;
2543 unsigned long mask = 0;
2544 u32 value;
2545 bool proto_down;
2546 int err;
2547
2548 if (!ops->ndo_change_proto_down) {
2549 NL_SET_ERR_MSG(extack, "Protodown not supported by device");
2550 return -EOPNOTSUPP;
2551 }
2552
2553 if (nl_proto_down_reason) {
2554 err = nla_parse_nested_deprecated(pdreason,
2555 IFLA_PROTO_DOWN_REASON_MAX,
2556 nl_proto_down_reason,
2557 ifla_proto_down_reason_policy,
2558 NULL);
2559 if (err < 0)
2560 return err;
2561
2562 if (!pdreason[IFLA_PROTO_DOWN_REASON_VALUE]) {
2563 NL_SET_ERR_MSG(extack, "Invalid protodown reason value");
2564 return -EINVAL;
2565 }
2566
2567 value = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_VALUE]);
2568
2569 if (pdreason[IFLA_PROTO_DOWN_REASON_MASK])
2570 mask = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_MASK]);
2571
2572 dev_change_proto_down_reason(dev, mask, value);
2573 }
2574
2575 if (nl_proto_down) {
2576 proto_down = nla_get_u8(nl_proto_down);
2577
Zheng Yongjund467d0b2021-06-02 14:56:23 +08002578 /* Don't turn off protodown if there are active reasons */
Roopa Prabhu829eb202020-07-31 17:34:01 -07002579 if (!proto_down && dev->proto_down_reason) {
2580 NL_SET_ERR_MSG(extack, "Cannot clear protodown, active reasons");
2581 return -EBUSY;
2582 }
2583 err = dev_change_proto_down(dev,
2584 proto_down);
2585 if (err)
2586 return err;
2587 }
2588
2589 return 0;
2590}
2591
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002592#define DO_SETLINK_MODIFIED 0x01
Nicolas Dichtelba998902014-09-01 16:07:29 +02002593/* notify flag means notify + modified. */
2594#define DO_SETLINK_NOTIFY 0x03
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002595static int do_setlink(const struct sk_buff *skb,
2596 struct net_device *dev, struct ifinfomsg *ifm,
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002597 struct netlink_ext_ack *extack,
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002598 struct nlattr **tb, char *ifname, int status)
Patrick McHardy0157f602007-06-13 12:03:36 -07002599{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002600 const struct net_device_ops *ops = dev->netdev_ops;
Patrick McHardy0157f602007-06-13 12:03:36 -07002601 int err;
2602
Rocco Yue8679c312021-08-03 20:02:50 +08002603 err = validate_linkmsg(dev, tb, extack);
Eric Dumazet644c7ee2018-06-05 09:25:19 -07002604 if (err < 0)
2605 return err;
2606
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002607 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) {
Andrey Ignatov96a6b932021-08-25 17:25:40 -07002608 const char *pat = ifname && ifname[0] ? ifname : NULL;
Andrei Vagineeb85a12021-04-05 00:12:23 -07002609 struct net *net;
2610 int new_ifindex;
2611
2612 net = rtnl_link_get_net_capable(skb, dev_net(dev),
2613 tb, CAP_NET_ADMIN);
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002614 if (IS_ERR(net)) {
2615 err = PTR_ERR(net);
2616 goto errout;
2617 }
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002618
Andrei Vagineeb85a12021-04-05 00:12:23 -07002619 if (tb[IFLA_NEW_IFINDEX])
2620 new_ifindex = nla_get_s32(tb[IFLA_NEW_IFINDEX]);
2621 else
2622 new_ifindex = 0;
2623
Andrey Ignatov96a6b932021-08-25 17:25:40 -07002624 err = __dev_change_net_namespace(dev, net, pat, new_ifindex);
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002625 put_net(net);
2626 if (err)
2627 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002628 status |= DO_SETLINK_MODIFIED;
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002629 }
2630
Patrick McHardy0157f602007-06-13 12:03:36 -07002631 if (tb[IFLA_MAP]) {
2632 struct rtnl_link_ifmap *u_map;
2633 struct ifmap k_map;
2634
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002635 if (!ops->ndo_set_config) {
Patrick McHardy0157f602007-06-13 12:03:36 -07002636 err = -EOPNOTSUPP;
2637 goto errout;
2638 }
2639
2640 if (!netif_device_present(dev)) {
2641 err = -ENODEV;
2642 goto errout;
2643 }
2644
2645 u_map = nla_data(tb[IFLA_MAP]);
2646 k_map.mem_start = (unsigned long) u_map->mem_start;
2647 k_map.mem_end = (unsigned long) u_map->mem_end;
2648 k_map.base_addr = (unsigned short) u_map->base_addr;
2649 k_map.irq = (unsigned char) u_map->irq;
2650 k_map.dma = (unsigned char) u_map->dma;
2651 k_map.port = (unsigned char) u_map->port;
2652
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002653 err = ops->ndo_set_config(dev, &k_map);
Patrick McHardy0157f602007-06-13 12:03:36 -07002654 if (err < 0)
2655 goto errout;
2656
Nicolas Dichtelba998902014-09-01 16:07:29 +02002657 status |= DO_SETLINK_NOTIFY;
Patrick McHardy0157f602007-06-13 12:03:36 -07002658 }
2659
2660 if (tb[IFLA_ADDRESS]) {
2661 struct sockaddr *sa;
2662 int len;
2663
WANG Cong153711f2017-07-20 11:27:57 -07002664 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2665 sizeof(*sa));
Patrick McHardy0157f602007-06-13 12:03:36 -07002666 sa = kmalloc(len, GFP_KERNEL);
2667 if (!sa) {
2668 err = -ENOMEM;
2669 goto errout;
2670 }
2671 sa->sa_family = dev->type;
2672 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
2673 dev->addr_len);
Cong Wang3b23a322021-02-11 11:34:10 -08002674 err = dev_set_mac_address_user(dev, sa, extack);
Patrick McHardy0157f602007-06-13 12:03:36 -07002675 kfree(sa);
2676 if (err)
2677 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002678 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002679 }
2680
2681 if (tb[IFLA_MTU]) {
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07002682 err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack);
Patrick McHardy0157f602007-06-13 12:03:36 -07002683 if (err < 0)
2684 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002685 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002686 }
2687
Vlad Dogarucbda10f2011-01-13 23:38:30 +00002688 if (tb[IFLA_GROUP]) {
2689 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Nicolas Dichtelba998902014-09-01 16:07:29 +02002690 status |= DO_SETLINK_NOTIFY;
Vlad Dogarucbda10f2011-01-13 23:38:30 +00002691 }
2692
Patrick McHardy0157f602007-06-13 12:03:36 -07002693 /*
2694 * Interface selected by interface index but interface
2695 * name provided implies that a name change has been
2696 * requested.
2697 */
2698 if (ifm->ifi_index > 0 && ifname[0]) {
2699 err = dev_change_name(dev, ifname);
2700 if (err < 0)
2701 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002702 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002703 }
2704
Stephen Hemminger0b815a12008-09-22 21:28:11 -07002705 if (tb[IFLA_IFALIAS]) {
2706 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2707 nla_len(tb[IFLA_IFALIAS]));
2708 if (err < 0)
2709 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002710 status |= DO_SETLINK_NOTIFY;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07002711 }
2712
Patrick McHardy0157f602007-06-13 12:03:36 -07002713 if (tb[IFLA_BROADCAST]) {
2714 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00002715 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Patrick McHardy0157f602007-06-13 12:03:36 -07002716 }
2717
2718 if (ifm->ifi_flags || ifm->ifi_change) {
Petr Machata567c5e12018-12-06 17:05:42 +00002719 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
2720 extack);
Johannes Berg5f9021c2008-11-16 23:20:31 -08002721 if (err < 0)
2722 goto errout;
Patrick McHardy0157f602007-06-13 12:03:36 -07002723 }
2724
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002725 if (tb[IFLA_MASTER]) {
David Ahern33eaf2a2017-10-04 17:48:46 -07002726 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002727 if (err)
2728 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002729 status |= DO_SETLINK_MODIFIED;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002730 }
2731
Jiri Pirko9a572472012-12-27 23:49:39 +00002732 if (tb[IFLA_CARRIER]) {
2733 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2734 if (err)
2735 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002736 status |= DO_SETLINK_MODIFIED;
Jiri Pirko9a572472012-12-27 23:49:39 +00002737 }
2738
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002739 if (tb[IFLA_TXQLEN]) {
Alexey Dobriyan0cd29502017-05-17 13:30:44 +03002740 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002741
Cong Wang6a643dd2018-01-25 18:26:22 -08002742 err = dev_change_tx_queue_len(dev, value);
2743 if (err)
2744 goto errout;
2745 status |= DO_SETLINK_MODIFIED;
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002746 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002747
Stephen Hemminger46e6b992017-12-07 15:40:19 -08002748 if (tb[IFLA_GSO_MAX_SIZE]) {
2749 u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]);
2750
2751 if (max_size > GSO_MAX_SIZE) {
2752 err = -EINVAL;
2753 goto errout;
2754 }
2755
2756 if (dev->gso_max_size ^ max_size) {
2757 netif_set_gso_max_size(dev, max_size);
2758 status |= DO_SETLINK_MODIFIED;
2759 }
2760 }
2761
2762 if (tb[IFLA_GSO_MAX_SEGS]) {
2763 u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
2764
2765 if (max_segs > GSO_MAX_SEGS) {
2766 err = -EINVAL;
2767 goto errout;
2768 }
2769
2770 if (dev->gso_max_segs ^ max_segs) {
2771 dev->gso_max_segs = max_segs;
2772 status |= DO_SETLINK_MODIFIED;
2773 }
2774 }
2775
Patrick McHardy0157f602007-06-13 12:03:36 -07002776 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002777 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Patrick McHardy0157f602007-06-13 12:03:36 -07002778
2779 if (tb[IFLA_LINKMODE]) {
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002780 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2781
David S. Miller93b2d4a2008-02-17 18:35:07 -08002782 write_lock_bh(&dev_base_lock);
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002783 if (dev->link_mode ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02002784 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002785 dev->link_mode = value;
David S. Miller93b2d4a2008-02-17 18:35:07 -08002786 write_unlock_bh(&dev_base_lock);
Patrick McHardy0157f602007-06-13 12:03:36 -07002787 }
2788
Chris Wrightc02db8c2010-05-16 01:05:45 -07002789 if (tb[IFLA_VFINFO_LIST]) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002790 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
Chris Wrightc02db8c2010-05-16 01:05:45 -07002791 struct nlattr *attr;
2792 int rem;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002793
Chris Wrightc02db8c2010-05-16 01:05:45 -07002794 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002795 if (nla_type(attr) != IFLA_VF_INFO ||
2796 nla_len(attr) < NLA_HDRLEN) {
David Howells253683b2010-05-21 02:25:27 +00002797 err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002798 goto errout;
David Howells253683b2010-05-21 02:25:27 +00002799 }
Johannes Berg8cb08172019-04-26 14:07:28 +02002800 err = nla_parse_nested_deprecated(vfinfo, IFLA_VF_MAX,
2801 attr,
2802 ifla_vf_policy,
2803 NULL);
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002804 if (err < 0)
2805 goto errout;
2806 err = do_setvfinfo(dev, vfinfo);
Chris Wrightc02db8c2010-05-16 01:05:45 -07002807 if (err < 0)
2808 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002809 status |= DO_SETLINK_NOTIFY;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002810 }
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00002811 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002812 err = 0;
2813
Scott Feldman57b61082010-05-17 22:49:55 -07002814 if (tb[IFLA_VF_PORTS]) {
2815 struct nlattr *port[IFLA_PORT_MAX+1];
2816 struct nlattr *attr;
2817 int vf;
2818 int rem;
2819
2820 err = -EOPNOTSUPP;
2821 if (!ops->ndo_set_vf_port)
2822 goto errout;
2823
2824 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
Daniel Borkmann035d2102015-07-13 00:06:02 +02002825 if (nla_type(attr) != IFLA_VF_PORT ||
2826 nla_len(attr) < NLA_HDRLEN) {
2827 err = -EINVAL;
2828 goto errout;
2829 }
Johannes Berg8cb08172019-04-26 14:07:28 +02002830 err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
2831 attr,
2832 ifla_port_policy,
2833 NULL);
Scott Feldman57b61082010-05-17 22:49:55 -07002834 if (err < 0)
2835 goto errout;
2836 if (!port[IFLA_PORT_VF]) {
2837 err = -EOPNOTSUPP;
2838 goto errout;
2839 }
2840 vf = nla_get_u32(port[IFLA_PORT_VF]);
2841 err = ops->ndo_set_vf_port(dev, vf, port);
2842 if (err < 0)
2843 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002844 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07002845 }
2846 }
2847 err = 0;
2848
2849 if (tb[IFLA_PORT_SELF]) {
2850 struct nlattr *port[IFLA_PORT_MAX+1];
2851
Johannes Berg8cb08172019-04-26 14:07:28 +02002852 err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
2853 tb[IFLA_PORT_SELF],
2854 ifla_port_policy, NULL);
Scott Feldman57b61082010-05-17 22:49:55 -07002855 if (err < 0)
2856 goto errout;
2857
2858 err = -EOPNOTSUPP;
2859 if (ops->ndo_set_vf_port)
2860 err = ops->ndo_set_vf_port(dev, PORT_SELF_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 }
Thomas Graff8ff1822010-11-16 04:30:14 +00002865
2866 if (tb[IFLA_AF_SPEC]) {
2867 struct nlattr *af;
2868 int rem;
2869
2870 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2871 const struct rtnl_af_ops *af_ops;
2872
Gustavo A. R. Silva058c8d52017-10-20 19:43:11 -05002873 BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
Thomas Graff8ff1822010-11-16 04:30:14 +00002874
Stephen Hemminger3583a4e2021-04-07 08:59:12 -07002875 err = af_ops->set_link_af(dev, af, extack);
Cong Wanga1002432021-05-08 11:00:33 -07002876 if (err < 0)
Thomas Graff8ff1822010-11-16 04:30:14 +00002877 goto errout;
2878
Nicolas Dichtelba998902014-09-01 16:07:29 +02002879 status |= DO_SETLINK_NOTIFY;
Thomas Graff8ff1822010-11-16 04:30:14 +00002880 }
2881 }
Scott Feldman57b61082010-05-17 22:49:55 -07002882 err = 0;
2883
Roopa Prabhu829eb202020-07-31 17:34:01 -07002884 if (tb[IFLA_PROTO_DOWN] || tb[IFLA_PROTO_DOWN_REASON]) {
2885 err = do_set_proto_down(dev, tb[IFLA_PROTO_DOWN],
2886 tb[IFLA_PROTO_DOWN_REASON], extack);
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07002887 if (err)
2888 goto errout;
2889 status |= DO_SETLINK_NOTIFY;
2890 }
2891
Brenden Blancod1fdd912016-07-19 12:16:49 -07002892 if (tb[IFLA_XDP]) {
2893 struct nlattr *xdp[IFLA_XDP_MAX + 1];
Daniel Borkmann85de8572016-11-28 23:16:54 +01002894 u32 xdp_flags = 0;
Brenden Blancod1fdd912016-07-19 12:16:49 -07002895
Johannes Berg8cb08172019-04-26 14:07:28 +02002896 err = nla_parse_nested_deprecated(xdp, IFLA_XDP_MAX,
2897 tb[IFLA_XDP],
2898 ifla_xdp_policy, NULL);
Brenden Blancod1fdd912016-07-19 12:16:49 -07002899 if (err < 0)
2900 goto errout;
2901
Martin KaFai Lau58038692017-06-15 17:29:09 -07002902 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
Brenden Blanco262d8622016-07-20 17:22:34 -07002903 err = -EINVAL;
2904 goto errout;
2905 }
Daniel Borkmann85de8572016-11-28 23:16:54 +01002906
2907 if (xdp[IFLA_XDP_FLAGS]) {
2908 xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
2909 if (xdp_flags & ~XDP_FLAGS_MASK) {
2910 err = -EINVAL;
2911 goto errout;
2912 }
Jakub Kicinskiee5d0322017-06-21 18:25:04 -07002913 if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
Daniel Borkmann0489df92017-05-12 01:04:45 +02002914 err = -EINVAL;
2915 goto errout;
2916 }
Daniel Borkmann85de8572016-11-28 23:16:54 +01002917 }
2918
Brenden Blancod1fdd912016-07-19 12:16:49 -07002919 if (xdp[IFLA_XDP_FD]) {
Toke Høiland-Jørgensen92234c82020-03-25 18:23:26 +01002920 int expected_fd = -1;
2921
2922 if (xdp_flags & XDP_FLAGS_REPLACE) {
2923 if (!xdp[IFLA_XDP_EXPECTED_FD]) {
2924 err = -EINVAL;
2925 goto errout;
2926 }
2927 expected_fd =
2928 nla_get_s32(xdp[IFLA_XDP_EXPECTED_FD]);
2929 }
2930
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002931 err = dev_change_xdp_fd(dev, extack,
Daniel Borkmann85de8572016-11-28 23:16:54 +01002932 nla_get_s32(xdp[IFLA_XDP_FD]),
Toke Høiland-Jørgensen92234c82020-03-25 18:23:26 +01002933 expected_fd,
Daniel Borkmann85de8572016-11-28 23:16:54 +01002934 xdp_flags);
Brenden Blancod1fdd912016-07-19 12:16:49 -07002935 if (err)
2936 goto errout;
2937 status |= DO_SETLINK_NOTIFY;
2938 }
2939 }
2940
Patrick McHardy0157f602007-06-13 12:03:36 -07002941errout:
Nicolas Dichtelba998902014-09-01 16:07:29 +02002942 if (status & DO_SETLINK_MODIFIED) {
Xin Long64ff90c2017-10-15 18:13:45 +08002943 if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
Nicolas Dichtelba998902014-09-01 16:07:29 +02002944 netdev_state_change(dev);
2945
2946 if (err < 0)
2947 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",
2948 dev->name);
2949 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002950
Patrick McHardy0157f602007-06-13 12:03:36 -07002951 return err;
2952}
2953
Jiri Pirkocc6090e2019-09-30 11:48:19 +02002954static struct net_device *rtnl_dev_get(struct net *net,
2955 struct nlattr *ifname_attr,
Jiri Pirko76c9ac02019-09-30 11:48:20 +02002956 struct nlattr *altifname_attr,
Jiri Pirkocc6090e2019-09-30 11:48:19 +02002957 char *ifname)
2958{
Jiri Pirko76c9ac02019-09-30 11:48:20 +02002959 char buffer[ALTIFNAMSIZ];
Jiri Pirkocc6090e2019-09-30 11:48:19 +02002960
2961 if (!ifname) {
2962 ifname = buffer;
2963 if (ifname_attr)
Francis Laniel872f6902020-11-15 18:08:06 +01002964 nla_strscpy(ifname, ifname_attr, IFNAMSIZ);
Jiri Pirko76c9ac02019-09-30 11:48:20 +02002965 else if (altifname_attr)
Francis Laniel872f6902020-11-15 18:08:06 +01002966 nla_strscpy(ifname, altifname_attr, ALTIFNAMSIZ);
Jiri Pirkocc6090e2019-09-30 11:48:19 +02002967 else
2968 return NULL;
2969 }
2970
2971 return __dev_get_by_name(net, ifname);
2972}
2973
David Ahernc21ef3e2017-04-16 09:48:24 -07002974static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2975 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002977 struct net *net = sock_net(skb->sk);
Thomas Grafda5e0492006-08-10 21:17:37 -07002978 struct ifinfomsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 struct net_device *dev;
Patrick McHardy0157f602007-06-13 12:03:36 -07002980 int err;
Thomas Grafda5e0492006-08-10 21:17:37 -07002981 struct nlattr *tb[IFLA_MAX+1];
2982 char ifname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983
Johannes Berg8cb08172019-04-26 14:07:28 +02002984 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
2985 ifla_policy, extack);
Thomas Grafda5e0492006-08-10 21:17:37 -07002986 if (err < 0)
2987 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002989 err = rtnl_ensure_unique_netns(tb, extack, false);
2990 if (err < 0)
2991 goto errout;
2992
Thomas Graf5176f912006-08-26 20:13:18 -07002993 if (tb[IFLA_IFNAME])
Francis Laniel872f6902020-11-15 18:08:06 +01002994 nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
Patrick McHardy78e5b8912006-09-13 20:35:36 -07002995 else
2996 ifname[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 err = -EINVAL;
Thomas Grafda5e0492006-08-10 21:17:37 -07002999 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -07003000 if (ifm->ifi_index > 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00003001 dev = __dev_get_by_index(net, ifm->ifi_index);
Jiri Pirko76c9ac02019-09-30 11:48:20 +02003002 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3003 dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname);
Thomas Grafda5e0492006-08-10 21:17:37 -07003004 else
3005 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
Thomas Grafda5e0492006-08-10 21:17:37 -07003007 if (dev == NULL) {
3008 err = -ENODEV;
3009 goto errout;
3010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07003012 err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
Thomas Grafda5e0492006-08-10 21:17:37 -07003013errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 return err;
3015}
3016
WANG Cong66400d52015-03-24 11:53:31 -07003017static int rtnl_group_dellink(const struct net *net, int group)
3018{
3019 struct net_device *dev, *aux;
3020 LIST_HEAD(list_kill);
3021 bool found = false;
3022
3023 if (!group)
3024 return -EPERM;
3025
3026 for_each_netdev(net, dev) {
3027 if (dev->group == group) {
3028 const struct rtnl_link_ops *ops;
3029
3030 found = true;
3031 ops = dev->rtnl_link_ops;
3032 if (!ops || !ops->dellink)
3033 return -EOPNOTSUPP;
3034 }
3035 }
3036
3037 if (!found)
3038 return -ENODEV;
3039
3040 for_each_netdev_safe(net, dev, aux) {
3041 if (dev->group == group) {
3042 const struct rtnl_link_ops *ops;
3043
3044 ops = dev->rtnl_link_ops;
3045 ops->dellink(dev, &list_kill);
3046 }
3047 }
3048 unregister_netdevice_many(&list_kill);
3049
3050 return 0;
3051}
3052
Thomas Graf614732e2015-07-21 10:44:06 +02003053int rtnl_delete_link(struct net_device *dev)
3054{
3055 const struct rtnl_link_ops *ops;
3056 LIST_HEAD(list_kill);
3057
3058 ops = dev->rtnl_link_ops;
3059 if (!ops || !ops->dellink)
3060 return -EOPNOTSUPP;
3061
3062 ops->dellink(dev, &list_kill);
3063 unregister_netdevice_many(&list_kill);
3064
3065 return 0;
3066}
3067EXPORT_SYMBOL_GPL(rtnl_delete_link);
3068
David Ahernc21ef3e2017-04-16 09:48:24 -07003069static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
3070 struct netlink_ext_ack *extack)
Patrick McHardy38f7b872007-06-13 12:03:51 -07003071{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003072 struct net *net = sock_net(skb->sk);
Christian Braunerb61ad682018-01-24 15:26:34 +01003073 struct net *tgt_net = net;
3074 struct net_device *dev = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003075 struct ifinfomsg *ifm;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003076 struct nlattr *tb[IFLA_MAX+1];
3077 int err;
Christian Braunerb61ad682018-01-24 15:26:34 +01003078 int netnsid = -1;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003079
Johannes Berg8cb08172019-04-26 14:07:28 +02003080 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3081 ifla_policy, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07003082 if (err < 0)
3083 return err;
3084
Christian Brauner4ff66ca2018-02-07 13:53:20 +01003085 err = rtnl_ensure_unique_netns(tb, extack, true);
3086 if (err < 0)
3087 return err;
3088
Christian Brauner7e4a8d52018-09-04 21:53:53 +02003089 if (tb[IFLA_TARGET_NETNSID]) {
3090 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
Christian Braunerc383edc2018-09-04 21:53:47 +02003091 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
Christian Braunerb61ad682018-01-24 15:26:34 +01003092 if (IS_ERR(tgt_net))
3093 return PTR_ERR(tgt_net);
3094 }
3095
3096 err = -EINVAL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003097 ifm = nlmsg_data(nlh);
3098 if (ifm->ifi_index > 0)
Christian Braunerb61ad682018-01-24 15:26:34 +01003099 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
Jiri Pirko76c9ac02019-09-30 11:48:20 +02003100 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3101 dev = rtnl_dev_get(net, tb[IFLA_IFNAME],
3102 tb[IFLA_ALT_IFNAME], NULL);
WANG Cong66400d52015-03-24 11:53:31 -07003103 else if (tb[IFLA_GROUP])
Christian Braunerb61ad682018-01-24 15:26:34 +01003104 err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP]));
Patrick McHardy38f7b872007-06-13 12:03:51 -07003105 else
Christian Braunerb61ad682018-01-24 15:26:34 +01003106 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003107
Christian Braunerb61ad682018-01-24 15:26:34 +01003108 if (!dev) {
3109 if (tb[IFLA_IFNAME] || ifm->ifi_index > 0)
3110 err = -ENODEV;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003111
Christian Braunerb61ad682018-01-24 15:26:34 +01003112 goto out;
3113 }
3114
3115 err = rtnl_delete_link(dev);
3116
3117out:
3118 if (netnsid >= 0)
3119 put_net(tgt_net);
3120
3121 return err;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003122}
3123
Patrick McHardy3729d502010-02-26 06:34:54 +00003124int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
3125{
3126 unsigned int old_flags;
3127 int err;
3128
3129 old_flags = dev->flags;
3130 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
Petr Machata6d040322018-12-06 17:05:43 +00003131 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
3132 NULL);
Patrick McHardy3729d502010-02-26 06:34:54 +00003133 if (err < 0)
3134 return err;
3135 }
3136
Roopa Prabhu8d356b82018-07-04 16:46:29 -07003137 if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) {
Roopa Prabhu56a49d72018-09-12 13:21:48 -07003138 __dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags));
Roopa Prabhu8d356b82018-07-04 16:46:29 -07003139 } else {
3140 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
3141 __dev_notify_flags(dev, old_flags, ~0U);
3142 }
Patrick McHardy3729d502010-02-26 06:34:54 +00003143 return 0;
3144}
3145EXPORT_SYMBOL(rtnl_configure_link);
3146
David Ahernd0522f12018-11-06 12:51:14 -08003147struct net_device *rtnl_create_link(struct net *net, const char *ifname,
3148 unsigned char name_assign_type,
3149 const struct rtnl_link_ops *ops,
3150 struct nlattr *tb[],
3151 struct netlink_ext_ack *extack)
Pavel Emelianove7199282007-08-08 22:16:38 -07003152{
Pavel Emelianove7199282007-08-08 22:16:38 -07003153 struct net_device *dev;
Jiri Pirkod40156a2012-07-20 02:28:47 +00003154 unsigned int num_tx_queues = 1;
3155 unsigned int num_rx_queues = 1;
Pavel Emelianove7199282007-08-08 22:16:38 -07003156
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00003157 if (tb[IFLA_NUM_TX_QUEUES])
3158 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
3159 else if (ops->get_num_tx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00003160 num_tx_queues = ops->get_num_tx_queues();
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00003161
3162 if (tb[IFLA_NUM_RX_QUEUES])
3163 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
3164 else if (ops->get_num_rx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00003165 num_rx_queues = ops->get_num_rx_queues();
stephen hemmingerefacb302012-04-10 18:34:43 +00003166
David Ahernd0522f12018-11-06 12:51:14 -08003167 if (num_tx_queues < 1 || num_tx_queues > 4096) {
3168 NL_SET_ERR_MSG(extack, "Invalid number of transmit queues");
Eric Dumazet0e1d6ec2018-10-02 15:47:35 -07003169 return ERR_PTR(-EINVAL);
David Ahernd0522f12018-11-06 12:51:14 -08003170 }
Eric Dumazet0e1d6ec2018-10-02 15:47:35 -07003171
David Ahernd0522f12018-11-06 12:51:14 -08003172 if (num_rx_queues < 1 || num_rx_queues > 4096) {
3173 NL_SET_ERR_MSG(extack, "Invalid number of receive queues");
Eric Dumazet0e1d6ec2018-10-02 15:47:35 -07003174 return ERR_PTR(-EINVAL);
David Ahernd0522f12018-11-06 12:51:14 -08003175 }
Eric Dumazet0e1d6ec2018-10-02 15:47:35 -07003176
Johannes Berg8c713dc2021-06-12 10:20:54 +02003177 if (ops->alloc) {
3178 dev = ops->alloc(tb, ifname, name_assign_type,
3179 num_tx_queues, num_rx_queues);
3180 if (IS_ERR(dev))
3181 return dev;
3182 } else {
3183 dev = alloc_netdev_mqs(ops->priv_size, ifname,
3184 name_assign_type, ops->setup,
3185 num_tx_queues, num_rx_queues);
3186 }
3187
Pavel Emelianove7199282007-08-08 22:16:38 -07003188 if (!dev)
Tobias Klauserd1892e42017-02-20 16:32:06 +01003189 return ERR_PTR(-ENOMEM);
Pavel Emelianove7199282007-08-08 22:16:38 -07003190
Eric W. Biederman81adee42009-11-08 00:53:51 -08003191 dev_net_set(dev, net);
3192 dev->rtnl_link_ops = ops;
Patrick McHardy3729d502010-02-26 06:34:54 +00003193 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
Eric W. Biederman81adee42009-11-08 00:53:51 -08003194
Eric Dumazetd836f5c2020-01-21 22:47:29 -08003195 if (tb[IFLA_MTU]) {
3196 u32 mtu = nla_get_u32(tb[IFLA_MTU]);
3197 int err;
3198
3199 err = dev_validate_mtu(dev, mtu, extack);
3200 if (err) {
3201 free_netdev(dev);
3202 return ERR_PTR(err);
3203 }
3204 dev->mtu = mtu;
3205 }
Jiri Pirko2afb9b52013-01-06 12:41:57 +00003206 if (tb[IFLA_ADDRESS]) {
Pavel Emelianove7199282007-08-08 22:16:38 -07003207 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
3208 nla_len(tb[IFLA_ADDRESS]));
Jiri Pirko2afb9b52013-01-06 12:41:57 +00003209 dev->addr_assign_type = NET_ADDR_SET;
3210 }
Pavel Emelianove7199282007-08-08 22:16:38 -07003211 if (tb[IFLA_BROADCAST])
3212 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
3213 nla_len(tb[IFLA_BROADCAST]));
3214 if (tb[IFLA_TXQLEN])
3215 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
3216 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08003217 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Pavel Emelianove7199282007-08-08 22:16:38 -07003218 if (tb[IFLA_LINKMODE])
3219 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
Patrick McHardyffa934f2011-01-20 03:00:42 +00003220 if (tb[IFLA_GROUP])
3221 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Stephen Hemminger46e6b992017-12-07 15:40:19 -08003222 if (tb[IFLA_GSO_MAX_SIZE])
3223 netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE]));
3224 if (tb[IFLA_GSO_MAX_SEGS])
Stephen Hemmingera0b586f2017-12-08 15:34:13 -08003225 dev->gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
Pavel Emelianove7199282007-08-08 22:16:38 -07003226
3227 return dev;
Pavel Emelianove7199282007-08-08 22:16:38 -07003228}
Eric Dumazete0d087a2009-11-07 01:26:17 -08003229EXPORT_SYMBOL(rtnl_create_link);
Pavel Emelianove7199282007-08-08 22:16:38 -07003230
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07003231static int rtnl_group_changelink(const struct sk_buff *skb,
3232 struct net *net, int group,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00003233 struct ifinfomsg *ifm,
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07003234 struct netlink_ext_ack *extack,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00003235 struct nlattr **tb)
3236{
WANG Congd0795352015-03-23 16:31:09 -07003237 struct net_device *dev, *aux;
Vlad Dogarue7ed8282011-01-13 23:38:31 +00003238 int err;
3239
WANG Congd0795352015-03-23 16:31:09 -07003240 for_each_netdev_safe(net, dev, aux) {
Vlad Dogarue7ed8282011-01-13 23:38:31 +00003241 if (dev->group == group) {
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07003242 err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00003243 if (err < 0)
3244 return err;
3245 }
3246 }
3247
3248 return 0;
3249}
3250
Jakub Kicinskia2939742018-11-27 22:32:31 -08003251static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3252 struct nlattr **attr, struct netlink_ext_ack *extack)
Patrick McHardy38f7b872007-06-13 12:03:51 -07003253{
Jakub Kicinski420d0312018-11-27 22:32:30 -08003254 struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1];
3255 unsigned char name_assign_type = NET_NAME_USER;
3256 struct nlattr *linkinfo[IFLA_INFO_MAX + 1];
3257 const struct rtnl_link_ops *m_ops = NULL;
Jakub Kicinski420d0312018-11-27 22:32:30 -08003258 struct net_device *master_dev = NULL;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003259 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07003260 const struct rtnl_link_ops *ops;
Jakub Kicinski420d0312018-11-27 22:32:30 -08003261 struct nlattr *tb[IFLA_MAX + 1];
3262 struct net *dest_net, *link_net;
3263 struct nlattr **slave_data;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003264 char kind[MODULE_NAME_LEN];
Jakub Kicinski420d0312018-11-27 22:32:30 -08003265 struct net_device *dev;
3266 struct ifinfomsg *ifm;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003267 char ifname[IFNAMSIZ];
Jakub Kicinski420d0312018-11-27 22:32:30 -08003268 struct nlattr **data;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003269 int err;
3270
Johannes Berg95a5afc2008-10-16 15:24:51 -07003271#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07003272replay:
Thomas Graf8072f082007-07-31 14:13:50 -07003273#endif
Johannes Berg8cb08172019-04-26 14:07:28 +02003274 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3275 ifla_policy, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07003276 if (err < 0)
3277 return err;
3278
Christian Brauner4ff66ca2018-02-07 13:53:20 +01003279 err = rtnl_ensure_unique_netns(tb, extack, false);
3280 if (err < 0)
3281 return err;
3282
Patrick McHardy38f7b872007-06-13 12:03:51 -07003283 if (tb[IFLA_IFNAME])
Francis Laniel872f6902020-11-15 18:08:06 +01003284 nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
Patrick McHardy38f7b872007-06-13 12:03:51 -07003285 else
3286 ifname[0] = '\0';
3287
3288 ifm = nlmsg_data(nlh);
3289 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07003290 dev = __dev_get_by_index(net, ifm->ifi_index);
Jiri Pirko76c9ac02019-09-30 11:48:20 +02003291 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3292 dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname);
Jiri Pirko7af12cb2019-09-30 11:48:18 +02003293 else
3294 dev = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003295
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01003296 if (dev) {
3297 master_dev = netdev_master_upper_dev_get(dev);
3298 if (master_dev)
3299 m_ops = master_dev->rtnl_link_ops;
3300 }
3301
Rocco Yue8679c312021-08-03 20:02:50 +08003302 err = validate_linkmsg(dev, tb, extack);
Eric Dumazete0d087a2009-11-07 01:26:17 -08003303 if (err < 0)
Thomas Graf1840bb12008-02-23 19:54:36 -08003304 return err;
3305
Patrick McHardy38f7b872007-06-13 12:03:51 -07003306 if (tb[IFLA_LINKINFO]) {
Johannes Berg8cb08172019-04-26 14:07:28 +02003307 err = nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX,
3308 tb[IFLA_LINKINFO],
3309 ifla_info_policy, NULL);
Patrick McHardy38f7b872007-06-13 12:03:51 -07003310 if (err < 0)
3311 return err;
3312 } else
3313 memset(linkinfo, 0, sizeof(linkinfo));
3314
3315 if (linkinfo[IFLA_INFO_KIND]) {
Francis Laniel872f6902020-11-15 18:08:06 +01003316 nla_strscpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
Patrick McHardy38f7b872007-06-13 12:03:51 -07003317 ops = rtnl_link_ops_get(kind);
3318 } else {
3319 kind[0] = '\0';
3320 ops = NULL;
3321 }
3322
Jakub Kicinski420d0312018-11-27 22:32:30 -08003323 data = NULL;
3324 if (ops) {
3325 if (ops->maxtype > RTNL_MAX_TYPE)
3326 return -EINVAL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003327
Jakub Kicinski420d0312018-11-27 22:32:30 -08003328 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
Johannes Berg8cb08172019-04-26 14:07:28 +02003329 err = nla_parse_nested_deprecated(attr, ops->maxtype,
3330 linkinfo[IFLA_INFO_DATA],
3331 ops->policy, extack);
Jakub Kicinski420d0312018-11-27 22:32:30 -08003332 if (err < 0)
3333 return err;
3334 data = attr;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003335 }
Jakub Kicinski420d0312018-11-27 22:32:30 -08003336 if (ops->validate) {
3337 err = ops->validate(tb, data, extack);
3338 if (err < 0)
3339 return err;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01003340 }
Jakub Kicinski420d0312018-11-27 22:32:30 -08003341 }
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01003342
Jakub Kicinski420d0312018-11-27 22:32:30 -08003343 slave_data = NULL;
3344 if (m_ops) {
3345 if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)
3346 return -EINVAL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003347
Jakub Kicinski420d0312018-11-27 22:32:30 -08003348 if (m_ops->slave_maxtype &&
3349 linkinfo[IFLA_INFO_SLAVE_DATA]) {
Johannes Berg8cb08172019-04-26 14:07:28 +02003350 err = nla_parse_nested_deprecated(slave_attr,
3351 m_ops->slave_maxtype,
3352 linkinfo[IFLA_INFO_SLAVE_DATA],
3353 m_ops->slave_policy,
3354 extack);
Jakub Kicinski420d0312018-11-27 22:32:30 -08003355 if (err < 0)
3356 return err;
3357 slave_data = slave_attr;
3358 }
3359 }
3360
3361 if (dev) {
3362 int status = 0;
3363
3364 if (nlh->nlmsg_flags & NLM_F_EXCL)
3365 return -EEXIST;
3366 if (nlh->nlmsg_flags & NLM_F_REPLACE)
3367 return -EOPNOTSUPP;
3368
3369 if (linkinfo[IFLA_INFO_DATA]) {
3370 if (!ops || ops != dev->rtnl_link_ops ||
3371 !ops->changelink)
Patrick McHardy38f7b872007-06-13 12:03:51 -07003372 return -EOPNOTSUPP;
3373
Jakub Kicinski420d0312018-11-27 22:32:30 -08003374 err = ops->changelink(dev, tb, data, extack);
3375 if (err < 0)
3376 return err;
3377 status |= DO_SETLINK_NOTIFY;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003378 }
3379
Jakub Kicinski420d0312018-11-27 22:32:30 -08003380 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
3381 if (!m_ops || !m_ops->slave_changelink)
3382 return -EOPNOTSUPP;
3383
3384 err = m_ops->slave_changelink(master_dev, dev, tb,
3385 slave_data, extack);
3386 if (err < 0)
3387 return err;
3388 status |= DO_SETLINK_NOTIFY;
3389 }
3390
3391 return do_setlink(skb, dev, ifm, extack, tb, ifname, status);
3392 }
3393
3394 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
3395 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
3396 return rtnl_group_changelink(skb, net,
Patrick McHardyffa934f2011-01-20 03:00:42 +00003397 nla_get_u32(tb[IFLA_GROUP]),
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07003398 ifm, extack, tb);
Jakub Kicinski420d0312018-11-27 22:32:30 -08003399 return -ENODEV;
3400 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07003401
Jakub Kicinski420d0312018-11-27 22:32:30 -08003402 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
3403 return -EOPNOTSUPP;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003404
Jakub Kicinski420d0312018-11-27 22:32:30 -08003405 if (!ops) {
Johannes Berg95a5afc2008-10-16 15:24:51 -07003406#ifdef CONFIG_MODULES
Jakub Kicinski420d0312018-11-27 22:32:30 -08003407 if (kind[0]) {
3408 __rtnl_unlock();
3409 request_module("rtnl-link-%s", kind);
3410 rtnl_lock();
3411 ops = rtnl_link_ops_get(kind);
3412 if (ops)
3413 goto replay;
3414 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07003415#endif
Jakub Kicinski420d0312018-11-27 22:32:30 -08003416 NL_SET_ERR_MSG(extack, "Unknown device type");
3417 return -EOPNOTSUPP;
3418 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07003419
Johannes Berg8c713dc2021-06-12 10:20:54 +02003420 if (!ops->alloc && !ops->setup)
Jakub Kicinski420d0312018-11-27 22:32:30 -08003421 return -EOPNOTSUPP;
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02003422
Jakub Kicinski420d0312018-11-27 22:32:30 -08003423 if (!ifname[0]) {
3424 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
3425 name_assign_type = NET_NAME_ENUM;
3426 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07003427
Jakub Kicinski420d0312018-11-27 22:32:30 -08003428 dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN);
3429 if (IS_ERR(dest_net))
3430 return PTR_ERR(dest_net);
Eric W. Biederman13ad1772011-01-29 14:57:22 +00003431
Jakub Kicinski420d0312018-11-27 22:32:30 -08003432 if (tb[IFLA_LINK_NETNSID]) {
3433 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
Nicolas Dichtel317f4812015-01-15 15:11:18 +01003434
Jakub Kicinski420d0312018-11-27 22:32:30 -08003435 link_net = get_net_ns_by_id(dest_net, id);
3436 if (!link_net) {
3437 NL_SET_ERR_MSG(extack, "Unknown network namespace id");
3438 err = -EINVAL;
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00003439 goto out;
3440 }
Jakub Kicinski420d0312018-11-27 22:32:30 -08003441 err = -EPERM;
3442 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
3443 goto out;
3444 } else {
3445 link_net = NULL;
3446 }
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00003447
Jakub Kicinski420d0312018-11-27 22:32:30 -08003448 dev = rtnl_create_link(link_net ? : dest_net, ifname,
3449 name_assign_type, ops, tb, extack);
3450 if (IS_ERR(dev)) {
3451 err = PTR_ERR(dev);
David S. Miller43638902015-03-10 21:58:32 -04003452 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003453 }
Jakub Kicinski420d0312018-11-27 22:32:30 -08003454
3455 dev->ifindex = ifm->ifi_index;
3456
Jakub Kicinskic269a242021-01-06 10:40:06 -08003457 if (ops->newlink)
Jakub Kicinski420d0312018-11-27 22:32:30 -08003458 err = ops->newlink(link_net ? : net, dev, tb, data, extack);
Jakub Kicinskic269a242021-01-06 10:40:06 -08003459 else
Jakub Kicinski420d0312018-11-27 22:32:30 -08003460 err = register_netdevice(dev);
Jakub Kicinskic269a242021-01-06 10:40:06 -08003461 if (err < 0) {
3462 free_netdev(dev);
3463 goto out;
Jakub Kicinski420d0312018-11-27 22:32:30 -08003464 }
Jakub Kicinskic269a242021-01-06 10:40:06 -08003465
Jakub Kicinski420d0312018-11-27 22:32:30 -08003466 err = rtnl_configure_link(dev, ifm);
3467 if (err < 0)
3468 goto out_unregister;
3469 if (link_net) {
Andrei Vagin0854fa82021-04-06 23:40:51 -07003470 err = dev_change_net_namespace(dev, dest_net, ifname);
Jakub Kicinski420d0312018-11-27 22:32:30 -08003471 if (err < 0)
3472 goto out_unregister;
3473 }
3474 if (tb[IFLA_MASTER]) {
3475 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
3476 if (err)
3477 goto out_unregister;
3478 }
3479out:
3480 if (link_net)
3481 put_net(link_net);
3482 put_net(dest_net);
3483 return err;
3484out_unregister:
3485 if (ops->newlink) {
3486 LIST_HEAD(list_kill);
3487
3488 ops->dellink(dev, &list_kill);
3489 unregister_netdevice_many(&list_kill);
3490 } else {
3491 unregister_netdevice(dev);
3492 }
3493 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003494}
3495
Jakub Kicinskia2939742018-11-27 22:32:31 -08003496static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3497 struct netlink_ext_ack *extack)
3498{
3499 struct nlattr **attr;
3500 int ret;
3501
3502 attr = kmalloc_array(RTNL_MAX_TYPE + 1, sizeof(*attr), GFP_KERNEL);
3503 if (!attr)
3504 return -ENOMEM;
3505
3506 ret = __rtnl_newlink(skb, nlh, attr, extack);
3507 kfree(attr);
3508 return ret;
3509}
3510
Jakub Kicinski9b3757b2019-01-18 10:46:16 -08003511static int rtnl_valid_getlink_req(struct sk_buff *skb,
3512 const struct nlmsghdr *nlh,
3513 struct nlattr **tb,
3514 struct netlink_ext_ack *extack)
3515{
3516 struct ifinfomsg *ifm;
3517 int i, err;
3518
3519 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
3520 NL_SET_ERR_MSG(extack, "Invalid header for get link");
3521 return -EINVAL;
3522 }
3523
3524 if (!netlink_strict_get_check(skb))
Johannes Berg8cb08172019-04-26 14:07:28 +02003525 return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3526 ifla_policy, extack);
Jakub Kicinski9b3757b2019-01-18 10:46:16 -08003527
3528 ifm = nlmsg_data(nlh);
3529 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
3530 ifm->ifi_change) {
3531 NL_SET_ERR_MSG(extack, "Invalid values in header for get link request");
3532 return -EINVAL;
3533 }
3534
Johannes Berg8cb08172019-04-26 14:07:28 +02003535 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFLA_MAX,
3536 ifla_policy, extack);
Jakub Kicinski9b3757b2019-01-18 10:46:16 -08003537 if (err)
3538 return err;
3539
3540 for (i = 0; i <= IFLA_MAX; i++) {
3541 if (!tb[i])
3542 continue;
3543
3544 switch (i) {
3545 case IFLA_IFNAME:
Jiri Pirko76c9ac02019-09-30 11:48:20 +02003546 case IFLA_ALT_IFNAME:
Jakub Kicinski9b3757b2019-01-18 10:46:16 -08003547 case IFLA_EXT_MASK:
3548 case IFLA_TARGET_NETNSID:
3549 break;
3550 default:
3551 NL_SET_ERR_MSG(extack, "Unsupported attribute in get link request");
3552 return -EINVAL;
3553 }
3554 }
3555
3556 return 0;
3557}
3558
David Ahernc21ef3e2017-04-16 09:48:24 -07003559static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3560 struct netlink_ext_ack *extack)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003561{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003562 struct net *net = sock_net(skb->sk);
Jiri Benc79e1ad12017-11-02 17:04:38 -02003563 struct net *tgt_net = net;
Thomas Grafb60c5112006-08-04 23:05:34 -07003564 struct ifinfomsg *ifm;
3565 struct nlattr *tb[IFLA_MAX+1];
3566 struct net_device *dev = NULL;
3567 struct sk_buff *nskb;
Jiri Benc79e1ad12017-11-02 17:04:38 -02003568 int netnsid = -1;
Thomas Graf339bf982006-11-10 14:10:15 -08003569 int err;
Greg Rose115c9b82012-02-21 16:54:48 -05003570 u32 ext_filter_mask = 0;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003571
Jakub Kicinski9b3757b2019-01-18 10:46:16 -08003572 err = rtnl_valid_getlink_req(skb, nlh, tb, extack);
Thomas Grafb60c5112006-08-04 23:05:34 -07003573 if (err < 0)
Eric Sesterhenn9918f232006-09-26 23:26:38 -07003574 return err;
Thomas Grafb60c5112006-08-04 23:05:34 -07003575
Christian Brauner4ff66ca2018-02-07 13:53:20 +01003576 err = rtnl_ensure_unique_netns(tb, extack, true);
3577 if (err < 0)
3578 return err;
3579
Christian Brauner7e4a8d52018-09-04 21:53:53 +02003580 if (tb[IFLA_TARGET_NETNSID]) {
3581 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
Christian Braunerc383edc2018-09-04 21:53:47 +02003582 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
Jiri Benc79e1ad12017-11-02 17:04:38 -02003583 if (IS_ERR(tgt_net))
3584 return PTR_ERR(tgt_net);
3585 }
3586
Greg Rose115c9b82012-02-21 16:54:48 -05003587 if (tb[IFLA_EXT_MASK])
3588 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3589
Jiri Benc79e1ad12017-11-02 17:04:38 -02003590 err = -EINVAL;
Thomas Grafb60c5112006-08-04 23:05:34 -07003591 ifm = nlmsg_data(nlh);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003592 if (ifm->ifi_index > 0)
Jiri Benc79e1ad12017-11-02 17:04:38 -02003593 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
Jiri Pirko76c9ac02019-09-30 11:48:20 +02003594 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3595 dev = rtnl_dev_get(tgt_net, tb[IFLA_IFNAME],
3596 tb[IFLA_ALT_IFNAME], NULL);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003597 else
Jiri Benc79e1ad12017-11-02 17:04:38 -02003598 goto out;
Thomas Grafb60c5112006-08-04 23:05:34 -07003599
Jiri Benc79e1ad12017-11-02 17:04:38 -02003600 err = -ENODEV;
Eric Dumazeta3d12892009-10-21 10:59:31 +00003601 if (dev == NULL)
Jiri Benc79e1ad12017-11-02 17:04:38 -02003602 goto out;
Eric Dumazeta3d12892009-10-21 10:59:31 +00003603
Jiri Benc79e1ad12017-11-02 17:04:38 -02003604 err = -ENOBUFS;
Greg Rose115c9b82012-02-21 16:54:48 -05003605 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003606 if (nskb == NULL)
Jiri Benc79e1ad12017-11-02 17:04:38 -02003607 goto out;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003608
Jiri Benc79e1ad12017-11-02 17:04:38 -02003609 err = rtnl_fill_ifinfo(nskb, dev, net,
3610 RTM_NEWLINK, NETLINK_CB(skb).portid,
3611 nlh->nlmsg_seq, 0, 0, ext_filter_mask,
Guillaume Naultd4e4fdf2019-10-23 18:39:04 +02003612 0, NULL, 0, netnsid, GFP_KERNEL);
Patrick McHardy26932562007-01-31 23:16:40 -08003613 if (err < 0) {
3614 /* -EMSGSIZE implies BUG in if_nlmsg_size */
3615 WARN_ON(err == -EMSGSIZE);
3616 kfree_skb(nskb);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003617 } else
Eric W. Biederman15e47302012-09-07 20:12:54 +00003618 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
Jiri Benc79e1ad12017-11-02 17:04:38 -02003619out:
3620 if (netnsid >= 0)
3621 put_net(tgt_net);
Thomas Grafb60c5112006-08-04 23:05:34 -07003622
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003623 return err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003624}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003625
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02003626static int rtnl_alt_ifname(int cmd, struct net_device *dev, struct nlattr *attr,
3627 bool *changed, struct netlink_ext_ack *extack)
3628{
3629 char *alt_ifname;
3630 int err;
3631
3632 err = nla_validate(attr, attr->nla_len, IFLA_MAX, ifla_policy, extack);
3633 if (err)
3634 return err;
3635
Eric Dumazet44bfa9c2020-02-12 20:58:26 -08003636 alt_ifname = nla_strdup(attr, GFP_KERNEL);
3637 if (!alt_ifname)
3638 return -ENOMEM;
3639
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02003640 if (cmd == RTM_NEWLINKPROP) {
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02003641 err = netdev_name_node_alt_create(dev, alt_ifname);
Eric Dumazet44bfa9c2020-02-12 20:58:26 -08003642 if (!err)
3643 alt_ifname = NULL;
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02003644 } else if (cmd == RTM_DELLINKPROP) {
3645 err = netdev_name_node_alt_destroy(dev, alt_ifname);
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02003646 } else {
Eric Dumazet44bfa9c2020-02-12 20:58:26 -08003647 WARN_ON_ONCE(1);
3648 err = -EINVAL;
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02003649 }
3650
Eric Dumazet44bfa9c2020-02-12 20:58:26 -08003651 kfree(alt_ifname);
3652 if (!err)
3653 *changed = true;
3654 return err;
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02003655}
3656
3657static int rtnl_linkprop(int cmd, struct sk_buff *skb, struct nlmsghdr *nlh,
3658 struct netlink_ext_ack *extack)
3659{
3660 struct net *net = sock_net(skb->sk);
3661 struct nlattr *tb[IFLA_MAX + 1];
3662 struct net_device *dev;
3663 struct ifinfomsg *ifm;
3664 bool changed = false;
3665 struct nlattr *attr;
3666 int err, rem;
3667
3668 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
3669 if (err)
3670 return err;
3671
3672 err = rtnl_ensure_unique_netns(tb, extack, true);
3673 if (err)
3674 return err;
3675
3676 ifm = nlmsg_data(nlh);
Jiri Pirkocc6090e2019-09-30 11:48:19 +02003677 if (ifm->ifi_index > 0)
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02003678 dev = __dev_get_by_index(net, ifm->ifi_index);
Jiri Pirko76c9ac02019-09-30 11:48:20 +02003679 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3680 dev = rtnl_dev_get(net, tb[IFLA_IFNAME],
3681 tb[IFLA_ALT_IFNAME], NULL);
Jiri Pirkocc6090e2019-09-30 11:48:19 +02003682 else
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02003683 return -EINVAL;
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02003684
3685 if (!dev)
3686 return -ENODEV;
3687
3688 if (!tb[IFLA_PROP_LIST])
3689 return 0;
3690
3691 nla_for_each_nested(attr, tb[IFLA_PROP_LIST], rem) {
3692 switch (nla_type(attr)) {
3693 case IFLA_ALT_IFNAME:
3694 err = rtnl_alt_ifname(cmd, dev, attr, &changed, extack);
3695 if (err)
3696 return err;
3697 break;
3698 }
3699 }
3700
3701 if (changed)
3702 netdev_state_change(dev);
3703 return 0;
3704}
3705
3706static int rtnl_newlinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
3707 struct netlink_ext_ack *extack)
3708{
3709 return rtnl_linkprop(RTM_NEWLINKPROP, skb, nlh, extack);
3710}
3711
3712static int rtnl_dellinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
3713 struct netlink_ext_ack *extack)
3714{
3715 return rtnl_linkprop(RTM_DELLINKPROP, skb, nlh, extack);
3716}
3717
Di Zhuebfe3c52020-10-21 10:00:53 +08003718static u32 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
Greg Rosec7ac8672011-06-10 01:27:09 +00003719{
Greg Rose115c9b82012-02-21 16:54:48 -05003720 struct net *net = sock_net(skb->sk);
Di Zhuebfe3c52020-10-21 10:00:53 +08003721 size_t min_ifinfo_dump_size = 0;
Greg Rose115c9b82012-02-21 16:54:48 -05003722 struct nlattr *tb[IFLA_MAX+1];
3723 u32 ext_filter_mask = 0;
Di Zhuebfe3c52020-10-21 10:00:53 +08003724 struct net_device *dev;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02003725 int hdrlen;
Greg Rose115c9b82012-02-21 16:54:48 -05003726
Michal Schmidte5eca6d2014-05-28 14:15:19 +02003727 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3728 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
3729 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
3730
Johannes Berg8cb08172019-04-26 14:07:28 +02003731 if (nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00003732 if (tb[IFLA_EXT_MASK])
3733 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3734 }
Greg Rose115c9b82012-02-21 16:54:48 -05003735
3736 if (!ext_filter_mask)
3737 return NLMSG_GOODSIZE;
3738 /*
3739 * traverse the list of net devices and compute the minimum
3740 * buffer size based upon the filter mask.
3741 */
Florian Westphal6853dd42017-08-09 20:41:51 +02003742 rcu_read_lock();
3743 for_each_netdev_rcu(net, dev) {
Di Zhuebfe3c52020-10-21 10:00:53 +08003744 min_ifinfo_dump_size = max(min_ifinfo_dump_size,
3745 if_nlmsg_size(dev, ext_filter_mask));
Greg Rose115c9b82012-02-21 16:54:48 -05003746 }
Florian Westphal6853dd42017-08-09 20:41:51 +02003747 rcu_read_unlock();
Greg Rose115c9b82012-02-21 16:54:48 -05003748
Zhang Shengju93af2052016-11-22 14:14:28 +08003749 return nlmsg_total_size(min_ifinfo_dump_size);
Greg Rosec7ac8672011-06-10 01:27:09 +00003750}
3751
Adrian Bunk42bad1d2007-04-26 00:57:41 -07003752static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753{
3754 int idx;
3755 int s_idx = cb->family;
Christian Brauner87ccbb12018-09-04 21:53:51 +02003756 int type = cb->nlh->nlmsg_type - RTM_BASE;
David Ahernc63586d2018-10-24 12:59:02 -07003757 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758
3759 if (s_idx == 0)
3760 s_idx = 1;
Florian Westphal6853dd42017-08-09 20:41:51 +02003761
Patrick McHardy25239ce2010-04-26 16:02:05 +02003762 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
Jakub Kicinski51e13682020-12-09 18:16:08 -08003763 struct rtnl_link __rcu **tab;
Florian Westphaladdf9b92017-12-02 21:44:05 +01003764 struct rtnl_link *link;
Florian Westphal6853dd42017-08-09 20:41:51 +02003765 rtnl_dumpit_func dumpit;
3766
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 if (idx < s_idx || idx == PF_PACKET)
3768 continue;
Florian Westphal6853dd42017-08-09 20:41:51 +02003769
Florian Westphaladdf9b92017-12-02 21:44:05 +01003770 if (type < 0 || type >= RTM_NR_MSGTYPES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 continue;
Florian Westphal6853dd42017-08-09 20:41:51 +02003772
Florian Westphaladdf9b92017-12-02 21:44:05 +01003773 tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]);
3774 if (!tab)
3775 continue;
3776
Jakub Kicinski51e13682020-12-09 18:16:08 -08003777 link = rcu_dereference_rtnl(tab[type]);
Florian Westphaladdf9b92017-12-02 21:44:05 +01003778 if (!link)
3779 continue;
3780
3781 dumpit = link->dumpit;
Florian Westphal6853dd42017-08-09 20:41:51 +02003782 if (!dumpit)
3783 continue;
3784
Nicolas Dichtel04652772013-03-22 06:28:42 +00003785 if (idx > s_idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 memset(&cb->args[0], 0, sizeof(cb->args));
Nicolas Dichtel04652772013-03-22 06:28:42 +00003787 cb->prev_seq = 0;
3788 cb->seq = 0;
3789 }
David Ahernc63586d2018-10-24 12:59:02 -07003790 ret = dumpit(skb, cb);
Alexey Kodanev5e1acb42018-11-02 19:11:04 +03003791 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 break;
3793 }
3794 cb->family = idx;
3795
David Ahernc63586d2018-10-24 12:59:02 -07003796 return skb->len ? : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797}
3798
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003799struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003800 unsigned int change,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003801 u32 event, gfp_t flags, int *new_nsid,
3802 int new_ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003804 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 struct sk_buff *skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003806 int err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00003807 size_t if_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808
Alexei Starovoitov7f294052013-10-23 16:02:42 -07003809 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003810 if (skb == NULL)
3811 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812
Jiri Benc79e1ad12017-11-02 17:04:38 -02003813 err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
3814 type, 0, 0, change, 0, 0, event,
Guillaume Naultd4e4fdf2019-10-23 18:39:04 +02003815 new_nsid, new_ifindex, -1, flags);
Patrick McHardy26932562007-01-31 23:16:40 -08003816 if (err < 0) {
3817 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
3818 WARN_ON(err == -EMSGSIZE);
3819 kfree_skb(skb);
3820 goto errout;
3821 }
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003822 return skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003823errout:
3824 if (err < 0)
Eric W. Biederman4b3da702007-11-19 22:27:40 -08003825 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003826 return NULL;
3827}
3828
3829void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
3830{
3831 struct net *net = dev_net(dev);
3832
3833 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
3834}
3835
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003836static void rtmsg_ifinfo_event(int type, struct net_device *dev,
3837 unsigned int change, u32 event,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003838 gfp_t flags, int *new_nsid, int new_ifindex)
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003839{
3840 struct sk_buff *skb;
3841
Nicolas Dichteled2a80a2015-05-13 14:19:42 +02003842 if (dev->reg_state != NETREG_REGISTERED)
3843 return;
3844
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003845 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid,
3846 new_ifindex);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003847 if (skb)
3848 rtmsg_ifinfo_send(skb, dev, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849}
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003850
3851void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
3852 gfp_t flags)
3853{
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003854 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3855 NULL, 0);
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003856}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003858void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003859 gfp_t flags, int *new_nsid, int new_ifindex)
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003860{
3861 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003862 new_nsid, new_ifindex);
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003863}
3864
John Fastabendd83b0602012-04-15 06:44:08 +00003865static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
3866 struct net_device *dev,
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003867 u8 *addr, u16 vid, u32 pid, u32 seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003868 int type, unsigned int flags,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003869 int nlflags, u16 ndm_state)
John Fastabendd83b0602012-04-15 06:44:08 +00003870{
3871 struct nlmsghdr *nlh;
3872 struct ndmsg *ndm;
3873
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003874 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
John Fastabendd83b0602012-04-15 06:44:08 +00003875 if (!nlh)
3876 return -EMSGSIZE;
3877
3878 ndm = nlmsg_data(nlh);
3879 ndm->ndm_family = AF_BRIDGE;
3880 ndm->ndm_pad1 = 0;
3881 ndm->ndm_pad2 = 0;
3882 ndm->ndm_flags = flags;
3883 ndm->ndm_type = 0;
3884 ndm->ndm_ifindex = dev->ifindex;
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003885 ndm->ndm_state = ndm_state;
John Fastabendd83b0602012-04-15 06:44:08 +00003886
3887 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
3888 goto nla_put_failure;
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003889 if (vid)
3890 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
3891 goto nla_put_failure;
John Fastabendd83b0602012-04-15 06:44:08 +00003892
Johannes Berg053c0952015-01-16 22:09:00 +01003893 nlmsg_end(skb, nlh);
3894 return 0;
John Fastabendd83b0602012-04-15 06:44:08 +00003895
3896nla_put_failure:
3897 nlmsg_cancel(skb, nlh);
3898 return -EMSGSIZE;
3899}
3900
John Fastabend3ff661c2012-04-15 06:44:14 +00003901static inline size_t rtnl_fdb_nlmsg_size(void)
3902{
Sabrina Dubrocaf82ef3e2016-11-18 15:50:39 +01003903 return NLMSG_ALIGN(sizeof(struct ndmsg)) +
3904 nla_total_size(ETH_ALEN) + /* NDA_LLADDR */
3905 nla_total_size(sizeof(u16)) + /* NDA_VLAN */
3906 0;
John Fastabend3ff661c2012-04-15 06:44:14 +00003907}
3908
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003909static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
3910 u16 ndm_state)
John Fastabend3ff661c2012-04-15 06:44:14 +00003911{
3912 struct net *net = dev_net(dev);
3913 struct sk_buff *skb;
3914 int err = -ENOBUFS;
3915
3916 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
3917 if (!skb)
3918 goto errout;
3919
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003920 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003921 0, 0, type, NTF_SELF, 0, ndm_state);
John Fastabend3ff661c2012-04-15 06:44:14 +00003922 if (err < 0) {
3923 kfree_skb(skb);
3924 goto errout;
3925 }
3926
3927 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
3928 return;
3929errout:
3930 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
3931}
3932
Bart Van Asschea9869672019-03-25 09:17:22 -07003933/*
Vlad Yasevich090096b2013-03-06 15:39:42 +00003934 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3935 */
3936int ndo_dflt_fdb_add(struct ndmsg *ndm,
3937 struct nlattr *tb[],
3938 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003939 const unsigned char *addr, u16 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003940 u16 flags)
3941{
3942 int err = -EINVAL;
3943
3944 /* If aging addresses are supported device will need to
3945 * implement its own handler for this.
3946 */
3947 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
Vladimir Oltean78ecc892021-06-29 03:29:26 +03003948 netdev_info(dev, "default FDB implementation only supports local addresses\n");
Vlad Yasevich090096b2013-03-06 15:39:42 +00003949 return err;
3950 }
3951
Or Gerlitz65891fe2014-12-14 18:19:05 +02003952 if (vid) {
Vladimir Oltean23ac0b42021-06-29 03:29:25 +03003953 netdev_info(dev, "vlans aren't supported yet for dev_uc|mc_add()\n");
Or Gerlitz65891fe2014-12-14 18:19:05 +02003954 return err;
3955 }
3956
Vlad Yasevich090096b2013-03-06 15:39:42 +00003957 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3958 err = dev_uc_add_excl(dev, addr);
3959 else if (is_multicast_ether_addr(addr))
3960 err = dev_mc_add_excl(dev, addr);
3961
3962 /* Only return duplicate errors if NLM_F_EXCL is set */
3963 if (err == -EEXIST && !(flags & NLM_F_EXCL))
3964 err = 0;
3965
3966 return err;
3967}
3968EXPORT_SYMBOL(ndo_dflt_fdb_add);
3969
Florian Westphalb88d12e2017-10-10 17:10:04 +02003970static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
3971 struct netlink_ext_ack *extack)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003972{
3973 u16 vid = 0;
3974
3975 if (vlan_attr) {
3976 if (nla_len(vlan_attr) != sizeof(u16)) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003977 NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
Jiri Pirkof6f64242014-11-28 14:34:15 +01003978 return -EINVAL;
3979 }
3980
3981 vid = nla_get_u16(vlan_attr);
3982
3983 if (!vid || vid >= VLAN_VID_MASK) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003984 NL_SET_ERR_MSG(extack, "invalid vlan id");
Jiri Pirkof6f64242014-11-28 14:34:15 +01003985 return -EINVAL;
3986 }
3987 }
3988 *p_vid = vid;
3989 return 0;
3990}
3991
David Ahernc21ef3e2017-04-16 09:48:24 -07003992static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
3993 struct netlink_ext_ack *extack)
John Fastabend77162022012-04-15 06:43:56 +00003994{
3995 struct net *net = sock_net(skb->sk);
John Fastabend77162022012-04-15 06:43:56 +00003996 struct ndmsg *ndm;
3997 struct nlattr *tb[NDA_MAX+1];
3998 struct net_device *dev;
3999 u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01004000 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00004001 int err;
4002
Johannes Berg8cb08172019-04-26 14:07:28 +02004003 err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
4004 extack);
John Fastabend77162022012-04-15 06:43:56 +00004005 if (err < 0)
4006 return err;
4007
4008 ndm = nlmsg_data(nlh);
4009 if (ndm->ndm_ifindex == 0) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004010 NL_SET_ERR_MSG(extack, "invalid ifindex");
John Fastabend77162022012-04-15 06:43:56 +00004011 return -EINVAL;
4012 }
4013
4014 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
4015 if (dev == NULL) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004016 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabend77162022012-04-15 06:43:56 +00004017 return -ENODEV;
4018 }
4019
4020 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004021 NL_SET_ERR_MSG(extack, "invalid address");
John Fastabend77162022012-04-15 06:43:56 +00004022 return -EINVAL;
4023 }
4024
Ido Schimmelda715772018-10-29 20:36:43 +00004025 if (dev->type != ARPHRD_ETHER) {
4026 NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices");
4027 return -EINVAL;
4028 }
4029
John Fastabend77162022012-04-15 06:43:56 +00004030 addr = nla_data(tb[NDA_LLADDR]);
John Fastabend77162022012-04-15 06:43:56 +00004031
Florian Westphalb88d12e2017-10-10 17:10:04 +02004032 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
Jiri Pirkof6f64242014-11-28 14:34:15 +01004033 if (err)
4034 return err;
4035
John Fastabend77162022012-04-15 06:43:56 +00004036 err = -EOPNOTSUPP;
4037
4038 /* Support fdb on master device the net/bridge default case */
4039 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
Julian Wiedmann2e92a2d2020-02-20 09:00:07 +01004040 netif_is_bridge_port(dev)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00004041 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4042 const struct net_device_ops *ops = br_dev->netdev_ops;
4043
Jiri Pirkof6f64242014-11-28 14:34:15 +01004044 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
Petr Machata87b09842019-01-16 23:06:50 +00004045 nlh->nlmsg_flags, extack);
John Fastabend77162022012-04-15 06:43:56 +00004046 if (err)
4047 goto out;
4048 else
4049 ndm->ndm_flags &= ~NTF_MASTER;
4050 }
4051
4052 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00004053 if ((ndm->ndm_flags & NTF_SELF)) {
4054 if (dev->netdev_ops->ndo_fdb_add)
4055 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
Jiri Pirkof6f64242014-11-28 14:34:15 +01004056 vid,
Petr Machata87b09842019-01-16 23:06:50 +00004057 nlh->nlmsg_flags,
4058 extack);
Vlad Yasevich090096b2013-03-06 15:39:42 +00004059 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01004060 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00004061 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00004062
John Fastabend3ff661c2012-04-15 06:44:14 +00004063 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00004064 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
4065 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00004066 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00004067 }
John Fastabend77162022012-04-15 06:43:56 +00004068 }
4069out:
4070 return err;
4071}
4072
Bart Van Asschea9869672019-03-25 09:17:22 -07004073/*
Vlad Yasevich090096b2013-03-06 15:39:42 +00004074 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
4075 */
4076int ndo_dflt_fdb_del(struct ndmsg *ndm,
4077 struct nlattr *tb[],
4078 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01004079 const unsigned char *addr, u16 vid)
Vlad Yasevich090096b2013-03-06 15:39:42 +00004080{
Alexander Duyckc8a89c42014-07-15 15:15:20 -07004081 int err = -EINVAL;
Vlad Yasevich090096b2013-03-06 15:39:42 +00004082
4083 /* If aging addresses are supported device will need to
4084 * implement its own handler for this.
4085 */
Sridhar Samudrala64535992013-08-08 15:19:48 -07004086 if (!(ndm->ndm_state & NUD_PERMANENT)) {
Vladimir Oltean78ecc892021-06-29 03:29:26 +03004087 netdev_info(dev, "default FDB implementation only supports local addresses\n");
Alexander Duyckc8a89c42014-07-15 15:15:20 -07004088 return err;
Vlad Yasevich090096b2013-03-06 15:39:42 +00004089 }
4090
4091 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
4092 err = dev_uc_del(dev, addr);
4093 else if (is_multicast_ether_addr(addr))
4094 err = dev_mc_del(dev, addr);
Vlad Yasevich090096b2013-03-06 15:39:42 +00004095
4096 return err;
4097}
4098EXPORT_SYMBOL(ndo_dflt_fdb_del);
4099
David Ahernc21ef3e2017-04-16 09:48:24 -07004100static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
4101 struct netlink_ext_ack *extack)
John Fastabend77162022012-04-15 06:43:56 +00004102{
4103 struct net *net = sock_net(skb->sk);
4104 struct ndmsg *ndm;
Vlad Yasevich1690be62013-02-13 12:00:18 +00004105 struct nlattr *tb[NDA_MAX+1];
John Fastabend77162022012-04-15 06:43:56 +00004106 struct net_device *dev;
John Fastabend77162022012-04-15 06:43:56 +00004107 __u8 *addr;
Colin Ian King7d311802020-04-25 12:28:14 +01004108 int err;
Jiri Pirkof6f64242014-11-28 14:34:15 +01004109 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00004110
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07004111 if (!netlink_capable(skb, CAP_NET_ADMIN))
Vlad Yasevich1690be62013-02-13 12:00:18 +00004112 return -EPERM;
4113
Johannes Berg8cb08172019-04-26 14:07:28 +02004114 err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
4115 extack);
Vlad Yasevich1690be62013-02-13 12:00:18 +00004116 if (err < 0)
4117 return err;
John Fastabend77162022012-04-15 06:43:56 +00004118
4119 ndm = nlmsg_data(nlh);
4120 if (ndm->ndm_ifindex == 0) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004121 NL_SET_ERR_MSG(extack, "invalid ifindex");
John Fastabend77162022012-04-15 06:43:56 +00004122 return -EINVAL;
4123 }
4124
4125 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
4126 if (dev == NULL) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004127 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabend77162022012-04-15 06:43:56 +00004128 return -ENODEV;
4129 }
4130
Vlad Yasevich1690be62013-02-13 12:00:18 +00004131 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004132 NL_SET_ERR_MSG(extack, "invalid address");
John Fastabend77162022012-04-15 06:43:56 +00004133 return -EINVAL;
4134 }
4135
Ido Schimmelda715772018-10-29 20:36:43 +00004136 if (dev->type != ARPHRD_ETHER) {
4137 NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices");
4138 return -EINVAL;
4139 }
4140
Vlad Yasevich1690be62013-02-13 12:00:18 +00004141 addr = nla_data(tb[NDA_LLADDR]);
Vlad Yasevich1690be62013-02-13 12:00:18 +00004142
Florian Westphalb88d12e2017-10-10 17:10:04 +02004143 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
Jiri Pirkof6f64242014-11-28 14:34:15 +01004144 if (err)
4145 return err;
4146
John Fastabend77162022012-04-15 06:43:56 +00004147 err = -EOPNOTSUPP;
4148
4149 /* Support fdb on master device the net/bridge default case */
4150 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
Julian Wiedmann2e92a2d2020-02-20 09:00:07 +01004151 netif_is_bridge_port(dev)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00004152 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4153 const struct net_device_ops *ops = br_dev->netdev_ops;
John Fastabend77162022012-04-15 06:43:56 +00004154
Jiri Pirko898e5062013-01-03 22:48:52 +00004155 if (ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01004156 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00004157
4158 if (err)
4159 goto out;
4160 else
4161 ndm->ndm_flags &= ~NTF_MASTER;
4162 }
4163
4164 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00004165 if (ndm->ndm_flags & NTF_SELF) {
4166 if (dev->netdev_ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01004167 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
4168 vid);
Vlad Yasevich090096b2013-03-06 15:39:42 +00004169 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01004170 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00004171
John Fastabend3ff661c2012-04-15 06:44:14 +00004172 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00004173 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
4174 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00004175 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00004176 }
John Fastabend77162022012-04-15 06:43:56 +00004177 }
4178out:
4179 return err;
4180}
4181
John Fastabendd83b0602012-04-15 06:44:08 +00004182static int nlmsg_populate_fdb(struct sk_buff *skb,
4183 struct netlink_callback *cb,
4184 struct net_device *dev,
4185 int *idx,
4186 struct netdev_hw_addr_list *list)
4187{
4188 struct netdev_hw_addr *ha;
4189 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00004190 u32 portid, seq;
John Fastabendd83b0602012-04-15 06:44:08 +00004191
Eric W. Biederman15e47302012-09-07 20:12:54 +00004192 portid = NETLINK_CB(cb->skb).portid;
John Fastabendd83b0602012-04-15 06:44:08 +00004193 seq = cb->nlh->nlmsg_seq;
4194
4195 list_for_each_entry(ha, &list->list, list) {
Roopa Prabhud2976532016-08-30 21:56:45 -07004196 if (*idx < cb->args[2])
John Fastabendd83b0602012-04-15 06:44:08 +00004197 goto skip;
4198
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00004199 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
John Fastabenda7a558f2012-11-01 16:23:10 +00004200 portid, seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01004201 RTM_NEWNEIGH, NTF_SELF,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00004202 NLM_F_MULTI, NUD_PERMANENT);
John Fastabendd83b0602012-04-15 06:44:08 +00004203 if (err < 0)
4204 return err;
4205skip:
4206 *idx += 1;
4207 }
4208 return 0;
4209}
4210
4211/**
Ben Hutchings2c530402012-07-10 10:55:09 +00004212 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
Bart Van Asschea9869672019-03-25 09:17:22 -07004213 * @skb: socket buffer to store message in
4214 * @cb: netlink callback
John Fastabendd83b0602012-04-15 06:44:08 +00004215 * @dev: netdevice
Bart Van Asschea9869672019-03-25 09:17:22 -07004216 * @filter_dev: ignored
4217 * @idx: the number of FDB table entries dumped is added to *@idx
John Fastabendd83b0602012-04-15 06:44:08 +00004218 *
4219 * Default netdevice operation to dump the existing unicast address list.
John Fastabend91f3e7b2013-03-29 08:18:37 +00004220 * Returns number of addresses from list put in skb.
John Fastabendd83b0602012-04-15 06:44:08 +00004221 */
4222int ndo_dflt_fdb_dump(struct sk_buff *skb,
4223 struct netlink_callback *cb,
4224 struct net_device *dev,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04004225 struct net_device *filter_dev,
Roopa Prabhud2976532016-08-30 21:56:45 -07004226 int *idx)
John Fastabendd83b0602012-04-15 06:44:08 +00004227{
4228 int err;
4229
Eric Dumazet68883892018-12-04 09:40:35 -08004230 if (dev->type != ARPHRD_ETHER)
4231 return -EINVAL;
4232
John Fastabendd83b0602012-04-15 06:44:08 +00004233 netif_addr_lock_bh(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -07004234 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
John Fastabendd83b0602012-04-15 06:44:08 +00004235 if (err)
4236 goto out;
Zhang Shengju2934c9d2016-11-30 16:37:34 +08004237 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
John Fastabendd83b0602012-04-15 06:44:08 +00004238out:
4239 netif_addr_unlock_bh(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -07004240 return err;
John Fastabendd83b0602012-04-15 06:44:08 +00004241}
4242EXPORT_SYMBOL(ndo_dflt_fdb_dump);
4243
David Ahern8c6e1372018-10-07 20:16:44 -07004244static int valid_fdb_dump_strict(const struct nlmsghdr *nlh,
4245 int *br_idx, int *brport_idx,
4246 struct netlink_ext_ack *extack)
4247{
4248 struct nlattr *tb[NDA_MAX + 1];
4249 struct ndmsg *ndm;
4250 int err, i;
4251
4252 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
4253 NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request");
4254 return -EINVAL;
4255 }
4256
4257 ndm = nlmsg_data(nlh);
4258 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
4259 ndm->ndm_flags || ndm->ndm_type) {
Nikolay Aleksandrov8b730182019-10-29 13:59:32 +02004260 NL_SET_ERR_MSG(extack, "Invalid values in header for fdb dump request");
David Ahern8c6e1372018-10-07 20:16:44 -07004261 return -EINVAL;
4262 }
4263
Johannes Berg8cb08172019-04-26 14:07:28 +02004264 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
4265 NDA_MAX, NULL, extack);
David Ahern8c6e1372018-10-07 20:16:44 -07004266 if (err < 0)
4267 return err;
4268
4269 *brport_idx = ndm->ndm_ifindex;
4270 for (i = 0; i <= NDA_MAX; ++i) {
4271 if (!tb[i])
4272 continue;
4273
4274 switch (i) {
4275 case NDA_IFINDEX:
4276 if (nla_len(tb[i]) != sizeof(u32)) {
4277 NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request");
4278 return -EINVAL;
4279 }
4280 *brport_idx = nla_get_u32(tb[NDA_IFINDEX]);
4281 break;
4282 case NDA_MASTER:
4283 if (nla_len(tb[i]) != sizeof(u32)) {
4284 NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request");
4285 return -EINVAL;
4286 }
4287 *br_idx = nla_get_u32(tb[NDA_MASTER]);
4288 break;
4289 default:
4290 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request");
4291 return -EINVAL;
4292 }
4293 }
4294
4295 return 0;
4296}
4297
David Ahern8dfbda12018-10-07 20:16:43 -07004298static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh,
4299 int *br_idx, int *brport_idx,
4300 struct netlink_ext_ack *extack)
4301{
David Ahern8dfbda12018-10-07 20:16:43 -07004302 struct nlattr *tb[IFLA_MAX+1];
4303 int err;
4304
4305 /* A hack to preserve kernel<->userspace interface.
4306 * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0.
4307 * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails.
4308 * So, check for ndmsg with an optional u32 attribute (not used here).
4309 * Fortunately these sizes don't conflict with the size of ifinfomsg
4310 * with an optional attribute.
4311 */
4312 if (nlmsg_len(nlh) != sizeof(struct ndmsg) &&
4313 (nlmsg_len(nlh) != sizeof(struct ndmsg) +
4314 nla_attr_size(sizeof(u32)))) {
David Ahern4565d7e2018-10-08 13:57:24 -07004315 struct ifinfomsg *ifm;
4316
Johannes Berg8cb08172019-04-26 14:07:28 +02004317 err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
4318 tb, IFLA_MAX, ifla_policy,
4319 extack);
David Ahern8dfbda12018-10-07 20:16:43 -07004320 if (err < 0) {
4321 return -EINVAL;
4322 } else if (err == 0) {
4323 if (tb[IFLA_MASTER])
4324 *br_idx = nla_get_u32(tb[IFLA_MASTER]);
4325 }
4326
David Ahern4565d7e2018-10-08 13:57:24 -07004327 ifm = nlmsg_data(nlh);
David Ahern8dfbda12018-10-07 20:16:43 -07004328 *brport_idx = ifm->ifi_index;
4329 }
4330 return 0;
4331}
4332
John Fastabend77162022012-04-15 06:43:56 +00004333static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
4334{
John Fastabend77162022012-04-15 06:43:56 +00004335 struct net_device *dev;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004336 struct net_device *br_dev = NULL;
4337 const struct net_device_ops *ops = NULL;
4338 const struct net_device_ops *cops = NULL;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004339 struct net *net = sock_net(skb->sk);
Roopa Prabhud2976532016-08-30 21:56:45 -07004340 struct hlist_head *head;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004341 int brport_idx = 0;
4342 int br_idx = 0;
Roopa Prabhud2976532016-08-30 21:56:45 -07004343 int h, s_h;
4344 int idx = 0, s_idx;
4345 int err = 0;
4346 int fidx = 0;
John Fastabend77162022012-04-15 06:43:56 +00004347
David Ahern8c6e1372018-10-07 20:16:44 -07004348 if (cb->strict_check)
4349 err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx,
4350 cb->extack);
4351 else
4352 err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx,
4353 cb->extack);
David Ahern8dfbda12018-10-07 20:16:43 -07004354 if (err < 0)
4355 return err;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004356
4357 if (br_idx) {
4358 br_dev = __dev_get_by_index(net, br_idx);
4359 if (!br_dev)
4360 return -ENODEV;
4361
4362 ops = br_dev->netdev_ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004363 }
4364
Roopa Prabhud2976532016-08-30 21:56:45 -07004365 s_h = cb->args[0];
4366 s_idx = cb->args[1];
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004367
Roopa Prabhud2976532016-08-30 21:56:45 -07004368 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4369 idx = 0;
4370 head = &net->dev_index_head[h];
4371 hlist_for_each_entry(dev, head, index_hlist) {
4372
4373 if (brport_idx && (dev->ifindex != brport_idx))
4374 continue;
4375
4376 if (!br_idx) { /* user did not specify a specific bridge */
Julian Wiedmann2e92a2d2020-02-20 09:00:07 +01004377 if (netif_is_bridge_port(dev)) {
Roopa Prabhud2976532016-08-30 21:56:45 -07004378 br_dev = netdev_master_upper_dev_get(dev);
4379 cops = br_dev->netdev_ops;
4380 }
4381 } else {
4382 if (dev != br_dev &&
Julian Wiedmann2e92a2d2020-02-20 09:00:07 +01004383 !netif_is_bridge_port(dev))
Roopa Prabhud2976532016-08-30 21:56:45 -07004384 continue;
4385
4386 if (br_dev != netdev_master_upper_dev_get(dev) &&
Kyungrok Chung254ec032021-10-16 20:21:36 +09004387 !netif_is_bridge_master(dev))
Roopa Prabhud2976532016-08-30 21:56:45 -07004388 continue;
4389 cops = ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004390 }
4391
Roopa Prabhud2976532016-08-30 21:56:45 -07004392 if (idx < s_idx)
4393 goto cont;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004394
Julian Wiedmann2e92a2d2020-02-20 09:00:07 +01004395 if (netif_is_bridge_port(dev)) {
Roopa Prabhud2976532016-08-30 21:56:45 -07004396 if (cops && cops->ndo_fdb_dump) {
4397 err = cops->ndo_fdb_dump(skb, cb,
4398 br_dev, dev,
4399 &fidx);
4400 if (err == -EMSGSIZE)
4401 goto out;
4402 }
4403 }
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004404
Roopa Prabhud2976532016-08-30 21:56:45 -07004405 if (dev->netdev_ops->ndo_fdb_dump)
4406 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
4407 dev, NULL,
4408 &fidx);
4409 else
4410 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
4411 &fidx);
4412 if (err == -EMSGSIZE)
4413 goto out;
4414
4415 cops = NULL;
4416
4417 /* reset fdb offset to 0 for rest of the interfaces */
4418 cb->args[2] = 0;
4419 fidx = 0;
4420cont:
4421 idx++;
John Fastabend77162022012-04-15 06:43:56 +00004422 }
John Fastabend77162022012-04-15 06:43:56 +00004423 }
John Fastabend77162022012-04-15 06:43:56 +00004424
Roopa Prabhud2976532016-08-30 21:56:45 -07004425out:
4426 cb->args[0] = h;
4427 cb->args[1] = idx;
4428 cb->args[2] = fidx;
4429
John Fastabend77162022012-04-15 06:43:56 +00004430 return skb->len;
4431}
4432
Roopa Prabhu5b2f94b2018-12-15 22:35:08 -08004433static int valid_fdb_get_strict(const struct nlmsghdr *nlh,
4434 struct nlattr **tb, u8 *ndm_flags,
4435 int *br_idx, int *brport_idx, u8 **addr,
4436 u16 *vid, struct netlink_ext_ack *extack)
4437{
4438 struct ndmsg *ndm;
4439 int err, i;
4440
4441 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
4442 NL_SET_ERR_MSG(extack, "Invalid header for fdb get request");
4443 return -EINVAL;
4444 }
4445
4446 ndm = nlmsg_data(nlh);
4447 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
4448 ndm->ndm_type) {
4449 NL_SET_ERR_MSG(extack, "Invalid values in header for fdb get request");
4450 return -EINVAL;
4451 }
4452
4453 if (ndm->ndm_flags & ~(NTF_MASTER | NTF_SELF)) {
4454 NL_SET_ERR_MSG(extack, "Invalid flags in header for fdb get request");
4455 return -EINVAL;
4456 }
4457
Johannes Berg8cb08172019-04-26 14:07:28 +02004458 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
4459 NDA_MAX, nda_policy, extack);
Roopa Prabhu5b2f94b2018-12-15 22:35:08 -08004460 if (err < 0)
4461 return err;
4462
4463 *ndm_flags = ndm->ndm_flags;
4464 *brport_idx = ndm->ndm_ifindex;
4465 for (i = 0; i <= NDA_MAX; ++i) {
4466 if (!tb[i])
4467 continue;
4468
4469 switch (i) {
4470 case NDA_MASTER:
4471 *br_idx = nla_get_u32(tb[i]);
4472 break;
4473 case NDA_LLADDR:
4474 if (nla_len(tb[i]) != ETH_ALEN) {
4475 NL_SET_ERR_MSG(extack, "Invalid address in fdb get request");
4476 return -EINVAL;
4477 }
4478 *addr = nla_data(tb[i]);
4479 break;
4480 case NDA_VLAN:
4481 err = fdb_vid_parse(tb[i], vid, extack);
4482 if (err)
4483 return err;
4484 break;
4485 case NDA_VNI:
4486 break;
4487 default:
4488 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb get request");
4489 return -EINVAL;
4490 }
4491 }
4492
4493 return 0;
4494}
4495
4496static int rtnl_fdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh,
4497 struct netlink_ext_ack *extack)
4498{
4499 struct net_device *dev = NULL, *br_dev = NULL;
4500 const struct net_device_ops *ops = NULL;
4501 struct net *net = sock_net(in_skb->sk);
4502 struct nlattr *tb[NDA_MAX + 1];
4503 struct sk_buff *skb;
4504 int brport_idx = 0;
4505 u8 ndm_flags = 0;
4506 int br_idx = 0;
4507 u8 *addr = NULL;
4508 u16 vid = 0;
4509 int err;
4510
4511 err = valid_fdb_get_strict(nlh, tb, &ndm_flags, &br_idx,
4512 &brport_idx, &addr, &vid, extack);
4513 if (err < 0)
4514 return err;
4515
Nikolay Aleksandrovf989d032018-12-30 14:33:20 +02004516 if (!addr) {
4517 NL_SET_ERR_MSG(extack, "Missing lookup address for fdb get request");
4518 return -EINVAL;
4519 }
4520
Roopa Prabhu5b2f94b2018-12-15 22:35:08 -08004521 if (brport_idx) {
4522 dev = __dev_get_by_index(net, brport_idx);
4523 if (!dev) {
4524 NL_SET_ERR_MSG(extack, "Unknown device ifindex");
4525 return -ENODEV;
4526 }
4527 }
4528
4529 if (br_idx) {
4530 if (dev) {
4531 NL_SET_ERR_MSG(extack, "Master and device are mutually exclusive");
4532 return -EINVAL;
4533 }
4534
4535 br_dev = __dev_get_by_index(net, br_idx);
4536 if (!br_dev) {
4537 NL_SET_ERR_MSG(extack, "Invalid master ifindex");
4538 return -EINVAL;
4539 }
4540 ops = br_dev->netdev_ops;
4541 }
4542
4543 if (dev) {
4544 if (!ndm_flags || (ndm_flags & NTF_MASTER)) {
Julian Wiedmann2e92a2d2020-02-20 09:00:07 +01004545 if (!netif_is_bridge_port(dev)) {
Roopa Prabhu5b2f94b2018-12-15 22:35:08 -08004546 NL_SET_ERR_MSG(extack, "Device is not a bridge port");
4547 return -EINVAL;
4548 }
4549 br_dev = netdev_master_upper_dev_get(dev);
4550 if (!br_dev) {
4551 NL_SET_ERR_MSG(extack, "Master of device not found");
4552 return -EINVAL;
4553 }
4554 ops = br_dev->netdev_ops;
4555 } else {
4556 if (!(ndm_flags & NTF_SELF)) {
4557 NL_SET_ERR_MSG(extack, "Missing NTF_SELF");
4558 return -EINVAL;
4559 }
4560 ops = dev->netdev_ops;
4561 }
4562 }
4563
4564 if (!br_dev && !dev) {
4565 NL_SET_ERR_MSG(extack, "No device specified");
4566 return -ENODEV;
4567 }
4568
4569 if (!ops || !ops->ndo_fdb_get) {
4570 NL_SET_ERR_MSG(extack, "Fdb get operation not supported by device");
4571 return -EOPNOTSUPP;
4572 }
4573
4574 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
4575 if (!skb)
4576 return -ENOBUFS;
4577
4578 if (br_dev)
4579 dev = br_dev;
4580 err = ops->ndo_fdb_get(skb, tb, dev, addr, vid,
4581 NETLINK_CB(in_skb).portid,
4582 nlh->nlmsg_seq, extack);
4583 if (err)
4584 goto out;
4585
4586 return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
4587out:
4588 kfree_skb(skb);
4589 return err;
4590}
4591
Scott Feldman2c3c0312014-11-28 14:34:25 +01004592static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
4593 unsigned int attrnum, unsigned int flag)
4594{
4595 if (mask & flag)
4596 return nla_put_u8(skb, attrnum, !!(flags & flag));
4597 return 0;
4598}
4599
John Fastabend815cccb2012-10-24 08:13:09 +00004600int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Scott Feldman2c3c0312014-11-28 14:34:25 +01004601 struct net_device *dev, u16 mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -07004602 u32 flags, u32 mask, int nlflags,
4603 u32 filter_mask,
4604 int (*vlan_fill)(struct sk_buff *skb,
4605 struct net_device *dev,
4606 u32 filter_mask))
John Fastabend815cccb2012-10-24 08:13:09 +00004607{
4608 struct nlmsghdr *nlh;
4609 struct ifinfomsg *ifm;
4610 struct nlattr *br_afspec;
Scott Feldman2c3c0312014-11-28 14:34:25 +01004611 struct nlattr *protinfo;
John Fastabend815cccb2012-10-24 08:13:09 +00004612 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Jiri Pirko898e5062013-01-03 22:48:52 +00004613 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07004614 int err = 0;
John Fastabend815cccb2012-10-24 08:13:09 +00004615
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02004616 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
John Fastabend815cccb2012-10-24 08:13:09 +00004617 if (nlh == NULL)
4618 return -EMSGSIZE;
4619
4620 ifm = nlmsg_data(nlh);
4621 ifm->ifi_family = AF_BRIDGE;
4622 ifm->__ifi_pad = 0;
4623 ifm->ifi_type = dev->type;
4624 ifm->ifi_index = dev->ifindex;
4625 ifm->ifi_flags = dev_get_flags(dev);
4626 ifm->ifi_change = 0;
4627
4628
4629 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4630 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4631 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00004632 (br_dev &&
4633 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
John Fastabend815cccb2012-10-24 08:13:09 +00004634 (dev->addr_len &&
4635 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02004636 (dev->ifindex != dev_get_iflink(dev) &&
4637 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
John Fastabend815cccb2012-10-24 08:13:09 +00004638 goto nla_put_failure;
4639
Michal Kubecekae0be8d2019-04-26 11:13:06 +02004640 br_afspec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
John Fastabend815cccb2012-10-24 08:13:09 +00004641 if (!br_afspec)
4642 goto nla_put_failure;
4643
Roopa Prabhu1d460b92014-12-08 14:04:20 -08004644 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
John Fastabend815cccb2012-10-24 08:13:09 +00004645 nla_nest_cancel(skb, br_afspec);
4646 goto nla_put_failure;
4647 }
Roopa Prabhu1d460b92014-12-08 14:04:20 -08004648
4649 if (mode != BRIDGE_MODE_UNDEF) {
4650 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
4651 nla_nest_cancel(skb, br_afspec);
4652 goto nla_put_failure;
4653 }
4654 }
Scott Feldman7d4f8d82015-06-22 00:27:17 -07004655 if (vlan_fill) {
4656 err = vlan_fill(skb, dev, filter_mask);
4657 if (err) {
4658 nla_nest_cancel(skb, br_afspec);
4659 goto nla_put_failure;
4660 }
4661 }
John Fastabend815cccb2012-10-24 08:13:09 +00004662 nla_nest_end(skb, br_afspec);
4663
Michal Kubecekae0be8d2019-04-26 11:13:06 +02004664 protinfo = nla_nest_start(skb, IFLA_PROTINFO);
Scott Feldman2c3c0312014-11-28 14:34:25 +01004665 if (!protinfo)
4666 goto nla_put_failure;
4667
4668 if (brport_nla_put_flag(skb, flags, mask,
4669 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
4670 brport_nla_put_flag(skb, flags, mask,
4671 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
4672 brport_nla_put_flag(skb, flags, mask,
4673 IFLA_BRPORT_FAST_LEAVE,
4674 BR_MULTICAST_FAST_LEAVE) ||
4675 brport_nla_put_flag(skb, flags, mask,
4676 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
4677 brport_nla_put_flag(skb, flags, mask,
4678 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
4679 brport_nla_put_flag(skb, flags, mask,
4680 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
4681 brport_nla_put_flag(skb, flags, mask,
4682 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
4683 brport_nla_put_flag(skb, flags, mask,
Julian Wiedmann583cb0b2020-02-17 14:45:01 +01004684 IFLA_BRPORT_PROXYARP, BR_PROXYARP) ||
4685 brport_nla_put_flag(skb, flags, mask,
4686 IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD) ||
4687 brport_nla_put_flag(skb, flags, mask,
4688 IFLA_BRPORT_BCAST_FLOOD, BR_BCAST_FLOOD)) {
Scott Feldman2c3c0312014-11-28 14:34:25 +01004689 nla_nest_cancel(skb, protinfo);
4690 goto nla_put_failure;
4691 }
4692
4693 nla_nest_end(skb, protinfo);
4694
Johannes Berg053c0952015-01-16 22:09:00 +01004695 nlmsg_end(skb, nlh);
4696 return 0;
John Fastabend815cccb2012-10-24 08:13:09 +00004697nla_put_failure:
4698 nlmsg_cancel(skb, nlh);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07004699 return err ? err : -EMSGSIZE;
John Fastabend815cccb2012-10-24 08:13:09 +00004700}
Scott Feldman7d4f8d82015-06-22 00:27:17 -07004701EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
John Fastabend815cccb2012-10-24 08:13:09 +00004702
David Ahern2d011be2018-10-07 20:16:31 -07004703static int valid_bridge_getlink_req(const struct nlmsghdr *nlh,
4704 bool strict_check, u32 *filter_mask,
4705 struct netlink_ext_ack *extack)
4706{
4707 struct nlattr *tb[IFLA_MAX+1];
4708 int err, i;
4709
4710 if (strict_check) {
4711 struct ifinfomsg *ifm;
4712
4713 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
4714 NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump");
4715 return -EINVAL;
4716 }
4717
4718 ifm = nlmsg_data(nlh);
4719 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
4720 ifm->ifi_change || ifm->ifi_index) {
4721 NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request");
4722 return -EINVAL;
4723 }
4724
Johannes Berg8cb08172019-04-26 14:07:28 +02004725 err = nlmsg_parse_deprecated_strict(nlh,
4726 sizeof(struct ifinfomsg),
4727 tb, IFLA_MAX, ifla_policy,
4728 extack);
David Ahern2d011be2018-10-07 20:16:31 -07004729 } else {
Johannes Berg8cb08172019-04-26 14:07:28 +02004730 err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
4731 tb, IFLA_MAX, ifla_policy,
4732 extack);
David Ahern2d011be2018-10-07 20:16:31 -07004733 }
4734 if (err < 0)
4735 return err;
4736
4737 /* new attributes should only be added with strict checking */
4738 for (i = 0; i <= IFLA_MAX; ++i) {
4739 if (!tb[i])
4740 continue;
4741
4742 switch (i) {
4743 case IFLA_EXT_MASK:
4744 *filter_mask = nla_get_u32(tb[i]);
4745 break;
4746 default:
4747 if (strict_check) {
4748 NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request");
4749 return -EINVAL;
4750 }
4751 }
4752 }
4753
4754 return 0;
4755}
4756
John Fastabende5a55a82012-10-24 08:12:57 +00004757static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
4758{
David Ahern2d011be2018-10-07 20:16:31 -07004759 const struct nlmsghdr *nlh = cb->nlh;
John Fastabende5a55a82012-10-24 08:12:57 +00004760 struct net *net = sock_net(skb->sk);
4761 struct net_device *dev;
4762 int idx = 0;
4763 u32 portid = NETLINK_CB(cb->skb).portid;
David Ahern2d011be2018-10-07 20:16:31 -07004764 u32 seq = nlh->nlmsg_seq;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00004765 u32 filter_mask = 0;
Roopa Prabhud64f69b2015-09-15 14:44:29 -07004766 int err;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00004767
David Ahern2d011be2018-10-07 20:16:31 -07004768 err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask,
4769 cb->extack);
4770 if (err < 0 && cb->strict_check)
4771 return err;
John Fastabende5a55a82012-10-24 08:12:57 +00004772
4773 rcu_read_lock();
4774 for_each_netdev_rcu(net, dev) {
4775 const struct net_device_ops *ops = dev->netdev_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00004776 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
John Fastabende5a55a82012-10-24 08:12:57 +00004777
Jiri Pirko898e5062013-01-03 22:48:52 +00004778 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07004779 if (idx >= cb->args[0]) {
4780 err = br_dev->netdev_ops->ndo_bridge_getlink(
4781 skb, portid, seq, dev,
4782 filter_mask, NLM_F_MULTI);
David Ahernf6c57752017-05-15 23:19:17 -07004783 if (err < 0 && err != -EOPNOTSUPP) {
4784 if (likely(skb->len))
4785 break;
4786
4787 goto out_err;
4788 }
Roopa Prabhud64f69b2015-09-15 14:44:29 -07004789 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00004790 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00004791 }
4792
4793 if (ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07004794 if (idx >= cb->args[0]) {
4795 err = ops->ndo_bridge_getlink(skb, portid,
4796 seq, dev,
4797 filter_mask,
4798 NLM_F_MULTI);
David Ahernf6c57752017-05-15 23:19:17 -07004799 if (err < 0 && err != -EOPNOTSUPP) {
4800 if (likely(skb->len))
4801 break;
4802
4803 goto out_err;
4804 }
Roopa Prabhud64f69b2015-09-15 14:44:29 -07004805 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00004806 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00004807 }
4808 }
David Ahernf6c57752017-05-15 23:19:17 -07004809 err = skb->len;
4810out_err:
John Fastabende5a55a82012-10-24 08:12:57 +00004811 rcu_read_unlock();
4812 cb->args[0] = idx;
4813
David Ahernf6c57752017-05-15 23:19:17 -07004814 return err;
John Fastabende5a55a82012-10-24 08:12:57 +00004815}
4816
John Fastabend2469ffd2012-10-24 08:13:03 +00004817static inline size_t bridge_nlmsg_size(void)
4818{
4819 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4820 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4821 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4822 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
4823 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
4824 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
4825 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
4826 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
4827 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
4828 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
4829 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
4830}
4831
Roopa Prabhu02dba432015-01-14 20:02:25 -08004832static int rtnl_bridge_notify(struct net_device *dev)
John Fastabend2469ffd2012-10-24 08:13:03 +00004833{
4834 struct net *net = dev_net(dev);
John Fastabend2469ffd2012-10-24 08:13:03 +00004835 struct sk_buff *skb;
4836 int err = -EOPNOTSUPP;
4837
Roopa Prabhu02dba432015-01-14 20:02:25 -08004838 if (!dev->netdev_ops->ndo_bridge_getlink)
4839 return 0;
4840
John Fastabend2469ffd2012-10-24 08:13:03 +00004841 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
4842 if (!skb) {
4843 err = -ENOMEM;
4844 goto errout;
4845 }
4846
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02004847 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
Roopa Prabhu02dba432015-01-14 20:02:25 -08004848 if (err < 0)
4849 goto errout;
John Fastabend2469ffd2012-10-24 08:13:03 +00004850
Ido Schimmeld2e381c2021-06-09 14:17:53 +03004851 /* Notification info is only filled for bridge ports, not the bridge
4852 * device itself. Therefore, a zero notification length is valid and
4853 * should not result in an error.
4854 */
4855 if (!skb->len)
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08004856 goto errout;
4857
John Fastabend2469ffd2012-10-24 08:13:03 +00004858 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
4859 return 0;
4860errout:
4861 WARN_ON(err == -EMSGSIZE);
4862 kfree_skb(skb);
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08004863 if (err)
4864 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
John Fastabend2469ffd2012-10-24 08:13:03 +00004865 return err;
4866}
4867
David Ahernc21ef3e2017-04-16 09:48:24 -07004868static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
4869 struct netlink_ext_ack *extack)
John Fastabende5a55a82012-10-24 08:12:57 +00004870{
4871 struct net *net = sock_net(skb->sk);
4872 struct ifinfomsg *ifm;
4873 struct net_device *dev;
John Fastabend2469ffd2012-10-24 08:13:03 +00004874 struct nlattr *br_spec, *attr = NULL;
4875 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02004876 u16 flags = 0;
John Fastabendc38e01b2012-11-02 16:32:36 +00004877 bool have_flags = false;
John Fastabende5a55a82012-10-24 08:12:57 +00004878
4879 if (nlmsg_len(nlh) < sizeof(*ifm))
4880 return -EINVAL;
4881
4882 ifm = nlmsg_data(nlh);
4883 if (ifm->ifi_family != AF_BRIDGE)
4884 return -EPFNOSUPPORT;
4885
4886 dev = __dev_get_by_index(net, ifm->ifi_index);
4887 if (!dev) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004888 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabende5a55a82012-10-24 08:12:57 +00004889 return -ENODEV;
4890 }
4891
John Fastabend2469ffd2012-10-24 08:13:03 +00004892 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4893 if (br_spec) {
4894 nla_for_each_nested(attr, br_spec, rem) {
4895 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01004896 if (nla_len(attr) < sizeof(flags))
4897 return -EINVAL;
4898
John Fastabendc38e01b2012-11-02 16:32:36 +00004899 have_flags = true;
John Fastabend2469ffd2012-10-24 08:13:03 +00004900 flags = nla_get_u16(attr);
4901 break;
4902 }
4903 }
4904 }
4905
4906 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00004907 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4908
4909 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
John Fastabend2469ffd2012-10-24 08:13:03 +00004910 err = -EOPNOTSUPP;
4911 goto out;
4912 }
4913
Petr Machata2fd527b2018-12-12 17:02:48 +00004914 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags,
4915 extack);
John Fastabende5a55a82012-10-24 08:12:57 +00004916 if (err)
4917 goto out;
John Fastabend2469ffd2012-10-24 08:13:03 +00004918
4919 flags &= ~BRIDGE_FLAGS_MASTER;
John Fastabende5a55a82012-10-24 08:12:57 +00004920 }
4921
John Fastabend2469ffd2012-10-24 08:13:03 +00004922 if ((flags & BRIDGE_FLAGS_SELF)) {
4923 if (!dev->netdev_ops->ndo_bridge_setlink)
4924 err = -EOPNOTSUPP;
4925 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08004926 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
Petr Machata2fd527b2018-12-12 17:02:48 +00004927 flags,
4928 extack);
Roopa Prabhu02dba432015-01-14 20:02:25 -08004929 if (!err) {
John Fastabend2469ffd2012-10-24 08:13:03 +00004930 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08004931
4932 /* Generate event to notify upper layer of bridge
4933 * change
4934 */
4935 err = rtnl_bridge_notify(dev);
4936 }
John Fastabend2469ffd2012-10-24 08:13:03 +00004937 }
4938
John Fastabendc38e01b2012-11-02 16:32:36 +00004939 if (have_flags)
John Fastabend2469ffd2012-10-24 08:13:03 +00004940 memcpy(nla_data(attr), &flags, sizeof(flags));
John Fastabende5a55a82012-10-24 08:12:57 +00004941out:
4942 return err;
4943}
4944
David Ahernc21ef3e2017-04-16 09:48:24 -07004945static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
4946 struct netlink_ext_ack *extack)
Vlad Yasevich407af322013-02-13 12:00:12 +00004947{
4948 struct net *net = sock_net(skb->sk);
4949 struct ifinfomsg *ifm;
4950 struct net_device *dev;
4951 struct nlattr *br_spec, *attr = NULL;
4952 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02004953 u16 flags = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +00004954 bool have_flags = false;
4955
4956 if (nlmsg_len(nlh) < sizeof(*ifm))
4957 return -EINVAL;
4958
4959 ifm = nlmsg_data(nlh);
4960 if (ifm->ifi_family != AF_BRIDGE)
4961 return -EPFNOSUPPORT;
4962
4963 dev = __dev_get_by_index(net, ifm->ifi_index);
4964 if (!dev) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004965 NL_SET_ERR_MSG(extack, "unknown ifindex");
Vlad Yasevich407af322013-02-13 12:00:12 +00004966 return -ENODEV;
4967 }
4968
4969 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4970 if (br_spec) {
4971 nla_for_each_nested(attr, br_spec, rem) {
4972 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01004973 if (nla_len(attr) < sizeof(flags))
4974 return -EINVAL;
4975
Vlad Yasevich407af322013-02-13 12:00:12 +00004976 have_flags = true;
4977 flags = nla_get_u16(attr);
4978 break;
4979 }
4980 }
4981 }
4982
Vlad Yasevich407af322013-02-13 12:00:12 +00004983 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
4984 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4985
4986 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
4987 err = -EOPNOTSUPP;
4988 goto out;
4989 }
4990
Roopa Prabhuadd511b2015-01-29 22:40:12 -08004991 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00004992 if (err)
4993 goto out;
4994
4995 flags &= ~BRIDGE_FLAGS_MASTER;
4996 }
4997
4998 if ((flags & BRIDGE_FLAGS_SELF)) {
4999 if (!dev->netdev_ops->ndo_bridge_dellink)
5000 err = -EOPNOTSUPP;
5001 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08005002 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
5003 flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00005004
Roopa Prabhu02dba432015-01-14 20:02:25 -08005005 if (!err) {
Vlad Yasevich407af322013-02-13 12:00:12 +00005006 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08005007
5008 /* Generate event to notify upper layer of bridge
5009 * change
5010 */
5011 err = rtnl_bridge_notify(dev);
5012 }
Vlad Yasevich407af322013-02-13 12:00:12 +00005013 }
5014
5015 if (have_flags)
5016 memcpy(nla_data(attr), &flags, sizeof(flags));
Vlad Yasevich407af322013-02-13 12:00:12 +00005017out:
5018 return err;
5019}
5020
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005021static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
5022{
5023 return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
5024 (!idxattr || idxattr == attrid);
5025}
5026
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02005027#define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
5028static int rtnl_get_offload_stats_attr_size(int attr_id)
5029{
5030 switch (attr_id) {
5031 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
5032 return sizeof(struct rtnl_link_stats64);
5033 }
5034
5035 return 0;
5036}
5037
5038static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
5039 int *prividx)
5040{
5041 struct nlattr *attr = NULL;
5042 int attr_id, size;
5043 void *attr_data;
5044 int err;
5045
5046 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
5047 dev->netdev_ops->ndo_get_offload_stats))
5048 return -ENODATA;
5049
5050 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
5051 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
5052 if (attr_id < *prividx)
5053 continue;
5054
5055 size = rtnl_get_offload_stats_attr_size(attr_id);
5056 if (!size)
5057 continue;
5058
Or Gerlitz3df5b3c2016-11-22 23:09:54 +02005059 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02005060 continue;
5061
5062 attr = nla_reserve_64bit(skb, attr_id, size,
5063 IFLA_OFFLOAD_XSTATS_UNSPEC);
5064 if (!attr)
5065 goto nla_put_failure;
5066
5067 attr_data = nla_data(attr);
5068 memset(attr_data, 0, size);
5069 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
5070 attr_data);
5071 if (err)
5072 goto get_offload_stats_failure;
5073 }
5074
5075 if (!attr)
5076 return -ENODATA;
5077
5078 *prividx = 0;
5079 return 0;
5080
5081nla_put_failure:
5082 err = -EMSGSIZE;
5083get_offload_stats_failure:
5084 *prividx = attr_id;
5085 return err;
5086}
5087
5088static int rtnl_get_offload_stats_size(const struct net_device *dev)
5089{
5090 int nla_size = 0;
5091 int attr_id;
5092 int size;
5093
5094 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
5095 dev->netdev_ops->ndo_get_offload_stats))
5096 return 0;
5097
5098 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
5099 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
Or Gerlitz3df5b3c2016-11-22 23:09:54 +02005100 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02005101 continue;
5102 size = rtnl_get_offload_stats_attr_size(attr_id);
5103 nla_size += nla_total_size_64bit(size);
5104 }
5105
5106 if (nla_size != 0)
5107 nla_size += nla_total_size(0);
5108
5109 return nla_size;
5110}
5111
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005112static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
5113 int type, u32 pid, u32 seq, u32 change,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005114 unsigned int flags, unsigned int filter_mask,
5115 int *idxattr, int *prividx)
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005116{
5117 struct if_stats_msg *ifsm;
5118 struct nlmsghdr *nlh;
5119 struct nlattr *attr;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005120 int s_prividx = *prividx;
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02005121 int err;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005122
5123 ASSERT_RTNL();
5124
5125 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
5126 if (!nlh)
5127 return -EMSGSIZE;
5128
5129 ifsm = nlmsg_data(nlh);
Nikolay Aleksandrovce024f42017-10-03 13:20:48 +03005130 ifsm->family = PF_UNSPEC;
5131 ifsm->pad1 = 0;
5132 ifsm->pad2 = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005133 ifsm->ifindex = dev->ifindex;
5134 ifsm->filter_mask = filter_mask;
5135
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005136 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005137 struct rtnl_link_stats64 *sp;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005138
Nicolas Dichtel58414d32016-04-21 18:58:25 +02005139 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
5140 sizeof(struct rtnl_link_stats64),
5141 IFLA_STATS_UNSPEC);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005142 if (!attr)
5143 goto nla_put_failure;
5144
5145 sp = nla_data(attr);
5146 dev_get_stats(dev, sp);
5147 }
5148
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02005149 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
5150 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
5151
5152 if (ops && ops->fill_linkxstats) {
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02005153 *idxattr = IFLA_STATS_LINK_XSTATS;
Michal Kubecekae0be8d2019-04-26 11:13:06 +02005154 attr = nla_nest_start_noflag(skb,
5155 IFLA_STATS_LINK_XSTATS);
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02005156 if (!attr)
5157 goto nla_put_failure;
5158
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02005159 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
5160 nla_nest_end(skb, attr);
5161 if (err)
5162 goto nla_put_failure;
5163 *idxattr = 0;
5164 }
5165 }
5166
5167 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
5168 *idxattr)) {
5169 const struct rtnl_link_ops *ops = NULL;
5170 const struct net_device *master;
5171
5172 master = netdev_master_upper_dev_get(dev);
5173 if (master)
5174 ops = master->rtnl_link_ops;
5175 if (ops && ops->fill_linkxstats) {
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02005176 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
Michal Kubecekae0be8d2019-04-26 11:13:06 +02005177 attr = nla_nest_start_noflag(skb,
5178 IFLA_STATS_LINK_XSTATS_SLAVE);
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02005179 if (!attr)
5180 goto nla_put_failure;
5181
5182 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02005183 nla_nest_end(skb, attr);
5184 if (err)
5185 goto nla_put_failure;
5186 *idxattr = 0;
5187 }
5188 }
5189
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02005190 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
5191 *idxattr)) {
5192 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
Michal Kubecekae0be8d2019-04-26 11:13:06 +02005193 attr = nla_nest_start_noflag(skb,
5194 IFLA_STATS_LINK_OFFLOAD_XSTATS);
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02005195 if (!attr)
5196 goto nla_put_failure;
5197
5198 err = rtnl_get_offload_stats(skb, dev, prividx);
5199 if (err == -ENODATA)
5200 nla_nest_cancel(skb, attr);
5201 else
5202 nla_nest_end(skb, attr);
5203
5204 if (err && err != -ENODATA)
5205 goto nla_put_failure;
5206 *idxattr = 0;
5207 }
5208
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005209 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
5210 struct rtnl_af_ops *af_ops;
5211
5212 *idxattr = IFLA_STATS_AF_SPEC;
Michal Kubecekae0be8d2019-04-26 11:13:06 +02005213 attr = nla_nest_start_noflag(skb, IFLA_STATS_AF_SPEC);
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005214 if (!attr)
5215 goto nla_put_failure;
5216
Florian Westphal5fa85a02017-10-16 15:44:36 +02005217 rcu_read_lock();
5218 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005219 if (af_ops->fill_stats_af) {
5220 struct nlattr *af;
5221 int err;
5222
Michal Kubecekae0be8d2019-04-26 11:13:06 +02005223 af = nla_nest_start_noflag(skb,
5224 af_ops->family);
Florian Westphal5fa85a02017-10-16 15:44:36 +02005225 if (!af) {
5226 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005227 goto nla_put_failure;
Florian Westphal5fa85a02017-10-16 15:44:36 +02005228 }
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005229 err = af_ops->fill_stats_af(skb, dev);
5230
Florian Westphal5fa85a02017-10-16 15:44:36 +02005231 if (err == -ENODATA) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005232 nla_nest_cancel(skb, af);
Florian Westphal5fa85a02017-10-16 15:44:36 +02005233 } else if (err < 0) {
5234 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005235 goto nla_put_failure;
Florian Westphal5fa85a02017-10-16 15:44:36 +02005236 }
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005237
5238 nla_nest_end(skb, af);
5239 }
5240 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02005241 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005242
5243 nla_nest_end(skb, attr);
5244
5245 *idxattr = 0;
5246 }
5247
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005248 nlmsg_end(skb, nlh);
5249
5250 return 0;
5251
5252nla_put_failure:
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005253 /* not a multi message or no progress mean a real error */
5254 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
5255 nlmsg_cancel(skb, nlh);
5256 else
5257 nlmsg_end(skb, nlh);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005258
5259 return -EMSGSIZE;
5260}
5261
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005262static size_t if_nlmsg_stats_size(const struct net_device *dev,
5263 u32 filter_mask)
5264{
Eric Dumazetd3436792021-10-05 14:04:17 -07005265 size_t size = NLMSG_ALIGN(sizeof(struct if_stats_msg));
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005266
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005267 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005268 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
5269
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02005270 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
5271 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02005272 int attr = IFLA_STATS_LINK_XSTATS;
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02005273
5274 if (ops && ops->get_linkxstats_size) {
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02005275 size += nla_total_size(ops->get_linkxstats_size(dev,
5276 attr));
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02005277 /* for IFLA_STATS_LINK_XSTATS */
5278 size += nla_total_size(0);
5279 }
5280 }
5281
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02005282 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
5283 struct net_device *_dev = (struct net_device *)dev;
5284 const struct rtnl_link_ops *ops = NULL;
5285 const struct net_device *master;
5286
5287 /* netdev_master_upper_dev_get can't take const */
5288 master = netdev_master_upper_dev_get(_dev);
5289 if (master)
5290 ops = master->rtnl_link_ops;
5291 if (ops && ops->get_linkxstats_size) {
5292 int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
5293
5294 size += nla_total_size(ops->get_linkxstats_size(dev,
5295 attr));
5296 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
5297 size += nla_total_size(0);
5298 }
5299 }
5300
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02005301 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
5302 size += rtnl_get_offload_stats_size(dev);
5303
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005304 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
5305 struct rtnl_af_ops *af_ops;
5306
5307 /* for IFLA_STATS_AF_SPEC */
5308 size += nla_total_size(0);
5309
Florian Westphal5fa85a02017-10-16 15:44:36 +02005310 rcu_read_lock();
5311 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005312 if (af_ops->get_stats_af_size) {
5313 size += nla_total_size(
5314 af_ops->get_stats_af_size(dev));
5315
5316 /* for AF_* */
5317 size += nla_total_size(0);
5318 }
5319 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02005320 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00005321 }
5322
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005323 return size;
5324}
5325
Jakub Kicinski51bc8602019-01-18 10:46:14 -08005326static int rtnl_valid_stats_req(const struct nlmsghdr *nlh, bool strict_check,
5327 bool is_dump, struct netlink_ext_ack *extack)
5328{
5329 struct if_stats_msg *ifsm;
5330
Eric Dumazet69f23a02019-04-14 11:02:05 -07005331 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifsm))) {
Jakub Kicinski51bc8602019-01-18 10:46:14 -08005332 NL_SET_ERR_MSG(extack, "Invalid header for stats dump");
5333 return -EINVAL;
5334 }
5335
5336 if (!strict_check)
5337 return 0;
5338
5339 ifsm = nlmsg_data(nlh);
5340
5341 /* only requests using strict checks can pass data to influence
5342 * the dump. The legacy exception is filter_mask.
5343 */
5344 if (ifsm->pad1 || ifsm->pad2 || (is_dump && ifsm->ifindex)) {
5345 NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request");
5346 return -EINVAL;
5347 }
5348 if (nlmsg_attrlen(nlh, sizeof(*ifsm))) {
5349 NL_SET_ERR_MSG(extack, "Invalid attributes after stats header");
5350 return -EINVAL;
5351 }
Jakub Kicinski6300acb2019-01-18 10:46:15 -08005352 if (ifsm->filter_mask >= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX + 1)) {
5353 NL_SET_ERR_MSG(extack, "Invalid stats requested through filter mask");
5354 return -EINVAL;
5355 }
Jakub Kicinski51bc8602019-01-18 10:46:14 -08005356
5357 return 0;
5358}
5359
David Ahernc21ef3e2017-04-16 09:48:24 -07005360static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
5361 struct netlink_ext_ack *extack)
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005362{
5363 struct net *net = sock_net(skb->sk);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005364 struct net_device *dev = NULL;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005365 int idxattr = 0, prividx = 0;
5366 struct if_stats_msg *ifsm;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005367 struct sk_buff *nskb;
5368 u32 filter_mask;
5369 int err;
5370
Jakub Kicinski51bc8602019-01-18 10:46:14 -08005371 err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb),
5372 false, extack);
5373 if (err)
5374 return err;
Mathias Krause4775cc12016-12-28 17:52:15 +01005375
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005376 ifsm = nlmsg_data(nlh);
5377 if (ifsm->ifindex > 0)
5378 dev = __dev_get_by_index(net, ifsm->ifindex);
5379 else
5380 return -EINVAL;
5381
5382 if (!dev)
5383 return -ENODEV;
5384
5385 filter_mask = ifsm->filter_mask;
5386 if (!filter_mask)
5387 return -EINVAL;
5388
5389 nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
5390 if (!nskb)
5391 return -ENOBUFS;
5392
5393 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
5394 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005395 0, filter_mask, &idxattr, &prividx);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005396 if (err < 0) {
5397 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
5398 WARN_ON(err == -EMSGSIZE);
5399 kfree_skb(nskb);
5400 } else {
5401 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
5402 }
5403
5404 return err;
5405}
5406
5407static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
5408{
David Ahern841891e2018-10-07 20:16:32 -07005409 struct netlink_ext_ack *extack = cb->extack;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005410 int h, s_h, err, s_idx, s_idxattr, s_prividx;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005411 struct net *net = sock_net(skb->sk);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005412 unsigned int flags = NLM_F_MULTI;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005413 struct if_stats_msg *ifsm;
5414 struct hlist_head *head;
5415 struct net_device *dev;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005416 u32 filter_mask = 0;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005417 int idx = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005418
5419 s_h = cb->args[0];
5420 s_idx = cb->args[1];
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005421 s_idxattr = cb->args[2];
5422 s_prividx = cb->args[3];
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005423
5424 cb->seq = net->dev_base_seq;
5425
Jakub Kicinski51bc8602019-01-18 10:46:14 -08005426 err = rtnl_valid_stats_req(cb->nlh, cb->strict_check, true, extack);
5427 if (err)
5428 return err;
Mathias Krause4775cc12016-12-28 17:52:15 +01005429
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005430 ifsm = nlmsg_data(cb->nlh);
5431 filter_mask = ifsm->filter_mask;
David Ahern841891e2018-10-07 20:16:32 -07005432 if (!filter_mask) {
5433 NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump");
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005434 return -EINVAL;
David Ahern841891e2018-10-07 20:16:32 -07005435 }
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005436
5437 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5438 idx = 0;
5439 head = &net->dev_index_head[h];
5440 hlist_for_each_entry(dev, head, index_hlist) {
5441 if (idx < s_idx)
5442 goto cont;
5443 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
5444 NETLINK_CB(cb->skb).portid,
5445 cb->nlh->nlmsg_seq, 0,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005446 flags, filter_mask,
5447 &s_idxattr, &s_prividx);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005448 /* If we ran out of room on the first message,
5449 * we're in trouble
5450 */
5451 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
5452
5453 if (err < 0)
5454 goto out;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005455 s_prividx = 0;
5456 s_idxattr = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005457 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
5458cont:
5459 idx++;
5460 }
5461 }
5462out:
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005463 cb->args[3] = s_prividx;
5464 cb->args[2] = s_idxattr;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005465 cb->args[1] = idx;
5466 cb->args[0] = h;
5467
5468 return skb->len;
5469}
5470
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471/* Process one rtnetlink message. */
5472
Johannes Berg2d4bc932017-04-12 14:34:04 +02005473static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
5474 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09005476 struct net *net = sock_net(skb->sk);
Florian Westphaladdf9b92017-12-02 21:44:05 +01005477 struct rtnl_link *link;
Florian Westphale4202512017-12-02 21:44:06 +01005478 struct module *owner;
Florian Westphal6853dd42017-08-09 20:41:51 +02005479 int err = -EOPNOTSUPP;
Thomas Grafe2849862007-03-22 11:48:11 -07005480 rtnl_doit_func doit;
Florian Westphal62256f92017-08-09 20:41:52 +02005481 unsigned int flags;
Alexander Kuleshov617cfc72016-01-10 21:26:57 +06005482 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 int family;
5484 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 if (type > RTM_MAX)
Thomas Graf038890f2007-04-05 14:35:52 -07005488 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489
5490 type -= RTM_BASE;
5491
5492 /* All the messages must have at least 1 byte length */
Hong zhi guo573ce262013-03-27 06:47:04 +00005493 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 return 0;
5495
Hong zhi guo573ce262013-03-27 06:47:04 +00005496 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 kind = type&3;
5498
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07005499 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07005500 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501
Florian Westphal6853dd42017-08-09 20:41:51 +02005502 rcu_read_lock();
David S. Millerb8f3ab42011-01-18 12:40:38 -08005503 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005504 struct sock *rtnl;
Thomas Grafe2849862007-03-22 11:48:11 -07005505 rtnl_dumpit_func dumpit;
Di Zhuebfe3c52020-10-21 10:00:53 +08005506 u32 min_dump_alloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507
Florian Westphaladdf9b92017-12-02 21:44:05 +01005508 link = rtnl_get_link(family, type);
5509 if (!link || !link->dumpit) {
Florian Westphal6853dd42017-08-09 20:41:51 +02005510 family = PF_UNSPEC;
Florian Westphaladdf9b92017-12-02 21:44:05 +01005511 link = rtnl_get_link(family, type);
5512 if (!link || !link->dumpit)
Florian Westphal6853dd42017-08-09 20:41:51 +02005513 goto err_unlock;
5514 }
Florian Westphale4202512017-12-02 21:44:06 +01005515 owner = link->owner;
Florian Westphaladdf9b92017-12-02 21:44:05 +01005516 dumpit = link->dumpit;
Florian Westphale1fa6d22017-08-09 20:41:47 +02005517
Florian Westphal5c2bb9b2017-08-10 16:52:58 +02005518 if (type == RTM_GETLINK - RTM_BASE)
Florian Westphale1fa6d22017-08-09 20:41:47 +02005519 min_dump_alloc = rtnl_calcit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520
Florian Westphale4202512017-12-02 21:44:06 +01005521 err = 0;
5522 /* need to do this before rcu_read_unlock() */
5523 if (!try_module_get(owner))
5524 err = -EPROTONOSUPPORT;
5525
Florian Westphal6853dd42017-08-09 20:41:51 +02005526 rcu_read_unlock();
5527
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005528 rtnl = net->rtnl;
Florian Westphale4202512017-12-02 21:44:06 +01005529 if (err == 0) {
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00005530 struct netlink_dump_control c = {
5531 .dump = dumpit,
5532 .min_dump_alloc = min_dump_alloc,
Florian Westphale4202512017-12-02 21:44:06 +01005533 .module = owner,
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00005534 };
5535 err = netlink_dump_start(rtnl, skb, nlh, &c);
Florian Westphale4202512017-12-02 21:44:06 +01005536 /* netlink_dump_start() will keep a reference on
5537 * module if dump is still in progress.
5538 */
5539 module_put(owner);
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00005540 }
Eric Dumazet2907c352011-05-25 07:34:04 +00005541 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542 }
5543
Florian Westphaladdf9b92017-12-02 21:44:05 +01005544 link = rtnl_get_link(family, type);
5545 if (!link || !link->doit) {
Florian Westphal8caa38b2017-08-10 16:53:01 +02005546 family = PF_UNSPEC;
Florian Westphaladdf9b92017-12-02 21:44:05 +01005547 link = rtnl_get_link(PF_UNSPEC, type);
5548 if (!link || !link->doit)
5549 goto out_unlock;
Florian Westphal8caa38b2017-08-10 16:53:01 +02005550 }
5551
Florian Westphale4202512017-12-02 21:44:06 +01005552 owner = link->owner;
5553 if (!try_module_get(owner)) {
5554 err = -EPROTONOSUPPORT;
5555 goto out_unlock;
5556 }
5557
Florian Westphaladdf9b92017-12-02 21:44:05 +01005558 flags = link->flags;
Florian Westphal62256f92017-08-09 20:41:52 +02005559 if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
Florian Westphaladdf9b92017-12-02 21:44:05 +01005560 doit = link->doit;
Florian Westphal62256f92017-08-09 20:41:52 +02005561 rcu_read_unlock();
5562 if (doit)
5563 err = doit(skb, nlh, extack);
Florian Westphale4202512017-12-02 21:44:06 +01005564 module_put(owner);
Florian Westphal62256f92017-08-09 20:41:52 +02005565 return err;
5566 }
Florian Westphal6853dd42017-08-09 20:41:51 +02005567 rcu_read_unlock();
5568
Florian Westphal0cc09022017-08-09 20:41:50 +02005569 rtnl_lock();
Florian Westphaladdf9b92017-12-02 21:44:05 +01005570 link = rtnl_get_link(family, type);
5571 if (link && link->doit)
5572 err = link->doit(skb, nlh, extack);
Florian Westphal0cc09022017-08-09 20:41:50 +02005573 rtnl_unlock();
Florian Westphaladdf9b92017-12-02 21:44:05 +01005574
Florian Westphale4202512017-12-02 21:44:06 +01005575 module_put(owner);
5576
Florian Westphaladdf9b92017-12-02 21:44:05 +01005577 return err;
5578
5579out_unlock:
5580 rcu_read_unlock();
Florian Westphal0cc09022017-08-09 20:41:50 +02005581 return err;
5582
5583err_unlock:
Florian Westphal6853dd42017-08-09 20:41:51 +02005584 rcu_read_unlock();
Florian Westphal0cc09022017-08-09 20:41:50 +02005585 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586}
5587
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07005588static void rtnetlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07005590 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591}
5592
Julien Gomes5f729ea2017-06-20 13:54:16 -07005593static int rtnetlink_bind(struct net *net, int group)
5594{
5595 switch (group) {
5596 case RTNLGRP_IPV4_MROUTE_R:
5597 case RTNLGRP_IPV6_MROUTE_R:
5598 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
5599 return -EPERM;
5600 break;
5601 }
5602 return 0;
5603}
5604
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
5606{
Jiri Pirko351638e2013-05-28 01:30:21 +00005607 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02005608
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609 switch (event) {
Vlad Yasevich5138e862017-04-04 09:23:41 -04005610 case NETDEV_REBOOT:
Xin Long8a212582017-10-15 18:13:41 +08005611 case NETDEV_CHANGEMTU:
David Ahern37536542017-07-19 10:22:40 -07005612 case NETDEV_CHANGEADDR:
Vlad Yasevich5138e862017-04-04 09:23:41 -04005613 case NETDEV_CHANGENAME:
5614 case NETDEV_FEAT_CHANGE:
5615 case NETDEV_BONDING_FAILOVER:
Xin Longe6e66592017-10-15 18:13:43 +08005616 case NETDEV_POST_TYPE_CHANGE:
Vlad Yasevich5138e862017-04-04 09:23:41 -04005617 case NETDEV_NOTIFY_PEERS:
Xin Longdc709f32017-10-15 18:13:44 +08005618 case NETDEV_CHANGEUPPER:
Vlad Yasevich5138e862017-04-04 09:23:41 -04005619 case NETDEV_RESEND_IGMP:
Vlad Yasevich5138e862017-04-04 09:23:41 -04005620 case NETDEV_CHANGEINFODATA:
Xin Longeeda3fb2017-10-24 13:54:19 +08005621 case NETDEV_CHANGELOWERSTATE:
Xin Longebdcf042017-10-15 18:13:42 +08005622 case NETDEV_CHANGE_TX_QUEUE_LEN:
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04005623 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01005624 GFP_KERNEL, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625 break;
5626 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 break;
5628 }
5629 return NOTIFY_DONE;
5630}
5631
5632static struct notifier_block rtnetlink_dev_notifier = {
5633 .notifier_call = rtnetlink_event,
5634};
5635
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005636
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005637static int __net_init rtnetlink_net_init(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005638{
5639 struct sock *sk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00005640 struct netlink_kernel_cfg cfg = {
5641 .groups = RTNLGRP_MAX,
5642 .input = rtnetlink_rcv,
5643 .cb_mutex = &rtnl_mutex,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00005644 .flags = NL_CFG_F_NONROOT_RECV,
Julien Gomes5f729ea2017-06-20 13:54:16 -07005645 .bind = rtnetlink_bind,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00005646 };
5647
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00005648 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005649 if (!sk)
5650 return -ENOMEM;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005651 net->rtnl = sk;
5652 return 0;
5653}
5654
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005655static void __net_exit rtnetlink_net_exit(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005656{
Denis V. Lunev775516b2008-01-18 23:55:19 -08005657 netlink_kernel_release(net->rtnl);
5658 net->rtnl = NULL;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005659}
5660
5661static struct pernet_operations rtnetlink_net_ops = {
5662 .init = rtnetlink_net_init,
5663 .exit = rtnetlink_net_exit,
5664};
5665
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666void __init rtnetlink_init(void)
5667{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005668 if (register_pernet_subsys(&rtnetlink_net_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669 panic("rtnetlink_init: cannot initialize rtnetlink\n");
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005670
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 register_netdevice_notifier(&rtnetlink_dev_notifier);
Thomas Graf340d17f2007-03-22 11:49:22 -07005672
Greg Rosec7ac8672011-06-10 01:27:09 +00005673 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
Florian Westphalb97bac62017-08-09 20:41:48 +02005674 rtnl_dump_ifinfo, 0);
5675 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
5676 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
5677 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
Thomas Graf687ad8c2007-03-22 11:59:42 -07005678
Florian Westphalb97bac62017-08-09 20:41:48 +02005679 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
5680 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
5681 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
John Fastabend77162022012-04-15 06:43:56 +00005682
Jiri Pirko36fbf1e2019-09-30 11:48:16 +02005683 rtnl_register(PF_UNSPEC, RTM_NEWLINKPROP, rtnl_newlinkprop, NULL, 0);
5684 rtnl_register(PF_UNSPEC, RTM_DELLINKPROP, rtnl_dellinkprop, NULL, 0);
5685
Florian Westphalb97bac62017-08-09 20:41:48 +02005686 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
5687 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
Roopa Prabhu5b2f94b2018-12-15 22:35:08 -08005688 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0);
John Fastabende5a55a82012-10-24 08:12:57 +00005689
Florian Westphalb97bac62017-08-09 20:41:48 +02005690 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
5691 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
5692 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005693
5694 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
Florian Westphalb97bac62017-08-09 20:41:48 +02005695 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696}