blob: a498bb41c9aa9b8976eb8d87f71489695cb019f2 [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 Westphal070cbf52017-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 Westphal070cbf52017-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 Westphal070cbf52017-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) {
Patrick McHardy3729d502010-02-26 06:34:54 +00002492 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
Johannes Berg5f9021c2008-11-16 23:20:31 -08002493 if (err < 0)
2494 goto errout;
Patrick McHardy0157f602007-06-13 12:03:36 -07002495 }
2496
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002497 if (tb[IFLA_MASTER]) {
David Ahern33eaf2a2017-10-04 17:48:46 -07002498 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002499 if (err)
2500 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002501 status |= DO_SETLINK_MODIFIED;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002502 }
2503
Jiri Pirko9a572472012-12-27 23:49:39 +00002504 if (tb[IFLA_CARRIER]) {
2505 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2506 if (err)
2507 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002508 status |= DO_SETLINK_MODIFIED;
Jiri Pirko9a572472012-12-27 23:49:39 +00002509 }
2510
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002511 if (tb[IFLA_TXQLEN]) {
Alexey Dobriyan0cd29502017-05-17 13:30:44 +03002512 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002513
Cong Wang6a643dd2018-01-25 18:26:22 -08002514 err = dev_change_tx_queue_len(dev, value);
2515 if (err)
2516 goto errout;
2517 status |= DO_SETLINK_MODIFIED;
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002518 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002519
Stephen Hemminger46e6b992017-12-07 15:40:19 -08002520 if (tb[IFLA_GSO_MAX_SIZE]) {
2521 u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]);
2522
2523 if (max_size > GSO_MAX_SIZE) {
2524 err = -EINVAL;
2525 goto errout;
2526 }
2527
2528 if (dev->gso_max_size ^ max_size) {
2529 netif_set_gso_max_size(dev, max_size);
2530 status |= DO_SETLINK_MODIFIED;
2531 }
2532 }
2533
2534 if (tb[IFLA_GSO_MAX_SEGS]) {
2535 u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
2536
2537 if (max_segs > GSO_MAX_SEGS) {
2538 err = -EINVAL;
2539 goto errout;
2540 }
2541
2542 if (dev->gso_max_segs ^ max_segs) {
2543 dev->gso_max_segs = max_segs;
2544 status |= DO_SETLINK_MODIFIED;
2545 }
2546 }
2547
Patrick McHardy0157f602007-06-13 12:03:36 -07002548 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002549 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Patrick McHardy0157f602007-06-13 12:03:36 -07002550
2551 if (tb[IFLA_LINKMODE]) {
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002552 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2553
David S. Miller93b2d4a2008-02-17 18:35:07 -08002554 write_lock_bh(&dev_base_lock);
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002555 if (dev->link_mode ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02002556 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002557 dev->link_mode = value;
David S. Miller93b2d4a2008-02-17 18:35:07 -08002558 write_unlock_bh(&dev_base_lock);
Patrick McHardy0157f602007-06-13 12:03:36 -07002559 }
2560
Chris Wrightc02db8c2010-05-16 01:05:45 -07002561 if (tb[IFLA_VFINFO_LIST]) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002562 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
Chris Wrightc02db8c2010-05-16 01:05:45 -07002563 struct nlattr *attr;
2564 int rem;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002565
Chris Wrightc02db8c2010-05-16 01:05:45 -07002566 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002567 if (nla_type(attr) != IFLA_VF_INFO ||
2568 nla_len(attr) < NLA_HDRLEN) {
David Howells253683b2010-05-21 02:25:27 +00002569 err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002570 goto errout;
David Howells253683b2010-05-21 02:25:27 +00002571 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002572 err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr,
Johannes Bergfceb6432017-04-12 14:34:07 +02002573 ifla_vf_policy, NULL);
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002574 if (err < 0)
2575 goto errout;
2576 err = do_setvfinfo(dev, vfinfo);
Chris Wrightc02db8c2010-05-16 01:05:45 -07002577 if (err < 0)
2578 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002579 status |= DO_SETLINK_NOTIFY;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002580 }
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00002581 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002582 err = 0;
2583
Scott Feldman57b61082010-05-17 22:49:55 -07002584 if (tb[IFLA_VF_PORTS]) {
2585 struct nlattr *port[IFLA_PORT_MAX+1];
2586 struct nlattr *attr;
2587 int vf;
2588 int rem;
2589
2590 err = -EOPNOTSUPP;
2591 if (!ops->ndo_set_vf_port)
2592 goto errout;
2593
2594 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
Daniel Borkmann035d2102015-07-13 00:06:02 +02002595 if (nla_type(attr) != IFLA_VF_PORT ||
2596 nla_len(attr) < NLA_HDRLEN) {
2597 err = -EINVAL;
2598 goto errout;
2599 }
2600 err = nla_parse_nested(port, IFLA_PORT_MAX, attr,
Johannes Bergfceb6432017-04-12 14:34:07 +02002601 ifla_port_policy, NULL);
Scott Feldman57b61082010-05-17 22:49:55 -07002602 if (err < 0)
2603 goto errout;
2604 if (!port[IFLA_PORT_VF]) {
2605 err = -EOPNOTSUPP;
2606 goto errout;
2607 }
2608 vf = nla_get_u32(port[IFLA_PORT_VF]);
2609 err = ops->ndo_set_vf_port(dev, vf, port);
2610 if (err < 0)
2611 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002612 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07002613 }
2614 }
2615 err = 0;
2616
2617 if (tb[IFLA_PORT_SELF]) {
2618 struct nlattr *port[IFLA_PORT_MAX+1];
2619
2620 err = nla_parse_nested(port, IFLA_PORT_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +02002621 tb[IFLA_PORT_SELF], ifla_port_policy,
2622 NULL);
Scott Feldman57b61082010-05-17 22:49:55 -07002623 if (err < 0)
2624 goto errout;
2625
2626 err = -EOPNOTSUPP;
2627 if (ops->ndo_set_vf_port)
2628 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
2629 if (err < 0)
2630 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002631 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07002632 }
Thomas Graff8ff1822010-11-16 04:30:14 +00002633
2634 if (tb[IFLA_AF_SPEC]) {
2635 struct nlattr *af;
2636 int rem;
2637
2638 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2639 const struct rtnl_af_ops *af_ops;
2640
Florian Westphal5fa85a02017-10-16 15:44:36 +02002641 rcu_read_lock();
2642
Gustavo A. R. Silva058c8d52017-10-20 19:43:11 -05002643 BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
Thomas Graff8ff1822010-11-16 04:30:14 +00002644
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002645 err = af_ops->set_link_af(dev, af);
Florian Westphal5fa85a02017-10-16 15:44:36 +02002646 if (err < 0) {
2647 rcu_read_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +00002648 goto errout;
Florian Westphal5fa85a02017-10-16 15:44:36 +02002649 }
Thomas Graff8ff1822010-11-16 04:30:14 +00002650
Florian Westphal5fa85a02017-10-16 15:44:36 +02002651 rcu_read_unlock();
Nicolas Dichtelba998902014-09-01 16:07:29 +02002652 status |= DO_SETLINK_NOTIFY;
Thomas Graff8ff1822010-11-16 04:30:14 +00002653 }
2654 }
Scott Feldman57b61082010-05-17 22:49:55 -07002655 err = 0;
2656
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07002657 if (tb[IFLA_PROTO_DOWN]) {
2658 err = dev_change_proto_down(dev,
2659 nla_get_u8(tb[IFLA_PROTO_DOWN]));
2660 if (err)
2661 goto errout;
2662 status |= DO_SETLINK_NOTIFY;
2663 }
2664
Brenden Blancod1fdd912016-07-19 12:16:49 -07002665 if (tb[IFLA_XDP]) {
2666 struct nlattr *xdp[IFLA_XDP_MAX + 1];
Daniel Borkmann85de8572016-11-28 23:16:54 +01002667 u32 xdp_flags = 0;
Brenden Blancod1fdd912016-07-19 12:16:49 -07002668
2669 err = nla_parse_nested(xdp, IFLA_XDP_MAX, tb[IFLA_XDP],
Johannes Bergfceb6432017-04-12 14:34:07 +02002670 ifla_xdp_policy, NULL);
Brenden Blancod1fdd912016-07-19 12:16:49 -07002671 if (err < 0)
2672 goto errout;
2673
Martin KaFai Lau58038692017-06-15 17:29:09 -07002674 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
Brenden Blanco262d8622016-07-20 17:22:34 -07002675 err = -EINVAL;
2676 goto errout;
2677 }
Daniel Borkmann85de8572016-11-28 23:16:54 +01002678
2679 if (xdp[IFLA_XDP_FLAGS]) {
2680 xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
2681 if (xdp_flags & ~XDP_FLAGS_MASK) {
2682 err = -EINVAL;
2683 goto errout;
2684 }
Jakub Kicinskiee5d0322017-06-21 18:25:04 -07002685 if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
Daniel Borkmann0489df92017-05-12 01:04:45 +02002686 err = -EINVAL;
2687 goto errout;
2688 }
Daniel Borkmann85de8572016-11-28 23:16:54 +01002689 }
2690
Brenden Blancod1fdd912016-07-19 12:16:49 -07002691 if (xdp[IFLA_XDP_FD]) {
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002692 err = dev_change_xdp_fd(dev, extack,
Daniel Borkmann85de8572016-11-28 23:16:54 +01002693 nla_get_s32(xdp[IFLA_XDP_FD]),
2694 xdp_flags);
Brenden Blancod1fdd912016-07-19 12:16:49 -07002695 if (err)
2696 goto errout;
2697 status |= DO_SETLINK_NOTIFY;
2698 }
2699 }
2700
Patrick McHardy0157f602007-06-13 12:03:36 -07002701errout:
Nicolas Dichtelba998902014-09-01 16:07:29 +02002702 if (status & DO_SETLINK_MODIFIED) {
Xin Long64ff90c2017-10-15 18:13:45 +08002703 if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
Nicolas Dichtelba998902014-09-01 16:07:29 +02002704 netdev_state_change(dev);
2705
2706 if (err < 0)
2707 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",
2708 dev->name);
2709 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002710
Patrick McHardy0157f602007-06-13 12:03:36 -07002711 return err;
2712}
2713
David Ahernc21ef3e2017-04-16 09:48:24 -07002714static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2715 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002717 struct net *net = sock_net(skb->sk);
Thomas Grafda5e0492006-08-10 21:17:37 -07002718 struct ifinfomsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 struct net_device *dev;
Patrick McHardy0157f602007-06-13 12:03:36 -07002720 int err;
Thomas Grafda5e0492006-08-10 21:17:37 -07002721 struct nlattr *tb[IFLA_MAX+1];
2722 char ifname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723
David Ahernc21ef3e2017-04-16 09:48:24 -07002724 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy,
2725 extack);
Thomas Grafda5e0492006-08-10 21:17:37 -07002726 if (err < 0)
2727 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002729 err = rtnl_ensure_unique_netns(tb, extack, false);
2730 if (err < 0)
2731 goto errout;
2732
Thomas Graf5176f912006-08-26 20:13:18 -07002733 if (tb[IFLA_IFNAME])
2734 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
Patrick McHardy78e5b8912006-09-13 20:35:36 -07002735 else
2736 ifname[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 err = -EINVAL;
Thomas Grafda5e0492006-08-10 21:17:37 -07002739 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -07002740 if (ifm->ifi_index > 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00002741 dev = __dev_get_by_index(net, ifm->ifi_index);
Thomas Grafda5e0492006-08-10 21:17:37 -07002742 else if (tb[IFLA_IFNAME])
Eric Dumazeta3d12892009-10-21 10:59:31 +00002743 dev = __dev_get_by_name(net, ifname);
Thomas Grafda5e0492006-08-10 21:17:37 -07002744 else
2745 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746
Thomas Grafda5e0492006-08-10 21:17:37 -07002747 if (dev == NULL) {
2748 err = -ENODEV;
2749 goto errout;
2750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002752 err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
Thomas Grafda5e0492006-08-10 21:17:37 -07002753errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 return err;
2755}
2756
WANG Cong66400d52015-03-24 11:53:31 -07002757static int rtnl_group_dellink(const struct net *net, int group)
2758{
2759 struct net_device *dev, *aux;
2760 LIST_HEAD(list_kill);
2761 bool found = false;
2762
2763 if (!group)
2764 return -EPERM;
2765
2766 for_each_netdev(net, dev) {
2767 if (dev->group == group) {
2768 const struct rtnl_link_ops *ops;
2769
2770 found = true;
2771 ops = dev->rtnl_link_ops;
2772 if (!ops || !ops->dellink)
2773 return -EOPNOTSUPP;
2774 }
2775 }
2776
2777 if (!found)
2778 return -ENODEV;
2779
2780 for_each_netdev_safe(net, dev, aux) {
2781 if (dev->group == group) {
2782 const struct rtnl_link_ops *ops;
2783
2784 ops = dev->rtnl_link_ops;
2785 ops->dellink(dev, &list_kill);
2786 }
2787 }
2788 unregister_netdevice_many(&list_kill);
2789
2790 return 0;
2791}
2792
Thomas Graf614732e2015-07-21 10:44:06 +02002793int rtnl_delete_link(struct net_device *dev)
2794{
2795 const struct rtnl_link_ops *ops;
2796 LIST_HEAD(list_kill);
2797
2798 ops = dev->rtnl_link_ops;
2799 if (!ops || !ops->dellink)
2800 return -EOPNOTSUPP;
2801
2802 ops->dellink(dev, &list_kill);
2803 unregister_netdevice_many(&list_kill);
2804
2805 return 0;
2806}
2807EXPORT_SYMBOL_GPL(rtnl_delete_link);
2808
David Ahernc21ef3e2017-04-16 09:48:24 -07002809static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
2810 struct netlink_ext_ack *extack)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002811{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002812 struct net *net = sock_net(skb->sk);
Christian Braunerb61ad682018-01-24 15:26:34 +01002813 struct net *tgt_net = net;
2814 struct net_device *dev = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002815 struct ifinfomsg *ifm;
2816 char ifname[IFNAMSIZ];
2817 struct nlattr *tb[IFLA_MAX+1];
2818 int err;
Christian Braunerb61ad682018-01-24 15:26:34 +01002819 int netnsid = -1;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002820
David Ahernc21ef3e2017-04-16 09:48:24 -07002821 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002822 if (err < 0)
2823 return err;
2824
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002825 err = rtnl_ensure_unique_netns(tb, extack, true);
2826 if (err < 0)
2827 return err;
2828
Patrick McHardy38f7b872007-06-13 12:03:51 -07002829 if (tb[IFLA_IFNAME])
2830 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2831
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002832 if (tb[IFLA_TARGET_NETNSID]) {
2833 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
Christian Braunerc383edc2018-09-04 21:53:47 +02002834 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
Christian Braunerb61ad682018-01-24 15:26:34 +01002835 if (IS_ERR(tgt_net))
2836 return PTR_ERR(tgt_net);
2837 }
2838
2839 err = -EINVAL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002840 ifm = nlmsg_data(nlh);
2841 if (ifm->ifi_index > 0)
Christian Braunerb61ad682018-01-24 15:26:34 +01002842 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002843 else if (tb[IFLA_IFNAME])
Christian Braunerb61ad682018-01-24 15:26:34 +01002844 dev = __dev_get_by_name(tgt_net, ifname);
WANG Cong66400d52015-03-24 11:53:31 -07002845 else if (tb[IFLA_GROUP])
Christian Braunerb61ad682018-01-24 15:26:34 +01002846 err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP]));
Patrick McHardy38f7b872007-06-13 12:03:51 -07002847 else
Christian Braunerb61ad682018-01-24 15:26:34 +01002848 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002849
Christian Braunerb61ad682018-01-24 15:26:34 +01002850 if (!dev) {
2851 if (tb[IFLA_IFNAME] || ifm->ifi_index > 0)
2852 err = -ENODEV;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002853
Christian Braunerb61ad682018-01-24 15:26:34 +01002854 goto out;
2855 }
2856
2857 err = rtnl_delete_link(dev);
2858
2859out:
2860 if (netnsid >= 0)
2861 put_net(tgt_net);
2862
2863 return err;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002864}
2865
Patrick McHardy3729d502010-02-26 06:34:54 +00002866int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
2867{
2868 unsigned int old_flags;
2869 int err;
2870
2871 old_flags = dev->flags;
2872 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
2873 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
2874 if (err < 0)
2875 return err;
2876 }
2877
Roopa Prabhu8d356b82018-07-04 16:46:29 -07002878 if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) {
Roopa Prabhu56a49d72018-09-12 13:21:48 -07002879 __dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags));
Roopa Prabhu8d356b82018-07-04 16:46:29 -07002880 } else {
2881 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
2882 __dev_notify_flags(dev, old_flags, ~0U);
2883 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002884 return 0;
2885}
2886EXPORT_SYMBOL(rtnl_configure_link);
2887
David Ahernd0522f12018-11-06 12:51:14 -08002888struct net_device *rtnl_create_link(struct net *net, const char *ifname,
2889 unsigned char name_assign_type,
2890 const struct rtnl_link_ops *ops,
2891 struct nlattr *tb[],
2892 struct netlink_ext_ack *extack)
Pavel Emelianove7199282007-08-08 22:16:38 -07002893{
Pavel Emelianove7199282007-08-08 22:16:38 -07002894 struct net_device *dev;
Jiri Pirkod40156a2012-07-20 02:28:47 +00002895 unsigned int num_tx_queues = 1;
2896 unsigned int num_rx_queues = 1;
Pavel Emelianove7199282007-08-08 22:16:38 -07002897
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002898 if (tb[IFLA_NUM_TX_QUEUES])
2899 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
2900 else if (ops->get_num_tx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002901 num_tx_queues = ops->get_num_tx_queues();
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002902
2903 if (tb[IFLA_NUM_RX_QUEUES])
2904 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
2905 else if (ops->get_num_rx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002906 num_rx_queues = ops->get_num_rx_queues();
stephen hemmingerefacb302012-04-10 18:34:43 +00002907
David Ahernd0522f12018-11-06 12:51:14 -08002908 if (num_tx_queues < 1 || num_tx_queues > 4096) {
2909 NL_SET_ERR_MSG(extack, "Invalid number of transmit queues");
Eric Dumazet0e1d6ec2018-10-02 15:47:35 -07002910 return ERR_PTR(-EINVAL);
David Ahernd0522f12018-11-06 12:51:14 -08002911 }
Eric Dumazet0e1d6ec2018-10-02 15:47:35 -07002912
David Ahernd0522f12018-11-06 12:51:14 -08002913 if (num_rx_queues < 1 || num_rx_queues > 4096) {
2914 NL_SET_ERR_MSG(extack, "Invalid number of receive queues");
Eric Dumazet0e1d6ec2018-10-02 15:47:35 -07002915 return ERR_PTR(-EINVAL);
David Ahernd0522f12018-11-06 12:51:14 -08002916 }
Eric Dumazet0e1d6ec2018-10-02 15:47:35 -07002917
Tom Gundersen55177502014-07-14 16:37:25 +02002918 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
Tom Gundersenc835a672014-07-14 16:37:24 +02002919 ops->setup, num_tx_queues, num_rx_queues);
Pavel Emelianove7199282007-08-08 22:16:38 -07002920 if (!dev)
Tobias Klauserd1892e42017-02-20 16:32:06 +01002921 return ERR_PTR(-ENOMEM);
Pavel Emelianove7199282007-08-08 22:16:38 -07002922
Eric W. Biederman81adee42009-11-08 00:53:51 -08002923 dev_net_set(dev, net);
2924 dev->rtnl_link_ops = ops;
Patrick McHardy3729d502010-02-26 06:34:54 +00002925 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
Eric W. Biederman81adee42009-11-08 00:53:51 -08002926
Pavel Emelianove7199282007-08-08 22:16:38 -07002927 if (tb[IFLA_MTU])
2928 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002929 if (tb[IFLA_ADDRESS]) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002930 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
2931 nla_len(tb[IFLA_ADDRESS]));
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002932 dev->addr_assign_type = NET_ADDR_SET;
2933 }
Pavel Emelianove7199282007-08-08 22:16:38 -07002934 if (tb[IFLA_BROADCAST])
2935 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
2936 nla_len(tb[IFLA_BROADCAST]));
2937 if (tb[IFLA_TXQLEN])
2938 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
2939 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002940 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Pavel Emelianove7199282007-08-08 22:16:38 -07002941 if (tb[IFLA_LINKMODE])
2942 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
Patrick McHardyffa934f2011-01-20 03:00:42 +00002943 if (tb[IFLA_GROUP])
2944 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Stephen Hemminger46e6b992017-12-07 15:40:19 -08002945 if (tb[IFLA_GSO_MAX_SIZE])
2946 netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE]));
2947 if (tb[IFLA_GSO_MAX_SEGS])
Stephen Hemmingera0b586f2017-12-08 15:34:13 -08002948 dev->gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
Pavel Emelianove7199282007-08-08 22:16:38 -07002949
2950 return dev;
Pavel Emelianove7199282007-08-08 22:16:38 -07002951}
Eric Dumazete0d087a2009-11-07 01:26:17 -08002952EXPORT_SYMBOL(rtnl_create_link);
Pavel Emelianove7199282007-08-08 22:16:38 -07002953
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002954static int rtnl_group_changelink(const struct sk_buff *skb,
2955 struct net *net, int group,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002956 struct ifinfomsg *ifm,
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002957 struct netlink_ext_ack *extack,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002958 struct nlattr **tb)
2959{
WANG Congd0795352015-03-23 16:31:09 -07002960 struct net_device *dev, *aux;
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002961 int err;
2962
WANG Congd0795352015-03-23 16:31:09 -07002963 for_each_netdev_safe(net, dev, aux) {
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002964 if (dev->group == group) {
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002965 err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002966 if (err < 0)
2967 return err;
2968 }
2969 }
2970
2971 return 0;
2972}
2973
David Ahernc21ef3e2017-04-16 09:48:24 -07002974static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2975 struct netlink_ext_ack *extack)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002976{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002977 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002978 const struct rtnl_link_ops *ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002979 const struct rtnl_link_ops *m_ops = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002980 struct net_device *dev;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002981 struct net_device *master_dev = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002982 struct ifinfomsg *ifm;
2983 char kind[MODULE_NAME_LEN];
2984 char ifname[IFNAMSIZ];
2985 struct nlattr *tb[IFLA_MAX+1];
2986 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
Tom Gundersen55177502014-07-14 16:37:25 +02002987 unsigned char name_assign_type = NET_NAME_USER;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002988 int err;
2989
Johannes Berg95a5afc2008-10-16 15:24:51 -07002990#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002991replay:
Thomas Graf8072f082007-07-31 14:13:50 -07002992#endif
David Ahernc21ef3e2017-04-16 09:48:24 -07002993 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002994 if (err < 0)
2995 return err;
2996
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002997 err = rtnl_ensure_unique_netns(tb, extack, false);
2998 if (err < 0)
2999 return err;
3000
Patrick McHardy38f7b872007-06-13 12:03:51 -07003001 if (tb[IFLA_IFNAME])
3002 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
3003 else
3004 ifname[0] = '\0';
3005
3006 ifm = nlmsg_data(nlh);
3007 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07003008 dev = __dev_get_by_index(net, ifm->ifi_index);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00003009 else {
3010 if (ifname[0])
3011 dev = __dev_get_by_name(net, ifname);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00003012 else
3013 dev = NULL;
3014 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07003015
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01003016 if (dev) {
3017 master_dev = netdev_master_upper_dev_get(dev);
3018 if (master_dev)
3019 m_ops = master_dev->rtnl_link_ops;
3020 }
3021
Eric Dumazete0d087a2009-11-07 01:26:17 -08003022 err = validate_linkmsg(dev, tb);
3023 if (err < 0)
Thomas Graf1840bb12008-02-23 19:54:36 -08003024 return err;
3025
Patrick McHardy38f7b872007-06-13 12:03:51 -07003026 if (tb[IFLA_LINKINFO]) {
3027 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +02003028 tb[IFLA_LINKINFO], ifla_info_policy,
3029 NULL);
Patrick McHardy38f7b872007-06-13 12:03:51 -07003030 if (err < 0)
3031 return err;
3032 } else
3033 memset(linkinfo, 0, sizeof(linkinfo));
3034
3035 if (linkinfo[IFLA_INFO_KIND]) {
3036 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
3037 ops = rtnl_link_ops_get(kind);
3038 } else {
3039 kind[0] = '\0';
3040 ops = NULL;
3041 }
3042
3043 if (1) {
Kees Cookccf8dbc2018-05-30 15:20:52 -07003044 struct nlattr *attr[RTNL_MAX_TYPE + 1];
3045 struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1];
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01003046 struct nlattr **data = NULL;
3047 struct nlattr **slave_data = NULL;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01003048 struct net *dest_net, *link_net = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003049
3050 if (ops) {
Kees Cookccf8dbc2018-05-30 15:20:52 -07003051 if (ops->maxtype > RTNL_MAX_TYPE)
3052 return -EINVAL;
3053
Patrick McHardy38f7b872007-06-13 12:03:51 -07003054 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
3055 err = nla_parse_nested(attr, ops->maxtype,
3056 linkinfo[IFLA_INFO_DATA],
David Ahern68d57f32018-11-06 12:51:16 -08003057 ops->policy, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07003058 if (err < 0)
3059 return err;
3060 data = attr;
3061 }
3062 if (ops->validate) {
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02003063 err = ops->validate(tb, data, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07003064 if (err < 0)
3065 return err;
3066 }
3067 }
3068
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01003069 if (m_ops) {
Prashant Bholeaf066ed2018-06-01 17:16:58 +09003070 if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)
Kees Cookccf8dbc2018-05-30 15:20:52 -07003071 return -EINVAL;
3072
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01003073 if (m_ops->slave_maxtype &&
3074 linkinfo[IFLA_INFO_SLAVE_DATA]) {
3075 err = nla_parse_nested(slave_attr,
3076 m_ops->slave_maxtype,
3077 linkinfo[IFLA_INFO_SLAVE_DATA],
Johannes Bergfceb6432017-04-12 14:34:07 +02003078 m_ops->slave_policy,
David Ahern68d57f32018-11-06 12:51:16 -08003079 extack);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01003080 if (err < 0)
3081 return err;
3082 slave_data = slave_attr;
3083 }
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01003084 }
3085
Patrick McHardy38f7b872007-06-13 12:03:51 -07003086 if (dev) {
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02003087 int status = 0;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003088
3089 if (nlh->nlmsg_flags & NLM_F_EXCL)
3090 return -EEXIST;
3091 if (nlh->nlmsg_flags & NLM_F_REPLACE)
3092 return -EOPNOTSUPP;
3093
3094 if (linkinfo[IFLA_INFO_DATA]) {
3095 if (!ops || ops != dev->rtnl_link_ops ||
3096 !ops->changelink)
3097 return -EOPNOTSUPP;
3098
Matthias Schifferad744b22017-06-25 23:56:00 +02003099 err = ops->changelink(dev, tb, data, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07003100 if (err < 0)
3101 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02003102 status |= DO_SETLINK_NOTIFY;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003103 }
3104
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01003105 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
3106 if (!m_ops || !m_ops->slave_changelink)
3107 return -EOPNOTSUPP;
3108
3109 err = m_ops->slave_changelink(master_dev, dev,
Matthias Schiffer17dd0ec2017-06-25 23:56:02 +02003110 tb, slave_data,
3111 extack);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01003112 if (err < 0)
3113 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02003114 status |= DO_SETLINK_NOTIFY;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01003115 }
3116
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07003117 return do_setlink(skb, dev, ifm, extack, tb, ifname,
3118 status);
Patrick McHardy38f7b872007-06-13 12:03:51 -07003119 }
3120
Patrick McHardyffa934f2011-01-20 03:00:42 +00003121 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
3122 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07003123 return rtnl_group_changelink(skb, net,
Patrick McHardyffa934f2011-01-20 03:00:42 +00003124 nla_get_u32(tb[IFLA_GROUP]),
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07003125 ifm, extack, tb);
Patrick McHardy38f7b872007-06-13 12:03:51 -07003126 return -ENODEV;
Patrick McHardyffa934f2011-01-20 03:00:42 +00003127 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07003128
Theuns Verwoerd160ca012017-01-31 12:23:46 +13003129 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
Patrick McHardy38f7b872007-06-13 12:03:51 -07003130 return -EOPNOTSUPP;
3131
3132 if (!ops) {
Johannes Berg95a5afc2008-10-16 15:24:51 -07003133#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07003134 if (kind[0]) {
3135 __rtnl_unlock();
3136 request_module("rtnl-link-%s", kind);
3137 rtnl_lock();
3138 ops = rtnl_link_ops_get(kind);
3139 if (ops)
3140 goto replay;
3141 }
3142#endif
David Ahern68d57f32018-11-06 12:51:16 -08003143 NL_SET_ERR_MSG(extack, "Unknown device type");
Patrick McHardy38f7b872007-06-13 12:03:51 -07003144 return -EOPNOTSUPP;
3145 }
3146
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02003147 if (!ops->setup)
3148 return -EOPNOTSUPP;
3149
Tom Gundersen55177502014-07-14 16:37:25 +02003150 if (!ifname[0]) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07003151 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
Tom Gundersen55177502014-07-14 16:37:25 +02003152 name_assign_type = NET_NAME_ENUM;
3153 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07003154
Christian Brauner5bb8ed02018-01-29 18:07:20 +01003155 dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN);
Eric W. Biederman13ad1772011-01-29 14:57:22 +00003156 if (IS_ERR(dest_net))
3157 return PTR_ERR(dest_net);
3158
Nicolas Dichtel317f4812015-01-15 15:11:18 +01003159 if (tb[IFLA_LINK_NETNSID]) {
3160 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
3161
3162 link_net = get_net_ns_by_id(dest_net, id);
3163 if (!link_net) {
David Ahern68d57f32018-11-06 12:51:16 -08003164 NL_SET_ERR_MSG(extack, "Unknown network namespace id");
Nicolas Dichtel317f4812015-01-15 15:11:18 +01003165 err = -EINVAL;
3166 goto out;
3167 }
Eric W. Biederman06615be2015-02-26 16:20:07 -06003168 err = -EPERM;
3169 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
3170 goto out;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01003171 }
3172
3173 dev = rtnl_create_link(link_net ? : dest_net, ifname,
David Ahernd0522f12018-11-06 12:51:14 -08003174 name_assign_type, ops, tb, extack);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00003175 if (IS_ERR(dev)) {
Pavel Emelianove7199282007-08-08 22:16:38 -07003176 err = PTR_ERR(dev);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00003177 goto out;
3178 }
3179
3180 dev->ifindex = ifm->ifi_index;
3181
Cong Wang0e0eee22014-02-11 15:51:30 -08003182 if (ops->newlink) {
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02003183 err = ops->newlink(link_net ? : net, dev, tb, data,
3184 extack);
Cong Wang0e0eee22014-02-11 15:51:30 -08003185 /* Drivers should call free_netdev() in ->destructor
Cong Wange51fb152014-06-03 16:40:47 -07003186 * and unregister it on failure after registration
3187 * so that device could be finally freed in rtnl_unlock.
Cong Wang0e0eee22014-02-11 15:51:30 -08003188 */
Cong Wange51fb152014-06-03 16:40:47 -07003189 if (err < 0) {
3190 /* If device is not registered at all, free it now */
3191 if (dev->reg_state == NETREG_UNINITIALIZED)
3192 free_netdev(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08003193 goto out;
Cong Wange51fb152014-06-03 16:40:47 -07003194 }
Cong Wang0e0eee22014-02-11 15:51:30 -08003195 } else {
Patrick McHardy2d85cba2007-07-11 19:42:13 -07003196 err = register_netdevice(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08003197 if (err < 0) {
3198 free_netdev(dev);
3199 goto out;
3200 }
Dan Carpenterfce9b9b2013-08-14 12:35:42 +03003201 }
Patrick McHardy3729d502010-02-26 06:34:54 +00003202 err = rtnl_configure_link(dev, ifm);
David S. Miller43638902015-03-10 21:58:32 -04003203 if (err < 0)
3204 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01003205 if (link_net) {
Nicolas Dichtel317f4812015-01-15 15:11:18 +01003206 err = dev_change_net_namespace(dev, dest_net, ifname);
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01003207 if (err < 0)
David S. Miller43638902015-03-10 21:58:32 -04003208 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01003209 }
Theuns Verwoerd160ca012017-01-31 12:23:46 +13003210 if (tb[IFLA_MASTER]) {
David Ahern33eaf2a2017-10-04 17:48:46 -07003211 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]),
3212 extack);
Theuns Verwoerd160ca012017-01-31 12:23:46 +13003213 if (err)
3214 goto out_unregister;
3215 }
Patrick McHardy3729d502010-02-26 06:34:54 +00003216out:
Nicolas Dichtel317f4812015-01-15 15:11:18 +01003217 if (link_net)
3218 put_net(link_net);
Eric W. Biederman81adee42009-11-08 00:53:51 -08003219 put_net(dest_net);
Patrick McHardy38f7b872007-06-13 12:03:51 -07003220 return err;
David S. Miller43638902015-03-10 21:58:32 -04003221out_unregister:
3222 if (ops->newlink) {
3223 LIST_HEAD(list_kill);
3224
3225 ops->dellink(dev, &list_kill);
3226 unregister_netdevice_many(&list_kill);
3227 } else {
3228 unregister_netdevice(dev);
3229 }
3230 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003231 }
3232}
3233
David Ahernc21ef3e2017-04-16 09:48:24 -07003234static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3235 struct netlink_ext_ack *extack)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003236{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003237 struct net *net = sock_net(skb->sk);
Jiri Benc79e1ad12017-11-02 17:04:38 -02003238 struct net *tgt_net = net;
Thomas Grafb60c5112006-08-04 23:05:34 -07003239 struct ifinfomsg *ifm;
Eric Dumazeta3d12892009-10-21 10:59:31 +00003240 char ifname[IFNAMSIZ];
Thomas Grafb60c5112006-08-04 23:05:34 -07003241 struct nlattr *tb[IFLA_MAX+1];
3242 struct net_device *dev = NULL;
3243 struct sk_buff *nskb;
Jiri Benc79e1ad12017-11-02 17:04:38 -02003244 int netnsid = -1;
Thomas Graf339bf982006-11-10 14:10:15 -08003245 int err;
Greg Rose115c9b82012-02-21 16:54:48 -05003246 u32 ext_filter_mask = 0;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003247
David Ahernc21ef3e2017-04-16 09:48:24 -07003248 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Thomas Grafb60c5112006-08-04 23:05:34 -07003249 if (err < 0)
Eric Sesterhenn9918f232006-09-26 23:26:38 -07003250 return err;
Thomas Grafb60c5112006-08-04 23:05:34 -07003251
Christian Brauner4ff66ca2018-02-07 13:53:20 +01003252 err = rtnl_ensure_unique_netns(tb, extack, true);
3253 if (err < 0)
3254 return err;
3255
Christian Brauner7e4a8d52018-09-04 21:53:53 +02003256 if (tb[IFLA_TARGET_NETNSID]) {
3257 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
Christian Braunerc383edc2018-09-04 21:53:47 +02003258 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
Jiri Benc79e1ad12017-11-02 17:04:38 -02003259 if (IS_ERR(tgt_net))
3260 return PTR_ERR(tgt_net);
3261 }
3262
Eric Dumazeta3d12892009-10-21 10:59:31 +00003263 if (tb[IFLA_IFNAME])
3264 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
3265
Greg Rose115c9b82012-02-21 16:54:48 -05003266 if (tb[IFLA_EXT_MASK])
3267 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3268
Jiri Benc79e1ad12017-11-02 17:04:38 -02003269 err = -EINVAL;
Thomas Grafb60c5112006-08-04 23:05:34 -07003270 ifm = nlmsg_data(nlh);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003271 if (ifm->ifi_index > 0)
Jiri Benc79e1ad12017-11-02 17:04:38 -02003272 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003273 else if (tb[IFLA_IFNAME])
Jiri Benc79e1ad12017-11-02 17:04:38 -02003274 dev = __dev_get_by_name(tgt_net, ifname);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003275 else
Jiri Benc79e1ad12017-11-02 17:04:38 -02003276 goto out;
Thomas Grafb60c5112006-08-04 23:05:34 -07003277
Jiri Benc79e1ad12017-11-02 17:04:38 -02003278 err = -ENODEV;
Eric Dumazeta3d12892009-10-21 10:59:31 +00003279 if (dev == NULL)
Jiri Benc79e1ad12017-11-02 17:04:38 -02003280 goto out;
Eric Dumazeta3d12892009-10-21 10:59:31 +00003281
Jiri Benc79e1ad12017-11-02 17:04:38 -02003282 err = -ENOBUFS;
Greg Rose115c9b82012-02-21 16:54:48 -05003283 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003284 if (nskb == NULL)
Jiri Benc79e1ad12017-11-02 17:04:38 -02003285 goto out;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003286
Jiri Benc79e1ad12017-11-02 17:04:38 -02003287 err = rtnl_fill_ifinfo(nskb, dev, net,
3288 RTM_NEWLINK, NETLINK_CB(skb).portid,
3289 nlh->nlmsg_seq, 0, 0, ext_filter_mask,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003290 0, NULL, 0, netnsid);
Patrick McHardy26932562007-01-31 23:16:40 -08003291 if (err < 0) {
3292 /* -EMSGSIZE implies BUG in if_nlmsg_size */
3293 WARN_ON(err == -EMSGSIZE);
3294 kfree_skb(nskb);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003295 } else
Eric W. Biederman15e47302012-09-07 20:12:54 +00003296 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
Jiri Benc79e1ad12017-11-02 17:04:38 -02003297out:
3298 if (netnsid >= 0)
3299 put_net(tgt_net);
Thomas Grafb60c5112006-08-04 23:05:34 -07003300
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003301 return err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003302}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003303
Greg Rose115c9b82012-02-21 16:54:48 -05003304static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
Greg Rosec7ac8672011-06-10 01:27:09 +00003305{
Greg Rose115c9b82012-02-21 16:54:48 -05003306 struct net *net = sock_net(skb->sk);
3307 struct net_device *dev;
3308 struct nlattr *tb[IFLA_MAX+1];
3309 u32 ext_filter_mask = 0;
3310 u16 min_ifinfo_dump_size = 0;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02003311 int hdrlen;
Greg Rose115c9b82012-02-21 16:54:48 -05003312
Michal Schmidte5eca6d2014-05-28 14:15:19 +02003313 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3314 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
3315 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
3316
Johannes Bergfceb6432017-04-12 14:34:07 +02003317 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00003318 if (tb[IFLA_EXT_MASK])
3319 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3320 }
Greg Rose115c9b82012-02-21 16:54:48 -05003321
3322 if (!ext_filter_mask)
3323 return NLMSG_GOODSIZE;
3324 /*
3325 * traverse the list of net devices and compute the minimum
3326 * buffer size based upon the filter mask.
3327 */
Florian Westphal6853dd42017-08-09 20:41:51 +02003328 rcu_read_lock();
3329 for_each_netdev_rcu(net, dev) {
Greg Rose115c9b82012-02-21 16:54:48 -05003330 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
3331 if_nlmsg_size(dev,
3332 ext_filter_mask));
3333 }
Florian Westphal6853dd42017-08-09 20:41:51 +02003334 rcu_read_unlock();
Greg Rose115c9b82012-02-21 16:54:48 -05003335
Zhang Shengju93af2052016-11-22 14:14:28 +08003336 return nlmsg_total_size(min_ifinfo_dump_size);
Greg Rosec7ac8672011-06-10 01:27:09 +00003337}
3338
Adrian Bunk42bad1d2007-04-26 00:57:41 -07003339static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340{
3341 int idx;
3342 int s_idx = cb->family;
Christian Brauner87ccbb12018-09-04 21:53:51 +02003343 int type = cb->nlh->nlmsg_type - RTM_BASE;
David Ahernc63586d2018-10-24 12:59:02 -07003344 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345
3346 if (s_idx == 0)
3347 s_idx = 1;
Florian Westphal6853dd42017-08-09 20:41:51 +02003348
Patrick McHardy25239ce2010-04-26 16:02:05 +02003349 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
Florian Westphaladdf9b92017-12-02 21:44:05 +01003350 struct rtnl_link **tab;
Florian Westphaladdf9b92017-12-02 21:44:05 +01003351 struct rtnl_link *link;
Florian Westphal6853dd42017-08-09 20:41:51 +02003352 rtnl_dumpit_func dumpit;
3353
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 if (idx < s_idx || idx == PF_PACKET)
3355 continue;
Florian Westphal6853dd42017-08-09 20:41:51 +02003356
Florian Westphaladdf9b92017-12-02 21:44:05 +01003357 if (type < 0 || type >= RTM_NR_MSGTYPES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 continue;
Florian Westphal6853dd42017-08-09 20:41:51 +02003359
Florian Westphaladdf9b92017-12-02 21:44:05 +01003360 tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]);
3361 if (!tab)
3362 continue;
3363
3364 link = tab[type];
3365 if (!link)
3366 continue;
3367
3368 dumpit = link->dumpit;
Florian Westphal6853dd42017-08-09 20:41:51 +02003369 if (!dumpit)
3370 continue;
3371
Nicolas Dichtel04652772013-03-22 06:28:42 +00003372 if (idx > s_idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 memset(&cb->args[0], 0, sizeof(cb->args));
Nicolas Dichtel04652772013-03-22 06:28:42 +00003374 cb->prev_seq = 0;
3375 cb->seq = 0;
3376 }
David Ahernc63586d2018-10-24 12:59:02 -07003377 ret = dumpit(skb, cb);
Alexey Kodanev5e1acb42018-11-02 19:11:04 +03003378 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 break;
3380 }
3381 cb->family = idx;
3382
David Ahernc63586d2018-10-24 12:59:02 -07003383 return skb->len ? : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384}
3385
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003386struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003387 unsigned int change,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003388 u32 event, gfp_t flags, int *new_nsid,
3389 int new_ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003391 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 struct sk_buff *skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003393 int err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00003394 size_t if_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395
Alexei Starovoitov7f294052013-10-23 16:02:42 -07003396 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003397 if (skb == NULL)
3398 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399
Jiri Benc79e1ad12017-11-02 17:04:38 -02003400 err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
3401 type, 0, 0, change, 0, 0, event,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003402 new_nsid, new_ifindex, -1);
Patrick McHardy26932562007-01-31 23:16:40 -08003403 if (err < 0) {
3404 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
3405 WARN_ON(err == -EMSGSIZE);
3406 kfree_skb(skb);
3407 goto errout;
3408 }
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003409 return skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003410errout:
3411 if (err < 0)
Eric W. Biederman4b3da702007-11-19 22:27:40 -08003412 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003413 return NULL;
3414}
3415
3416void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
3417{
3418 struct net *net = dev_net(dev);
3419
3420 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
3421}
3422
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003423static void rtmsg_ifinfo_event(int type, struct net_device *dev,
3424 unsigned int change, u32 event,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003425 gfp_t flags, int *new_nsid, int new_ifindex)
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003426{
3427 struct sk_buff *skb;
3428
Nicolas Dichteled2a80a2015-05-13 14:19:42 +02003429 if (dev->reg_state != NETREG_REGISTERED)
3430 return;
3431
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003432 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid,
3433 new_ifindex);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003434 if (skb)
3435 rtmsg_ifinfo_send(skb, dev, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436}
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003437
3438void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
3439 gfp_t flags)
3440{
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003441 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3442 NULL, 0);
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003443}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003445void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003446 gfp_t flags, int *new_nsid, int new_ifindex)
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003447{
3448 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003449 new_nsid, new_ifindex);
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003450}
3451
John Fastabendd83b0602012-04-15 06:44:08 +00003452static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
3453 struct net_device *dev,
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003454 u8 *addr, u16 vid, u32 pid, u32 seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003455 int type, unsigned int flags,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003456 int nlflags, u16 ndm_state)
John Fastabendd83b0602012-04-15 06:44:08 +00003457{
3458 struct nlmsghdr *nlh;
3459 struct ndmsg *ndm;
3460
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003461 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
John Fastabendd83b0602012-04-15 06:44:08 +00003462 if (!nlh)
3463 return -EMSGSIZE;
3464
3465 ndm = nlmsg_data(nlh);
3466 ndm->ndm_family = AF_BRIDGE;
3467 ndm->ndm_pad1 = 0;
3468 ndm->ndm_pad2 = 0;
3469 ndm->ndm_flags = flags;
3470 ndm->ndm_type = 0;
3471 ndm->ndm_ifindex = dev->ifindex;
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003472 ndm->ndm_state = ndm_state;
John Fastabendd83b0602012-04-15 06:44:08 +00003473
3474 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
3475 goto nla_put_failure;
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003476 if (vid)
3477 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
3478 goto nla_put_failure;
John Fastabendd83b0602012-04-15 06:44:08 +00003479
Johannes Berg053c0952015-01-16 22:09:00 +01003480 nlmsg_end(skb, nlh);
3481 return 0;
John Fastabendd83b0602012-04-15 06:44:08 +00003482
3483nla_put_failure:
3484 nlmsg_cancel(skb, nlh);
3485 return -EMSGSIZE;
3486}
3487
John Fastabend3ff661c2012-04-15 06:44:14 +00003488static inline size_t rtnl_fdb_nlmsg_size(void)
3489{
Sabrina Dubrocaf82ef3e2016-11-18 15:50:39 +01003490 return NLMSG_ALIGN(sizeof(struct ndmsg)) +
3491 nla_total_size(ETH_ALEN) + /* NDA_LLADDR */
3492 nla_total_size(sizeof(u16)) + /* NDA_VLAN */
3493 0;
John Fastabend3ff661c2012-04-15 06:44:14 +00003494}
3495
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003496static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
3497 u16 ndm_state)
John Fastabend3ff661c2012-04-15 06:44:14 +00003498{
3499 struct net *net = dev_net(dev);
3500 struct sk_buff *skb;
3501 int err = -ENOBUFS;
3502
3503 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
3504 if (!skb)
3505 goto errout;
3506
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003507 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003508 0, 0, type, NTF_SELF, 0, ndm_state);
John Fastabend3ff661c2012-04-15 06:44:14 +00003509 if (err < 0) {
3510 kfree_skb(skb);
3511 goto errout;
3512 }
3513
3514 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
3515 return;
3516errout:
3517 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
3518}
3519
Vlad Yasevich090096b2013-03-06 15:39:42 +00003520/**
3521 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3522 */
3523int ndo_dflt_fdb_add(struct ndmsg *ndm,
3524 struct nlattr *tb[],
3525 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003526 const unsigned char *addr, u16 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003527 u16 flags)
3528{
3529 int err = -EINVAL;
3530
3531 /* If aging addresses are supported device will need to
3532 * implement its own handler for this.
3533 */
3534 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
3535 pr_info("%s: FDB only supports static addresses\n", dev->name);
3536 return err;
3537 }
3538
Or Gerlitz65891fe2014-12-14 18:19:05 +02003539 if (vid) {
3540 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
3541 return err;
3542 }
3543
Vlad Yasevich090096b2013-03-06 15:39:42 +00003544 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3545 err = dev_uc_add_excl(dev, addr);
3546 else if (is_multicast_ether_addr(addr))
3547 err = dev_mc_add_excl(dev, addr);
3548
3549 /* Only return duplicate errors if NLM_F_EXCL is set */
3550 if (err == -EEXIST && !(flags & NLM_F_EXCL))
3551 err = 0;
3552
3553 return err;
3554}
3555EXPORT_SYMBOL(ndo_dflt_fdb_add);
3556
Florian Westphalb88d12e2017-10-10 17:10:04 +02003557static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
3558 struct netlink_ext_ack *extack)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003559{
3560 u16 vid = 0;
3561
3562 if (vlan_attr) {
3563 if (nla_len(vlan_attr) != sizeof(u16)) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003564 NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
Jiri Pirkof6f64242014-11-28 14:34:15 +01003565 return -EINVAL;
3566 }
3567
3568 vid = nla_get_u16(vlan_attr);
3569
3570 if (!vid || vid >= VLAN_VID_MASK) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003571 NL_SET_ERR_MSG(extack, "invalid vlan id");
Jiri Pirkof6f64242014-11-28 14:34:15 +01003572 return -EINVAL;
3573 }
3574 }
3575 *p_vid = vid;
3576 return 0;
3577}
3578
David Ahernc21ef3e2017-04-16 09:48:24 -07003579static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
3580 struct netlink_ext_ack *extack)
John Fastabend77162022012-04-15 06:43:56 +00003581{
3582 struct net *net = sock_net(skb->sk);
John Fastabend77162022012-04-15 06:43:56 +00003583 struct ndmsg *ndm;
3584 struct nlattr *tb[NDA_MAX+1];
3585 struct net_device *dev;
3586 u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01003587 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00003588 int err;
3589
David Ahernc21ef3e2017-04-16 09:48:24 -07003590 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
John Fastabend77162022012-04-15 06:43:56 +00003591 if (err < 0)
3592 return err;
3593
3594 ndm = nlmsg_data(nlh);
3595 if (ndm->ndm_ifindex == 0) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003596 NL_SET_ERR_MSG(extack, "invalid ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003597 return -EINVAL;
3598 }
3599
3600 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3601 if (dev == NULL) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003602 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003603 return -ENODEV;
3604 }
3605
3606 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003607 NL_SET_ERR_MSG(extack, "invalid address");
John Fastabend77162022012-04-15 06:43:56 +00003608 return -EINVAL;
3609 }
3610
Ido Schimmelda715772018-10-29 20:36:43 +00003611 if (dev->type != ARPHRD_ETHER) {
3612 NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices");
3613 return -EINVAL;
3614 }
3615
John Fastabend77162022012-04-15 06:43:56 +00003616 addr = nla_data(tb[NDA_LLADDR]);
John Fastabend77162022012-04-15 06:43:56 +00003617
Florian Westphalb88d12e2017-10-10 17:10:04 +02003618 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
Jiri Pirkof6f64242014-11-28 14:34:15 +01003619 if (err)
3620 return err;
3621
John Fastabend77162022012-04-15 06:43:56 +00003622 err = -EOPNOTSUPP;
3623
3624 /* Support fdb on master device the net/bridge default case */
3625 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3626 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003627 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3628 const struct net_device_ops *ops = br_dev->netdev_ops;
3629
Jiri Pirkof6f64242014-11-28 14:34:15 +01003630 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
3631 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00003632 if (err)
3633 goto out;
3634 else
3635 ndm->ndm_flags &= ~NTF_MASTER;
3636 }
3637
3638 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00003639 if ((ndm->ndm_flags & NTF_SELF)) {
3640 if (dev->netdev_ops->ndo_fdb_add)
3641 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003642 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003643 nlh->nlmsg_flags);
3644 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01003645 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003646 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00003647
John Fastabend3ff661c2012-04-15 06:44:14 +00003648 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003649 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
3650 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00003651 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00003652 }
John Fastabend77162022012-04-15 06:43:56 +00003653 }
3654out:
3655 return err;
3656}
3657
Vlad Yasevich090096b2013-03-06 15:39:42 +00003658/**
3659 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3660 */
3661int ndo_dflt_fdb_del(struct ndmsg *ndm,
3662 struct nlattr *tb[],
3663 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003664 const unsigned char *addr, u16 vid)
Vlad Yasevich090096b2013-03-06 15:39:42 +00003665{
Alexander Duyckc8a89c42014-07-15 15:15:20 -07003666 int err = -EINVAL;
Vlad Yasevich090096b2013-03-06 15:39:42 +00003667
3668 /* If aging addresses are supported device will need to
3669 * implement its own handler for this.
3670 */
Sridhar Samudrala64535992013-08-08 15:19:48 -07003671 if (!(ndm->ndm_state & NUD_PERMANENT)) {
Vlad Yasevich090096b2013-03-06 15:39:42 +00003672 pr_info("%s: FDB only supports static addresses\n", dev->name);
Alexander Duyckc8a89c42014-07-15 15:15:20 -07003673 return err;
Vlad Yasevich090096b2013-03-06 15:39:42 +00003674 }
3675
3676 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3677 err = dev_uc_del(dev, addr);
3678 else if (is_multicast_ether_addr(addr))
3679 err = dev_mc_del(dev, addr);
Vlad Yasevich090096b2013-03-06 15:39:42 +00003680
3681 return err;
3682}
3683EXPORT_SYMBOL(ndo_dflt_fdb_del);
3684
David Ahernc21ef3e2017-04-16 09:48:24 -07003685static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
3686 struct netlink_ext_ack *extack)
John Fastabend77162022012-04-15 06:43:56 +00003687{
3688 struct net *net = sock_net(skb->sk);
3689 struct ndmsg *ndm;
Vlad Yasevich1690be62013-02-13 12:00:18 +00003690 struct nlattr *tb[NDA_MAX+1];
John Fastabend77162022012-04-15 06:43:56 +00003691 struct net_device *dev;
3692 int err = -EINVAL;
3693 __u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01003694 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00003695
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07003696 if (!netlink_capable(skb, CAP_NET_ADMIN))
Vlad Yasevich1690be62013-02-13 12:00:18 +00003697 return -EPERM;
3698
David Ahernc21ef3e2017-04-16 09:48:24 -07003699 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
Vlad Yasevich1690be62013-02-13 12:00:18 +00003700 if (err < 0)
3701 return err;
John Fastabend77162022012-04-15 06:43:56 +00003702
3703 ndm = nlmsg_data(nlh);
3704 if (ndm->ndm_ifindex == 0) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003705 NL_SET_ERR_MSG(extack, "invalid ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003706 return -EINVAL;
3707 }
3708
3709 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3710 if (dev == NULL) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003711 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003712 return -ENODEV;
3713 }
3714
Vlad Yasevich1690be62013-02-13 12:00:18 +00003715 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003716 NL_SET_ERR_MSG(extack, "invalid address");
John Fastabend77162022012-04-15 06:43:56 +00003717 return -EINVAL;
3718 }
3719
Ido Schimmelda715772018-10-29 20:36:43 +00003720 if (dev->type != ARPHRD_ETHER) {
3721 NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices");
3722 return -EINVAL;
3723 }
3724
Vlad Yasevich1690be62013-02-13 12:00:18 +00003725 addr = nla_data(tb[NDA_LLADDR]);
Vlad Yasevich1690be62013-02-13 12:00:18 +00003726
Florian Westphalb88d12e2017-10-10 17:10:04 +02003727 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
Jiri Pirkof6f64242014-11-28 14:34:15 +01003728 if (err)
3729 return err;
3730
John Fastabend77162022012-04-15 06:43:56 +00003731 err = -EOPNOTSUPP;
3732
3733 /* Support fdb on master device the net/bridge default case */
3734 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3735 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003736 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3737 const struct net_device_ops *ops = br_dev->netdev_ops;
John Fastabend77162022012-04-15 06:43:56 +00003738
Jiri Pirko898e5062013-01-03 22:48:52 +00003739 if (ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003740 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00003741
3742 if (err)
3743 goto out;
3744 else
3745 ndm->ndm_flags &= ~NTF_MASTER;
3746 }
3747
3748 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00003749 if (ndm->ndm_flags & NTF_SELF) {
3750 if (dev->netdev_ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003751 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
3752 vid);
Vlad Yasevich090096b2013-03-06 15:39:42 +00003753 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01003754 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00003755
John Fastabend3ff661c2012-04-15 06:44:14 +00003756 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003757 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
3758 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00003759 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00003760 }
John Fastabend77162022012-04-15 06:43:56 +00003761 }
3762out:
3763 return err;
3764}
3765
John Fastabendd83b0602012-04-15 06:44:08 +00003766static int nlmsg_populate_fdb(struct sk_buff *skb,
3767 struct netlink_callback *cb,
3768 struct net_device *dev,
3769 int *idx,
3770 struct netdev_hw_addr_list *list)
3771{
3772 struct netdev_hw_addr *ha;
3773 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00003774 u32 portid, seq;
John Fastabendd83b0602012-04-15 06:44:08 +00003775
Eric W. Biederman15e47302012-09-07 20:12:54 +00003776 portid = NETLINK_CB(cb->skb).portid;
John Fastabendd83b0602012-04-15 06:44:08 +00003777 seq = cb->nlh->nlmsg_seq;
3778
3779 list_for_each_entry(ha, &list->list, list) {
Roopa Prabhud2976532016-08-30 21:56:45 -07003780 if (*idx < cb->args[2])
John Fastabendd83b0602012-04-15 06:44:08 +00003781 goto skip;
3782
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003783 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
John Fastabenda7a558f2012-11-01 16:23:10 +00003784 portid, seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003785 RTM_NEWNEIGH, NTF_SELF,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003786 NLM_F_MULTI, NUD_PERMANENT);
John Fastabendd83b0602012-04-15 06:44:08 +00003787 if (err < 0)
3788 return err;
3789skip:
3790 *idx += 1;
3791 }
3792 return 0;
3793}
3794
3795/**
Ben Hutchings2c530402012-07-10 10:55:09 +00003796 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
John Fastabendd83b0602012-04-15 06:44:08 +00003797 * @nlh: netlink message header
3798 * @dev: netdevice
3799 *
3800 * Default netdevice operation to dump the existing unicast address list.
John Fastabend91f3e7b2013-03-29 08:18:37 +00003801 * Returns number of addresses from list put in skb.
John Fastabendd83b0602012-04-15 06:44:08 +00003802 */
3803int ndo_dflt_fdb_dump(struct sk_buff *skb,
3804 struct netlink_callback *cb,
3805 struct net_device *dev,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04003806 struct net_device *filter_dev,
Roopa Prabhud2976532016-08-30 21:56:45 -07003807 int *idx)
John Fastabendd83b0602012-04-15 06:44:08 +00003808{
3809 int err;
3810
3811 netif_addr_lock_bh(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -07003812 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
John Fastabendd83b0602012-04-15 06:44:08 +00003813 if (err)
3814 goto out;
Zhang Shengju2934c9d2016-11-30 16:37:34 +08003815 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
John Fastabendd83b0602012-04-15 06:44:08 +00003816out:
3817 netif_addr_unlock_bh(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -07003818 return err;
John Fastabendd83b0602012-04-15 06:44:08 +00003819}
3820EXPORT_SYMBOL(ndo_dflt_fdb_dump);
3821
David Ahern8c6e1372018-10-07 20:16:44 -07003822static int valid_fdb_dump_strict(const struct nlmsghdr *nlh,
3823 int *br_idx, int *brport_idx,
3824 struct netlink_ext_ack *extack)
3825{
3826 struct nlattr *tb[NDA_MAX + 1];
3827 struct ndmsg *ndm;
3828 int err, i;
3829
3830 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
3831 NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request");
3832 return -EINVAL;
3833 }
3834
3835 ndm = nlmsg_data(nlh);
3836 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
3837 ndm->ndm_flags || ndm->ndm_type) {
3838 NL_SET_ERR_MSG(extack, "Invalid values in header for fbd dump request");
3839 return -EINVAL;
3840 }
3841
3842 err = nlmsg_parse_strict(nlh, sizeof(struct ndmsg), tb, NDA_MAX,
3843 NULL, extack);
3844 if (err < 0)
3845 return err;
3846
3847 *brport_idx = ndm->ndm_ifindex;
3848 for (i = 0; i <= NDA_MAX; ++i) {
3849 if (!tb[i])
3850 continue;
3851
3852 switch (i) {
3853 case NDA_IFINDEX:
3854 if (nla_len(tb[i]) != sizeof(u32)) {
3855 NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request");
3856 return -EINVAL;
3857 }
3858 *brport_idx = nla_get_u32(tb[NDA_IFINDEX]);
3859 break;
3860 case NDA_MASTER:
3861 if (nla_len(tb[i]) != sizeof(u32)) {
3862 NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request");
3863 return -EINVAL;
3864 }
3865 *br_idx = nla_get_u32(tb[NDA_MASTER]);
3866 break;
3867 default:
3868 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request");
3869 return -EINVAL;
3870 }
3871 }
3872
3873 return 0;
3874}
3875
David Ahern8dfbda12018-10-07 20:16:43 -07003876static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh,
3877 int *br_idx, int *brport_idx,
3878 struct netlink_ext_ack *extack)
3879{
David Ahern8dfbda12018-10-07 20:16:43 -07003880 struct nlattr *tb[IFLA_MAX+1];
3881 int err;
3882
3883 /* A hack to preserve kernel<->userspace interface.
3884 * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0.
3885 * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails.
3886 * So, check for ndmsg with an optional u32 attribute (not used here).
3887 * Fortunately these sizes don't conflict with the size of ifinfomsg
3888 * with an optional attribute.
3889 */
3890 if (nlmsg_len(nlh) != sizeof(struct ndmsg) &&
3891 (nlmsg_len(nlh) != sizeof(struct ndmsg) +
3892 nla_attr_size(sizeof(u32)))) {
David Ahern4565d7e2018-10-08 13:57:24 -07003893 struct ifinfomsg *ifm;
3894
David Ahern8dfbda12018-10-07 20:16:43 -07003895 err = nlmsg_parse(nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
3896 ifla_policy, extack);
3897 if (err < 0) {
3898 return -EINVAL;
3899 } else if (err == 0) {
3900 if (tb[IFLA_MASTER])
3901 *br_idx = nla_get_u32(tb[IFLA_MASTER]);
3902 }
3903
David Ahern4565d7e2018-10-08 13:57:24 -07003904 ifm = nlmsg_data(nlh);
David Ahern8dfbda12018-10-07 20:16:43 -07003905 *brport_idx = ifm->ifi_index;
3906 }
3907 return 0;
3908}
3909
John Fastabend77162022012-04-15 06:43:56 +00003910static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
3911{
John Fastabend77162022012-04-15 06:43:56 +00003912 struct net_device *dev;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003913 struct net_device *br_dev = NULL;
3914 const struct net_device_ops *ops = NULL;
3915 const struct net_device_ops *cops = NULL;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003916 struct net *net = sock_net(skb->sk);
Roopa Prabhud2976532016-08-30 21:56:45 -07003917 struct hlist_head *head;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003918 int brport_idx = 0;
3919 int br_idx = 0;
Roopa Prabhud2976532016-08-30 21:56:45 -07003920 int h, s_h;
3921 int idx = 0, s_idx;
3922 int err = 0;
3923 int fidx = 0;
John Fastabend77162022012-04-15 06:43:56 +00003924
David Ahern8c6e1372018-10-07 20:16:44 -07003925 if (cb->strict_check)
3926 err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx,
3927 cb->extack);
3928 else
3929 err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx,
3930 cb->extack);
David Ahern8dfbda12018-10-07 20:16:43 -07003931 if (err < 0)
3932 return err;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003933
3934 if (br_idx) {
3935 br_dev = __dev_get_by_index(net, br_idx);
3936 if (!br_dev)
3937 return -ENODEV;
3938
3939 ops = br_dev->netdev_ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003940 }
3941
Roopa Prabhud2976532016-08-30 21:56:45 -07003942 s_h = cb->args[0];
3943 s_idx = cb->args[1];
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003944
Roopa Prabhud2976532016-08-30 21:56:45 -07003945 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
3946 idx = 0;
3947 head = &net->dev_index_head[h];
3948 hlist_for_each_entry(dev, head, index_hlist) {
3949
3950 if (brport_idx && (dev->ifindex != brport_idx))
3951 continue;
3952
3953 if (!br_idx) { /* user did not specify a specific bridge */
3954 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3955 br_dev = netdev_master_upper_dev_get(dev);
3956 cops = br_dev->netdev_ops;
3957 }
3958 } else {
3959 if (dev != br_dev &&
3960 !(dev->priv_flags & IFF_BRIDGE_PORT))
3961 continue;
3962
3963 if (br_dev != netdev_master_upper_dev_get(dev) &&
3964 !(dev->priv_flags & IFF_EBRIDGE))
3965 continue;
3966 cops = ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003967 }
3968
Roopa Prabhud2976532016-08-30 21:56:45 -07003969 if (idx < s_idx)
3970 goto cont;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003971
Roopa Prabhud2976532016-08-30 21:56:45 -07003972 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3973 if (cops && cops->ndo_fdb_dump) {
3974 err = cops->ndo_fdb_dump(skb, cb,
3975 br_dev, dev,
3976 &fidx);
3977 if (err == -EMSGSIZE)
3978 goto out;
3979 }
3980 }
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003981
Roopa Prabhud2976532016-08-30 21:56:45 -07003982 if (dev->netdev_ops->ndo_fdb_dump)
3983 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
3984 dev, NULL,
3985 &fidx);
3986 else
3987 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
3988 &fidx);
3989 if (err == -EMSGSIZE)
3990 goto out;
3991
3992 cops = NULL;
3993
3994 /* reset fdb offset to 0 for rest of the interfaces */
3995 cb->args[2] = 0;
3996 fidx = 0;
3997cont:
3998 idx++;
John Fastabend77162022012-04-15 06:43:56 +00003999 }
John Fastabend77162022012-04-15 06:43:56 +00004000 }
John Fastabend77162022012-04-15 06:43:56 +00004001
Roopa Prabhud2976532016-08-30 21:56:45 -07004002out:
4003 cb->args[0] = h;
4004 cb->args[1] = idx;
4005 cb->args[2] = fidx;
4006
John Fastabend77162022012-04-15 06:43:56 +00004007 return skb->len;
4008}
4009
Scott Feldman2c3c0312014-11-28 14:34:25 +01004010static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
4011 unsigned int attrnum, unsigned int flag)
4012{
4013 if (mask & flag)
4014 return nla_put_u8(skb, attrnum, !!(flags & flag));
4015 return 0;
4016}
4017
John Fastabend815cccb2012-10-24 08:13:09 +00004018int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Scott Feldman2c3c0312014-11-28 14:34:25 +01004019 struct net_device *dev, u16 mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -07004020 u32 flags, u32 mask, int nlflags,
4021 u32 filter_mask,
4022 int (*vlan_fill)(struct sk_buff *skb,
4023 struct net_device *dev,
4024 u32 filter_mask))
John Fastabend815cccb2012-10-24 08:13:09 +00004025{
4026 struct nlmsghdr *nlh;
4027 struct ifinfomsg *ifm;
4028 struct nlattr *br_afspec;
Scott Feldman2c3c0312014-11-28 14:34:25 +01004029 struct nlattr *protinfo;
John Fastabend815cccb2012-10-24 08:13:09 +00004030 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Jiri Pirko898e5062013-01-03 22:48:52 +00004031 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07004032 int err = 0;
John Fastabend815cccb2012-10-24 08:13:09 +00004033
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02004034 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
John Fastabend815cccb2012-10-24 08:13:09 +00004035 if (nlh == NULL)
4036 return -EMSGSIZE;
4037
4038 ifm = nlmsg_data(nlh);
4039 ifm->ifi_family = AF_BRIDGE;
4040 ifm->__ifi_pad = 0;
4041 ifm->ifi_type = dev->type;
4042 ifm->ifi_index = dev->ifindex;
4043 ifm->ifi_flags = dev_get_flags(dev);
4044 ifm->ifi_change = 0;
4045
4046
4047 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4048 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4049 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00004050 (br_dev &&
4051 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
John Fastabend815cccb2012-10-24 08:13:09 +00004052 (dev->addr_len &&
4053 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02004054 (dev->ifindex != dev_get_iflink(dev) &&
4055 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
John Fastabend815cccb2012-10-24 08:13:09 +00004056 goto nla_put_failure;
4057
4058 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
4059 if (!br_afspec)
4060 goto nla_put_failure;
4061
Roopa Prabhu1d460b92014-12-08 14:04:20 -08004062 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
John Fastabend815cccb2012-10-24 08:13:09 +00004063 nla_nest_cancel(skb, br_afspec);
4064 goto nla_put_failure;
4065 }
Roopa Prabhu1d460b92014-12-08 14:04:20 -08004066
4067 if (mode != BRIDGE_MODE_UNDEF) {
4068 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
4069 nla_nest_cancel(skb, br_afspec);
4070 goto nla_put_failure;
4071 }
4072 }
Scott Feldman7d4f8d82015-06-22 00:27:17 -07004073 if (vlan_fill) {
4074 err = vlan_fill(skb, dev, filter_mask);
4075 if (err) {
4076 nla_nest_cancel(skb, br_afspec);
4077 goto nla_put_failure;
4078 }
4079 }
John Fastabend815cccb2012-10-24 08:13:09 +00004080 nla_nest_end(skb, br_afspec);
4081
Scott Feldman2c3c0312014-11-28 14:34:25 +01004082 protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
4083 if (!protinfo)
4084 goto nla_put_failure;
4085
4086 if (brport_nla_put_flag(skb, flags, mask,
4087 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
4088 brport_nla_put_flag(skb, flags, mask,
4089 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
4090 brport_nla_put_flag(skb, flags, mask,
4091 IFLA_BRPORT_FAST_LEAVE,
4092 BR_MULTICAST_FAST_LEAVE) ||
4093 brport_nla_put_flag(skb, flags, mask,
4094 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
4095 brport_nla_put_flag(skb, flags, mask,
4096 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
4097 brport_nla_put_flag(skb, flags, mask,
4098 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
4099 brport_nla_put_flag(skb, flags, mask,
4100 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
4101 brport_nla_put_flag(skb, flags, mask,
4102 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
4103 nla_nest_cancel(skb, protinfo);
4104 goto nla_put_failure;
4105 }
4106
4107 nla_nest_end(skb, protinfo);
4108
Johannes Berg053c0952015-01-16 22:09:00 +01004109 nlmsg_end(skb, nlh);
4110 return 0;
John Fastabend815cccb2012-10-24 08:13:09 +00004111nla_put_failure:
4112 nlmsg_cancel(skb, nlh);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07004113 return err ? err : -EMSGSIZE;
John Fastabend815cccb2012-10-24 08:13:09 +00004114}
Scott Feldman7d4f8d82015-06-22 00:27:17 -07004115EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
John Fastabend815cccb2012-10-24 08:13:09 +00004116
David Ahern2d011be2018-10-07 20:16:31 -07004117static int valid_bridge_getlink_req(const struct nlmsghdr *nlh,
4118 bool strict_check, u32 *filter_mask,
4119 struct netlink_ext_ack *extack)
4120{
4121 struct nlattr *tb[IFLA_MAX+1];
4122 int err, i;
4123
4124 if (strict_check) {
4125 struct ifinfomsg *ifm;
4126
4127 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
4128 NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump");
4129 return -EINVAL;
4130 }
4131
4132 ifm = nlmsg_data(nlh);
4133 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
4134 ifm->ifi_change || ifm->ifi_index) {
4135 NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request");
4136 return -EINVAL;
4137 }
4138
4139 err = nlmsg_parse_strict(nlh, sizeof(struct ifinfomsg), tb,
4140 IFLA_MAX, ifla_policy, extack);
4141 } else {
4142 err = nlmsg_parse(nlh, sizeof(struct ifinfomsg), tb,
4143 IFLA_MAX, ifla_policy, extack);
4144 }
4145 if (err < 0)
4146 return err;
4147
4148 /* new attributes should only be added with strict checking */
4149 for (i = 0; i <= IFLA_MAX; ++i) {
4150 if (!tb[i])
4151 continue;
4152
4153 switch (i) {
4154 case IFLA_EXT_MASK:
4155 *filter_mask = nla_get_u32(tb[i]);
4156 break;
4157 default:
4158 if (strict_check) {
4159 NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request");
4160 return -EINVAL;
4161 }
4162 }
4163 }
4164
4165 return 0;
4166}
4167
John Fastabende5a55a82012-10-24 08:12:57 +00004168static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
4169{
David Ahern2d011be2018-10-07 20:16:31 -07004170 const struct nlmsghdr *nlh = cb->nlh;
John Fastabende5a55a82012-10-24 08:12:57 +00004171 struct net *net = sock_net(skb->sk);
4172 struct net_device *dev;
4173 int idx = 0;
4174 u32 portid = NETLINK_CB(cb->skb).portid;
David Ahern2d011be2018-10-07 20:16:31 -07004175 u32 seq = nlh->nlmsg_seq;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00004176 u32 filter_mask = 0;
Roopa Prabhud64f69b2015-09-15 14:44:29 -07004177 int err;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00004178
David Ahern2d011be2018-10-07 20:16:31 -07004179 err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask,
4180 cb->extack);
4181 if (err < 0 && cb->strict_check)
4182 return err;
John Fastabende5a55a82012-10-24 08:12:57 +00004183
4184 rcu_read_lock();
4185 for_each_netdev_rcu(net, dev) {
4186 const struct net_device_ops *ops = dev->netdev_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00004187 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
John Fastabende5a55a82012-10-24 08:12:57 +00004188
Jiri Pirko898e5062013-01-03 22:48:52 +00004189 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07004190 if (idx >= cb->args[0]) {
4191 err = br_dev->netdev_ops->ndo_bridge_getlink(
4192 skb, portid, seq, dev,
4193 filter_mask, NLM_F_MULTI);
David Ahernf6c57752017-05-15 23:19:17 -07004194 if (err < 0 && err != -EOPNOTSUPP) {
4195 if (likely(skb->len))
4196 break;
4197
4198 goto out_err;
4199 }
Roopa Prabhud64f69b2015-09-15 14:44:29 -07004200 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00004201 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00004202 }
4203
4204 if (ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07004205 if (idx >= cb->args[0]) {
4206 err = ops->ndo_bridge_getlink(skb, portid,
4207 seq, dev,
4208 filter_mask,
4209 NLM_F_MULTI);
David Ahernf6c57752017-05-15 23:19:17 -07004210 if (err < 0 && err != -EOPNOTSUPP) {
4211 if (likely(skb->len))
4212 break;
4213
4214 goto out_err;
4215 }
Roopa Prabhud64f69b2015-09-15 14:44:29 -07004216 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00004217 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00004218 }
4219 }
David Ahernf6c57752017-05-15 23:19:17 -07004220 err = skb->len;
4221out_err:
John Fastabende5a55a82012-10-24 08:12:57 +00004222 rcu_read_unlock();
4223 cb->args[0] = idx;
4224
David Ahernf6c57752017-05-15 23:19:17 -07004225 return err;
John Fastabende5a55a82012-10-24 08:12:57 +00004226}
4227
John Fastabend2469ffd2012-10-24 08:13:03 +00004228static inline size_t bridge_nlmsg_size(void)
4229{
4230 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4231 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4232 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4233 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
4234 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
4235 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
4236 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
4237 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
4238 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
4239 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
4240 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
4241}
4242
Roopa Prabhu02dba432015-01-14 20:02:25 -08004243static int rtnl_bridge_notify(struct net_device *dev)
John Fastabend2469ffd2012-10-24 08:13:03 +00004244{
4245 struct net *net = dev_net(dev);
John Fastabend2469ffd2012-10-24 08:13:03 +00004246 struct sk_buff *skb;
4247 int err = -EOPNOTSUPP;
4248
Roopa Prabhu02dba432015-01-14 20:02:25 -08004249 if (!dev->netdev_ops->ndo_bridge_getlink)
4250 return 0;
4251
John Fastabend2469ffd2012-10-24 08:13:03 +00004252 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
4253 if (!skb) {
4254 err = -ENOMEM;
4255 goto errout;
4256 }
4257
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02004258 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
Roopa Prabhu02dba432015-01-14 20:02:25 -08004259 if (err < 0)
4260 goto errout;
John Fastabend2469ffd2012-10-24 08:13:03 +00004261
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08004262 if (!skb->len)
4263 goto errout;
4264
John Fastabend2469ffd2012-10-24 08:13:03 +00004265 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
4266 return 0;
4267errout:
4268 WARN_ON(err == -EMSGSIZE);
4269 kfree_skb(skb);
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08004270 if (err)
4271 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
John Fastabend2469ffd2012-10-24 08:13:03 +00004272 return err;
4273}
4274
David Ahernc21ef3e2017-04-16 09:48:24 -07004275static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
4276 struct netlink_ext_ack *extack)
John Fastabende5a55a82012-10-24 08:12:57 +00004277{
4278 struct net *net = sock_net(skb->sk);
4279 struct ifinfomsg *ifm;
4280 struct net_device *dev;
John Fastabend2469ffd2012-10-24 08:13:03 +00004281 struct nlattr *br_spec, *attr = NULL;
4282 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02004283 u16 flags = 0;
John Fastabendc38e01b2012-11-02 16:32:36 +00004284 bool have_flags = false;
John Fastabende5a55a82012-10-24 08:12:57 +00004285
4286 if (nlmsg_len(nlh) < sizeof(*ifm))
4287 return -EINVAL;
4288
4289 ifm = nlmsg_data(nlh);
4290 if (ifm->ifi_family != AF_BRIDGE)
4291 return -EPFNOSUPPORT;
4292
4293 dev = __dev_get_by_index(net, ifm->ifi_index);
4294 if (!dev) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004295 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabende5a55a82012-10-24 08:12:57 +00004296 return -ENODEV;
4297 }
4298
John Fastabend2469ffd2012-10-24 08:13:03 +00004299 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4300 if (br_spec) {
4301 nla_for_each_nested(attr, br_spec, rem) {
4302 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01004303 if (nla_len(attr) < sizeof(flags))
4304 return -EINVAL;
4305
John Fastabendc38e01b2012-11-02 16:32:36 +00004306 have_flags = true;
John Fastabend2469ffd2012-10-24 08:13:03 +00004307 flags = nla_get_u16(attr);
4308 break;
4309 }
4310 }
4311 }
4312
4313 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00004314 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4315
4316 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
John Fastabend2469ffd2012-10-24 08:13:03 +00004317 err = -EOPNOTSUPP;
4318 goto out;
4319 }
4320
Roopa Prabhuadd511b2015-01-29 22:40:12 -08004321 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
John Fastabende5a55a82012-10-24 08:12:57 +00004322 if (err)
4323 goto out;
John Fastabend2469ffd2012-10-24 08:13:03 +00004324
4325 flags &= ~BRIDGE_FLAGS_MASTER;
John Fastabende5a55a82012-10-24 08:12:57 +00004326 }
4327
John Fastabend2469ffd2012-10-24 08:13:03 +00004328 if ((flags & BRIDGE_FLAGS_SELF)) {
4329 if (!dev->netdev_ops->ndo_bridge_setlink)
4330 err = -EOPNOTSUPP;
4331 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08004332 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
4333 flags);
Roopa Prabhu02dba432015-01-14 20:02:25 -08004334 if (!err) {
John Fastabend2469ffd2012-10-24 08:13:03 +00004335 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08004336
4337 /* Generate event to notify upper layer of bridge
4338 * change
4339 */
4340 err = rtnl_bridge_notify(dev);
4341 }
John Fastabend2469ffd2012-10-24 08:13:03 +00004342 }
4343
John Fastabendc38e01b2012-11-02 16:32:36 +00004344 if (have_flags)
John Fastabend2469ffd2012-10-24 08:13:03 +00004345 memcpy(nla_data(attr), &flags, sizeof(flags));
John Fastabende5a55a82012-10-24 08:12:57 +00004346out:
4347 return err;
4348}
4349
David Ahernc21ef3e2017-04-16 09:48:24 -07004350static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
4351 struct netlink_ext_ack *extack)
Vlad Yasevich407af322013-02-13 12:00:12 +00004352{
4353 struct net *net = sock_net(skb->sk);
4354 struct ifinfomsg *ifm;
4355 struct net_device *dev;
4356 struct nlattr *br_spec, *attr = NULL;
4357 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02004358 u16 flags = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +00004359 bool have_flags = false;
4360
4361 if (nlmsg_len(nlh) < sizeof(*ifm))
4362 return -EINVAL;
4363
4364 ifm = nlmsg_data(nlh);
4365 if (ifm->ifi_family != AF_BRIDGE)
4366 return -EPFNOSUPPORT;
4367
4368 dev = __dev_get_by_index(net, ifm->ifi_index);
4369 if (!dev) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004370 NL_SET_ERR_MSG(extack, "unknown ifindex");
Vlad Yasevich407af322013-02-13 12:00:12 +00004371 return -ENODEV;
4372 }
4373
4374 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4375 if (br_spec) {
4376 nla_for_each_nested(attr, br_spec, rem) {
4377 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01004378 if (nla_len(attr) < sizeof(flags))
4379 return -EINVAL;
4380
Vlad Yasevich407af322013-02-13 12:00:12 +00004381 have_flags = true;
4382 flags = nla_get_u16(attr);
4383 break;
4384 }
4385 }
4386 }
4387
Vlad Yasevich407af322013-02-13 12:00:12 +00004388 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
4389 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4390
4391 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
4392 err = -EOPNOTSUPP;
4393 goto out;
4394 }
4395
Roopa Prabhuadd511b2015-01-29 22:40:12 -08004396 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00004397 if (err)
4398 goto out;
4399
4400 flags &= ~BRIDGE_FLAGS_MASTER;
4401 }
4402
4403 if ((flags & BRIDGE_FLAGS_SELF)) {
4404 if (!dev->netdev_ops->ndo_bridge_dellink)
4405 err = -EOPNOTSUPP;
4406 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08004407 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
4408 flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00004409
Roopa Prabhu02dba432015-01-14 20:02:25 -08004410 if (!err) {
Vlad Yasevich407af322013-02-13 12:00:12 +00004411 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08004412
4413 /* Generate event to notify upper layer of bridge
4414 * change
4415 */
4416 err = rtnl_bridge_notify(dev);
4417 }
Vlad Yasevich407af322013-02-13 12:00:12 +00004418 }
4419
4420 if (have_flags)
4421 memcpy(nla_data(attr), &flags, sizeof(flags));
Vlad Yasevich407af322013-02-13 12:00:12 +00004422out:
4423 return err;
4424}
4425
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004426static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
4427{
4428 return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
4429 (!idxattr || idxattr == attrid);
4430}
4431
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004432#define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
4433static int rtnl_get_offload_stats_attr_size(int attr_id)
4434{
4435 switch (attr_id) {
4436 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
4437 return sizeof(struct rtnl_link_stats64);
4438 }
4439
4440 return 0;
4441}
4442
4443static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
4444 int *prividx)
4445{
4446 struct nlattr *attr = NULL;
4447 int attr_id, size;
4448 void *attr_data;
4449 int err;
4450
4451 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
4452 dev->netdev_ops->ndo_get_offload_stats))
4453 return -ENODATA;
4454
4455 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
4456 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
4457 if (attr_id < *prividx)
4458 continue;
4459
4460 size = rtnl_get_offload_stats_attr_size(attr_id);
4461 if (!size)
4462 continue;
4463
Or Gerlitz3df5b3c2016-11-22 23:09:54 +02004464 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004465 continue;
4466
4467 attr = nla_reserve_64bit(skb, attr_id, size,
4468 IFLA_OFFLOAD_XSTATS_UNSPEC);
4469 if (!attr)
4470 goto nla_put_failure;
4471
4472 attr_data = nla_data(attr);
4473 memset(attr_data, 0, size);
4474 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
4475 attr_data);
4476 if (err)
4477 goto get_offload_stats_failure;
4478 }
4479
4480 if (!attr)
4481 return -ENODATA;
4482
4483 *prividx = 0;
4484 return 0;
4485
4486nla_put_failure:
4487 err = -EMSGSIZE;
4488get_offload_stats_failure:
4489 *prividx = attr_id;
4490 return err;
4491}
4492
4493static int rtnl_get_offload_stats_size(const struct net_device *dev)
4494{
4495 int nla_size = 0;
4496 int attr_id;
4497 int size;
4498
4499 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
4500 dev->netdev_ops->ndo_get_offload_stats))
4501 return 0;
4502
4503 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
4504 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
Or Gerlitz3df5b3c2016-11-22 23:09:54 +02004505 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004506 continue;
4507 size = rtnl_get_offload_stats_attr_size(attr_id);
4508 nla_size += nla_total_size_64bit(size);
4509 }
4510
4511 if (nla_size != 0)
4512 nla_size += nla_total_size(0);
4513
4514 return nla_size;
4515}
4516
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004517static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
4518 int type, u32 pid, u32 seq, u32 change,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004519 unsigned int flags, unsigned int filter_mask,
4520 int *idxattr, int *prividx)
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004521{
4522 struct if_stats_msg *ifsm;
4523 struct nlmsghdr *nlh;
4524 struct nlattr *attr;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004525 int s_prividx = *prividx;
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004526 int err;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004527
4528 ASSERT_RTNL();
4529
4530 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
4531 if (!nlh)
4532 return -EMSGSIZE;
4533
4534 ifsm = nlmsg_data(nlh);
Nikolay Aleksandrovce024f42017-10-03 13:20:48 +03004535 ifsm->family = PF_UNSPEC;
4536 ifsm->pad1 = 0;
4537 ifsm->pad2 = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004538 ifsm->ifindex = dev->ifindex;
4539 ifsm->filter_mask = filter_mask;
4540
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004541 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004542 struct rtnl_link_stats64 *sp;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004543
Nicolas Dichtel58414d32016-04-21 18:58:25 +02004544 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
4545 sizeof(struct rtnl_link_stats64),
4546 IFLA_STATS_UNSPEC);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004547 if (!attr)
4548 goto nla_put_failure;
4549
4550 sp = nla_data(attr);
4551 dev_get_stats(dev, sp);
4552 }
4553
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004554 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
4555 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
4556
4557 if (ops && ops->fill_linkxstats) {
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004558 *idxattr = IFLA_STATS_LINK_XSTATS;
4559 attr = nla_nest_start(skb,
4560 IFLA_STATS_LINK_XSTATS);
4561 if (!attr)
4562 goto nla_put_failure;
4563
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004564 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
4565 nla_nest_end(skb, attr);
4566 if (err)
4567 goto nla_put_failure;
4568 *idxattr = 0;
4569 }
4570 }
4571
4572 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
4573 *idxattr)) {
4574 const struct rtnl_link_ops *ops = NULL;
4575 const struct net_device *master;
4576
4577 master = netdev_master_upper_dev_get(dev);
4578 if (master)
4579 ops = master->rtnl_link_ops;
4580 if (ops && ops->fill_linkxstats) {
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004581 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
4582 attr = nla_nest_start(skb,
4583 IFLA_STATS_LINK_XSTATS_SLAVE);
4584 if (!attr)
4585 goto nla_put_failure;
4586
4587 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004588 nla_nest_end(skb, attr);
4589 if (err)
4590 goto nla_put_failure;
4591 *idxattr = 0;
4592 }
4593 }
4594
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004595 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
4596 *idxattr)) {
4597 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
4598 attr = nla_nest_start(skb, IFLA_STATS_LINK_OFFLOAD_XSTATS);
4599 if (!attr)
4600 goto nla_put_failure;
4601
4602 err = rtnl_get_offload_stats(skb, dev, prividx);
4603 if (err == -ENODATA)
4604 nla_nest_cancel(skb, attr);
4605 else
4606 nla_nest_end(skb, attr);
4607
4608 if (err && err != -ENODATA)
4609 goto nla_put_failure;
4610 *idxattr = 0;
4611 }
4612
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004613 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
4614 struct rtnl_af_ops *af_ops;
4615
4616 *idxattr = IFLA_STATS_AF_SPEC;
4617 attr = nla_nest_start(skb, IFLA_STATS_AF_SPEC);
4618 if (!attr)
4619 goto nla_put_failure;
4620
Florian Westphal5fa85a02017-10-16 15:44:36 +02004621 rcu_read_lock();
4622 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004623 if (af_ops->fill_stats_af) {
4624 struct nlattr *af;
4625 int err;
4626
4627 af = nla_nest_start(skb, af_ops->family);
Florian Westphal5fa85a02017-10-16 15:44:36 +02004628 if (!af) {
4629 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004630 goto nla_put_failure;
Florian Westphal5fa85a02017-10-16 15:44:36 +02004631 }
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004632 err = af_ops->fill_stats_af(skb, dev);
4633
Florian Westphal5fa85a02017-10-16 15:44:36 +02004634 if (err == -ENODATA) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004635 nla_nest_cancel(skb, af);
Florian Westphal5fa85a02017-10-16 15:44:36 +02004636 } else if (err < 0) {
4637 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004638 goto nla_put_failure;
Florian Westphal5fa85a02017-10-16 15:44:36 +02004639 }
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004640
4641 nla_nest_end(skb, af);
4642 }
4643 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02004644 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004645
4646 nla_nest_end(skb, attr);
4647
4648 *idxattr = 0;
4649 }
4650
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004651 nlmsg_end(skb, nlh);
4652
4653 return 0;
4654
4655nla_put_failure:
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004656 /* not a multi message or no progress mean a real error */
4657 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
4658 nlmsg_cancel(skb, nlh);
4659 else
4660 nlmsg_end(skb, nlh);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004661
4662 return -EMSGSIZE;
4663}
4664
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004665static size_t if_nlmsg_stats_size(const struct net_device *dev,
4666 u32 filter_mask)
4667{
4668 size_t size = 0;
4669
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004670 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004671 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
4672
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004673 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
4674 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004675 int attr = IFLA_STATS_LINK_XSTATS;
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004676
4677 if (ops && ops->get_linkxstats_size) {
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004678 size += nla_total_size(ops->get_linkxstats_size(dev,
4679 attr));
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004680 /* for IFLA_STATS_LINK_XSTATS */
4681 size += nla_total_size(0);
4682 }
4683 }
4684
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004685 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
4686 struct net_device *_dev = (struct net_device *)dev;
4687 const struct rtnl_link_ops *ops = NULL;
4688 const struct net_device *master;
4689
4690 /* netdev_master_upper_dev_get can't take const */
4691 master = netdev_master_upper_dev_get(_dev);
4692 if (master)
4693 ops = master->rtnl_link_ops;
4694 if (ops && ops->get_linkxstats_size) {
4695 int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
4696
4697 size += nla_total_size(ops->get_linkxstats_size(dev,
4698 attr));
4699 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
4700 size += nla_total_size(0);
4701 }
4702 }
4703
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004704 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
4705 size += rtnl_get_offload_stats_size(dev);
4706
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004707 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
4708 struct rtnl_af_ops *af_ops;
4709
4710 /* for IFLA_STATS_AF_SPEC */
4711 size += nla_total_size(0);
4712
Florian Westphal5fa85a02017-10-16 15:44:36 +02004713 rcu_read_lock();
4714 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004715 if (af_ops->get_stats_af_size) {
4716 size += nla_total_size(
4717 af_ops->get_stats_af_size(dev));
4718
4719 /* for AF_* */
4720 size += nla_total_size(0);
4721 }
4722 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02004723 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004724 }
4725
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004726 return size;
4727}
4728
David Ahernc21ef3e2017-04-16 09:48:24 -07004729static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
4730 struct netlink_ext_ack *extack)
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004731{
4732 struct net *net = sock_net(skb->sk);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004733 struct net_device *dev = NULL;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004734 int idxattr = 0, prividx = 0;
4735 struct if_stats_msg *ifsm;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004736 struct sk_buff *nskb;
4737 u32 filter_mask;
4738 int err;
4739
Mathias Krause4775cc12016-12-28 17:52:15 +01004740 if (nlmsg_len(nlh) < sizeof(*ifsm))
4741 return -EINVAL;
4742
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004743 ifsm = nlmsg_data(nlh);
4744 if (ifsm->ifindex > 0)
4745 dev = __dev_get_by_index(net, ifsm->ifindex);
4746 else
4747 return -EINVAL;
4748
4749 if (!dev)
4750 return -ENODEV;
4751
4752 filter_mask = ifsm->filter_mask;
4753 if (!filter_mask)
4754 return -EINVAL;
4755
4756 nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
4757 if (!nskb)
4758 return -ENOBUFS;
4759
4760 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
4761 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004762 0, filter_mask, &idxattr, &prividx);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004763 if (err < 0) {
4764 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
4765 WARN_ON(err == -EMSGSIZE);
4766 kfree_skb(nskb);
4767 } else {
4768 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
4769 }
4770
4771 return err;
4772}
4773
4774static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
4775{
David Ahern841891e2018-10-07 20:16:32 -07004776 struct netlink_ext_ack *extack = cb->extack;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004777 int h, s_h, err, s_idx, s_idxattr, s_prividx;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004778 struct net *net = sock_net(skb->sk);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004779 unsigned int flags = NLM_F_MULTI;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004780 struct if_stats_msg *ifsm;
4781 struct hlist_head *head;
4782 struct net_device *dev;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004783 u32 filter_mask = 0;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004784 int idx = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004785
4786 s_h = cb->args[0];
4787 s_idx = cb->args[1];
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004788 s_idxattr = cb->args[2];
4789 s_prividx = cb->args[3];
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004790
4791 cb->seq = net->dev_base_seq;
4792
David Ahern841891e2018-10-07 20:16:32 -07004793 if (nlmsg_len(cb->nlh) < sizeof(*ifsm)) {
4794 NL_SET_ERR_MSG(extack, "Invalid header for stats dump");
Mathias Krause4775cc12016-12-28 17:52:15 +01004795 return -EINVAL;
David Ahern841891e2018-10-07 20:16:32 -07004796 }
Mathias Krause4775cc12016-12-28 17:52:15 +01004797
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004798 ifsm = nlmsg_data(cb->nlh);
David Ahern841891e2018-10-07 20:16:32 -07004799
David Aherne75fa072018-10-08 13:58:07 -07004800 /* only requests using strict checks can pass data to influence
4801 * the dump. The legacy exception is filter_mask.
David Ahern841891e2018-10-07 20:16:32 -07004802 */
4803 if (cb->strict_check) {
4804 if (ifsm->pad1 || ifsm->pad2 || ifsm->ifindex) {
4805 NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request");
4806 return -EINVAL;
4807 }
4808 if (nlmsg_attrlen(cb->nlh, sizeof(*ifsm))) {
4809 NL_SET_ERR_MSG(extack, "Invalid attributes after stats header");
4810 return -EINVAL;
4811 }
4812 }
4813
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004814 filter_mask = ifsm->filter_mask;
David Ahern841891e2018-10-07 20:16:32 -07004815 if (!filter_mask) {
4816 NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump");
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004817 return -EINVAL;
David Ahern841891e2018-10-07 20:16:32 -07004818 }
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004819
4820 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4821 idx = 0;
4822 head = &net->dev_index_head[h];
4823 hlist_for_each_entry(dev, head, index_hlist) {
4824 if (idx < s_idx)
4825 goto cont;
4826 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
4827 NETLINK_CB(cb->skb).portid,
4828 cb->nlh->nlmsg_seq, 0,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004829 flags, filter_mask,
4830 &s_idxattr, &s_prividx);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004831 /* If we ran out of room on the first message,
4832 * we're in trouble
4833 */
4834 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
4835
4836 if (err < 0)
4837 goto out;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004838 s_prividx = 0;
4839 s_idxattr = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004840 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
4841cont:
4842 idx++;
4843 }
4844 }
4845out:
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004846 cb->args[3] = s_prividx;
4847 cb->args[2] = s_idxattr;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004848 cb->args[1] = idx;
4849 cb->args[0] = h;
4850
4851 return skb->len;
4852}
4853
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854/* Process one rtnetlink message. */
4855
Johannes Berg2d4bc932017-04-12 14:34:04 +02004856static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
4857 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004859 struct net *net = sock_net(skb->sk);
Florian Westphaladdf9b92017-12-02 21:44:05 +01004860 struct rtnl_link *link;
Florian Westphale4202512017-12-02 21:44:06 +01004861 struct module *owner;
Florian Westphal6853dd42017-08-09 20:41:51 +02004862 int err = -EOPNOTSUPP;
Thomas Grafe2849862007-03-22 11:48:11 -07004863 rtnl_doit_func doit;
Florian Westphal62256f92017-08-09 20:41:52 +02004864 unsigned int flags;
Alexander Kuleshov617cfc72016-01-10 21:26:57 +06004865 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866 int family;
4867 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 if (type > RTM_MAX)
Thomas Graf038890f2007-04-05 14:35:52 -07004871 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872
4873 type -= RTM_BASE;
4874
4875 /* All the messages must have at least 1 byte length */
Hong zhi guo573ce262013-03-27 06:47:04 +00004876 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877 return 0;
4878
Hong zhi guo573ce262013-03-27 06:47:04 +00004879 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 kind = type&3;
4881
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07004882 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07004883 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884
Florian Westphal6853dd42017-08-09 20:41:51 +02004885 rcu_read_lock();
David S. Millerb8f3ab42011-01-18 12:40:38 -08004886 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004887 struct sock *rtnl;
Thomas Grafe2849862007-03-22 11:48:11 -07004888 rtnl_dumpit_func dumpit;
Greg Rosec7ac8672011-06-10 01:27:09 +00004889 u16 min_dump_alloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890
Florian Westphaladdf9b92017-12-02 21:44:05 +01004891 link = rtnl_get_link(family, type);
4892 if (!link || !link->dumpit) {
Florian Westphal6853dd42017-08-09 20:41:51 +02004893 family = PF_UNSPEC;
Florian Westphaladdf9b92017-12-02 21:44:05 +01004894 link = rtnl_get_link(family, type);
4895 if (!link || !link->dumpit)
Florian Westphal6853dd42017-08-09 20:41:51 +02004896 goto err_unlock;
4897 }
Florian Westphale4202512017-12-02 21:44:06 +01004898 owner = link->owner;
Florian Westphaladdf9b92017-12-02 21:44:05 +01004899 dumpit = link->dumpit;
Florian Westphale1fa6d22017-08-09 20:41:47 +02004900
Florian Westphal5c2bb9b2017-08-10 16:52:58 +02004901 if (type == RTM_GETLINK - RTM_BASE)
Florian Westphale1fa6d22017-08-09 20:41:47 +02004902 min_dump_alloc = rtnl_calcit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903
Florian Westphale4202512017-12-02 21:44:06 +01004904 err = 0;
4905 /* need to do this before rcu_read_unlock() */
4906 if (!try_module_get(owner))
4907 err = -EPROTONOSUPPORT;
4908
Florian Westphal6853dd42017-08-09 20:41:51 +02004909 rcu_read_unlock();
4910
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004911 rtnl = net->rtnl;
Florian Westphale4202512017-12-02 21:44:06 +01004912 if (err == 0) {
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00004913 struct netlink_dump_control c = {
4914 .dump = dumpit,
4915 .min_dump_alloc = min_dump_alloc,
Florian Westphale4202512017-12-02 21:44:06 +01004916 .module = owner,
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00004917 };
4918 err = netlink_dump_start(rtnl, skb, nlh, &c);
Florian Westphale4202512017-12-02 21:44:06 +01004919 /* netlink_dump_start() will keep a reference on
4920 * module if dump is still in progress.
4921 */
4922 module_put(owner);
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00004923 }
Eric Dumazet2907c352011-05-25 07:34:04 +00004924 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 }
4926
Florian Westphaladdf9b92017-12-02 21:44:05 +01004927 link = rtnl_get_link(family, type);
4928 if (!link || !link->doit) {
Florian Westphal8caa38b2017-08-10 16:53:01 +02004929 family = PF_UNSPEC;
Florian Westphaladdf9b92017-12-02 21:44:05 +01004930 link = rtnl_get_link(PF_UNSPEC, type);
4931 if (!link || !link->doit)
4932 goto out_unlock;
Florian Westphal8caa38b2017-08-10 16:53:01 +02004933 }
4934
Florian Westphale4202512017-12-02 21:44:06 +01004935 owner = link->owner;
4936 if (!try_module_get(owner)) {
4937 err = -EPROTONOSUPPORT;
4938 goto out_unlock;
4939 }
4940
Florian Westphaladdf9b92017-12-02 21:44:05 +01004941 flags = link->flags;
Florian Westphal62256f92017-08-09 20:41:52 +02004942 if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
Florian Westphaladdf9b92017-12-02 21:44:05 +01004943 doit = link->doit;
Florian Westphal62256f92017-08-09 20:41:52 +02004944 rcu_read_unlock();
4945 if (doit)
4946 err = doit(skb, nlh, extack);
Florian Westphale4202512017-12-02 21:44:06 +01004947 module_put(owner);
Florian Westphal62256f92017-08-09 20:41:52 +02004948 return err;
4949 }
Florian Westphal6853dd42017-08-09 20:41:51 +02004950 rcu_read_unlock();
4951
Florian Westphal0cc09022017-08-09 20:41:50 +02004952 rtnl_lock();
Florian Westphaladdf9b92017-12-02 21:44:05 +01004953 link = rtnl_get_link(family, type);
4954 if (link && link->doit)
4955 err = link->doit(skb, nlh, extack);
Florian Westphal0cc09022017-08-09 20:41:50 +02004956 rtnl_unlock();
Florian Westphaladdf9b92017-12-02 21:44:05 +01004957
Florian Westphale4202512017-12-02 21:44:06 +01004958 module_put(owner);
4959
Florian Westphaladdf9b92017-12-02 21:44:05 +01004960 return err;
4961
4962out_unlock:
4963 rcu_read_unlock();
Florian Westphal0cc09022017-08-09 20:41:50 +02004964 return err;
4965
4966err_unlock:
Florian Westphal6853dd42017-08-09 20:41:51 +02004967 rcu_read_unlock();
Florian Westphal0cc09022017-08-09 20:41:50 +02004968 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969}
4970
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07004971static void rtnetlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07004973 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974}
4975
Julien Gomes5f729ea2017-06-20 13:54:16 -07004976static int rtnetlink_bind(struct net *net, int group)
4977{
4978 switch (group) {
4979 case RTNLGRP_IPV4_MROUTE_R:
4980 case RTNLGRP_IPV6_MROUTE_R:
4981 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
4982 return -EPERM;
4983 break;
4984 }
4985 return 0;
4986}
4987
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
4989{
Jiri Pirko351638e2013-05-28 01:30:21 +00004990 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02004991
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 switch (event) {
Vlad Yasevich5138e862017-04-04 09:23:41 -04004993 case NETDEV_REBOOT:
Xin Long8a212582017-10-15 18:13:41 +08004994 case NETDEV_CHANGEMTU:
David Ahern37536542017-07-19 10:22:40 -07004995 case NETDEV_CHANGEADDR:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004996 case NETDEV_CHANGENAME:
4997 case NETDEV_FEAT_CHANGE:
4998 case NETDEV_BONDING_FAILOVER:
Xin Longe6e66592017-10-15 18:13:43 +08004999 case NETDEV_POST_TYPE_CHANGE:
Vlad Yasevich5138e862017-04-04 09:23:41 -04005000 case NETDEV_NOTIFY_PEERS:
Xin Longdc709f32017-10-15 18:13:44 +08005001 case NETDEV_CHANGEUPPER:
Vlad Yasevich5138e862017-04-04 09:23:41 -04005002 case NETDEV_RESEND_IGMP:
Vlad Yasevich5138e862017-04-04 09:23:41 -04005003 case NETDEV_CHANGEINFODATA:
Xin Longeeda3fb2017-10-24 13:54:19 +08005004 case NETDEV_CHANGELOWERSTATE:
Xin Longebdcf042017-10-15 18:13:42 +08005005 case NETDEV_CHANGE_TX_QUEUE_LEN:
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04005006 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01005007 GFP_KERNEL, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008 break;
5009 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010 break;
5011 }
5012 return NOTIFY_DONE;
5013}
5014
5015static struct notifier_block rtnetlink_dev_notifier = {
5016 .notifier_call = rtnetlink_event,
5017};
5018
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005019
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005020static int __net_init rtnetlink_net_init(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005021{
5022 struct sock *sk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00005023 struct netlink_kernel_cfg cfg = {
5024 .groups = RTNLGRP_MAX,
5025 .input = rtnetlink_rcv,
5026 .cb_mutex = &rtnl_mutex,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00005027 .flags = NL_CFG_F_NONROOT_RECV,
Julien Gomes5f729ea2017-06-20 13:54:16 -07005028 .bind = rtnetlink_bind,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00005029 };
5030
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00005031 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005032 if (!sk)
5033 return -ENOMEM;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005034 net->rtnl = sk;
5035 return 0;
5036}
5037
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005038static void __net_exit rtnetlink_net_exit(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005039{
Denis V. Lunev775516b2008-01-18 23:55:19 -08005040 netlink_kernel_release(net->rtnl);
5041 net->rtnl = NULL;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005042}
5043
5044static struct pernet_operations rtnetlink_net_ops = {
5045 .init = rtnetlink_net_init,
5046 .exit = rtnetlink_net_exit,
5047};
5048
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049void __init rtnetlink_init(void)
5050{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005051 if (register_pernet_subsys(&rtnetlink_net_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052 panic("rtnetlink_init: cannot initialize rtnetlink\n");
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005053
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054 register_netdevice_notifier(&rtnetlink_dev_notifier);
Thomas Graf340d17f2007-03-22 11:49:22 -07005055
Greg Rosec7ac8672011-06-10 01:27:09 +00005056 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
Florian Westphalb97bac62017-08-09 20:41:48 +02005057 rtnl_dump_ifinfo, 0);
5058 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
5059 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
5060 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
Thomas Graf687ad8c2007-03-22 11:59:42 -07005061
Florian Westphalb97bac62017-08-09 20:41:48 +02005062 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
5063 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
5064 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
John Fastabend77162022012-04-15 06:43:56 +00005065
Florian Westphalb97bac62017-08-09 20:41:48 +02005066 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
5067 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
5068 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, 0);
John Fastabende5a55a82012-10-24 08:12:57 +00005069
Florian Westphalb97bac62017-08-09 20:41:48 +02005070 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
5071 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
5072 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005073
5074 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
Florian Westphalb97bac62017-08-09 20:41:48 +02005075 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076}