blob: 3b6e551f9e694b92153badca8e564f1bd1b7ea75 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * Routing netlink socket interface: protocol independent part.
7 *
8 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
14 *
15 * Fixes:
16 * Vitaly E. Lavrov RTA_OK arithmetics was wrong.
17 */
18
Jakub Kicinskiee5d0322017-06-21 18:25:04 -070019#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/errno.h>
21#include <linux/module.h>
22#include <linux/types.h>
23#include <linux/socket.h>
24#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/timer.h>
26#include <linux/string.h>
27#include <linux/sockios.h>
28#include <linux/net.h>
29#include <linux/fcntl.h>
30#include <linux/mm.h>
31#include <linux/slab.h>
32#include <linux/interrupt.h>
33#include <linux/capability.h>
34#include <linux/skbuff.h>
35#include <linux/init.h>
36#include <linux/security.h>
Stephen Hemminger6756ae42006-03-20 22:23:58 -080037#include <linux/mutex.h>
Thomas Graf18237302006-08-04 23:04:54 -070038#include <linux/if_addr.h>
John Fastabend77162022012-04-15 06:43:56 +000039#include <linux/if_bridge.h>
Jiri Pirkof6f64242014-11-28 14:34:15 +010040#include <linux/if_vlan.h>
Williams, Mitch Aebc08a62010-02-10 01:44:05 +000041#include <linux/pci.h>
John Fastabend77162022012-04-15 06:43:56 +000042#include <linux/etherdevice.h>
Martin KaFai Lau58038692017-06-15 17:29:09 -070043#include <linux/bpf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080045#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#include <linux/inet.h>
48#include <linux/netdevice.h>
Jiri Pirko82f28412014-11-28 14:34:18 +010049#include <net/switchdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <net/ip.h>
51#include <net/protocol.h>
52#include <net/arp.h>
53#include <net/route.h>
54#include <net/udp.h>
Daniel Borkmannea697632015-01-05 23:57:47 +010055#include <net/tcp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <net/sock.h>
57#include <net/pkt_sched.h>
Thomas Graf14c0b972006-08-04 03:38:38 -070058#include <net/fib_rules.h>
Thomas Grafe2849862007-03-22 11:48:11 -070059#include <net/rtnetlink.h>
Johannes Berg30ffee82009-07-10 09:51:35 +000060#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Nikolay Aleksandrova428afe2018-11-24 04:34:20 +020062#define RTNL_MAX_TYPE 50
Kees Cookccf8dbc2018-05-30 15:20:52 -070063#define RTNL_SLAVE_MAX_TYPE 36
64
Eric Dumazete0d087a2009-11-07 01:26:17 -080065struct rtnl_link {
Thomas Grafe2849862007-03-22 11:48:11 -070066 rtnl_doit_func doit;
67 rtnl_dumpit_func dumpit;
Florian Westphale4202512017-12-02 21:44:06 +010068 struct module *owner;
Florian Westphal62256f92017-08-09 20:41:52 +020069 unsigned int flags;
Florian Westphaladdf9b92017-12-02 21:44:05 +010070 struct rcu_head rcu;
Thomas Grafe2849862007-03-22 11:48:11 -070071};
72
Stephen Hemminger6756ae42006-03-20 22:23:58 -080073static DEFINE_MUTEX(rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75void rtnl_lock(void)
76{
Stephen Hemminger6756ae42006-03-20 22:23:58 -080077 mutex_lock(&rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
Eric Dumazete0d087a2009-11-07 01:26:17 -080079EXPORT_SYMBOL(rtnl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Kirill Tkhai79ffdfc2018-03-14 22:17:20 +030081int rtnl_lock_killable(void)
82{
83 return mutex_lock_killable(&rtnl_mutex);
84}
85EXPORT_SYMBOL(rtnl_lock_killable);
86
Eric Dumazet1b5c5492016-06-13 20:21:50 -070087static struct sk_buff *defer_kfree_skb_list;
88void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail)
89{
90 if (head && tail) {
91 tail->next = defer_kfree_skb_list;
92 defer_kfree_skb_list = head;
93 }
94}
95EXPORT_SYMBOL(rtnl_kfree_skbs);
96
Stephen Hemminger6756ae42006-03-20 22:23:58 -080097void __rtnl_unlock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Eric Dumazet1b5c5492016-06-13 20:21:50 -070099 struct sk_buff *head = defer_kfree_skb_list;
100
101 defer_kfree_skb_list = NULL;
102
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800103 mutex_unlock(&rtnl_mutex);
Eric Dumazet1b5c5492016-06-13 20:21:50 -0700104
105 while (head) {
106 struct sk_buff *next = head->next;
107
108 kfree_skb(head);
109 cond_resched();
110 head = next;
111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114void rtnl_unlock(void)
115{
Herbert Xu58ec3b42008-10-07 15:50:03 -0700116 /* This fellow will unlock it for us. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 netdev_run_todo();
118}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800119EXPORT_SYMBOL(rtnl_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800121int rtnl_trylock(void)
122{
123 return mutex_trylock(&rtnl_mutex);
124}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800125EXPORT_SYMBOL(rtnl_trylock);
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800126
Patrick McHardyc9c10142008-04-23 22:10:48 -0700127int rtnl_is_locked(void)
128{
129 return mutex_is_locked(&rtnl_mutex);
130}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800131EXPORT_SYMBOL(rtnl_is_locked);
Patrick McHardyc9c10142008-04-23 22:10:48 -0700132
Vlad Buslov6f995282018-09-24 19:22:49 +0300133bool refcount_dec_and_rtnl_lock(refcount_t *r)
134{
135 return refcount_dec_and_mutex_lock(r, &rtnl_mutex);
136}
137EXPORT_SYMBOL(refcount_dec_and_rtnl_lock);
138
Paul E. McKenneya898def2010-02-22 17:04:49 -0800139#ifdef CONFIG_PROVE_LOCKING
Yaowei Bai0cbf3342015-10-08 21:29:02 +0800140bool lockdep_rtnl_is_held(void)
Paul E. McKenneya898def2010-02-22 17:04:49 -0800141{
142 return lockdep_is_held(&rtnl_mutex);
143}
144EXPORT_SYMBOL(lockdep_rtnl_is_held);
145#endif /* #ifdef CONFIG_PROVE_LOCKING */
146
Florian Westphalb0e9fe12017-12-04 22:42:30 +0100147static struct rtnl_link *__rcu *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
Thomas Grafe2849862007-03-22 11:48:11 -0700148
149static inline int rtm_msgindex(int msgtype)
150{
151 int msgindex = msgtype - RTM_BASE;
152
153 /*
154 * msgindex < 0 implies someone tried to register a netlink
155 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
156 * the message type has not been added to linux/rtnetlink.h
157 */
158 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
159
160 return msgindex;
161}
162
Florian Westphaladdf9b92017-12-02 21:44:05 +0100163static struct rtnl_link *rtnl_get_link(int protocol, int msgtype)
164{
165 struct rtnl_link **tab;
166
167 if (protocol >= ARRAY_SIZE(rtnl_msg_handlers))
168 protocol = PF_UNSPEC;
169
170 tab = rcu_dereference_rtnl(rtnl_msg_handlers[protocol]);
171 if (!tab)
172 tab = rcu_dereference_rtnl(rtnl_msg_handlers[PF_UNSPEC]);
173
174 return tab[msgtype];
175}
176
Florian Westphale4202512017-12-02 21:44:06 +0100177static int rtnl_register_internal(struct module *owner,
178 int protocol, int msgtype,
179 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
180 unsigned int flags)
Thomas Grafe2849862007-03-22 11:48:11 -0700181{
Florian Westphalb0e9fe12017-12-04 22:42:30 +0100182 struct rtnl_link *link, *old;
183 struct rtnl_link __rcu **tab;
Thomas Grafe2849862007-03-22 11:48:11 -0700184 int msgindex;
Florian Westphaladdf9b92017-12-02 21:44:05 +0100185 int ret = -ENOBUFS;
Thomas Grafe2849862007-03-22 11:48:11 -0700186
Patrick McHardy25239ce2010-04-26 16:02:05 +0200187 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700188 msgindex = rtm_msgindex(msgtype);
189
Florian Westphaladdf9b92017-12-02 21:44:05 +0100190 rtnl_lock();
191 tab = rtnl_msg_handlers[protocol];
Thomas Grafe2849862007-03-22 11:48:11 -0700192 if (tab == NULL) {
Florian Westphaladdf9b92017-12-02 21:44:05 +0100193 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(void *), GFP_KERNEL);
194 if (!tab)
195 goto unlock;
Thomas Grafe2849862007-03-22 11:48:11 -0700196
Florian Westphaladdf9b92017-12-02 21:44:05 +0100197 /* ensures we see the 0 stores */
Florian Westphal6853dd42017-08-09 20:41:51 +0200198 rcu_assign_pointer(rtnl_msg_handlers[protocol], tab);
Thomas Grafe2849862007-03-22 11:48:11 -0700199 }
200
Florian Westphaladdf9b92017-12-02 21:44:05 +0100201 old = rtnl_dereference(tab[msgindex]);
202 if (old) {
203 link = kmemdup(old, sizeof(*old), GFP_KERNEL);
204 if (!link)
205 goto unlock;
206 } else {
207 link = kzalloc(sizeof(*link), GFP_KERNEL);
208 if (!link)
209 goto unlock;
210 }
Thomas Grafe2849862007-03-22 11:48:11 -0700211
Florian Westphale4202512017-12-02 21:44:06 +0100212 WARN_ON(link->owner && link->owner != owner);
213 link->owner = owner;
214
Florian Westphaladdf9b92017-12-02 21:44:05 +0100215 WARN_ON(doit && link->doit && link->doit != doit);
216 if (doit)
217 link->doit = doit;
218 WARN_ON(dumpit && link->dumpit && link->dumpit != dumpit);
219 if (dumpit)
220 link->dumpit = dumpit;
221
222 link->flags |= flags;
223
224 /* publish protocol:msgtype */
225 rcu_assign_pointer(tab[msgindex], link);
226 ret = 0;
227 if (old)
228 kfree_rcu(old, rcu);
229unlock:
230 rtnl_unlock();
231 return ret;
Thomas Grafe2849862007-03-22 11:48:11 -0700232}
Florian Westphale4202512017-12-02 21:44:06 +0100233
234/**
235 * rtnl_register_module - Register a rtnetlink message type
236 *
237 * @owner: module registering the hook (THIS_MODULE)
238 * @protocol: Protocol family or PF_UNSPEC
239 * @msgtype: rtnetlink message type
240 * @doit: Function pointer called for each request message
241 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
242 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
243 *
244 * Like rtnl_register, but for use by removable modules.
245 */
246int rtnl_register_module(struct module *owner,
247 int protocol, int msgtype,
248 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
249 unsigned int flags)
250{
251 return rtnl_register_internal(owner, protocol, msgtype,
252 doit, dumpit, flags);
253}
254EXPORT_SYMBOL_GPL(rtnl_register_module);
255
256/**
Florian Westphal16feebc2017-12-02 21:44:08 +0100257 * rtnl_register - Register a rtnetlink message type
Florian Westphale4202512017-12-02 21:44:06 +0100258 * @protocol: Protocol family or PF_UNSPEC
259 * @msgtype: rtnetlink message type
260 * @doit: Function pointer called for each request message
261 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
262 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
263 *
264 * Registers the specified function pointers (at least one of them has
265 * to be non-NULL) to be called whenever a request message for the
266 * specified protocol family and message type is received.
267 *
268 * The special protocol family PF_UNSPEC may be used to define fallback
269 * function pointers for the case when no entry for the specific protocol
270 * family exists.
Thomas Grafe2849862007-03-22 11:48:11 -0700271 */
272void rtnl_register(int protocol, int msgtype,
Greg Rosec7ac8672011-06-10 01:27:09 +0000273 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
Florian Westphalb97bac62017-08-09 20:41:48 +0200274 unsigned int flags)
Thomas Grafe2849862007-03-22 11:48:11 -0700275{
Florian Westphal16feebc2017-12-02 21:44:08 +0100276 int err;
277
278 err = rtnl_register_internal(NULL, protocol, msgtype, doit, dumpit,
279 flags);
280 if (err)
281 pr_err("Unable to register rtnetlink message handler, "
282 "protocol = %d, message type = %d\n", protocol, msgtype);
Thomas Grafe2849862007-03-22 11:48:11 -0700283}
Thomas Grafe2849862007-03-22 11:48:11 -0700284
285/**
286 * rtnl_unregister - Unregister a rtnetlink message type
287 * @protocol: Protocol family or PF_UNSPEC
288 * @msgtype: rtnetlink message type
289 *
290 * Returns 0 on success or a negative error code.
291 */
292int rtnl_unregister(int protocol, int msgtype)
293{
Florian Westphaladdf9b92017-12-02 21:44:05 +0100294 struct rtnl_link **tab, *link;
Thomas Grafe2849862007-03-22 11:48:11 -0700295 int msgindex;
296
Patrick McHardy25239ce2010-04-26 16:02:05 +0200297 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700298 msgindex = rtm_msgindex(msgtype);
299
Florian Westphal6853dd42017-08-09 20:41:51 +0200300 rtnl_lock();
Florian Westphaladdf9b92017-12-02 21:44:05 +0100301 tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
302 if (!tab) {
Florian Westphal6853dd42017-08-09 20:41:51 +0200303 rtnl_unlock();
Thomas Grafe2849862007-03-22 11:48:11 -0700304 return -ENOENT;
Florian Westphal6853dd42017-08-09 20:41:51 +0200305 }
Thomas Grafe2849862007-03-22 11:48:11 -0700306
Florian Westphaladdf9b92017-12-02 21:44:05 +0100307 link = tab[msgindex];
308 rcu_assign_pointer(tab[msgindex], NULL);
Florian Westphal6853dd42017-08-09 20:41:51 +0200309 rtnl_unlock();
Thomas Grafe2849862007-03-22 11:48:11 -0700310
Florian Westphaladdf9b92017-12-02 21:44:05 +0100311 kfree_rcu(link, rcu);
312
Thomas Grafe2849862007-03-22 11:48:11 -0700313 return 0;
314}
Thomas Grafe2849862007-03-22 11:48:11 -0700315EXPORT_SYMBOL_GPL(rtnl_unregister);
316
317/**
318 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
319 * @protocol : Protocol family or PF_UNSPEC
320 *
321 * Identical to calling rtnl_unregster() for all registered message types
322 * of a certain protocol family.
323 */
324void rtnl_unregister_all(int protocol)
325{
Florian Westphaladdf9b92017-12-02 21:44:05 +0100326 struct rtnl_link **tab, *link;
327 int msgindex;
Florian Westphal019a3162017-08-09 20:41:49 +0200328
Patrick McHardy25239ce2010-04-26 16:02:05 +0200329 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700330
Florian Westphal019a3162017-08-09 20:41:49 +0200331 rtnl_lock();
Florian Westphaladdf9b92017-12-02 21:44:05 +0100332 tab = rtnl_msg_handlers[protocol];
Sabrina Dubrocaf707ef62018-08-28 13:40:53 +0200333 if (!tab) {
334 rtnl_unlock();
335 return;
336 }
Florian Westphal6853dd42017-08-09 20:41:51 +0200337 RCU_INIT_POINTER(rtnl_msg_handlers[protocol], NULL);
Florian Westphaladdf9b92017-12-02 21:44:05 +0100338 for (msgindex = 0; msgindex < RTM_NR_MSGTYPES; msgindex++) {
339 link = tab[msgindex];
340 if (!link)
341 continue;
342
343 rcu_assign_pointer(tab[msgindex], NULL);
344 kfree_rcu(link, rcu);
345 }
Florian Westphal019a3162017-08-09 20:41:49 +0200346 rtnl_unlock();
347
Florian Westphal6853dd42017-08-09 20:41:51 +0200348 synchronize_net();
349
Florian Westphaladdf9b92017-12-02 21:44:05 +0100350 kfree(tab);
Thomas Grafe2849862007-03-22 11:48:11 -0700351}
Thomas Grafe2849862007-03-22 11:48:11 -0700352EXPORT_SYMBOL_GPL(rtnl_unregister_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Patrick McHardy38f7b872007-06-13 12:03:51 -0700354static LIST_HEAD(link_ops);
355
Eric Dumazetc63044f2011-12-13 11:38:00 +0000356static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
357{
358 const struct rtnl_link_ops *ops;
359
360 list_for_each_entry(ops, &link_ops, list) {
361 if (!strcmp(ops->kind, kind))
362 return ops;
363 }
364 return NULL;
365}
366
Patrick McHardy38f7b872007-06-13 12:03:51 -0700367/**
368 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
369 * @ops: struct rtnl_link_ops * to register
370 *
371 * The caller must hold the rtnl_mutex. This function should be used
372 * by drivers that create devices during module initialization. It
373 * must be called before registering the devices.
374 *
375 * Returns 0 on success or a negative error code.
376 */
377int __rtnl_link_register(struct rtnl_link_ops *ops)
378{
Eric Dumazetc63044f2011-12-13 11:38:00 +0000379 if (rtnl_link_ops_get(ops->kind))
380 return -EEXIST;
381
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +0200382 /* The check for setup is here because if ops
383 * does not have that filled up, it is not possible
384 * to use the ops for creating device. So do not
385 * fill up dellink as well. That disables rtnl_dellink.
386 */
387 if (ops->setup && !ops->dellink)
Eric Dumazet23289a32009-10-27 07:06:36 +0000388 ops->dellink = unregister_netdevice_queue;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700389
Patrick McHardy38f7b872007-06-13 12:03:51 -0700390 list_add_tail(&ops->list, &link_ops);
391 return 0;
392}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700393EXPORT_SYMBOL_GPL(__rtnl_link_register);
394
395/**
396 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
397 * @ops: struct rtnl_link_ops * to register
398 *
399 * Returns 0 on success or a negative error code.
400 */
401int rtnl_link_register(struct rtnl_link_ops *ops)
402{
403 int err;
404
Kees Cookccf8dbc2018-05-30 15:20:52 -0700405 /* Sanity-check max sizes to avoid stack buffer overflow. */
406 if (WARN_ON(ops->maxtype > RTNL_MAX_TYPE ||
407 ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE))
408 return -EINVAL;
409
Patrick McHardy38f7b872007-06-13 12:03:51 -0700410 rtnl_lock();
411 err = __rtnl_link_register(ops);
412 rtnl_unlock();
413 return err;
414}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700415EXPORT_SYMBOL_GPL(rtnl_link_register);
416
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700417static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
418{
419 struct net_device *dev;
Eric Dumazet23289a32009-10-27 07:06:36 +0000420 LIST_HEAD(list_kill);
421
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700422 for_each_netdev(net, dev) {
Eric Dumazet23289a32009-10-27 07:06:36 +0000423 if (dev->rtnl_link_ops == ops)
424 ops->dellink(dev, &list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700425 }
Eric Dumazet23289a32009-10-27 07:06:36 +0000426 unregister_netdevice_many(&list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700427}
428
Patrick McHardy38f7b872007-06-13 12:03:51 -0700429/**
430 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
431 * @ops: struct rtnl_link_ops * to unregister
432 *
Kirill Tkhai554873e2018-03-30 19:38:37 +0300433 * The caller must hold the rtnl_mutex and guarantee net_namespace_list
434 * integrity (hold pernet_ops_rwsem for writing to close the race
435 * with setup_net() and cleanup_net()).
Patrick McHardy38f7b872007-06-13 12:03:51 -0700436 */
437void __rtnl_link_unregister(struct rtnl_link_ops *ops)
438{
Eric W. Biederman881d9662007-09-17 11:56:21 -0700439 struct net *net;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700440
Eric W. Biederman881d9662007-09-17 11:56:21 -0700441 for_each_net(net) {
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700442 __rtnl_kill_links(net, ops);
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700443 }
Patrick McHardy38f7b872007-06-13 12:03:51 -0700444 list_del(&ops->list);
445}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700446EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
447
Cong Wang200b9162014-05-12 15:11:20 -0700448/* Return with the rtnl_lock held when there are no network
449 * devices unregistering in any network namespace.
450 */
451static void rtnl_lock_unregistering_all(void)
452{
453 struct net *net;
454 bool unregistering;
Peter Zijlstraff960a72014-10-29 17:04:56 +0100455 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Cong Wang200b9162014-05-12 15:11:20 -0700456
Peter Zijlstraff960a72014-10-29 17:04:56 +0100457 add_wait_queue(&netdev_unregistering_wq, &wait);
Cong Wang200b9162014-05-12 15:11:20 -0700458 for (;;) {
Cong Wang200b9162014-05-12 15:11:20 -0700459 unregistering = false;
460 rtnl_lock();
Kirill Tkhaif0b07bb12018-03-29 19:20:32 +0300461 /* We held write locked pernet_ops_rwsem, and parallel
462 * setup_net() and cleanup_net() are not possible.
463 */
Cong Wang200b9162014-05-12 15:11:20 -0700464 for_each_net(net) {
465 if (net->dev_unreg_count > 0) {
466 unregistering = true;
467 break;
468 }
469 }
470 if (!unregistering)
471 break;
472 __rtnl_unlock();
Peter Zijlstraff960a72014-10-29 17:04:56 +0100473
474 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Cong Wang200b9162014-05-12 15:11:20 -0700475 }
Peter Zijlstraff960a72014-10-29 17:04:56 +0100476 remove_wait_queue(&netdev_unregistering_wq, &wait);
Cong Wang200b9162014-05-12 15:11:20 -0700477}
478
Patrick McHardy38f7b872007-06-13 12:03:51 -0700479/**
480 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
481 * @ops: struct rtnl_link_ops * to unregister
482 */
483void rtnl_link_unregister(struct rtnl_link_ops *ops)
484{
Kirill Tkhai8518e9b2018-03-27 18:02:32 +0300485 /* Close the race with setup_net() and cleanup_net() */
Kirill Tkhai4420bf22018-03-27 18:02:23 +0300486 down_write(&pernet_ops_rwsem);
Cong Wang200b9162014-05-12 15:11:20 -0700487 rtnl_lock_unregistering_all();
Patrick McHardy38f7b872007-06-13 12:03:51 -0700488 __rtnl_link_unregister(ops);
489 rtnl_unlock();
Kirill Tkhai4420bf22018-03-27 18:02:23 +0300490 up_write(&pernet_ops_rwsem);
Patrick McHardy38f7b872007-06-13 12:03:51 -0700491}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700492EXPORT_SYMBOL_GPL(rtnl_link_unregister);
493
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100494static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
495{
496 struct net_device *master_dev;
497 const struct rtnl_link_ops *ops;
Florian Westphal8515ae32017-08-10 16:52:59 +0200498 size_t size = 0;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100499
Florian Westphal8515ae32017-08-10 16:52:59 +0200500 rcu_read_lock();
501
502 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100503 if (!master_dev)
Florian Westphal8515ae32017-08-10 16:52:59 +0200504 goto out;
505
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100506 ops = master_dev->rtnl_link_ops;
Fernando Luis Vazquez Cao6049f252014-02-04 19:35:02 +0900507 if (!ops || !ops->get_slave_size)
Florian Westphal8515ae32017-08-10 16:52:59 +0200508 goto out;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100509 /* IFLA_INFO_SLAVE_DATA + nested data */
Florian Westphal8515ae32017-08-10 16:52:59 +0200510 size = nla_total_size(sizeof(struct nlattr)) +
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100511 ops->get_slave_size(master_dev, dev);
Florian Westphal8515ae32017-08-10 16:52:59 +0200512
513out:
514 rcu_read_unlock();
515 return size;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100516}
517
Patrick McHardy38f7b872007-06-13 12:03:51 -0700518static size_t rtnl_link_get_size(const struct net_device *dev)
519{
520 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
521 size_t size;
522
523 if (!ops)
524 return 0;
525
Thomas Graf369cf772010-11-11 15:47:59 +0000526 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
527 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700528
529 if (ops->get_size)
530 /* IFLA_INFO_DATA + nested data */
Thomas Graf369cf772010-11-11 15:47:59 +0000531 size += nla_total_size(sizeof(struct nlattr)) +
Patrick McHardy38f7b872007-06-13 12:03:51 -0700532 ops->get_size(dev);
533
534 if (ops->get_xstats_size)
Thomas Graf369cf772010-11-11 15:47:59 +0000535 /* IFLA_INFO_XSTATS */
536 size += nla_total_size(ops->get_xstats_size(dev));
Patrick McHardy38f7b872007-06-13 12:03:51 -0700537
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100538 size += rtnl_link_get_slave_info_data_size(dev);
539
Patrick McHardy38f7b872007-06-13 12:03:51 -0700540 return size;
541}
542
Thomas Graff8ff1822010-11-16 04:30:14 +0000543static LIST_HEAD(rtnl_af_ops);
544
545static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
546{
547 const struct rtnl_af_ops *ops;
548
Florian Westphal5fa85a02017-10-16 15:44:36 +0200549 list_for_each_entry_rcu(ops, &rtnl_af_ops, list) {
Thomas Graff8ff1822010-11-16 04:30:14 +0000550 if (ops->family == family)
551 return ops;
552 }
553
554 return NULL;
555}
556
557/**
Thomas Graff8ff1822010-11-16 04:30:14 +0000558 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
559 * @ops: struct rtnl_af_ops * to register
560 *
561 * Returns 0 on success or a negative error code.
562 */
stephen hemminger3678a9d2013-12-30 10:41:32 -0800563void rtnl_af_register(struct rtnl_af_ops *ops)
Thomas Graff8ff1822010-11-16 04:30:14 +0000564{
Thomas Graff8ff1822010-11-16 04:30:14 +0000565 rtnl_lock();
Florian Westphal5fa85a02017-10-16 15:44:36 +0200566 list_add_tail_rcu(&ops->list, &rtnl_af_ops);
Thomas Graff8ff1822010-11-16 04:30:14 +0000567 rtnl_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +0000568}
569EXPORT_SYMBOL_GPL(rtnl_af_register);
570
571/**
Thomas Graff8ff1822010-11-16 04:30:14 +0000572 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
573 * @ops: struct rtnl_af_ops * to unregister
574 */
575void rtnl_af_unregister(struct rtnl_af_ops *ops)
576{
577 rtnl_lock();
Florian Westphal5fa85a02017-10-16 15:44:36 +0200578 list_del_rcu(&ops->list);
Thomas Graff8ff1822010-11-16 04:30:14 +0000579 rtnl_unlock();
Florian Westphal5fa85a02017-10-16 15:44:36 +0200580
581 synchronize_rcu();
Thomas Graff8ff1822010-11-16 04:30:14 +0000582}
583EXPORT_SYMBOL_GPL(rtnl_af_unregister);
584
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700585static size_t rtnl_link_get_af_size(const struct net_device *dev,
586 u32 ext_filter_mask)
Thomas Graff8ff1822010-11-16 04:30:14 +0000587{
588 struct rtnl_af_ops *af_ops;
589 size_t size;
590
591 /* IFLA_AF_SPEC */
592 size = nla_total_size(sizeof(struct nlattr));
593
Florian Westphal5fa85a02017-10-16 15:44:36 +0200594 rcu_read_lock();
595 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Thomas Graff8ff1822010-11-16 04:30:14 +0000596 if (af_ops->get_link_af_size) {
597 /* AF_* + nested data */
598 size += nla_total_size(sizeof(struct nlattr)) +
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700599 af_ops->get_link_af_size(dev, ext_filter_mask);
Thomas Graff8ff1822010-11-16 04:30:14 +0000600 }
601 }
Florian Westphal5fa85a02017-10-16 15:44:36 +0200602 rcu_read_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +0000603
604 return size;
605}
606
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100607static bool rtnl_have_link_slave_info(const struct net_device *dev)
608{
609 struct net_device *master_dev;
Florian Westphal4c82a952017-09-26 13:58:43 +0200610 bool ret = false;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100611
Florian Westphal4c82a952017-09-26 13:58:43 +0200612 rcu_read_lock();
613
614 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
Jiri Pirko813f0202014-01-23 19:19:21 +0100615 if (master_dev && master_dev->rtnl_link_ops)
Florian Westphal4c82a952017-09-26 13:58:43 +0200616 ret = true;
617 rcu_read_unlock();
618 return ret;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100619}
620
621static int rtnl_link_slave_info_fill(struct sk_buff *skb,
622 const struct net_device *dev)
623{
624 struct net_device *master_dev;
625 const struct rtnl_link_ops *ops;
626 struct nlattr *slave_data;
627 int err;
628
629 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
630 if (!master_dev)
631 return 0;
632 ops = master_dev->rtnl_link_ops;
633 if (!ops)
634 return 0;
635 if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
636 return -EMSGSIZE;
637 if (ops->fill_slave_info) {
638 slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA);
639 if (!slave_data)
640 return -EMSGSIZE;
641 err = ops->fill_slave_info(skb, master_dev, dev);
642 if (err < 0)
643 goto err_cancel_slave_data;
644 nla_nest_end(skb, slave_data);
645 }
646 return 0;
647
648err_cancel_slave_data:
649 nla_nest_cancel(skb, slave_data);
650 return err;
651}
652
653static int rtnl_link_info_fill(struct sk_buff *skb,
654 const struct net_device *dev)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700655{
656 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100657 struct nlattr *data;
658 int err;
659
660 if (!ops)
661 return 0;
662 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
663 return -EMSGSIZE;
664 if (ops->fill_xstats) {
665 err = ops->fill_xstats(skb, dev);
666 if (err < 0)
667 return err;
668 }
669 if (ops->fill_info) {
670 data = nla_nest_start(skb, IFLA_INFO_DATA);
671 if (data == NULL)
672 return -EMSGSIZE;
673 err = ops->fill_info(skb, dev);
674 if (err < 0)
675 goto err_cancel_data;
676 nla_nest_end(skb, data);
677 }
678 return 0;
679
680err_cancel_data:
681 nla_nest_cancel(skb, data);
682 return err;
683}
684
685static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
686{
687 struct nlattr *linkinfo;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700688 int err = -EMSGSIZE;
689
690 linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
691 if (linkinfo == NULL)
692 goto out;
693
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100694 err = rtnl_link_info_fill(skb, dev);
695 if (err < 0)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700696 goto err_cancel_link;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100697
698 err = rtnl_link_slave_info_fill(skb, dev);
699 if (err < 0)
700 goto err_cancel_link;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700701
702 nla_nest_end(skb, linkinfo);
703 return 0;
704
Patrick McHardy38f7b872007-06-13 12:03:51 -0700705err_cancel_link:
706 nla_nest_cancel(skb, linkinfo);
707out:
708 return err;
709}
710
Eric Dumazet95c96172012-04-15 05:58:06 +0000711int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800713 struct sock *rtnl = net->rtnl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 int err = 0;
715
Patrick McHardyac6d4392005-08-14 19:29:52 -0700716 NETLINK_CB(skb).dst_group = group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 if (echo)
Reshetova, Elena63354792017-06-30 13:07:58 +0300718 refcount_inc(&skb->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
720 if (echo)
721 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
722 return err;
723}
724
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800725int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
Thomas Graf2942e902006-08-15 00:30:25 -0700726{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800727 struct sock *rtnl = net->rtnl;
728
Thomas Graf2942e902006-08-15 00:30:25 -0700729 return nlmsg_unicast(rtnl, skb, pid);
730}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800731EXPORT_SYMBOL(rtnl_unicast);
Thomas Graf2942e902006-08-15 00:30:25 -0700732
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800733void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
734 struct nlmsghdr *nlh, gfp_t flags)
Thomas Graf97676b62006-08-15 00:31:41 -0700735{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800736 struct sock *rtnl = net->rtnl;
Thomas Graf97676b62006-08-15 00:31:41 -0700737 int report = 0;
738
739 if (nlh)
740 report = nlmsg_report(nlh);
741
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800742 nlmsg_notify(rtnl, skb, pid, group, report, flags);
Thomas Graf97676b62006-08-15 00:31:41 -0700743}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800744EXPORT_SYMBOL(rtnl_notify);
Thomas Graf97676b62006-08-15 00:31:41 -0700745
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800746void rtnl_set_sk_err(struct net *net, u32 group, int error)
Thomas Graf97676b62006-08-15 00:31:41 -0700747{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800748 struct sock *rtnl = net->rtnl;
749
Thomas Graf97676b62006-08-15 00:31:41 -0700750 netlink_set_err(rtnl, 0, group, error);
751}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800752EXPORT_SYMBOL(rtnl_set_sk_err);
Thomas Graf97676b62006-08-15 00:31:41 -0700753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
755{
Thomas Graf2d7202b2006-08-22 00:01:27 -0700756 struct nlattr *mx;
757 int i, valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Thomas Graf2d7202b2006-08-22 00:01:27 -0700759 mx = nla_nest_start(skb, RTA_METRICS);
760 if (mx == NULL)
761 return -ENOBUFS;
762
763 for (i = 0; i < RTAX_MAX; i++) {
764 if (metrics[i]) {
Daniel Borkmannea697632015-01-05 23:57:47 +0100765 if (i == RTAX_CC_ALGO - 1) {
766 char tmp[TCP_CA_NAME_MAX], *name;
767
768 name = tcp_ca_get_name_by_key(metrics[i], tmp);
769 if (!name)
770 continue;
771 if (nla_put_string(skb, i + 1, name))
772 goto nla_put_failure;
Daniel Borkmannc3a8d942015-08-31 15:58:47 +0200773 } else if (i == RTAX_FEATURES - 1) {
774 u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
775
Phil Sutterf8edcd12016-08-23 13:14:31 +0200776 if (!user_features)
777 continue;
Daniel Borkmannc3a8d942015-08-31 15:58:47 +0200778 BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
779 if (nla_put_u32(skb, i + 1, user_features))
780 goto nla_put_failure;
Daniel Borkmannea697632015-01-05 23:57:47 +0100781 } else {
782 if (nla_put_u32(skb, i + 1, metrics[i]))
783 goto nla_put_failure;
784 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700785 valid++;
Thomas Graf2d7202b2006-08-22 00:01:27 -0700786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
David S. Millera57d27f2006-08-22 22:20:14 -0700789 if (!valid) {
790 nla_nest_cancel(skb, mx);
791 return 0;
792 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700793
794 return nla_nest_end(skb, mx);
795
796nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700797 nla_nest_cancel(skb, mx);
798 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800800EXPORT_SYMBOL(rtnetlink_put_metrics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Thomas Grafe3703b32006-11-27 09:27:07 -0800802int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
David S. Miller87a50692012-07-10 05:06:14 -0700803 long expires, u32 error)
Thomas Grafe3703b32006-11-27 09:27:07 -0800804{
805 struct rta_cacheinfo ci = {
Thomas Grafe3703b32006-11-27 09:27:07 -0800806 .rta_error = error,
807 .rta_id = id,
Thomas Grafe3703b32006-11-27 09:27:07 -0800808 };
809
David Ahern39407462018-04-17 17:33:08 -0700810 if (dst) {
811 ci.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse);
812 ci.rta_used = dst->__use;
813 ci.rta_clntref = atomic_read(&dst->__refcnt);
814 }
Li Wei82539472012-07-29 16:01:30 +0000815 if (expires) {
816 unsigned long clock;
Thomas Grafe3703b32006-11-27 09:27:07 -0800817
Li Wei82539472012-07-29 16:01:30 +0000818 clock = jiffies_to_clock_t(abs(expires));
819 clock = min_t(unsigned long, clock, INT_MAX);
820 ci.rta_expires = (expires > 0) ? clock : -clock;
821 }
Thomas Grafe3703b32006-11-27 09:27:07 -0800822 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
823}
Thomas Grafe3703b32006-11-27 09:27:07 -0800824EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
David S. Miller93b2d4a2008-02-17 18:35:07 -0800826static void set_operstate(struct net_device *dev, unsigned char transition)
Stefan Rompfb00055a2006-03-20 17:09:11 -0800827{
828 unsigned char operstate = dev->operstate;
829
Eric Dumazete0d087a2009-11-07 01:26:17 -0800830 switch (transition) {
Stefan Rompfb00055a2006-03-20 17:09:11 -0800831 case IF_OPER_UP:
832 if ((operstate == IF_OPER_DORMANT ||
833 operstate == IF_OPER_UNKNOWN) &&
834 !netif_dormant(dev))
835 operstate = IF_OPER_UP;
836 break;
837
838 case IF_OPER_DORMANT:
839 if (operstate == IF_OPER_UP ||
840 operstate == IF_OPER_UNKNOWN)
841 operstate = IF_OPER_DORMANT;
842 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700843 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800844
845 if (dev->operstate != operstate) {
846 write_lock_bh(&dev_base_lock);
847 dev->operstate = operstate;
848 write_unlock_bh(&dev_base_lock);
David S. Miller93b2d4a2008-02-17 18:35:07 -0800849 netdev_state_change(dev);
850 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800851}
852
Jiri Bencb1beb682012-07-27 02:58:22 +0000853static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
854{
855 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
856 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
857}
858
Patrick McHardy3729d502010-02-26 06:34:54 +0000859static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
860 const struct ifinfomsg *ifm)
861{
862 unsigned int flags = ifm->ifi_flags;
863
864 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
865 if (ifm->ifi_change)
866 flags = (flags & ifm->ifi_change) |
Jiri Bencb1beb682012-07-27 02:58:22 +0000867 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
Patrick McHardy3729d502010-02-26 06:34:54 +0000868
869 return flags;
870}
871
Thomas Grafb60c5112006-08-04 23:05:34 -0700872static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000873 const struct rtnl_link_stats64 *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Thomas Grafb60c5112006-08-04 23:05:34 -0700875 a->rx_packets = b->rx_packets;
876 a->tx_packets = b->tx_packets;
877 a->rx_bytes = b->rx_bytes;
878 a->tx_bytes = b->tx_bytes;
879 a->rx_errors = b->rx_errors;
880 a->tx_errors = b->tx_errors;
881 a->rx_dropped = b->rx_dropped;
882 a->tx_dropped = b->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Thomas Grafb60c5112006-08-04 23:05:34 -0700884 a->multicast = b->multicast;
885 a->collisions = b->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Thomas Grafb60c5112006-08-04 23:05:34 -0700887 a->rx_length_errors = b->rx_length_errors;
888 a->rx_over_errors = b->rx_over_errors;
889 a->rx_crc_errors = b->rx_crc_errors;
890 a->rx_frame_errors = b->rx_frame_errors;
891 a->rx_fifo_errors = b->rx_fifo_errors;
892 a->rx_missed_errors = b->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Thomas Grafb60c5112006-08-04 23:05:34 -0700894 a->tx_aborted_errors = b->tx_aborted_errors;
895 a->tx_carrier_errors = b->tx_carrier_errors;
896 a->tx_fifo_errors = b->tx_fifo_errors;
897 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
898 a->tx_window_errors = b->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Thomas Grafb60c5112006-08-04 23:05:34 -0700900 a->rx_compressed = b->rx_compressed;
901 a->tx_compressed = b->tx_compressed;
Jarod Wilson6e7333d2016-02-01 18:51:05 -0500902
903 a->rx_nohandler = b->rx_nohandler;
Jan Engelhardt10708f32010-03-11 09:57:29 +0000904}
905
Chris Wrightc02db8c2010-05-16 01:05:45 -0700906/* All VF info */
Greg Rose115c9b82012-02-21 16:54:48 -0500907static inline int rtnl_vfinfo_size(const struct net_device *dev,
908 u32 ext_filter_mask)
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000909{
Phil Sutter9af15c32017-01-18 14:04:39 +0100910 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
Chris Wrightc02db8c2010-05-16 01:05:45 -0700911 int num_vfs = dev_num_vf(dev->dev.parent);
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100912 size_t size = nla_total_size(0);
Scott Feldman045de012010-05-28 03:42:43 -0700913 size += num_vfs *
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100914 (nla_total_size(0) +
915 nla_total_size(sizeof(struct ifla_vf_mac)) +
916 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
Greg Rose115c9b82012-02-21 16:54:48 -0500983static noinline size_t if_nlmsg_size(const struct net_device *dev,
984 u32 ext_filter_mask)
Thomas Graf339bf982006-11-10 14:10:15 -0800985{
986 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
987 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700988 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
Thomas Graf339bf982006-11-10 14:10:15 -0800989 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
Nicolas Dichtel270cb4d2016-04-26 10:06:16 +0200990 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
Thomas Graf339bf982006-11-10 14:10:15 -0800991 + nla_total_size(sizeof(struct rtnl_link_stats))
David S. Miller35c58452016-04-19 19:49:29 -0400992 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
Thomas Graf339bf982006-11-10 14:10:15 -0800993 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
994 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
995 + nla_total_size(4) /* IFLA_TXQLEN */
996 + nla_total_size(4) /* IFLA_WEIGHT */
997 + nla_total_size(4) /* IFLA_MTU */
998 + nla_total_size(4) /* IFLA_LINK */
999 + nla_total_size(4) /* IFLA_MASTER */
Jiri Pirko9a572472012-12-27 23:49:39 +00001000 + nla_total_size(1) /* IFLA_CARRIER */
Ben Greearedbc0bb2012-03-29 12:51:30 +00001001 + nla_total_size(4) /* IFLA_PROMISCUITY */
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001002 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
1003 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
Tobias Klauser69197562016-11-30 14:30:37 +01001004 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
1005 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
Thomas Graf339bf982006-11-10 14:10:15 -08001006 + nla_total_size(1) /* IFLA_OPERSTATE */
Patrick McHardy38f7b872007-06-13 12:03:51 -07001007 + nla_total_size(1) /* IFLA_LINKMODE */
david decotigny2d3b4792014-03-29 09:48:35 -07001008 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001009 + nla_total_size(4) /* IFLA_LINK_NETNSID */
Serhey Popovychdb833d42017-06-20 14:35:23 +03001010 + nla_total_size(4) /* IFLA_GROUP */
Greg Rose115c9b82012-02-21 16:54:48 -05001011 + nla_total_size(ext_filter_mask
1012 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
1013 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
David Gibsonc53864f2014-04-24 10:22:36 +10001014 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
Thomas Graff8ff1822010-11-16 04:30:14 +00001015 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
Arad, Ronenb1974ed2015-10-19 09:23:28 -07001016 + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
Jiri Pirko82f28412014-11-28 14:34:18 +01001017 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001018 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
Nicolas Dichtelc57c7a92016-03-31 18:10:31 +02001019 + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
David S. Millerb5cdae32017-04-18 15:36:58 -04001020 + rtnl_xdp_size() /* IFLA_XDP */
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001021 + nla_total_size(4) /* IFLA_EVENT */
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02001022 + nla_total_size(4) /* IFLA_NEW_NETNSID */
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01001023 + nla_total_size(4) /* IFLA_NEW_IFINDEX */
Colin Ian King03ac7382017-11-06 15:04:54 +00001024 + nla_total_size(1) /* IFLA_PROTO_DOWN */
Christian Brauner7e4a8d52018-09-04 21:53:53 +02001025 + nla_total_size(4) /* IFLA_TARGET_NETNSID */
David Decotignyb2d3bcf2018-01-18 09:59:13 -08001026 + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */
1027 + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */
Stephen Hemminger3e7a50c2018-07-27 13:43:22 -07001028 + nla_total_size(4) /* IFLA_MIN_MTU */
1029 + nla_total_size(4) /* IFLA_MAX_MTU */
Jiri Benc79e1ad12017-11-02 17:04:38 -02001030 + 0;
Thomas Graf339bf982006-11-10 14:10:15 -08001031}
1032
Scott Feldman57b61082010-05-17 22:49:55 -07001033static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
1034{
1035 struct nlattr *vf_ports;
1036 struct nlattr *vf_port;
1037 int vf;
1038 int err;
1039
1040 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
1041 if (!vf_ports)
1042 return -EMSGSIZE;
1043
1044 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
1045 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
Scott Feldman8ca94182010-05-28 03:42:18 -07001046 if (!vf_port)
1047 goto nla_put_failure;
David S. Millera6574342012-04-01 20:12:00 -04001048 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
1049 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001050 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
Scott Feldman8ca94182010-05-28 03:42:18 -07001051 if (err == -EMSGSIZE)
1052 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001053 if (err) {
Scott Feldman57b61082010-05-17 22:49:55 -07001054 nla_nest_cancel(skb, vf_port);
1055 continue;
1056 }
1057 nla_nest_end(skb, vf_port);
1058 }
1059
1060 nla_nest_end(skb, vf_ports);
1061
1062 return 0;
Scott Feldman8ca94182010-05-28 03:42:18 -07001063
1064nla_put_failure:
1065 nla_nest_cancel(skb, vf_ports);
1066 return -EMSGSIZE;
Scott Feldman57b61082010-05-17 22:49:55 -07001067}
1068
1069static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
1070{
1071 struct nlattr *port_self;
1072 int err;
1073
1074 port_self = nla_nest_start(skb, IFLA_PORT_SELF);
1075 if (!port_self)
1076 return -EMSGSIZE;
1077
1078 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
1079 if (err) {
1080 nla_nest_cancel(skb, port_self);
Scott Feldman8ca94182010-05-28 03:42:18 -07001081 return (err == -EMSGSIZE) ? err : 0;
Scott Feldman57b61082010-05-17 22:49:55 -07001082 }
1083
1084 nla_nest_end(skb, port_self);
1085
1086 return 0;
1087}
1088
David Gibsonc53864f2014-04-24 10:22:36 +10001089static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1090 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -07001091{
1092 int err;
1093
David Gibsonc53864f2014-04-24 10:22:36 +10001094 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1095 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -07001096 return 0;
1097
1098 err = rtnl_port_self_fill(skb, dev);
1099 if (err)
1100 return err;
1101
1102 if (dev_num_vf(dev->dev.parent)) {
1103 err = rtnl_vf_ports_fill(skb, dev);
1104 if (err)
1105 return err;
1106 }
1107
1108 return 0;
1109}
1110
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001111static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
1112{
1113 int err;
Jiri Pirko02637fc2014-11-28 14:34:16 +01001114 struct netdev_phys_item_id ppid;
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001115
1116 err = dev_get_phys_port_id(dev, &ppid);
1117 if (err) {
1118 if (err == -EOPNOTSUPP)
1119 return 0;
1120 return err;
1121 }
1122
1123 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1124 return -EMSGSIZE;
1125
1126 return 0;
1127}
1128
David Aherndb24a902015-03-17 20:23:15 -06001129static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1130{
1131 char name[IFNAMSIZ];
1132 int err;
1133
1134 err = dev_get_phys_port_name(dev, name, sizeof(name));
1135 if (err) {
1136 if (err == -EOPNOTSUPP)
1137 return 0;
1138 return err;
1139 }
1140
Michal Schmidt77ef0332017-05-04 16:48:58 +02001141 if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
David Aherndb24a902015-03-17 20:23:15 -06001142 return -EMSGSIZE;
1143
1144 return 0;
1145}
1146
Jiri Pirko82f28412014-11-28 14:34:18 +01001147static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1148{
1149 int err;
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001150 struct switchdev_attr attr = {
Ido Schimmel6ff64f62015-12-15 16:03:35 +01001151 .orig_dev = dev,
Jiri Pirko1f868392015-10-01 11:03:42 +02001152 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001153 .flags = SWITCHDEV_F_NO_RECURSE,
1154 };
Jiri Pirko82f28412014-11-28 14:34:18 +01001155
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001156 err = switchdev_port_attr_get(dev, &attr);
Jiri Pirko82f28412014-11-28 14:34:18 +01001157 if (err) {
1158 if (err == -EOPNOTSUPP)
1159 return 0;
1160 return err;
1161 }
1162
Scott Feldman42275bd2015-05-13 11:16:50 -07001163 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len,
1164 attr.u.ppid.id))
Jiri Pirko82f28412014-11-28 14:34:18 +01001165 return -EMSGSIZE;
1166
1167 return 0;
1168}
1169
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001170static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1171 struct net_device *dev)
1172{
Roopa Prabhu550bce52016-04-15 20:36:25 -07001173 struct rtnl_link_stats64 *sp;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001174 struct nlattr *attr;
1175
Nicolas Dichtel58414d32016-04-21 18:58:25 +02001176 attr = nla_reserve_64bit(skb, IFLA_STATS64,
1177 sizeof(struct rtnl_link_stats64), IFLA_PAD);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001178 if (!attr)
1179 return -EMSGSIZE;
1180
Roopa Prabhu550bce52016-04-15 20:36:25 -07001181 sp = nla_data(attr);
1182 dev_get_stats(dev, sp);
1183
1184 attr = nla_reserve(skb, IFLA_STATS,
1185 sizeof(struct rtnl_link_stats));
1186 if (!attr)
1187 return -EMSGSIZE;
1188
1189 copy_rtnl_link_stats(nla_data(attr), sp);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001190
1191 return 0;
1192}
1193
1194static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1195 struct net_device *dev,
1196 int vfs_num,
1197 struct nlattr *vfinfo)
1198{
1199 struct ifla_vf_rss_query_en vf_rss_query_en;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001200 struct nlattr *vf, *vfstats, *vfvlanlist;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001201 struct ifla_vf_link_state vf_linkstate;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001202 struct ifla_vf_vlan_info vf_vlan_info;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001203 struct ifla_vf_spoofchk vf_spoofchk;
1204 struct ifla_vf_tx_rate vf_tx_rate;
1205 struct ifla_vf_stats vf_stats;
1206 struct ifla_vf_trust vf_trust;
1207 struct ifla_vf_vlan vf_vlan;
1208 struct ifla_vf_rate vf_rate;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001209 struct ifla_vf_mac vf_mac;
1210 struct ifla_vf_info ivi;
1211
Mintz, Yuval0eed9cf2017-06-07 21:00:33 +03001212 memset(&ivi, 0, sizeof(ivi));
1213
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001214 /* Not all SR-IOV capable drivers support the
1215 * spoofcheck and "RSS query enable" query. Preset to
1216 * -1 so the user space tool can detect that the driver
1217 * didn't report anything.
1218 */
1219 ivi.spoofchk = -1;
1220 ivi.rss_query_en = -1;
1221 ivi.trusted = -1;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001222 /* The default value for VF link state is "auto"
1223 * IFLA_VF_LINK_STATE_AUTO which equals zero
1224 */
1225 ivi.linkstate = 0;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001226 /* VLAN Protocol by default is 802.1Q */
1227 ivi.vlan_proto = htons(ETH_P_8021Q);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001228 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1229 return 0;
1230
Dan Carpenter775f4f02016-10-13 11:45:28 +03001231 memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
1232
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001233 vf_mac.vf =
1234 vf_vlan.vf =
Moshe Shemesh79aab092016-09-22 12:11:15 +03001235 vf_vlan_info.vf =
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001236 vf_rate.vf =
1237 vf_tx_rate.vf =
1238 vf_spoofchk.vf =
1239 vf_linkstate.vf =
1240 vf_rss_query_en.vf =
1241 vf_trust.vf = ivi.vf;
1242
1243 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1244 vf_vlan.vlan = ivi.vlan;
1245 vf_vlan.qos = ivi.qos;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001246 vf_vlan_info.vlan = ivi.vlan;
1247 vf_vlan_info.qos = ivi.qos;
1248 vf_vlan_info.vlan_proto = ivi.vlan_proto;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001249 vf_tx_rate.rate = ivi.max_tx_rate;
1250 vf_rate.min_tx_rate = ivi.min_tx_rate;
1251 vf_rate.max_tx_rate = ivi.max_tx_rate;
1252 vf_spoofchk.setting = ivi.spoofchk;
1253 vf_linkstate.link_state = ivi.linkstate;
1254 vf_rss_query_en.setting = ivi.rss_query_en;
1255 vf_trust.setting = ivi.trusted;
1256 vf = nla_nest_start(skb, IFLA_VF_INFO);
Moshe Shemesh79aab092016-09-22 12:11:15 +03001257 if (!vf)
1258 goto nla_put_vfinfo_failure;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001259 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1260 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1261 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1262 &vf_rate) ||
1263 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1264 &vf_tx_rate) ||
1265 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1266 &vf_spoofchk) ||
1267 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1268 &vf_linkstate) ||
1269 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1270 sizeof(vf_rss_query_en),
1271 &vf_rss_query_en) ||
1272 nla_put(skb, IFLA_VF_TRUST,
1273 sizeof(vf_trust), &vf_trust))
Moshe Shemesh79aab092016-09-22 12:11:15 +03001274 goto nla_put_vf_failure;
1275 vfvlanlist = nla_nest_start(skb, IFLA_VF_VLAN_LIST);
1276 if (!vfvlanlist)
1277 goto nla_put_vf_failure;
1278 if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
1279 &vf_vlan_info)) {
1280 nla_nest_cancel(skb, vfvlanlist);
1281 goto nla_put_vf_failure;
1282 }
1283 nla_nest_end(skb, vfvlanlist);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001284 memset(&vf_stats, 0, sizeof(vf_stats));
1285 if (dev->netdev_ops->ndo_get_vf_stats)
1286 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1287 &vf_stats);
1288 vfstats = nla_nest_start(skb, IFLA_VF_STATS);
Moshe Shemesh79aab092016-09-22 12:11:15 +03001289 if (!vfstats)
1290 goto nla_put_vf_failure;
Nicolas Dichtel343a6d82016-04-25 10:25:14 +02001291 if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1292 vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1293 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1294 vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1295 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1296 vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1297 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1298 vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1299 nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1300 vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1301 nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
Eugenia Emantayevc5a9f6f2017-07-17 13:47:07 +03001302 vf_stats.multicast, IFLA_VF_STATS_PAD) ||
1303 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED,
1304 vf_stats.rx_dropped, IFLA_VF_STATS_PAD) ||
1305 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED,
1306 vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) {
Moshe Shemesh79aab092016-09-22 12:11:15 +03001307 nla_nest_cancel(skb, vfstats);
1308 goto nla_put_vf_failure;
1309 }
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001310 nla_nest_end(skb, vfstats);
1311 nla_nest_end(skb, vf);
1312 return 0;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001313
1314nla_put_vf_failure:
1315 nla_nest_cancel(skb, vf);
1316nla_put_vfinfo_failure:
1317 nla_nest_cancel(skb, vfinfo);
1318 return -EMSGSIZE;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001319}
1320
Florian Westphal250fc3d2017-09-26 13:58:41 +02001321static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb,
1322 struct net_device *dev,
1323 u32 ext_filter_mask)
1324{
1325 struct nlattr *vfinfo;
1326 int i, num_vfs;
1327
1328 if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0))
1329 return 0;
1330
1331 num_vfs = dev_num_vf(dev->dev.parent);
1332 if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs))
1333 return -EMSGSIZE;
1334
1335 if (!dev->netdev_ops->ndo_get_vf_config)
1336 return 0;
1337
1338 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1339 if (!vfinfo)
1340 return -EMSGSIZE;
1341
1342 for (i = 0; i < num_vfs; i++) {
1343 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1344 return -EMSGSIZE;
1345 }
1346
1347 nla_nest_end(skb, vfinfo);
1348 return 0;
1349}
1350
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001351static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1352{
Kangjie Lu5f8e4472016-05-03 16:46:24 -04001353 struct rtnl_link_ifmap map;
1354
1355 memset(&map, 0, sizeof(map));
1356 map.mem_start = dev->mem_start;
1357 map.mem_end = dev->mem_end;
1358 map.base_addr = dev->base_addr;
1359 map.irq = dev->irq;
1360 map.dma = dev->dma;
1361 map.port = dev->if_port;
1362
Nicolas Dichtel270cb4d2016-04-26 10:06:16 +02001363 if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001364 return -EMSGSIZE;
1365
1366 return 0;
1367}
1368
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001369static u32 rtnl_xdp_prog_skb(struct net_device *dev)
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001370{
Martin KaFai Lau58038692017-06-15 17:29:09 -07001371 const struct bpf_prog *generic_xdp_prog;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001372
1373 ASSERT_RTNL();
1374
Martin KaFai Lau58038692017-06-15 17:29:09 -07001375 generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001376 if (!generic_xdp_prog)
1377 return 0;
1378 return generic_xdp_prog->aux->id;
1379}
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001380
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001381static u32 rtnl_xdp_prog_drv(struct net_device *dev)
1382{
1383 return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf, XDP_QUERY_PROG);
1384}
Jakub Kicinski118b4aa2017-12-01 15:08:55 -08001385
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001386static u32 rtnl_xdp_prog_hw(struct net_device *dev)
1387{
1388 return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf,
1389 XDP_QUERY_PROG_HW);
1390}
1391
1392static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev,
1393 u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr,
1394 u32 (*get_prog_id)(struct net_device *dev))
1395{
1396 u32 curr_id;
1397 int err;
1398
1399 curr_id = get_prog_id(dev);
1400 if (!curr_id)
1401 return 0;
1402
1403 *prog_id = curr_id;
1404 err = nla_put_u32(skb, attr, curr_id);
1405 if (err)
1406 return err;
1407
1408 if (*mode != XDP_ATTACHED_NONE)
1409 *mode = XDP_ATTACHED_MULTI;
1410 else
1411 *mode = tgt_mode;
1412
1413 return 0;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001414}
1415
Brenden Blancod1fdd912016-07-19 12:16:49 -07001416static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1417{
Brenden Blancod1fdd912016-07-19 12:16:49 -07001418 struct nlattr *xdp;
Martin KaFai Lau58038692017-06-15 17:29:09 -07001419 u32 prog_id;
Brenden Blancod1fdd912016-07-19 12:16:49 -07001420 int err;
Jakub Kicinski4f91da22018-07-11 20:36:38 -07001421 u8 mode;
Brenden Blancod1fdd912016-07-19 12:16:49 -07001422
Brenden Blancod1fdd912016-07-19 12:16:49 -07001423 xdp = nla_nest_start(skb, IFLA_XDP);
1424 if (!xdp)
1425 return -EMSGSIZE;
David S. Millerb5cdae32017-04-18 15:36:58 -04001426
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001427 prog_id = 0;
1428 mode = XDP_ATTACHED_NONE;
Jakub Kicinski202aabe2018-07-16 19:08:50 -07001429 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB,
1430 IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb);
1431 if (err)
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001432 goto err_cancel;
Jakub Kicinski202aabe2018-07-16 19:08:50 -07001433 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV,
1434 IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv);
1435 if (err)
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001436 goto err_cancel;
Jakub Kicinski202aabe2018-07-16 19:08:50 -07001437 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW,
1438 IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw);
1439 if (err)
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001440 goto err_cancel;
1441
Jakub Kicinski4f91da22018-07-11 20:36:38 -07001442 err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode);
Brenden Blancod1fdd912016-07-19 12:16:49 -07001443 if (err)
1444 goto err_cancel;
1445
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001446 if (prog_id && mode != XDP_ATTACHED_MULTI) {
Martin KaFai Lau58038692017-06-15 17:29:09 -07001447 err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
1448 if (err)
1449 goto err_cancel;
1450 }
1451
Brenden Blancod1fdd912016-07-19 12:16:49 -07001452 nla_nest_end(skb, xdp);
1453 return 0;
1454
1455err_cancel:
1456 nla_nest_cancel(skb, xdp);
1457 return err;
1458}
1459
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001460static u32 rtnl_get_event(unsigned long event)
1461{
1462 u32 rtnl_event_type = IFLA_EVENT_NONE;
1463
1464 switch (event) {
1465 case NETDEV_REBOOT:
1466 rtnl_event_type = IFLA_EVENT_REBOOT;
1467 break;
1468 case NETDEV_FEAT_CHANGE:
1469 rtnl_event_type = IFLA_EVENT_FEATURES;
1470 break;
1471 case NETDEV_BONDING_FAILOVER:
1472 rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
1473 break;
1474 case NETDEV_NOTIFY_PEERS:
1475 rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
1476 break;
1477 case NETDEV_RESEND_IGMP:
1478 rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
1479 break;
1480 case NETDEV_CHANGEINFODATA:
1481 rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
1482 break;
1483 default:
1484 break;
1485 }
1486
1487 return rtnl_event_type;
1488}
1489
Florian Westphal79110a02017-09-26 13:58:40 +02001490static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
1491{
1492 const struct net_device *upper_dev;
1493 int ret = 0;
1494
1495 rcu_read_lock();
1496
1497 upper_dev = netdev_master_upper_dev_get_rcu(dev);
1498 if (upper_dev)
1499 ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);
1500
1501 rcu_read_unlock();
1502 return ret;
1503}
1504
1505static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev)
1506{
1507 int ifindex = dev_get_iflink(dev);
1508
1509 if (dev->ifindex == ifindex)
1510 return 0;
1511
1512 return nla_put_u32(skb, IFLA_LINK, ifindex);
1513}
1514
Florian Westphal6c557002017-10-02 23:50:05 +02001515static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
1516 struct net_device *dev)
1517{
1518 char buf[IFALIASZ];
1519 int ret;
1520
1521 ret = dev_get_alias(dev, buf, sizeof(buf));
1522 return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
1523}
1524
Florian Westphalb1e66b92017-09-26 13:58:42 +02001525static int rtnl_fill_link_netnsid(struct sk_buff *skb,
Jiri Benc79e1ad12017-11-02 17:04:38 -02001526 const struct net_device *dev,
1527 struct net *src_net)
Florian Westphalb1e66b92017-09-26 13:58:42 +02001528{
1529 if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
1530 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1531
1532 if (!net_eq(dev_net(dev), link_net)) {
Jiri Benc79e1ad12017-11-02 17:04:38 -02001533 int id = peernet2id_alloc(src_net, link_net);
Florian Westphalb1e66b92017-09-26 13:58:42 +02001534
1535 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1536 return -EMSGSIZE;
1537 }
1538 }
1539
1540 return 0;
1541}
1542
Florian Westphal070cbf5b2017-10-16 15:44:35 +02001543static int rtnl_fill_link_af(struct sk_buff *skb,
1544 const struct net_device *dev,
1545 u32 ext_filter_mask)
1546{
1547 const struct rtnl_af_ops *af_ops;
1548 struct nlattr *af_spec;
1549
1550 af_spec = nla_nest_start(skb, IFLA_AF_SPEC);
1551 if (!af_spec)
1552 return -EMSGSIZE;
1553
Florian Westphal5fa85a02017-10-16 15:44:36 +02001554 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Florian Westphal070cbf5b2017-10-16 15:44:35 +02001555 struct nlattr *af;
1556 int err;
1557
1558 if (!af_ops->fill_link_af)
1559 continue;
1560
1561 af = nla_nest_start(skb, af_ops->family);
1562 if (!af)
1563 return -EMSGSIZE;
1564
1565 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1566 /*
1567 * Caller may return ENODATA to indicate that there
1568 * was no data to be dumped. This is not an error, it
1569 * means we should trim the attribute header and
1570 * continue.
1571 */
1572 if (err == -ENODATA)
1573 nla_nest_cancel(skb, af);
1574 else if (err < 0)
1575 return -EMSGSIZE;
1576
1577 nla_nest_end(skb, af);
1578 }
1579
1580 nla_nest_end(skb, af_spec);
1581 return 0;
1582}
1583
Jiri Benc79e1ad12017-11-02 17:04:38 -02001584static int rtnl_fill_ifinfo(struct sk_buff *skb,
1585 struct net_device *dev, struct net *src_net,
Patrick McHardy575c3e22007-05-22 17:00:49 -07001586 int type, u32 pid, u32 seq, u32 change,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001587 unsigned int flags, u32 ext_filter_mask,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01001588 u32 event, int *new_nsid, int new_ifindex,
1589 int tgt_netnsid)
Thomas Grafb60c5112006-08-04 23:05:34 -07001590{
1591 struct ifinfomsg *ifm;
1592 struct nlmsghdr *nlh;
1593
Eric Dumazet2907c352011-05-25 07:34:04 +00001594 ASSERT_RTNL();
Thomas Grafb60c5112006-08-04 23:05:34 -07001595 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1596 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08001597 return -EMSGSIZE;
Thomas Grafb60c5112006-08-04 23:05:34 -07001598
1599 ifm = nlmsg_data(nlh);
1600 ifm->ifi_family = AF_UNSPEC;
1601 ifm->__ifi_pad = 0;
1602 ifm->ifi_type = dev->type;
1603 ifm->ifi_index = dev->ifindex;
1604 ifm->ifi_flags = dev_get_flags(dev);
1605 ifm->ifi_change = change;
1606
Christian Brauner7e4a8d52018-09-04 21:53:53 +02001607 if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid))
Jiri Benc79e1ad12017-11-02 17:04:38 -02001608 goto nla_put_failure;
1609
David S. Millera6574342012-04-01 20:12:00 -04001610 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1611 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1612 nla_put_u8(skb, IFLA_OPERSTATE,
1613 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1614 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1615 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
Stephen Hemminger3e7a50c2018-07-27 13:43:22 -07001616 nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) ||
1617 nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) ||
David S. Millera6574342012-04-01 20:12:00 -04001618 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
Ben Greearedbc0bb2012-03-29 12:51:30 +00001619 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001620 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
Eric Dumazetc70ce022016-03-21 09:55:10 -07001621 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1622 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001623#ifdef CONFIG_RPS
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001624 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001625#endif
Florian Westphal79110a02017-09-26 13:58:40 +02001626 nla_put_iflink(skb, dev) ||
1627 put_master_ifindex(skb, dev) ||
Jiri Pirko9a572472012-12-27 23:49:39 +00001628 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
David S. Millera6574342012-04-01 20:12:00 -04001629 (dev->qdisc &&
1630 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
Florian Westphal6c557002017-10-02 23:50:05 +02001631 nla_put_ifalias(skb, dev) ||
david decotigny2d3b4792014-03-29 09:48:35 -07001632 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
David Decotignyb2d3bcf2018-01-18 09:59:13 -08001633 atomic_read(&dev->carrier_up_count) +
1634 atomic_read(&dev->carrier_down_count)) ||
1635 nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down) ||
1636 nla_put_u32(skb, IFLA_CARRIER_UP_COUNT,
1637 atomic_read(&dev->carrier_up_count)) ||
1638 nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT,
1639 atomic_read(&dev->carrier_down_count)))
David S. Millera6574342012-04-01 20:12:00 -04001640 goto nla_put_failure;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001641
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001642 if (event != IFLA_EVENT_NONE) {
1643 if (nla_put_u32(skb, IFLA_EVENT, event))
1644 goto nla_put_failure;
1645 }
1646
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001647 if (rtnl_fill_link_ifmap(skb, dev))
1648 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
1650 if (dev->addr_len) {
David S. Millera6574342012-04-01 20:12:00 -04001651 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1652 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1653 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 }
1655
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001656 if (rtnl_phys_port_id_fill(skb, dev))
1657 goto nla_put_failure;
1658
David Aherndb24a902015-03-17 20:23:15 -06001659 if (rtnl_phys_port_name_fill(skb, dev))
1660 goto nla_put_failure;
1661
Jiri Pirko82f28412014-11-28 14:34:18 +01001662 if (rtnl_phys_switch_id_fill(skb, dev))
1663 goto nla_put_failure;
1664
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001665 if (rtnl_fill_stats(skb, dev))
Pavel Emelyanov96e74082008-05-21 14:12:46 -07001666 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
Florian Westphal250fc3d2017-09-26 13:58:41 +02001668 if (rtnl_fill_vf(skb, dev, ext_filter_mask))
David S. Millera6574342012-04-01 20:12:00 -04001669 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001670
David Gibsonc53864f2014-04-24 10:22:36 +10001671 if (rtnl_port_fill(skb, dev, ext_filter_mask))
Scott Feldman57b61082010-05-17 22:49:55 -07001672 goto nla_put_failure;
1673
Brenden Blancod1fdd912016-07-19 12:16:49 -07001674 if (rtnl_xdp_fill(skb, dev))
1675 goto nla_put_failure;
1676
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001677 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07001678 if (rtnl_link_fill(skb, dev) < 0)
1679 goto nla_put_failure;
1680 }
1681
Jiri Benc79e1ad12017-11-02 17:04:38 -02001682 if (rtnl_fill_link_netnsid(skb, dev, src_net))
Florian Westphalb1e66b92017-09-26 13:58:42 +02001683 goto nla_put_failure;
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001684
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02001685 if (new_nsid &&
1686 nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0)
1687 goto nla_put_failure;
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01001688 if (new_ifindex &&
1689 nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0)
1690 goto nla_put_failure;
1691
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02001692
Florian Westphal5fa85a02017-10-16 15:44:36 +02001693 rcu_read_lock();
Florian Westphal070cbf5b2017-10-16 15:44:35 +02001694 if (rtnl_fill_link_af(skb, dev, ext_filter_mask))
Florian Westphal5fa85a02017-10-16 15:44:36 +02001695 goto nla_put_failure_rcu;
1696 rcu_read_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +00001697
Johannes Berg053c0952015-01-16 22:09:00 +01001698 nlmsg_end(skb, nlh);
1699 return 0;
Thomas Grafb60c5112006-08-04 23:05:34 -07001700
Florian Westphal5fa85a02017-10-16 15:44:36 +02001701nla_put_failure_rcu:
1702 rcu_read_unlock();
Thomas Grafb60c5112006-08-04 23:05:34 -07001703nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001704 nlmsg_cancel(skb, nlh);
1705 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706}
1707
Jiri Pirkof7b12602014-02-18 20:53:18 +01001708static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1709 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1710 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1711 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1712 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1713 [IFLA_MTU] = { .type = NLA_U32 },
1714 [IFLA_LINK] = { .type = NLA_U32 },
1715 [IFLA_MASTER] = { .type = NLA_U32 },
1716 [IFLA_CARRIER] = { .type = NLA_U8 },
1717 [IFLA_TXQLEN] = { .type = NLA_U32 },
1718 [IFLA_WEIGHT] = { .type = NLA_U32 },
1719 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1720 [IFLA_LINKMODE] = { .type = NLA_U8 },
1721 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1722 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1723 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
Nicolas Dichtel2459b4c2017-10-11 16:24:48 +02001724 /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
1725 * allow 0-length string (needed to remove an alias).
1726 */
1727 [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001728 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1729 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1730 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1731 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1732 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1733 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1734 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1735 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
Stephen Hemminger46e6b992017-12-07 15:40:19 -08001736 [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 },
1737 [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 },
Jiri Pirko02637fc2014-11-28 14:34:16 +01001738 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
david decotigny2d3b4792014-03-29 09:48:35 -07001739 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
Jiri Pirko82f28412014-11-28 14:34:18 +01001740 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
Nicolas Dichtel317f4812015-01-15 15:11:18 +01001741 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001742 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001743 [IFLA_XDP] = { .type = NLA_NESTED },
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001744 [IFLA_EVENT] = { .type = NLA_U32 },
Serhey Popovychdb833d42017-06-20 14:35:23 +03001745 [IFLA_GROUP] = { .type = NLA_U32 },
Christian Brauner7e4a8d52018-09-04 21:53:53 +02001746 [IFLA_TARGET_NETNSID] = { .type = NLA_S32 },
David Decotignyb2d3bcf2018-01-18 09:59:13 -08001747 [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 },
1748 [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 },
Stephen Hemminger3e7a50c2018-07-27 13:43:22 -07001749 [IFLA_MIN_MTU] = { .type = NLA_U32 },
1750 [IFLA_MAX_MTU] = { .type = NLA_U32 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001751};
1752
1753static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1754 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1755 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1756 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1757 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1758};
1759
Jiri Pirkof7b12602014-02-18 20:53:18 +01001760static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
Daniel Borkmann364d5712015-02-05 18:44:04 +01001761 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1762 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
Moshe Shemesh79aab092016-09-22 12:11:15 +03001763 [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED },
Daniel Borkmann364d5712015-02-05 18:44:04 +01001764 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1765 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1766 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1767 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001768 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001769 [IFLA_VF_STATS] = { .type = NLA_NESTED },
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00001770 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
Eli Cohencc8e27c2016-03-11 22:58:34 +02001771 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1772 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001773};
1774
Jiri Pirkof7b12602014-02-18 20:53:18 +01001775static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1776 [IFLA_PORT_VF] = { .type = NLA_U32 },
1777 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1778 .len = PORT_PROFILE_MAX },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001779 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1780 .len = PORT_UUID_MAX },
1781 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1782 .len = PORT_UUID_MAX },
1783 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1784 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
Daniel Borkmann025331d2017-02-17 01:56:11 +01001785
1786 /* Unused, but we need to keep it here since user space could
1787 * fill it. It's also broken with regard to NLA_BINARY use in
1788 * combination with structs.
1789 */
1790 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1791 .len = sizeof(struct ifla_port_vsi) },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001792};
1793
Brenden Blancod1fdd912016-07-19 12:16:49 -07001794static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
1795 [IFLA_XDP_FD] = { .type = NLA_S32 },
1796 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 },
Daniel Borkmann85de8572016-11-28 23:16:54 +01001797 [IFLA_XDP_FLAGS] = { .type = NLA_U32 },
Martin KaFai Lau58038692017-06-15 17:29:09 -07001798 [IFLA_XDP_PROG_ID] = { .type = NLA_U32 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001799};
1800
David Aherndc599f72016-02-02 08:17:07 -08001801static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1802{
1803 const struct rtnl_link_ops *ops = NULL;
1804 struct nlattr *linfo[IFLA_INFO_MAX + 1];
1805
Johannes Bergfceb6432017-04-12 14:34:07 +02001806 if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla,
1807 ifla_info_policy, NULL) < 0)
David Aherndc599f72016-02-02 08:17:07 -08001808 return NULL;
1809
1810 if (linfo[IFLA_INFO_KIND]) {
1811 char kind[MODULE_NAME_LEN];
1812
1813 nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1814 ops = rtnl_link_ops_get(kind);
1815 }
1816
1817 return ops;
1818}
1819
1820static bool link_master_filtered(struct net_device *dev, int master_idx)
1821{
1822 struct net_device *master;
1823
1824 if (!master_idx)
1825 return false;
1826
1827 master = netdev_master_upper_dev_get(dev);
1828 if (!master || master->ifindex != master_idx)
1829 return true;
1830
1831 return false;
1832}
1833
1834static bool link_kind_filtered(const struct net_device *dev,
1835 const struct rtnl_link_ops *kind_ops)
1836{
1837 if (kind_ops && dev->rtnl_link_ops != kind_ops)
1838 return true;
1839
1840 return false;
1841}
1842
1843static bool link_dump_filtered(struct net_device *dev,
1844 int master_idx,
1845 const struct rtnl_link_ops *kind_ops)
1846{
1847 if (link_master_filtered(dev, master_idx) ||
1848 link_kind_filtered(dev, kind_ops))
1849 return true;
1850
1851 return false;
1852}
1853
Christian Braunerc383edc2018-09-04 21:53:47 +02001854/**
1855 * rtnl_get_net_ns_capable - Get netns if sufficiently privileged.
1856 * @sk: netlink socket
1857 * @netnsid: network namespace identifier
1858 *
1859 * Returns the network namespace identified by netnsid on success or an error
1860 * pointer on failure.
1861 */
1862struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid)
Jiri Benc79e1ad12017-11-02 17:04:38 -02001863{
1864 struct net *net;
1865
Andrei Vaginf428fe42018-01-02 23:27:33 -08001866 net = get_net_ns_by_id(sock_net(sk), netnsid);
Jiri Benc79e1ad12017-11-02 17:04:38 -02001867 if (!net)
1868 return ERR_PTR(-EINVAL);
1869
1870 /* For now, the caller is required to have CAP_NET_ADMIN in
1871 * the user namespace owning the target net ns.
1872 */
Andrei Vaginf428fe42018-01-02 23:27:33 -08001873 if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) {
Jiri Benc79e1ad12017-11-02 17:04:38 -02001874 put_net(net);
1875 return ERR_PTR(-EACCES);
1876 }
1877 return net;
1878}
Christian Braunerc383edc2018-09-04 21:53:47 +02001879EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable);
Jiri Benc79e1ad12017-11-02 17:04:38 -02001880
David Ahern905cf0a2018-10-07 20:16:30 -07001881static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh,
1882 bool strict_check, struct nlattr **tb,
1883 struct netlink_ext_ack *extack)
1884{
1885 int hdrlen;
1886
1887 if (strict_check) {
1888 struct ifinfomsg *ifm;
1889
1890 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
1891 NL_SET_ERR_MSG(extack, "Invalid header for link dump");
1892 return -EINVAL;
1893 }
1894
1895 ifm = nlmsg_data(nlh);
1896 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
1897 ifm->ifi_change) {
1898 NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request");
1899 return -EINVAL;
1900 }
1901 if (ifm->ifi_index) {
1902 NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps");
1903 return -EINVAL;
1904 }
1905
1906 return nlmsg_parse_strict(nlh, sizeof(*ifm), tb, IFLA_MAX,
1907 ifla_policy, extack);
1908 }
1909
1910 /* A hack to preserve kernel<->userspace interface.
1911 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1912 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1913 * what iproute2 < v3.9.0 used.
1914 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1915 * attribute, its netlink message is shorter than struct ifinfomsg.
1916 */
1917 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
1918 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1919
1920 return nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, extack);
1921}
1922
Thomas Grafb60c5112006-08-04 23:05:34 -07001923static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
David Ahern905cf0a2018-10-07 20:16:30 -07001925 struct netlink_ext_ack *extack = cb->extack;
1926 const struct nlmsghdr *nlh = cb->nlh;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001927 struct net *net = sock_net(skb->sk);
Jiri Benc79e1ad12017-11-02 17:04:38 -02001928 struct net *tgt_net = net;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001929 int h, s_h;
1930 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 struct net_device *dev;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001932 struct hlist_head *head;
Greg Rose115c9b82012-02-21 16:54:48 -05001933 struct nlattr *tb[IFLA_MAX+1];
1934 u32 ext_filter_mask = 0;
David Aherndc599f72016-02-02 08:17:07 -08001935 const struct rtnl_link_ops *kind_ops = NULL;
1936 unsigned int flags = NLM_F_MULTI;
1937 int master_idx = 0;
Jiri Benc79e1ad12017-11-02 17:04:38 -02001938 int netnsid = -1;
David Ahern905cf0a2018-10-07 20:16:30 -07001939 int err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001941 s_h = cb->args[0];
1942 s_idx = cb->args[1];
1943
David Ahern905cf0a2018-10-07 20:16:30 -07001944 err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack);
1945 if (err < 0) {
1946 if (cb->strict_check)
1947 return err;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001948
David Ahern905cf0a2018-10-07 20:16:30 -07001949 goto walk_entries;
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001950 }
Greg Rose115c9b82012-02-21 16:54:48 -05001951
David Ahern905cf0a2018-10-07 20:16:30 -07001952 for (i = 0; i <= IFLA_MAX; ++i) {
1953 if (!tb[i])
1954 continue;
1955
1956 /* new attributes should only be added with strict checking */
1957 switch (i) {
1958 case IFLA_TARGET_NETNSID:
1959 netnsid = nla_get_s32(tb[i]);
1960 tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid);
1961 if (IS_ERR(tgt_net)) {
1962 NL_SET_ERR_MSG(extack, "Invalid target network namespace id");
1963 return PTR_ERR(tgt_net);
1964 }
1965 break;
1966 case IFLA_EXT_MASK:
1967 ext_filter_mask = nla_get_u32(tb[i]);
1968 break;
1969 case IFLA_MASTER:
1970 master_idx = nla_get_u32(tb[i]);
1971 break;
1972 case IFLA_LINKINFO:
1973 kind_ops = linkinfo_to_kind_ops(tb[i]);
1974 break;
1975 default:
1976 if (cb->strict_check) {
1977 NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request");
1978 return -EINVAL;
1979 }
1980 }
1981 }
1982
1983 if (master_idx || kind_ops)
1984 flags |= NLM_F_DUMP_FILTERED;
1985
1986walk_entries:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001987 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1988 idx = 0;
Jiri Benc79e1ad12017-11-02 17:04:38 -02001989 head = &tgt_net->dev_index_head[h];
Eric Dumazetcac5e652015-02-27 09:42:50 -08001990 hlist_for_each_entry(dev, head, index_hlist) {
David Aherndc599f72016-02-02 08:17:07 -08001991 if (link_dump_filtered(dev, master_idx, kind_ops))
Zhang Shengju3f0ae052016-11-19 23:28:32 +08001992 goto cont;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001993 if (idx < s_idx)
1994 goto cont;
Jiri Benc79e1ad12017-11-02 17:04:38 -02001995 err = rtnl_fill_ifinfo(skb, dev, net,
1996 RTM_NEWLINK,
David Gibson973462b2014-04-24 10:22:35 +10001997 NETLINK_CB(cb->skb).portid,
David Ahern905cf0a2018-10-07 20:16:30 -07001998 nlh->nlmsg_seq, 0, flags,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01001999 ext_filter_mask, 0, NULL, 0,
Jiri Benc79e1ad12017-11-02 17:04:38 -02002000 netnsid);
David Gibson973462b2014-04-24 10:22:35 +10002001
David Ahernf6c57752017-05-15 23:19:17 -07002002 if (err < 0) {
2003 if (likely(skb->len))
2004 goto out;
2005
2006 goto out_err;
2007 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07002008cont:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002009 idx++;
2010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 }
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002012out:
David Ahernf6c57752017-05-15 23:19:17 -07002013 err = skb->len;
2014out_err:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002015 cb->args[1] = idx;
2016 cb->args[0] = h;
Jakub Sitnickid0225782017-08-09 17:39:12 +02002017 cb->seq = net->dev_base_seq;
2018 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Jiri Benc79e1ad12017-11-02 17:04:38 -02002019 if (netnsid >= 0)
2020 put_net(tgt_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
David Ahernf6c57752017-05-15 23:19:17 -07002022 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023}
2024
Johannes Bergfceb6432017-04-12 14:34:07 +02002025int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
2026 struct netlink_ext_ack *exterr)
Jiri Pirkof7b12602014-02-18 20:53:18 +01002027{
Johannes Bergfceb6432017-04-12 14:34:07 +02002028 return nla_parse(tb, IFLA_MAX, head, len, ifla_policy, exterr);
Jiri Pirkof7b12602014-02-18 20:53:18 +01002029}
2030EXPORT_SYMBOL(rtnl_nla_parse_ifla);
Scott Feldman57b61082010-05-17 22:49:55 -07002031
Eric W. Biederman81adee42009-11-08 00:53:51 -08002032struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
2033{
2034 struct net *net;
2035 /* Examine the link attributes and figure out which
2036 * network namespace we are talking about.
2037 */
2038 if (tb[IFLA_NET_NS_PID])
2039 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
Eric W. Biedermanf0630522011-05-04 17:51:50 -07002040 else if (tb[IFLA_NET_NS_FD])
2041 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
Eric W. Biederman81adee42009-11-08 00:53:51 -08002042 else
2043 net = get_net(src_net);
2044 return net;
2045}
2046EXPORT_SYMBOL(rtnl_link_get_net);
2047
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002048/* Figure out which network namespace we are talking about by
2049 * examining the link attributes in the following order:
2050 *
2051 * 1. IFLA_NET_NS_PID
2052 * 2. IFLA_NET_NS_FD
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002053 * 3. IFLA_TARGET_NETNSID
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002054 */
2055static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net,
2056 struct nlattr *tb[])
2057{
2058 struct net *net;
2059
2060 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])
2061 return rtnl_link_get_net(src_net, tb);
2062
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002063 if (!tb[IFLA_TARGET_NETNSID])
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002064 return get_net(src_net);
2065
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002066 net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID]));
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002067 if (!net)
2068 return ERR_PTR(-EINVAL);
2069
2070 return net;
2071}
2072
2073static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb,
2074 struct net *src_net,
2075 struct nlattr *tb[], int cap)
2076{
2077 struct net *net;
2078
2079 net = rtnl_link_get_net_by_nlattr(src_net, tb);
2080 if (IS_ERR(net))
2081 return net;
2082
2083 if (!netlink_ns_capable(skb, net->user_ns, cap)) {
2084 put_net(net);
2085 return ERR_PTR(-EPERM);
2086 }
2087
2088 return net;
2089}
2090
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002091/* Verify that rtnetlink requests do not pass additional properties
2092 * potentially referring to different network namespaces.
2093 */
2094static int rtnl_ensure_unique_netns(struct nlattr *tb[],
2095 struct netlink_ext_ack *extack,
2096 bool netns_id_only)
2097{
2098
2099 if (netns_id_only) {
2100 if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD])
2101 return 0;
2102
2103 NL_SET_ERR_MSG(extack, "specified netns attribute not supported");
2104 return -EOPNOTSUPP;
2105 }
2106
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002107 if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]))
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002108 goto invalid_attr;
2109
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002110 if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD]))
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002111 goto invalid_attr;
2112
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002113 if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID]))
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002114 goto invalid_attr;
2115
2116 return 0;
2117
2118invalid_attr:
2119 NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified");
2120 return -EINVAL;
2121}
2122
Thomas Graf1840bb12008-02-23 19:54:36 -08002123static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
2124{
2125 if (dev) {
2126 if (tb[IFLA_ADDRESS] &&
2127 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
2128 return -EINVAL;
2129
2130 if (tb[IFLA_BROADCAST] &&
2131 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
2132 return -EINVAL;
2133 }
2134
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002135 if (tb[IFLA_AF_SPEC]) {
2136 struct nlattr *af;
2137 int rem, err;
2138
2139 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2140 const struct rtnl_af_ops *af_ops;
2141
Florian Westphal5fa85a02017-10-16 15:44:36 +02002142 rcu_read_lock();
2143 af_ops = rtnl_af_lookup(nla_type(af));
2144 if (!af_ops) {
2145 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002146 return -EAFNOSUPPORT;
Florian Westphal5fa85a02017-10-16 15:44:36 +02002147 }
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002148
Florian Westphal5fa85a02017-10-16 15:44:36 +02002149 if (!af_ops->set_link_af) {
2150 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002151 return -EOPNOTSUPP;
Florian Westphal5fa85a02017-10-16 15:44:36 +02002152 }
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002153
2154 if (af_ops->validate_link_af) {
Kurt Van Dijck6d3a9a62011-01-26 04:55:24 +00002155 err = af_ops->validate_link_af(dev, af);
Florian Westphal5fa85a02017-10-16 15:44:36 +02002156 if (err < 0) {
2157 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002158 return err;
Florian Westphal5fa85a02017-10-16 15:44:36 +02002159 }
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002160 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02002161
2162 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002163 }
2164 }
2165
Thomas Graf1840bb12008-02-23 19:54:36 -08002166 return 0;
2167}
2168
Eli Cohencc8e27c2016-03-11 22:58:34 +02002169static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
2170 int guid_type)
2171{
2172 const struct net_device_ops *ops = dev->netdev_ops;
2173
2174 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
2175}
2176
2177static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
2178{
2179 if (dev->type != ARPHRD_INFINIBAND)
2180 return -EOPNOTSUPP;
2181
2182 return handle_infiniband_guid(dev, ivt, guid_type);
2183}
2184
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002185static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
Chris Wrightc02db8c2010-05-16 01:05:45 -07002186{
Chris Wrightc02db8c2010-05-16 01:05:45 -07002187 const struct net_device_ops *ops = dev->netdev_ops;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002188 int err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002189
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002190 if (tb[IFLA_VF_MAC]) {
2191 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03002192
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002193 err = -EOPNOTSUPP;
2194 if (ops->ndo_set_vf_mac)
2195 err = ops->ndo_set_vf_mac(dev, ivm->vf,
2196 ivm->mac);
2197 if (err < 0)
2198 return err;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002199 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002200
2201 if (tb[IFLA_VF_VLAN]) {
2202 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
2203
2204 err = -EOPNOTSUPP;
2205 if (ops->ndo_set_vf_vlan)
2206 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
Moshe Shemesh79aab092016-09-22 12:11:15 +03002207 ivv->qos,
2208 htons(ETH_P_8021Q));
2209 if (err < 0)
2210 return err;
2211 }
2212
2213 if (tb[IFLA_VF_VLAN_LIST]) {
2214 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
2215 struct nlattr *attr;
2216 int rem, len = 0;
2217
2218 err = -EOPNOTSUPP;
2219 if (!ops->ndo_set_vf_vlan)
2220 return err;
2221
2222 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
2223 if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
2224 nla_len(attr) < NLA_HDRLEN) {
2225 return -EINVAL;
2226 }
2227 if (len >= MAX_VLAN_LIST_LEN)
2228 return -EOPNOTSUPP;
2229 ivvl[len] = nla_data(attr);
2230
2231 len++;
2232 }
Arnd Bergmannfa34cd92016-09-30 18:13:49 +02002233 if (len == 0)
2234 return -EINVAL;
2235
Moshe Shemesh79aab092016-09-22 12:11:15 +03002236 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
2237 ivvl[0]->qos, ivvl[0]->vlan_proto);
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002238 if (err < 0)
2239 return err;
2240 }
2241
2242 if (tb[IFLA_VF_TX_RATE]) {
2243 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
2244 struct ifla_vf_info ivf;
2245
2246 err = -EOPNOTSUPP;
2247 if (ops->ndo_get_vf_config)
2248 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
2249 if (err < 0)
2250 return err;
2251
2252 err = -EOPNOTSUPP;
2253 if (ops->ndo_set_vf_rate)
2254 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2255 ivf.min_tx_rate,
2256 ivt->rate);
2257 if (err < 0)
2258 return err;
2259 }
2260
2261 if (tb[IFLA_VF_RATE]) {
2262 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
2263
2264 err = -EOPNOTSUPP;
2265 if (ops->ndo_set_vf_rate)
2266 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2267 ivt->min_tx_rate,
2268 ivt->max_tx_rate);
2269 if (err < 0)
2270 return err;
2271 }
2272
2273 if (tb[IFLA_VF_SPOOFCHK]) {
2274 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
2275
2276 err = -EOPNOTSUPP;
2277 if (ops->ndo_set_vf_spoofchk)
2278 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
2279 ivs->setting);
2280 if (err < 0)
2281 return err;
2282 }
2283
2284 if (tb[IFLA_VF_LINK_STATE]) {
2285 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
2286
2287 err = -EOPNOTSUPP;
2288 if (ops->ndo_set_vf_link_state)
2289 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
2290 ivl->link_state);
2291 if (err < 0)
2292 return err;
2293 }
2294
2295 if (tb[IFLA_VF_RSS_QUERY_EN]) {
2296 struct ifla_vf_rss_query_en *ivrssq_en;
2297
2298 err = -EOPNOTSUPP;
2299 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
2300 if (ops->ndo_set_vf_rss_query_en)
2301 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
2302 ivrssq_en->setting);
2303 if (err < 0)
2304 return err;
2305 }
2306
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00002307 if (tb[IFLA_VF_TRUST]) {
2308 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
2309
2310 err = -EOPNOTSUPP;
2311 if (ops->ndo_set_vf_trust)
2312 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
2313 if (err < 0)
2314 return err;
2315 }
2316
Eli Cohencc8e27c2016-03-11 22:58:34 +02002317 if (tb[IFLA_VF_IB_NODE_GUID]) {
2318 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
2319
2320 if (!ops->ndo_set_vf_guid)
2321 return -EOPNOTSUPP;
2322
2323 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
2324 }
2325
2326 if (tb[IFLA_VF_IB_PORT_GUID]) {
2327 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
2328
2329 if (!ops->ndo_set_vf_guid)
2330 return -EOPNOTSUPP;
2331
2332 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
2333 }
2334
Chris Wrightc02db8c2010-05-16 01:05:45 -07002335 return err;
2336}
2337
David Ahern33eaf2a2017-10-04 17:48:46 -07002338static int do_set_master(struct net_device *dev, int ifindex,
2339 struct netlink_ext_ack *extack)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002340{
Jiri Pirko898e5062013-01-03 22:48:52 +00002341 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002342 const struct net_device_ops *ops;
2343 int err;
2344
Jiri Pirko898e5062013-01-03 22:48:52 +00002345 if (upper_dev) {
2346 if (upper_dev->ifindex == ifindex)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002347 return 0;
Jiri Pirko898e5062013-01-03 22:48:52 +00002348 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002349 if (ops->ndo_del_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002350 err = ops->ndo_del_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002351 if (err)
2352 return err;
2353 } else {
2354 return -EOPNOTSUPP;
2355 }
2356 }
2357
2358 if (ifindex) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002359 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
2360 if (!upper_dev)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002361 return -EINVAL;
Jiri Pirko898e5062013-01-03 22:48:52 +00002362 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002363 if (ops->ndo_add_slave) {
David Ahern33eaf2a2017-10-04 17:48:46 -07002364 err = ops->ndo_add_slave(upper_dev, dev, extack);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002365 if (err)
2366 return err;
2367 } else {
2368 return -EOPNOTSUPP;
2369 }
2370 }
2371 return 0;
2372}
2373
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002374#define DO_SETLINK_MODIFIED 0x01
Nicolas Dichtelba998902014-09-01 16:07:29 +02002375/* notify flag means notify + modified. */
2376#define DO_SETLINK_NOTIFY 0x03
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002377static int do_setlink(const struct sk_buff *skb,
2378 struct net_device *dev, struct ifinfomsg *ifm,
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002379 struct netlink_ext_ack *extack,
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002380 struct nlattr **tb, char *ifname, int status)
Patrick McHardy0157f602007-06-13 12:03:36 -07002381{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002382 const struct net_device_ops *ops = dev->netdev_ops;
Patrick McHardy0157f602007-06-13 12:03:36 -07002383 int err;
2384
Eric Dumazet644c7ee2018-06-05 09:25:19 -07002385 err = validate_linkmsg(dev, tb);
2386 if (err < 0)
2387 return err;
2388
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002389 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) {
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002390 struct net *net = rtnl_link_get_net_capable(skb, dev_net(dev),
2391 tb, CAP_NET_ADMIN);
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002392 if (IS_ERR(net)) {
2393 err = PTR_ERR(net);
2394 goto errout;
2395 }
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002396
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002397 err = dev_change_net_namespace(dev, net, ifname);
2398 put_net(net);
2399 if (err)
2400 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002401 status |= DO_SETLINK_MODIFIED;
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002402 }
2403
Patrick McHardy0157f602007-06-13 12:03:36 -07002404 if (tb[IFLA_MAP]) {
2405 struct rtnl_link_ifmap *u_map;
2406 struct ifmap k_map;
2407
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002408 if (!ops->ndo_set_config) {
Patrick McHardy0157f602007-06-13 12:03:36 -07002409 err = -EOPNOTSUPP;
2410 goto errout;
2411 }
2412
2413 if (!netif_device_present(dev)) {
2414 err = -ENODEV;
2415 goto errout;
2416 }
2417
2418 u_map = nla_data(tb[IFLA_MAP]);
2419 k_map.mem_start = (unsigned long) u_map->mem_start;
2420 k_map.mem_end = (unsigned long) u_map->mem_end;
2421 k_map.base_addr = (unsigned short) u_map->base_addr;
2422 k_map.irq = (unsigned char) u_map->irq;
2423 k_map.dma = (unsigned char) u_map->dma;
2424 k_map.port = (unsigned char) u_map->port;
2425
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002426 err = ops->ndo_set_config(dev, &k_map);
Patrick McHardy0157f602007-06-13 12:03:36 -07002427 if (err < 0)
2428 goto errout;
2429
Nicolas Dichtelba998902014-09-01 16:07:29 +02002430 status |= DO_SETLINK_NOTIFY;
Patrick McHardy0157f602007-06-13 12:03:36 -07002431 }
2432
2433 if (tb[IFLA_ADDRESS]) {
2434 struct sockaddr *sa;
2435 int len;
2436
WANG Cong153711f2017-07-20 11:27:57 -07002437 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2438 sizeof(*sa));
Patrick McHardy0157f602007-06-13 12:03:36 -07002439 sa = kmalloc(len, GFP_KERNEL);
2440 if (!sa) {
2441 err = -ENOMEM;
2442 goto errout;
2443 }
2444 sa->sa_family = dev->type;
2445 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
2446 dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00002447 err = dev_set_mac_address(dev, sa);
Patrick McHardy0157f602007-06-13 12:03:36 -07002448 kfree(sa);
2449 if (err)
2450 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002451 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002452 }
2453
2454 if (tb[IFLA_MTU]) {
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07002455 err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack);
Patrick McHardy0157f602007-06-13 12:03:36 -07002456 if (err < 0)
2457 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002458 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002459 }
2460
Vlad Dogarucbda10f2011-01-13 23:38:30 +00002461 if (tb[IFLA_GROUP]) {
2462 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Nicolas Dichtelba998902014-09-01 16:07:29 +02002463 status |= DO_SETLINK_NOTIFY;
Vlad Dogarucbda10f2011-01-13 23:38:30 +00002464 }
2465
Patrick McHardy0157f602007-06-13 12:03:36 -07002466 /*
2467 * Interface selected by interface index but interface
2468 * name provided implies that a name change has been
2469 * requested.
2470 */
2471 if (ifm->ifi_index > 0 && ifname[0]) {
2472 err = dev_change_name(dev, ifname);
2473 if (err < 0)
2474 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002475 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002476 }
2477
Stephen Hemminger0b815a12008-09-22 21:28:11 -07002478 if (tb[IFLA_IFALIAS]) {
2479 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2480 nla_len(tb[IFLA_IFALIAS]));
2481 if (err < 0)
2482 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002483 status |= DO_SETLINK_NOTIFY;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07002484 }
2485
Patrick McHardy0157f602007-06-13 12:03:36 -07002486 if (tb[IFLA_BROADCAST]) {
2487 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00002488 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Patrick McHardy0157f602007-06-13 12:03:36 -07002489 }
2490
2491 if (ifm->ifi_flags || ifm->ifi_change) {
Petr Machata567c5e12018-12-06 17:05:42 +00002492 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
2493 extack);
Johannes Berg5f9021c2008-11-16 23:20:31 -08002494 if (err < 0)
2495 goto errout;
Patrick McHardy0157f602007-06-13 12:03:36 -07002496 }
2497
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002498 if (tb[IFLA_MASTER]) {
David Ahern33eaf2a2017-10-04 17:48:46 -07002499 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002500 if (err)
2501 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002502 status |= DO_SETLINK_MODIFIED;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002503 }
2504
Jiri Pirko9a572472012-12-27 23:49:39 +00002505 if (tb[IFLA_CARRIER]) {
2506 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2507 if (err)
2508 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002509 status |= DO_SETLINK_MODIFIED;
Jiri Pirko9a572472012-12-27 23:49:39 +00002510 }
2511
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002512 if (tb[IFLA_TXQLEN]) {
Alexey Dobriyan0cd29502017-05-17 13:30:44 +03002513 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002514
Cong Wang6a643dd2018-01-25 18:26:22 -08002515 err = dev_change_tx_queue_len(dev, value);
2516 if (err)
2517 goto errout;
2518 status |= DO_SETLINK_MODIFIED;
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002519 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002520
Stephen Hemminger46e6b992017-12-07 15:40:19 -08002521 if (tb[IFLA_GSO_MAX_SIZE]) {
2522 u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]);
2523
2524 if (max_size > GSO_MAX_SIZE) {
2525 err = -EINVAL;
2526 goto errout;
2527 }
2528
2529 if (dev->gso_max_size ^ max_size) {
2530 netif_set_gso_max_size(dev, max_size);
2531 status |= DO_SETLINK_MODIFIED;
2532 }
2533 }
2534
2535 if (tb[IFLA_GSO_MAX_SEGS]) {
2536 u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
2537
2538 if (max_segs > GSO_MAX_SEGS) {
2539 err = -EINVAL;
2540 goto errout;
2541 }
2542
2543 if (dev->gso_max_segs ^ max_segs) {
2544 dev->gso_max_segs = max_segs;
2545 status |= DO_SETLINK_MODIFIED;
2546 }
2547 }
2548
Patrick McHardy0157f602007-06-13 12:03:36 -07002549 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002550 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Patrick McHardy0157f602007-06-13 12:03:36 -07002551
2552 if (tb[IFLA_LINKMODE]) {
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002553 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2554
David S. Miller93b2d4a2008-02-17 18:35:07 -08002555 write_lock_bh(&dev_base_lock);
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002556 if (dev->link_mode ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02002557 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002558 dev->link_mode = value;
David S. Miller93b2d4a2008-02-17 18:35:07 -08002559 write_unlock_bh(&dev_base_lock);
Patrick McHardy0157f602007-06-13 12:03:36 -07002560 }
2561
Chris Wrightc02db8c2010-05-16 01:05:45 -07002562 if (tb[IFLA_VFINFO_LIST]) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002563 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
Chris Wrightc02db8c2010-05-16 01:05:45 -07002564 struct nlattr *attr;
2565 int rem;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002566
Chris Wrightc02db8c2010-05-16 01:05:45 -07002567 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002568 if (nla_type(attr) != IFLA_VF_INFO ||
2569 nla_len(attr) < NLA_HDRLEN) {
David Howells253683b2010-05-21 02:25:27 +00002570 err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002571 goto errout;
David Howells253683b2010-05-21 02:25:27 +00002572 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002573 err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr,
Johannes Bergfceb6432017-04-12 14:34:07 +02002574 ifla_vf_policy, NULL);
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002575 if (err < 0)
2576 goto errout;
2577 err = do_setvfinfo(dev, vfinfo);
Chris Wrightc02db8c2010-05-16 01:05:45 -07002578 if (err < 0)
2579 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002580 status |= DO_SETLINK_NOTIFY;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002581 }
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00002582 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002583 err = 0;
2584
Scott Feldman57b61082010-05-17 22:49:55 -07002585 if (tb[IFLA_VF_PORTS]) {
2586 struct nlattr *port[IFLA_PORT_MAX+1];
2587 struct nlattr *attr;
2588 int vf;
2589 int rem;
2590
2591 err = -EOPNOTSUPP;
2592 if (!ops->ndo_set_vf_port)
2593 goto errout;
2594
2595 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
Daniel Borkmann035d2102015-07-13 00:06:02 +02002596 if (nla_type(attr) != IFLA_VF_PORT ||
2597 nla_len(attr) < NLA_HDRLEN) {
2598 err = -EINVAL;
2599 goto errout;
2600 }
2601 err = nla_parse_nested(port, IFLA_PORT_MAX, attr,
Johannes Bergfceb6432017-04-12 14:34:07 +02002602 ifla_port_policy, NULL);
Scott Feldman57b61082010-05-17 22:49:55 -07002603 if (err < 0)
2604 goto errout;
2605 if (!port[IFLA_PORT_VF]) {
2606 err = -EOPNOTSUPP;
2607 goto errout;
2608 }
2609 vf = nla_get_u32(port[IFLA_PORT_VF]);
2610 err = ops->ndo_set_vf_port(dev, vf, port);
2611 if (err < 0)
2612 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002613 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07002614 }
2615 }
2616 err = 0;
2617
2618 if (tb[IFLA_PORT_SELF]) {
2619 struct nlattr *port[IFLA_PORT_MAX+1];
2620
2621 err = nla_parse_nested(port, IFLA_PORT_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +02002622 tb[IFLA_PORT_SELF], ifla_port_policy,
2623 NULL);
Scott Feldman57b61082010-05-17 22:49:55 -07002624 if (err < 0)
2625 goto errout;
2626
2627 err = -EOPNOTSUPP;
2628 if (ops->ndo_set_vf_port)
2629 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
2630 if (err < 0)
2631 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002632 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07002633 }
Thomas Graff8ff1822010-11-16 04:30:14 +00002634
2635 if (tb[IFLA_AF_SPEC]) {
2636 struct nlattr *af;
2637 int rem;
2638
2639 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2640 const struct rtnl_af_ops *af_ops;
2641
Florian Westphal5fa85a02017-10-16 15:44:36 +02002642 rcu_read_lock();
2643
Gustavo A. R. Silva058c8d52017-10-20 19:43:11 -05002644 BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
Thomas Graff8ff1822010-11-16 04:30:14 +00002645
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002646 err = af_ops->set_link_af(dev, af);
Florian Westphal5fa85a02017-10-16 15:44:36 +02002647 if (err < 0) {
2648 rcu_read_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +00002649 goto errout;
Florian Westphal5fa85a02017-10-16 15:44:36 +02002650 }
Thomas Graff8ff1822010-11-16 04:30:14 +00002651
Florian Westphal5fa85a02017-10-16 15:44:36 +02002652 rcu_read_unlock();
Nicolas Dichtelba998902014-09-01 16:07:29 +02002653 status |= DO_SETLINK_NOTIFY;
Thomas Graff8ff1822010-11-16 04:30:14 +00002654 }
2655 }
Scott Feldman57b61082010-05-17 22:49:55 -07002656 err = 0;
2657
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07002658 if (tb[IFLA_PROTO_DOWN]) {
2659 err = dev_change_proto_down(dev,
2660 nla_get_u8(tb[IFLA_PROTO_DOWN]));
2661 if (err)
2662 goto errout;
2663 status |= DO_SETLINK_NOTIFY;
2664 }
2665
Brenden Blancod1fdd912016-07-19 12:16:49 -07002666 if (tb[IFLA_XDP]) {
2667 struct nlattr *xdp[IFLA_XDP_MAX + 1];
Daniel Borkmann85de8572016-11-28 23:16:54 +01002668 u32 xdp_flags = 0;
Brenden Blancod1fdd912016-07-19 12:16:49 -07002669
2670 err = nla_parse_nested(xdp, IFLA_XDP_MAX, tb[IFLA_XDP],
Johannes Bergfceb6432017-04-12 14:34:07 +02002671 ifla_xdp_policy, NULL);
Brenden Blancod1fdd912016-07-19 12:16:49 -07002672 if (err < 0)
2673 goto errout;
2674
Martin KaFai Lau58038692017-06-15 17:29:09 -07002675 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
Brenden Blanco262d8622016-07-20 17:22:34 -07002676 err = -EINVAL;
2677 goto errout;
2678 }
Daniel Borkmann85de8572016-11-28 23:16:54 +01002679
2680 if (xdp[IFLA_XDP_FLAGS]) {
2681 xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
2682 if (xdp_flags & ~XDP_FLAGS_MASK) {
2683 err = -EINVAL;
2684 goto errout;
2685 }
Jakub Kicinskiee5d0322017-06-21 18:25:04 -07002686 if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
Daniel Borkmann0489df92017-05-12 01:04:45 +02002687 err = -EINVAL;
2688 goto errout;
2689 }
Daniel Borkmann85de8572016-11-28 23:16:54 +01002690 }
2691
Brenden Blancod1fdd912016-07-19 12:16:49 -07002692 if (xdp[IFLA_XDP_FD]) {
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002693 err = dev_change_xdp_fd(dev, extack,
Daniel Borkmann85de8572016-11-28 23:16:54 +01002694 nla_get_s32(xdp[IFLA_XDP_FD]),
2695 xdp_flags);
Brenden Blancod1fdd912016-07-19 12:16:49 -07002696 if (err)
2697 goto errout;
2698 status |= DO_SETLINK_NOTIFY;
2699 }
2700 }
2701
Patrick McHardy0157f602007-06-13 12:03:36 -07002702errout:
Nicolas Dichtelba998902014-09-01 16:07:29 +02002703 if (status & DO_SETLINK_MODIFIED) {
Xin Long64ff90c2017-10-15 18:13:45 +08002704 if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
Nicolas Dichtelba998902014-09-01 16:07:29 +02002705 netdev_state_change(dev);
2706
2707 if (err < 0)
2708 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",
2709 dev->name);
2710 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002711
Patrick McHardy0157f602007-06-13 12:03:36 -07002712 return err;
2713}
2714
David Ahernc21ef3e2017-04-16 09:48:24 -07002715static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2716 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002718 struct net *net = sock_net(skb->sk);
Thomas Grafda5e0492006-08-10 21:17:37 -07002719 struct ifinfomsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 struct net_device *dev;
Patrick McHardy0157f602007-06-13 12:03:36 -07002721 int err;
Thomas Grafda5e0492006-08-10 21:17:37 -07002722 struct nlattr *tb[IFLA_MAX+1];
2723 char ifname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724
David Ahernc21ef3e2017-04-16 09:48:24 -07002725 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy,
2726 extack);
Thomas Grafda5e0492006-08-10 21:17:37 -07002727 if (err < 0)
2728 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002730 err = rtnl_ensure_unique_netns(tb, extack, false);
2731 if (err < 0)
2732 goto errout;
2733
Thomas Graf5176f912006-08-26 20:13:18 -07002734 if (tb[IFLA_IFNAME])
2735 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
Patrick McHardy78e5b8912006-09-13 20:35:36 -07002736 else
2737 ifname[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 err = -EINVAL;
Thomas Grafda5e0492006-08-10 21:17:37 -07002740 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -07002741 if (ifm->ifi_index > 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00002742 dev = __dev_get_by_index(net, ifm->ifi_index);
Thomas Grafda5e0492006-08-10 21:17:37 -07002743 else if (tb[IFLA_IFNAME])
Eric Dumazeta3d12892009-10-21 10:59:31 +00002744 dev = __dev_get_by_name(net, ifname);
Thomas Grafda5e0492006-08-10 21:17:37 -07002745 else
2746 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747
Thomas Grafda5e0492006-08-10 21:17:37 -07002748 if (dev == NULL) {
2749 err = -ENODEV;
2750 goto errout;
2751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002753 err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
Thomas Grafda5e0492006-08-10 21:17:37 -07002754errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 return err;
2756}
2757
WANG Cong66400d52015-03-24 11:53:31 -07002758static int rtnl_group_dellink(const struct net *net, int group)
2759{
2760 struct net_device *dev, *aux;
2761 LIST_HEAD(list_kill);
2762 bool found = false;
2763
2764 if (!group)
2765 return -EPERM;
2766
2767 for_each_netdev(net, dev) {
2768 if (dev->group == group) {
2769 const struct rtnl_link_ops *ops;
2770
2771 found = true;
2772 ops = dev->rtnl_link_ops;
2773 if (!ops || !ops->dellink)
2774 return -EOPNOTSUPP;
2775 }
2776 }
2777
2778 if (!found)
2779 return -ENODEV;
2780
2781 for_each_netdev_safe(net, dev, aux) {
2782 if (dev->group == group) {
2783 const struct rtnl_link_ops *ops;
2784
2785 ops = dev->rtnl_link_ops;
2786 ops->dellink(dev, &list_kill);
2787 }
2788 }
2789 unregister_netdevice_many(&list_kill);
2790
2791 return 0;
2792}
2793
Thomas Graf614732e2015-07-21 10:44:06 +02002794int rtnl_delete_link(struct net_device *dev)
2795{
2796 const struct rtnl_link_ops *ops;
2797 LIST_HEAD(list_kill);
2798
2799 ops = dev->rtnl_link_ops;
2800 if (!ops || !ops->dellink)
2801 return -EOPNOTSUPP;
2802
2803 ops->dellink(dev, &list_kill);
2804 unregister_netdevice_many(&list_kill);
2805
2806 return 0;
2807}
2808EXPORT_SYMBOL_GPL(rtnl_delete_link);
2809
David Ahernc21ef3e2017-04-16 09:48:24 -07002810static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
2811 struct netlink_ext_ack *extack)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002812{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002813 struct net *net = sock_net(skb->sk);
Christian Braunerb61ad682018-01-24 15:26:34 +01002814 struct net *tgt_net = net;
2815 struct net_device *dev = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002816 struct ifinfomsg *ifm;
2817 char ifname[IFNAMSIZ];
2818 struct nlattr *tb[IFLA_MAX+1];
2819 int err;
Christian Braunerb61ad682018-01-24 15:26:34 +01002820 int netnsid = -1;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002821
David Ahernc21ef3e2017-04-16 09:48:24 -07002822 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002823 if (err < 0)
2824 return err;
2825
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002826 err = rtnl_ensure_unique_netns(tb, extack, true);
2827 if (err < 0)
2828 return err;
2829
Patrick McHardy38f7b872007-06-13 12:03:51 -07002830 if (tb[IFLA_IFNAME])
2831 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2832
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002833 if (tb[IFLA_TARGET_NETNSID]) {
2834 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
Christian Braunerc383edc2018-09-04 21:53:47 +02002835 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
Christian Braunerb61ad682018-01-24 15:26:34 +01002836 if (IS_ERR(tgt_net))
2837 return PTR_ERR(tgt_net);
2838 }
2839
2840 err = -EINVAL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002841 ifm = nlmsg_data(nlh);
2842 if (ifm->ifi_index > 0)
Christian Braunerb61ad682018-01-24 15:26:34 +01002843 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002844 else if (tb[IFLA_IFNAME])
Christian Braunerb61ad682018-01-24 15:26:34 +01002845 dev = __dev_get_by_name(tgt_net, ifname);
WANG Cong66400d52015-03-24 11:53:31 -07002846 else if (tb[IFLA_GROUP])
Christian Braunerb61ad682018-01-24 15:26:34 +01002847 err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP]));
Patrick McHardy38f7b872007-06-13 12:03:51 -07002848 else
Christian Braunerb61ad682018-01-24 15:26:34 +01002849 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002850
Christian Braunerb61ad682018-01-24 15:26:34 +01002851 if (!dev) {
2852 if (tb[IFLA_IFNAME] || ifm->ifi_index > 0)
2853 err = -ENODEV;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002854
Christian Braunerb61ad682018-01-24 15:26:34 +01002855 goto out;
2856 }
2857
2858 err = rtnl_delete_link(dev);
2859
2860out:
2861 if (netnsid >= 0)
2862 put_net(tgt_net);
2863
2864 return err;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002865}
2866
Patrick McHardy3729d502010-02-26 06:34:54 +00002867int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
2868{
2869 unsigned int old_flags;
2870 int err;
2871
2872 old_flags = dev->flags;
2873 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
Petr Machata6d040322018-12-06 17:05:43 +00002874 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
2875 NULL);
Patrick McHardy3729d502010-02-26 06:34:54 +00002876 if (err < 0)
2877 return err;
2878 }
2879
Roopa Prabhu8d356b82018-07-04 16:46:29 -07002880 if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) {
Roopa Prabhu56a49d72018-09-12 13:21:48 -07002881 __dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags));
Roopa Prabhu8d356b82018-07-04 16:46:29 -07002882 } else {
2883 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
2884 __dev_notify_flags(dev, old_flags, ~0U);
2885 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002886 return 0;
2887}
2888EXPORT_SYMBOL(rtnl_configure_link);
2889
David Ahernd0522f12018-11-06 12:51:14 -08002890struct net_device *rtnl_create_link(struct net *net, const char *ifname,
2891 unsigned char name_assign_type,
2892 const struct rtnl_link_ops *ops,
2893 struct nlattr *tb[],
2894 struct netlink_ext_ack *extack)
Pavel Emelianove7199282007-08-08 22:16:38 -07002895{
Pavel Emelianove7199282007-08-08 22:16:38 -07002896 struct net_device *dev;
Jiri Pirkod40156a2012-07-20 02:28:47 +00002897 unsigned int num_tx_queues = 1;
2898 unsigned int num_rx_queues = 1;
Pavel Emelianove7199282007-08-08 22:16:38 -07002899
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002900 if (tb[IFLA_NUM_TX_QUEUES])
2901 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
2902 else if (ops->get_num_tx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002903 num_tx_queues = ops->get_num_tx_queues();
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002904
2905 if (tb[IFLA_NUM_RX_QUEUES])
2906 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
2907 else if (ops->get_num_rx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002908 num_rx_queues = ops->get_num_rx_queues();
stephen hemmingerefacb302012-04-10 18:34:43 +00002909
David Ahernd0522f12018-11-06 12:51:14 -08002910 if (num_tx_queues < 1 || num_tx_queues > 4096) {
2911 NL_SET_ERR_MSG(extack, "Invalid number of transmit queues");
Eric Dumazet0e1d6ec2018-10-02 15:47:35 -07002912 return ERR_PTR(-EINVAL);
David Ahernd0522f12018-11-06 12:51:14 -08002913 }
Eric Dumazet0e1d6ec2018-10-02 15:47:35 -07002914
David Ahernd0522f12018-11-06 12:51:14 -08002915 if (num_rx_queues < 1 || num_rx_queues > 4096) {
2916 NL_SET_ERR_MSG(extack, "Invalid number of receive queues");
Eric Dumazet0e1d6ec2018-10-02 15:47:35 -07002917 return ERR_PTR(-EINVAL);
David Ahernd0522f12018-11-06 12:51:14 -08002918 }
Eric Dumazet0e1d6ec2018-10-02 15:47:35 -07002919
Tom Gundersen55177502014-07-14 16:37:25 +02002920 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
Tom Gundersenc835a672014-07-14 16:37:24 +02002921 ops->setup, num_tx_queues, num_rx_queues);
Pavel Emelianove7199282007-08-08 22:16:38 -07002922 if (!dev)
Tobias Klauserd1892e42017-02-20 16:32:06 +01002923 return ERR_PTR(-ENOMEM);
Pavel Emelianove7199282007-08-08 22:16:38 -07002924
Eric W. Biederman81adee42009-11-08 00:53:51 -08002925 dev_net_set(dev, net);
2926 dev->rtnl_link_ops = ops;
Patrick McHardy3729d502010-02-26 06:34:54 +00002927 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
Eric W. Biederman81adee42009-11-08 00:53:51 -08002928
Pavel Emelianove7199282007-08-08 22:16:38 -07002929 if (tb[IFLA_MTU])
2930 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002931 if (tb[IFLA_ADDRESS]) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002932 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
2933 nla_len(tb[IFLA_ADDRESS]));
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002934 dev->addr_assign_type = NET_ADDR_SET;
2935 }
Pavel Emelianove7199282007-08-08 22:16:38 -07002936 if (tb[IFLA_BROADCAST])
2937 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
2938 nla_len(tb[IFLA_BROADCAST]));
2939 if (tb[IFLA_TXQLEN])
2940 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
2941 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002942 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Pavel Emelianove7199282007-08-08 22:16:38 -07002943 if (tb[IFLA_LINKMODE])
2944 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
Patrick McHardyffa934f2011-01-20 03:00:42 +00002945 if (tb[IFLA_GROUP])
2946 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Stephen Hemminger46e6b992017-12-07 15:40:19 -08002947 if (tb[IFLA_GSO_MAX_SIZE])
2948 netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE]));
2949 if (tb[IFLA_GSO_MAX_SEGS])
Stephen Hemmingera0b586f2017-12-08 15:34:13 -08002950 dev->gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
Pavel Emelianove7199282007-08-08 22:16:38 -07002951
2952 return dev;
Pavel Emelianove7199282007-08-08 22:16:38 -07002953}
Eric Dumazete0d087a2009-11-07 01:26:17 -08002954EXPORT_SYMBOL(rtnl_create_link);
Pavel Emelianove7199282007-08-08 22:16:38 -07002955
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002956static int rtnl_group_changelink(const struct sk_buff *skb,
2957 struct net *net, int group,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002958 struct ifinfomsg *ifm,
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002959 struct netlink_ext_ack *extack,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002960 struct nlattr **tb)
2961{
WANG Congd0795352015-03-23 16:31:09 -07002962 struct net_device *dev, *aux;
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002963 int err;
2964
WANG Congd0795352015-03-23 16:31:09 -07002965 for_each_netdev_safe(net, dev, aux) {
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002966 if (dev->group == group) {
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002967 err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002968 if (err < 0)
2969 return err;
2970 }
2971 }
2972
2973 return 0;
2974}
2975
Jakub Kicinskia2939742018-11-27 22:32:31 -08002976static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2977 struct nlattr **attr, struct netlink_ext_ack *extack)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002978{
Jakub Kicinski420d0312018-11-27 22:32:30 -08002979 struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1];
2980 unsigned char name_assign_type = NET_NAME_USER;
2981 struct nlattr *linkinfo[IFLA_INFO_MAX + 1];
2982 const struct rtnl_link_ops *m_ops = NULL;
Jakub Kicinski420d0312018-11-27 22:32:30 -08002983 struct net_device *master_dev = NULL;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002984 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002985 const struct rtnl_link_ops *ops;
Jakub Kicinski420d0312018-11-27 22:32:30 -08002986 struct nlattr *tb[IFLA_MAX + 1];
2987 struct net *dest_net, *link_net;
2988 struct nlattr **slave_data;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002989 char kind[MODULE_NAME_LEN];
Jakub Kicinski420d0312018-11-27 22:32:30 -08002990 struct net_device *dev;
2991 struct ifinfomsg *ifm;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002992 char ifname[IFNAMSIZ];
Jakub Kicinski420d0312018-11-27 22:32:30 -08002993 struct nlattr **data;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002994 int err;
2995
Johannes Berg95a5afc2008-10-16 15:24:51 -07002996#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002997replay:
Thomas Graf8072f082007-07-31 14:13:50 -07002998#endif
David Ahernc21ef3e2017-04-16 09:48:24 -07002999 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07003000 if (err < 0)
3001 return err;
3002
Christian Brauner4ff66ca2018-02-07 13:53:20 +01003003 err = rtnl_ensure_unique_netns(tb, extack, false);
3004 if (err < 0)
3005 return err;
3006
Patrick McHardy38f7b872007-06-13 12:03:51 -07003007 if (tb[IFLA_IFNAME])
3008 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
3009 else
3010 ifname[0] = '\0';
3011
3012 ifm = nlmsg_data(nlh);
3013 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07003014 dev = __dev_get_by_index(net, ifm->ifi_index);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00003015 else {
3016 if (ifname[0])
3017 dev = __dev_get_by_name(net, ifname);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00003018 else
3019 dev = NULL;
3020 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07003021
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01003022 if (dev) {
3023 master_dev = netdev_master_upper_dev_get(dev);
3024 if (master_dev)
3025 m_ops = master_dev->rtnl_link_ops;
3026 }
3027
Eric Dumazete0d087a2009-11-07 01:26:17 -08003028 err = validate_linkmsg(dev, tb);
3029 if (err < 0)
Thomas Graf1840bb12008-02-23 19:54:36 -08003030 return err;
3031
Patrick McHardy38f7b872007-06-13 12:03:51 -07003032 if (tb[IFLA_LINKINFO]) {
3033 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +02003034 tb[IFLA_LINKINFO], ifla_info_policy,
3035 NULL);
Patrick McHardy38f7b872007-06-13 12:03:51 -07003036 if (err < 0)
3037 return err;
3038 } else
3039 memset(linkinfo, 0, sizeof(linkinfo));
3040
3041 if (linkinfo[IFLA_INFO_KIND]) {
3042 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
3043 ops = rtnl_link_ops_get(kind);
3044 } else {
3045 kind[0] = '\0';
3046 ops = NULL;
3047 }
3048
Jakub Kicinski420d0312018-11-27 22:32:30 -08003049 data = NULL;
3050 if (ops) {
3051 if (ops->maxtype > RTNL_MAX_TYPE)
3052 return -EINVAL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003053
Jakub Kicinski420d0312018-11-27 22:32:30 -08003054 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
3055 err = nla_parse_nested(attr, ops->maxtype,
3056 linkinfo[IFLA_INFO_DATA],
3057 ops->policy, extack);
3058 if (err < 0)
3059 return err;
3060 data = attr;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003061 }
Jakub Kicinski420d0312018-11-27 22:32:30 -08003062 if (ops->validate) {
3063 err = ops->validate(tb, data, extack);
3064 if (err < 0)
3065 return err;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01003066 }
Jakub Kicinski420d0312018-11-27 22:32:30 -08003067 }
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01003068
Jakub Kicinski420d0312018-11-27 22:32:30 -08003069 slave_data = NULL;
3070 if (m_ops) {
3071 if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)
3072 return -EINVAL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003073
Jakub Kicinski420d0312018-11-27 22:32:30 -08003074 if (m_ops->slave_maxtype &&
3075 linkinfo[IFLA_INFO_SLAVE_DATA]) {
3076 err = nla_parse_nested(slave_attr, m_ops->slave_maxtype,
3077 linkinfo[IFLA_INFO_SLAVE_DATA],
3078 m_ops->slave_policy, extack);
3079 if (err < 0)
3080 return err;
3081 slave_data = slave_attr;
3082 }
3083 }
3084
3085 if (dev) {
3086 int status = 0;
3087
3088 if (nlh->nlmsg_flags & NLM_F_EXCL)
3089 return -EEXIST;
3090 if (nlh->nlmsg_flags & NLM_F_REPLACE)
3091 return -EOPNOTSUPP;
3092
3093 if (linkinfo[IFLA_INFO_DATA]) {
3094 if (!ops || ops != dev->rtnl_link_ops ||
3095 !ops->changelink)
Patrick McHardy38f7b872007-06-13 12:03:51 -07003096 return -EOPNOTSUPP;
3097
Jakub Kicinski420d0312018-11-27 22:32:30 -08003098 err = ops->changelink(dev, tb, data, extack);
3099 if (err < 0)
3100 return err;
3101 status |= DO_SETLINK_NOTIFY;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003102 }
3103
Jakub Kicinski420d0312018-11-27 22:32:30 -08003104 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
3105 if (!m_ops || !m_ops->slave_changelink)
3106 return -EOPNOTSUPP;
3107
3108 err = m_ops->slave_changelink(master_dev, dev, tb,
3109 slave_data, extack);
3110 if (err < 0)
3111 return err;
3112 status |= DO_SETLINK_NOTIFY;
3113 }
3114
3115 return do_setlink(skb, dev, ifm, extack, tb, ifname, status);
3116 }
3117
3118 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
3119 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
3120 return rtnl_group_changelink(skb, net,
Patrick McHardyffa934f2011-01-20 03:00:42 +00003121 nla_get_u32(tb[IFLA_GROUP]),
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07003122 ifm, extack, tb);
Jakub Kicinski420d0312018-11-27 22:32:30 -08003123 return -ENODEV;
3124 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07003125
Jakub Kicinski420d0312018-11-27 22:32:30 -08003126 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
3127 return -EOPNOTSUPP;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003128
Jakub Kicinski420d0312018-11-27 22:32:30 -08003129 if (!ops) {
Johannes Berg95a5afc2008-10-16 15:24:51 -07003130#ifdef CONFIG_MODULES
Jakub Kicinski420d0312018-11-27 22:32:30 -08003131 if (kind[0]) {
3132 __rtnl_unlock();
3133 request_module("rtnl-link-%s", kind);
3134 rtnl_lock();
3135 ops = rtnl_link_ops_get(kind);
3136 if (ops)
3137 goto replay;
3138 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07003139#endif
Jakub Kicinski420d0312018-11-27 22:32:30 -08003140 NL_SET_ERR_MSG(extack, "Unknown device type");
3141 return -EOPNOTSUPP;
3142 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07003143
Jakub Kicinski420d0312018-11-27 22:32:30 -08003144 if (!ops->setup)
3145 return -EOPNOTSUPP;
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02003146
Jakub Kicinski420d0312018-11-27 22:32:30 -08003147 if (!ifname[0]) {
3148 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
3149 name_assign_type = NET_NAME_ENUM;
3150 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07003151
Jakub Kicinski420d0312018-11-27 22:32:30 -08003152 dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN);
3153 if (IS_ERR(dest_net))
3154 return PTR_ERR(dest_net);
Eric W. Biederman13ad1772011-01-29 14:57:22 +00003155
Jakub Kicinski420d0312018-11-27 22:32:30 -08003156 if (tb[IFLA_LINK_NETNSID]) {
3157 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
Nicolas Dichtel317f4812015-01-15 15:11:18 +01003158
Jakub Kicinski420d0312018-11-27 22:32:30 -08003159 link_net = get_net_ns_by_id(dest_net, id);
3160 if (!link_net) {
3161 NL_SET_ERR_MSG(extack, "Unknown network namespace id");
3162 err = -EINVAL;
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00003163 goto out;
3164 }
Jakub Kicinski420d0312018-11-27 22:32:30 -08003165 err = -EPERM;
3166 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
3167 goto out;
3168 } else {
3169 link_net = NULL;
3170 }
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00003171
Jakub Kicinski420d0312018-11-27 22:32:30 -08003172 dev = rtnl_create_link(link_net ? : dest_net, ifname,
3173 name_assign_type, ops, tb, extack);
3174 if (IS_ERR(dev)) {
3175 err = PTR_ERR(dev);
David S. Miller43638902015-03-10 21:58:32 -04003176 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003177 }
Jakub Kicinski420d0312018-11-27 22:32:30 -08003178
3179 dev->ifindex = ifm->ifi_index;
3180
3181 if (ops->newlink) {
3182 err = ops->newlink(link_net ? : net, dev, tb, data, extack);
3183 /* Drivers should call free_netdev() in ->destructor
3184 * and unregister it on failure after registration
3185 * so that device could be finally freed in rtnl_unlock.
3186 */
3187 if (err < 0) {
3188 /* If device is not registered at all, free it now */
3189 if (dev->reg_state == NETREG_UNINITIALIZED)
3190 free_netdev(dev);
3191 goto out;
3192 }
3193 } else {
3194 err = register_netdevice(dev);
3195 if (err < 0) {
3196 free_netdev(dev);
3197 goto out;
3198 }
3199 }
3200 err = rtnl_configure_link(dev, ifm);
3201 if (err < 0)
3202 goto out_unregister;
3203 if (link_net) {
3204 err = dev_change_net_namespace(dev, dest_net, ifname);
3205 if (err < 0)
3206 goto out_unregister;
3207 }
3208 if (tb[IFLA_MASTER]) {
3209 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
3210 if (err)
3211 goto out_unregister;
3212 }
3213out:
3214 if (link_net)
3215 put_net(link_net);
3216 put_net(dest_net);
3217 return err;
3218out_unregister:
3219 if (ops->newlink) {
3220 LIST_HEAD(list_kill);
3221
3222 ops->dellink(dev, &list_kill);
3223 unregister_netdevice_many(&list_kill);
3224 } else {
3225 unregister_netdevice(dev);
3226 }
3227 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003228}
3229
Jakub Kicinskia2939742018-11-27 22:32:31 -08003230static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3231 struct netlink_ext_ack *extack)
3232{
3233 struct nlattr **attr;
3234 int ret;
3235
3236 attr = kmalloc_array(RTNL_MAX_TYPE + 1, sizeof(*attr), GFP_KERNEL);
3237 if (!attr)
3238 return -ENOMEM;
3239
3240 ret = __rtnl_newlink(skb, nlh, attr, extack);
3241 kfree(attr);
3242 return ret;
3243}
3244
David Ahernc21ef3e2017-04-16 09:48:24 -07003245static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3246 struct netlink_ext_ack *extack)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003247{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003248 struct net *net = sock_net(skb->sk);
Jiri Benc79e1ad12017-11-02 17:04:38 -02003249 struct net *tgt_net = net;
Thomas Grafb60c5112006-08-04 23:05:34 -07003250 struct ifinfomsg *ifm;
Eric Dumazeta3d12892009-10-21 10:59:31 +00003251 char ifname[IFNAMSIZ];
Thomas Grafb60c5112006-08-04 23:05:34 -07003252 struct nlattr *tb[IFLA_MAX+1];
3253 struct net_device *dev = NULL;
3254 struct sk_buff *nskb;
Jiri Benc79e1ad12017-11-02 17:04:38 -02003255 int netnsid = -1;
Thomas Graf339bf982006-11-10 14:10:15 -08003256 int err;
Greg Rose115c9b82012-02-21 16:54:48 -05003257 u32 ext_filter_mask = 0;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003258
David Ahernc21ef3e2017-04-16 09:48:24 -07003259 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Thomas Grafb60c5112006-08-04 23:05:34 -07003260 if (err < 0)
Eric Sesterhenn9918f232006-09-26 23:26:38 -07003261 return err;
Thomas Grafb60c5112006-08-04 23:05:34 -07003262
Christian Brauner4ff66ca2018-02-07 13:53:20 +01003263 err = rtnl_ensure_unique_netns(tb, extack, true);
3264 if (err < 0)
3265 return err;
3266
Christian Brauner7e4a8d52018-09-04 21:53:53 +02003267 if (tb[IFLA_TARGET_NETNSID]) {
3268 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
Christian Braunerc383edc2018-09-04 21:53:47 +02003269 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
Jiri Benc79e1ad12017-11-02 17:04:38 -02003270 if (IS_ERR(tgt_net))
3271 return PTR_ERR(tgt_net);
3272 }
3273
Eric Dumazeta3d12892009-10-21 10:59:31 +00003274 if (tb[IFLA_IFNAME])
3275 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
3276
Greg Rose115c9b82012-02-21 16:54:48 -05003277 if (tb[IFLA_EXT_MASK])
3278 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3279
Jiri Benc79e1ad12017-11-02 17:04:38 -02003280 err = -EINVAL;
Thomas Grafb60c5112006-08-04 23:05:34 -07003281 ifm = nlmsg_data(nlh);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003282 if (ifm->ifi_index > 0)
Jiri Benc79e1ad12017-11-02 17:04:38 -02003283 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003284 else if (tb[IFLA_IFNAME])
Jiri Benc79e1ad12017-11-02 17:04:38 -02003285 dev = __dev_get_by_name(tgt_net, ifname);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003286 else
Jiri Benc79e1ad12017-11-02 17:04:38 -02003287 goto out;
Thomas Grafb60c5112006-08-04 23:05:34 -07003288
Jiri Benc79e1ad12017-11-02 17:04:38 -02003289 err = -ENODEV;
Eric Dumazeta3d12892009-10-21 10:59:31 +00003290 if (dev == NULL)
Jiri Benc79e1ad12017-11-02 17:04:38 -02003291 goto out;
Eric Dumazeta3d12892009-10-21 10:59:31 +00003292
Jiri Benc79e1ad12017-11-02 17:04:38 -02003293 err = -ENOBUFS;
Greg Rose115c9b82012-02-21 16:54:48 -05003294 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003295 if (nskb == NULL)
Jiri Benc79e1ad12017-11-02 17:04:38 -02003296 goto out;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003297
Jiri Benc79e1ad12017-11-02 17:04:38 -02003298 err = rtnl_fill_ifinfo(nskb, dev, net,
3299 RTM_NEWLINK, NETLINK_CB(skb).portid,
3300 nlh->nlmsg_seq, 0, 0, ext_filter_mask,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003301 0, NULL, 0, netnsid);
Patrick McHardy26932562007-01-31 23:16:40 -08003302 if (err < 0) {
3303 /* -EMSGSIZE implies BUG in if_nlmsg_size */
3304 WARN_ON(err == -EMSGSIZE);
3305 kfree_skb(nskb);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003306 } else
Eric W. Biederman15e47302012-09-07 20:12:54 +00003307 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
Jiri Benc79e1ad12017-11-02 17:04:38 -02003308out:
3309 if (netnsid >= 0)
3310 put_net(tgt_net);
Thomas Grafb60c5112006-08-04 23:05:34 -07003311
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003312 return err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003313}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003314
Greg Rose115c9b82012-02-21 16:54:48 -05003315static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
Greg Rosec7ac8672011-06-10 01:27:09 +00003316{
Greg Rose115c9b82012-02-21 16:54:48 -05003317 struct net *net = sock_net(skb->sk);
3318 struct net_device *dev;
3319 struct nlattr *tb[IFLA_MAX+1];
3320 u32 ext_filter_mask = 0;
3321 u16 min_ifinfo_dump_size = 0;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02003322 int hdrlen;
Greg Rose115c9b82012-02-21 16:54:48 -05003323
Michal Schmidte5eca6d2014-05-28 14:15:19 +02003324 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3325 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
3326 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
3327
Johannes Bergfceb6432017-04-12 14:34:07 +02003328 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00003329 if (tb[IFLA_EXT_MASK])
3330 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3331 }
Greg Rose115c9b82012-02-21 16:54:48 -05003332
3333 if (!ext_filter_mask)
3334 return NLMSG_GOODSIZE;
3335 /*
3336 * traverse the list of net devices and compute the minimum
3337 * buffer size based upon the filter mask.
3338 */
Florian Westphal6853dd42017-08-09 20:41:51 +02003339 rcu_read_lock();
3340 for_each_netdev_rcu(net, dev) {
Greg Rose115c9b82012-02-21 16:54:48 -05003341 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
3342 if_nlmsg_size(dev,
3343 ext_filter_mask));
3344 }
Florian Westphal6853dd42017-08-09 20:41:51 +02003345 rcu_read_unlock();
Greg Rose115c9b82012-02-21 16:54:48 -05003346
Zhang Shengju93af2052016-11-22 14:14:28 +08003347 return nlmsg_total_size(min_ifinfo_dump_size);
Greg Rosec7ac8672011-06-10 01:27:09 +00003348}
3349
Adrian Bunk42bad1d2007-04-26 00:57:41 -07003350static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351{
3352 int idx;
3353 int s_idx = cb->family;
Christian Brauner87ccbb12018-09-04 21:53:51 +02003354 int type = cb->nlh->nlmsg_type - RTM_BASE;
David Ahernc63586d2018-10-24 12:59:02 -07003355 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356
3357 if (s_idx == 0)
3358 s_idx = 1;
Florian Westphal6853dd42017-08-09 20:41:51 +02003359
Patrick McHardy25239ce2010-04-26 16:02:05 +02003360 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
Florian Westphaladdf9b92017-12-02 21:44:05 +01003361 struct rtnl_link **tab;
Florian Westphaladdf9b92017-12-02 21:44:05 +01003362 struct rtnl_link *link;
Florian Westphal6853dd42017-08-09 20:41:51 +02003363 rtnl_dumpit_func dumpit;
3364
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 if (idx < s_idx || idx == PF_PACKET)
3366 continue;
Florian Westphal6853dd42017-08-09 20:41:51 +02003367
Florian Westphaladdf9b92017-12-02 21:44:05 +01003368 if (type < 0 || type >= RTM_NR_MSGTYPES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 continue;
Florian Westphal6853dd42017-08-09 20:41:51 +02003370
Florian Westphaladdf9b92017-12-02 21:44:05 +01003371 tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]);
3372 if (!tab)
3373 continue;
3374
3375 link = tab[type];
3376 if (!link)
3377 continue;
3378
3379 dumpit = link->dumpit;
Florian Westphal6853dd42017-08-09 20:41:51 +02003380 if (!dumpit)
3381 continue;
3382
Nicolas Dichtel04652772013-03-22 06:28:42 +00003383 if (idx > s_idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 memset(&cb->args[0], 0, sizeof(cb->args));
Nicolas Dichtel04652772013-03-22 06:28:42 +00003385 cb->prev_seq = 0;
3386 cb->seq = 0;
3387 }
David Ahernc63586d2018-10-24 12:59:02 -07003388 ret = dumpit(skb, cb);
Alexey Kodanev5e1acb42018-11-02 19:11:04 +03003389 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 break;
3391 }
3392 cb->family = idx;
3393
David Ahernc63586d2018-10-24 12:59:02 -07003394 return skb->len ? : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395}
3396
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003397struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003398 unsigned int change,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003399 u32 event, gfp_t flags, int *new_nsid,
3400 int new_ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003402 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 struct sk_buff *skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003404 int err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00003405 size_t if_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406
Alexei Starovoitov7f294052013-10-23 16:02:42 -07003407 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003408 if (skb == NULL)
3409 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410
Jiri Benc79e1ad12017-11-02 17:04:38 -02003411 err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
3412 type, 0, 0, change, 0, 0, event,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003413 new_nsid, new_ifindex, -1);
Patrick McHardy26932562007-01-31 23:16:40 -08003414 if (err < 0) {
3415 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
3416 WARN_ON(err == -EMSGSIZE);
3417 kfree_skb(skb);
3418 goto errout;
3419 }
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003420 return skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003421errout:
3422 if (err < 0)
Eric W. Biederman4b3da702007-11-19 22:27:40 -08003423 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003424 return NULL;
3425}
3426
3427void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
3428{
3429 struct net *net = dev_net(dev);
3430
3431 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
3432}
3433
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003434static void rtmsg_ifinfo_event(int type, struct net_device *dev,
3435 unsigned int change, u32 event,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003436 gfp_t flags, int *new_nsid, int new_ifindex)
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003437{
3438 struct sk_buff *skb;
3439
Nicolas Dichteled2a80a2015-05-13 14:19:42 +02003440 if (dev->reg_state != NETREG_REGISTERED)
3441 return;
3442
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003443 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid,
3444 new_ifindex);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003445 if (skb)
3446 rtmsg_ifinfo_send(skb, dev, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447}
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003448
3449void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
3450 gfp_t flags)
3451{
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003452 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3453 NULL, 0);
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003454}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003456void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003457 gfp_t flags, int *new_nsid, int new_ifindex)
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003458{
3459 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003460 new_nsid, new_ifindex);
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003461}
3462
John Fastabendd83b0602012-04-15 06:44:08 +00003463static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
3464 struct net_device *dev,
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003465 u8 *addr, u16 vid, u32 pid, u32 seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003466 int type, unsigned int flags,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003467 int nlflags, u16 ndm_state)
John Fastabendd83b0602012-04-15 06:44:08 +00003468{
3469 struct nlmsghdr *nlh;
3470 struct ndmsg *ndm;
3471
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003472 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
John Fastabendd83b0602012-04-15 06:44:08 +00003473 if (!nlh)
3474 return -EMSGSIZE;
3475
3476 ndm = nlmsg_data(nlh);
3477 ndm->ndm_family = AF_BRIDGE;
3478 ndm->ndm_pad1 = 0;
3479 ndm->ndm_pad2 = 0;
3480 ndm->ndm_flags = flags;
3481 ndm->ndm_type = 0;
3482 ndm->ndm_ifindex = dev->ifindex;
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003483 ndm->ndm_state = ndm_state;
John Fastabendd83b0602012-04-15 06:44:08 +00003484
3485 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
3486 goto nla_put_failure;
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003487 if (vid)
3488 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
3489 goto nla_put_failure;
John Fastabendd83b0602012-04-15 06:44:08 +00003490
Johannes Berg053c0952015-01-16 22:09:00 +01003491 nlmsg_end(skb, nlh);
3492 return 0;
John Fastabendd83b0602012-04-15 06:44:08 +00003493
3494nla_put_failure:
3495 nlmsg_cancel(skb, nlh);
3496 return -EMSGSIZE;
3497}
3498
John Fastabend3ff661c2012-04-15 06:44:14 +00003499static inline size_t rtnl_fdb_nlmsg_size(void)
3500{
Sabrina Dubrocaf82ef3e2016-11-18 15:50:39 +01003501 return NLMSG_ALIGN(sizeof(struct ndmsg)) +
3502 nla_total_size(ETH_ALEN) + /* NDA_LLADDR */
3503 nla_total_size(sizeof(u16)) + /* NDA_VLAN */
3504 0;
John Fastabend3ff661c2012-04-15 06:44:14 +00003505}
3506
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003507static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
3508 u16 ndm_state)
John Fastabend3ff661c2012-04-15 06:44:14 +00003509{
3510 struct net *net = dev_net(dev);
3511 struct sk_buff *skb;
3512 int err = -ENOBUFS;
3513
3514 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
3515 if (!skb)
3516 goto errout;
3517
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003518 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003519 0, 0, type, NTF_SELF, 0, ndm_state);
John Fastabend3ff661c2012-04-15 06:44:14 +00003520 if (err < 0) {
3521 kfree_skb(skb);
3522 goto errout;
3523 }
3524
3525 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
3526 return;
3527errout:
3528 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
3529}
3530
Vlad Yasevich090096b2013-03-06 15:39:42 +00003531/**
3532 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3533 */
3534int ndo_dflt_fdb_add(struct ndmsg *ndm,
3535 struct nlattr *tb[],
3536 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003537 const unsigned char *addr, u16 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003538 u16 flags)
3539{
3540 int err = -EINVAL;
3541
3542 /* If aging addresses are supported device will need to
3543 * implement its own handler for this.
3544 */
3545 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
3546 pr_info("%s: FDB only supports static addresses\n", dev->name);
3547 return err;
3548 }
3549
Or Gerlitz65891fe2014-12-14 18:19:05 +02003550 if (vid) {
3551 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
3552 return err;
3553 }
3554
Vlad Yasevich090096b2013-03-06 15:39:42 +00003555 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3556 err = dev_uc_add_excl(dev, addr);
3557 else if (is_multicast_ether_addr(addr))
3558 err = dev_mc_add_excl(dev, addr);
3559
3560 /* Only return duplicate errors if NLM_F_EXCL is set */
3561 if (err == -EEXIST && !(flags & NLM_F_EXCL))
3562 err = 0;
3563
3564 return err;
3565}
3566EXPORT_SYMBOL(ndo_dflt_fdb_add);
3567
Florian Westphalb88d12e2017-10-10 17:10:04 +02003568static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
3569 struct netlink_ext_ack *extack)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003570{
3571 u16 vid = 0;
3572
3573 if (vlan_attr) {
3574 if (nla_len(vlan_attr) != sizeof(u16)) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003575 NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
Jiri Pirkof6f64242014-11-28 14:34:15 +01003576 return -EINVAL;
3577 }
3578
3579 vid = nla_get_u16(vlan_attr);
3580
3581 if (!vid || vid >= VLAN_VID_MASK) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003582 NL_SET_ERR_MSG(extack, "invalid vlan id");
Jiri Pirkof6f64242014-11-28 14:34:15 +01003583 return -EINVAL;
3584 }
3585 }
3586 *p_vid = vid;
3587 return 0;
3588}
3589
David Ahernc21ef3e2017-04-16 09:48:24 -07003590static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
3591 struct netlink_ext_ack *extack)
John Fastabend77162022012-04-15 06:43:56 +00003592{
3593 struct net *net = sock_net(skb->sk);
John Fastabend77162022012-04-15 06:43:56 +00003594 struct ndmsg *ndm;
3595 struct nlattr *tb[NDA_MAX+1];
3596 struct net_device *dev;
3597 u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01003598 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00003599 int err;
3600
David Ahernc21ef3e2017-04-16 09:48:24 -07003601 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
John Fastabend77162022012-04-15 06:43:56 +00003602 if (err < 0)
3603 return err;
3604
3605 ndm = nlmsg_data(nlh);
3606 if (ndm->ndm_ifindex == 0) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003607 NL_SET_ERR_MSG(extack, "invalid ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003608 return -EINVAL;
3609 }
3610
3611 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3612 if (dev == NULL) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003613 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003614 return -ENODEV;
3615 }
3616
3617 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003618 NL_SET_ERR_MSG(extack, "invalid address");
John Fastabend77162022012-04-15 06:43:56 +00003619 return -EINVAL;
3620 }
3621
Ido Schimmelda715772018-10-29 20:36:43 +00003622 if (dev->type != ARPHRD_ETHER) {
3623 NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices");
3624 return -EINVAL;
3625 }
3626
John Fastabend77162022012-04-15 06:43:56 +00003627 addr = nla_data(tb[NDA_LLADDR]);
John Fastabend77162022012-04-15 06:43:56 +00003628
Florian Westphalb88d12e2017-10-10 17:10:04 +02003629 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
Jiri Pirkof6f64242014-11-28 14:34:15 +01003630 if (err)
3631 return err;
3632
John Fastabend77162022012-04-15 06:43:56 +00003633 err = -EOPNOTSUPP;
3634
3635 /* Support fdb on master device the net/bridge default case */
3636 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3637 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003638 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3639 const struct net_device_ops *ops = br_dev->netdev_ops;
3640
Jiri Pirkof6f64242014-11-28 14:34:15 +01003641 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
3642 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00003643 if (err)
3644 goto out;
3645 else
3646 ndm->ndm_flags &= ~NTF_MASTER;
3647 }
3648
3649 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00003650 if ((ndm->ndm_flags & NTF_SELF)) {
3651 if (dev->netdev_ops->ndo_fdb_add)
3652 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003653 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003654 nlh->nlmsg_flags);
3655 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01003656 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003657 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00003658
John Fastabend3ff661c2012-04-15 06:44:14 +00003659 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003660 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
3661 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00003662 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00003663 }
John Fastabend77162022012-04-15 06:43:56 +00003664 }
3665out:
3666 return err;
3667}
3668
Vlad Yasevich090096b2013-03-06 15:39:42 +00003669/**
3670 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3671 */
3672int ndo_dflt_fdb_del(struct ndmsg *ndm,
3673 struct nlattr *tb[],
3674 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003675 const unsigned char *addr, u16 vid)
Vlad Yasevich090096b2013-03-06 15:39:42 +00003676{
Alexander Duyckc8a89c42014-07-15 15:15:20 -07003677 int err = -EINVAL;
Vlad Yasevich090096b2013-03-06 15:39:42 +00003678
3679 /* If aging addresses are supported device will need to
3680 * implement its own handler for this.
3681 */
Sridhar Samudrala64535992013-08-08 15:19:48 -07003682 if (!(ndm->ndm_state & NUD_PERMANENT)) {
Vlad Yasevich090096b2013-03-06 15:39:42 +00003683 pr_info("%s: FDB only supports static addresses\n", dev->name);
Alexander Duyckc8a89c42014-07-15 15:15:20 -07003684 return err;
Vlad Yasevich090096b2013-03-06 15:39:42 +00003685 }
3686
3687 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3688 err = dev_uc_del(dev, addr);
3689 else if (is_multicast_ether_addr(addr))
3690 err = dev_mc_del(dev, addr);
Vlad Yasevich090096b2013-03-06 15:39:42 +00003691
3692 return err;
3693}
3694EXPORT_SYMBOL(ndo_dflt_fdb_del);
3695
David Ahernc21ef3e2017-04-16 09:48:24 -07003696static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
3697 struct netlink_ext_ack *extack)
John Fastabend77162022012-04-15 06:43:56 +00003698{
3699 struct net *net = sock_net(skb->sk);
3700 struct ndmsg *ndm;
Vlad Yasevich1690be62013-02-13 12:00:18 +00003701 struct nlattr *tb[NDA_MAX+1];
John Fastabend77162022012-04-15 06:43:56 +00003702 struct net_device *dev;
3703 int err = -EINVAL;
3704 __u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01003705 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00003706
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07003707 if (!netlink_capable(skb, CAP_NET_ADMIN))
Vlad Yasevich1690be62013-02-13 12:00:18 +00003708 return -EPERM;
3709
David Ahernc21ef3e2017-04-16 09:48:24 -07003710 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
Vlad Yasevich1690be62013-02-13 12:00:18 +00003711 if (err < 0)
3712 return err;
John Fastabend77162022012-04-15 06:43:56 +00003713
3714 ndm = nlmsg_data(nlh);
3715 if (ndm->ndm_ifindex == 0) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003716 NL_SET_ERR_MSG(extack, "invalid ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003717 return -EINVAL;
3718 }
3719
3720 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3721 if (dev == NULL) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003722 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003723 return -ENODEV;
3724 }
3725
Vlad Yasevich1690be62013-02-13 12:00:18 +00003726 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003727 NL_SET_ERR_MSG(extack, "invalid address");
John Fastabend77162022012-04-15 06:43:56 +00003728 return -EINVAL;
3729 }
3730
Ido Schimmelda715772018-10-29 20:36:43 +00003731 if (dev->type != ARPHRD_ETHER) {
3732 NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices");
3733 return -EINVAL;
3734 }
3735
Vlad Yasevich1690be62013-02-13 12:00:18 +00003736 addr = nla_data(tb[NDA_LLADDR]);
Vlad Yasevich1690be62013-02-13 12:00:18 +00003737
Florian Westphalb88d12e2017-10-10 17:10:04 +02003738 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
Jiri Pirkof6f64242014-11-28 14:34:15 +01003739 if (err)
3740 return err;
3741
John Fastabend77162022012-04-15 06:43:56 +00003742 err = -EOPNOTSUPP;
3743
3744 /* Support fdb on master device the net/bridge default case */
3745 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3746 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003747 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3748 const struct net_device_ops *ops = br_dev->netdev_ops;
John Fastabend77162022012-04-15 06:43:56 +00003749
Jiri Pirko898e5062013-01-03 22:48:52 +00003750 if (ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003751 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00003752
3753 if (err)
3754 goto out;
3755 else
3756 ndm->ndm_flags &= ~NTF_MASTER;
3757 }
3758
3759 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00003760 if (ndm->ndm_flags & NTF_SELF) {
3761 if (dev->netdev_ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003762 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
3763 vid);
Vlad Yasevich090096b2013-03-06 15:39:42 +00003764 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01003765 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00003766
John Fastabend3ff661c2012-04-15 06:44:14 +00003767 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003768 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
3769 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00003770 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00003771 }
John Fastabend77162022012-04-15 06:43:56 +00003772 }
3773out:
3774 return err;
3775}
3776
John Fastabendd83b0602012-04-15 06:44:08 +00003777static int nlmsg_populate_fdb(struct sk_buff *skb,
3778 struct netlink_callback *cb,
3779 struct net_device *dev,
3780 int *idx,
3781 struct netdev_hw_addr_list *list)
3782{
3783 struct netdev_hw_addr *ha;
3784 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00003785 u32 portid, seq;
John Fastabendd83b0602012-04-15 06:44:08 +00003786
Eric W. Biederman15e47302012-09-07 20:12:54 +00003787 portid = NETLINK_CB(cb->skb).portid;
John Fastabendd83b0602012-04-15 06:44:08 +00003788 seq = cb->nlh->nlmsg_seq;
3789
3790 list_for_each_entry(ha, &list->list, list) {
Roopa Prabhud2976532016-08-30 21:56:45 -07003791 if (*idx < cb->args[2])
John Fastabendd83b0602012-04-15 06:44:08 +00003792 goto skip;
3793
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003794 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
John Fastabenda7a558f2012-11-01 16:23:10 +00003795 portid, seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003796 RTM_NEWNEIGH, NTF_SELF,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003797 NLM_F_MULTI, NUD_PERMANENT);
John Fastabendd83b0602012-04-15 06:44:08 +00003798 if (err < 0)
3799 return err;
3800skip:
3801 *idx += 1;
3802 }
3803 return 0;
3804}
3805
3806/**
Ben Hutchings2c530402012-07-10 10:55:09 +00003807 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
John Fastabendd83b0602012-04-15 06:44:08 +00003808 * @nlh: netlink message header
3809 * @dev: netdevice
3810 *
3811 * Default netdevice operation to dump the existing unicast address list.
John Fastabend91f3e7b2013-03-29 08:18:37 +00003812 * Returns number of addresses from list put in skb.
John Fastabendd83b0602012-04-15 06:44:08 +00003813 */
3814int ndo_dflt_fdb_dump(struct sk_buff *skb,
3815 struct netlink_callback *cb,
3816 struct net_device *dev,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04003817 struct net_device *filter_dev,
Roopa Prabhud2976532016-08-30 21:56:45 -07003818 int *idx)
John Fastabendd83b0602012-04-15 06:44:08 +00003819{
3820 int err;
3821
Eric Dumazet68883892018-12-04 09:40:35 -08003822 if (dev->type != ARPHRD_ETHER)
3823 return -EINVAL;
3824
John Fastabendd83b0602012-04-15 06:44:08 +00003825 netif_addr_lock_bh(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -07003826 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
John Fastabendd83b0602012-04-15 06:44:08 +00003827 if (err)
3828 goto out;
Zhang Shengju2934c9d2016-11-30 16:37:34 +08003829 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
John Fastabendd83b0602012-04-15 06:44:08 +00003830out:
3831 netif_addr_unlock_bh(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -07003832 return err;
John Fastabendd83b0602012-04-15 06:44:08 +00003833}
3834EXPORT_SYMBOL(ndo_dflt_fdb_dump);
3835
David Ahern8c6e1372018-10-07 20:16:44 -07003836static int valid_fdb_dump_strict(const struct nlmsghdr *nlh,
3837 int *br_idx, int *brport_idx,
3838 struct netlink_ext_ack *extack)
3839{
3840 struct nlattr *tb[NDA_MAX + 1];
3841 struct ndmsg *ndm;
3842 int err, i;
3843
3844 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
3845 NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request");
3846 return -EINVAL;
3847 }
3848
3849 ndm = nlmsg_data(nlh);
3850 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
3851 ndm->ndm_flags || ndm->ndm_type) {
3852 NL_SET_ERR_MSG(extack, "Invalid values in header for fbd dump request");
3853 return -EINVAL;
3854 }
3855
3856 err = nlmsg_parse_strict(nlh, sizeof(struct ndmsg), tb, NDA_MAX,
3857 NULL, extack);
3858 if (err < 0)
3859 return err;
3860
3861 *brport_idx = ndm->ndm_ifindex;
3862 for (i = 0; i <= NDA_MAX; ++i) {
3863 if (!tb[i])
3864 continue;
3865
3866 switch (i) {
3867 case NDA_IFINDEX:
3868 if (nla_len(tb[i]) != sizeof(u32)) {
3869 NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request");
3870 return -EINVAL;
3871 }
3872 *brport_idx = nla_get_u32(tb[NDA_IFINDEX]);
3873 break;
3874 case NDA_MASTER:
3875 if (nla_len(tb[i]) != sizeof(u32)) {
3876 NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request");
3877 return -EINVAL;
3878 }
3879 *br_idx = nla_get_u32(tb[NDA_MASTER]);
3880 break;
3881 default:
3882 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request");
3883 return -EINVAL;
3884 }
3885 }
3886
3887 return 0;
3888}
3889
David Ahern8dfbda12018-10-07 20:16:43 -07003890static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh,
3891 int *br_idx, int *brport_idx,
3892 struct netlink_ext_ack *extack)
3893{
David Ahern8dfbda12018-10-07 20:16:43 -07003894 struct nlattr *tb[IFLA_MAX+1];
3895 int err;
3896
3897 /* A hack to preserve kernel<->userspace interface.
3898 * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0.
3899 * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails.
3900 * So, check for ndmsg with an optional u32 attribute (not used here).
3901 * Fortunately these sizes don't conflict with the size of ifinfomsg
3902 * with an optional attribute.
3903 */
3904 if (nlmsg_len(nlh) != sizeof(struct ndmsg) &&
3905 (nlmsg_len(nlh) != sizeof(struct ndmsg) +
3906 nla_attr_size(sizeof(u32)))) {
David Ahern4565d7e2018-10-08 13:57:24 -07003907 struct ifinfomsg *ifm;
3908
David Ahern8dfbda12018-10-07 20:16:43 -07003909 err = nlmsg_parse(nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
3910 ifla_policy, extack);
3911 if (err < 0) {
3912 return -EINVAL;
3913 } else if (err == 0) {
3914 if (tb[IFLA_MASTER])
3915 *br_idx = nla_get_u32(tb[IFLA_MASTER]);
3916 }
3917
David Ahern4565d7e2018-10-08 13:57:24 -07003918 ifm = nlmsg_data(nlh);
David Ahern8dfbda12018-10-07 20:16:43 -07003919 *brport_idx = ifm->ifi_index;
3920 }
3921 return 0;
3922}
3923
John Fastabend77162022012-04-15 06:43:56 +00003924static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
3925{
John Fastabend77162022012-04-15 06:43:56 +00003926 struct net_device *dev;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003927 struct net_device *br_dev = NULL;
3928 const struct net_device_ops *ops = NULL;
3929 const struct net_device_ops *cops = NULL;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003930 struct net *net = sock_net(skb->sk);
Roopa Prabhud2976532016-08-30 21:56:45 -07003931 struct hlist_head *head;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003932 int brport_idx = 0;
3933 int br_idx = 0;
Roopa Prabhud2976532016-08-30 21:56:45 -07003934 int h, s_h;
3935 int idx = 0, s_idx;
3936 int err = 0;
3937 int fidx = 0;
John Fastabend77162022012-04-15 06:43:56 +00003938
David Ahern8c6e1372018-10-07 20:16:44 -07003939 if (cb->strict_check)
3940 err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx,
3941 cb->extack);
3942 else
3943 err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx,
3944 cb->extack);
David Ahern8dfbda12018-10-07 20:16:43 -07003945 if (err < 0)
3946 return err;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003947
3948 if (br_idx) {
3949 br_dev = __dev_get_by_index(net, br_idx);
3950 if (!br_dev)
3951 return -ENODEV;
3952
3953 ops = br_dev->netdev_ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003954 }
3955
Roopa Prabhud2976532016-08-30 21:56:45 -07003956 s_h = cb->args[0];
3957 s_idx = cb->args[1];
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003958
Roopa Prabhud2976532016-08-30 21:56:45 -07003959 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
3960 idx = 0;
3961 head = &net->dev_index_head[h];
3962 hlist_for_each_entry(dev, head, index_hlist) {
3963
3964 if (brport_idx && (dev->ifindex != brport_idx))
3965 continue;
3966
3967 if (!br_idx) { /* user did not specify a specific bridge */
3968 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3969 br_dev = netdev_master_upper_dev_get(dev);
3970 cops = br_dev->netdev_ops;
3971 }
3972 } else {
3973 if (dev != br_dev &&
3974 !(dev->priv_flags & IFF_BRIDGE_PORT))
3975 continue;
3976
3977 if (br_dev != netdev_master_upper_dev_get(dev) &&
3978 !(dev->priv_flags & IFF_EBRIDGE))
3979 continue;
3980 cops = ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003981 }
3982
Roopa Prabhud2976532016-08-30 21:56:45 -07003983 if (idx < s_idx)
3984 goto cont;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003985
Roopa Prabhud2976532016-08-30 21:56:45 -07003986 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3987 if (cops && cops->ndo_fdb_dump) {
3988 err = cops->ndo_fdb_dump(skb, cb,
3989 br_dev, dev,
3990 &fidx);
3991 if (err == -EMSGSIZE)
3992 goto out;
3993 }
3994 }
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003995
Roopa Prabhud2976532016-08-30 21:56:45 -07003996 if (dev->netdev_ops->ndo_fdb_dump)
3997 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
3998 dev, NULL,
3999 &fidx);
4000 else
4001 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
4002 &fidx);
4003 if (err == -EMSGSIZE)
4004 goto out;
4005
4006 cops = NULL;
4007
4008 /* reset fdb offset to 0 for rest of the interfaces */
4009 cb->args[2] = 0;
4010 fidx = 0;
4011cont:
4012 idx++;
John Fastabend77162022012-04-15 06:43:56 +00004013 }
John Fastabend77162022012-04-15 06:43:56 +00004014 }
John Fastabend77162022012-04-15 06:43:56 +00004015
Roopa Prabhud2976532016-08-30 21:56:45 -07004016out:
4017 cb->args[0] = h;
4018 cb->args[1] = idx;
4019 cb->args[2] = fidx;
4020
John Fastabend77162022012-04-15 06:43:56 +00004021 return skb->len;
4022}
4023
Scott Feldman2c3c0312014-11-28 14:34:25 +01004024static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
4025 unsigned int attrnum, unsigned int flag)
4026{
4027 if (mask & flag)
4028 return nla_put_u8(skb, attrnum, !!(flags & flag));
4029 return 0;
4030}
4031
John Fastabend815cccb2012-10-24 08:13:09 +00004032int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Scott Feldman2c3c0312014-11-28 14:34:25 +01004033 struct net_device *dev, u16 mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -07004034 u32 flags, u32 mask, int nlflags,
4035 u32 filter_mask,
4036 int (*vlan_fill)(struct sk_buff *skb,
4037 struct net_device *dev,
4038 u32 filter_mask))
John Fastabend815cccb2012-10-24 08:13:09 +00004039{
4040 struct nlmsghdr *nlh;
4041 struct ifinfomsg *ifm;
4042 struct nlattr *br_afspec;
Scott Feldman2c3c0312014-11-28 14:34:25 +01004043 struct nlattr *protinfo;
John Fastabend815cccb2012-10-24 08:13:09 +00004044 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Jiri Pirko898e5062013-01-03 22:48:52 +00004045 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07004046 int err = 0;
John Fastabend815cccb2012-10-24 08:13:09 +00004047
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02004048 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
John Fastabend815cccb2012-10-24 08:13:09 +00004049 if (nlh == NULL)
4050 return -EMSGSIZE;
4051
4052 ifm = nlmsg_data(nlh);
4053 ifm->ifi_family = AF_BRIDGE;
4054 ifm->__ifi_pad = 0;
4055 ifm->ifi_type = dev->type;
4056 ifm->ifi_index = dev->ifindex;
4057 ifm->ifi_flags = dev_get_flags(dev);
4058 ifm->ifi_change = 0;
4059
4060
4061 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4062 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4063 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00004064 (br_dev &&
4065 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
John Fastabend815cccb2012-10-24 08:13:09 +00004066 (dev->addr_len &&
4067 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02004068 (dev->ifindex != dev_get_iflink(dev) &&
4069 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
John Fastabend815cccb2012-10-24 08:13:09 +00004070 goto nla_put_failure;
4071
4072 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
4073 if (!br_afspec)
4074 goto nla_put_failure;
4075
Roopa Prabhu1d460b92014-12-08 14:04:20 -08004076 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
John Fastabend815cccb2012-10-24 08:13:09 +00004077 nla_nest_cancel(skb, br_afspec);
4078 goto nla_put_failure;
4079 }
Roopa Prabhu1d460b92014-12-08 14:04:20 -08004080
4081 if (mode != BRIDGE_MODE_UNDEF) {
4082 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
4083 nla_nest_cancel(skb, br_afspec);
4084 goto nla_put_failure;
4085 }
4086 }
Scott Feldman7d4f8d82015-06-22 00:27:17 -07004087 if (vlan_fill) {
4088 err = vlan_fill(skb, dev, filter_mask);
4089 if (err) {
4090 nla_nest_cancel(skb, br_afspec);
4091 goto nla_put_failure;
4092 }
4093 }
John Fastabend815cccb2012-10-24 08:13:09 +00004094 nla_nest_end(skb, br_afspec);
4095
Scott Feldman2c3c0312014-11-28 14:34:25 +01004096 protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
4097 if (!protinfo)
4098 goto nla_put_failure;
4099
4100 if (brport_nla_put_flag(skb, flags, mask,
4101 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
4102 brport_nla_put_flag(skb, flags, mask,
4103 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
4104 brport_nla_put_flag(skb, flags, mask,
4105 IFLA_BRPORT_FAST_LEAVE,
4106 BR_MULTICAST_FAST_LEAVE) ||
4107 brport_nla_put_flag(skb, flags, mask,
4108 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
4109 brport_nla_put_flag(skb, flags, mask,
4110 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
4111 brport_nla_put_flag(skb, flags, mask,
4112 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
4113 brport_nla_put_flag(skb, flags, mask,
4114 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
4115 brport_nla_put_flag(skb, flags, mask,
4116 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
4117 nla_nest_cancel(skb, protinfo);
4118 goto nla_put_failure;
4119 }
4120
4121 nla_nest_end(skb, protinfo);
4122
Johannes Berg053c0952015-01-16 22:09:00 +01004123 nlmsg_end(skb, nlh);
4124 return 0;
John Fastabend815cccb2012-10-24 08:13:09 +00004125nla_put_failure:
4126 nlmsg_cancel(skb, nlh);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07004127 return err ? err : -EMSGSIZE;
John Fastabend815cccb2012-10-24 08:13:09 +00004128}
Scott Feldman7d4f8d82015-06-22 00:27:17 -07004129EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
John Fastabend815cccb2012-10-24 08:13:09 +00004130
David Ahern2d011be2018-10-07 20:16:31 -07004131static int valid_bridge_getlink_req(const struct nlmsghdr *nlh,
4132 bool strict_check, u32 *filter_mask,
4133 struct netlink_ext_ack *extack)
4134{
4135 struct nlattr *tb[IFLA_MAX+1];
4136 int err, i;
4137
4138 if (strict_check) {
4139 struct ifinfomsg *ifm;
4140
4141 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
4142 NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump");
4143 return -EINVAL;
4144 }
4145
4146 ifm = nlmsg_data(nlh);
4147 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
4148 ifm->ifi_change || ifm->ifi_index) {
4149 NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request");
4150 return -EINVAL;
4151 }
4152
4153 err = nlmsg_parse_strict(nlh, sizeof(struct ifinfomsg), tb,
4154 IFLA_MAX, ifla_policy, extack);
4155 } else {
4156 err = nlmsg_parse(nlh, sizeof(struct ifinfomsg), tb,
4157 IFLA_MAX, ifla_policy, extack);
4158 }
4159 if (err < 0)
4160 return err;
4161
4162 /* new attributes should only be added with strict checking */
4163 for (i = 0; i <= IFLA_MAX; ++i) {
4164 if (!tb[i])
4165 continue;
4166
4167 switch (i) {
4168 case IFLA_EXT_MASK:
4169 *filter_mask = nla_get_u32(tb[i]);
4170 break;
4171 default:
4172 if (strict_check) {
4173 NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request");
4174 return -EINVAL;
4175 }
4176 }
4177 }
4178
4179 return 0;
4180}
4181
John Fastabende5a55a82012-10-24 08:12:57 +00004182static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
4183{
David Ahern2d011be2018-10-07 20:16:31 -07004184 const struct nlmsghdr *nlh = cb->nlh;
John Fastabende5a55a82012-10-24 08:12:57 +00004185 struct net *net = sock_net(skb->sk);
4186 struct net_device *dev;
4187 int idx = 0;
4188 u32 portid = NETLINK_CB(cb->skb).portid;
David Ahern2d011be2018-10-07 20:16:31 -07004189 u32 seq = nlh->nlmsg_seq;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00004190 u32 filter_mask = 0;
Roopa Prabhud64f69b2015-09-15 14:44:29 -07004191 int err;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00004192
David Ahern2d011be2018-10-07 20:16:31 -07004193 err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask,
4194 cb->extack);
4195 if (err < 0 && cb->strict_check)
4196 return err;
John Fastabende5a55a82012-10-24 08:12:57 +00004197
4198 rcu_read_lock();
4199 for_each_netdev_rcu(net, dev) {
4200 const struct net_device_ops *ops = dev->netdev_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00004201 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
John Fastabende5a55a82012-10-24 08:12:57 +00004202
Jiri Pirko898e5062013-01-03 22:48:52 +00004203 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07004204 if (idx >= cb->args[0]) {
4205 err = br_dev->netdev_ops->ndo_bridge_getlink(
4206 skb, portid, seq, dev,
4207 filter_mask, NLM_F_MULTI);
David Ahernf6c57752017-05-15 23:19:17 -07004208 if (err < 0 && err != -EOPNOTSUPP) {
4209 if (likely(skb->len))
4210 break;
4211
4212 goto out_err;
4213 }
Roopa Prabhud64f69b2015-09-15 14:44:29 -07004214 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00004215 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00004216 }
4217
4218 if (ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07004219 if (idx >= cb->args[0]) {
4220 err = ops->ndo_bridge_getlink(skb, portid,
4221 seq, dev,
4222 filter_mask,
4223 NLM_F_MULTI);
David Ahernf6c57752017-05-15 23:19:17 -07004224 if (err < 0 && err != -EOPNOTSUPP) {
4225 if (likely(skb->len))
4226 break;
4227
4228 goto out_err;
4229 }
Roopa Prabhud64f69b2015-09-15 14:44:29 -07004230 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00004231 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00004232 }
4233 }
David Ahernf6c57752017-05-15 23:19:17 -07004234 err = skb->len;
4235out_err:
John Fastabende5a55a82012-10-24 08:12:57 +00004236 rcu_read_unlock();
4237 cb->args[0] = idx;
4238
David Ahernf6c57752017-05-15 23:19:17 -07004239 return err;
John Fastabende5a55a82012-10-24 08:12:57 +00004240}
4241
John Fastabend2469ffd2012-10-24 08:13:03 +00004242static inline size_t bridge_nlmsg_size(void)
4243{
4244 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4245 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4246 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4247 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
4248 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
4249 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
4250 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
4251 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
4252 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
4253 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
4254 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
4255}
4256
Roopa Prabhu02dba432015-01-14 20:02:25 -08004257static int rtnl_bridge_notify(struct net_device *dev)
John Fastabend2469ffd2012-10-24 08:13:03 +00004258{
4259 struct net *net = dev_net(dev);
John Fastabend2469ffd2012-10-24 08:13:03 +00004260 struct sk_buff *skb;
4261 int err = -EOPNOTSUPP;
4262
Roopa Prabhu02dba432015-01-14 20:02:25 -08004263 if (!dev->netdev_ops->ndo_bridge_getlink)
4264 return 0;
4265
John Fastabend2469ffd2012-10-24 08:13:03 +00004266 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
4267 if (!skb) {
4268 err = -ENOMEM;
4269 goto errout;
4270 }
4271
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02004272 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
Roopa Prabhu02dba432015-01-14 20:02:25 -08004273 if (err < 0)
4274 goto errout;
John Fastabend2469ffd2012-10-24 08:13:03 +00004275
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08004276 if (!skb->len)
4277 goto errout;
4278
John Fastabend2469ffd2012-10-24 08:13:03 +00004279 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
4280 return 0;
4281errout:
4282 WARN_ON(err == -EMSGSIZE);
4283 kfree_skb(skb);
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08004284 if (err)
4285 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
John Fastabend2469ffd2012-10-24 08:13:03 +00004286 return err;
4287}
4288
David Ahernc21ef3e2017-04-16 09:48:24 -07004289static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
4290 struct netlink_ext_ack *extack)
John Fastabende5a55a82012-10-24 08:12:57 +00004291{
4292 struct net *net = sock_net(skb->sk);
4293 struct ifinfomsg *ifm;
4294 struct net_device *dev;
John Fastabend2469ffd2012-10-24 08:13:03 +00004295 struct nlattr *br_spec, *attr = NULL;
4296 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02004297 u16 flags = 0;
John Fastabendc38e01b2012-11-02 16:32:36 +00004298 bool have_flags = false;
John Fastabende5a55a82012-10-24 08:12:57 +00004299
4300 if (nlmsg_len(nlh) < sizeof(*ifm))
4301 return -EINVAL;
4302
4303 ifm = nlmsg_data(nlh);
4304 if (ifm->ifi_family != AF_BRIDGE)
4305 return -EPFNOSUPPORT;
4306
4307 dev = __dev_get_by_index(net, ifm->ifi_index);
4308 if (!dev) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004309 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabende5a55a82012-10-24 08:12:57 +00004310 return -ENODEV;
4311 }
4312
John Fastabend2469ffd2012-10-24 08:13:03 +00004313 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4314 if (br_spec) {
4315 nla_for_each_nested(attr, br_spec, rem) {
4316 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01004317 if (nla_len(attr) < sizeof(flags))
4318 return -EINVAL;
4319
John Fastabendc38e01b2012-11-02 16:32:36 +00004320 have_flags = true;
John Fastabend2469ffd2012-10-24 08:13:03 +00004321 flags = nla_get_u16(attr);
4322 break;
4323 }
4324 }
4325 }
4326
4327 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00004328 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4329
4330 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
John Fastabend2469ffd2012-10-24 08:13:03 +00004331 err = -EOPNOTSUPP;
4332 goto out;
4333 }
4334
Petr Machata2fd527b2018-12-12 17:02:48 +00004335 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags,
4336 extack);
John Fastabende5a55a82012-10-24 08:12:57 +00004337 if (err)
4338 goto out;
John Fastabend2469ffd2012-10-24 08:13:03 +00004339
4340 flags &= ~BRIDGE_FLAGS_MASTER;
John Fastabende5a55a82012-10-24 08:12:57 +00004341 }
4342
John Fastabend2469ffd2012-10-24 08:13:03 +00004343 if ((flags & BRIDGE_FLAGS_SELF)) {
4344 if (!dev->netdev_ops->ndo_bridge_setlink)
4345 err = -EOPNOTSUPP;
4346 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08004347 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
Petr Machata2fd527b2018-12-12 17:02:48 +00004348 flags,
4349 extack);
Roopa Prabhu02dba432015-01-14 20:02:25 -08004350 if (!err) {
John Fastabend2469ffd2012-10-24 08:13:03 +00004351 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08004352
4353 /* Generate event to notify upper layer of bridge
4354 * change
4355 */
4356 err = rtnl_bridge_notify(dev);
4357 }
John Fastabend2469ffd2012-10-24 08:13:03 +00004358 }
4359
John Fastabendc38e01b2012-11-02 16:32:36 +00004360 if (have_flags)
John Fastabend2469ffd2012-10-24 08:13:03 +00004361 memcpy(nla_data(attr), &flags, sizeof(flags));
John Fastabende5a55a82012-10-24 08:12:57 +00004362out:
4363 return err;
4364}
4365
David Ahernc21ef3e2017-04-16 09:48:24 -07004366static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
4367 struct netlink_ext_ack *extack)
Vlad Yasevich407af322013-02-13 12:00:12 +00004368{
4369 struct net *net = sock_net(skb->sk);
4370 struct ifinfomsg *ifm;
4371 struct net_device *dev;
4372 struct nlattr *br_spec, *attr = NULL;
4373 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02004374 u16 flags = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +00004375 bool have_flags = false;
4376
4377 if (nlmsg_len(nlh) < sizeof(*ifm))
4378 return -EINVAL;
4379
4380 ifm = nlmsg_data(nlh);
4381 if (ifm->ifi_family != AF_BRIDGE)
4382 return -EPFNOSUPPORT;
4383
4384 dev = __dev_get_by_index(net, ifm->ifi_index);
4385 if (!dev) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004386 NL_SET_ERR_MSG(extack, "unknown ifindex");
Vlad Yasevich407af322013-02-13 12:00:12 +00004387 return -ENODEV;
4388 }
4389
4390 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4391 if (br_spec) {
4392 nla_for_each_nested(attr, br_spec, rem) {
4393 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01004394 if (nla_len(attr) < sizeof(flags))
4395 return -EINVAL;
4396
Vlad Yasevich407af322013-02-13 12:00:12 +00004397 have_flags = true;
4398 flags = nla_get_u16(attr);
4399 break;
4400 }
4401 }
4402 }
4403
Vlad Yasevich407af322013-02-13 12:00:12 +00004404 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
4405 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4406
4407 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
4408 err = -EOPNOTSUPP;
4409 goto out;
4410 }
4411
Roopa Prabhuadd511b2015-01-29 22:40:12 -08004412 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00004413 if (err)
4414 goto out;
4415
4416 flags &= ~BRIDGE_FLAGS_MASTER;
4417 }
4418
4419 if ((flags & BRIDGE_FLAGS_SELF)) {
4420 if (!dev->netdev_ops->ndo_bridge_dellink)
4421 err = -EOPNOTSUPP;
4422 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08004423 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
4424 flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00004425
Roopa Prabhu02dba432015-01-14 20:02:25 -08004426 if (!err) {
Vlad Yasevich407af322013-02-13 12:00:12 +00004427 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08004428
4429 /* Generate event to notify upper layer of bridge
4430 * change
4431 */
4432 err = rtnl_bridge_notify(dev);
4433 }
Vlad Yasevich407af322013-02-13 12:00:12 +00004434 }
4435
4436 if (have_flags)
4437 memcpy(nla_data(attr), &flags, sizeof(flags));
Vlad Yasevich407af322013-02-13 12:00:12 +00004438out:
4439 return err;
4440}
4441
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004442static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
4443{
4444 return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
4445 (!idxattr || idxattr == attrid);
4446}
4447
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004448#define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
4449static int rtnl_get_offload_stats_attr_size(int attr_id)
4450{
4451 switch (attr_id) {
4452 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
4453 return sizeof(struct rtnl_link_stats64);
4454 }
4455
4456 return 0;
4457}
4458
4459static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
4460 int *prividx)
4461{
4462 struct nlattr *attr = NULL;
4463 int attr_id, size;
4464 void *attr_data;
4465 int err;
4466
4467 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
4468 dev->netdev_ops->ndo_get_offload_stats))
4469 return -ENODATA;
4470
4471 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
4472 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
4473 if (attr_id < *prividx)
4474 continue;
4475
4476 size = rtnl_get_offload_stats_attr_size(attr_id);
4477 if (!size)
4478 continue;
4479
Or Gerlitz3df5b3c2016-11-22 23:09:54 +02004480 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004481 continue;
4482
4483 attr = nla_reserve_64bit(skb, attr_id, size,
4484 IFLA_OFFLOAD_XSTATS_UNSPEC);
4485 if (!attr)
4486 goto nla_put_failure;
4487
4488 attr_data = nla_data(attr);
4489 memset(attr_data, 0, size);
4490 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
4491 attr_data);
4492 if (err)
4493 goto get_offload_stats_failure;
4494 }
4495
4496 if (!attr)
4497 return -ENODATA;
4498
4499 *prividx = 0;
4500 return 0;
4501
4502nla_put_failure:
4503 err = -EMSGSIZE;
4504get_offload_stats_failure:
4505 *prividx = attr_id;
4506 return err;
4507}
4508
4509static int rtnl_get_offload_stats_size(const struct net_device *dev)
4510{
4511 int nla_size = 0;
4512 int attr_id;
4513 int size;
4514
4515 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
4516 dev->netdev_ops->ndo_get_offload_stats))
4517 return 0;
4518
4519 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
4520 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
Or Gerlitz3df5b3c2016-11-22 23:09:54 +02004521 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004522 continue;
4523 size = rtnl_get_offload_stats_attr_size(attr_id);
4524 nla_size += nla_total_size_64bit(size);
4525 }
4526
4527 if (nla_size != 0)
4528 nla_size += nla_total_size(0);
4529
4530 return nla_size;
4531}
4532
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004533static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
4534 int type, u32 pid, u32 seq, u32 change,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004535 unsigned int flags, unsigned int filter_mask,
4536 int *idxattr, int *prividx)
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004537{
4538 struct if_stats_msg *ifsm;
4539 struct nlmsghdr *nlh;
4540 struct nlattr *attr;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004541 int s_prividx = *prividx;
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004542 int err;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004543
4544 ASSERT_RTNL();
4545
4546 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
4547 if (!nlh)
4548 return -EMSGSIZE;
4549
4550 ifsm = nlmsg_data(nlh);
Nikolay Aleksandrovce024f42017-10-03 13:20:48 +03004551 ifsm->family = PF_UNSPEC;
4552 ifsm->pad1 = 0;
4553 ifsm->pad2 = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004554 ifsm->ifindex = dev->ifindex;
4555 ifsm->filter_mask = filter_mask;
4556
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004557 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004558 struct rtnl_link_stats64 *sp;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004559
Nicolas Dichtel58414d32016-04-21 18:58:25 +02004560 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
4561 sizeof(struct rtnl_link_stats64),
4562 IFLA_STATS_UNSPEC);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004563 if (!attr)
4564 goto nla_put_failure;
4565
4566 sp = nla_data(attr);
4567 dev_get_stats(dev, sp);
4568 }
4569
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004570 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
4571 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
4572
4573 if (ops && ops->fill_linkxstats) {
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004574 *idxattr = IFLA_STATS_LINK_XSTATS;
4575 attr = nla_nest_start(skb,
4576 IFLA_STATS_LINK_XSTATS);
4577 if (!attr)
4578 goto nla_put_failure;
4579
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004580 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
4581 nla_nest_end(skb, attr);
4582 if (err)
4583 goto nla_put_failure;
4584 *idxattr = 0;
4585 }
4586 }
4587
4588 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
4589 *idxattr)) {
4590 const struct rtnl_link_ops *ops = NULL;
4591 const struct net_device *master;
4592
4593 master = netdev_master_upper_dev_get(dev);
4594 if (master)
4595 ops = master->rtnl_link_ops;
4596 if (ops && ops->fill_linkxstats) {
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004597 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
4598 attr = nla_nest_start(skb,
4599 IFLA_STATS_LINK_XSTATS_SLAVE);
4600 if (!attr)
4601 goto nla_put_failure;
4602
4603 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004604 nla_nest_end(skb, attr);
4605 if (err)
4606 goto nla_put_failure;
4607 *idxattr = 0;
4608 }
4609 }
4610
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004611 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
4612 *idxattr)) {
4613 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
4614 attr = nla_nest_start(skb, IFLA_STATS_LINK_OFFLOAD_XSTATS);
4615 if (!attr)
4616 goto nla_put_failure;
4617
4618 err = rtnl_get_offload_stats(skb, dev, prividx);
4619 if (err == -ENODATA)
4620 nla_nest_cancel(skb, attr);
4621 else
4622 nla_nest_end(skb, attr);
4623
4624 if (err && err != -ENODATA)
4625 goto nla_put_failure;
4626 *idxattr = 0;
4627 }
4628
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004629 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
4630 struct rtnl_af_ops *af_ops;
4631
4632 *idxattr = IFLA_STATS_AF_SPEC;
4633 attr = nla_nest_start(skb, IFLA_STATS_AF_SPEC);
4634 if (!attr)
4635 goto nla_put_failure;
4636
Florian Westphal5fa85a02017-10-16 15:44:36 +02004637 rcu_read_lock();
4638 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004639 if (af_ops->fill_stats_af) {
4640 struct nlattr *af;
4641 int err;
4642
4643 af = nla_nest_start(skb, af_ops->family);
Florian Westphal5fa85a02017-10-16 15:44:36 +02004644 if (!af) {
4645 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004646 goto nla_put_failure;
Florian Westphal5fa85a02017-10-16 15:44:36 +02004647 }
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004648 err = af_ops->fill_stats_af(skb, dev);
4649
Florian Westphal5fa85a02017-10-16 15:44:36 +02004650 if (err == -ENODATA) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004651 nla_nest_cancel(skb, af);
Florian Westphal5fa85a02017-10-16 15:44:36 +02004652 } else if (err < 0) {
4653 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004654 goto nla_put_failure;
Florian Westphal5fa85a02017-10-16 15:44:36 +02004655 }
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004656
4657 nla_nest_end(skb, af);
4658 }
4659 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02004660 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004661
4662 nla_nest_end(skb, attr);
4663
4664 *idxattr = 0;
4665 }
4666
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004667 nlmsg_end(skb, nlh);
4668
4669 return 0;
4670
4671nla_put_failure:
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004672 /* not a multi message or no progress mean a real error */
4673 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
4674 nlmsg_cancel(skb, nlh);
4675 else
4676 nlmsg_end(skb, nlh);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004677
4678 return -EMSGSIZE;
4679}
4680
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004681static size_t if_nlmsg_stats_size(const struct net_device *dev,
4682 u32 filter_mask)
4683{
4684 size_t size = 0;
4685
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004686 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004687 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
4688
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004689 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
4690 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004691 int attr = IFLA_STATS_LINK_XSTATS;
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004692
4693 if (ops && ops->get_linkxstats_size) {
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004694 size += nla_total_size(ops->get_linkxstats_size(dev,
4695 attr));
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004696 /* for IFLA_STATS_LINK_XSTATS */
4697 size += nla_total_size(0);
4698 }
4699 }
4700
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004701 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
4702 struct net_device *_dev = (struct net_device *)dev;
4703 const struct rtnl_link_ops *ops = NULL;
4704 const struct net_device *master;
4705
4706 /* netdev_master_upper_dev_get can't take const */
4707 master = netdev_master_upper_dev_get(_dev);
4708 if (master)
4709 ops = master->rtnl_link_ops;
4710 if (ops && ops->get_linkxstats_size) {
4711 int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
4712
4713 size += nla_total_size(ops->get_linkxstats_size(dev,
4714 attr));
4715 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
4716 size += nla_total_size(0);
4717 }
4718 }
4719
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004720 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
4721 size += rtnl_get_offload_stats_size(dev);
4722
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004723 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
4724 struct rtnl_af_ops *af_ops;
4725
4726 /* for IFLA_STATS_AF_SPEC */
4727 size += nla_total_size(0);
4728
Florian Westphal5fa85a02017-10-16 15:44:36 +02004729 rcu_read_lock();
4730 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004731 if (af_ops->get_stats_af_size) {
4732 size += nla_total_size(
4733 af_ops->get_stats_af_size(dev));
4734
4735 /* for AF_* */
4736 size += nla_total_size(0);
4737 }
4738 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02004739 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004740 }
4741
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004742 return size;
4743}
4744
David Ahernc21ef3e2017-04-16 09:48:24 -07004745static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
4746 struct netlink_ext_ack *extack)
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004747{
4748 struct net *net = sock_net(skb->sk);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004749 struct net_device *dev = NULL;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004750 int idxattr = 0, prividx = 0;
4751 struct if_stats_msg *ifsm;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004752 struct sk_buff *nskb;
4753 u32 filter_mask;
4754 int err;
4755
Mathias Krause4775cc12016-12-28 17:52:15 +01004756 if (nlmsg_len(nlh) < sizeof(*ifsm))
4757 return -EINVAL;
4758
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004759 ifsm = nlmsg_data(nlh);
4760 if (ifsm->ifindex > 0)
4761 dev = __dev_get_by_index(net, ifsm->ifindex);
4762 else
4763 return -EINVAL;
4764
4765 if (!dev)
4766 return -ENODEV;
4767
4768 filter_mask = ifsm->filter_mask;
4769 if (!filter_mask)
4770 return -EINVAL;
4771
4772 nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
4773 if (!nskb)
4774 return -ENOBUFS;
4775
4776 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
4777 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004778 0, filter_mask, &idxattr, &prividx);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004779 if (err < 0) {
4780 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
4781 WARN_ON(err == -EMSGSIZE);
4782 kfree_skb(nskb);
4783 } else {
4784 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
4785 }
4786
4787 return err;
4788}
4789
4790static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
4791{
David Ahern841891e2018-10-07 20:16:32 -07004792 struct netlink_ext_ack *extack = cb->extack;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004793 int h, s_h, err, s_idx, s_idxattr, s_prividx;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004794 struct net *net = sock_net(skb->sk);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004795 unsigned int flags = NLM_F_MULTI;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004796 struct if_stats_msg *ifsm;
4797 struct hlist_head *head;
4798 struct net_device *dev;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004799 u32 filter_mask = 0;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004800 int idx = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004801
4802 s_h = cb->args[0];
4803 s_idx = cb->args[1];
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004804 s_idxattr = cb->args[2];
4805 s_prividx = cb->args[3];
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004806
4807 cb->seq = net->dev_base_seq;
4808
David Ahern841891e2018-10-07 20:16:32 -07004809 if (nlmsg_len(cb->nlh) < sizeof(*ifsm)) {
4810 NL_SET_ERR_MSG(extack, "Invalid header for stats dump");
Mathias Krause4775cc12016-12-28 17:52:15 +01004811 return -EINVAL;
David Ahern841891e2018-10-07 20:16:32 -07004812 }
Mathias Krause4775cc12016-12-28 17:52:15 +01004813
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004814 ifsm = nlmsg_data(cb->nlh);
David Ahern841891e2018-10-07 20:16:32 -07004815
David Aherne75fa072018-10-08 13:58:07 -07004816 /* only requests using strict checks can pass data to influence
4817 * the dump. The legacy exception is filter_mask.
David Ahern841891e2018-10-07 20:16:32 -07004818 */
4819 if (cb->strict_check) {
4820 if (ifsm->pad1 || ifsm->pad2 || ifsm->ifindex) {
4821 NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request");
4822 return -EINVAL;
4823 }
4824 if (nlmsg_attrlen(cb->nlh, sizeof(*ifsm))) {
4825 NL_SET_ERR_MSG(extack, "Invalid attributes after stats header");
4826 return -EINVAL;
4827 }
4828 }
4829
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004830 filter_mask = ifsm->filter_mask;
David Ahern841891e2018-10-07 20:16:32 -07004831 if (!filter_mask) {
4832 NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump");
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004833 return -EINVAL;
David Ahern841891e2018-10-07 20:16:32 -07004834 }
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004835
4836 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4837 idx = 0;
4838 head = &net->dev_index_head[h];
4839 hlist_for_each_entry(dev, head, index_hlist) {
4840 if (idx < s_idx)
4841 goto cont;
4842 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
4843 NETLINK_CB(cb->skb).portid,
4844 cb->nlh->nlmsg_seq, 0,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004845 flags, filter_mask,
4846 &s_idxattr, &s_prividx);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004847 /* If we ran out of room on the first message,
4848 * we're in trouble
4849 */
4850 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
4851
4852 if (err < 0)
4853 goto out;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004854 s_prividx = 0;
4855 s_idxattr = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004856 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
4857cont:
4858 idx++;
4859 }
4860 }
4861out:
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004862 cb->args[3] = s_prividx;
4863 cb->args[2] = s_idxattr;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004864 cb->args[1] = idx;
4865 cb->args[0] = h;
4866
4867 return skb->len;
4868}
4869
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870/* Process one rtnetlink message. */
4871
Johannes Berg2d4bc932017-04-12 14:34:04 +02004872static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
4873 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004875 struct net *net = sock_net(skb->sk);
Florian Westphaladdf9b92017-12-02 21:44:05 +01004876 struct rtnl_link *link;
Florian Westphale4202512017-12-02 21:44:06 +01004877 struct module *owner;
Florian Westphal6853dd42017-08-09 20:41:51 +02004878 int err = -EOPNOTSUPP;
Thomas Grafe2849862007-03-22 11:48:11 -07004879 rtnl_doit_func doit;
Florian Westphal62256f92017-08-09 20:41:52 +02004880 unsigned int flags;
Alexander Kuleshov617cfc72016-01-10 21:26:57 +06004881 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 int family;
4883 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 if (type > RTM_MAX)
Thomas Graf038890f2007-04-05 14:35:52 -07004887 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888
4889 type -= RTM_BASE;
4890
4891 /* All the messages must have at least 1 byte length */
Hong zhi guo573ce262013-03-27 06:47:04 +00004892 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 return 0;
4894
Hong zhi guo573ce262013-03-27 06:47:04 +00004895 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 kind = type&3;
4897
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07004898 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07004899 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900
Florian Westphal6853dd42017-08-09 20:41:51 +02004901 rcu_read_lock();
David S. Millerb8f3ab42011-01-18 12:40:38 -08004902 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004903 struct sock *rtnl;
Thomas Grafe2849862007-03-22 11:48:11 -07004904 rtnl_dumpit_func dumpit;
Greg Rosec7ac8672011-06-10 01:27:09 +00004905 u16 min_dump_alloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906
Florian Westphaladdf9b92017-12-02 21:44:05 +01004907 link = rtnl_get_link(family, type);
4908 if (!link || !link->dumpit) {
Florian Westphal6853dd42017-08-09 20:41:51 +02004909 family = PF_UNSPEC;
Florian Westphaladdf9b92017-12-02 21:44:05 +01004910 link = rtnl_get_link(family, type);
4911 if (!link || !link->dumpit)
Florian Westphal6853dd42017-08-09 20:41:51 +02004912 goto err_unlock;
4913 }
Florian Westphale4202512017-12-02 21:44:06 +01004914 owner = link->owner;
Florian Westphaladdf9b92017-12-02 21:44:05 +01004915 dumpit = link->dumpit;
Florian Westphale1fa6d22017-08-09 20:41:47 +02004916
Florian Westphal5c2bb9b2017-08-10 16:52:58 +02004917 if (type == RTM_GETLINK - RTM_BASE)
Florian Westphale1fa6d22017-08-09 20:41:47 +02004918 min_dump_alloc = rtnl_calcit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919
Florian Westphale4202512017-12-02 21:44:06 +01004920 err = 0;
4921 /* need to do this before rcu_read_unlock() */
4922 if (!try_module_get(owner))
4923 err = -EPROTONOSUPPORT;
4924
Florian Westphal6853dd42017-08-09 20:41:51 +02004925 rcu_read_unlock();
4926
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004927 rtnl = net->rtnl;
Florian Westphale4202512017-12-02 21:44:06 +01004928 if (err == 0) {
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00004929 struct netlink_dump_control c = {
4930 .dump = dumpit,
4931 .min_dump_alloc = min_dump_alloc,
Florian Westphale4202512017-12-02 21:44:06 +01004932 .module = owner,
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00004933 };
4934 err = netlink_dump_start(rtnl, skb, nlh, &c);
Florian Westphale4202512017-12-02 21:44:06 +01004935 /* netlink_dump_start() will keep a reference on
4936 * module if dump is still in progress.
4937 */
4938 module_put(owner);
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00004939 }
Eric Dumazet2907c352011-05-25 07:34:04 +00004940 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 }
4942
Florian Westphaladdf9b92017-12-02 21:44:05 +01004943 link = rtnl_get_link(family, type);
4944 if (!link || !link->doit) {
Florian Westphal8caa38b2017-08-10 16:53:01 +02004945 family = PF_UNSPEC;
Florian Westphaladdf9b92017-12-02 21:44:05 +01004946 link = rtnl_get_link(PF_UNSPEC, type);
4947 if (!link || !link->doit)
4948 goto out_unlock;
Florian Westphal8caa38b2017-08-10 16:53:01 +02004949 }
4950
Florian Westphale4202512017-12-02 21:44:06 +01004951 owner = link->owner;
4952 if (!try_module_get(owner)) {
4953 err = -EPROTONOSUPPORT;
4954 goto out_unlock;
4955 }
4956
Florian Westphaladdf9b92017-12-02 21:44:05 +01004957 flags = link->flags;
Florian Westphal62256f92017-08-09 20:41:52 +02004958 if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
Florian Westphaladdf9b92017-12-02 21:44:05 +01004959 doit = link->doit;
Florian Westphal62256f92017-08-09 20:41:52 +02004960 rcu_read_unlock();
4961 if (doit)
4962 err = doit(skb, nlh, extack);
Florian Westphale4202512017-12-02 21:44:06 +01004963 module_put(owner);
Florian Westphal62256f92017-08-09 20:41:52 +02004964 return err;
4965 }
Florian Westphal6853dd42017-08-09 20:41:51 +02004966 rcu_read_unlock();
4967
Florian Westphal0cc09022017-08-09 20:41:50 +02004968 rtnl_lock();
Florian Westphaladdf9b92017-12-02 21:44:05 +01004969 link = rtnl_get_link(family, type);
4970 if (link && link->doit)
4971 err = link->doit(skb, nlh, extack);
Florian Westphal0cc09022017-08-09 20:41:50 +02004972 rtnl_unlock();
Florian Westphaladdf9b92017-12-02 21:44:05 +01004973
Florian Westphale4202512017-12-02 21:44:06 +01004974 module_put(owner);
4975
Florian Westphaladdf9b92017-12-02 21:44:05 +01004976 return err;
4977
4978out_unlock:
4979 rcu_read_unlock();
Florian Westphal0cc09022017-08-09 20:41:50 +02004980 return err;
4981
4982err_unlock:
Florian Westphal6853dd42017-08-09 20:41:51 +02004983 rcu_read_unlock();
Florian Westphal0cc09022017-08-09 20:41:50 +02004984 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985}
4986
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07004987static void rtnetlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07004989 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990}
4991
Julien Gomes5f729ea2017-06-20 13:54:16 -07004992static int rtnetlink_bind(struct net *net, int group)
4993{
4994 switch (group) {
4995 case RTNLGRP_IPV4_MROUTE_R:
4996 case RTNLGRP_IPV6_MROUTE_R:
4997 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
4998 return -EPERM;
4999 break;
5000 }
5001 return 0;
5002}
5003
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
5005{
Jiri Pirko351638e2013-05-28 01:30:21 +00005006 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02005007
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008 switch (event) {
Vlad Yasevich5138e862017-04-04 09:23:41 -04005009 case NETDEV_REBOOT:
Xin Long8a212582017-10-15 18:13:41 +08005010 case NETDEV_CHANGEMTU:
David Ahern37536542017-07-19 10:22:40 -07005011 case NETDEV_CHANGEADDR:
Vlad Yasevich5138e862017-04-04 09:23:41 -04005012 case NETDEV_CHANGENAME:
5013 case NETDEV_FEAT_CHANGE:
5014 case NETDEV_BONDING_FAILOVER:
Xin Longe6e66592017-10-15 18:13:43 +08005015 case NETDEV_POST_TYPE_CHANGE:
Vlad Yasevich5138e862017-04-04 09:23:41 -04005016 case NETDEV_NOTIFY_PEERS:
Xin Longdc709f32017-10-15 18:13:44 +08005017 case NETDEV_CHANGEUPPER:
Vlad Yasevich5138e862017-04-04 09:23:41 -04005018 case NETDEV_RESEND_IGMP:
Vlad Yasevich5138e862017-04-04 09:23:41 -04005019 case NETDEV_CHANGEINFODATA:
Xin Longeeda3fb2017-10-24 13:54:19 +08005020 case NETDEV_CHANGELOWERSTATE:
Xin Longebdcf042017-10-15 18:13:42 +08005021 case NETDEV_CHANGE_TX_QUEUE_LEN:
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04005022 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01005023 GFP_KERNEL, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024 break;
5025 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 break;
5027 }
5028 return NOTIFY_DONE;
5029}
5030
5031static struct notifier_block rtnetlink_dev_notifier = {
5032 .notifier_call = rtnetlink_event,
5033};
5034
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005035
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005036static int __net_init rtnetlink_net_init(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005037{
5038 struct sock *sk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00005039 struct netlink_kernel_cfg cfg = {
5040 .groups = RTNLGRP_MAX,
5041 .input = rtnetlink_rcv,
5042 .cb_mutex = &rtnl_mutex,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00005043 .flags = NL_CFG_F_NONROOT_RECV,
Julien Gomes5f729ea2017-06-20 13:54:16 -07005044 .bind = rtnetlink_bind,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00005045 };
5046
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00005047 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005048 if (!sk)
5049 return -ENOMEM;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005050 net->rtnl = sk;
5051 return 0;
5052}
5053
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005054static void __net_exit rtnetlink_net_exit(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005055{
Denis V. Lunev775516b2008-01-18 23:55:19 -08005056 netlink_kernel_release(net->rtnl);
5057 net->rtnl = NULL;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005058}
5059
5060static struct pernet_operations rtnetlink_net_ops = {
5061 .init = rtnetlink_net_init,
5062 .exit = rtnetlink_net_exit,
5063};
5064
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065void __init rtnetlink_init(void)
5066{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005067 if (register_pernet_subsys(&rtnetlink_net_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 panic("rtnetlink_init: cannot initialize rtnetlink\n");
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005069
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070 register_netdevice_notifier(&rtnetlink_dev_notifier);
Thomas Graf340d17f2007-03-22 11:49:22 -07005071
Greg Rosec7ac8672011-06-10 01:27:09 +00005072 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
Florian Westphalb97bac62017-08-09 20:41:48 +02005073 rtnl_dump_ifinfo, 0);
5074 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
5075 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
5076 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
Thomas Graf687ad8c2007-03-22 11:59:42 -07005077
Florian Westphalb97bac62017-08-09 20:41:48 +02005078 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
5079 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
5080 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
John Fastabend77162022012-04-15 06:43:56 +00005081
Florian Westphalb97bac62017-08-09 20:41:48 +02005082 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
5083 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
5084 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, 0);
John Fastabende5a55a82012-10-24 08:12:57 +00005085
Florian Westphalb97bac62017-08-09 20:41:48 +02005086 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
5087 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
5088 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005089
5090 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
Florian Westphalb97bac62017-08-09 20:41:48 +02005091 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092}