blob: f6d2609cfa9fb75f19d84072293d3389b237fe51 [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
Kees Cookccf8dbc2018-05-30 15:20:52 -070062#define RTNL_MAX_TYPE 48
63#define RTNL_SLAVE_MAX_TYPE 36
64
Eric Dumazete0d087a2009-11-07 01:26:17 -080065struct rtnl_link {
Thomas Grafe2849862007-03-22 11:48:11 -070066 rtnl_doit_func doit;
67 rtnl_dumpit_func dumpit;
Florian Westphale4202512017-12-02 21:44:06 +010068 struct module *owner;
Florian Westphal62256f92017-08-09 20:41:52 +020069 unsigned int flags;
Florian Westphaladdf9b92017-12-02 21:44:05 +010070 struct rcu_head rcu;
Thomas Grafe2849862007-03-22 11:48:11 -070071};
72
Stephen Hemminger6756ae42006-03-20 22:23:58 -080073static DEFINE_MUTEX(rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75void rtnl_lock(void)
76{
Stephen Hemminger6756ae42006-03-20 22:23:58 -080077 mutex_lock(&rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
Eric Dumazete0d087a2009-11-07 01:26:17 -080079EXPORT_SYMBOL(rtnl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Kirill Tkhai79ffdfc2018-03-14 22:17:20 +030081int rtnl_lock_killable(void)
82{
83 return mutex_lock_killable(&rtnl_mutex);
84}
85EXPORT_SYMBOL(rtnl_lock_killable);
86
Eric Dumazet1b5c5492016-06-13 20:21:50 -070087static struct sk_buff *defer_kfree_skb_list;
88void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail)
89{
90 if (head && tail) {
91 tail->next = defer_kfree_skb_list;
92 defer_kfree_skb_list = head;
93 }
94}
95EXPORT_SYMBOL(rtnl_kfree_skbs);
96
Stephen Hemminger6756ae42006-03-20 22:23:58 -080097void __rtnl_unlock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Eric Dumazet1b5c5492016-06-13 20:21:50 -070099 struct sk_buff *head = defer_kfree_skb_list;
100
101 defer_kfree_skb_list = NULL;
102
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800103 mutex_unlock(&rtnl_mutex);
Eric Dumazet1b5c5492016-06-13 20:21:50 -0700104
105 while (head) {
106 struct sk_buff *next = head->next;
107
108 kfree_skb(head);
109 cond_resched();
110 head = next;
111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114void rtnl_unlock(void)
115{
Herbert Xu58ec3b42008-10-07 15:50:03 -0700116 /* This fellow will unlock it for us. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 netdev_run_todo();
118}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800119EXPORT_SYMBOL(rtnl_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800121int rtnl_trylock(void)
122{
123 return mutex_trylock(&rtnl_mutex);
124}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800125EXPORT_SYMBOL(rtnl_trylock);
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800126
Patrick McHardyc9c10142008-04-23 22:10:48 -0700127int rtnl_is_locked(void)
128{
129 return mutex_is_locked(&rtnl_mutex);
130}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800131EXPORT_SYMBOL(rtnl_is_locked);
Patrick McHardyc9c10142008-04-23 22:10:48 -0700132
Vlad Buslov6f995282018-09-24 19:22:49 +0300133bool refcount_dec_and_rtnl_lock(refcount_t *r)
134{
135 return refcount_dec_and_mutex_lock(r, &rtnl_mutex);
136}
137EXPORT_SYMBOL(refcount_dec_and_rtnl_lock);
138
Paul E. McKenneya898def2010-02-22 17:04:49 -0800139#ifdef CONFIG_PROVE_LOCKING
Yaowei Bai0cbf3342015-10-08 21:29:02 +0800140bool lockdep_rtnl_is_held(void)
Paul E. McKenneya898def2010-02-22 17:04:49 -0800141{
142 return lockdep_is_held(&rtnl_mutex);
143}
144EXPORT_SYMBOL(lockdep_rtnl_is_held);
145#endif /* #ifdef CONFIG_PROVE_LOCKING */
146
Florian Westphalb0e9fe12017-12-04 22:42:30 +0100147static struct rtnl_link *__rcu *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
Thomas Grafe2849862007-03-22 11:48:11 -0700148
149static inline int rtm_msgindex(int msgtype)
150{
151 int msgindex = msgtype - RTM_BASE;
152
153 /*
154 * msgindex < 0 implies someone tried to register a netlink
155 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
156 * the message type has not been added to linux/rtnetlink.h
157 */
158 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
159
160 return msgindex;
161}
162
Florian Westphaladdf9b92017-12-02 21:44:05 +0100163static struct rtnl_link *rtnl_get_link(int protocol, int msgtype)
164{
165 struct rtnl_link **tab;
166
167 if (protocol >= ARRAY_SIZE(rtnl_msg_handlers))
168 protocol = PF_UNSPEC;
169
170 tab = rcu_dereference_rtnl(rtnl_msg_handlers[protocol]);
171 if (!tab)
172 tab = rcu_dereference_rtnl(rtnl_msg_handlers[PF_UNSPEC]);
173
174 return tab[msgtype];
175}
176
Florian Westphale4202512017-12-02 21:44:06 +0100177static int rtnl_register_internal(struct module *owner,
178 int protocol, int msgtype,
179 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
180 unsigned int flags)
Thomas Grafe2849862007-03-22 11:48:11 -0700181{
Florian Westphalb0e9fe12017-12-04 22:42:30 +0100182 struct rtnl_link *link, *old;
183 struct rtnl_link __rcu **tab;
Thomas Grafe2849862007-03-22 11:48:11 -0700184 int msgindex;
Florian Westphaladdf9b92017-12-02 21:44:05 +0100185 int ret = -ENOBUFS;
Thomas Grafe2849862007-03-22 11:48:11 -0700186
Patrick McHardy25239ce2010-04-26 16:02:05 +0200187 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700188 msgindex = rtm_msgindex(msgtype);
189
Florian Westphaladdf9b92017-12-02 21:44:05 +0100190 rtnl_lock();
191 tab = rtnl_msg_handlers[protocol];
Thomas Grafe2849862007-03-22 11:48:11 -0700192 if (tab == NULL) {
Florian Westphaladdf9b92017-12-02 21:44:05 +0100193 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(void *), GFP_KERNEL);
194 if (!tab)
195 goto unlock;
Thomas Grafe2849862007-03-22 11:48:11 -0700196
Florian Westphaladdf9b92017-12-02 21:44:05 +0100197 /* ensures we see the 0 stores */
Florian Westphal6853dd42017-08-09 20:41:51 +0200198 rcu_assign_pointer(rtnl_msg_handlers[protocol], tab);
Thomas Grafe2849862007-03-22 11:48:11 -0700199 }
200
Florian Westphaladdf9b92017-12-02 21:44:05 +0100201 old = rtnl_dereference(tab[msgindex]);
202 if (old) {
203 link = kmemdup(old, sizeof(*old), GFP_KERNEL);
204 if (!link)
205 goto unlock;
206 } else {
207 link = kzalloc(sizeof(*link), GFP_KERNEL);
208 if (!link)
209 goto unlock;
210 }
Thomas Grafe2849862007-03-22 11:48:11 -0700211
Florian Westphale4202512017-12-02 21:44:06 +0100212 WARN_ON(link->owner && link->owner != owner);
213 link->owner = owner;
214
Florian Westphaladdf9b92017-12-02 21:44:05 +0100215 WARN_ON(doit && link->doit && link->doit != doit);
216 if (doit)
217 link->doit = doit;
218 WARN_ON(dumpit && link->dumpit && link->dumpit != dumpit);
219 if (dumpit)
220 link->dumpit = dumpit;
221
222 link->flags |= flags;
223
224 /* publish protocol:msgtype */
225 rcu_assign_pointer(tab[msgindex], link);
226 ret = 0;
227 if (old)
228 kfree_rcu(old, rcu);
229unlock:
230 rtnl_unlock();
231 return ret;
Thomas Grafe2849862007-03-22 11:48:11 -0700232}
Florian Westphale4202512017-12-02 21:44:06 +0100233
234/**
235 * rtnl_register_module - Register a rtnetlink message type
236 *
237 * @owner: module registering the hook (THIS_MODULE)
238 * @protocol: Protocol family or PF_UNSPEC
239 * @msgtype: rtnetlink message type
240 * @doit: Function pointer called for each request message
241 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
242 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
243 *
244 * Like rtnl_register, but for use by removable modules.
245 */
246int rtnl_register_module(struct module *owner,
247 int protocol, int msgtype,
248 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
249 unsigned int flags)
250{
251 return rtnl_register_internal(owner, protocol, msgtype,
252 doit, dumpit, flags);
253}
254EXPORT_SYMBOL_GPL(rtnl_register_module);
255
256/**
Florian Westphal16feebc2017-12-02 21:44:08 +0100257 * rtnl_register - Register a rtnetlink message type
Florian Westphale4202512017-12-02 21:44:06 +0100258 * @protocol: Protocol family or PF_UNSPEC
259 * @msgtype: rtnetlink message type
260 * @doit: Function pointer called for each request message
261 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
262 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
263 *
264 * Registers the specified function pointers (at least one of them has
265 * to be non-NULL) to be called whenever a request message for the
266 * specified protocol family and message type is received.
267 *
268 * The special protocol family PF_UNSPEC may be used to define fallback
269 * function pointers for the case when no entry for the specific protocol
270 * family exists.
Thomas Grafe2849862007-03-22 11:48:11 -0700271 */
272void rtnl_register(int protocol, int msgtype,
Greg Rosec7ac8672011-06-10 01:27:09 +0000273 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
Florian Westphalb97bac62017-08-09 20:41:48 +0200274 unsigned int flags)
Thomas Grafe2849862007-03-22 11:48:11 -0700275{
Florian Westphal16feebc2017-12-02 21:44:08 +0100276 int err;
277
278 err = rtnl_register_internal(NULL, protocol, msgtype, doit, dumpit,
279 flags);
280 if (err)
281 pr_err("Unable to register rtnetlink message handler, "
282 "protocol = %d, message type = %d\n", protocol, msgtype);
Thomas Grafe2849862007-03-22 11:48:11 -0700283}
Thomas Grafe2849862007-03-22 11:48:11 -0700284
285/**
286 * rtnl_unregister - Unregister a rtnetlink message type
287 * @protocol: Protocol family or PF_UNSPEC
288 * @msgtype: rtnetlink message type
289 *
290 * Returns 0 on success or a negative error code.
291 */
292int rtnl_unregister(int protocol, int msgtype)
293{
Florian Westphaladdf9b92017-12-02 21:44:05 +0100294 struct rtnl_link **tab, *link;
Thomas Grafe2849862007-03-22 11:48:11 -0700295 int msgindex;
296
Patrick McHardy25239ce2010-04-26 16:02:05 +0200297 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700298 msgindex = rtm_msgindex(msgtype);
299
Florian Westphal6853dd42017-08-09 20:41:51 +0200300 rtnl_lock();
Florian Westphaladdf9b92017-12-02 21:44:05 +0100301 tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
302 if (!tab) {
Florian Westphal6853dd42017-08-09 20:41:51 +0200303 rtnl_unlock();
Thomas Grafe2849862007-03-22 11:48:11 -0700304 return -ENOENT;
Florian Westphal6853dd42017-08-09 20:41:51 +0200305 }
Thomas Grafe2849862007-03-22 11:48:11 -0700306
Florian Westphaladdf9b92017-12-02 21:44:05 +0100307 link = tab[msgindex];
308 rcu_assign_pointer(tab[msgindex], NULL);
Florian Westphal6853dd42017-08-09 20:41:51 +0200309 rtnl_unlock();
Thomas Grafe2849862007-03-22 11:48:11 -0700310
Florian Westphaladdf9b92017-12-02 21:44:05 +0100311 kfree_rcu(link, rcu);
312
Thomas Grafe2849862007-03-22 11:48:11 -0700313 return 0;
314}
Thomas Grafe2849862007-03-22 11:48:11 -0700315EXPORT_SYMBOL_GPL(rtnl_unregister);
316
317/**
318 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
319 * @protocol : Protocol family or PF_UNSPEC
320 *
321 * Identical to calling rtnl_unregster() for all registered message types
322 * of a certain protocol family.
323 */
324void rtnl_unregister_all(int protocol)
325{
Florian Westphaladdf9b92017-12-02 21:44:05 +0100326 struct rtnl_link **tab, *link;
327 int msgindex;
Florian Westphal019a3162017-08-09 20:41:49 +0200328
Patrick McHardy25239ce2010-04-26 16:02:05 +0200329 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700330
Florian Westphal019a3162017-08-09 20:41:49 +0200331 rtnl_lock();
Florian Westphaladdf9b92017-12-02 21:44:05 +0100332 tab = rtnl_msg_handlers[protocol];
Sabrina Dubrocaf707ef62018-08-28 13:40:53 +0200333 if (!tab) {
334 rtnl_unlock();
335 return;
336 }
Florian Westphal6853dd42017-08-09 20:41:51 +0200337 RCU_INIT_POINTER(rtnl_msg_handlers[protocol], NULL);
Florian Westphaladdf9b92017-12-02 21:44:05 +0100338 for (msgindex = 0; msgindex < RTM_NR_MSGTYPES; msgindex++) {
339 link = tab[msgindex];
340 if (!link)
341 continue;
342
343 rcu_assign_pointer(tab[msgindex], NULL);
344 kfree_rcu(link, rcu);
345 }
Florian Westphal019a3162017-08-09 20:41:49 +0200346 rtnl_unlock();
347
Florian Westphal6853dd42017-08-09 20:41:51 +0200348 synchronize_net();
349
Florian Westphaladdf9b92017-12-02 21:44:05 +0100350 kfree(tab);
Thomas Grafe2849862007-03-22 11:48:11 -0700351}
Thomas Grafe2849862007-03-22 11:48:11 -0700352EXPORT_SYMBOL_GPL(rtnl_unregister_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Patrick McHardy38f7b872007-06-13 12:03:51 -0700354static LIST_HEAD(link_ops);
355
Eric Dumazetc63044f2011-12-13 11:38:00 +0000356static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
357{
358 const struct rtnl_link_ops *ops;
359
360 list_for_each_entry(ops, &link_ops, list) {
361 if (!strcmp(ops->kind, kind))
362 return ops;
363 }
364 return NULL;
365}
366
Patrick McHardy38f7b872007-06-13 12:03:51 -0700367/**
368 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
369 * @ops: struct rtnl_link_ops * to register
370 *
371 * The caller must hold the rtnl_mutex. This function should be used
372 * by drivers that create devices during module initialization. It
373 * must be called before registering the devices.
374 *
375 * Returns 0 on success or a negative error code.
376 */
377int __rtnl_link_register(struct rtnl_link_ops *ops)
378{
Eric Dumazetc63044f2011-12-13 11:38:00 +0000379 if (rtnl_link_ops_get(ops->kind))
380 return -EEXIST;
381
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +0200382 /* The check for setup is here because if ops
383 * does not have that filled up, it is not possible
384 * to use the ops for creating device. So do not
385 * fill up dellink as well. That disables rtnl_dellink.
386 */
387 if (ops->setup && !ops->dellink)
Eric Dumazet23289a32009-10-27 07:06:36 +0000388 ops->dellink = unregister_netdevice_queue;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700389
Patrick McHardy38f7b872007-06-13 12:03:51 -0700390 list_add_tail(&ops->list, &link_ops);
391 return 0;
392}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700393EXPORT_SYMBOL_GPL(__rtnl_link_register);
394
395/**
396 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
397 * @ops: struct rtnl_link_ops * to register
398 *
399 * Returns 0 on success or a negative error code.
400 */
401int rtnl_link_register(struct rtnl_link_ops *ops)
402{
403 int err;
404
Kees Cookccf8dbc2018-05-30 15:20:52 -0700405 /* Sanity-check max sizes to avoid stack buffer overflow. */
406 if (WARN_ON(ops->maxtype > RTNL_MAX_TYPE ||
407 ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE))
408 return -EINVAL;
409
Patrick McHardy38f7b872007-06-13 12:03:51 -0700410 rtnl_lock();
411 err = __rtnl_link_register(ops);
412 rtnl_unlock();
413 return err;
414}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700415EXPORT_SYMBOL_GPL(rtnl_link_register);
416
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700417static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
418{
419 struct net_device *dev;
Eric Dumazet23289a32009-10-27 07:06:36 +0000420 LIST_HEAD(list_kill);
421
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700422 for_each_netdev(net, dev) {
Eric Dumazet23289a32009-10-27 07:06:36 +0000423 if (dev->rtnl_link_ops == ops)
424 ops->dellink(dev, &list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700425 }
Eric Dumazet23289a32009-10-27 07:06:36 +0000426 unregister_netdevice_many(&list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700427}
428
Patrick McHardy38f7b872007-06-13 12:03:51 -0700429/**
430 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
431 * @ops: struct rtnl_link_ops * to unregister
432 *
Kirill Tkhai554873e2018-03-30 19:38:37 +0300433 * The caller must hold the rtnl_mutex and guarantee net_namespace_list
434 * integrity (hold pernet_ops_rwsem for writing to close the race
435 * with setup_net() and cleanup_net()).
Patrick McHardy38f7b872007-06-13 12:03:51 -0700436 */
437void __rtnl_link_unregister(struct rtnl_link_ops *ops)
438{
Eric W. Biederman881d9662007-09-17 11:56:21 -0700439 struct net *net;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700440
Eric W. Biederman881d9662007-09-17 11:56:21 -0700441 for_each_net(net) {
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700442 __rtnl_kill_links(net, ops);
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700443 }
Patrick McHardy38f7b872007-06-13 12:03:51 -0700444 list_del(&ops->list);
445}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700446EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
447
Cong Wang200b9162014-05-12 15:11:20 -0700448/* Return with the rtnl_lock held when there are no network
449 * devices unregistering in any network namespace.
450 */
451static void rtnl_lock_unregistering_all(void)
452{
453 struct net *net;
454 bool unregistering;
Peter Zijlstraff960a72014-10-29 17:04:56 +0100455 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Cong Wang200b9162014-05-12 15:11:20 -0700456
Peter Zijlstraff960a72014-10-29 17:04:56 +0100457 add_wait_queue(&netdev_unregistering_wq, &wait);
Cong Wang200b9162014-05-12 15:11:20 -0700458 for (;;) {
Cong Wang200b9162014-05-12 15:11:20 -0700459 unregistering = false;
460 rtnl_lock();
Kirill Tkhaif0b07bb12018-03-29 19:20:32 +0300461 /* We held write locked pernet_ops_rwsem, and parallel
462 * setup_net() and cleanup_net() are not possible.
463 */
Cong Wang200b9162014-05-12 15:11:20 -0700464 for_each_net(net) {
465 if (net->dev_unreg_count > 0) {
466 unregistering = true;
467 break;
468 }
469 }
470 if (!unregistering)
471 break;
472 __rtnl_unlock();
Peter Zijlstraff960a72014-10-29 17:04:56 +0100473
474 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Cong Wang200b9162014-05-12 15:11:20 -0700475 }
Peter Zijlstraff960a72014-10-29 17:04:56 +0100476 remove_wait_queue(&netdev_unregistering_wq, &wait);
Cong Wang200b9162014-05-12 15:11:20 -0700477}
478
Patrick McHardy38f7b872007-06-13 12:03:51 -0700479/**
480 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
481 * @ops: struct rtnl_link_ops * to unregister
482 */
483void rtnl_link_unregister(struct rtnl_link_ops *ops)
484{
Kirill Tkhai8518e9b2018-03-27 18:02:32 +0300485 /* Close the race with setup_net() and cleanup_net() */
Kirill Tkhai4420bf22018-03-27 18:02:23 +0300486 down_write(&pernet_ops_rwsem);
Cong Wang200b9162014-05-12 15:11:20 -0700487 rtnl_lock_unregistering_all();
Patrick McHardy38f7b872007-06-13 12:03:51 -0700488 __rtnl_link_unregister(ops);
489 rtnl_unlock();
Kirill Tkhai4420bf22018-03-27 18:02:23 +0300490 up_write(&pernet_ops_rwsem);
Patrick McHardy38f7b872007-06-13 12:03:51 -0700491}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700492EXPORT_SYMBOL_GPL(rtnl_link_unregister);
493
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100494static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
495{
496 struct net_device *master_dev;
497 const struct rtnl_link_ops *ops;
Florian Westphal8515ae32017-08-10 16:52:59 +0200498 size_t size = 0;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100499
Florian Westphal8515ae32017-08-10 16:52:59 +0200500 rcu_read_lock();
501
502 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100503 if (!master_dev)
Florian Westphal8515ae32017-08-10 16:52:59 +0200504 goto out;
505
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100506 ops = master_dev->rtnl_link_ops;
Fernando Luis Vazquez Cao6049f252014-02-04 19:35:02 +0900507 if (!ops || !ops->get_slave_size)
Florian Westphal8515ae32017-08-10 16:52:59 +0200508 goto out;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100509 /* IFLA_INFO_SLAVE_DATA + nested data */
Florian Westphal8515ae32017-08-10 16:52:59 +0200510 size = nla_total_size(sizeof(struct nlattr)) +
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100511 ops->get_slave_size(master_dev, dev);
Florian Westphal8515ae32017-08-10 16:52:59 +0200512
513out:
514 rcu_read_unlock();
515 return size;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100516}
517
Patrick McHardy38f7b872007-06-13 12:03:51 -0700518static size_t rtnl_link_get_size(const struct net_device *dev)
519{
520 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
521 size_t size;
522
523 if (!ops)
524 return 0;
525
Thomas Graf369cf772010-11-11 15:47:59 +0000526 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
527 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700528
529 if (ops->get_size)
530 /* IFLA_INFO_DATA + nested data */
Thomas Graf369cf772010-11-11 15:47:59 +0000531 size += nla_total_size(sizeof(struct nlattr)) +
Patrick McHardy38f7b872007-06-13 12:03:51 -0700532 ops->get_size(dev);
533
534 if (ops->get_xstats_size)
Thomas Graf369cf772010-11-11 15:47:59 +0000535 /* IFLA_INFO_XSTATS */
536 size += nla_total_size(ops->get_xstats_size(dev));
Patrick McHardy38f7b872007-06-13 12:03:51 -0700537
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100538 size += rtnl_link_get_slave_info_data_size(dev);
539
Patrick McHardy38f7b872007-06-13 12:03:51 -0700540 return size;
541}
542
Thomas Graff8ff1822010-11-16 04:30:14 +0000543static LIST_HEAD(rtnl_af_ops);
544
545static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
546{
547 const struct rtnl_af_ops *ops;
548
Florian Westphal5fa85a02017-10-16 15:44:36 +0200549 list_for_each_entry_rcu(ops, &rtnl_af_ops, list) {
Thomas Graff8ff1822010-11-16 04:30:14 +0000550 if (ops->family == family)
551 return ops;
552 }
553
554 return NULL;
555}
556
557/**
Thomas Graff8ff1822010-11-16 04:30:14 +0000558 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
559 * @ops: struct rtnl_af_ops * to register
560 *
561 * Returns 0 on success or a negative error code.
562 */
stephen hemminger3678a9d2013-12-30 10:41:32 -0800563void rtnl_af_register(struct rtnl_af_ops *ops)
Thomas Graff8ff1822010-11-16 04:30:14 +0000564{
Thomas Graff8ff1822010-11-16 04:30:14 +0000565 rtnl_lock();
Florian Westphal5fa85a02017-10-16 15:44:36 +0200566 list_add_tail_rcu(&ops->list, &rtnl_af_ops);
Thomas Graff8ff1822010-11-16 04:30:14 +0000567 rtnl_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +0000568}
569EXPORT_SYMBOL_GPL(rtnl_af_register);
570
571/**
Thomas Graff8ff1822010-11-16 04:30:14 +0000572 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
573 * @ops: struct rtnl_af_ops * to unregister
574 */
575void rtnl_af_unregister(struct rtnl_af_ops *ops)
576{
577 rtnl_lock();
Florian Westphal5fa85a02017-10-16 15:44:36 +0200578 list_del_rcu(&ops->list);
Thomas Graff8ff1822010-11-16 04:30:14 +0000579 rtnl_unlock();
Florian Westphal5fa85a02017-10-16 15:44:36 +0200580
581 synchronize_rcu();
Thomas Graff8ff1822010-11-16 04:30:14 +0000582}
583EXPORT_SYMBOL_GPL(rtnl_af_unregister);
584
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700585static size_t rtnl_link_get_af_size(const struct net_device *dev,
586 u32 ext_filter_mask)
Thomas Graff8ff1822010-11-16 04:30:14 +0000587{
588 struct rtnl_af_ops *af_ops;
589 size_t size;
590
591 /* IFLA_AF_SPEC */
592 size = nla_total_size(sizeof(struct nlattr));
593
Florian Westphal5fa85a02017-10-16 15:44:36 +0200594 rcu_read_lock();
595 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Thomas Graff8ff1822010-11-16 04:30:14 +0000596 if (af_ops->get_link_af_size) {
597 /* AF_* + nested data */
598 size += nla_total_size(sizeof(struct nlattr)) +
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700599 af_ops->get_link_af_size(dev, ext_filter_mask);
Thomas Graff8ff1822010-11-16 04:30:14 +0000600 }
601 }
Florian Westphal5fa85a02017-10-16 15:44:36 +0200602 rcu_read_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +0000603
604 return size;
605}
606
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100607static bool rtnl_have_link_slave_info(const struct net_device *dev)
608{
609 struct net_device *master_dev;
Florian Westphal4c82a952017-09-26 13:58:43 +0200610 bool ret = false;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100611
Florian Westphal4c82a952017-09-26 13:58:43 +0200612 rcu_read_lock();
613
614 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
Jiri Pirko813f0202014-01-23 19:19:21 +0100615 if (master_dev && master_dev->rtnl_link_ops)
Florian Westphal4c82a952017-09-26 13:58:43 +0200616 ret = true;
617 rcu_read_unlock();
618 return ret;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100619}
620
621static int rtnl_link_slave_info_fill(struct sk_buff *skb,
622 const struct net_device *dev)
623{
624 struct net_device *master_dev;
625 const struct rtnl_link_ops *ops;
626 struct nlattr *slave_data;
627 int err;
628
629 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
630 if (!master_dev)
631 return 0;
632 ops = master_dev->rtnl_link_ops;
633 if (!ops)
634 return 0;
635 if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
636 return -EMSGSIZE;
637 if (ops->fill_slave_info) {
638 slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA);
639 if (!slave_data)
640 return -EMSGSIZE;
641 err = ops->fill_slave_info(skb, master_dev, dev);
642 if (err < 0)
643 goto err_cancel_slave_data;
644 nla_nest_end(skb, slave_data);
645 }
646 return 0;
647
648err_cancel_slave_data:
649 nla_nest_cancel(skb, slave_data);
650 return err;
651}
652
653static int rtnl_link_info_fill(struct sk_buff *skb,
654 const struct net_device *dev)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700655{
656 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100657 struct nlattr *data;
658 int err;
659
660 if (!ops)
661 return 0;
662 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
663 return -EMSGSIZE;
664 if (ops->fill_xstats) {
665 err = ops->fill_xstats(skb, dev);
666 if (err < 0)
667 return err;
668 }
669 if (ops->fill_info) {
670 data = nla_nest_start(skb, IFLA_INFO_DATA);
671 if (data == NULL)
672 return -EMSGSIZE;
673 err = ops->fill_info(skb, dev);
674 if (err < 0)
675 goto err_cancel_data;
676 nla_nest_end(skb, data);
677 }
678 return 0;
679
680err_cancel_data:
681 nla_nest_cancel(skb, data);
682 return err;
683}
684
685static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
686{
687 struct nlattr *linkinfo;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700688 int err = -EMSGSIZE;
689
690 linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
691 if (linkinfo == NULL)
692 goto out;
693
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100694 err = rtnl_link_info_fill(skb, dev);
695 if (err < 0)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700696 goto err_cancel_link;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100697
698 err = rtnl_link_slave_info_fill(skb, dev);
699 if (err < 0)
700 goto err_cancel_link;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700701
702 nla_nest_end(skb, linkinfo);
703 return 0;
704
Patrick McHardy38f7b872007-06-13 12:03:51 -0700705err_cancel_link:
706 nla_nest_cancel(skb, linkinfo);
707out:
708 return err;
709}
710
Eric Dumazet95c96172012-04-15 05:58:06 +0000711int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800713 struct sock *rtnl = net->rtnl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 int err = 0;
715
Patrick McHardyac6d4392005-08-14 19:29:52 -0700716 NETLINK_CB(skb).dst_group = group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 if (echo)
Reshetova, Elena63354792017-06-30 13:07:58 +0300718 refcount_inc(&skb->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
720 if (echo)
721 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
722 return err;
723}
724
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800725int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
Thomas Graf2942e902006-08-15 00:30:25 -0700726{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800727 struct sock *rtnl = net->rtnl;
728
Thomas Graf2942e902006-08-15 00:30:25 -0700729 return nlmsg_unicast(rtnl, skb, pid);
730}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800731EXPORT_SYMBOL(rtnl_unicast);
Thomas Graf2942e902006-08-15 00:30:25 -0700732
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800733void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
734 struct nlmsghdr *nlh, gfp_t flags)
Thomas Graf97676b62006-08-15 00:31:41 -0700735{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800736 struct sock *rtnl = net->rtnl;
Thomas Graf97676b62006-08-15 00:31:41 -0700737 int report = 0;
738
739 if (nlh)
740 report = nlmsg_report(nlh);
741
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800742 nlmsg_notify(rtnl, skb, pid, group, report, flags);
Thomas Graf97676b62006-08-15 00:31:41 -0700743}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800744EXPORT_SYMBOL(rtnl_notify);
Thomas Graf97676b62006-08-15 00:31:41 -0700745
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800746void rtnl_set_sk_err(struct net *net, u32 group, int error)
Thomas Graf97676b62006-08-15 00:31:41 -0700747{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800748 struct sock *rtnl = net->rtnl;
749
Thomas Graf97676b62006-08-15 00:31:41 -0700750 netlink_set_err(rtnl, 0, group, error);
751}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800752EXPORT_SYMBOL(rtnl_set_sk_err);
Thomas Graf97676b62006-08-15 00:31:41 -0700753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
755{
Thomas Graf2d7202b2006-08-22 00:01:27 -0700756 struct nlattr *mx;
757 int i, valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Thomas Graf2d7202b2006-08-22 00:01:27 -0700759 mx = nla_nest_start(skb, RTA_METRICS);
760 if (mx == NULL)
761 return -ENOBUFS;
762
763 for (i = 0; i < RTAX_MAX; i++) {
764 if (metrics[i]) {
Daniel Borkmannea697632015-01-05 23:57:47 +0100765 if (i == RTAX_CC_ALGO - 1) {
766 char tmp[TCP_CA_NAME_MAX], *name;
767
768 name = tcp_ca_get_name_by_key(metrics[i], tmp);
769 if (!name)
770 continue;
771 if (nla_put_string(skb, i + 1, name))
772 goto nla_put_failure;
Daniel Borkmannc3a8d942015-08-31 15:58:47 +0200773 } else if (i == RTAX_FEATURES - 1) {
774 u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
775
Phil Sutterf8edcd12016-08-23 13:14:31 +0200776 if (!user_features)
777 continue;
Daniel Borkmannc3a8d942015-08-31 15:58:47 +0200778 BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
779 if (nla_put_u32(skb, i + 1, user_features))
780 goto nla_put_failure;
Daniel Borkmannea697632015-01-05 23:57:47 +0100781 } else {
782 if (nla_put_u32(skb, i + 1, metrics[i]))
783 goto nla_put_failure;
784 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700785 valid++;
Thomas Graf2d7202b2006-08-22 00:01:27 -0700786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
David S. Millera57d27f2006-08-22 22:20:14 -0700789 if (!valid) {
790 nla_nest_cancel(skb, mx);
791 return 0;
792 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700793
794 return nla_nest_end(skb, mx);
795
796nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700797 nla_nest_cancel(skb, mx);
798 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800800EXPORT_SYMBOL(rtnetlink_put_metrics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Thomas Grafe3703b32006-11-27 09:27:07 -0800802int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
David S. Miller87a50692012-07-10 05:06:14 -0700803 long expires, u32 error)
Thomas Grafe3703b32006-11-27 09:27:07 -0800804{
805 struct rta_cacheinfo ci = {
Thomas Grafe3703b32006-11-27 09:27:07 -0800806 .rta_error = error,
807 .rta_id = id,
Thomas Grafe3703b32006-11-27 09:27:07 -0800808 };
809
David Ahern39407462018-04-17 17:33:08 -0700810 if (dst) {
811 ci.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse);
812 ci.rta_used = dst->__use;
813 ci.rta_clntref = atomic_read(&dst->__refcnt);
814 }
Li Wei82539472012-07-29 16:01:30 +0000815 if (expires) {
816 unsigned long clock;
Thomas Grafe3703b32006-11-27 09:27:07 -0800817
Li Wei82539472012-07-29 16:01:30 +0000818 clock = jiffies_to_clock_t(abs(expires));
819 clock = min_t(unsigned long, clock, INT_MAX);
820 ci.rta_expires = (expires > 0) ? clock : -clock;
821 }
Thomas Grafe3703b32006-11-27 09:27:07 -0800822 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
823}
Thomas Grafe3703b32006-11-27 09:27:07 -0800824EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
David S. Miller93b2d4a2008-02-17 18:35:07 -0800826static void set_operstate(struct net_device *dev, unsigned char transition)
Stefan Rompfb00055a2006-03-20 17:09:11 -0800827{
828 unsigned char operstate = dev->operstate;
829
Eric Dumazete0d087a2009-11-07 01:26:17 -0800830 switch (transition) {
Stefan Rompfb00055a2006-03-20 17:09:11 -0800831 case IF_OPER_UP:
832 if ((operstate == IF_OPER_DORMANT ||
833 operstate == IF_OPER_UNKNOWN) &&
834 !netif_dormant(dev))
835 operstate = IF_OPER_UP;
836 break;
837
838 case IF_OPER_DORMANT:
839 if (operstate == IF_OPER_UP ||
840 operstate == IF_OPER_UNKNOWN)
841 operstate = IF_OPER_DORMANT;
842 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700843 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800844
845 if (dev->operstate != operstate) {
846 write_lock_bh(&dev_base_lock);
847 dev->operstate = operstate;
848 write_unlock_bh(&dev_base_lock);
David S. Miller93b2d4a2008-02-17 18:35:07 -0800849 netdev_state_change(dev);
850 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800851}
852
Jiri Bencb1beb682012-07-27 02:58:22 +0000853static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
854{
855 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
856 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
857}
858
Patrick McHardy3729d502010-02-26 06:34:54 +0000859static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
860 const struct ifinfomsg *ifm)
861{
862 unsigned int flags = ifm->ifi_flags;
863
864 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
865 if (ifm->ifi_change)
866 flags = (flags & ifm->ifi_change) |
Jiri Bencb1beb682012-07-27 02:58:22 +0000867 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
Patrick McHardy3729d502010-02-26 06:34:54 +0000868
869 return flags;
870}
871
Thomas Grafb60c5112006-08-04 23:05:34 -0700872static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000873 const struct rtnl_link_stats64 *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Thomas Grafb60c5112006-08-04 23:05:34 -0700875 a->rx_packets = b->rx_packets;
876 a->tx_packets = b->tx_packets;
877 a->rx_bytes = b->rx_bytes;
878 a->tx_bytes = b->tx_bytes;
879 a->rx_errors = b->rx_errors;
880 a->tx_errors = b->tx_errors;
881 a->rx_dropped = b->rx_dropped;
882 a->tx_dropped = b->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Thomas Grafb60c5112006-08-04 23:05:34 -0700884 a->multicast = b->multicast;
885 a->collisions = b->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Thomas Grafb60c5112006-08-04 23:05:34 -0700887 a->rx_length_errors = b->rx_length_errors;
888 a->rx_over_errors = b->rx_over_errors;
889 a->rx_crc_errors = b->rx_crc_errors;
890 a->rx_frame_errors = b->rx_frame_errors;
891 a->rx_fifo_errors = b->rx_fifo_errors;
892 a->rx_missed_errors = b->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Thomas Grafb60c5112006-08-04 23:05:34 -0700894 a->tx_aborted_errors = b->tx_aborted_errors;
895 a->tx_carrier_errors = b->tx_carrier_errors;
896 a->tx_fifo_errors = b->tx_fifo_errors;
897 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
898 a->tx_window_errors = b->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Thomas Grafb60c5112006-08-04 23:05:34 -0700900 a->rx_compressed = b->rx_compressed;
901 a->tx_compressed = b->tx_compressed;
Jarod Wilson6e7333d2016-02-01 18:51:05 -0500902
903 a->rx_nohandler = b->rx_nohandler;
Jan Engelhardt10708f32010-03-11 09:57:29 +0000904}
905
Chris Wrightc02db8c2010-05-16 01:05:45 -0700906/* All VF info */
Greg Rose115c9b82012-02-21 16:54:48 -0500907static inline int rtnl_vfinfo_size(const struct net_device *dev,
908 u32 ext_filter_mask)
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000909{
Phil Sutter9af15c32017-01-18 14:04:39 +0100910 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
Chris Wrightc02db8c2010-05-16 01:05:45 -0700911 int num_vfs = dev_num_vf(dev->dev.parent);
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100912 size_t size = nla_total_size(0);
Scott Feldman045de012010-05-28 03:42:43 -0700913 size += num_vfs *
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100914 (nla_total_size(0) +
915 nla_total_size(sizeof(struct ifla_vf_mac)) +
916 nla_total_size(sizeof(struct ifla_vf_vlan)) +
917 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
Moshe Shemesh79aab092016-09-22 12:11:15 +0300918 nla_total_size(MAX_VLAN_LIST_LEN *
919 sizeof(struct ifla_vf_vlan_info)) +
Sucheta Chakrabortyed616682014-05-22 09:59:05 -0400920 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100921 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
Jiri Benc945a3672014-08-08 16:44:32 +0200922 nla_total_size(sizeof(struct ifla_vf_rate)) +
Vlad Zolotarov01a3d792015-03-30 21:35:23 +0300923 nla_total_size(sizeof(struct ifla_vf_link_state)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300924 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100925 nla_total_size(0) + /* nest IFLA_VF_STATS */
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300926 /* IFLA_VF_STATS_RX_PACKETS */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200927 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300928 /* IFLA_VF_STATS_TX_PACKETS */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200929 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300930 /* IFLA_VF_STATS_RX_BYTES */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200931 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300932 /* IFLA_VF_STATS_TX_BYTES */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200933 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300934 /* IFLA_VF_STATS_BROADCAST */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200935 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300936 /* IFLA_VF_STATS_MULTICAST */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200937 nla_total_size_64bit(sizeof(__u64)) +
Eugenia Emantayevc5a9f6f2017-07-17 13:47:07 +0300938 /* IFLA_VF_STATS_RX_DROPPED */
939 nla_total_size_64bit(sizeof(__u64)) +
940 /* IFLA_VF_STATS_TX_DROPPED */
941 nla_total_size_64bit(sizeof(__u64)) +
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +0000942 nla_total_size(sizeof(struct ifla_vf_trust)));
Chris Wrightc02db8c2010-05-16 01:05:45 -0700943 return size;
944 } else
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000945 return 0;
946}
947
David Gibsonc53864f2014-04-24 10:22:36 +1000948static size_t rtnl_port_size(const struct net_device *dev,
949 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -0700950{
951 size_t port_size = nla_total_size(4) /* PORT_VF */
952 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
Scott Feldman57b61082010-05-17 22:49:55 -0700953 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
954 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
955 + nla_total_size(1) /* PROT_VDP_REQUEST */
956 + nla_total_size(2); /* PORT_VDP_RESPONSE */
957 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
958 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
959 + port_size;
960 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
961 + port_size;
962
David Gibsonc53864f2014-04-24 10:22:36 +1000963 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
964 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -0700965 return 0;
966 if (dev_num_vf(dev->dev.parent))
967 return port_self_size + vf_ports_size +
968 vf_port_size * dev_num_vf(dev->dev.parent);
969 else
970 return port_self_size;
971}
972
David S. Millerb5cdae32017-04-18 15:36:58 -0400973static size_t rtnl_xdp_size(void)
Brenden Blancod1fdd912016-07-19 12:16:49 -0700974{
Sabrina Dubrocab3cfaa32016-11-15 11:16:35 +0100975 size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */
Martin KaFai Lau58038692017-06-15 17:29:09 -0700976 nla_total_size(1) + /* XDP_ATTACHED */
Jakub Kicinskia25717d2018-07-11 20:36:41 -0700977 nla_total_size(4) + /* XDP_PROG_ID (or 1st mode) */
Jakub Kicinski4f91da22018-07-11 20:36:38 -0700978 nla_total_size(4); /* XDP_<mode>_PROG_ID */
Brenden Blancod1fdd912016-07-19 12:16:49 -0700979
David S. Millerb5cdae32017-04-18 15:36:58 -0400980 return xdp_size;
Brenden Blancod1fdd912016-07-19 12:16:49 -0700981}
982
Greg Rose115c9b82012-02-21 16:54:48 -0500983static noinline size_t if_nlmsg_size(const struct net_device *dev,
984 u32 ext_filter_mask)
Thomas Graf339bf982006-11-10 14:10:15 -0800985{
986 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
987 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700988 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
Thomas Graf339bf982006-11-10 14:10:15 -0800989 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
Nicolas Dichtel270cb4d2016-04-26 10:06:16 +0200990 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
Thomas Graf339bf982006-11-10 14:10:15 -0800991 + nla_total_size(sizeof(struct rtnl_link_stats))
David S. Miller35c58452016-04-19 19:49:29 -0400992 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
Thomas Graf339bf982006-11-10 14:10:15 -0800993 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
994 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
995 + nla_total_size(4) /* IFLA_TXQLEN */
996 + nla_total_size(4) /* IFLA_WEIGHT */
997 + nla_total_size(4) /* IFLA_MTU */
998 + nla_total_size(4) /* IFLA_LINK */
999 + nla_total_size(4) /* IFLA_MASTER */
Jiri Pirko9a572472012-12-27 23:49:39 +00001000 + nla_total_size(1) /* IFLA_CARRIER */
Ben Greearedbc0bb2012-03-29 12:51:30 +00001001 + nla_total_size(4) /* IFLA_PROMISCUITY */
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001002 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
1003 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
Tobias Klauser69197562016-11-30 14:30:37 +01001004 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
1005 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
Thomas Graf339bf982006-11-10 14:10:15 -08001006 + nla_total_size(1) /* IFLA_OPERSTATE */
Patrick McHardy38f7b872007-06-13 12:03:51 -07001007 + nla_total_size(1) /* IFLA_LINKMODE */
david decotigny2d3b4792014-03-29 09:48:35 -07001008 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001009 + nla_total_size(4) /* IFLA_LINK_NETNSID */
Serhey Popovychdb833d42017-06-20 14:35:23 +03001010 + nla_total_size(4) /* IFLA_GROUP */
Greg Rose115c9b82012-02-21 16:54:48 -05001011 + nla_total_size(ext_filter_mask
1012 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
1013 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
David Gibsonc53864f2014-04-24 10:22:36 +10001014 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
Thomas Graff8ff1822010-11-16 04:30:14 +00001015 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
Arad, Ronenb1974ed2015-10-19 09:23:28 -07001016 + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
Jiri Pirko82f28412014-11-28 14:34:18 +01001017 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001018 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
Nicolas Dichtelc57c7a92016-03-31 18:10:31 +02001019 + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
David S. Millerb5cdae32017-04-18 15:36:58 -04001020 + rtnl_xdp_size() /* IFLA_XDP */
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001021 + nla_total_size(4) /* IFLA_EVENT */
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02001022 + nla_total_size(4) /* IFLA_NEW_NETNSID */
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01001023 + nla_total_size(4) /* IFLA_NEW_IFINDEX */
Colin Ian King03ac7382017-11-06 15:04:54 +00001024 + nla_total_size(1) /* IFLA_PROTO_DOWN */
Christian Brauner7e4a8d52018-09-04 21:53:53 +02001025 + nla_total_size(4) /* IFLA_TARGET_NETNSID */
David Decotignyb2d3bcf2018-01-18 09:59:13 -08001026 + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */
1027 + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */
Stephen Hemminger3e7a50c2018-07-27 13:43:22 -07001028 + nla_total_size(4) /* IFLA_MIN_MTU */
1029 + nla_total_size(4) /* IFLA_MAX_MTU */
Jiri Benc79e1ad12017-11-02 17:04:38 -02001030 + 0;
Thomas Graf339bf982006-11-10 14:10:15 -08001031}
1032
Scott Feldman57b61082010-05-17 22:49:55 -07001033static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
1034{
1035 struct nlattr *vf_ports;
1036 struct nlattr *vf_port;
1037 int vf;
1038 int err;
1039
1040 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
1041 if (!vf_ports)
1042 return -EMSGSIZE;
1043
1044 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
1045 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
Scott Feldman8ca94182010-05-28 03:42:18 -07001046 if (!vf_port)
1047 goto nla_put_failure;
David S. Millera6574342012-04-01 20:12:00 -04001048 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
1049 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001050 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
Scott Feldman8ca94182010-05-28 03:42:18 -07001051 if (err == -EMSGSIZE)
1052 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001053 if (err) {
Scott Feldman57b61082010-05-17 22:49:55 -07001054 nla_nest_cancel(skb, vf_port);
1055 continue;
1056 }
1057 nla_nest_end(skb, vf_port);
1058 }
1059
1060 nla_nest_end(skb, vf_ports);
1061
1062 return 0;
Scott Feldman8ca94182010-05-28 03:42:18 -07001063
1064nla_put_failure:
1065 nla_nest_cancel(skb, vf_ports);
1066 return -EMSGSIZE;
Scott Feldman57b61082010-05-17 22:49:55 -07001067}
1068
1069static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
1070{
1071 struct nlattr *port_self;
1072 int err;
1073
1074 port_self = nla_nest_start(skb, IFLA_PORT_SELF);
1075 if (!port_self)
1076 return -EMSGSIZE;
1077
1078 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
1079 if (err) {
1080 nla_nest_cancel(skb, port_self);
Scott Feldman8ca94182010-05-28 03:42:18 -07001081 return (err == -EMSGSIZE) ? err : 0;
Scott Feldman57b61082010-05-17 22:49:55 -07001082 }
1083
1084 nla_nest_end(skb, port_self);
1085
1086 return 0;
1087}
1088
David Gibsonc53864f2014-04-24 10:22:36 +10001089static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1090 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -07001091{
1092 int err;
1093
David Gibsonc53864f2014-04-24 10:22:36 +10001094 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1095 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -07001096 return 0;
1097
1098 err = rtnl_port_self_fill(skb, dev);
1099 if (err)
1100 return err;
1101
1102 if (dev_num_vf(dev->dev.parent)) {
1103 err = rtnl_vf_ports_fill(skb, dev);
1104 if (err)
1105 return err;
1106 }
1107
1108 return 0;
1109}
1110
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001111static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
1112{
1113 int err;
Jiri Pirko02637fc2014-11-28 14:34:16 +01001114 struct netdev_phys_item_id ppid;
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001115
1116 err = dev_get_phys_port_id(dev, &ppid);
1117 if (err) {
1118 if (err == -EOPNOTSUPP)
1119 return 0;
1120 return err;
1121 }
1122
1123 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1124 return -EMSGSIZE;
1125
1126 return 0;
1127}
1128
David Aherndb24a902015-03-17 20:23:15 -06001129static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1130{
1131 char name[IFNAMSIZ];
1132 int err;
1133
1134 err = dev_get_phys_port_name(dev, name, sizeof(name));
1135 if (err) {
1136 if (err == -EOPNOTSUPP)
1137 return 0;
1138 return err;
1139 }
1140
Michal Schmidt77ef0332017-05-04 16:48:58 +02001141 if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
David Aherndb24a902015-03-17 20:23:15 -06001142 return -EMSGSIZE;
1143
1144 return 0;
1145}
1146
Jiri Pirko82f28412014-11-28 14:34:18 +01001147static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1148{
1149 int err;
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001150 struct switchdev_attr attr = {
Ido Schimmel6ff64f62015-12-15 16:03:35 +01001151 .orig_dev = dev,
Jiri Pirko1f868392015-10-01 11:03:42 +02001152 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001153 .flags = SWITCHDEV_F_NO_RECURSE,
1154 };
Jiri Pirko82f28412014-11-28 14:34:18 +01001155
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001156 err = switchdev_port_attr_get(dev, &attr);
Jiri Pirko82f28412014-11-28 14:34:18 +01001157 if (err) {
1158 if (err == -EOPNOTSUPP)
1159 return 0;
1160 return err;
1161 }
1162
Scott Feldman42275bd2015-05-13 11:16:50 -07001163 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len,
1164 attr.u.ppid.id))
Jiri Pirko82f28412014-11-28 14:34:18 +01001165 return -EMSGSIZE;
1166
1167 return 0;
1168}
1169
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001170static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1171 struct net_device *dev)
1172{
Roopa Prabhu550bce52016-04-15 20:36:25 -07001173 struct rtnl_link_stats64 *sp;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001174 struct nlattr *attr;
1175
Nicolas Dichtel58414d32016-04-21 18:58:25 +02001176 attr = nla_reserve_64bit(skb, IFLA_STATS64,
1177 sizeof(struct rtnl_link_stats64), IFLA_PAD);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001178 if (!attr)
1179 return -EMSGSIZE;
1180
Roopa Prabhu550bce52016-04-15 20:36:25 -07001181 sp = nla_data(attr);
1182 dev_get_stats(dev, sp);
1183
1184 attr = nla_reserve(skb, IFLA_STATS,
1185 sizeof(struct rtnl_link_stats));
1186 if (!attr)
1187 return -EMSGSIZE;
1188
1189 copy_rtnl_link_stats(nla_data(attr), sp);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001190
1191 return 0;
1192}
1193
1194static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1195 struct net_device *dev,
1196 int vfs_num,
1197 struct nlattr *vfinfo)
1198{
1199 struct ifla_vf_rss_query_en vf_rss_query_en;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001200 struct nlattr *vf, *vfstats, *vfvlanlist;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001201 struct ifla_vf_link_state vf_linkstate;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001202 struct ifla_vf_vlan_info vf_vlan_info;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001203 struct ifla_vf_spoofchk vf_spoofchk;
1204 struct ifla_vf_tx_rate vf_tx_rate;
1205 struct ifla_vf_stats vf_stats;
1206 struct ifla_vf_trust vf_trust;
1207 struct ifla_vf_vlan vf_vlan;
1208 struct ifla_vf_rate vf_rate;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001209 struct ifla_vf_mac vf_mac;
1210 struct ifla_vf_info ivi;
1211
Mintz, Yuval0eed9cf2017-06-07 21:00:33 +03001212 memset(&ivi, 0, sizeof(ivi));
1213
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001214 /* Not all SR-IOV capable drivers support the
1215 * spoofcheck and "RSS query enable" query. Preset to
1216 * -1 so the user space tool can detect that the driver
1217 * didn't report anything.
1218 */
1219 ivi.spoofchk = -1;
1220 ivi.rss_query_en = -1;
1221 ivi.trusted = -1;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001222 /* The default value for VF link state is "auto"
1223 * IFLA_VF_LINK_STATE_AUTO which equals zero
1224 */
1225 ivi.linkstate = 0;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001226 /* VLAN Protocol by default is 802.1Q */
1227 ivi.vlan_proto = htons(ETH_P_8021Q);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001228 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1229 return 0;
1230
Dan Carpenter775f4f02016-10-13 11:45:28 +03001231 memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
1232
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001233 vf_mac.vf =
1234 vf_vlan.vf =
Moshe Shemesh79aab092016-09-22 12:11:15 +03001235 vf_vlan_info.vf =
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001236 vf_rate.vf =
1237 vf_tx_rate.vf =
1238 vf_spoofchk.vf =
1239 vf_linkstate.vf =
1240 vf_rss_query_en.vf =
1241 vf_trust.vf = ivi.vf;
1242
1243 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1244 vf_vlan.vlan = ivi.vlan;
1245 vf_vlan.qos = ivi.qos;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001246 vf_vlan_info.vlan = ivi.vlan;
1247 vf_vlan_info.qos = ivi.qos;
1248 vf_vlan_info.vlan_proto = ivi.vlan_proto;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001249 vf_tx_rate.rate = ivi.max_tx_rate;
1250 vf_rate.min_tx_rate = ivi.min_tx_rate;
1251 vf_rate.max_tx_rate = ivi.max_tx_rate;
1252 vf_spoofchk.setting = ivi.spoofchk;
1253 vf_linkstate.link_state = ivi.linkstate;
1254 vf_rss_query_en.setting = ivi.rss_query_en;
1255 vf_trust.setting = ivi.trusted;
1256 vf = nla_nest_start(skb, IFLA_VF_INFO);
Moshe Shemesh79aab092016-09-22 12:11:15 +03001257 if (!vf)
1258 goto nla_put_vfinfo_failure;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001259 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1260 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1261 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1262 &vf_rate) ||
1263 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1264 &vf_tx_rate) ||
1265 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1266 &vf_spoofchk) ||
1267 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1268 &vf_linkstate) ||
1269 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1270 sizeof(vf_rss_query_en),
1271 &vf_rss_query_en) ||
1272 nla_put(skb, IFLA_VF_TRUST,
1273 sizeof(vf_trust), &vf_trust))
Moshe Shemesh79aab092016-09-22 12:11:15 +03001274 goto nla_put_vf_failure;
1275 vfvlanlist = nla_nest_start(skb, IFLA_VF_VLAN_LIST);
1276 if (!vfvlanlist)
1277 goto nla_put_vf_failure;
1278 if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
1279 &vf_vlan_info)) {
1280 nla_nest_cancel(skb, vfvlanlist);
1281 goto nla_put_vf_failure;
1282 }
1283 nla_nest_end(skb, vfvlanlist);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001284 memset(&vf_stats, 0, sizeof(vf_stats));
1285 if (dev->netdev_ops->ndo_get_vf_stats)
1286 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1287 &vf_stats);
1288 vfstats = nla_nest_start(skb, IFLA_VF_STATS);
Moshe Shemesh79aab092016-09-22 12:11:15 +03001289 if (!vfstats)
1290 goto nla_put_vf_failure;
Nicolas Dichtel343a6d82016-04-25 10:25:14 +02001291 if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1292 vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1293 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1294 vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1295 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1296 vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1297 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1298 vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1299 nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1300 vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1301 nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
Eugenia Emantayevc5a9f6f2017-07-17 13:47:07 +03001302 vf_stats.multicast, IFLA_VF_STATS_PAD) ||
1303 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED,
1304 vf_stats.rx_dropped, IFLA_VF_STATS_PAD) ||
1305 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED,
1306 vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) {
Moshe Shemesh79aab092016-09-22 12:11:15 +03001307 nla_nest_cancel(skb, vfstats);
1308 goto nla_put_vf_failure;
1309 }
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001310 nla_nest_end(skb, vfstats);
1311 nla_nest_end(skb, vf);
1312 return 0;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001313
1314nla_put_vf_failure:
1315 nla_nest_cancel(skb, vf);
1316nla_put_vfinfo_failure:
1317 nla_nest_cancel(skb, vfinfo);
1318 return -EMSGSIZE;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001319}
1320
Florian Westphal250fc3d2017-09-26 13:58:41 +02001321static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb,
1322 struct net_device *dev,
1323 u32 ext_filter_mask)
1324{
1325 struct nlattr *vfinfo;
1326 int i, num_vfs;
1327
1328 if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0))
1329 return 0;
1330
1331 num_vfs = dev_num_vf(dev->dev.parent);
1332 if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs))
1333 return -EMSGSIZE;
1334
1335 if (!dev->netdev_ops->ndo_get_vf_config)
1336 return 0;
1337
1338 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1339 if (!vfinfo)
1340 return -EMSGSIZE;
1341
1342 for (i = 0; i < num_vfs; i++) {
1343 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1344 return -EMSGSIZE;
1345 }
1346
1347 nla_nest_end(skb, vfinfo);
1348 return 0;
1349}
1350
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001351static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1352{
Kangjie Lu5f8e4472016-05-03 16:46:24 -04001353 struct rtnl_link_ifmap map;
1354
1355 memset(&map, 0, sizeof(map));
1356 map.mem_start = dev->mem_start;
1357 map.mem_end = dev->mem_end;
1358 map.base_addr = dev->base_addr;
1359 map.irq = dev->irq;
1360 map.dma = dev->dma;
1361 map.port = dev->if_port;
1362
Nicolas Dichtel270cb4d2016-04-26 10:06:16 +02001363 if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001364 return -EMSGSIZE;
1365
1366 return 0;
1367}
1368
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001369static u32 rtnl_xdp_prog_skb(struct net_device *dev)
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001370{
Martin KaFai Lau58038692017-06-15 17:29:09 -07001371 const struct bpf_prog *generic_xdp_prog;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001372
1373 ASSERT_RTNL();
1374
Martin KaFai Lau58038692017-06-15 17:29:09 -07001375 generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001376 if (!generic_xdp_prog)
1377 return 0;
1378 return generic_xdp_prog->aux->id;
1379}
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001380
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001381static u32 rtnl_xdp_prog_drv(struct net_device *dev)
1382{
1383 return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf, XDP_QUERY_PROG);
1384}
Jakub Kicinski118b4aa2017-12-01 15:08:55 -08001385
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001386static u32 rtnl_xdp_prog_hw(struct net_device *dev)
1387{
1388 return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf,
1389 XDP_QUERY_PROG_HW);
1390}
1391
1392static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev,
1393 u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr,
1394 u32 (*get_prog_id)(struct net_device *dev))
1395{
1396 u32 curr_id;
1397 int err;
1398
1399 curr_id = get_prog_id(dev);
1400 if (!curr_id)
1401 return 0;
1402
1403 *prog_id = curr_id;
1404 err = nla_put_u32(skb, attr, curr_id);
1405 if (err)
1406 return err;
1407
1408 if (*mode != XDP_ATTACHED_NONE)
1409 *mode = XDP_ATTACHED_MULTI;
1410 else
1411 *mode = tgt_mode;
1412
1413 return 0;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001414}
1415
Brenden Blancod1fdd912016-07-19 12:16:49 -07001416static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1417{
Brenden Blancod1fdd912016-07-19 12:16:49 -07001418 struct nlattr *xdp;
Martin KaFai Lau58038692017-06-15 17:29:09 -07001419 u32 prog_id;
Brenden Blancod1fdd912016-07-19 12:16:49 -07001420 int err;
Jakub Kicinski4f91da22018-07-11 20:36:38 -07001421 u8 mode;
Brenden Blancod1fdd912016-07-19 12:16:49 -07001422
Brenden Blancod1fdd912016-07-19 12:16:49 -07001423 xdp = nla_nest_start(skb, IFLA_XDP);
1424 if (!xdp)
1425 return -EMSGSIZE;
David S. Millerb5cdae32017-04-18 15:36:58 -04001426
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001427 prog_id = 0;
1428 mode = XDP_ATTACHED_NONE;
Jakub Kicinski202aabe2018-07-16 19:08:50 -07001429 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB,
1430 IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb);
1431 if (err)
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001432 goto err_cancel;
Jakub Kicinski202aabe2018-07-16 19:08:50 -07001433 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV,
1434 IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv);
1435 if (err)
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001436 goto err_cancel;
Jakub Kicinski202aabe2018-07-16 19:08:50 -07001437 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW,
1438 IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw);
1439 if (err)
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001440 goto err_cancel;
1441
Jakub Kicinski4f91da22018-07-11 20:36:38 -07001442 err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode);
Brenden Blancod1fdd912016-07-19 12:16:49 -07001443 if (err)
1444 goto err_cancel;
1445
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001446 if (prog_id && mode != XDP_ATTACHED_MULTI) {
Martin KaFai Lau58038692017-06-15 17:29:09 -07001447 err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
1448 if (err)
1449 goto err_cancel;
1450 }
1451
Brenden Blancod1fdd912016-07-19 12:16:49 -07001452 nla_nest_end(skb, xdp);
1453 return 0;
1454
1455err_cancel:
1456 nla_nest_cancel(skb, xdp);
1457 return err;
1458}
1459
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001460static u32 rtnl_get_event(unsigned long event)
1461{
1462 u32 rtnl_event_type = IFLA_EVENT_NONE;
1463
1464 switch (event) {
1465 case NETDEV_REBOOT:
1466 rtnl_event_type = IFLA_EVENT_REBOOT;
1467 break;
1468 case NETDEV_FEAT_CHANGE:
1469 rtnl_event_type = IFLA_EVENT_FEATURES;
1470 break;
1471 case NETDEV_BONDING_FAILOVER:
1472 rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
1473 break;
1474 case NETDEV_NOTIFY_PEERS:
1475 rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
1476 break;
1477 case NETDEV_RESEND_IGMP:
1478 rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
1479 break;
1480 case NETDEV_CHANGEINFODATA:
1481 rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
1482 break;
1483 default:
1484 break;
1485 }
1486
1487 return rtnl_event_type;
1488}
1489
Florian Westphal79110a02017-09-26 13:58:40 +02001490static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
1491{
1492 const struct net_device *upper_dev;
1493 int ret = 0;
1494
1495 rcu_read_lock();
1496
1497 upper_dev = netdev_master_upper_dev_get_rcu(dev);
1498 if (upper_dev)
1499 ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);
1500
1501 rcu_read_unlock();
1502 return ret;
1503}
1504
1505static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev)
1506{
1507 int ifindex = dev_get_iflink(dev);
1508
1509 if (dev->ifindex == ifindex)
1510 return 0;
1511
1512 return nla_put_u32(skb, IFLA_LINK, ifindex);
1513}
1514
Florian Westphal6c557002017-10-02 23:50:05 +02001515static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
1516 struct net_device *dev)
1517{
1518 char buf[IFALIASZ];
1519 int ret;
1520
1521 ret = dev_get_alias(dev, buf, sizeof(buf));
1522 return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
1523}
1524
Florian Westphalb1e66b92017-09-26 13:58:42 +02001525static int rtnl_fill_link_netnsid(struct sk_buff *skb,
Jiri Benc79e1ad12017-11-02 17:04:38 -02001526 const struct net_device *dev,
1527 struct net *src_net)
Florian Westphalb1e66b92017-09-26 13:58:42 +02001528{
1529 if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
1530 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1531
1532 if (!net_eq(dev_net(dev), link_net)) {
Jiri Benc79e1ad12017-11-02 17:04:38 -02001533 int id = peernet2id_alloc(src_net, link_net);
Florian Westphalb1e66b92017-09-26 13:58:42 +02001534
1535 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1536 return -EMSGSIZE;
1537 }
1538 }
1539
1540 return 0;
1541}
1542
Florian Westphal070cbf5b2017-10-16 15:44:35 +02001543static int rtnl_fill_link_af(struct sk_buff *skb,
1544 const struct net_device *dev,
1545 u32 ext_filter_mask)
1546{
1547 const struct rtnl_af_ops *af_ops;
1548 struct nlattr *af_spec;
1549
1550 af_spec = nla_nest_start(skb, IFLA_AF_SPEC);
1551 if (!af_spec)
1552 return -EMSGSIZE;
1553
Florian Westphal5fa85a02017-10-16 15:44:36 +02001554 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Florian Westphal070cbf5b2017-10-16 15:44:35 +02001555 struct nlattr *af;
1556 int err;
1557
1558 if (!af_ops->fill_link_af)
1559 continue;
1560
1561 af = nla_nest_start(skb, af_ops->family);
1562 if (!af)
1563 return -EMSGSIZE;
1564
1565 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1566 /*
1567 * Caller may return ENODATA to indicate that there
1568 * was no data to be dumped. This is not an error, it
1569 * means we should trim the attribute header and
1570 * continue.
1571 */
1572 if (err == -ENODATA)
1573 nla_nest_cancel(skb, af);
1574 else if (err < 0)
1575 return -EMSGSIZE;
1576
1577 nla_nest_end(skb, af);
1578 }
1579
1580 nla_nest_end(skb, af_spec);
1581 return 0;
1582}
1583
Jiri Benc79e1ad12017-11-02 17:04:38 -02001584static int rtnl_fill_ifinfo(struct sk_buff *skb,
1585 struct net_device *dev, struct net *src_net,
Patrick McHardy575c3e22007-05-22 17:00:49 -07001586 int type, u32 pid, u32 seq, u32 change,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001587 unsigned int flags, u32 ext_filter_mask,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01001588 u32 event, int *new_nsid, int new_ifindex,
1589 int tgt_netnsid)
Thomas Grafb60c5112006-08-04 23:05:34 -07001590{
1591 struct ifinfomsg *ifm;
1592 struct nlmsghdr *nlh;
1593
Eric Dumazet2907c352011-05-25 07:34:04 +00001594 ASSERT_RTNL();
Thomas Grafb60c5112006-08-04 23:05:34 -07001595 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1596 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08001597 return -EMSGSIZE;
Thomas Grafb60c5112006-08-04 23:05:34 -07001598
1599 ifm = nlmsg_data(nlh);
1600 ifm->ifi_family = AF_UNSPEC;
1601 ifm->__ifi_pad = 0;
1602 ifm->ifi_type = dev->type;
1603 ifm->ifi_index = dev->ifindex;
1604 ifm->ifi_flags = dev_get_flags(dev);
1605 ifm->ifi_change = change;
1606
Christian Brauner7e4a8d52018-09-04 21:53:53 +02001607 if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid))
Jiri Benc79e1ad12017-11-02 17:04:38 -02001608 goto nla_put_failure;
1609
David S. Millera6574342012-04-01 20:12:00 -04001610 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1611 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1612 nla_put_u8(skb, IFLA_OPERSTATE,
1613 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1614 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1615 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
Stephen Hemminger3e7a50c2018-07-27 13:43:22 -07001616 nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) ||
1617 nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) ||
David S. Millera6574342012-04-01 20:12:00 -04001618 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
Ben Greearedbc0bb2012-03-29 12:51:30 +00001619 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001620 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
Eric Dumazetc70ce022016-03-21 09:55:10 -07001621 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1622 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001623#ifdef CONFIG_RPS
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001624 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001625#endif
Florian Westphal79110a02017-09-26 13:58:40 +02001626 nla_put_iflink(skb, dev) ||
1627 put_master_ifindex(skb, dev) ||
Jiri Pirko9a572472012-12-27 23:49:39 +00001628 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
David S. Millera6574342012-04-01 20:12:00 -04001629 (dev->qdisc &&
1630 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
Florian Westphal6c557002017-10-02 23:50:05 +02001631 nla_put_ifalias(skb, dev) ||
david decotigny2d3b4792014-03-29 09:48:35 -07001632 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
David Decotignyb2d3bcf2018-01-18 09:59:13 -08001633 atomic_read(&dev->carrier_up_count) +
1634 atomic_read(&dev->carrier_down_count)) ||
1635 nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down) ||
1636 nla_put_u32(skb, IFLA_CARRIER_UP_COUNT,
1637 atomic_read(&dev->carrier_up_count)) ||
1638 nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT,
1639 atomic_read(&dev->carrier_down_count)))
David S. Millera6574342012-04-01 20:12:00 -04001640 goto nla_put_failure;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001641
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001642 if (event != IFLA_EVENT_NONE) {
1643 if (nla_put_u32(skb, IFLA_EVENT, event))
1644 goto nla_put_failure;
1645 }
1646
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001647 if (rtnl_fill_link_ifmap(skb, dev))
1648 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
1650 if (dev->addr_len) {
David S. Millera6574342012-04-01 20:12:00 -04001651 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1652 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1653 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 }
1655
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001656 if (rtnl_phys_port_id_fill(skb, dev))
1657 goto nla_put_failure;
1658
David Aherndb24a902015-03-17 20:23:15 -06001659 if (rtnl_phys_port_name_fill(skb, dev))
1660 goto nla_put_failure;
1661
Jiri Pirko82f28412014-11-28 14:34:18 +01001662 if (rtnl_phys_switch_id_fill(skb, dev))
1663 goto nla_put_failure;
1664
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001665 if (rtnl_fill_stats(skb, dev))
Pavel Emelyanov96e74082008-05-21 14:12:46 -07001666 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
Florian Westphal250fc3d2017-09-26 13:58:41 +02001668 if (rtnl_fill_vf(skb, dev, ext_filter_mask))
David S. Millera6574342012-04-01 20:12:00 -04001669 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001670
David Gibsonc53864f2014-04-24 10:22:36 +10001671 if (rtnl_port_fill(skb, dev, ext_filter_mask))
Scott Feldman57b61082010-05-17 22:49:55 -07001672 goto nla_put_failure;
1673
Brenden Blancod1fdd912016-07-19 12:16:49 -07001674 if (rtnl_xdp_fill(skb, dev))
1675 goto nla_put_failure;
1676
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001677 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07001678 if (rtnl_link_fill(skb, dev) < 0)
1679 goto nla_put_failure;
1680 }
1681
Jiri Benc79e1ad12017-11-02 17:04:38 -02001682 if (rtnl_fill_link_netnsid(skb, dev, src_net))
Florian Westphalb1e66b92017-09-26 13:58:42 +02001683 goto nla_put_failure;
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001684
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02001685 if (new_nsid &&
1686 nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0)
1687 goto nla_put_failure;
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01001688 if (new_ifindex &&
1689 nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0)
1690 goto nla_put_failure;
1691
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02001692
Florian Westphal5fa85a02017-10-16 15:44:36 +02001693 rcu_read_lock();
Florian Westphal070cbf5b2017-10-16 15:44:35 +02001694 if (rtnl_fill_link_af(skb, dev, ext_filter_mask))
Florian Westphal5fa85a02017-10-16 15:44:36 +02001695 goto nla_put_failure_rcu;
1696 rcu_read_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +00001697
Johannes Berg053c0952015-01-16 22:09:00 +01001698 nlmsg_end(skb, nlh);
1699 return 0;
Thomas Grafb60c5112006-08-04 23:05:34 -07001700
Florian Westphal5fa85a02017-10-16 15:44:36 +02001701nla_put_failure_rcu:
1702 rcu_read_unlock();
Thomas Grafb60c5112006-08-04 23:05:34 -07001703nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001704 nlmsg_cancel(skb, nlh);
1705 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706}
1707
Jiri Pirkof7b12602014-02-18 20:53:18 +01001708static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1709 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1710 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1711 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1712 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1713 [IFLA_MTU] = { .type = NLA_U32 },
1714 [IFLA_LINK] = { .type = NLA_U32 },
1715 [IFLA_MASTER] = { .type = NLA_U32 },
1716 [IFLA_CARRIER] = { .type = NLA_U8 },
1717 [IFLA_TXQLEN] = { .type = NLA_U32 },
1718 [IFLA_WEIGHT] = { .type = NLA_U32 },
1719 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1720 [IFLA_LINKMODE] = { .type = NLA_U8 },
1721 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1722 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1723 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
Nicolas Dichtel2459b4c2017-10-11 16:24:48 +02001724 /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
1725 * allow 0-length string (needed to remove an alias).
1726 */
1727 [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001728 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1729 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1730 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1731 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1732 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1733 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1734 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1735 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
Stephen Hemminger46e6b992017-12-07 15:40:19 -08001736 [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 },
1737 [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 },
Jiri Pirko02637fc2014-11-28 14:34:16 +01001738 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
david decotigny2d3b4792014-03-29 09:48:35 -07001739 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
Jiri Pirko82f28412014-11-28 14:34:18 +01001740 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
Nicolas Dichtel317f4812015-01-15 15:11:18 +01001741 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001742 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001743 [IFLA_XDP] = { .type = NLA_NESTED },
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001744 [IFLA_EVENT] = { .type = NLA_U32 },
Serhey Popovychdb833d42017-06-20 14:35:23 +03001745 [IFLA_GROUP] = { .type = NLA_U32 },
Christian Brauner7e4a8d52018-09-04 21:53:53 +02001746 [IFLA_TARGET_NETNSID] = { .type = NLA_S32 },
David Decotignyb2d3bcf2018-01-18 09:59:13 -08001747 [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 },
1748 [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 },
Stephen Hemminger3e7a50c2018-07-27 13:43:22 -07001749 [IFLA_MIN_MTU] = { .type = NLA_U32 },
1750 [IFLA_MAX_MTU] = { .type = NLA_U32 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001751};
1752
1753static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1754 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1755 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1756 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1757 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1758};
1759
Jiri Pirkof7b12602014-02-18 20:53:18 +01001760static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
Daniel Borkmann364d5712015-02-05 18:44:04 +01001761 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1762 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
Moshe Shemesh79aab092016-09-22 12:11:15 +03001763 [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED },
Daniel Borkmann364d5712015-02-05 18:44:04 +01001764 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1765 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1766 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1767 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001768 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001769 [IFLA_VF_STATS] = { .type = NLA_NESTED },
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00001770 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
Eli Cohencc8e27c2016-03-11 22:58:34 +02001771 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1772 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001773};
1774
Jiri Pirkof7b12602014-02-18 20:53:18 +01001775static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1776 [IFLA_PORT_VF] = { .type = NLA_U32 },
1777 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1778 .len = PORT_PROFILE_MAX },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001779 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1780 .len = PORT_UUID_MAX },
1781 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1782 .len = PORT_UUID_MAX },
1783 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1784 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
Daniel Borkmann025331d2017-02-17 01:56:11 +01001785
1786 /* Unused, but we need to keep it here since user space could
1787 * fill it. It's also broken with regard to NLA_BINARY use in
1788 * combination with structs.
1789 */
1790 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1791 .len = sizeof(struct ifla_port_vsi) },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001792};
1793
Brenden Blancod1fdd912016-07-19 12:16:49 -07001794static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
1795 [IFLA_XDP_FD] = { .type = NLA_S32 },
1796 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 },
Daniel Borkmann85de8572016-11-28 23:16:54 +01001797 [IFLA_XDP_FLAGS] = { .type = NLA_U32 },
Martin KaFai Lau58038692017-06-15 17:29:09 -07001798 [IFLA_XDP_PROG_ID] = { .type = NLA_U32 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001799};
1800
David Aherndc599f72016-02-02 08:17:07 -08001801static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1802{
1803 const struct rtnl_link_ops *ops = NULL;
1804 struct nlattr *linfo[IFLA_INFO_MAX + 1];
1805
Johannes Bergfceb6432017-04-12 14:34:07 +02001806 if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla,
1807 ifla_info_policy, NULL) < 0)
David Aherndc599f72016-02-02 08:17:07 -08001808 return NULL;
1809
1810 if (linfo[IFLA_INFO_KIND]) {
1811 char kind[MODULE_NAME_LEN];
1812
1813 nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1814 ops = rtnl_link_ops_get(kind);
1815 }
1816
1817 return ops;
1818}
1819
1820static bool link_master_filtered(struct net_device *dev, int master_idx)
1821{
1822 struct net_device *master;
1823
1824 if (!master_idx)
1825 return false;
1826
1827 master = netdev_master_upper_dev_get(dev);
1828 if (!master || master->ifindex != master_idx)
1829 return true;
1830
1831 return false;
1832}
1833
1834static bool link_kind_filtered(const struct net_device *dev,
1835 const struct rtnl_link_ops *kind_ops)
1836{
1837 if (kind_ops && dev->rtnl_link_ops != kind_ops)
1838 return true;
1839
1840 return false;
1841}
1842
1843static bool link_dump_filtered(struct net_device *dev,
1844 int master_idx,
1845 const struct rtnl_link_ops *kind_ops)
1846{
1847 if (link_master_filtered(dev, master_idx) ||
1848 link_kind_filtered(dev, kind_ops))
1849 return true;
1850
1851 return false;
1852}
1853
Christian Braunerc383edc2018-09-04 21:53:47 +02001854/**
1855 * rtnl_get_net_ns_capable - Get netns if sufficiently privileged.
1856 * @sk: netlink socket
1857 * @netnsid: network namespace identifier
1858 *
1859 * Returns the network namespace identified by netnsid on success or an error
1860 * pointer on failure.
1861 */
1862struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid)
Jiri Benc79e1ad12017-11-02 17:04:38 -02001863{
1864 struct net *net;
1865
Andrei Vaginf428fe42018-01-02 23:27:33 -08001866 net = get_net_ns_by_id(sock_net(sk), netnsid);
Jiri Benc79e1ad12017-11-02 17:04:38 -02001867 if (!net)
1868 return ERR_PTR(-EINVAL);
1869
1870 /* For now, the caller is required to have CAP_NET_ADMIN in
1871 * the user namespace owning the target net ns.
1872 */
Andrei Vaginf428fe42018-01-02 23:27:33 -08001873 if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) {
Jiri Benc79e1ad12017-11-02 17:04:38 -02001874 put_net(net);
1875 return ERR_PTR(-EACCES);
1876 }
1877 return net;
1878}
Christian Braunerc383edc2018-09-04 21:53:47 +02001879EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable);
Jiri Benc79e1ad12017-11-02 17:04:38 -02001880
David Ahern905cf0a2018-10-07 20:16:30 -07001881static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh,
1882 bool strict_check, struct nlattr **tb,
1883 struct netlink_ext_ack *extack)
1884{
1885 int hdrlen;
1886
1887 if (strict_check) {
1888 struct ifinfomsg *ifm;
1889
1890 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
1891 NL_SET_ERR_MSG(extack, "Invalid header for link dump");
1892 return -EINVAL;
1893 }
1894
1895 ifm = nlmsg_data(nlh);
1896 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
1897 ifm->ifi_change) {
1898 NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request");
1899 return -EINVAL;
1900 }
1901 if (ifm->ifi_index) {
1902 NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps");
1903 return -EINVAL;
1904 }
1905
1906 return nlmsg_parse_strict(nlh, sizeof(*ifm), tb, IFLA_MAX,
1907 ifla_policy, extack);
1908 }
1909
1910 /* A hack to preserve kernel<->userspace interface.
1911 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1912 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1913 * what iproute2 < v3.9.0 used.
1914 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1915 * attribute, its netlink message is shorter than struct ifinfomsg.
1916 */
1917 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
1918 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1919
1920 return nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, extack);
1921}
1922
Thomas Grafb60c5112006-08-04 23:05:34 -07001923static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
David Ahern905cf0a2018-10-07 20:16:30 -07001925 struct netlink_ext_ack *extack = cb->extack;
1926 const struct nlmsghdr *nlh = cb->nlh;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001927 struct net *net = sock_net(skb->sk);
Jiri Benc79e1ad12017-11-02 17:04:38 -02001928 struct net *tgt_net = net;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001929 int h, s_h;
1930 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 struct net_device *dev;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001932 struct hlist_head *head;
Greg Rose115c9b82012-02-21 16:54:48 -05001933 struct nlattr *tb[IFLA_MAX+1];
1934 u32 ext_filter_mask = 0;
David Aherndc599f72016-02-02 08:17:07 -08001935 const struct rtnl_link_ops *kind_ops = NULL;
1936 unsigned int flags = NLM_F_MULTI;
1937 int master_idx = 0;
Jiri Benc79e1ad12017-11-02 17:04:38 -02001938 int netnsid = -1;
David Ahern905cf0a2018-10-07 20:16:30 -07001939 int err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001941 s_h = cb->args[0];
1942 s_idx = cb->args[1];
1943
David Ahern905cf0a2018-10-07 20:16:30 -07001944 err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack);
1945 if (err < 0) {
1946 if (cb->strict_check)
1947 return err;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001948
David Ahern905cf0a2018-10-07 20:16:30 -07001949 goto walk_entries;
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001950 }
Greg Rose115c9b82012-02-21 16:54:48 -05001951
David Ahern905cf0a2018-10-07 20:16:30 -07001952 for (i = 0; i <= IFLA_MAX; ++i) {
1953 if (!tb[i])
1954 continue;
1955
1956 /* new attributes should only be added with strict checking */
1957 switch (i) {
1958 case IFLA_TARGET_NETNSID:
1959 netnsid = nla_get_s32(tb[i]);
1960 tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid);
1961 if (IS_ERR(tgt_net)) {
1962 NL_SET_ERR_MSG(extack, "Invalid target network namespace id");
1963 return PTR_ERR(tgt_net);
1964 }
1965 break;
1966 case IFLA_EXT_MASK:
1967 ext_filter_mask = nla_get_u32(tb[i]);
1968 break;
1969 case IFLA_MASTER:
1970 master_idx = nla_get_u32(tb[i]);
1971 break;
1972 case IFLA_LINKINFO:
1973 kind_ops = linkinfo_to_kind_ops(tb[i]);
1974 break;
1975 default:
1976 if (cb->strict_check) {
1977 NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request");
1978 return -EINVAL;
1979 }
1980 }
1981 }
1982
1983 if (master_idx || kind_ops)
1984 flags |= NLM_F_DUMP_FILTERED;
1985
1986walk_entries:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001987 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1988 idx = 0;
Jiri Benc79e1ad12017-11-02 17:04:38 -02001989 head = &tgt_net->dev_index_head[h];
Eric Dumazetcac5e652015-02-27 09:42:50 -08001990 hlist_for_each_entry(dev, head, index_hlist) {
David Aherndc599f72016-02-02 08:17:07 -08001991 if (link_dump_filtered(dev, master_idx, kind_ops))
Zhang Shengju3f0ae052016-11-19 23:28:32 +08001992 goto cont;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001993 if (idx < s_idx)
1994 goto cont;
Jiri Benc79e1ad12017-11-02 17:04:38 -02001995 err = rtnl_fill_ifinfo(skb, dev, net,
1996 RTM_NEWLINK,
David Gibson973462b2014-04-24 10:22:35 +10001997 NETLINK_CB(cb->skb).portid,
David Ahern905cf0a2018-10-07 20:16:30 -07001998 nlh->nlmsg_seq, 0, flags,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01001999 ext_filter_mask, 0, NULL, 0,
Jiri Benc79e1ad12017-11-02 17:04:38 -02002000 netnsid);
David Gibson973462b2014-04-24 10:22:35 +10002001
David Ahernf6c57752017-05-15 23:19:17 -07002002 if (err < 0) {
2003 if (likely(skb->len))
2004 goto out;
2005
2006 goto out_err;
2007 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07002008cont:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002009 idx++;
2010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 }
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002012out:
David Ahernf6c57752017-05-15 23:19:17 -07002013 err = skb->len;
2014out_err:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002015 cb->args[1] = idx;
2016 cb->args[0] = h;
Jakub Sitnickid0225782017-08-09 17:39:12 +02002017 cb->seq = net->dev_base_seq;
2018 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Jiri Benc79e1ad12017-11-02 17:04:38 -02002019 if (netnsid >= 0)
2020 put_net(tgt_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
David Ahernf6c57752017-05-15 23:19:17 -07002022 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023}
2024
Johannes Bergfceb6432017-04-12 14:34:07 +02002025int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
2026 struct netlink_ext_ack *exterr)
Jiri Pirkof7b12602014-02-18 20:53:18 +01002027{
Johannes Bergfceb6432017-04-12 14:34:07 +02002028 return nla_parse(tb, IFLA_MAX, head, len, ifla_policy, exterr);
Jiri Pirkof7b12602014-02-18 20:53:18 +01002029}
2030EXPORT_SYMBOL(rtnl_nla_parse_ifla);
Scott Feldman57b61082010-05-17 22:49:55 -07002031
Eric W. Biederman81adee42009-11-08 00:53:51 -08002032struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
2033{
2034 struct net *net;
2035 /* Examine the link attributes and figure out which
2036 * network namespace we are talking about.
2037 */
2038 if (tb[IFLA_NET_NS_PID])
2039 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
Eric W. Biedermanf0630522011-05-04 17:51:50 -07002040 else if (tb[IFLA_NET_NS_FD])
2041 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
Eric W. Biederman81adee42009-11-08 00:53:51 -08002042 else
2043 net = get_net(src_net);
2044 return net;
2045}
2046EXPORT_SYMBOL(rtnl_link_get_net);
2047
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002048/* Figure out which network namespace we are talking about by
2049 * examining the link attributes in the following order:
2050 *
2051 * 1. IFLA_NET_NS_PID
2052 * 2. IFLA_NET_NS_FD
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002053 * 3. IFLA_TARGET_NETNSID
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002054 */
2055static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net,
2056 struct nlattr *tb[])
2057{
2058 struct net *net;
2059
2060 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])
2061 return rtnl_link_get_net(src_net, tb);
2062
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002063 if (!tb[IFLA_TARGET_NETNSID])
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002064 return get_net(src_net);
2065
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002066 net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID]));
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002067 if (!net)
2068 return ERR_PTR(-EINVAL);
2069
2070 return net;
2071}
2072
2073static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb,
2074 struct net *src_net,
2075 struct nlattr *tb[], int cap)
2076{
2077 struct net *net;
2078
2079 net = rtnl_link_get_net_by_nlattr(src_net, tb);
2080 if (IS_ERR(net))
2081 return net;
2082
2083 if (!netlink_ns_capable(skb, net->user_ns, cap)) {
2084 put_net(net);
2085 return ERR_PTR(-EPERM);
2086 }
2087
2088 return net;
2089}
2090
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002091/* Verify that rtnetlink requests do not pass additional properties
2092 * potentially referring to different network namespaces.
2093 */
2094static int rtnl_ensure_unique_netns(struct nlattr *tb[],
2095 struct netlink_ext_ack *extack,
2096 bool netns_id_only)
2097{
2098
2099 if (netns_id_only) {
2100 if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD])
2101 return 0;
2102
2103 NL_SET_ERR_MSG(extack, "specified netns attribute not supported");
2104 return -EOPNOTSUPP;
2105 }
2106
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002107 if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]))
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002108 goto invalid_attr;
2109
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002110 if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD]))
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002111 goto invalid_attr;
2112
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002113 if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID]))
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002114 goto invalid_attr;
2115
2116 return 0;
2117
2118invalid_attr:
2119 NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified");
2120 return -EINVAL;
2121}
2122
Thomas Graf1840bb12008-02-23 19:54:36 -08002123static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
2124{
2125 if (dev) {
2126 if (tb[IFLA_ADDRESS] &&
2127 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
2128 return -EINVAL;
2129
2130 if (tb[IFLA_BROADCAST] &&
2131 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
2132 return -EINVAL;
2133 }
2134
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002135 if (tb[IFLA_AF_SPEC]) {
2136 struct nlattr *af;
2137 int rem, err;
2138
2139 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2140 const struct rtnl_af_ops *af_ops;
2141
Florian Westphal5fa85a02017-10-16 15:44:36 +02002142 rcu_read_lock();
2143 af_ops = rtnl_af_lookup(nla_type(af));
2144 if (!af_ops) {
2145 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002146 return -EAFNOSUPPORT;
Florian Westphal5fa85a02017-10-16 15:44:36 +02002147 }
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002148
Florian Westphal5fa85a02017-10-16 15:44:36 +02002149 if (!af_ops->set_link_af) {
2150 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002151 return -EOPNOTSUPP;
Florian Westphal5fa85a02017-10-16 15:44:36 +02002152 }
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002153
2154 if (af_ops->validate_link_af) {
Kurt Van Dijck6d3a9a62011-01-26 04:55:24 +00002155 err = af_ops->validate_link_af(dev, af);
Florian Westphal5fa85a02017-10-16 15:44:36 +02002156 if (err < 0) {
2157 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002158 return err;
Florian Westphal5fa85a02017-10-16 15:44:36 +02002159 }
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002160 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02002161
2162 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002163 }
2164 }
2165
Thomas Graf1840bb12008-02-23 19:54:36 -08002166 return 0;
2167}
2168
Eli Cohencc8e27c2016-03-11 22:58:34 +02002169static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
2170 int guid_type)
2171{
2172 const struct net_device_ops *ops = dev->netdev_ops;
2173
2174 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
2175}
2176
2177static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
2178{
2179 if (dev->type != ARPHRD_INFINIBAND)
2180 return -EOPNOTSUPP;
2181
2182 return handle_infiniband_guid(dev, ivt, guid_type);
2183}
2184
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002185static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
Chris Wrightc02db8c2010-05-16 01:05:45 -07002186{
Chris Wrightc02db8c2010-05-16 01:05:45 -07002187 const struct net_device_ops *ops = dev->netdev_ops;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002188 int err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002189
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002190 if (tb[IFLA_VF_MAC]) {
2191 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03002192
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002193 err = -EOPNOTSUPP;
2194 if (ops->ndo_set_vf_mac)
2195 err = ops->ndo_set_vf_mac(dev, ivm->vf,
2196 ivm->mac);
2197 if (err < 0)
2198 return err;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002199 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002200
2201 if (tb[IFLA_VF_VLAN]) {
2202 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
2203
2204 err = -EOPNOTSUPP;
2205 if (ops->ndo_set_vf_vlan)
2206 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
Moshe Shemesh79aab092016-09-22 12:11:15 +03002207 ivv->qos,
2208 htons(ETH_P_8021Q));
2209 if (err < 0)
2210 return err;
2211 }
2212
2213 if (tb[IFLA_VF_VLAN_LIST]) {
2214 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
2215 struct nlattr *attr;
2216 int rem, len = 0;
2217
2218 err = -EOPNOTSUPP;
2219 if (!ops->ndo_set_vf_vlan)
2220 return err;
2221
2222 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
2223 if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
2224 nla_len(attr) < NLA_HDRLEN) {
2225 return -EINVAL;
2226 }
2227 if (len >= MAX_VLAN_LIST_LEN)
2228 return -EOPNOTSUPP;
2229 ivvl[len] = nla_data(attr);
2230
2231 len++;
2232 }
Arnd Bergmannfa34cd92016-09-30 18:13:49 +02002233 if (len == 0)
2234 return -EINVAL;
2235
Moshe Shemesh79aab092016-09-22 12:11:15 +03002236 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
2237 ivvl[0]->qos, ivvl[0]->vlan_proto);
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002238 if (err < 0)
2239 return err;
2240 }
2241
2242 if (tb[IFLA_VF_TX_RATE]) {
2243 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
2244 struct ifla_vf_info ivf;
2245
2246 err = -EOPNOTSUPP;
2247 if (ops->ndo_get_vf_config)
2248 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
2249 if (err < 0)
2250 return err;
2251
2252 err = -EOPNOTSUPP;
2253 if (ops->ndo_set_vf_rate)
2254 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2255 ivf.min_tx_rate,
2256 ivt->rate);
2257 if (err < 0)
2258 return err;
2259 }
2260
2261 if (tb[IFLA_VF_RATE]) {
2262 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
2263
2264 err = -EOPNOTSUPP;
2265 if (ops->ndo_set_vf_rate)
2266 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2267 ivt->min_tx_rate,
2268 ivt->max_tx_rate);
2269 if (err < 0)
2270 return err;
2271 }
2272
2273 if (tb[IFLA_VF_SPOOFCHK]) {
2274 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
2275
2276 err = -EOPNOTSUPP;
2277 if (ops->ndo_set_vf_spoofchk)
2278 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
2279 ivs->setting);
2280 if (err < 0)
2281 return err;
2282 }
2283
2284 if (tb[IFLA_VF_LINK_STATE]) {
2285 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
2286
2287 err = -EOPNOTSUPP;
2288 if (ops->ndo_set_vf_link_state)
2289 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
2290 ivl->link_state);
2291 if (err < 0)
2292 return err;
2293 }
2294
2295 if (tb[IFLA_VF_RSS_QUERY_EN]) {
2296 struct ifla_vf_rss_query_en *ivrssq_en;
2297
2298 err = -EOPNOTSUPP;
2299 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
2300 if (ops->ndo_set_vf_rss_query_en)
2301 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
2302 ivrssq_en->setting);
2303 if (err < 0)
2304 return err;
2305 }
2306
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00002307 if (tb[IFLA_VF_TRUST]) {
2308 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
2309
2310 err = -EOPNOTSUPP;
2311 if (ops->ndo_set_vf_trust)
2312 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
2313 if (err < 0)
2314 return err;
2315 }
2316
Eli Cohencc8e27c2016-03-11 22:58:34 +02002317 if (tb[IFLA_VF_IB_NODE_GUID]) {
2318 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
2319
2320 if (!ops->ndo_set_vf_guid)
2321 return -EOPNOTSUPP;
2322
2323 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
2324 }
2325
2326 if (tb[IFLA_VF_IB_PORT_GUID]) {
2327 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
2328
2329 if (!ops->ndo_set_vf_guid)
2330 return -EOPNOTSUPP;
2331
2332 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
2333 }
2334
Chris Wrightc02db8c2010-05-16 01:05:45 -07002335 return err;
2336}
2337
David Ahern33eaf2a2017-10-04 17:48:46 -07002338static int do_set_master(struct net_device *dev, int ifindex,
2339 struct netlink_ext_ack *extack)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002340{
Jiri Pirko898e5062013-01-03 22:48:52 +00002341 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002342 const struct net_device_ops *ops;
2343 int err;
2344
Jiri Pirko898e5062013-01-03 22:48:52 +00002345 if (upper_dev) {
2346 if (upper_dev->ifindex == ifindex)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002347 return 0;
Jiri Pirko898e5062013-01-03 22:48:52 +00002348 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002349 if (ops->ndo_del_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002350 err = ops->ndo_del_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002351 if (err)
2352 return err;
2353 } else {
2354 return -EOPNOTSUPP;
2355 }
2356 }
2357
2358 if (ifindex) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002359 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
2360 if (!upper_dev)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002361 return -EINVAL;
Jiri Pirko898e5062013-01-03 22:48:52 +00002362 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002363 if (ops->ndo_add_slave) {
David Ahern33eaf2a2017-10-04 17:48:46 -07002364 err = ops->ndo_add_slave(upper_dev, dev, extack);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002365 if (err)
2366 return err;
2367 } else {
2368 return -EOPNOTSUPP;
2369 }
2370 }
2371 return 0;
2372}
2373
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002374#define DO_SETLINK_MODIFIED 0x01
Nicolas Dichtelba998902014-09-01 16:07:29 +02002375/* notify flag means notify + modified. */
2376#define DO_SETLINK_NOTIFY 0x03
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002377static int do_setlink(const struct sk_buff *skb,
2378 struct net_device *dev, struct ifinfomsg *ifm,
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002379 struct netlink_ext_ack *extack,
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002380 struct nlattr **tb, char *ifname, int status)
Patrick McHardy0157f602007-06-13 12:03:36 -07002381{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002382 const struct net_device_ops *ops = dev->netdev_ops;
Patrick McHardy0157f602007-06-13 12:03:36 -07002383 int err;
2384
Eric Dumazet644c7ee2018-06-05 09:25:19 -07002385 err = validate_linkmsg(dev, tb);
2386 if (err < 0)
2387 return err;
2388
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002389 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) {
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002390 struct net *net = rtnl_link_get_net_capable(skb, dev_net(dev),
2391 tb, CAP_NET_ADMIN);
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002392 if (IS_ERR(net)) {
2393 err = PTR_ERR(net);
2394 goto errout;
2395 }
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002396
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002397 err = dev_change_net_namespace(dev, net, ifname);
2398 put_net(net);
2399 if (err)
2400 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002401 status |= DO_SETLINK_MODIFIED;
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002402 }
2403
Patrick McHardy0157f602007-06-13 12:03:36 -07002404 if (tb[IFLA_MAP]) {
2405 struct rtnl_link_ifmap *u_map;
2406 struct ifmap k_map;
2407
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002408 if (!ops->ndo_set_config) {
Patrick McHardy0157f602007-06-13 12:03:36 -07002409 err = -EOPNOTSUPP;
2410 goto errout;
2411 }
2412
2413 if (!netif_device_present(dev)) {
2414 err = -ENODEV;
2415 goto errout;
2416 }
2417
2418 u_map = nla_data(tb[IFLA_MAP]);
2419 k_map.mem_start = (unsigned long) u_map->mem_start;
2420 k_map.mem_end = (unsigned long) u_map->mem_end;
2421 k_map.base_addr = (unsigned short) u_map->base_addr;
2422 k_map.irq = (unsigned char) u_map->irq;
2423 k_map.dma = (unsigned char) u_map->dma;
2424 k_map.port = (unsigned char) u_map->port;
2425
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002426 err = ops->ndo_set_config(dev, &k_map);
Patrick McHardy0157f602007-06-13 12:03:36 -07002427 if (err < 0)
2428 goto errout;
2429
Nicolas Dichtelba998902014-09-01 16:07:29 +02002430 status |= DO_SETLINK_NOTIFY;
Patrick McHardy0157f602007-06-13 12:03:36 -07002431 }
2432
2433 if (tb[IFLA_ADDRESS]) {
2434 struct sockaddr *sa;
2435 int len;
2436
WANG Cong153711f2017-07-20 11:27:57 -07002437 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2438 sizeof(*sa));
Patrick McHardy0157f602007-06-13 12:03:36 -07002439 sa = kmalloc(len, GFP_KERNEL);
2440 if (!sa) {
2441 err = -ENOMEM;
2442 goto errout;
2443 }
2444 sa->sa_family = dev->type;
2445 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
2446 dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00002447 err = dev_set_mac_address(dev, sa);
Patrick McHardy0157f602007-06-13 12:03:36 -07002448 kfree(sa);
2449 if (err)
2450 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002451 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002452 }
2453
2454 if (tb[IFLA_MTU]) {
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07002455 err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack);
Patrick McHardy0157f602007-06-13 12:03:36 -07002456 if (err < 0)
2457 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002458 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002459 }
2460
Vlad Dogarucbda10f2011-01-13 23:38:30 +00002461 if (tb[IFLA_GROUP]) {
2462 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Nicolas Dichtelba998902014-09-01 16:07:29 +02002463 status |= DO_SETLINK_NOTIFY;
Vlad Dogarucbda10f2011-01-13 23:38:30 +00002464 }
2465
Patrick McHardy0157f602007-06-13 12:03:36 -07002466 /*
2467 * Interface selected by interface index but interface
2468 * name provided implies that a name change has been
2469 * requested.
2470 */
2471 if (ifm->ifi_index > 0 && ifname[0]) {
2472 err = dev_change_name(dev, ifname);
2473 if (err < 0)
2474 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002475 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002476 }
2477
Stephen Hemminger0b815a12008-09-22 21:28:11 -07002478 if (tb[IFLA_IFALIAS]) {
2479 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2480 nla_len(tb[IFLA_IFALIAS]));
2481 if (err < 0)
2482 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002483 status |= DO_SETLINK_NOTIFY;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07002484 }
2485
Patrick McHardy0157f602007-06-13 12:03:36 -07002486 if (tb[IFLA_BROADCAST]) {
2487 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00002488 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Patrick McHardy0157f602007-06-13 12:03:36 -07002489 }
2490
2491 if (ifm->ifi_flags || ifm->ifi_change) {
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
Rami Rosenc0713562012-11-30 01:08:47 +00002888struct net_device *rtnl_create_link(struct net *net,
Thomas Graf78ebb0d2015-04-10 01:45:53 +02002889 const char *ifname, unsigned char name_assign_type,
Tom Gundersen55177502014-07-14 16:37:25 +02002890 const struct rtnl_link_ops *ops, struct nlattr *tb[])
Pavel Emelianove7199282007-08-08 22:16:38 -07002891{
Pavel Emelianove7199282007-08-08 22:16:38 -07002892 struct net_device *dev;
Jiri Pirkod40156a2012-07-20 02:28:47 +00002893 unsigned int num_tx_queues = 1;
2894 unsigned int num_rx_queues = 1;
Pavel Emelianove7199282007-08-08 22:16:38 -07002895
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002896 if (tb[IFLA_NUM_TX_QUEUES])
2897 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
2898 else if (ops->get_num_tx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002899 num_tx_queues = ops->get_num_tx_queues();
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002900
2901 if (tb[IFLA_NUM_RX_QUEUES])
2902 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
2903 else if (ops->get_num_rx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002904 num_rx_queues = ops->get_num_rx_queues();
stephen hemmingerefacb302012-04-10 18:34:43 +00002905
Eric Dumazet0e1d6ec2018-10-02 15:47:35 -07002906 if (num_tx_queues < 1 || num_tx_queues > 4096)
2907 return ERR_PTR(-EINVAL);
2908
2909 if (num_rx_queues < 1 || num_rx_queues > 4096)
2910 return ERR_PTR(-EINVAL);
2911
Tom Gundersen55177502014-07-14 16:37:25 +02002912 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
Tom Gundersenc835a672014-07-14 16:37:24 +02002913 ops->setup, num_tx_queues, num_rx_queues);
Pavel Emelianove7199282007-08-08 22:16:38 -07002914 if (!dev)
Tobias Klauserd1892e42017-02-20 16:32:06 +01002915 return ERR_PTR(-ENOMEM);
Pavel Emelianove7199282007-08-08 22:16:38 -07002916
Eric W. Biederman81adee42009-11-08 00:53:51 -08002917 dev_net_set(dev, net);
2918 dev->rtnl_link_ops = ops;
Patrick McHardy3729d502010-02-26 06:34:54 +00002919 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
Eric W. Biederman81adee42009-11-08 00:53:51 -08002920
Pavel Emelianove7199282007-08-08 22:16:38 -07002921 if (tb[IFLA_MTU])
2922 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002923 if (tb[IFLA_ADDRESS]) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002924 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
2925 nla_len(tb[IFLA_ADDRESS]));
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002926 dev->addr_assign_type = NET_ADDR_SET;
2927 }
Pavel Emelianove7199282007-08-08 22:16:38 -07002928 if (tb[IFLA_BROADCAST])
2929 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
2930 nla_len(tb[IFLA_BROADCAST]));
2931 if (tb[IFLA_TXQLEN])
2932 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
2933 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002934 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Pavel Emelianove7199282007-08-08 22:16:38 -07002935 if (tb[IFLA_LINKMODE])
2936 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
Patrick McHardyffa934f2011-01-20 03:00:42 +00002937 if (tb[IFLA_GROUP])
2938 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Stephen Hemminger46e6b992017-12-07 15:40:19 -08002939 if (tb[IFLA_GSO_MAX_SIZE])
2940 netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE]));
2941 if (tb[IFLA_GSO_MAX_SEGS])
Stephen Hemmingera0b586f2017-12-08 15:34:13 -08002942 dev->gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
Pavel Emelianove7199282007-08-08 22:16:38 -07002943
2944 return dev;
Pavel Emelianove7199282007-08-08 22:16:38 -07002945}
Eric Dumazete0d087a2009-11-07 01:26:17 -08002946EXPORT_SYMBOL(rtnl_create_link);
Pavel Emelianove7199282007-08-08 22:16:38 -07002947
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002948static int rtnl_group_changelink(const struct sk_buff *skb,
2949 struct net *net, int group,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002950 struct ifinfomsg *ifm,
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002951 struct netlink_ext_ack *extack,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002952 struct nlattr **tb)
2953{
WANG Congd0795352015-03-23 16:31:09 -07002954 struct net_device *dev, *aux;
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002955 int err;
2956
WANG Congd0795352015-03-23 16:31:09 -07002957 for_each_netdev_safe(net, dev, aux) {
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002958 if (dev->group == group) {
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002959 err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002960 if (err < 0)
2961 return err;
2962 }
2963 }
2964
2965 return 0;
2966}
2967
David Ahernc21ef3e2017-04-16 09:48:24 -07002968static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2969 struct netlink_ext_ack *extack)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002970{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002971 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002972 const struct rtnl_link_ops *ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002973 const struct rtnl_link_ops *m_ops = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002974 struct net_device *dev;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002975 struct net_device *master_dev = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002976 struct ifinfomsg *ifm;
2977 char kind[MODULE_NAME_LEN];
2978 char ifname[IFNAMSIZ];
2979 struct nlattr *tb[IFLA_MAX+1];
2980 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
Tom Gundersen55177502014-07-14 16:37:25 +02002981 unsigned char name_assign_type = NET_NAME_USER;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002982 int err;
2983
Johannes Berg95a5afc2008-10-16 15:24:51 -07002984#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002985replay:
Thomas Graf8072f082007-07-31 14:13:50 -07002986#endif
David Ahernc21ef3e2017-04-16 09:48:24 -07002987 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002988 if (err < 0)
2989 return err;
2990
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002991 err = rtnl_ensure_unique_netns(tb, extack, false);
2992 if (err < 0)
2993 return err;
2994
Patrick McHardy38f7b872007-06-13 12:03:51 -07002995 if (tb[IFLA_IFNAME])
2996 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2997 else
2998 ifname[0] = '\0';
2999
3000 ifm = nlmsg_data(nlh);
3001 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07003002 dev = __dev_get_by_index(net, ifm->ifi_index);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00003003 else {
3004 if (ifname[0])
3005 dev = __dev_get_by_name(net, ifname);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00003006 else
3007 dev = NULL;
3008 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07003009
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01003010 if (dev) {
3011 master_dev = netdev_master_upper_dev_get(dev);
3012 if (master_dev)
3013 m_ops = master_dev->rtnl_link_ops;
3014 }
3015
Eric Dumazete0d087a2009-11-07 01:26:17 -08003016 err = validate_linkmsg(dev, tb);
3017 if (err < 0)
Thomas Graf1840bb12008-02-23 19:54:36 -08003018 return err;
3019
Patrick McHardy38f7b872007-06-13 12:03:51 -07003020 if (tb[IFLA_LINKINFO]) {
3021 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +02003022 tb[IFLA_LINKINFO], ifla_info_policy,
3023 NULL);
Patrick McHardy38f7b872007-06-13 12:03:51 -07003024 if (err < 0)
3025 return err;
3026 } else
3027 memset(linkinfo, 0, sizeof(linkinfo));
3028
3029 if (linkinfo[IFLA_INFO_KIND]) {
3030 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
3031 ops = rtnl_link_ops_get(kind);
3032 } else {
3033 kind[0] = '\0';
3034 ops = NULL;
3035 }
3036
3037 if (1) {
Kees Cookccf8dbc2018-05-30 15:20:52 -07003038 struct nlattr *attr[RTNL_MAX_TYPE + 1];
3039 struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1];
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01003040 struct nlattr **data = NULL;
3041 struct nlattr **slave_data = NULL;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01003042 struct net *dest_net, *link_net = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003043
3044 if (ops) {
Kees Cookccf8dbc2018-05-30 15:20:52 -07003045 if (ops->maxtype > RTNL_MAX_TYPE)
3046 return -EINVAL;
3047
Patrick McHardy38f7b872007-06-13 12:03:51 -07003048 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
3049 err = nla_parse_nested(attr, ops->maxtype,
3050 linkinfo[IFLA_INFO_DATA],
Johannes Bergfceb6432017-04-12 14:34:07 +02003051 ops->policy, NULL);
Patrick McHardy38f7b872007-06-13 12:03:51 -07003052 if (err < 0)
3053 return err;
3054 data = attr;
3055 }
3056 if (ops->validate) {
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02003057 err = ops->validate(tb, data, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07003058 if (err < 0)
3059 return err;
3060 }
3061 }
3062
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01003063 if (m_ops) {
Prashant Bholeaf066ed2018-06-01 17:16:58 +09003064 if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)
Kees Cookccf8dbc2018-05-30 15:20:52 -07003065 return -EINVAL;
3066
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01003067 if (m_ops->slave_maxtype &&
3068 linkinfo[IFLA_INFO_SLAVE_DATA]) {
3069 err = nla_parse_nested(slave_attr,
3070 m_ops->slave_maxtype,
3071 linkinfo[IFLA_INFO_SLAVE_DATA],
Johannes Bergfceb6432017-04-12 14:34:07 +02003072 m_ops->slave_policy,
3073 NULL);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01003074 if (err < 0)
3075 return err;
3076 slave_data = slave_attr;
3077 }
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01003078 }
3079
Patrick McHardy38f7b872007-06-13 12:03:51 -07003080 if (dev) {
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02003081 int status = 0;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003082
3083 if (nlh->nlmsg_flags & NLM_F_EXCL)
3084 return -EEXIST;
3085 if (nlh->nlmsg_flags & NLM_F_REPLACE)
3086 return -EOPNOTSUPP;
3087
3088 if (linkinfo[IFLA_INFO_DATA]) {
3089 if (!ops || ops != dev->rtnl_link_ops ||
3090 !ops->changelink)
3091 return -EOPNOTSUPP;
3092
Matthias Schifferad744b22017-06-25 23:56:00 +02003093 err = ops->changelink(dev, tb, data, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07003094 if (err < 0)
3095 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02003096 status |= DO_SETLINK_NOTIFY;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003097 }
3098
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01003099 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
3100 if (!m_ops || !m_ops->slave_changelink)
3101 return -EOPNOTSUPP;
3102
3103 err = m_ops->slave_changelink(master_dev, dev,
Matthias Schiffer17dd0ec2017-06-25 23:56:02 +02003104 tb, slave_data,
3105 extack);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01003106 if (err < 0)
3107 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02003108 status |= DO_SETLINK_NOTIFY;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01003109 }
3110
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07003111 return do_setlink(skb, dev, ifm, extack, tb, ifname,
3112 status);
Patrick McHardy38f7b872007-06-13 12:03:51 -07003113 }
3114
Patrick McHardyffa934f2011-01-20 03:00:42 +00003115 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
3116 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07003117 return rtnl_group_changelink(skb, net,
Patrick McHardyffa934f2011-01-20 03:00:42 +00003118 nla_get_u32(tb[IFLA_GROUP]),
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07003119 ifm, extack, tb);
Patrick McHardy38f7b872007-06-13 12:03:51 -07003120 return -ENODEV;
Patrick McHardyffa934f2011-01-20 03:00:42 +00003121 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07003122
Theuns Verwoerd160ca012017-01-31 12:23:46 +13003123 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
Patrick McHardy38f7b872007-06-13 12:03:51 -07003124 return -EOPNOTSUPP;
3125
3126 if (!ops) {
Johannes Berg95a5afc2008-10-16 15:24:51 -07003127#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07003128 if (kind[0]) {
3129 __rtnl_unlock();
3130 request_module("rtnl-link-%s", kind);
3131 rtnl_lock();
3132 ops = rtnl_link_ops_get(kind);
3133 if (ops)
3134 goto replay;
3135 }
3136#endif
3137 return -EOPNOTSUPP;
3138 }
3139
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02003140 if (!ops->setup)
3141 return -EOPNOTSUPP;
3142
Tom Gundersen55177502014-07-14 16:37:25 +02003143 if (!ifname[0]) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07003144 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
Tom Gundersen55177502014-07-14 16:37:25 +02003145 name_assign_type = NET_NAME_ENUM;
3146 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07003147
Christian Brauner5bb8ed02018-01-29 18:07:20 +01003148 dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN);
Eric W. Biederman13ad1772011-01-29 14:57:22 +00003149 if (IS_ERR(dest_net))
3150 return PTR_ERR(dest_net);
3151
Nicolas Dichtel317f4812015-01-15 15:11:18 +01003152 if (tb[IFLA_LINK_NETNSID]) {
3153 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
3154
3155 link_net = get_net_ns_by_id(dest_net, id);
3156 if (!link_net) {
3157 err = -EINVAL;
3158 goto out;
3159 }
Eric W. Biederman06615be2015-02-26 16:20:07 -06003160 err = -EPERM;
3161 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
3162 goto out;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01003163 }
3164
3165 dev = rtnl_create_link(link_net ? : dest_net, ifname,
3166 name_assign_type, ops, tb);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00003167 if (IS_ERR(dev)) {
Pavel Emelianove7199282007-08-08 22:16:38 -07003168 err = PTR_ERR(dev);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00003169 goto out;
3170 }
3171
3172 dev->ifindex = ifm->ifi_index;
3173
Cong Wang0e0eee22014-02-11 15:51:30 -08003174 if (ops->newlink) {
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02003175 err = ops->newlink(link_net ? : net, dev, tb, data,
3176 extack);
Cong Wang0e0eee22014-02-11 15:51:30 -08003177 /* Drivers should call free_netdev() in ->destructor
Cong Wange51fb152014-06-03 16:40:47 -07003178 * and unregister it on failure after registration
3179 * so that device could be finally freed in rtnl_unlock.
Cong Wang0e0eee22014-02-11 15:51:30 -08003180 */
Cong Wange51fb152014-06-03 16:40:47 -07003181 if (err < 0) {
3182 /* If device is not registered at all, free it now */
3183 if (dev->reg_state == NETREG_UNINITIALIZED)
3184 free_netdev(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08003185 goto out;
Cong Wange51fb152014-06-03 16:40:47 -07003186 }
Cong Wang0e0eee22014-02-11 15:51:30 -08003187 } else {
Patrick McHardy2d85cba2007-07-11 19:42:13 -07003188 err = register_netdevice(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08003189 if (err < 0) {
3190 free_netdev(dev);
3191 goto out;
3192 }
Dan Carpenterfce9b9b2013-08-14 12:35:42 +03003193 }
Patrick McHardy3729d502010-02-26 06:34:54 +00003194 err = rtnl_configure_link(dev, ifm);
David S. Miller43638902015-03-10 21:58:32 -04003195 if (err < 0)
3196 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01003197 if (link_net) {
Nicolas Dichtel317f4812015-01-15 15:11:18 +01003198 err = dev_change_net_namespace(dev, dest_net, ifname);
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01003199 if (err < 0)
David S. Miller43638902015-03-10 21:58:32 -04003200 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01003201 }
Theuns Verwoerd160ca012017-01-31 12:23:46 +13003202 if (tb[IFLA_MASTER]) {
David Ahern33eaf2a2017-10-04 17:48:46 -07003203 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]),
3204 extack);
Theuns Verwoerd160ca012017-01-31 12:23:46 +13003205 if (err)
3206 goto out_unregister;
3207 }
Patrick McHardy3729d502010-02-26 06:34:54 +00003208out:
Nicolas Dichtel317f4812015-01-15 15:11:18 +01003209 if (link_net)
3210 put_net(link_net);
Eric W. Biederman81adee42009-11-08 00:53:51 -08003211 put_net(dest_net);
Patrick McHardy38f7b872007-06-13 12:03:51 -07003212 return err;
David S. Miller43638902015-03-10 21:58:32 -04003213out_unregister:
3214 if (ops->newlink) {
3215 LIST_HEAD(list_kill);
3216
3217 ops->dellink(dev, &list_kill);
3218 unregister_netdevice_many(&list_kill);
3219 } else {
3220 unregister_netdevice(dev);
3221 }
3222 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003223 }
3224}
3225
David Ahernc21ef3e2017-04-16 09:48:24 -07003226static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3227 struct netlink_ext_ack *extack)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003228{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003229 struct net *net = sock_net(skb->sk);
Jiri Benc79e1ad12017-11-02 17:04:38 -02003230 struct net *tgt_net = net;
Thomas Grafb60c5112006-08-04 23:05:34 -07003231 struct ifinfomsg *ifm;
Eric Dumazeta3d12892009-10-21 10:59:31 +00003232 char ifname[IFNAMSIZ];
Thomas Grafb60c5112006-08-04 23:05:34 -07003233 struct nlattr *tb[IFLA_MAX+1];
3234 struct net_device *dev = NULL;
3235 struct sk_buff *nskb;
Jiri Benc79e1ad12017-11-02 17:04:38 -02003236 int netnsid = -1;
Thomas Graf339bf982006-11-10 14:10:15 -08003237 int err;
Greg Rose115c9b82012-02-21 16:54:48 -05003238 u32 ext_filter_mask = 0;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003239
David Ahernc21ef3e2017-04-16 09:48:24 -07003240 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Thomas Grafb60c5112006-08-04 23:05:34 -07003241 if (err < 0)
Eric Sesterhenn9918f232006-09-26 23:26:38 -07003242 return err;
Thomas Grafb60c5112006-08-04 23:05:34 -07003243
Christian Brauner4ff66ca2018-02-07 13:53:20 +01003244 err = rtnl_ensure_unique_netns(tb, extack, true);
3245 if (err < 0)
3246 return err;
3247
Christian Brauner7e4a8d52018-09-04 21:53:53 +02003248 if (tb[IFLA_TARGET_NETNSID]) {
3249 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
Christian Braunerc383edc2018-09-04 21:53:47 +02003250 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
Jiri Benc79e1ad12017-11-02 17:04:38 -02003251 if (IS_ERR(tgt_net))
3252 return PTR_ERR(tgt_net);
3253 }
3254
Eric Dumazeta3d12892009-10-21 10:59:31 +00003255 if (tb[IFLA_IFNAME])
3256 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
3257
Greg Rose115c9b82012-02-21 16:54:48 -05003258 if (tb[IFLA_EXT_MASK])
3259 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3260
Jiri Benc79e1ad12017-11-02 17:04:38 -02003261 err = -EINVAL;
Thomas Grafb60c5112006-08-04 23:05:34 -07003262 ifm = nlmsg_data(nlh);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003263 if (ifm->ifi_index > 0)
Jiri Benc79e1ad12017-11-02 17:04:38 -02003264 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003265 else if (tb[IFLA_IFNAME])
Jiri Benc79e1ad12017-11-02 17:04:38 -02003266 dev = __dev_get_by_name(tgt_net, ifname);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003267 else
Jiri Benc79e1ad12017-11-02 17:04:38 -02003268 goto out;
Thomas Grafb60c5112006-08-04 23:05:34 -07003269
Jiri Benc79e1ad12017-11-02 17:04:38 -02003270 err = -ENODEV;
Eric Dumazeta3d12892009-10-21 10:59:31 +00003271 if (dev == NULL)
Jiri Benc79e1ad12017-11-02 17:04:38 -02003272 goto out;
Eric Dumazeta3d12892009-10-21 10:59:31 +00003273
Jiri Benc79e1ad12017-11-02 17:04:38 -02003274 err = -ENOBUFS;
Greg Rose115c9b82012-02-21 16:54:48 -05003275 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003276 if (nskb == NULL)
Jiri Benc79e1ad12017-11-02 17:04:38 -02003277 goto out;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003278
Jiri Benc79e1ad12017-11-02 17:04:38 -02003279 err = rtnl_fill_ifinfo(nskb, dev, net,
3280 RTM_NEWLINK, NETLINK_CB(skb).portid,
3281 nlh->nlmsg_seq, 0, 0, ext_filter_mask,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003282 0, NULL, 0, netnsid);
Patrick McHardy26932562007-01-31 23:16:40 -08003283 if (err < 0) {
3284 /* -EMSGSIZE implies BUG in if_nlmsg_size */
3285 WARN_ON(err == -EMSGSIZE);
3286 kfree_skb(nskb);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003287 } else
Eric W. Biederman15e47302012-09-07 20:12:54 +00003288 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
Jiri Benc79e1ad12017-11-02 17:04:38 -02003289out:
3290 if (netnsid >= 0)
3291 put_net(tgt_net);
Thomas Grafb60c5112006-08-04 23:05:34 -07003292
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003293 return err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003294}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003295
Greg Rose115c9b82012-02-21 16:54:48 -05003296static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
Greg Rosec7ac8672011-06-10 01:27:09 +00003297{
Greg Rose115c9b82012-02-21 16:54:48 -05003298 struct net *net = sock_net(skb->sk);
3299 struct net_device *dev;
3300 struct nlattr *tb[IFLA_MAX+1];
3301 u32 ext_filter_mask = 0;
3302 u16 min_ifinfo_dump_size = 0;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02003303 int hdrlen;
Greg Rose115c9b82012-02-21 16:54:48 -05003304
Michal Schmidte5eca6d2014-05-28 14:15:19 +02003305 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3306 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
3307 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
3308
Johannes Bergfceb6432017-04-12 14:34:07 +02003309 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00003310 if (tb[IFLA_EXT_MASK])
3311 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3312 }
Greg Rose115c9b82012-02-21 16:54:48 -05003313
3314 if (!ext_filter_mask)
3315 return NLMSG_GOODSIZE;
3316 /*
3317 * traverse the list of net devices and compute the minimum
3318 * buffer size based upon the filter mask.
3319 */
Florian Westphal6853dd42017-08-09 20:41:51 +02003320 rcu_read_lock();
3321 for_each_netdev_rcu(net, dev) {
Greg Rose115c9b82012-02-21 16:54:48 -05003322 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
3323 if_nlmsg_size(dev,
3324 ext_filter_mask));
3325 }
Florian Westphal6853dd42017-08-09 20:41:51 +02003326 rcu_read_unlock();
Greg Rose115c9b82012-02-21 16:54:48 -05003327
Zhang Shengju93af2052016-11-22 14:14:28 +08003328 return nlmsg_total_size(min_ifinfo_dump_size);
Greg Rosec7ac8672011-06-10 01:27:09 +00003329}
3330
Adrian Bunk42bad1d2007-04-26 00:57:41 -07003331static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332{
3333 int idx;
3334 int s_idx = cb->family;
Christian Brauner87ccbb12018-09-04 21:53:51 +02003335 int type = cb->nlh->nlmsg_type - RTM_BASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336
3337 if (s_idx == 0)
3338 s_idx = 1;
Florian Westphal6853dd42017-08-09 20:41:51 +02003339
Patrick McHardy25239ce2010-04-26 16:02:05 +02003340 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
Florian Westphaladdf9b92017-12-02 21:44:05 +01003341 struct rtnl_link **tab;
Florian Westphaladdf9b92017-12-02 21:44:05 +01003342 struct rtnl_link *link;
Florian Westphal6853dd42017-08-09 20:41:51 +02003343 rtnl_dumpit_func dumpit;
3344
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 if (idx < s_idx || idx == PF_PACKET)
3346 continue;
Florian Westphal6853dd42017-08-09 20:41:51 +02003347
Florian Westphaladdf9b92017-12-02 21:44:05 +01003348 if (type < 0 || type >= RTM_NR_MSGTYPES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 continue;
Florian Westphal6853dd42017-08-09 20:41:51 +02003350
Florian Westphaladdf9b92017-12-02 21:44:05 +01003351 tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]);
3352 if (!tab)
3353 continue;
3354
3355 link = tab[type];
3356 if (!link)
3357 continue;
3358
3359 dumpit = link->dumpit;
Florian Westphal6853dd42017-08-09 20:41:51 +02003360 if (!dumpit)
3361 continue;
3362
Nicolas Dichtel04652772013-03-22 06:28:42 +00003363 if (idx > s_idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 memset(&cb->args[0], 0, sizeof(cb->args));
Nicolas Dichtel04652772013-03-22 06:28:42 +00003365 cb->prev_seq = 0;
3366 cb->seq = 0;
3367 }
Florian Westphal6853dd42017-08-09 20:41:51 +02003368 if (dumpit(skb, cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 break;
3370 }
3371 cb->family = idx;
3372
3373 return skb->len;
3374}
3375
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003376struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003377 unsigned int change,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003378 u32 event, gfp_t flags, int *new_nsid,
3379 int new_ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003381 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 struct sk_buff *skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003383 int err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00003384 size_t if_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385
Alexei Starovoitov7f294052013-10-23 16:02:42 -07003386 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003387 if (skb == NULL)
3388 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389
Jiri Benc79e1ad12017-11-02 17:04:38 -02003390 err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
3391 type, 0, 0, change, 0, 0, event,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003392 new_nsid, new_ifindex, -1);
Patrick McHardy26932562007-01-31 23:16:40 -08003393 if (err < 0) {
3394 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
3395 WARN_ON(err == -EMSGSIZE);
3396 kfree_skb(skb);
3397 goto errout;
3398 }
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003399 return skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003400errout:
3401 if (err < 0)
Eric W. Biederman4b3da702007-11-19 22:27:40 -08003402 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003403 return NULL;
3404}
3405
3406void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
3407{
3408 struct net *net = dev_net(dev);
3409
3410 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
3411}
3412
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003413static void rtmsg_ifinfo_event(int type, struct net_device *dev,
3414 unsigned int change, u32 event,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003415 gfp_t flags, int *new_nsid, int new_ifindex)
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003416{
3417 struct sk_buff *skb;
3418
Nicolas Dichteled2a80a2015-05-13 14:19:42 +02003419 if (dev->reg_state != NETREG_REGISTERED)
3420 return;
3421
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003422 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid,
3423 new_ifindex);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003424 if (skb)
3425 rtmsg_ifinfo_send(skb, dev, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426}
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003427
3428void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
3429 gfp_t flags)
3430{
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003431 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3432 NULL, 0);
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003433}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003435void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003436 gfp_t flags, int *new_nsid, int new_ifindex)
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003437{
3438 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003439 new_nsid, new_ifindex);
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003440}
3441
John Fastabendd83b0602012-04-15 06:44:08 +00003442static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
3443 struct net_device *dev,
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003444 u8 *addr, u16 vid, u32 pid, u32 seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003445 int type, unsigned int flags,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003446 int nlflags, u16 ndm_state)
John Fastabendd83b0602012-04-15 06:44:08 +00003447{
3448 struct nlmsghdr *nlh;
3449 struct ndmsg *ndm;
3450
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003451 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
John Fastabendd83b0602012-04-15 06:44:08 +00003452 if (!nlh)
3453 return -EMSGSIZE;
3454
3455 ndm = nlmsg_data(nlh);
3456 ndm->ndm_family = AF_BRIDGE;
3457 ndm->ndm_pad1 = 0;
3458 ndm->ndm_pad2 = 0;
3459 ndm->ndm_flags = flags;
3460 ndm->ndm_type = 0;
3461 ndm->ndm_ifindex = dev->ifindex;
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003462 ndm->ndm_state = ndm_state;
John Fastabendd83b0602012-04-15 06:44:08 +00003463
3464 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
3465 goto nla_put_failure;
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003466 if (vid)
3467 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
3468 goto nla_put_failure;
John Fastabendd83b0602012-04-15 06:44:08 +00003469
Johannes Berg053c0952015-01-16 22:09:00 +01003470 nlmsg_end(skb, nlh);
3471 return 0;
John Fastabendd83b0602012-04-15 06:44:08 +00003472
3473nla_put_failure:
3474 nlmsg_cancel(skb, nlh);
3475 return -EMSGSIZE;
3476}
3477
John Fastabend3ff661c2012-04-15 06:44:14 +00003478static inline size_t rtnl_fdb_nlmsg_size(void)
3479{
Sabrina Dubrocaf82ef3e2016-11-18 15:50:39 +01003480 return NLMSG_ALIGN(sizeof(struct ndmsg)) +
3481 nla_total_size(ETH_ALEN) + /* NDA_LLADDR */
3482 nla_total_size(sizeof(u16)) + /* NDA_VLAN */
3483 0;
John Fastabend3ff661c2012-04-15 06:44:14 +00003484}
3485
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003486static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
3487 u16 ndm_state)
John Fastabend3ff661c2012-04-15 06:44:14 +00003488{
3489 struct net *net = dev_net(dev);
3490 struct sk_buff *skb;
3491 int err = -ENOBUFS;
3492
3493 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
3494 if (!skb)
3495 goto errout;
3496
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003497 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003498 0, 0, type, NTF_SELF, 0, ndm_state);
John Fastabend3ff661c2012-04-15 06:44:14 +00003499 if (err < 0) {
3500 kfree_skb(skb);
3501 goto errout;
3502 }
3503
3504 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
3505 return;
3506errout:
3507 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
3508}
3509
Vlad Yasevich090096b2013-03-06 15:39:42 +00003510/**
3511 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3512 */
3513int ndo_dflt_fdb_add(struct ndmsg *ndm,
3514 struct nlattr *tb[],
3515 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003516 const unsigned char *addr, u16 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003517 u16 flags)
3518{
3519 int err = -EINVAL;
3520
3521 /* If aging addresses are supported device will need to
3522 * implement its own handler for this.
3523 */
3524 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
3525 pr_info("%s: FDB only supports static addresses\n", dev->name);
3526 return err;
3527 }
3528
Or Gerlitz65891fe2014-12-14 18:19:05 +02003529 if (vid) {
3530 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
3531 return err;
3532 }
3533
Vlad Yasevich090096b2013-03-06 15:39:42 +00003534 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3535 err = dev_uc_add_excl(dev, addr);
3536 else if (is_multicast_ether_addr(addr))
3537 err = dev_mc_add_excl(dev, addr);
3538
3539 /* Only return duplicate errors if NLM_F_EXCL is set */
3540 if (err == -EEXIST && !(flags & NLM_F_EXCL))
3541 err = 0;
3542
3543 return err;
3544}
3545EXPORT_SYMBOL(ndo_dflt_fdb_add);
3546
Florian Westphalb88d12e2017-10-10 17:10:04 +02003547static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
3548 struct netlink_ext_ack *extack)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003549{
3550 u16 vid = 0;
3551
3552 if (vlan_attr) {
3553 if (nla_len(vlan_attr) != sizeof(u16)) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003554 NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
Jiri Pirkof6f64242014-11-28 14:34:15 +01003555 return -EINVAL;
3556 }
3557
3558 vid = nla_get_u16(vlan_attr);
3559
3560 if (!vid || vid >= VLAN_VID_MASK) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003561 NL_SET_ERR_MSG(extack, "invalid vlan id");
Jiri Pirkof6f64242014-11-28 14:34:15 +01003562 return -EINVAL;
3563 }
3564 }
3565 *p_vid = vid;
3566 return 0;
3567}
3568
David Ahernc21ef3e2017-04-16 09:48:24 -07003569static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
3570 struct netlink_ext_ack *extack)
John Fastabend77162022012-04-15 06:43:56 +00003571{
3572 struct net *net = sock_net(skb->sk);
John Fastabend77162022012-04-15 06:43:56 +00003573 struct ndmsg *ndm;
3574 struct nlattr *tb[NDA_MAX+1];
3575 struct net_device *dev;
3576 u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01003577 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00003578 int err;
3579
David Ahernc21ef3e2017-04-16 09:48:24 -07003580 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
John Fastabend77162022012-04-15 06:43:56 +00003581 if (err < 0)
3582 return err;
3583
3584 ndm = nlmsg_data(nlh);
3585 if (ndm->ndm_ifindex == 0) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003586 NL_SET_ERR_MSG(extack, "invalid ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003587 return -EINVAL;
3588 }
3589
3590 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3591 if (dev == NULL) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003592 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003593 return -ENODEV;
3594 }
3595
3596 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003597 NL_SET_ERR_MSG(extack, "invalid address");
John Fastabend77162022012-04-15 06:43:56 +00003598 return -EINVAL;
3599 }
3600
3601 addr = nla_data(tb[NDA_LLADDR]);
John Fastabend77162022012-04-15 06:43:56 +00003602
Florian Westphalb88d12e2017-10-10 17:10:04 +02003603 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
Jiri Pirkof6f64242014-11-28 14:34:15 +01003604 if (err)
3605 return err;
3606
John Fastabend77162022012-04-15 06:43:56 +00003607 err = -EOPNOTSUPP;
3608
3609 /* Support fdb on master device the net/bridge default case */
3610 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3611 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003612 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3613 const struct net_device_ops *ops = br_dev->netdev_ops;
3614
Jiri Pirkof6f64242014-11-28 14:34:15 +01003615 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
3616 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00003617 if (err)
3618 goto out;
3619 else
3620 ndm->ndm_flags &= ~NTF_MASTER;
3621 }
3622
3623 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00003624 if ((ndm->ndm_flags & NTF_SELF)) {
3625 if (dev->netdev_ops->ndo_fdb_add)
3626 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003627 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003628 nlh->nlmsg_flags);
3629 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01003630 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003631 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00003632
John Fastabend3ff661c2012-04-15 06:44:14 +00003633 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003634 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
3635 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00003636 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00003637 }
John Fastabend77162022012-04-15 06:43:56 +00003638 }
3639out:
3640 return err;
3641}
3642
Vlad Yasevich090096b2013-03-06 15:39:42 +00003643/**
3644 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3645 */
3646int ndo_dflt_fdb_del(struct ndmsg *ndm,
3647 struct nlattr *tb[],
3648 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003649 const unsigned char *addr, u16 vid)
Vlad Yasevich090096b2013-03-06 15:39:42 +00003650{
Alexander Duyckc8a89c42014-07-15 15:15:20 -07003651 int err = -EINVAL;
Vlad Yasevich090096b2013-03-06 15:39:42 +00003652
3653 /* If aging addresses are supported device will need to
3654 * implement its own handler for this.
3655 */
Sridhar Samudrala64535992013-08-08 15:19:48 -07003656 if (!(ndm->ndm_state & NUD_PERMANENT)) {
Vlad Yasevich090096b2013-03-06 15:39:42 +00003657 pr_info("%s: FDB only supports static addresses\n", dev->name);
Alexander Duyckc8a89c42014-07-15 15:15:20 -07003658 return err;
Vlad Yasevich090096b2013-03-06 15:39:42 +00003659 }
3660
3661 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3662 err = dev_uc_del(dev, addr);
3663 else if (is_multicast_ether_addr(addr))
3664 err = dev_mc_del(dev, addr);
Vlad Yasevich090096b2013-03-06 15:39:42 +00003665
3666 return err;
3667}
3668EXPORT_SYMBOL(ndo_dflt_fdb_del);
3669
David Ahernc21ef3e2017-04-16 09:48:24 -07003670static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
3671 struct netlink_ext_ack *extack)
John Fastabend77162022012-04-15 06:43:56 +00003672{
3673 struct net *net = sock_net(skb->sk);
3674 struct ndmsg *ndm;
Vlad Yasevich1690be62013-02-13 12:00:18 +00003675 struct nlattr *tb[NDA_MAX+1];
John Fastabend77162022012-04-15 06:43:56 +00003676 struct net_device *dev;
3677 int err = -EINVAL;
3678 __u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01003679 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00003680
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07003681 if (!netlink_capable(skb, CAP_NET_ADMIN))
Vlad Yasevich1690be62013-02-13 12:00:18 +00003682 return -EPERM;
3683
David Ahernc21ef3e2017-04-16 09:48:24 -07003684 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
Vlad Yasevich1690be62013-02-13 12:00:18 +00003685 if (err < 0)
3686 return err;
John Fastabend77162022012-04-15 06:43:56 +00003687
3688 ndm = nlmsg_data(nlh);
3689 if (ndm->ndm_ifindex == 0) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003690 NL_SET_ERR_MSG(extack, "invalid ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003691 return -EINVAL;
3692 }
3693
3694 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3695 if (dev == NULL) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003696 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003697 return -ENODEV;
3698 }
3699
Vlad Yasevich1690be62013-02-13 12:00:18 +00003700 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003701 NL_SET_ERR_MSG(extack, "invalid address");
John Fastabend77162022012-04-15 06:43:56 +00003702 return -EINVAL;
3703 }
3704
Vlad Yasevich1690be62013-02-13 12:00:18 +00003705 addr = nla_data(tb[NDA_LLADDR]);
Vlad Yasevich1690be62013-02-13 12:00:18 +00003706
Florian Westphalb88d12e2017-10-10 17:10:04 +02003707 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
Jiri Pirkof6f64242014-11-28 14:34:15 +01003708 if (err)
3709 return err;
3710
John Fastabend77162022012-04-15 06:43:56 +00003711 err = -EOPNOTSUPP;
3712
3713 /* Support fdb on master device the net/bridge default case */
3714 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3715 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003716 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3717 const struct net_device_ops *ops = br_dev->netdev_ops;
John Fastabend77162022012-04-15 06:43:56 +00003718
Jiri Pirko898e5062013-01-03 22:48:52 +00003719 if (ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003720 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00003721
3722 if (err)
3723 goto out;
3724 else
3725 ndm->ndm_flags &= ~NTF_MASTER;
3726 }
3727
3728 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00003729 if (ndm->ndm_flags & NTF_SELF) {
3730 if (dev->netdev_ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003731 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
3732 vid);
Vlad Yasevich090096b2013-03-06 15:39:42 +00003733 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01003734 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00003735
John Fastabend3ff661c2012-04-15 06:44:14 +00003736 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003737 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
3738 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00003739 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00003740 }
John Fastabend77162022012-04-15 06:43:56 +00003741 }
3742out:
3743 return err;
3744}
3745
John Fastabendd83b0602012-04-15 06:44:08 +00003746static int nlmsg_populate_fdb(struct sk_buff *skb,
3747 struct netlink_callback *cb,
3748 struct net_device *dev,
3749 int *idx,
3750 struct netdev_hw_addr_list *list)
3751{
3752 struct netdev_hw_addr *ha;
3753 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00003754 u32 portid, seq;
John Fastabendd83b0602012-04-15 06:44:08 +00003755
Eric W. Biederman15e47302012-09-07 20:12:54 +00003756 portid = NETLINK_CB(cb->skb).portid;
John Fastabendd83b0602012-04-15 06:44:08 +00003757 seq = cb->nlh->nlmsg_seq;
3758
3759 list_for_each_entry(ha, &list->list, list) {
Roopa Prabhud2976532016-08-30 21:56:45 -07003760 if (*idx < cb->args[2])
John Fastabendd83b0602012-04-15 06:44:08 +00003761 goto skip;
3762
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003763 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
John Fastabenda7a558f2012-11-01 16:23:10 +00003764 portid, seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003765 RTM_NEWNEIGH, NTF_SELF,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003766 NLM_F_MULTI, NUD_PERMANENT);
John Fastabendd83b0602012-04-15 06:44:08 +00003767 if (err < 0)
3768 return err;
3769skip:
3770 *idx += 1;
3771 }
3772 return 0;
3773}
3774
3775/**
Ben Hutchings2c530402012-07-10 10:55:09 +00003776 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
John Fastabendd83b0602012-04-15 06:44:08 +00003777 * @nlh: netlink message header
3778 * @dev: netdevice
3779 *
3780 * Default netdevice operation to dump the existing unicast address list.
John Fastabend91f3e7b2013-03-29 08:18:37 +00003781 * Returns number of addresses from list put in skb.
John Fastabendd83b0602012-04-15 06:44:08 +00003782 */
3783int ndo_dflt_fdb_dump(struct sk_buff *skb,
3784 struct netlink_callback *cb,
3785 struct net_device *dev,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04003786 struct net_device *filter_dev,
Roopa Prabhud2976532016-08-30 21:56:45 -07003787 int *idx)
John Fastabendd83b0602012-04-15 06:44:08 +00003788{
3789 int err;
3790
3791 netif_addr_lock_bh(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -07003792 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
John Fastabendd83b0602012-04-15 06:44:08 +00003793 if (err)
3794 goto out;
Zhang Shengju2934c9d2016-11-30 16:37:34 +08003795 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
John Fastabendd83b0602012-04-15 06:44:08 +00003796out:
3797 netif_addr_unlock_bh(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -07003798 return err;
John Fastabendd83b0602012-04-15 06:44:08 +00003799}
3800EXPORT_SYMBOL(ndo_dflt_fdb_dump);
3801
John Fastabend77162022012-04-15 06:43:56 +00003802static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
3803{
John Fastabend77162022012-04-15 06:43:56 +00003804 struct net_device *dev;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003805 struct nlattr *tb[IFLA_MAX+1];
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003806 struct net_device *br_dev = NULL;
3807 const struct net_device_ops *ops = NULL;
3808 const struct net_device_ops *cops = NULL;
3809 struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
3810 struct net *net = sock_net(skb->sk);
Roopa Prabhud2976532016-08-30 21:56:45 -07003811 struct hlist_head *head;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003812 int brport_idx = 0;
3813 int br_idx = 0;
Roopa Prabhud2976532016-08-30 21:56:45 -07003814 int h, s_h;
3815 int idx = 0, s_idx;
3816 int err = 0;
3817 int fidx = 0;
John Fastabend77162022012-04-15 06:43:56 +00003818
Mauricio Faria de Oliveirabd961c92018-10-01 22:46:40 -03003819 /* A hack to preserve kernel<->userspace interface.
3820 * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0.
3821 * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails.
3822 * So, check for ndmsg with an optional u32 attribute (not used here).
3823 * Fortunately these sizes don't conflict with the size of ifinfomsg
3824 * with an optional attribute.
3825 */
3826 if (nlmsg_len(cb->nlh) != sizeof(struct ndmsg) &&
3827 (nlmsg_len(cb->nlh) != sizeof(struct ndmsg) +
3828 nla_attr_size(sizeof(u32)))) {
3829 err = nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb,
David Aherndac9c972018-10-07 20:16:24 -07003830 IFLA_MAX, ifla_policy, cb->extack);
Mauricio Faria de Oliveirabd961c92018-10-01 22:46:40 -03003831 if (err < 0) {
3832 return -EINVAL;
3833 } else if (err == 0) {
3834 if (tb[IFLA_MASTER])
3835 br_idx = nla_get_u32(tb[IFLA_MASTER]);
3836 }
John Fastabend77162022012-04-15 06:43:56 +00003837
Mauricio Faria de Oliveirabd961c92018-10-01 22:46:40 -03003838 brport_idx = ifm->ifi_index;
3839 }
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003840
3841 if (br_idx) {
3842 br_dev = __dev_get_by_index(net, br_idx);
3843 if (!br_dev)
3844 return -ENODEV;
3845
3846 ops = br_dev->netdev_ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003847 }
3848
Roopa Prabhud2976532016-08-30 21:56:45 -07003849 s_h = cb->args[0];
3850 s_idx = cb->args[1];
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003851
Roopa Prabhud2976532016-08-30 21:56:45 -07003852 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
3853 idx = 0;
3854 head = &net->dev_index_head[h];
3855 hlist_for_each_entry(dev, head, index_hlist) {
3856
3857 if (brport_idx && (dev->ifindex != brport_idx))
3858 continue;
3859
3860 if (!br_idx) { /* user did not specify a specific bridge */
3861 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3862 br_dev = netdev_master_upper_dev_get(dev);
3863 cops = br_dev->netdev_ops;
3864 }
3865 } else {
3866 if (dev != br_dev &&
3867 !(dev->priv_flags & IFF_BRIDGE_PORT))
3868 continue;
3869
3870 if (br_dev != netdev_master_upper_dev_get(dev) &&
3871 !(dev->priv_flags & IFF_EBRIDGE))
3872 continue;
3873 cops = ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003874 }
3875
Roopa Prabhud2976532016-08-30 21:56:45 -07003876 if (idx < s_idx)
3877 goto cont;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003878
Roopa Prabhud2976532016-08-30 21:56:45 -07003879 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3880 if (cops && cops->ndo_fdb_dump) {
3881 err = cops->ndo_fdb_dump(skb, cb,
3882 br_dev, dev,
3883 &fidx);
3884 if (err == -EMSGSIZE)
3885 goto out;
3886 }
3887 }
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003888
Roopa Prabhud2976532016-08-30 21:56:45 -07003889 if (dev->netdev_ops->ndo_fdb_dump)
3890 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
3891 dev, NULL,
3892 &fidx);
3893 else
3894 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
3895 &fidx);
3896 if (err == -EMSGSIZE)
3897 goto out;
3898
3899 cops = NULL;
3900
3901 /* reset fdb offset to 0 for rest of the interfaces */
3902 cb->args[2] = 0;
3903 fidx = 0;
3904cont:
3905 idx++;
John Fastabend77162022012-04-15 06:43:56 +00003906 }
John Fastabend77162022012-04-15 06:43:56 +00003907 }
John Fastabend77162022012-04-15 06:43:56 +00003908
Roopa Prabhud2976532016-08-30 21:56:45 -07003909out:
3910 cb->args[0] = h;
3911 cb->args[1] = idx;
3912 cb->args[2] = fidx;
3913
John Fastabend77162022012-04-15 06:43:56 +00003914 return skb->len;
3915}
3916
Scott Feldman2c3c0312014-11-28 14:34:25 +01003917static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
3918 unsigned int attrnum, unsigned int flag)
3919{
3920 if (mask & flag)
3921 return nla_put_u8(skb, attrnum, !!(flags & flag));
3922 return 0;
3923}
3924
John Fastabend815cccb2012-10-24 08:13:09 +00003925int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Scott Feldman2c3c0312014-11-28 14:34:25 +01003926 struct net_device *dev, u16 mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003927 u32 flags, u32 mask, int nlflags,
3928 u32 filter_mask,
3929 int (*vlan_fill)(struct sk_buff *skb,
3930 struct net_device *dev,
3931 u32 filter_mask))
John Fastabend815cccb2012-10-24 08:13:09 +00003932{
3933 struct nlmsghdr *nlh;
3934 struct ifinfomsg *ifm;
3935 struct nlattr *br_afspec;
Scott Feldman2c3c0312014-11-28 14:34:25 +01003936 struct nlattr *protinfo;
John Fastabend815cccb2012-10-24 08:13:09 +00003937 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Jiri Pirko898e5062013-01-03 22:48:52 +00003938 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003939 int err = 0;
John Fastabend815cccb2012-10-24 08:13:09 +00003940
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02003941 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
John Fastabend815cccb2012-10-24 08:13:09 +00003942 if (nlh == NULL)
3943 return -EMSGSIZE;
3944
3945 ifm = nlmsg_data(nlh);
3946 ifm->ifi_family = AF_BRIDGE;
3947 ifm->__ifi_pad = 0;
3948 ifm->ifi_type = dev->type;
3949 ifm->ifi_index = dev->ifindex;
3950 ifm->ifi_flags = dev_get_flags(dev);
3951 ifm->ifi_change = 0;
3952
3953
3954 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
3955 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
3956 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00003957 (br_dev &&
3958 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
John Fastabend815cccb2012-10-24 08:13:09 +00003959 (dev->addr_len &&
3960 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02003961 (dev->ifindex != dev_get_iflink(dev) &&
3962 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
John Fastabend815cccb2012-10-24 08:13:09 +00003963 goto nla_put_failure;
3964
3965 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
3966 if (!br_afspec)
3967 goto nla_put_failure;
3968
Roopa Prabhu1d460b92014-12-08 14:04:20 -08003969 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
John Fastabend815cccb2012-10-24 08:13:09 +00003970 nla_nest_cancel(skb, br_afspec);
3971 goto nla_put_failure;
3972 }
Roopa Prabhu1d460b92014-12-08 14:04:20 -08003973
3974 if (mode != BRIDGE_MODE_UNDEF) {
3975 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
3976 nla_nest_cancel(skb, br_afspec);
3977 goto nla_put_failure;
3978 }
3979 }
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003980 if (vlan_fill) {
3981 err = vlan_fill(skb, dev, filter_mask);
3982 if (err) {
3983 nla_nest_cancel(skb, br_afspec);
3984 goto nla_put_failure;
3985 }
3986 }
John Fastabend815cccb2012-10-24 08:13:09 +00003987 nla_nest_end(skb, br_afspec);
3988
Scott Feldman2c3c0312014-11-28 14:34:25 +01003989 protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
3990 if (!protinfo)
3991 goto nla_put_failure;
3992
3993 if (brport_nla_put_flag(skb, flags, mask,
3994 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
3995 brport_nla_put_flag(skb, flags, mask,
3996 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
3997 brport_nla_put_flag(skb, flags, mask,
3998 IFLA_BRPORT_FAST_LEAVE,
3999 BR_MULTICAST_FAST_LEAVE) ||
4000 brport_nla_put_flag(skb, flags, mask,
4001 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
4002 brport_nla_put_flag(skb, flags, mask,
4003 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
4004 brport_nla_put_flag(skb, flags, mask,
4005 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
4006 brport_nla_put_flag(skb, flags, mask,
4007 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
4008 brport_nla_put_flag(skb, flags, mask,
4009 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
4010 nla_nest_cancel(skb, protinfo);
4011 goto nla_put_failure;
4012 }
4013
4014 nla_nest_end(skb, protinfo);
4015
Johannes Berg053c0952015-01-16 22:09:00 +01004016 nlmsg_end(skb, nlh);
4017 return 0;
John Fastabend815cccb2012-10-24 08:13:09 +00004018nla_put_failure:
4019 nlmsg_cancel(skb, nlh);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07004020 return err ? err : -EMSGSIZE;
John Fastabend815cccb2012-10-24 08:13:09 +00004021}
Scott Feldman7d4f8d82015-06-22 00:27:17 -07004022EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
John Fastabend815cccb2012-10-24 08:13:09 +00004023
David Ahern2d011be2018-10-07 20:16:31 -07004024static int valid_bridge_getlink_req(const struct nlmsghdr *nlh,
4025 bool strict_check, u32 *filter_mask,
4026 struct netlink_ext_ack *extack)
4027{
4028 struct nlattr *tb[IFLA_MAX+1];
4029 int err, i;
4030
4031 if (strict_check) {
4032 struct ifinfomsg *ifm;
4033
4034 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
4035 NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump");
4036 return -EINVAL;
4037 }
4038
4039 ifm = nlmsg_data(nlh);
4040 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
4041 ifm->ifi_change || ifm->ifi_index) {
4042 NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request");
4043 return -EINVAL;
4044 }
4045
4046 err = nlmsg_parse_strict(nlh, sizeof(struct ifinfomsg), tb,
4047 IFLA_MAX, ifla_policy, extack);
4048 } else {
4049 err = nlmsg_parse(nlh, sizeof(struct ifinfomsg), tb,
4050 IFLA_MAX, ifla_policy, extack);
4051 }
4052 if (err < 0)
4053 return err;
4054
4055 /* new attributes should only be added with strict checking */
4056 for (i = 0; i <= IFLA_MAX; ++i) {
4057 if (!tb[i])
4058 continue;
4059
4060 switch (i) {
4061 case IFLA_EXT_MASK:
4062 *filter_mask = nla_get_u32(tb[i]);
4063 break;
4064 default:
4065 if (strict_check) {
4066 NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request");
4067 return -EINVAL;
4068 }
4069 }
4070 }
4071
4072 return 0;
4073}
4074
John Fastabende5a55a82012-10-24 08:12:57 +00004075static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
4076{
David Ahern2d011be2018-10-07 20:16:31 -07004077 const struct nlmsghdr *nlh = cb->nlh;
John Fastabende5a55a82012-10-24 08:12:57 +00004078 struct net *net = sock_net(skb->sk);
4079 struct net_device *dev;
4080 int idx = 0;
4081 u32 portid = NETLINK_CB(cb->skb).portid;
David Ahern2d011be2018-10-07 20:16:31 -07004082 u32 seq = nlh->nlmsg_seq;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00004083 u32 filter_mask = 0;
Roopa Prabhud64f69b2015-09-15 14:44:29 -07004084 int err;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00004085
David Ahern2d011be2018-10-07 20:16:31 -07004086 err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask,
4087 cb->extack);
4088 if (err < 0 && cb->strict_check)
4089 return err;
John Fastabende5a55a82012-10-24 08:12:57 +00004090
4091 rcu_read_lock();
4092 for_each_netdev_rcu(net, dev) {
4093 const struct net_device_ops *ops = dev->netdev_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00004094 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
John Fastabende5a55a82012-10-24 08:12:57 +00004095
Jiri Pirko898e5062013-01-03 22:48:52 +00004096 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07004097 if (idx >= cb->args[0]) {
4098 err = br_dev->netdev_ops->ndo_bridge_getlink(
4099 skb, portid, seq, dev,
4100 filter_mask, NLM_F_MULTI);
David Ahernf6c57752017-05-15 23:19:17 -07004101 if (err < 0 && err != -EOPNOTSUPP) {
4102 if (likely(skb->len))
4103 break;
4104
4105 goto out_err;
4106 }
Roopa Prabhud64f69b2015-09-15 14:44:29 -07004107 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00004108 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00004109 }
4110
4111 if (ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07004112 if (idx >= cb->args[0]) {
4113 err = ops->ndo_bridge_getlink(skb, portid,
4114 seq, dev,
4115 filter_mask,
4116 NLM_F_MULTI);
David Ahernf6c57752017-05-15 23:19:17 -07004117 if (err < 0 && err != -EOPNOTSUPP) {
4118 if (likely(skb->len))
4119 break;
4120
4121 goto out_err;
4122 }
Roopa Prabhud64f69b2015-09-15 14:44:29 -07004123 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00004124 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00004125 }
4126 }
David Ahernf6c57752017-05-15 23:19:17 -07004127 err = skb->len;
4128out_err:
John Fastabende5a55a82012-10-24 08:12:57 +00004129 rcu_read_unlock();
4130 cb->args[0] = idx;
4131
David Ahernf6c57752017-05-15 23:19:17 -07004132 return err;
John Fastabende5a55a82012-10-24 08:12:57 +00004133}
4134
John Fastabend2469ffd2012-10-24 08:13:03 +00004135static inline size_t bridge_nlmsg_size(void)
4136{
4137 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4138 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4139 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4140 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
4141 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
4142 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
4143 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
4144 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
4145 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
4146 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
4147 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
4148}
4149
Roopa Prabhu02dba432015-01-14 20:02:25 -08004150static int rtnl_bridge_notify(struct net_device *dev)
John Fastabend2469ffd2012-10-24 08:13:03 +00004151{
4152 struct net *net = dev_net(dev);
John Fastabend2469ffd2012-10-24 08:13:03 +00004153 struct sk_buff *skb;
4154 int err = -EOPNOTSUPP;
4155
Roopa Prabhu02dba432015-01-14 20:02:25 -08004156 if (!dev->netdev_ops->ndo_bridge_getlink)
4157 return 0;
4158
John Fastabend2469ffd2012-10-24 08:13:03 +00004159 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
4160 if (!skb) {
4161 err = -ENOMEM;
4162 goto errout;
4163 }
4164
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02004165 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
Roopa Prabhu02dba432015-01-14 20:02:25 -08004166 if (err < 0)
4167 goto errout;
John Fastabend2469ffd2012-10-24 08:13:03 +00004168
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08004169 if (!skb->len)
4170 goto errout;
4171
John Fastabend2469ffd2012-10-24 08:13:03 +00004172 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
4173 return 0;
4174errout:
4175 WARN_ON(err == -EMSGSIZE);
4176 kfree_skb(skb);
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08004177 if (err)
4178 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
John Fastabend2469ffd2012-10-24 08:13:03 +00004179 return err;
4180}
4181
David Ahernc21ef3e2017-04-16 09:48:24 -07004182static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
4183 struct netlink_ext_ack *extack)
John Fastabende5a55a82012-10-24 08:12:57 +00004184{
4185 struct net *net = sock_net(skb->sk);
4186 struct ifinfomsg *ifm;
4187 struct net_device *dev;
John Fastabend2469ffd2012-10-24 08:13:03 +00004188 struct nlattr *br_spec, *attr = NULL;
4189 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02004190 u16 flags = 0;
John Fastabendc38e01b2012-11-02 16:32:36 +00004191 bool have_flags = false;
John Fastabende5a55a82012-10-24 08:12:57 +00004192
4193 if (nlmsg_len(nlh) < sizeof(*ifm))
4194 return -EINVAL;
4195
4196 ifm = nlmsg_data(nlh);
4197 if (ifm->ifi_family != AF_BRIDGE)
4198 return -EPFNOSUPPORT;
4199
4200 dev = __dev_get_by_index(net, ifm->ifi_index);
4201 if (!dev) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004202 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabende5a55a82012-10-24 08:12:57 +00004203 return -ENODEV;
4204 }
4205
John Fastabend2469ffd2012-10-24 08:13:03 +00004206 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4207 if (br_spec) {
4208 nla_for_each_nested(attr, br_spec, rem) {
4209 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01004210 if (nla_len(attr) < sizeof(flags))
4211 return -EINVAL;
4212
John Fastabendc38e01b2012-11-02 16:32:36 +00004213 have_flags = true;
John Fastabend2469ffd2012-10-24 08:13:03 +00004214 flags = nla_get_u16(attr);
4215 break;
4216 }
4217 }
4218 }
4219
4220 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00004221 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4222
4223 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
John Fastabend2469ffd2012-10-24 08:13:03 +00004224 err = -EOPNOTSUPP;
4225 goto out;
4226 }
4227
Roopa Prabhuadd511b2015-01-29 22:40:12 -08004228 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
John Fastabende5a55a82012-10-24 08:12:57 +00004229 if (err)
4230 goto out;
John Fastabend2469ffd2012-10-24 08:13:03 +00004231
4232 flags &= ~BRIDGE_FLAGS_MASTER;
John Fastabende5a55a82012-10-24 08:12:57 +00004233 }
4234
John Fastabend2469ffd2012-10-24 08:13:03 +00004235 if ((flags & BRIDGE_FLAGS_SELF)) {
4236 if (!dev->netdev_ops->ndo_bridge_setlink)
4237 err = -EOPNOTSUPP;
4238 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08004239 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
4240 flags);
Roopa Prabhu02dba432015-01-14 20:02:25 -08004241 if (!err) {
John Fastabend2469ffd2012-10-24 08:13:03 +00004242 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08004243
4244 /* Generate event to notify upper layer of bridge
4245 * change
4246 */
4247 err = rtnl_bridge_notify(dev);
4248 }
John Fastabend2469ffd2012-10-24 08:13:03 +00004249 }
4250
John Fastabendc38e01b2012-11-02 16:32:36 +00004251 if (have_flags)
John Fastabend2469ffd2012-10-24 08:13:03 +00004252 memcpy(nla_data(attr), &flags, sizeof(flags));
John Fastabende5a55a82012-10-24 08:12:57 +00004253out:
4254 return err;
4255}
4256
David Ahernc21ef3e2017-04-16 09:48:24 -07004257static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
4258 struct netlink_ext_ack *extack)
Vlad Yasevich407af322013-02-13 12:00:12 +00004259{
4260 struct net *net = sock_net(skb->sk);
4261 struct ifinfomsg *ifm;
4262 struct net_device *dev;
4263 struct nlattr *br_spec, *attr = NULL;
4264 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02004265 u16 flags = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +00004266 bool have_flags = false;
4267
4268 if (nlmsg_len(nlh) < sizeof(*ifm))
4269 return -EINVAL;
4270
4271 ifm = nlmsg_data(nlh);
4272 if (ifm->ifi_family != AF_BRIDGE)
4273 return -EPFNOSUPPORT;
4274
4275 dev = __dev_get_by_index(net, ifm->ifi_index);
4276 if (!dev) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004277 NL_SET_ERR_MSG(extack, "unknown ifindex");
Vlad Yasevich407af322013-02-13 12:00:12 +00004278 return -ENODEV;
4279 }
4280
4281 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4282 if (br_spec) {
4283 nla_for_each_nested(attr, br_spec, rem) {
4284 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01004285 if (nla_len(attr) < sizeof(flags))
4286 return -EINVAL;
4287
Vlad Yasevich407af322013-02-13 12:00:12 +00004288 have_flags = true;
4289 flags = nla_get_u16(attr);
4290 break;
4291 }
4292 }
4293 }
4294
Vlad Yasevich407af322013-02-13 12:00:12 +00004295 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
4296 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4297
4298 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
4299 err = -EOPNOTSUPP;
4300 goto out;
4301 }
4302
Roopa Prabhuadd511b2015-01-29 22:40:12 -08004303 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00004304 if (err)
4305 goto out;
4306
4307 flags &= ~BRIDGE_FLAGS_MASTER;
4308 }
4309
4310 if ((flags & BRIDGE_FLAGS_SELF)) {
4311 if (!dev->netdev_ops->ndo_bridge_dellink)
4312 err = -EOPNOTSUPP;
4313 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08004314 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
4315 flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00004316
Roopa Prabhu02dba432015-01-14 20:02:25 -08004317 if (!err) {
Vlad Yasevich407af322013-02-13 12:00:12 +00004318 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08004319
4320 /* Generate event to notify upper layer of bridge
4321 * change
4322 */
4323 err = rtnl_bridge_notify(dev);
4324 }
Vlad Yasevich407af322013-02-13 12:00:12 +00004325 }
4326
4327 if (have_flags)
4328 memcpy(nla_data(attr), &flags, sizeof(flags));
Vlad Yasevich407af322013-02-13 12:00:12 +00004329out:
4330 return err;
4331}
4332
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004333static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
4334{
4335 return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
4336 (!idxattr || idxattr == attrid);
4337}
4338
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004339#define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
4340static int rtnl_get_offload_stats_attr_size(int attr_id)
4341{
4342 switch (attr_id) {
4343 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
4344 return sizeof(struct rtnl_link_stats64);
4345 }
4346
4347 return 0;
4348}
4349
4350static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
4351 int *prividx)
4352{
4353 struct nlattr *attr = NULL;
4354 int attr_id, size;
4355 void *attr_data;
4356 int err;
4357
4358 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
4359 dev->netdev_ops->ndo_get_offload_stats))
4360 return -ENODATA;
4361
4362 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
4363 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
4364 if (attr_id < *prividx)
4365 continue;
4366
4367 size = rtnl_get_offload_stats_attr_size(attr_id);
4368 if (!size)
4369 continue;
4370
Or Gerlitz3df5b3c2016-11-22 23:09:54 +02004371 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004372 continue;
4373
4374 attr = nla_reserve_64bit(skb, attr_id, size,
4375 IFLA_OFFLOAD_XSTATS_UNSPEC);
4376 if (!attr)
4377 goto nla_put_failure;
4378
4379 attr_data = nla_data(attr);
4380 memset(attr_data, 0, size);
4381 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
4382 attr_data);
4383 if (err)
4384 goto get_offload_stats_failure;
4385 }
4386
4387 if (!attr)
4388 return -ENODATA;
4389
4390 *prividx = 0;
4391 return 0;
4392
4393nla_put_failure:
4394 err = -EMSGSIZE;
4395get_offload_stats_failure:
4396 *prividx = attr_id;
4397 return err;
4398}
4399
4400static int rtnl_get_offload_stats_size(const struct net_device *dev)
4401{
4402 int nla_size = 0;
4403 int attr_id;
4404 int size;
4405
4406 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
4407 dev->netdev_ops->ndo_get_offload_stats))
4408 return 0;
4409
4410 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
4411 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
Or Gerlitz3df5b3c2016-11-22 23:09:54 +02004412 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004413 continue;
4414 size = rtnl_get_offload_stats_attr_size(attr_id);
4415 nla_size += nla_total_size_64bit(size);
4416 }
4417
4418 if (nla_size != 0)
4419 nla_size += nla_total_size(0);
4420
4421 return nla_size;
4422}
4423
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004424static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
4425 int type, u32 pid, u32 seq, u32 change,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004426 unsigned int flags, unsigned int filter_mask,
4427 int *idxattr, int *prividx)
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004428{
4429 struct if_stats_msg *ifsm;
4430 struct nlmsghdr *nlh;
4431 struct nlattr *attr;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004432 int s_prividx = *prividx;
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004433 int err;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004434
4435 ASSERT_RTNL();
4436
4437 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
4438 if (!nlh)
4439 return -EMSGSIZE;
4440
4441 ifsm = nlmsg_data(nlh);
Nikolay Aleksandrovce024f42017-10-03 13:20:48 +03004442 ifsm->family = PF_UNSPEC;
4443 ifsm->pad1 = 0;
4444 ifsm->pad2 = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004445 ifsm->ifindex = dev->ifindex;
4446 ifsm->filter_mask = filter_mask;
4447
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004448 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004449 struct rtnl_link_stats64 *sp;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004450
Nicolas Dichtel58414d32016-04-21 18:58:25 +02004451 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
4452 sizeof(struct rtnl_link_stats64),
4453 IFLA_STATS_UNSPEC);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004454 if (!attr)
4455 goto nla_put_failure;
4456
4457 sp = nla_data(attr);
4458 dev_get_stats(dev, sp);
4459 }
4460
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004461 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
4462 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
4463
4464 if (ops && ops->fill_linkxstats) {
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004465 *idxattr = IFLA_STATS_LINK_XSTATS;
4466 attr = nla_nest_start(skb,
4467 IFLA_STATS_LINK_XSTATS);
4468 if (!attr)
4469 goto nla_put_failure;
4470
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004471 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
4472 nla_nest_end(skb, attr);
4473 if (err)
4474 goto nla_put_failure;
4475 *idxattr = 0;
4476 }
4477 }
4478
4479 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
4480 *idxattr)) {
4481 const struct rtnl_link_ops *ops = NULL;
4482 const struct net_device *master;
4483
4484 master = netdev_master_upper_dev_get(dev);
4485 if (master)
4486 ops = master->rtnl_link_ops;
4487 if (ops && ops->fill_linkxstats) {
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004488 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
4489 attr = nla_nest_start(skb,
4490 IFLA_STATS_LINK_XSTATS_SLAVE);
4491 if (!attr)
4492 goto nla_put_failure;
4493
4494 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004495 nla_nest_end(skb, attr);
4496 if (err)
4497 goto nla_put_failure;
4498 *idxattr = 0;
4499 }
4500 }
4501
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004502 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
4503 *idxattr)) {
4504 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
4505 attr = nla_nest_start(skb, IFLA_STATS_LINK_OFFLOAD_XSTATS);
4506 if (!attr)
4507 goto nla_put_failure;
4508
4509 err = rtnl_get_offload_stats(skb, dev, prividx);
4510 if (err == -ENODATA)
4511 nla_nest_cancel(skb, attr);
4512 else
4513 nla_nest_end(skb, attr);
4514
4515 if (err && err != -ENODATA)
4516 goto nla_put_failure;
4517 *idxattr = 0;
4518 }
4519
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004520 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
4521 struct rtnl_af_ops *af_ops;
4522
4523 *idxattr = IFLA_STATS_AF_SPEC;
4524 attr = nla_nest_start(skb, IFLA_STATS_AF_SPEC);
4525 if (!attr)
4526 goto nla_put_failure;
4527
Florian Westphal5fa85a02017-10-16 15:44:36 +02004528 rcu_read_lock();
4529 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004530 if (af_ops->fill_stats_af) {
4531 struct nlattr *af;
4532 int err;
4533
4534 af = nla_nest_start(skb, af_ops->family);
Florian Westphal5fa85a02017-10-16 15:44:36 +02004535 if (!af) {
4536 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004537 goto nla_put_failure;
Florian Westphal5fa85a02017-10-16 15:44:36 +02004538 }
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004539 err = af_ops->fill_stats_af(skb, dev);
4540
Florian Westphal5fa85a02017-10-16 15:44:36 +02004541 if (err == -ENODATA) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004542 nla_nest_cancel(skb, af);
Florian Westphal5fa85a02017-10-16 15:44:36 +02004543 } else if (err < 0) {
4544 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004545 goto nla_put_failure;
Florian Westphal5fa85a02017-10-16 15:44:36 +02004546 }
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004547
4548 nla_nest_end(skb, af);
4549 }
4550 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02004551 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004552
4553 nla_nest_end(skb, attr);
4554
4555 *idxattr = 0;
4556 }
4557
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004558 nlmsg_end(skb, nlh);
4559
4560 return 0;
4561
4562nla_put_failure:
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004563 /* not a multi message or no progress mean a real error */
4564 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
4565 nlmsg_cancel(skb, nlh);
4566 else
4567 nlmsg_end(skb, nlh);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004568
4569 return -EMSGSIZE;
4570}
4571
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004572static size_t if_nlmsg_stats_size(const struct net_device *dev,
4573 u32 filter_mask)
4574{
4575 size_t size = 0;
4576
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004577 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004578 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
4579
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004580 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
4581 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004582 int attr = IFLA_STATS_LINK_XSTATS;
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004583
4584 if (ops && ops->get_linkxstats_size) {
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004585 size += nla_total_size(ops->get_linkxstats_size(dev,
4586 attr));
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004587 /* for IFLA_STATS_LINK_XSTATS */
4588 size += nla_total_size(0);
4589 }
4590 }
4591
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004592 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
4593 struct net_device *_dev = (struct net_device *)dev;
4594 const struct rtnl_link_ops *ops = NULL;
4595 const struct net_device *master;
4596
4597 /* netdev_master_upper_dev_get can't take const */
4598 master = netdev_master_upper_dev_get(_dev);
4599 if (master)
4600 ops = master->rtnl_link_ops;
4601 if (ops && ops->get_linkxstats_size) {
4602 int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
4603
4604 size += nla_total_size(ops->get_linkxstats_size(dev,
4605 attr));
4606 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
4607 size += nla_total_size(0);
4608 }
4609 }
4610
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004611 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
4612 size += rtnl_get_offload_stats_size(dev);
4613
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004614 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
4615 struct rtnl_af_ops *af_ops;
4616
4617 /* for IFLA_STATS_AF_SPEC */
4618 size += nla_total_size(0);
4619
Florian Westphal5fa85a02017-10-16 15:44:36 +02004620 rcu_read_lock();
4621 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004622 if (af_ops->get_stats_af_size) {
4623 size += nla_total_size(
4624 af_ops->get_stats_af_size(dev));
4625
4626 /* for AF_* */
4627 size += nla_total_size(0);
4628 }
4629 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02004630 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004631 }
4632
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004633 return size;
4634}
4635
David Ahernc21ef3e2017-04-16 09:48:24 -07004636static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
4637 struct netlink_ext_ack *extack)
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004638{
4639 struct net *net = sock_net(skb->sk);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004640 struct net_device *dev = NULL;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004641 int idxattr = 0, prividx = 0;
4642 struct if_stats_msg *ifsm;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004643 struct sk_buff *nskb;
4644 u32 filter_mask;
4645 int err;
4646
Mathias Krause4775cc12016-12-28 17:52:15 +01004647 if (nlmsg_len(nlh) < sizeof(*ifsm))
4648 return -EINVAL;
4649
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004650 ifsm = nlmsg_data(nlh);
4651 if (ifsm->ifindex > 0)
4652 dev = __dev_get_by_index(net, ifsm->ifindex);
4653 else
4654 return -EINVAL;
4655
4656 if (!dev)
4657 return -ENODEV;
4658
4659 filter_mask = ifsm->filter_mask;
4660 if (!filter_mask)
4661 return -EINVAL;
4662
4663 nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
4664 if (!nskb)
4665 return -ENOBUFS;
4666
4667 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
4668 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004669 0, filter_mask, &idxattr, &prividx);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004670 if (err < 0) {
4671 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
4672 WARN_ON(err == -EMSGSIZE);
4673 kfree_skb(nskb);
4674 } else {
4675 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
4676 }
4677
4678 return err;
4679}
4680
4681static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
4682{
David Ahern841891e2018-10-07 20:16:32 -07004683 struct netlink_ext_ack *extack = cb->extack;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004684 int h, s_h, err, s_idx, s_idxattr, s_prividx;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004685 struct net *net = sock_net(skb->sk);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004686 unsigned int flags = NLM_F_MULTI;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004687 struct if_stats_msg *ifsm;
4688 struct hlist_head *head;
4689 struct net_device *dev;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004690 u32 filter_mask = 0;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004691 int idx = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004692
4693 s_h = cb->args[0];
4694 s_idx = cb->args[1];
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004695 s_idxattr = cb->args[2];
4696 s_prividx = cb->args[3];
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004697
4698 cb->seq = net->dev_base_seq;
4699
David Ahern841891e2018-10-07 20:16:32 -07004700 if (nlmsg_len(cb->nlh) < sizeof(*ifsm)) {
4701 NL_SET_ERR_MSG(extack, "Invalid header for stats dump");
Mathias Krause4775cc12016-12-28 17:52:15 +01004702 return -EINVAL;
David Ahern841891e2018-10-07 20:16:32 -07004703 }
Mathias Krause4775cc12016-12-28 17:52:15 +01004704
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004705 ifsm = nlmsg_data(cb->nlh);
David Ahern841891e2018-10-07 20:16:32 -07004706
4707 /* only requests using NLM_F_DUMP_PROPER_HDR can pass data to
4708 * influence the dump. The legacy exception is filter_mask.
4709 */
4710 if (cb->strict_check) {
4711 if (ifsm->pad1 || ifsm->pad2 || ifsm->ifindex) {
4712 NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request");
4713 return -EINVAL;
4714 }
4715 if (nlmsg_attrlen(cb->nlh, sizeof(*ifsm))) {
4716 NL_SET_ERR_MSG(extack, "Invalid attributes after stats header");
4717 return -EINVAL;
4718 }
4719 }
4720
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004721 filter_mask = ifsm->filter_mask;
David Ahern841891e2018-10-07 20:16:32 -07004722 if (!filter_mask) {
4723 NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump");
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004724 return -EINVAL;
David Ahern841891e2018-10-07 20:16:32 -07004725 }
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004726
4727 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4728 idx = 0;
4729 head = &net->dev_index_head[h];
4730 hlist_for_each_entry(dev, head, index_hlist) {
4731 if (idx < s_idx)
4732 goto cont;
4733 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
4734 NETLINK_CB(cb->skb).portid,
4735 cb->nlh->nlmsg_seq, 0,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004736 flags, filter_mask,
4737 &s_idxattr, &s_prividx);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004738 /* If we ran out of room on the first message,
4739 * we're in trouble
4740 */
4741 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
4742
4743 if (err < 0)
4744 goto out;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004745 s_prividx = 0;
4746 s_idxattr = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004747 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
4748cont:
4749 idx++;
4750 }
4751 }
4752out:
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004753 cb->args[3] = s_prividx;
4754 cb->args[2] = s_idxattr;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004755 cb->args[1] = idx;
4756 cb->args[0] = h;
4757
4758 return skb->len;
4759}
4760
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761/* Process one rtnetlink message. */
4762
Johannes Berg2d4bc932017-04-12 14:34:04 +02004763static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
4764 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004766 struct net *net = sock_net(skb->sk);
Florian Westphaladdf9b92017-12-02 21:44:05 +01004767 struct rtnl_link *link;
Florian Westphale4202512017-12-02 21:44:06 +01004768 struct module *owner;
Florian Westphal6853dd42017-08-09 20:41:51 +02004769 int err = -EOPNOTSUPP;
Thomas Grafe2849862007-03-22 11:48:11 -07004770 rtnl_doit_func doit;
Florian Westphal62256f92017-08-09 20:41:52 +02004771 unsigned int flags;
Alexander Kuleshov617cfc72016-01-10 21:26:57 +06004772 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 int family;
4774 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 if (type > RTM_MAX)
Thomas Graf038890f2007-04-05 14:35:52 -07004778 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779
4780 type -= RTM_BASE;
4781
4782 /* All the messages must have at least 1 byte length */
Hong zhi guo573ce262013-03-27 06:47:04 +00004783 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 return 0;
4785
Hong zhi guo573ce262013-03-27 06:47:04 +00004786 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 kind = type&3;
4788
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07004789 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07004790 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791
Florian Westphal6853dd42017-08-09 20:41:51 +02004792 rcu_read_lock();
David S. Millerb8f3ab42011-01-18 12:40:38 -08004793 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004794 struct sock *rtnl;
Thomas Grafe2849862007-03-22 11:48:11 -07004795 rtnl_dumpit_func dumpit;
Greg Rosec7ac8672011-06-10 01:27:09 +00004796 u16 min_dump_alloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797
Florian Westphaladdf9b92017-12-02 21:44:05 +01004798 link = rtnl_get_link(family, type);
4799 if (!link || !link->dumpit) {
Florian Westphal6853dd42017-08-09 20:41:51 +02004800 family = PF_UNSPEC;
Florian Westphaladdf9b92017-12-02 21:44:05 +01004801 link = rtnl_get_link(family, type);
4802 if (!link || !link->dumpit)
Florian Westphal6853dd42017-08-09 20:41:51 +02004803 goto err_unlock;
4804 }
Florian Westphale4202512017-12-02 21:44:06 +01004805 owner = link->owner;
Florian Westphaladdf9b92017-12-02 21:44:05 +01004806 dumpit = link->dumpit;
Florian Westphale1fa6d22017-08-09 20:41:47 +02004807
Florian Westphal5c2bb9b2017-08-10 16:52:58 +02004808 if (type == RTM_GETLINK - RTM_BASE)
Florian Westphale1fa6d22017-08-09 20:41:47 +02004809 min_dump_alloc = rtnl_calcit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810
Florian Westphale4202512017-12-02 21:44:06 +01004811 err = 0;
4812 /* need to do this before rcu_read_unlock() */
4813 if (!try_module_get(owner))
4814 err = -EPROTONOSUPPORT;
4815
Florian Westphal6853dd42017-08-09 20:41:51 +02004816 rcu_read_unlock();
4817
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004818 rtnl = net->rtnl;
Florian Westphale4202512017-12-02 21:44:06 +01004819 if (err == 0) {
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00004820 struct netlink_dump_control c = {
4821 .dump = dumpit,
4822 .min_dump_alloc = min_dump_alloc,
Florian Westphale4202512017-12-02 21:44:06 +01004823 .module = owner,
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00004824 };
4825 err = netlink_dump_start(rtnl, skb, nlh, &c);
Florian Westphale4202512017-12-02 21:44:06 +01004826 /* netlink_dump_start() will keep a reference on
4827 * module if dump is still in progress.
4828 */
4829 module_put(owner);
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00004830 }
Eric Dumazet2907c352011-05-25 07:34:04 +00004831 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 }
4833
Florian Westphaladdf9b92017-12-02 21:44:05 +01004834 link = rtnl_get_link(family, type);
4835 if (!link || !link->doit) {
Florian Westphal8caa38b2017-08-10 16:53:01 +02004836 family = PF_UNSPEC;
Florian Westphaladdf9b92017-12-02 21:44:05 +01004837 link = rtnl_get_link(PF_UNSPEC, type);
4838 if (!link || !link->doit)
4839 goto out_unlock;
Florian Westphal8caa38b2017-08-10 16:53:01 +02004840 }
4841
Florian Westphale4202512017-12-02 21:44:06 +01004842 owner = link->owner;
4843 if (!try_module_get(owner)) {
4844 err = -EPROTONOSUPPORT;
4845 goto out_unlock;
4846 }
4847
Florian Westphaladdf9b92017-12-02 21:44:05 +01004848 flags = link->flags;
Florian Westphal62256f92017-08-09 20:41:52 +02004849 if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
Florian Westphaladdf9b92017-12-02 21:44:05 +01004850 doit = link->doit;
Florian Westphal62256f92017-08-09 20:41:52 +02004851 rcu_read_unlock();
4852 if (doit)
4853 err = doit(skb, nlh, extack);
Florian Westphale4202512017-12-02 21:44:06 +01004854 module_put(owner);
Florian Westphal62256f92017-08-09 20:41:52 +02004855 return err;
4856 }
Florian Westphal6853dd42017-08-09 20:41:51 +02004857 rcu_read_unlock();
4858
Florian Westphal0cc09022017-08-09 20:41:50 +02004859 rtnl_lock();
Florian Westphaladdf9b92017-12-02 21:44:05 +01004860 link = rtnl_get_link(family, type);
4861 if (link && link->doit)
4862 err = link->doit(skb, nlh, extack);
Florian Westphal0cc09022017-08-09 20:41:50 +02004863 rtnl_unlock();
Florian Westphaladdf9b92017-12-02 21:44:05 +01004864
Florian Westphale4202512017-12-02 21:44:06 +01004865 module_put(owner);
4866
Florian Westphaladdf9b92017-12-02 21:44:05 +01004867 return err;
4868
4869out_unlock:
4870 rcu_read_unlock();
Florian Westphal0cc09022017-08-09 20:41:50 +02004871 return err;
4872
4873err_unlock:
Florian Westphal6853dd42017-08-09 20:41:51 +02004874 rcu_read_unlock();
Florian Westphal0cc09022017-08-09 20:41:50 +02004875 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876}
4877
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07004878static void rtnetlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07004880 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881}
4882
Julien Gomes5f729ea2017-06-20 13:54:16 -07004883static int rtnetlink_bind(struct net *net, int group)
4884{
4885 switch (group) {
4886 case RTNLGRP_IPV4_MROUTE_R:
4887 case RTNLGRP_IPV6_MROUTE_R:
4888 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
4889 return -EPERM;
4890 break;
4891 }
4892 return 0;
4893}
4894
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
4896{
Jiri Pirko351638e2013-05-28 01:30:21 +00004897 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02004898
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899 switch (event) {
Vlad Yasevich5138e862017-04-04 09:23:41 -04004900 case NETDEV_REBOOT:
Xin Long8a212582017-10-15 18:13:41 +08004901 case NETDEV_CHANGEMTU:
David Ahern37536542017-07-19 10:22:40 -07004902 case NETDEV_CHANGEADDR:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004903 case NETDEV_CHANGENAME:
4904 case NETDEV_FEAT_CHANGE:
4905 case NETDEV_BONDING_FAILOVER:
Xin Longe6e66592017-10-15 18:13:43 +08004906 case NETDEV_POST_TYPE_CHANGE:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004907 case NETDEV_NOTIFY_PEERS:
Xin Longdc709f32017-10-15 18:13:44 +08004908 case NETDEV_CHANGEUPPER:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004909 case NETDEV_RESEND_IGMP:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004910 case NETDEV_CHANGEINFODATA:
Xin Longeeda3fb2017-10-24 13:54:19 +08004911 case NETDEV_CHANGELOWERSTATE:
Xin Longebdcf042017-10-15 18:13:42 +08004912 case NETDEV_CHANGE_TX_QUEUE_LEN:
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04004913 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01004914 GFP_KERNEL, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 break;
4916 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 break;
4918 }
4919 return NOTIFY_DONE;
4920}
4921
4922static struct notifier_block rtnetlink_dev_notifier = {
4923 .notifier_call = rtnetlink_event,
4924};
4925
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004926
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004927static int __net_init rtnetlink_net_init(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004928{
4929 struct sock *sk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00004930 struct netlink_kernel_cfg cfg = {
4931 .groups = RTNLGRP_MAX,
4932 .input = rtnetlink_rcv,
4933 .cb_mutex = &rtnl_mutex,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00004934 .flags = NL_CFG_F_NONROOT_RECV,
Julien Gomes5f729ea2017-06-20 13:54:16 -07004935 .bind = rtnetlink_bind,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00004936 };
4937
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00004938 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004939 if (!sk)
4940 return -ENOMEM;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004941 net->rtnl = sk;
4942 return 0;
4943}
4944
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004945static void __net_exit rtnetlink_net_exit(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004946{
Denis V. Lunev775516b2008-01-18 23:55:19 -08004947 netlink_kernel_release(net->rtnl);
4948 net->rtnl = NULL;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004949}
4950
4951static struct pernet_operations rtnetlink_net_ops = {
4952 .init = rtnetlink_net_init,
4953 .exit = rtnetlink_net_exit,
4954};
4955
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956void __init rtnetlink_init(void)
4957{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004958 if (register_pernet_subsys(&rtnetlink_net_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 panic("rtnetlink_init: cannot initialize rtnetlink\n");
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004960
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 register_netdevice_notifier(&rtnetlink_dev_notifier);
Thomas Graf340d17f2007-03-22 11:49:22 -07004962
Greg Rosec7ac8672011-06-10 01:27:09 +00004963 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
Florian Westphalb97bac62017-08-09 20:41:48 +02004964 rtnl_dump_ifinfo, 0);
4965 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
4966 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
4967 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
Thomas Graf687ad8c2007-03-22 11:59:42 -07004968
Florian Westphalb97bac62017-08-09 20:41:48 +02004969 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
4970 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
4971 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
John Fastabend77162022012-04-15 06:43:56 +00004972
Florian Westphalb97bac62017-08-09 20:41:48 +02004973 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
4974 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
4975 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, 0);
John Fastabende5a55a82012-10-24 08:12:57 +00004976
Florian Westphalb97bac62017-08-09 20:41:48 +02004977 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
4978 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
4979 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004980
4981 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
Florian Westphalb97bac62017-08-09 20:41:48 +02004982 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983}