blob: a4faefd65006919f1b277c1ea87885e7d4451b80 [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
Eric Dumazete0d087a2009-11-07 01:26:17 -080062struct rtnl_link {
Thomas Grafe2849862007-03-22 11:48:11 -070063 rtnl_doit_func doit;
64 rtnl_dumpit_func dumpit;
Florian Westphale4202512017-12-02 21:44:06 +010065 struct module *owner;
Florian Westphal62256f92017-08-09 20:41:52 +020066 unsigned int flags;
Florian Westphaladdf9b92017-12-02 21:44:05 +010067 struct rcu_head rcu;
Thomas Grafe2849862007-03-22 11:48:11 -070068};
69
Stephen Hemminger6756ae42006-03-20 22:23:58 -080070static DEFINE_MUTEX(rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72void rtnl_lock(void)
73{
Stephen Hemminger6756ae42006-03-20 22:23:58 -080074 mutex_lock(&rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
Eric Dumazete0d087a2009-11-07 01:26:17 -080076EXPORT_SYMBOL(rtnl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Eric Dumazet1b5c5492016-06-13 20:21:50 -070078static struct sk_buff *defer_kfree_skb_list;
79void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail)
80{
81 if (head && tail) {
82 tail->next = defer_kfree_skb_list;
83 defer_kfree_skb_list = head;
84 }
85}
86EXPORT_SYMBOL(rtnl_kfree_skbs);
87
Stephen Hemminger6756ae42006-03-20 22:23:58 -080088void __rtnl_unlock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Eric Dumazet1b5c5492016-06-13 20:21:50 -070090 struct sk_buff *head = defer_kfree_skb_list;
91
92 defer_kfree_skb_list = NULL;
93
Stephen Hemminger6756ae42006-03-20 22:23:58 -080094 mutex_unlock(&rtnl_mutex);
Eric Dumazet1b5c5492016-06-13 20:21:50 -070095
96 while (head) {
97 struct sk_buff *next = head->next;
98
99 kfree_skb(head);
100 cond_resched();
101 head = next;
102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105void rtnl_unlock(void)
106{
Herbert Xu58ec3b42008-10-07 15:50:03 -0700107 /* This fellow will unlock it for us. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 netdev_run_todo();
109}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800110EXPORT_SYMBOL(rtnl_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800112int rtnl_trylock(void)
113{
114 return mutex_trylock(&rtnl_mutex);
115}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800116EXPORT_SYMBOL(rtnl_trylock);
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800117
Patrick McHardyc9c10142008-04-23 22:10:48 -0700118int rtnl_is_locked(void)
119{
120 return mutex_is_locked(&rtnl_mutex);
121}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800122EXPORT_SYMBOL(rtnl_is_locked);
Patrick McHardyc9c10142008-04-23 22:10:48 -0700123
Paul E. McKenneya898def2010-02-22 17:04:49 -0800124#ifdef CONFIG_PROVE_LOCKING
Yaowei Bai0cbf3342015-10-08 21:29:02 +0800125bool lockdep_rtnl_is_held(void)
Paul E. McKenneya898def2010-02-22 17:04:49 -0800126{
127 return lockdep_is_held(&rtnl_mutex);
128}
129EXPORT_SYMBOL(lockdep_rtnl_is_held);
130#endif /* #ifdef CONFIG_PROVE_LOCKING */
131
Florian Westphalb0e9fe12017-12-04 22:42:30 +0100132static struct rtnl_link *__rcu *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
Thomas Grafe2849862007-03-22 11:48:11 -0700133
134static inline int rtm_msgindex(int msgtype)
135{
136 int msgindex = msgtype - RTM_BASE;
137
138 /*
139 * msgindex < 0 implies someone tried to register a netlink
140 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
141 * the message type has not been added to linux/rtnetlink.h
142 */
143 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
144
145 return msgindex;
146}
147
Florian Westphaladdf9b92017-12-02 21:44:05 +0100148static struct rtnl_link *rtnl_get_link(int protocol, int msgtype)
149{
150 struct rtnl_link **tab;
151
152 if (protocol >= ARRAY_SIZE(rtnl_msg_handlers))
153 protocol = PF_UNSPEC;
154
155 tab = rcu_dereference_rtnl(rtnl_msg_handlers[protocol]);
156 if (!tab)
157 tab = rcu_dereference_rtnl(rtnl_msg_handlers[PF_UNSPEC]);
158
159 return tab[msgtype];
160}
161
Florian Westphale4202512017-12-02 21:44:06 +0100162static int rtnl_register_internal(struct module *owner,
163 int protocol, int msgtype,
164 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
165 unsigned int flags)
Thomas Grafe2849862007-03-22 11:48:11 -0700166{
Florian Westphalb0e9fe12017-12-04 22:42:30 +0100167 struct rtnl_link *link, *old;
168 struct rtnl_link __rcu **tab;
Thomas Grafe2849862007-03-22 11:48:11 -0700169 int msgindex;
Florian Westphaladdf9b92017-12-02 21:44:05 +0100170 int ret = -ENOBUFS;
Thomas Grafe2849862007-03-22 11:48:11 -0700171
Patrick McHardy25239ce2010-04-26 16:02:05 +0200172 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700173 msgindex = rtm_msgindex(msgtype);
174
Florian Westphaladdf9b92017-12-02 21:44:05 +0100175 rtnl_lock();
176 tab = rtnl_msg_handlers[protocol];
Thomas Grafe2849862007-03-22 11:48:11 -0700177 if (tab == NULL) {
Florian Westphaladdf9b92017-12-02 21:44:05 +0100178 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(void *), GFP_KERNEL);
179 if (!tab)
180 goto unlock;
Thomas Grafe2849862007-03-22 11:48:11 -0700181
Florian Westphaladdf9b92017-12-02 21:44:05 +0100182 /* ensures we see the 0 stores */
Florian Westphal6853dd42017-08-09 20:41:51 +0200183 rcu_assign_pointer(rtnl_msg_handlers[protocol], tab);
Thomas Grafe2849862007-03-22 11:48:11 -0700184 }
185
Florian Westphaladdf9b92017-12-02 21:44:05 +0100186 old = rtnl_dereference(tab[msgindex]);
187 if (old) {
188 link = kmemdup(old, sizeof(*old), GFP_KERNEL);
189 if (!link)
190 goto unlock;
191 } else {
192 link = kzalloc(sizeof(*link), GFP_KERNEL);
193 if (!link)
194 goto unlock;
195 }
Thomas Grafe2849862007-03-22 11:48:11 -0700196
Florian Westphale4202512017-12-02 21:44:06 +0100197 WARN_ON(link->owner && link->owner != owner);
198 link->owner = owner;
199
Florian Westphaladdf9b92017-12-02 21:44:05 +0100200 WARN_ON(doit && link->doit && link->doit != doit);
201 if (doit)
202 link->doit = doit;
203 WARN_ON(dumpit && link->dumpit && link->dumpit != dumpit);
204 if (dumpit)
205 link->dumpit = dumpit;
206
207 link->flags |= flags;
208
209 /* publish protocol:msgtype */
210 rcu_assign_pointer(tab[msgindex], link);
211 ret = 0;
212 if (old)
213 kfree_rcu(old, rcu);
214unlock:
215 rtnl_unlock();
216 return ret;
Thomas Grafe2849862007-03-22 11:48:11 -0700217}
Florian Westphale4202512017-12-02 21:44:06 +0100218
219/**
220 * rtnl_register_module - Register a rtnetlink message type
221 *
222 * @owner: module registering the hook (THIS_MODULE)
223 * @protocol: Protocol family or PF_UNSPEC
224 * @msgtype: rtnetlink message type
225 * @doit: Function pointer called for each request message
226 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
227 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
228 *
229 * Like rtnl_register, but for use by removable modules.
230 */
231int rtnl_register_module(struct module *owner,
232 int protocol, int msgtype,
233 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
234 unsigned int flags)
235{
236 return rtnl_register_internal(owner, protocol, msgtype,
237 doit, dumpit, flags);
238}
239EXPORT_SYMBOL_GPL(rtnl_register_module);
240
241/**
Florian Westphal16feebc2017-12-02 21:44:08 +0100242 * rtnl_register - Register a rtnetlink message type
Florian Westphale4202512017-12-02 21:44:06 +0100243 * @protocol: Protocol family or PF_UNSPEC
244 * @msgtype: rtnetlink message type
245 * @doit: Function pointer called for each request message
246 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
247 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
248 *
249 * Registers the specified function pointers (at least one of them has
250 * to be non-NULL) to be called whenever a request message for the
251 * specified protocol family and message type is received.
252 *
253 * The special protocol family PF_UNSPEC may be used to define fallback
254 * function pointers for the case when no entry for the specific protocol
255 * family exists.
Thomas Grafe2849862007-03-22 11:48:11 -0700256 */
257void rtnl_register(int protocol, int msgtype,
Greg Rosec7ac8672011-06-10 01:27:09 +0000258 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
Florian Westphalb97bac62017-08-09 20:41:48 +0200259 unsigned int flags)
Thomas Grafe2849862007-03-22 11:48:11 -0700260{
Florian Westphal16feebc2017-12-02 21:44:08 +0100261 int err;
262
263 err = rtnl_register_internal(NULL, protocol, msgtype, doit, dumpit,
264 flags);
265 if (err)
266 pr_err("Unable to register rtnetlink message handler, "
267 "protocol = %d, message type = %d\n", protocol, msgtype);
Thomas Grafe2849862007-03-22 11:48:11 -0700268}
Thomas Grafe2849862007-03-22 11:48:11 -0700269
270/**
271 * rtnl_unregister - Unregister a rtnetlink message type
272 * @protocol: Protocol family or PF_UNSPEC
273 * @msgtype: rtnetlink message type
274 *
275 * Returns 0 on success or a negative error code.
276 */
277int rtnl_unregister(int protocol, int msgtype)
278{
Florian Westphaladdf9b92017-12-02 21:44:05 +0100279 struct rtnl_link **tab, *link;
Thomas Grafe2849862007-03-22 11:48:11 -0700280 int msgindex;
281
Patrick McHardy25239ce2010-04-26 16:02:05 +0200282 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700283 msgindex = rtm_msgindex(msgtype);
284
Florian Westphal6853dd42017-08-09 20:41:51 +0200285 rtnl_lock();
Florian Westphaladdf9b92017-12-02 21:44:05 +0100286 tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
287 if (!tab) {
Florian Westphal6853dd42017-08-09 20:41:51 +0200288 rtnl_unlock();
Thomas Grafe2849862007-03-22 11:48:11 -0700289 return -ENOENT;
Florian Westphal6853dd42017-08-09 20:41:51 +0200290 }
Thomas Grafe2849862007-03-22 11:48:11 -0700291
Florian Westphaladdf9b92017-12-02 21:44:05 +0100292 link = tab[msgindex];
293 rcu_assign_pointer(tab[msgindex], NULL);
Florian Westphal6853dd42017-08-09 20:41:51 +0200294 rtnl_unlock();
Thomas Grafe2849862007-03-22 11:48:11 -0700295
Florian Westphaladdf9b92017-12-02 21:44:05 +0100296 kfree_rcu(link, rcu);
297
Thomas Grafe2849862007-03-22 11:48:11 -0700298 return 0;
299}
Thomas Grafe2849862007-03-22 11:48:11 -0700300EXPORT_SYMBOL_GPL(rtnl_unregister);
301
302/**
303 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
304 * @protocol : Protocol family or PF_UNSPEC
305 *
306 * Identical to calling rtnl_unregster() for all registered message types
307 * of a certain protocol family.
308 */
309void rtnl_unregister_all(int protocol)
310{
Florian Westphaladdf9b92017-12-02 21:44:05 +0100311 struct rtnl_link **tab, *link;
312 int msgindex;
Florian Westphal019a3162017-08-09 20:41:49 +0200313
Patrick McHardy25239ce2010-04-26 16:02:05 +0200314 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700315
Florian Westphal019a3162017-08-09 20:41:49 +0200316 rtnl_lock();
Florian Westphaladdf9b92017-12-02 21:44:05 +0100317 tab = rtnl_msg_handlers[protocol];
Florian Westphal6853dd42017-08-09 20:41:51 +0200318 RCU_INIT_POINTER(rtnl_msg_handlers[protocol], NULL);
Florian Westphaladdf9b92017-12-02 21:44:05 +0100319 for (msgindex = 0; msgindex < RTM_NR_MSGTYPES; msgindex++) {
320 link = tab[msgindex];
321 if (!link)
322 continue;
323
324 rcu_assign_pointer(tab[msgindex], NULL);
325 kfree_rcu(link, rcu);
326 }
Florian Westphal019a3162017-08-09 20:41:49 +0200327 rtnl_unlock();
328
Florian Westphal6853dd42017-08-09 20:41:51 +0200329 synchronize_net();
330
Florian Westphaladdf9b92017-12-02 21:44:05 +0100331 kfree(tab);
Thomas Grafe2849862007-03-22 11:48:11 -0700332}
Thomas Grafe2849862007-03-22 11:48:11 -0700333EXPORT_SYMBOL_GPL(rtnl_unregister_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Patrick McHardy38f7b872007-06-13 12:03:51 -0700335static LIST_HEAD(link_ops);
336
Eric Dumazetc63044f2011-12-13 11:38:00 +0000337static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
338{
339 const struct rtnl_link_ops *ops;
340
341 list_for_each_entry(ops, &link_ops, list) {
342 if (!strcmp(ops->kind, kind))
343 return ops;
344 }
345 return NULL;
346}
347
Patrick McHardy38f7b872007-06-13 12:03:51 -0700348/**
349 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
350 * @ops: struct rtnl_link_ops * to register
351 *
352 * The caller must hold the rtnl_mutex. This function should be used
353 * by drivers that create devices during module initialization. It
354 * must be called before registering the devices.
355 *
356 * Returns 0 on success or a negative error code.
357 */
358int __rtnl_link_register(struct rtnl_link_ops *ops)
359{
Eric Dumazetc63044f2011-12-13 11:38:00 +0000360 if (rtnl_link_ops_get(ops->kind))
361 return -EEXIST;
362
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +0200363 /* The check for setup is here because if ops
364 * does not have that filled up, it is not possible
365 * to use the ops for creating device. So do not
366 * fill up dellink as well. That disables rtnl_dellink.
367 */
368 if (ops->setup && !ops->dellink)
Eric Dumazet23289a32009-10-27 07:06:36 +0000369 ops->dellink = unregister_netdevice_queue;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700370
Patrick McHardy38f7b872007-06-13 12:03:51 -0700371 list_add_tail(&ops->list, &link_ops);
372 return 0;
373}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700374EXPORT_SYMBOL_GPL(__rtnl_link_register);
375
376/**
377 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
378 * @ops: struct rtnl_link_ops * to register
379 *
380 * Returns 0 on success or a negative error code.
381 */
382int rtnl_link_register(struct rtnl_link_ops *ops)
383{
384 int err;
385
386 rtnl_lock();
387 err = __rtnl_link_register(ops);
388 rtnl_unlock();
389 return err;
390}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700391EXPORT_SYMBOL_GPL(rtnl_link_register);
392
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700393static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
394{
395 struct net_device *dev;
Eric Dumazet23289a32009-10-27 07:06:36 +0000396 LIST_HEAD(list_kill);
397
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700398 for_each_netdev(net, dev) {
Eric Dumazet23289a32009-10-27 07:06:36 +0000399 if (dev->rtnl_link_ops == ops)
400 ops->dellink(dev, &list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700401 }
Eric Dumazet23289a32009-10-27 07:06:36 +0000402 unregister_netdevice_many(&list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700403}
404
Patrick McHardy38f7b872007-06-13 12:03:51 -0700405/**
406 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
407 * @ops: struct rtnl_link_ops * to unregister
408 *
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700409 * The caller must hold the rtnl_mutex.
Patrick McHardy38f7b872007-06-13 12:03:51 -0700410 */
411void __rtnl_link_unregister(struct rtnl_link_ops *ops)
412{
Eric W. Biederman881d9662007-09-17 11:56:21 -0700413 struct net *net;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700414
Eric W. Biederman881d9662007-09-17 11:56:21 -0700415 for_each_net(net) {
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700416 __rtnl_kill_links(net, ops);
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700417 }
Patrick McHardy38f7b872007-06-13 12:03:51 -0700418 list_del(&ops->list);
419}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700420EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
421
Cong Wang200b9162014-05-12 15:11:20 -0700422/* Return with the rtnl_lock held when there are no network
423 * devices unregistering in any network namespace.
424 */
425static void rtnl_lock_unregistering_all(void)
426{
427 struct net *net;
428 bool unregistering;
Peter Zijlstraff960a72014-10-29 17:04:56 +0100429 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Cong Wang200b9162014-05-12 15:11:20 -0700430
Peter Zijlstraff960a72014-10-29 17:04:56 +0100431 add_wait_queue(&netdev_unregistering_wq, &wait);
Cong Wang200b9162014-05-12 15:11:20 -0700432 for (;;) {
Cong Wang200b9162014-05-12 15:11:20 -0700433 unregistering = false;
434 rtnl_lock();
435 for_each_net(net) {
436 if (net->dev_unreg_count > 0) {
437 unregistering = true;
438 break;
439 }
440 }
441 if (!unregistering)
442 break;
443 __rtnl_unlock();
Peter Zijlstraff960a72014-10-29 17:04:56 +0100444
445 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Cong Wang200b9162014-05-12 15:11:20 -0700446 }
Peter Zijlstraff960a72014-10-29 17:04:56 +0100447 remove_wait_queue(&netdev_unregistering_wq, &wait);
Cong Wang200b9162014-05-12 15:11:20 -0700448}
449
Patrick McHardy38f7b872007-06-13 12:03:51 -0700450/**
451 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
452 * @ops: struct rtnl_link_ops * to unregister
453 */
454void rtnl_link_unregister(struct rtnl_link_ops *ops)
455{
Cong Wang200b9162014-05-12 15:11:20 -0700456 /* Close the race with cleanup_net() */
457 mutex_lock(&net_mutex);
458 rtnl_lock_unregistering_all();
Patrick McHardy38f7b872007-06-13 12:03:51 -0700459 __rtnl_link_unregister(ops);
460 rtnl_unlock();
Cong Wang200b9162014-05-12 15:11:20 -0700461 mutex_unlock(&net_mutex);
Patrick McHardy38f7b872007-06-13 12:03:51 -0700462}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700463EXPORT_SYMBOL_GPL(rtnl_link_unregister);
464
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100465static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
466{
467 struct net_device *master_dev;
468 const struct rtnl_link_ops *ops;
Florian Westphal8515ae32017-08-10 16:52:59 +0200469 size_t size = 0;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100470
Florian Westphal8515ae32017-08-10 16:52:59 +0200471 rcu_read_lock();
472
473 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100474 if (!master_dev)
Florian Westphal8515ae32017-08-10 16:52:59 +0200475 goto out;
476
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100477 ops = master_dev->rtnl_link_ops;
Fernando Luis Vazquez Cao6049f252014-02-04 19:35:02 +0900478 if (!ops || !ops->get_slave_size)
Florian Westphal8515ae32017-08-10 16:52:59 +0200479 goto out;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100480 /* IFLA_INFO_SLAVE_DATA + nested data */
Florian Westphal8515ae32017-08-10 16:52:59 +0200481 size = nla_total_size(sizeof(struct nlattr)) +
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100482 ops->get_slave_size(master_dev, dev);
Florian Westphal8515ae32017-08-10 16:52:59 +0200483
484out:
485 rcu_read_unlock();
486 return size;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100487}
488
Patrick McHardy38f7b872007-06-13 12:03:51 -0700489static size_t rtnl_link_get_size(const struct net_device *dev)
490{
491 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
492 size_t size;
493
494 if (!ops)
495 return 0;
496
Thomas Graf369cf772010-11-11 15:47:59 +0000497 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
498 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700499
500 if (ops->get_size)
501 /* IFLA_INFO_DATA + nested data */
Thomas Graf369cf772010-11-11 15:47:59 +0000502 size += nla_total_size(sizeof(struct nlattr)) +
Patrick McHardy38f7b872007-06-13 12:03:51 -0700503 ops->get_size(dev);
504
505 if (ops->get_xstats_size)
Thomas Graf369cf772010-11-11 15:47:59 +0000506 /* IFLA_INFO_XSTATS */
507 size += nla_total_size(ops->get_xstats_size(dev));
Patrick McHardy38f7b872007-06-13 12:03:51 -0700508
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100509 size += rtnl_link_get_slave_info_data_size(dev);
510
Patrick McHardy38f7b872007-06-13 12:03:51 -0700511 return size;
512}
513
Thomas Graff8ff1822010-11-16 04:30:14 +0000514static LIST_HEAD(rtnl_af_ops);
515
516static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
517{
518 const struct rtnl_af_ops *ops;
519
Florian Westphal5fa85a02017-10-16 15:44:36 +0200520 list_for_each_entry_rcu(ops, &rtnl_af_ops, list) {
Thomas Graff8ff1822010-11-16 04:30:14 +0000521 if (ops->family == family)
522 return ops;
523 }
524
525 return NULL;
526}
527
528/**
Thomas Graff8ff1822010-11-16 04:30:14 +0000529 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
530 * @ops: struct rtnl_af_ops * to register
531 *
532 * Returns 0 on success or a negative error code.
533 */
stephen hemminger3678a9d2013-12-30 10:41:32 -0800534void rtnl_af_register(struct rtnl_af_ops *ops)
Thomas Graff8ff1822010-11-16 04:30:14 +0000535{
Thomas Graff8ff1822010-11-16 04:30:14 +0000536 rtnl_lock();
Florian Westphal5fa85a02017-10-16 15:44:36 +0200537 list_add_tail_rcu(&ops->list, &rtnl_af_ops);
Thomas Graff8ff1822010-11-16 04:30:14 +0000538 rtnl_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +0000539}
540EXPORT_SYMBOL_GPL(rtnl_af_register);
541
542/**
Thomas Graff8ff1822010-11-16 04:30:14 +0000543 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
544 * @ops: struct rtnl_af_ops * to unregister
545 */
546void rtnl_af_unregister(struct rtnl_af_ops *ops)
547{
548 rtnl_lock();
Florian Westphal5fa85a02017-10-16 15:44:36 +0200549 list_del_rcu(&ops->list);
Thomas Graff8ff1822010-11-16 04:30:14 +0000550 rtnl_unlock();
Florian Westphal5fa85a02017-10-16 15:44:36 +0200551
552 synchronize_rcu();
Thomas Graff8ff1822010-11-16 04:30:14 +0000553}
554EXPORT_SYMBOL_GPL(rtnl_af_unregister);
555
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700556static size_t rtnl_link_get_af_size(const struct net_device *dev,
557 u32 ext_filter_mask)
Thomas Graff8ff1822010-11-16 04:30:14 +0000558{
559 struct rtnl_af_ops *af_ops;
560 size_t size;
561
562 /* IFLA_AF_SPEC */
563 size = nla_total_size(sizeof(struct nlattr));
564
Florian Westphal5fa85a02017-10-16 15:44:36 +0200565 rcu_read_lock();
566 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Thomas Graff8ff1822010-11-16 04:30:14 +0000567 if (af_ops->get_link_af_size) {
568 /* AF_* + nested data */
569 size += nla_total_size(sizeof(struct nlattr)) +
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700570 af_ops->get_link_af_size(dev, ext_filter_mask);
Thomas Graff8ff1822010-11-16 04:30:14 +0000571 }
572 }
Florian Westphal5fa85a02017-10-16 15:44:36 +0200573 rcu_read_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +0000574
575 return size;
576}
577
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100578static bool rtnl_have_link_slave_info(const struct net_device *dev)
579{
580 struct net_device *master_dev;
Florian Westphal4c82a952017-09-26 13:58:43 +0200581 bool ret = false;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100582
Florian Westphal4c82a952017-09-26 13:58:43 +0200583 rcu_read_lock();
584
585 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
Jiri Pirko813f0202014-01-23 19:19:21 +0100586 if (master_dev && master_dev->rtnl_link_ops)
Florian Westphal4c82a952017-09-26 13:58:43 +0200587 ret = true;
588 rcu_read_unlock();
589 return ret;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100590}
591
592static int rtnl_link_slave_info_fill(struct sk_buff *skb,
593 const struct net_device *dev)
594{
595 struct net_device *master_dev;
596 const struct rtnl_link_ops *ops;
597 struct nlattr *slave_data;
598 int err;
599
600 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
601 if (!master_dev)
602 return 0;
603 ops = master_dev->rtnl_link_ops;
604 if (!ops)
605 return 0;
606 if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
607 return -EMSGSIZE;
608 if (ops->fill_slave_info) {
609 slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA);
610 if (!slave_data)
611 return -EMSGSIZE;
612 err = ops->fill_slave_info(skb, master_dev, dev);
613 if (err < 0)
614 goto err_cancel_slave_data;
615 nla_nest_end(skb, slave_data);
616 }
617 return 0;
618
619err_cancel_slave_data:
620 nla_nest_cancel(skb, slave_data);
621 return err;
622}
623
624static int rtnl_link_info_fill(struct sk_buff *skb,
625 const struct net_device *dev)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700626{
627 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100628 struct nlattr *data;
629 int err;
630
631 if (!ops)
632 return 0;
633 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
634 return -EMSGSIZE;
635 if (ops->fill_xstats) {
636 err = ops->fill_xstats(skb, dev);
637 if (err < 0)
638 return err;
639 }
640 if (ops->fill_info) {
641 data = nla_nest_start(skb, IFLA_INFO_DATA);
642 if (data == NULL)
643 return -EMSGSIZE;
644 err = ops->fill_info(skb, dev);
645 if (err < 0)
646 goto err_cancel_data;
647 nla_nest_end(skb, data);
648 }
649 return 0;
650
651err_cancel_data:
652 nla_nest_cancel(skb, data);
653 return err;
654}
655
656static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
657{
658 struct nlattr *linkinfo;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700659 int err = -EMSGSIZE;
660
661 linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
662 if (linkinfo == NULL)
663 goto out;
664
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100665 err = rtnl_link_info_fill(skb, dev);
666 if (err < 0)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700667 goto err_cancel_link;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100668
669 err = rtnl_link_slave_info_fill(skb, dev);
670 if (err < 0)
671 goto err_cancel_link;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700672
673 nla_nest_end(skb, linkinfo);
674 return 0;
675
Patrick McHardy38f7b872007-06-13 12:03:51 -0700676err_cancel_link:
677 nla_nest_cancel(skb, linkinfo);
678out:
679 return err;
680}
681
Eric Dumazet95c96172012-04-15 05:58:06 +0000682int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800684 struct sock *rtnl = net->rtnl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 int err = 0;
686
Patrick McHardyac6d4392005-08-14 19:29:52 -0700687 NETLINK_CB(skb).dst_group = group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (echo)
Reshetova, Elena63354792017-06-30 13:07:58 +0300689 refcount_inc(&skb->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
691 if (echo)
692 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
693 return err;
694}
695
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800696int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
Thomas Graf2942e902006-08-15 00:30:25 -0700697{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800698 struct sock *rtnl = net->rtnl;
699
Thomas Graf2942e902006-08-15 00:30:25 -0700700 return nlmsg_unicast(rtnl, skb, pid);
701}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800702EXPORT_SYMBOL(rtnl_unicast);
Thomas Graf2942e902006-08-15 00:30:25 -0700703
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800704void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
705 struct nlmsghdr *nlh, gfp_t flags)
Thomas Graf97676b62006-08-15 00:31:41 -0700706{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800707 struct sock *rtnl = net->rtnl;
Thomas Graf97676b62006-08-15 00:31:41 -0700708 int report = 0;
709
710 if (nlh)
711 report = nlmsg_report(nlh);
712
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800713 nlmsg_notify(rtnl, skb, pid, group, report, flags);
Thomas Graf97676b62006-08-15 00:31:41 -0700714}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800715EXPORT_SYMBOL(rtnl_notify);
Thomas Graf97676b62006-08-15 00:31:41 -0700716
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800717void rtnl_set_sk_err(struct net *net, u32 group, int error)
Thomas Graf97676b62006-08-15 00:31:41 -0700718{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800719 struct sock *rtnl = net->rtnl;
720
Thomas Graf97676b62006-08-15 00:31:41 -0700721 netlink_set_err(rtnl, 0, group, error);
722}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800723EXPORT_SYMBOL(rtnl_set_sk_err);
Thomas Graf97676b62006-08-15 00:31:41 -0700724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
726{
Thomas Graf2d7202b2006-08-22 00:01:27 -0700727 struct nlattr *mx;
728 int i, valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Thomas Graf2d7202b2006-08-22 00:01:27 -0700730 mx = nla_nest_start(skb, RTA_METRICS);
731 if (mx == NULL)
732 return -ENOBUFS;
733
734 for (i = 0; i < RTAX_MAX; i++) {
735 if (metrics[i]) {
Daniel Borkmannea697632015-01-05 23:57:47 +0100736 if (i == RTAX_CC_ALGO - 1) {
737 char tmp[TCP_CA_NAME_MAX], *name;
738
739 name = tcp_ca_get_name_by_key(metrics[i], tmp);
740 if (!name)
741 continue;
742 if (nla_put_string(skb, i + 1, name))
743 goto nla_put_failure;
Daniel Borkmannc3a8d942015-08-31 15:58:47 +0200744 } else if (i == RTAX_FEATURES - 1) {
745 u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
746
Phil Sutterf8edcd12016-08-23 13:14:31 +0200747 if (!user_features)
748 continue;
Daniel Borkmannc3a8d942015-08-31 15:58:47 +0200749 BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
750 if (nla_put_u32(skb, i + 1, user_features))
751 goto nla_put_failure;
Daniel Borkmannea697632015-01-05 23:57:47 +0100752 } else {
753 if (nla_put_u32(skb, i + 1, metrics[i]))
754 goto nla_put_failure;
755 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700756 valid++;
Thomas Graf2d7202b2006-08-22 00:01:27 -0700757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
David S. Millera57d27f2006-08-22 22:20:14 -0700760 if (!valid) {
761 nla_nest_cancel(skb, mx);
762 return 0;
763 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700764
765 return nla_nest_end(skb, mx);
766
767nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700768 nla_nest_cancel(skb, mx);
769 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800771EXPORT_SYMBOL(rtnetlink_put_metrics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Thomas Grafe3703b32006-11-27 09:27:07 -0800773int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
David S. Miller87a50692012-07-10 05:06:14 -0700774 long expires, u32 error)
Thomas Grafe3703b32006-11-27 09:27:07 -0800775{
776 struct rta_cacheinfo ci = {
Eric Dumazeta399a802012-08-08 21:13:53 +0000777 .rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse),
Thomas Grafe3703b32006-11-27 09:27:07 -0800778 .rta_used = dst->__use,
779 .rta_clntref = atomic_read(&(dst->__refcnt)),
780 .rta_error = error,
781 .rta_id = id,
Thomas Grafe3703b32006-11-27 09:27:07 -0800782 };
783
Li Wei82539472012-07-29 16:01:30 +0000784 if (expires) {
785 unsigned long clock;
Thomas Grafe3703b32006-11-27 09:27:07 -0800786
Li Wei82539472012-07-29 16:01:30 +0000787 clock = jiffies_to_clock_t(abs(expires));
788 clock = min_t(unsigned long, clock, INT_MAX);
789 ci.rta_expires = (expires > 0) ? clock : -clock;
790 }
Thomas Grafe3703b32006-11-27 09:27:07 -0800791 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
792}
Thomas Grafe3703b32006-11-27 09:27:07 -0800793EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
David S. Miller93b2d4a2008-02-17 18:35:07 -0800795static void set_operstate(struct net_device *dev, unsigned char transition)
Stefan Rompfb00055a2006-03-20 17:09:11 -0800796{
797 unsigned char operstate = dev->operstate;
798
Eric Dumazete0d087a2009-11-07 01:26:17 -0800799 switch (transition) {
Stefan Rompfb00055a2006-03-20 17:09:11 -0800800 case IF_OPER_UP:
801 if ((operstate == IF_OPER_DORMANT ||
802 operstate == IF_OPER_UNKNOWN) &&
803 !netif_dormant(dev))
804 operstate = IF_OPER_UP;
805 break;
806
807 case IF_OPER_DORMANT:
808 if (operstate == IF_OPER_UP ||
809 operstate == IF_OPER_UNKNOWN)
810 operstate = IF_OPER_DORMANT;
811 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700812 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800813
814 if (dev->operstate != operstate) {
815 write_lock_bh(&dev_base_lock);
816 dev->operstate = operstate;
817 write_unlock_bh(&dev_base_lock);
David S. Miller93b2d4a2008-02-17 18:35:07 -0800818 netdev_state_change(dev);
819 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800820}
821
Jiri Bencb1beb682012-07-27 02:58:22 +0000822static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
823{
824 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
825 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
826}
827
Patrick McHardy3729d502010-02-26 06:34:54 +0000828static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
829 const struct ifinfomsg *ifm)
830{
831 unsigned int flags = ifm->ifi_flags;
832
833 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
834 if (ifm->ifi_change)
835 flags = (flags & ifm->ifi_change) |
Jiri Bencb1beb682012-07-27 02:58:22 +0000836 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
Patrick McHardy3729d502010-02-26 06:34:54 +0000837
838 return flags;
839}
840
Thomas Grafb60c5112006-08-04 23:05:34 -0700841static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000842 const struct rtnl_link_stats64 *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
Thomas Grafb60c5112006-08-04 23:05:34 -0700844 a->rx_packets = b->rx_packets;
845 a->tx_packets = b->tx_packets;
846 a->rx_bytes = b->rx_bytes;
847 a->tx_bytes = b->tx_bytes;
848 a->rx_errors = b->rx_errors;
849 a->tx_errors = b->tx_errors;
850 a->rx_dropped = b->rx_dropped;
851 a->tx_dropped = b->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Thomas Grafb60c5112006-08-04 23:05:34 -0700853 a->multicast = b->multicast;
854 a->collisions = b->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Thomas Grafb60c5112006-08-04 23:05:34 -0700856 a->rx_length_errors = b->rx_length_errors;
857 a->rx_over_errors = b->rx_over_errors;
858 a->rx_crc_errors = b->rx_crc_errors;
859 a->rx_frame_errors = b->rx_frame_errors;
860 a->rx_fifo_errors = b->rx_fifo_errors;
861 a->rx_missed_errors = b->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Thomas Grafb60c5112006-08-04 23:05:34 -0700863 a->tx_aborted_errors = b->tx_aborted_errors;
864 a->tx_carrier_errors = b->tx_carrier_errors;
865 a->tx_fifo_errors = b->tx_fifo_errors;
866 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
867 a->tx_window_errors = b->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Thomas Grafb60c5112006-08-04 23:05:34 -0700869 a->rx_compressed = b->rx_compressed;
870 a->tx_compressed = b->tx_compressed;
Jarod Wilson6e7333d2016-02-01 18:51:05 -0500871
872 a->rx_nohandler = b->rx_nohandler;
Jan Engelhardt10708f32010-03-11 09:57:29 +0000873}
874
Chris Wrightc02db8c2010-05-16 01:05:45 -0700875/* All VF info */
Greg Rose115c9b82012-02-21 16:54:48 -0500876static inline int rtnl_vfinfo_size(const struct net_device *dev,
877 u32 ext_filter_mask)
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000878{
Phil Sutter9af15c32017-01-18 14:04:39 +0100879 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
Chris Wrightc02db8c2010-05-16 01:05:45 -0700880 int num_vfs = dev_num_vf(dev->dev.parent);
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100881 size_t size = nla_total_size(0);
Scott Feldman045de012010-05-28 03:42:43 -0700882 size += num_vfs *
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100883 (nla_total_size(0) +
884 nla_total_size(sizeof(struct ifla_vf_mac)) +
885 nla_total_size(sizeof(struct ifla_vf_vlan)) +
886 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
Moshe Shemesh79aab092016-09-22 12:11:15 +0300887 nla_total_size(MAX_VLAN_LIST_LEN *
888 sizeof(struct ifla_vf_vlan_info)) +
Sucheta Chakrabortyed616682014-05-22 09:59:05 -0400889 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100890 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
Jiri Benc945a3672014-08-08 16:44:32 +0200891 nla_total_size(sizeof(struct ifla_vf_rate)) +
Vlad Zolotarov01a3d792015-03-30 21:35:23 +0300892 nla_total_size(sizeof(struct ifla_vf_link_state)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300893 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100894 nla_total_size(0) + /* nest IFLA_VF_STATS */
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300895 /* IFLA_VF_STATS_RX_PACKETS */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200896 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300897 /* IFLA_VF_STATS_TX_PACKETS */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200898 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300899 /* IFLA_VF_STATS_RX_BYTES */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200900 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300901 /* IFLA_VF_STATS_TX_BYTES */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200902 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300903 /* IFLA_VF_STATS_BROADCAST */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200904 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300905 /* IFLA_VF_STATS_MULTICAST */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200906 nla_total_size_64bit(sizeof(__u64)) +
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +0000907 nla_total_size(sizeof(struct ifla_vf_trust)));
Chris Wrightc02db8c2010-05-16 01:05:45 -0700908 return size;
909 } else
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000910 return 0;
911}
912
David Gibsonc53864f2014-04-24 10:22:36 +1000913static size_t rtnl_port_size(const struct net_device *dev,
914 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -0700915{
916 size_t port_size = nla_total_size(4) /* PORT_VF */
917 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
Scott Feldman57b61082010-05-17 22:49:55 -0700918 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
919 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
920 + nla_total_size(1) /* PROT_VDP_REQUEST */
921 + nla_total_size(2); /* PORT_VDP_RESPONSE */
922 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
923 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
924 + port_size;
925 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
926 + port_size;
927
David Gibsonc53864f2014-04-24 10:22:36 +1000928 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
929 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -0700930 return 0;
931 if (dev_num_vf(dev->dev.parent))
932 return port_self_size + vf_ports_size +
933 vf_port_size * dev_num_vf(dev->dev.parent);
934 else
935 return port_self_size;
936}
937
David S. Millerb5cdae32017-04-18 15:36:58 -0400938static size_t rtnl_xdp_size(void)
Brenden Blancod1fdd912016-07-19 12:16:49 -0700939{
Sabrina Dubrocab3cfaa32016-11-15 11:16:35 +0100940 size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */
Martin KaFai Lau58038692017-06-15 17:29:09 -0700941 nla_total_size(1) + /* XDP_ATTACHED */
942 nla_total_size(4); /* XDP_PROG_ID */
Brenden Blancod1fdd912016-07-19 12:16:49 -0700943
David S. Millerb5cdae32017-04-18 15:36:58 -0400944 return xdp_size;
Brenden Blancod1fdd912016-07-19 12:16:49 -0700945}
946
Greg Rose115c9b82012-02-21 16:54:48 -0500947static noinline size_t if_nlmsg_size(const struct net_device *dev,
948 u32 ext_filter_mask)
Thomas Graf339bf982006-11-10 14:10:15 -0800949{
950 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
951 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700952 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
Thomas Graf339bf982006-11-10 14:10:15 -0800953 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
Nicolas Dichtel270cb4d2016-04-26 10:06:16 +0200954 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
Thomas Graf339bf982006-11-10 14:10:15 -0800955 + nla_total_size(sizeof(struct rtnl_link_stats))
David S. Miller35c58452016-04-19 19:49:29 -0400956 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
Thomas Graf339bf982006-11-10 14:10:15 -0800957 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
958 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
959 + nla_total_size(4) /* IFLA_TXQLEN */
960 + nla_total_size(4) /* IFLA_WEIGHT */
961 + nla_total_size(4) /* IFLA_MTU */
962 + nla_total_size(4) /* IFLA_LINK */
963 + nla_total_size(4) /* IFLA_MASTER */
Jiri Pirko9a572472012-12-27 23:49:39 +0000964 + nla_total_size(1) /* IFLA_CARRIER */
Ben Greearedbc0bb2012-03-29 12:51:30 +0000965 + nla_total_size(4) /* IFLA_PROMISCUITY */
Jiri Pirko76ff5cc2012-07-20 02:28:48 +0000966 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
967 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
Tobias Klauser69197562016-11-30 14:30:37 +0100968 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
969 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
Thomas Graf339bf982006-11-10 14:10:15 -0800970 + nla_total_size(1) /* IFLA_OPERSTATE */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700971 + nla_total_size(1) /* IFLA_LINKMODE */
david decotigny2d3b4792014-03-29 09:48:35 -0700972 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
Nicolas Dichteld37512a2015-01-15 15:11:16 +0100973 + nla_total_size(4) /* IFLA_LINK_NETNSID */
Serhey Popovychdb833d42017-06-20 14:35:23 +0300974 + nla_total_size(4) /* IFLA_GROUP */
Greg Rose115c9b82012-02-21 16:54:48 -0500975 + nla_total_size(ext_filter_mask
976 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
977 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
David Gibsonc53864f2014-04-24 10:22:36 +1000978 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
Thomas Graff8ff1822010-11-16 04:30:14 +0000979 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700980 + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
Jiri Pirko82f28412014-11-28 14:34:18 +0100981 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
Anuradha Karuppiah88d63782015-07-14 13:43:20 -0700982 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
Nicolas Dichtelc57c7a92016-03-31 18:10:31 +0200983 + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
David S. Millerb5cdae32017-04-18 15:36:58 -0400984 + rtnl_xdp_size() /* IFLA_XDP */
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -0400985 + nla_total_size(4) /* IFLA_EVENT */
Nicolas Dichtel6621dd22017-10-03 13:53:23 +0200986 + nla_total_size(4) /* IFLA_NEW_NETNSID */
Colin Ian King03ac7382017-11-06 15:04:54 +0000987 + nla_total_size(1) /* IFLA_PROTO_DOWN */
Jiri Benc79e1ad12017-11-02 17:04:38 -0200988 + nla_total_size(4) /* IFLA_IF_NETNSID */
989 + 0;
Thomas Graf339bf982006-11-10 14:10:15 -0800990}
991
Scott Feldman57b61082010-05-17 22:49:55 -0700992static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
993{
994 struct nlattr *vf_ports;
995 struct nlattr *vf_port;
996 int vf;
997 int err;
998
999 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
1000 if (!vf_ports)
1001 return -EMSGSIZE;
1002
1003 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
1004 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
Scott Feldman8ca94182010-05-28 03:42:18 -07001005 if (!vf_port)
1006 goto nla_put_failure;
David S. Millera6574342012-04-01 20:12:00 -04001007 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
1008 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001009 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
Scott Feldman8ca94182010-05-28 03:42:18 -07001010 if (err == -EMSGSIZE)
1011 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001012 if (err) {
Scott Feldman57b61082010-05-17 22:49:55 -07001013 nla_nest_cancel(skb, vf_port);
1014 continue;
1015 }
1016 nla_nest_end(skb, vf_port);
1017 }
1018
1019 nla_nest_end(skb, vf_ports);
1020
1021 return 0;
Scott Feldman8ca94182010-05-28 03:42:18 -07001022
1023nla_put_failure:
1024 nla_nest_cancel(skb, vf_ports);
1025 return -EMSGSIZE;
Scott Feldman57b61082010-05-17 22:49:55 -07001026}
1027
1028static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
1029{
1030 struct nlattr *port_self;
1031 int err;
1032
1033 port_self = nla_nest_start(skb, IFLA_PORT_SELF);
1034 if (!port_self)
1035 return -EMSGSIZE;
1036
1037 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
1038 if (err) {
1039 nla_nest_cancel(skb, port_self);
Scott Feldman8ca94182010-05-28 03:42:18 -07001040 return (err == -EMSGSIZE) ? err : 0;
Scott Feldman57b61082010-05-17 22:49:55 -07001041 }
1042
1043 nla_nest_end(skb, port_self);
1044
1045 return 0;
1046}
1047
David Gibsonc53864f2014-04-24 10:22:36 +10001048static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1049 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -07001050{
1051 int err;
1052
David Gibsonc53864f2014-04-24 10:22:36 +10001053 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1054 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -07001055 return 0;
1056
1057 err = rtnl_port_self_fill(skb, dev);
1058 if (err)
1059 return err;
1060
1061 if (dev_num_vf(dev->dev.parent)) {
1062 err = rtnl_vf_ports_fill(skb, dev);
1063 if (err)
1064 return err;
1065 }
1066
1067 return 0;
1068}
1069
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001070static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
1071{
1072 int err;
Jiri Pirko02637fc2014-11-28 14:34:16 +01001073 struct netdev_phys_item_id ppid;
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001074
1075 err = dev_get_phys_port_id(dev, &ppid);
1076 if (err) {
1077 if (err == -EOPNOTSUPP)
1078 return 0;
1079 return err;
1080 }
1081
1082 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1083 return -EMSGSIZE;
1084
1085 return 0;
1086}
1087
David Aherndb24a902015-03-17 20:23:15 -06001088static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1089{
1090 char name[IFNAMSIZ];
1091 int err;
1092
1093 err = dev_get_phys_port_name(dev, name, sizeof(name));
1094 if (err) {
1095 if (err == -EOPNOTSUPP)
1096 return 0;
1097 return err;
1098 }
1099
Michal Schmidt77ef0332017-05-04 16:48:58 +02001100 if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
David Aherndb24a902015-03-17 20:23:15 -06001101 return -EMSGSIZE;
1102
1103 return 0;
1104}
1105
Jiri Pirko82f28412014-11-28 14:34:18 +01001106static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1107{
1108 int err;
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001109 struct switchdev_attr attr = {
Ido Schimmel6ff64f62015-12-15 16:03:35 +01001110 .orig_dev = dev,
Jiri Pirko1f868392015-10-01 11:03:42 +02001111 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001112 .flags = SWITCHDEV_F_NO_RECURSE,
1113 };
Jiri Pirko82f28412014-11-28 14:34:18 +01001114
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001115 err = switchdev_port_attr_get(dev, &attr);
Jiri Pirko82f28412014-11-28 14:34:18 +01001116 if (err) {
1117 if (err == -EOPNOTSUPP)
1118 return 0;
1119 return err;
1120 }
1121
Scott Feldman42275bd2015-05-13 11:16:50 -07001122 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len,
1123 attr.u.ppid.id))
Jiri Pirko82f28412014-11-28 14:34:18 +01001124 return -EMSGSIZE;
1125
1126 return 0;
1127}
1128
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001129static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1130 struct net_device *dev)
1131{
Roopa Prabhu550bce52016-04-15 20:36:25 -07001132 struct rtnl_link_stats64 *sp;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001133 struct nlattr *attr;
1134
Nicolas Dichtel58414d32016-04-21 18:58:25 +02001135 attr = nla_reserve_64bit(skb, IFLA_STATS64,
1136 sizeof(struct rtnl_link_stats64), IFLA_PAD);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001137 if (!attr)
1138 return -EMSGSIZE;
1139
Roopa Prabhu550bce52016-04-15 20:36:25 -07001140 sp = nla_data(attr);
1141 dev_get_stats(dev, sp);
1142
1143 attr = nla_reserve(skb, IFLA_STATS,
1144 sizeof(struct rtnl_link_stats));
1145 if (!attr)
1146 return -EMSGSIZE;
1147
1148 copy_rtnl_link_stats(nla_data(attr), sp);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001149
1150 return 0;
1151}
1152
1153static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1154 struct net_device *dev,
1155 int vfs_num,
1156 struct nlattr *vfinfo)
1157{
1158 struct ifla_vf_rss_query_en vf_rss_query_en;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001159 struct nlattr *vf, *vfstats, *vfvlanlist;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001160 struct ifla_vf_link_state vf_linkstate;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001161 struct ifla_vf_vlan_info vf_vlan_info;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001162 struct ifla_vf_spoofchk vf_spoofchk;
1163 struct ifla_vf_tx_rate vf_tx_rate;
1164 struct ifla_vf_stats vf_stats;
1165 struct ifla_vf_trust vf_trust;
1166 struct ifla_vf_vlan vf_vlan;
1167 struct ifla_vf_rate vf_rate;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001168 struct ifla_vf_mac vf_mac;
1169 struct ifla_vf_info ivi;
1170
Mintz, Yuval0eed9cf2017-06-07 21:00:33 +03001171 memset(&ivi, 0, sizeof(ivi));
1172
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001173 /* Not all SR-IOV capable drivers support the
1174 * spoofcheck and "RSS query enable" query. Preset to
1175 * -1 so the user space tool can detect that the driver
1176 * didn't report anything.
1177 */
1178 ivi.spoofchk = -1;
1179 ivi.rss_query_en = -1;
1180 ivi.trusted = -1;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001181 /* The default value for VF link state is "auto"
1182 * IFLA_VF_LINK_STATE_AUTO which equals zero
1183 */
1184 ivi.linkstate = 0;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001185 /* VLAN Protocol by default is 802.1Q */
1186 ivi.vlan_proto = htons(ETH_P_8021Q);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001187 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1188 return 0;
1189
Dan Carpenter775f4f02016-10-13 11:45:28 +03001190 memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
1191
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001192 vf_mac.vf =
1193 vf_vlan.vf =
Moshe Shemesh79aab092016-09-22 12:11:15 +03001194 vf_vlan_info.vf =
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001195 vf_rate.vf =
1196 vf_tx_rate.vf =
1197 vf_spoofchk.vf =
1198 vf_linkstate.vf =
1199 vf_rss_query_en.vf =
1200 vf_trust.vf = ivi.vf;
1201
1202 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1203 vf_vlan.vlan = ivi.vlan;
1204 vf_vlan.qos = ivi.qos;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001205 vf_vlan_info.vlan = ivi.vlan;
1206 vf_vlan_info.qos = ivi.qos;
1207 vf_vlan_info.vlan_proto = ivi.vlan_proto;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001208 vf_tx_rate.rate = ivi.max_tx_rate;
1209 vf_rate.min_tx_rate = ivi.min_tx_rate;
1210 vf_rate.max_tx_rate = ivi.max_tx_rate;
1211 vf_spoofchk.setting = ivi.spoofchk;
1212 vf_linkstate.link_state = ivi.linkstate;
1213 vf_rss_query_en.setting = ivi.rss_query_en;
1214 vf_trust.setting = ivi.trusted;
1215 vf = nla_nest_start(skb, IFLA_VF_INFO);
Moshe Shemesh79aab092016-09-22 12:11:15 +03001216 if (!vf)
1217 goto nla_put_vfinfo_failure;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001218 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1219 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1220 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1221 &vf_rate) ||
1222 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1223 &vf_tx_rate) ||
1224 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1225 &vf_spoofchk) ||
1226 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1227 &vf_linkstate) ||
1228 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1229 sizeof(vf_rss_query_en),
1230 &vf_rss_query_en) ||
1231 nla_put(skb, IFLA_VF_TRUST,
1232 sizeof(vf_trust), &vf_trust))
Moshe Shemesh79aab092016-09-22 12:11:15 +03001233 goto nla_put_vf_failure;
1234 vfvlanlist = nla_nest_start(skb, IFLA_VF_VLAN_LIST);
1235 if (!vfvlanlist)
1236 goto nla_put_vf_failure;
1237 if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
1238 &vf_vlan_info)) {
1239 nla_nest_cancel(skb, vfvlanlist);
1240 goto nla_put_vf_failure;
1241 }
1242 nla_nest_end(skb, vfvlanlist);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001243 memset(&vf_stats, 0, sizeof(vf_stats));
1244 if (dev->netdev_ops->ndo_get_vf_stats)
1245 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1246 &vf_stats);
1247 vfstats = nla_nest_start(skb, IFLA_VF_STATS);
Moshe Shemesh79aab092016-09-22 12:11:15 +03001248 if (!vfstats)
1249 goto nla_put_vf_failure;
Nicolas Dichtel343a6d82016-04-25 10:25:14 +02001250 if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1251 vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1252 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1253 vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1254 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1255 vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1256 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1257 vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1258 nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1259 vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1260 nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
Moshe Shemesh79aab092016-09-22 12:11:15 +03001261 vf_stats.multicast, IFLA_VF_STATS_PAD)) {
1262 nla_nest_cancel(skb, vfstats);
1263 goto nla_put_vf_failure;
1264 }
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001265 nla_nest_end(skb, vfstats);
1266 nla_nest_end(skb, vf);
1267 return 0;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001268
1269nla_put_vf_failure:
1270 nla_nest_cancel(skb, vf);
1271nla_put_vfinfo_failure:
1272 nla_nest_cancel(skb, vfinfo);
1273 return -EMSGSIZE;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001274}
1275
Florian Westphal250fc3d2017-09-26 13:58:41 +02001276static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb,
1277 struct net_device *dev,
1278 u32 ext_filter_mask)
1279{
1280 struct nlattr *vfinfo;
1281 int i, num_vfs;
1282
1283 if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0))
1284 return 0;
1285
1286 num_vfs = dev_num_vf(dev->dev.parent);
1287 if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs))
1288 return -EMSGSIZE;
1289
1290 if (!dev->netdev_ops->ndo_get_vf_config)
1291 return 0;
1292
1293 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1294 if (!vfinfo)
1295 return -EMSGSIZE;
1296
1297 for (i = 0; i < num_vfs; i++) {
1298 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1299 return -EMSGSIZE;
1300 }
1301
1302 nla_nest_end(skb, vfinfo);
1303 return 0;
1304}
1305
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001306static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1307{
Kangjie Lu5f8e4472016-05-03 16:46:24 -04001308 struct rtnl_link_ifmap map;
1309
1310 memset(&map, 0, sizeof(map));
1311 map.mem_start = dev->mem_start;
1312 map.mem_end = dev->mem_end;
1313 map.base_addr = dev->base_addr;
1314 map.irq = dev->irq;
1315 map.dma = dev->dma;
1316 map.port = dev->if_port;
1317
Nicolas Dichtel270cb4d2016-04-26 10:06:16 +02001318 if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001319 return -EMSGSIZE;
1320
1321 return 0;
1322}
1323
Martin KaFai Lau58038692017-06-15 17:29:09 -07001324static u8 rtnl_xdp_attached_mode(struct net_device *dev, u32 *prog_id)
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001325{
1326 const struct net_device_ops *ops = dev->netdev_ops;
Martin KaFai Lau58038692017-06-15 17:29:09 -07001327 const struct bpf_prog *generic_xdp_prog;
Jakub Kicinski118b4aa2017-12-01 15:08:55 -08001328 struct netdev_bpf xdp;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001329
1330 ASSERT_RTNL();
1331
Martin KaFai Lau58038692017-06-15 17:29:09 -07001332 *prog_id = 0;
1333 generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
1334 if (generic_xdp_prog) {
1335 *prog_id = generic_xdp_prog->aux->id;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001336 return XDP_ATTACHED_SKB;
Martin KaFai Lau58038692017-06-15 17:29:09 -07001337 }
Jakub Kicinskif4e63522017-11-03 13:56:16 -07001338 if (!ops->ndo_bpf)
Jakub Kicinskice158e52017-06-21 18:25:09 -07001339 return XDP_ATTACHED_NONE;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001340
Jakub Kicinski118b4aa2017-12-01 15:08:55 -08001341 __dev_xdp_query(dev, ops->ndo_bpf, &xdp);
1342 *prog_id = xdp.prog_id;
1343
1344 return xdp.prog_attached;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001345}
1346
Brenden Blancod1fdd912016-07-19 12:16:49 -07001347static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1348{
Brenden Blancod1fdd912016-07-19 12:16:49 -07001349 struct nlattr *xdp;
Martin KaFai Lau58038692017-06-15 17:29:09 -07001350 u32 prog_id;
Brenden Blancod1fdd912016-07-19 12:16:49 -07001351 int err;
1352
Brenden Blancod1fdd912016-07-19 12:16:49 -07001353 xdp = nla_nest_start(skb, IFLA_XDP);
1354 if (!xdp)
1355 return -EMSGSIZE;
David S. Millerb5cdae32017-04-18 15:36:58 -04001356
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001357 err = nla_put_u8(skb, IFLA_XDP_ATTACHED,
Martin KaFai Lau58038692017-06-15 17:29:09 -07001358 rtnl_xdp_attached_mode(dev, &prog_id));
Brenden Blancod1fdd912016-07-19 12:16:49 -07001359 if (err)
1360 goto err_cancel;
1361
Martin KaFai Lau58038692017-06-15 17:29:09 -07001362 if (prog_id) {
1363 err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
1364 if (err)
1365 goto err_cancel;
1366 }
1367
Brenden Blancod1fdd912016-07-19 12:16:49 -07001368 nla_nest_end(skb, xdp);
1369 return 0;
1370
1371err_cancel:
1372 nla_nest_cancel(skb, xdp);
1373 return err;
1374}
1375
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001376static u32 rtnl_get_event(unsigned long event)
1377{
1378 u32 rtnl_event_type = IFLA_EVENT_NONE;
1379
1380 switch (event) {
1381 case NETDEV_REBOOT:
1382 rtnl_event_type = IFLA_EVENT_REBOOT;
1383 break;
1384 case NETDEV_FEAT_CHANGE:
1385 rtnl_event_type = IFLA_EVENT_FEATURES;
1386 break;
1387 case NETDEV_BONDING_FAILOVER:
1388 rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
1389 break;
1390 case NETDEV_NOTIFY_PEERS:
1391 rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
1392 break;
1393 case NETDEV_RESEND_IGMP:
1394 rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
1395 break;
1396 case NETDEV_CHANGEINFODATA:
1397 rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
1398 break;
1399 default:
1400 break;
1401 }
1402
1403 return rtnl_event_type;
1404}
1405
Florian Westphal79110a02017-09-26 13:58:40 +02001406static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
1407{
1408 const struct net_device *upper_dev;
1409 int ret = 0;
1410
1411 rcu_read_lock();
1412
1413 upper_dev = netdev_master_upper_dev_get_rcu(dev);
1414 if (upper_dev)
1415 ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);
1416
1417 rcu_read_unlock();
1418 return ret;
1419}
1420
1421static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev)
1422{
1423 int ifindex = dev_get_iflink(dev);
1424
1425 if (dev->ifindex == ifindex)
1426 return 0;
1427
1428 return nla_put_u32(skb, IFLA_LINK, ifindex);
1429}
1430
Florian Westphal6c557002017-10-02 23:50:05 +02001431static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
1432 struct net_device *dev)
1433{
1434 char buf[IFALIASZ];
1435 int ret;
1436
1437 ret = dev_get_alias(dev, buf, sizeof(buf));
1438 return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
1439}
1440
Florian Westphalb1e66b92017-09-26 13:58:42 +02001441static int rtnl_fill_link_netnsid(struct sk_buff *skb,
Jiri Benc79e1ad12017-11-02 17:04:38 -02001442 const struct net_device *dev,
1443 struct net *src_net)
Florian Westphalb1e66b92017-09-26 13:58:42 +02001444{
1445 if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
1446 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1447
1448 if (!net_eq(dev_net(dev), link_net)) {
Jiri Benc79e1ad12017-11-02 17:04:38 -02001449 int id = peernet2id_alloc(src_net, link_net);
Florian Westphalb1e66b92017-09-26 13:58:42 +02001450
1451 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1452 return -EMSGSIZE;
1453 }
1454 }
1455
1456 return 0;
1457}
1458
Florian Westphal070cbf5b2017-10-16 15:44:35 +02001459static int rtnl_fill_link_af(struct sk_buff *skb,
1460 const struct net_device *dev,
1461 u32 ext_filter_mask)
1462{
1463 const struct rtnl_af_ops *af_ops;
1464 struct nlattr *af_spec;
1465
1466 af_spec = nla_nest_start(skb, IFLA_AF_SPEC);
1467 if (!af_spec)
1468 return -EMSGSIZE;
1469
Florian Westphal5fa85a02017-10-16 15:44:36 +02001470 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Florian Westphal070cbf5b2017-10-16 15:44:35 +02001471 struct nlattr *af;
1472 int err;
1473
1474 if (!af_ops->fill_link_af)
1475 continue;
1476
1477 af = nla_nest_start(skb, af_ops->family);
1478 if (!af)
1479 return -EMSGSIZE;
1480
1481 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1482 /*
1483 * Caller may return ENODATA to indicate that there
1484 * was no data to be dumped. This is not an error, it
1485 * means we should trim the attribute header and
1486 * continue.
1487 */
1488 if (err == -ENODATA)
1489 nla_nest_cancel(skb, af);
1490 else if (err < 0)
1491 return -EMSGSIZE;
1492
1493 nla_nest_end(skb, af);
1494 }
1495
1496 nla_nest_end(skb, af_spec);
1497 return 0;
1498}
1499
Jiri Benc79e1ad12017-11-02 17:04:38 -02001500static int rtnl_fill_ifinfo(struct sk_buff *skb,
1501 struct net_device *dev, struct net *src_net,
Patrick McHardy575c3e22007-05-22 17:00:49 -07001502 int type, u32 pid, u32 seq, u32 change,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001503 unsigned int flags, u32 ext_filter_mask,
Jiri Benc79e1ad12017-11-02 17:04:38 -02001504 u32 event, int *new_nsid, int tgt_netnsid)
Thomas Grafb60c5112006-08-04 23:05:34 -07001505{
1506 struct ifinfomsg *ifm;
1507 struct nlmsghdr *nlh;
1508
Eric Dumazet2907c352011-05-25 07:34:04 +00001509 ASSERT_RTNL();
Thomas Grafb60c5112006-08-04 23:05:34 -07001510 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1511 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08001512 return -EMSGSIZE;
Thomas Grafb60c5112006-08-04 23:05:34 -07001513
1514 ifm = nlmsg_data(nlh);
1515 ifm->ifi_family = AF_UNSPEC;
1516 ifm->__ifi_pad = 0;
1517 ifm->ifi_type = dev->type;
1518 ifm->ifi_index = dev->ifindex;
1519 ifm->ifi_flags = dev_get_flags(dev);
1520 ifm->ifi_change = change;
1521
Jiri Benc79e1ad12017-11-02 17:04:38 -02001522 if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_IF_NETNSID, tgt_netnsid))
1523 goto nla_put_failure;
1524
David S. Millera6574342012-04-01 20:12:00 -04001525 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1526 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1527 nla_put_u8(skb, IFLA_OPERSTATE,
1528 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1529 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1530 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1531 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
Ben Greearedbc0bb2012-03-29 12:51:30 +00001532 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001533 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
Eric Dumazetc70ce022016-03-21 09:55:10 -07001534 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1535 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001536#ifdef CONFIG_RPS
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001537 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001538#endif
Florian Westphal79110a02017-09-26 13:58:40 +02001539 nla_put_iflink(skb, dev) ||
1540 put_master_ifindex(skb, dev) ||
Jiri Pirko9a572472012-12-27 23:49:39 +00001541 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
David S. Millera6574342012-04-01 20:12:00 -04001542 (dev->qdisc &&
1543 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
Florian Westphal6c557002017-10-02 23:50:05 +02001544 nla_put_ifalias(skb, dev) ||
david decotigny2d3b4792014-03-29 09:48:35 -07001545 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001546 atomic_read(&dev->carrier_changes)) ||
1547 nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
David S. Millera6574342012-04-01 20:12:00 -04001548 goto nla_put_failure;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001549
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001550 if (event != IFLA_EVENT_NONE) {
1551 if (nla_put_u32(skb, IFLA_EVENT, event))
1552 goto nla_put_failure;
1553 }
1554
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001555 if (rtnl_fill_link_ifmap(skb, dev))
1556 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558 if (dev->addr_len) {
David S. Millera6574342012-04-01 20:12:00 -04001559 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1560 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1561 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 }
1563
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001564 if (rtnl_phys_port_id_fill(skb, dev))
1565 goto nla_put_failure;
1566
David Aherndb24a902015-03-17 20:23:15 -06001567 if (rtnl_phys_port_name_fill(skb, dev))
1568 goto nla_put_failure;
1569
Jiri Pirko82f28412014-11-28 14:34:18 +01001570 if (rtnl_phys_switch_id_fill(skb, dev))
1571 goto nla_put_failure;
1572
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001573 if (rtnl_fill_stats(skb, dev))
Pavel Emelyanov96e74082008-05-21 14:12:46 -07001574 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
Florian Westphal250fc3d2017-09-26 13:58:41 +02001576 if (rtnl_fill_vf(skb, dev, ext_filter_mask))
David S. Millera6574342012-04-01 20:12:00 -04001577 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001578
David Gibsonc53864f2014-04-24 10:22:36 +10001579 if (rtnl_port_fill(skb, dev, ext_filter_mask))
Scott Feldman57b61082010-05-17 22:49:55 -07001580 goto nla_put_failure;
1581
Brenden Blancod1fdd912016-07-19 12:16:49 -07001582 if (rtnl_xdp_fill(skb, dev))
1583 goto nla_put_failure;
1584
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001585 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07001586 if (rtnl_link_fill(skb, dev) < 0)
1587 goto nla_put_failure;
1588 }
1589
Jiri Benc79e1ad12017-11-02 17:04:38 -02001590 if (rtnl_fill_link_netnsid(skb, dev, src_net))
Florian Westphalb1e66b92017-09-26 13:58:42 +02001591 goto nla_put_failure;
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001592
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02001593 if (new_nsid &&
1594 nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0)
1595 goto nla_put_failure;
1596
Florian Westphal5fa85a02017-10-16 15:44:36 +02001597 rcu_read_lock();
Florian Westphal070cbf5b2017-10-16 15:44:35 +02001598 if (rtnl_fill_link_af(skb, dev, ext_filter_mask))
Florian Westphal5fa85a02017-10-16 15:44:36 +02001599 goto nla_put_failure_rcu;
1600 rcu_read_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +00001601
Johannes Berg053c0952015-01-16 22:09:00 +01001602 nlmsg_end(skb, nlh);
1603 return 0;
Thomas Grafb60c5112006-08-04 23:05:34 -07001604
Florian Westphal5fa85a02017-10-16 15:44:36 +02001605nla_put_failure_rcu:
1606 rcu_read_unlock();
Thomas Grafb60c5112006-08-04 23:05:34 -07001607nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001608 nlmsg_cancel(skb, nlh);
1609 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610}
1611
Jiri Pirkof7b12602014-02-18 20:53:18 +01001612static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1613 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1614 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1615 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1616 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1617 [IFLA_MTU] = { .type = NLA_U32 },
1618 [IFLA_LINK] = { .type = NLA_U32 },
1619 [IFLA_MASTER] = { .type = NLA_U32 },
1620 [IFLA_CARRIER] = { .type = NLA_U8 },
1621 [IFLA_TXQLEN] = { .type = NLA_U32 },
1622 [IFLA_WEIGHT] = { .type = NLA_U32 },
1623 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1624 [IFLA_LINKMODE] = { .type = NLA_U8 },
1625 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1626 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1627 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
Nicolas Dichtel2459b4c2017-10-11 16:24:48 +02001628 /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
1629 * allow 0-length string (needed to remove an alias).
1630 */
1631 [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001632 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1633 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1634 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1635 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1636 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1637 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1638 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1639 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
Jiri Pirko02637fc2014-11-28 14:34:16 +01001640 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
david decotigny2d3b4792014-03-29 09:48:35 -07001641 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
Jiri Pirko82f28412014-11-28 14:34:18 +01001642 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
Nicolas Dichtel317f4812015-01-15 15:11:18 +01001643 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001644 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001645 [IFLA_XDP] = { .type = NLA_NESTED },
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001646 [IFLA_EVENT] = { .type = NLA_U32 },
Serhey Popovychdb833d42017-06-20 14:35:23 +03001647 [IFLA_GROUP] = { .type = NLA_U32 },
Jiri Benc79e1ad12017-11-02 17:04:38 -02001648 [IFLA_IF_NETNSID] = { .type = NLA_S32 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001649};
1650
1651static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1652 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1653 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1654 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1655 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1656};
1657
Jiri Pirkof7b12602014-02-18 20:53:18 +01001658static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
Daniel Borkmann364d5712015-02-05 18:44:04 +01001659 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1660 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
Moshe Shemesh79aab092016-09-22 12:11:15 +03001661 [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED },
Daniel Borkmann364d5712015-02-05 18:44:04 +01001662 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1663 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1664 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1665 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001666 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001667 [IFLA_VF_STATS] = { .type = NLA_NESTED },
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00001668 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
Eli Cohencc8e27c2016-03-11 22:58:34 +02001669 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1670 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001671};
1672
Jiri Pirkof7b12602014-02-18 20:53:18 +01001673static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1674 [IFLA_PORT_VF] = { .type = NLA_U32 },
1675 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1676 .len = PORT_PROFILE_MAX },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001677 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1678 .len = PORT_UUID_MAX },
1679 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1680 .len = PORT_UUID_MAX },
1681 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1682 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
Daniel Borkmann025331d2017-02-17 01:56:11 +01001683
1684 /* Unused, but we need to keep it here since user space could
1685 * fill it. It's also broken with regard to NLA_BINARY use in
1686 * combination with structs.
1687 */
1688 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1689 .len = sizeof(struct ifla_port_vsi) },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001690};
1691
Brenden Blancod1fdd912016-07-19 12:16:49 -07001692static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
1693 [IFLA_XDP_FD] = { .type = NLA_S32 },
1694 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 },
Daniel Borkmann85de8572016-11-28 23:16:54 +01001695 [IFLA_XDP_FLAGS] = { .type = NLA_U32 },
Martin KaFai Lau58038692017-06-15 17:29:09 -07001696 [IFLA_XDP_PROG_ID] = { .type = NLA_U32 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001697};
1698
David Aherndc599f72016-02-02 08:17:07 -08001699static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1700{
1701 const struct rtnl_link_ops *ops = NULL;
1702 struct nlattr *linfo[IFLA_INFO_MAX + 1];
1703
Johannes Bergfceb6432017-04-12 14:34:07 +02001704 if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla,
1705 ifla_info_policy, NULL) < 0)
David Aherndc599f72016-02-02 08:17:07 -08001706 return NULL;
1707
1708 if (linfo[IFLA_INFO_KIND]) {
1709 char kind[MODULE_NAME_LEN];
1710
1711 nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1712 ops = rtnl_link_ops_get(kind);
1713 }
1714
1715 return ops;
1716}
1717
1718static bool link_master_filtered(struct net_device *dev, int master_idx)
1719{
1720 struct net_device *master;
1721
1722 if (!master_idx)
1723 return false;
1724
1725 master = netdev_master_upper_dev_get(dev);
1726 if (!master || master->ifindex != master_idx)
1727 return true;
1728
1729 return false;
1730}
1731
1732static bool link_kind_filtered(const struct net_device *dev,
1733 const struct rtnl_link_ops *kind_ops)
1734{
1735 if (kind_ops && dev->rtnl_link_ops != kind_ops)
1736 return true;
1737
1738 return false;
1739}
1740
1741static bool link_dump_filtered(struct net_device *dev,
1742 int master_idx,
1743 const struct rtnl_link_ops *kind_ops)
1744{
1745 if (link_master_filtered(dev, master_idx) ||
1746 link_kind_filtered(dev, kind_ops))
1747 return true;
1748
1749 return false;
1750}
1751
Jiri Benc79e1ad12017-11-02 17:04:38 -02001752static struct net *get_target_net(struct sk_buff *skb, int netnsid)
1753{
1754 struct net *net;
1755
1756 net = get_net_ns_by_id(sock_net(skb->sk), netnsid);
1757 if (!net)
1758 return ERR_PTR(-EINVAL);
1759
1760 /* For now, the caller is required to have CAP_NET_ADMIN in
1761 * the user namespace owning the target net ns.
1762 */
1763 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
1764 put_net(net);
1765 return ERR_PTR(-EACCES);
1766 }
1767 return net;
1768}
1769
Thomas Grafb60c5112006-08-04 23:05:34 -07001770static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001772 struct net *net = sock_net(skb->sk);
Jiri Benc79e1ad12017-11-02 17:04:38 -02001773 struct net *tgt_net = net;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001774 int h, s_h;
1775 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 struct net_device *dev;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001777 struct hlist_head *head;
Greg Rose115c9b82012-02-21 16:54:48 -05001778 struct nlattr *tb[IFLA_MAX+1];
1779 u32 ext_filter_mask = 0;
David Aherndc599f72016-02-02 08:17:07 -08001780 const struct rtnl_link_ops *kind_ops = NULL;
1781 unsigned int flags = NLM_F_MULTI;
1782 int master_idx = 0;
Jiri Benc79e1ad12017-11-02 17:04:38 -02001783 int netnsid = -1;
David Gibson973462b2014-04-24 10:22:35 +10001784 int err;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001785 int hdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001787 s_h = cb->args[0];
1788 s_idx = cb->args[1];
1789
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001790 /* A hack to preserve kernel<->userspace interface.
1791 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1792 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1793 * what iproute2 < v3.9.0 used.
1794 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1795 * attribute, its netlink message is shorter than struct ifinfomsg.
1796 */
1797 hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1798 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1799
Johannes Bergfceb6432017-04-12 14:34:07 +02001800 if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX,
1801 ifla_policy, NULL) >= 0) {
Jiri Benc79e1ad12017-11-02 17:04:38 -02001802 if (tb[IFLA_IF_NETNSID]) {
1803 netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]);
1804 tgt_net = get_target_net(skb, netnsid);
1805 if (IS_ERR(tgt_net)) {
1806 tgt_net = net;
1807 netnsid = -1;
1808 }
1809 }
1810
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001811 if (tb[IFLA_EXT_MASK])
1812 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
David Aherndc599f72016-02-02 08:17:07 -08001813
1814 if (tb[IFLA_MASTER])
1815 master_idx = nla_get_u32(tb[IFLA_MASTER]);
1816
1817 if (tb[IFLA_LINKINFO])
1818 kind_ops = linkinfo_to_kind_ops(tb[IFLA_LINKINFO]);
1819
1820 if (master_idx || kind_ops)
1821 flags |= NLM_F_DUMP_FILTERED;
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001822 }
Greg Rose115c9b82012-02-21 16:54:48 -05001823
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001824 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1825 idx = 0;
Jiri Benc79e1ad12017-11-02 17:04:38 -02001826 head = &tgt_net->dev_index_head[h];
Eric Dumazetcac5e652015-02-27 09:42:50 -08001827 hlist_for_each_entry(dev, head, index_hlist) {
David Aherndc599f72016-02-02 08:17:07 -08001828 if (link_dump_filtered(dev, master_idx, kind_ops))
Zhang Shengju3f0ae052016-11-19 23:28:32 +08001829 goto cont;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001830 if (idx < s_idx)
1831 goto cont;
Jiri Benc79e1ad12017-11-02 17:04:38 -02001832 err = rtnl_fill_ifinfo(skb, dev, net,
1833 RTM_NEWLINK,
David Gibson973462b2014-04-24 10:22:35 +10001834 NETLINK_CB(cb->skb).portid,
1835 cb->nlh->nlmsg_seq, 0,
David Aherndc599f72016-02-02 08:17:07 -08001836 flags,
Jiri Benc79e1ad12017-11-02 17:04:38 -02001837 ext_filter_mask, 0, NULL,
1838 netnsid);
David Gibson973462b2014-04-24 10:22:35 +10001839
David Ahernf6c57752017-05-15 23:19:17 -07001840 if (err < 0) {
1841 if (likely(skb->len))
1842 goto out;
1843
1844 goto out_err;
1845 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07001846cont:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001847 idx++;
1848 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 }
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001850out:
David Ahernf6c57752017-05-15 23:19:17 -07001851 err = skb->len;
1852out_err:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001853 cb->args[1] = idx;
1854 cb->args[0] = h;
Jakub Sitnickid0225782017-08-09 17:39:12 +02001855 cb->seq = net->dev_base_seq;
1856 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Jiri Benc79e1ad12017-11-02 17:04:38 -02001857 if (netnsid >= 0)
1858 put_net(tgt_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
David Ahernf6c57752017-05-15 23:19:17 -07001860 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861}
1862
Johannes Bergfceb6432017-04-12 14:34:07 +02001863int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
1864 struct netlink_ext_ack *exterr)
Jiri Pirkof7b12602014-02-18 20:53:18 +01001865{
Johannes Bergfceb6432017-04-12 14:34:07 +02001866 return nla_parse(tb, IFLA_MAX, head, len, ifla_policy, exterr);
Jiri Pirkof7b12602014-02-18 20:53:18 +01001867}
1868EXPORT_SYMBOL(rtnl_nla_parse_ifla);
Scott Feldman57b61082010-05-17 22:49:55 -07001869
Eric W. Biederman81adee42009-11-08 00:53:51 -08001870struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1871{
1872 struct net *net;
1873 /* Examine the link attributes and figure out which
1874 * network namespace we are talking about.
1875 */
1876 if (tb[IFLA_NET_NS_PID])
1877 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001878 else if (tb[IFLA_NET_NS_FD])
1879 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
Eric W. Biederman81adee42009-11-08 00:53:51 -08001880 else
1881 net = get_net(src_net);
1882 return net;
1883}
1884EXPORT_SYMBOL(rtnl_link_get_net);
1885
Thomas Graf1840bb12008-02-23 19:54:36 -08001886static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1887{
1888 if (dev) {
1889 if (tb[IFLA_ADDRESS] &&
1890 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1891 return -EINVAL;
1892
1893 if (tb[IFLA_BROADCAST] &&
1894 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1895 return -EINVAL;
1896 }
1897
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001898 if (tb[IFLA_AF_SPEC]) {
1899 struct nlattr *af;
1900 int rem, err;
1901
1902 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1903 const struct rtnl_af_ops *af_ops;
1904
Florian Westphal5fa85a02017-10-16 15:44:36 +02001905 rcu_read_lock();
1906 af_ops = rtnl_af_lookup(nla_type(af));
1907 if (!af_ops) {
1908 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001909 return -EAFNOSUPPORT;
Florian Westphal5fa85a02017-10-16 15:44:36 +02001910 }
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001911
Florian Westphal5fa85a02017-10-16 15:44:36 +02001912 if (!af_ops->set_link_af) {
1913 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001914 return -EOPNOTSUPP;
Florian Westphal5fa85a02017-10-16 15:44:36 +02001915 }
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001916
1917 if (af_ops->validate_link_af) {
Kurt Van Dijck6d3a9a62011-01-26 04:55:24 +00001918 err = af_ops->validate_link_af(dev, af);
Florian Westphal5fa85a02017-10-16 15:44:36 +02001919 if (err < 0) {
1920 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001921 return err;
Florian Westphal5fa85a02017-10-16 15:44:36 +02001922 }
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001923 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02001924
1925 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001926 }
1927 }
1928
Thomas Graf1840bb12008-02-23 19:54:36 -08001929 return 0;
1930}
1931
Eli Cohencc8e27c2016-03-11 22:58:34 +02001932static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
1933 int guid_type)
1934{
1935 const struct net_device_ops *ops = dev->netdev_ops;
1936
1937 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
1938}
1939
1940static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
1941{
1942 if (dev->type != ARPHRD_INFINIBAND)
1943 return -EOPNOTSUPP;
1944
1945 return handle_infiniband_guid(dev, ivt, guid_type);
1946}
1947
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001948static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
Chris Wrightc02db8c2010-05-16 01:05:45 -07001949{
Chris Wrightc02db8c2010-05-16 01:05:45 -07001950 const struct net_device_ops *ops = dev->netdev_ops;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001951 int err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001952
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001953 if (tb[IFLA_VF_MAC]) {
1954 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001955
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001956 err = -EOPNOTSUPP;
1957 if (ops->ndo_set_vf_mac)
1958 err = ops->ndo_set_vf_mac(dev, ivm->vf,
1959 ivm->mac);
1960 if (err < 0)
1961 return err;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001962 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001963
1964 if (tb[IFLA_VF_VLAN]) {
1965 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
1966
1967 err = -EOPNOTSUPP;
1968 if (ops->ndo_set_vf_vlan)
1969 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
Moshe Shemesh79aab092016-09-22 12:11:15 +03001970 ivv->qos,
1971 htons(ETH_P_8021Q));
1972 if (err < 0)
1973 return err;
1974 }
1975
1976 if (tb[IFLA_VF_VLAN_LIST]) {
1977 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
1978 struct nlattr *attr;
1979 int rem, len = 0;
1980
1981 err = -EOPNOTSUPP;
1982 if (!ops->ndo_set_vf_vlan)
1983 return err;
1984
1985 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
1986 if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
1987 nla_len(attr) < NLA_HDRLEN) {
1988 return -EINVAL;
1989 }
1990 if (len >= MAX_VLAN_LIST_LEN)
1991 return -EOPNOTSUPP;
1992 ivvl[len] = nla_data(attr);
1993
1994 len++;
1995 }
Arnd Bergmannfa34cd92016-09-30 18:13:49 +02001996 if (len == 0)
1997 return -EINVAL;
1998
Moshe Shemesh79aab092016-09-22 12:11:15 +03001999 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
2000 ivvl[0]->qos, ivvl[0]->vlan_proto);
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002001 if (err < 0)
2002 return err;
2003 }
2004
2005 if (tb[IFLA_VF_TX_RATE]) {
2006 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
2007 struct ifla_vf_info ivf;
2008
2009 err = -EOPNOTSUPP;
2010 if (ops->ndo_get_vf_config)
2011 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
2012 if (err < 0)
2013 return err;
2014
2015 err = -EOPNOTSUPP;
2016 if (ops->ndo_set_vf_rate)
2017 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2018 ivf.min_tx_rate,
2019 ivt->rate);
2020 if (err < 0)
2021 return err;
2022 }
2023
2024 if (tb[IFLA_VF_RATE]) {
2025 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
2026
2027 err = -EOPNOTSUPP;
2028 if (ops->ndo_set_vf_rate)
2029 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2030 ivt->min_tx_rate,
2031 ivt->max_tx_rate);
2032 if (err < 0)
2033 return err;
2034 }
2035
2036 if (tb[IFLA_VF_SPOOFCHK]) {
2037 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
2038
2039 err = -EOPNOTSUPP;
2040 if (ops->ndo_set_vf_spoofchk)
2041 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
2042 ivs->setting);
2043 if (err < 0)
2044 return err;
2045 }
2046
2047 if (tb[IFLA_VF_LINK_STATE]) {
2048 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
2049
2050 err = -EOPNOTSUPP;
2051 if (ops->ndo_set_vf_link_state)
2052 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
2053 ivl->link_state);
2054 if (err < 0)
2055 return err;
2056 }
2057
2058 if (tb[IFLA_VF_RSS_QUERY_EN]) {
2059 struct ifla_vf_rss_query_en *ivrssq_en;
2060
2061 err = -EOPNOTSUPP;
2062 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
2063 if (ops->ndo_set_vf_rss_query_en)
2064 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
2065 ivrssq_en->setting);
2066 if (err < 0)
2067 return err;
2068 }
2069
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00002070 if (tb[IFLA_VF_TRUST]) {
2071 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
2072
2073 err = -EOPNOTSUPP;
2074 if (ops->ndo_set_vf_trust)
2075 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
2076 if (err < 0)
2077 return err;
2078 }
2079
Eli Cohencc8e27c2016-03-11 22:58:34 +02002080 if (tb[IFLA_VF_IB_NODE_GUID]) {
2081 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
2082
2083 if (!ops->ndo_set_vf_guid)
2084 return -EOPNOTSUPP;
2085
2086 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
2087 }
2088
2089 if (tb[IFLA_VF_IB_PORT_GUID]) {
2090 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
2091
2092 if (!ops->ndo_set_vf_guid)
2093 return -EOPNOTSUPP;
2094
2095 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
2096 }
2097
Chris Wrightc02db8c2010-05-16 01:05:45 -07002098 return err;
2099}
2100
David Ahern33eaf2a2017-10-04 17:48:46 -07002101static int do_set_master(struct net_device *dev, int ifindex,
2102 struct netlink_ext_ack *extack)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002103{
Jiri Pirko898e5062013-01-03 22:48:52 +00002104 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002105 const struct net_device_ops *ops;
2106 int err;
2107
Jiri Pirko898e5062013-01-03 22:48:52 +00002108 if (upper_dev) {
2109 if (upper_dev->ifindex == ifindex)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002110 return 0;
Jiri Pirko898e5062013-01-03 22:48:52 +00002111 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002112 if (ops->ndo_del_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002113 err = ops->ndo_del_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002114 if (err)
2115 return err;
2116 } else {
2117 return -EOPNOTSUPP;
2118 }
2119 }
2120
2121 if (ifindex) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002122 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
2123 if (!upper_dev)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002124 return -EINVAL;
Jiri Pirko898e5062013-01-03 22:48:52 +00002125 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002126 if (ops->ndo_add_slave) {
David Ahern33eaf2a2017-10-04 17:48:46 -07002127 err = ops->ndo_add_slave(upper_dev, dev, extack);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002128 if (err)
2129 return err;
2130 } else {
2131 return -EOPNOTSUPP;
2132 }
2133 }
2134 return 0;
2135}
2136
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002137#define DO_SETLINK_MODIFIED 0x01
Nicolas Dichtelba998902014-09-01 16:07:29 +02002138/* notify flag means notify + modified. */
2139#define DO_SETLINK_NOTIFY 0x03
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002140static int do_setlink(const struct sk_buff *skb,
2141 struct net_device *dev, struct ifinfomsg *ifm,
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002142 struct netlink_ext_ack *extack,
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002143 struct nlattr **tb, char *ifname, int status)
Patrick McHardy0157f602007-06-13 12:03:36 -07002144{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002145 const struct net_device_ops *ops = dev->netdev_ops;
Patrick McHardy0157f602007-06-13 12:03:36 -07002146 int err;
2147
Eric W. Biedermanf0630522011-05-04 17:51:50 -07002148 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
Eric W. Biederman81adee42009-11-08 00:53:51 -08002149 struct net *net = rtnl_link_get_net(dev_net(dev), tb);
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002150 if (IS_ERR(net)) {
2151 err = PTR_ERR(net);
2152 goto errout;
2153 }
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002154 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
Nicolas Dichtele0ebde02014-11-27 10:16:15 +01002155 put_net(net);
Eric W. Biedermanb51642f2012-11-16 03:03:11 +00002156 err = -EPERM;
2157 goto errout;
2158 }
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002159 err = dev_change_net_namespace(dev, net, ifname);
2160 put_net(net);
2161 if (err)
2162 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002163 status |= DO_SETLINK_MODIFIED;
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002164 }
2165
Patrick McHardy0157f602007-06-13 12:03:36 -07002166 if (tb[IFLA_MAP]) {
2167 struct rtnl_link_ifmap *u_map;
2168 struct ifmap k_map;
2169
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002170 if (!ops->ndo_set_config) {
Patrick McHardy0157f602007-06-13 12:03:36 -07002171 err = -EOPNOTSUPP;
2172 goto errout;
2173 }
2174
2175 if (!netif_device_present(dev)) {
2176 err = -ENODEV;
2177 goto errout;
2178 }
2179
2180 u_map = nla_data(tb[IFLA_MAP]);
2181 k_map.mem_start = (unsigned long) u_map->mem_start;
2182 k_map.mem_end = (unsigned long) u_map->mem_end;
2183 k_map.base_addr = (unsigned short) u_map->base_addr;
2184 k_map.irq = (unsigned char) u_map->irq;
2185 k_map.dma = (unsigned char) u_map->dma;
2186 k_map.port = (unsigned char) u_map->port;
2187
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002188 err = ops->ndo_set_config(dev, &k_map);
Patrick McHardy0157f602007-06-13 12:03:36 -07002189 if (err < 0)
2190 goto errout;
2191
Nicolas Dichtelba998902014-09-01 16:07:29 +02002192 status |= DO_SETLINK_NOTIFY;
Patrick McHardy0157f602007-06-13 12:03:36 -07002193 }
2194
2195 if (tb[IFLA_ADDRESS]) {
2196 struct sockaddr *sa;
2197 int len;
2198
WANG Cong153711f2017-07-20 11:27:57 -07002199 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2200 sizeof(*sa));
Patrick McHardy0157f602007-06-13 12:03:36 -07002201 sa = kmalloc(len, GFP_KERNEL);
2202 if (!sa) {
2203 err = -ENOMEM;
2204 goto errout;
2205 }
2206 sa->sa_family = dev->type;
2207 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
2208 dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00002209 err = dev_set_mac_address(dev, sa);
Patrick McHardy0157f602007-06-13 12:03:36 -07002210 kfree(sa);
2211 if (err)
2212 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002213 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002214 }
2215
2216 if (tb[IFLA_MTU]) {
2217 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
2218 if (err < 0)
2219 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002220 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002221 }
2222
Vlad Dogarucbda10f2011-01-13 23:38:30 +00002223 if (tb[IFLA_GROUP]) {
2224 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Nicolas Dichtelba998902014-09-01 16:07:29 +02002225 status |= DO_SETLINK_NOTIFY;
Vlad Dogarucbda10f2011-01-13 23:38:30 +00002226 }
2227
Patrick McHardy0157f602007-06-13 12:03:36 -07002228 /*
2229 * Interface selected by interface index but interface
2230 * name provided implies that a name change has been
2231 * requested.
2232 */
2233 if (ifm->ifi_index > 0 && ifname[0]) {
2234 err = dev_change_name(dev, ifname);
2235 if (err < 0)
2236 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002237 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002238 }
2239
Stephen Hemminger0b815a12008-09-22 21:28:11 -07002240 if (tb[IFLA_IFALIAS]) {
2241 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2242 nla_len(tb[IFLA_IFALIAS]));
2243 if (err < 0)
2244 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002245 status |= DO_SETLINK_NOTIFY;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07002246 }
2247
Patrick McHardy0157f602007-06-13 12:03:36 -07002248 if (tb[IFLA_BROADCAST]) {
2249 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00002250 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Patrick McHardy0157f602007-06-13 12:03:36 -07002251 }
2252
2253 if (ifm->ifi_flags || ifm->ifi_change) {
Patrick McHardy3729d502010-02-26 06:34:54 +00002254 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
Johannes Berg5f9021c2008-11-16 23:20:31 -08002255 if (err < 0)
2256 goto errout;
Patrick McHardy0157f602007-06-13 12:03:36 -07002257 }
2258
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002259 if (tb[IFLA_MASTER]) {
David Ahern33eaf2a2017-10-04 17:48:46 -07002260 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002261 if (err)
2262 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002263 status |= DO_SETLINK_MODIFIED;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002264 }
2265
Jiri Pirko9a572472012-12-27 23:49:39 +00002266 if (tb[IFLA_CARRIER]) {
2267 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2268 if (err)
2269 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002270 status |= DO_SETLINK_MODIFIED;
Jiri Pirko9a572472012-12-27 23:49:39 +00002271 }
2272
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002273 if (tb[IFLA_TXQLEN]) {
Alexey Dobriyan0cd29502017-05-17 13:30:44 +03002274 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
2275 unsigned int orig_len = dev->tx_queue_len;
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002276
Jason Wang08294a22016-06-30 14:45:35 +08002277 if (dev->tx_queue_len ^ value) {
2278 dev->tx_queue_len = value;
2279 err = call_netdevice_notifiers(
2280 NETDEV_CHANGE_TX_QUEUE_LEN, dev);
2281 err = notifier_to_errno(err);
2282 if (err) {
2283 dev->tx_queue_len = orig_len;
2284 goto errout;
2285 }
Xin Long2d7f6692017-10-15 18:13:46 +08002286 status |= DO_SETLINK_MODIFIED;
Jason Wang08294a22016-06-30 14:45:35 +08002287 }
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002288 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002289
Patrick McHardy0157f602007-06-13 12:03:36 -07002290 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002291 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Patrick McHardy0157f602007-06-13 12:03:36 -07002292
2293 if (tb[IFLA_LINKMODE]) {
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002294 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2295
David S. Miller93b2d4a2008-02-17 18:35:07 -08002296 write_lock_bh(&dev_base_lock);
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002297 if (dev->link_mode ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02002298 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002299 dev->link_mode = value;
David S. Miller93b2d4a2008-02-17 18:35:07 -08002300 write_unlock_bh(&dev_base_lock);
Patrick McHardy0157f602007-06-13 12:03:36 -07002301 }
2302
Chris Wrightc02db8c2010-05-16 01:05:45 -07002303 if (tb[IFLA_VFINFO_LIST]) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002304 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
Chris Wrightc02db8c2010-05-16 01:05:45 -07002305 struct nlattr *attr;
2306 int rem;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002307
Chris Wrightc02db8c2010-05-16 01:05:45 -07002308 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002309 if (nla_type(attr) != IFLA_VF_INFO ||
2310 nla_len(attr) < NLA_HDRLEN) {
David Howells253683b2010-05-21 02:25:27 +00002311 err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002312 goto errout;
David Howells253683b2010-05-21 02:25:27 +00002313 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002314 err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr,
Johannes Bergfceb6432017-04-12 14:34:07 +02002315 ifla_vf_policy, NULL);
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002316 if (err < 0)
2317 goto errout;
2318 err = do_setvfinfo(dev, vfinfo);
Chris Wrightc02db8c2010-05-16 01:05:45 -07002319 if (err < 0)
2320 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002321 status |= DO_SETLINK_NOTIFY;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002322 }
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00002323 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002324 err = 0;
2325
Scott Feldman57b61082010-05-17 22:49:55 -07002326 if (tb[IFLA_VF_PORTS]) {
2327 struct nlattr *port[IFLA_PORT_MAX+1];
2328 struct nlattr *attr;
2329 int vf;
2330 int rem;
2331
2332 err = -EOPNOTSUPP;
2333 if (!ops->ndo_set_vf_port)
2334 goto errout;
2335
2336 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
Daniel Borkmann035d2102015-07-13 00:06:02 +02002337 if (nla_type(attr) != IFLA_VF_PORT ||
2338 nla_len(attr) < NLA_HDRLEN) {
2339 err = -EINVAL;
2340 goto errout;
2341 }
2342 err = nla_parse_nested(port, IFLA_PORT_MAX, attr,
Johannes Bergfceb6432017-04-12 14:34:07 +02002343 ifla_port_policy, NULL);
Scott Feldman57b61082010-05-17 22:49:55 -07002344 if (err < 0)
2345 goto errout;
2346 if (!port[IFLA_PORT_VF]) {
2347 err = -EOPNOTSUPP;
2348 goto errout;
2349 }
2350 vf = nla_get_u32(port[IFLA_PORT_VF]);
2351 err = ops->ndo_set_vf_port(dev, vf, port);
2352 if (err < 0)
2353 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002354 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07002355 }
2356 }
2357 err = 0;
2358
2359 if (tb[IFLA_PORT_SELF]) {
2360 struct nlattr *port[IFLA_PORT_MAX+1];
2361
2362 err = nla_parse_nested(port, IFLA_PORT_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +02002363 tb[IFLA_PORT_SELF], ifla_port_policy,
2364 NULL);
Scott Feldman57b61082010-05-17 22:49:55 -07002365 if (err < 0)
2366 goto errout;
2367
2368 err = -EOPNOTSUPP;
2369 if (ops->ndo_set_vf_port)
2370 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
2371 if (err < 0)
2372 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002373 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07002374 }
Thomas Graff8ff1822010-11-16 04:30:14 +00002375
2376 if (tb[IFLA_AF_SPEC]) {
2377 struct nlattr *af;
2378 int rem;
2379
2380 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2381 const struct rtnl_af_ops *af_ops;
2382
Florian Westphal5fa85a02017-10-16 15:44:36 +02002383 rcu_read_lock();
2384
Gustavo A. R. Silva058c8d52017-10-20 19:43:11 -05002385 BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
Thomas Graff8ff1822010-11-16 04:30:14 +00002386
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002387 err = af_ops->set_link_af(dev, af);
Florian Westphal5fa85a02017-10-16 15:44:36 +02002388 if (err < 0) {
2389 rcu_read_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +00002390 goto errout;
Florian Westphal5fa85a02017-10-16 15:44:36 +02002391 }
Thomas Graff8ff1822010-11-16 04:30:14 +00002392
Florian Westphal5fa85a02017-10-16 15:44:36 +02002393 rcu_read_unlock();
Nicolas Dichtelba998902014-09-01 16:07:29 +02002394 status |= DO_SETLINK_NOTIFY;
Thomas Graff8ff1822010-11-16 04:30:14 +00002395 }
2396 }
Scott Feldman57b61082010-05-17 22:49:55 -07002397 err = 0;
2398
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07002399 if (tb[IFLA_PROTO_DOWN]) {
2400 err = dev_change_proto_down(dev,
2401 nla_get_u8(tb[IFLA_PROTO_DOWN]));
2402 if (err)
2403 goto errout;
2404 status |= DO_SETLINK_NOTIFY;
2405 }
2406
Brenden Blancod1fdd912016-07-19 12:16:49 -07002407 if (tb[IFLA_XDP]) {
2408 struct nlattr *xdp[IFLA_XDP_MAX + 1];
Daniel Borkmann85de8572016-11-28 23:16:54 +01002409 u32 xdp_flags = 0;
Brenden Blancod1fdd912016-07-19 12:16:49 -07002410
2411 err = nla_parse_nested(xdp, IFLA_XDP_MAX, tb[IFLA_XDP],
Johannes Bergfceb6432017-04-12 14:34:07 +02002412 ifla_xdp_policy, NULL);
Brenden Blancod1fdd912016-07-19 12:16:49 -07002413 if (err < 0)
2414 goto errout;
2415
Martin KaFai Lau58038692017-06-15 17:29:09 -07002416 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
Brenden Blanco262d8622016-07-20 17:22:34 -07002417 err = -EINVAL;
2418 goto errout;
2419 }
Daniel Borkmann85de8572016-11-28 23:16:54 +01002420
2421 if (xdp[IFLA_XDP_FLAGS]) {
2422 xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
2423 if (xdp_flags & ~XDP_FLAGS_MASK) {
2424 err = -EINVAL;
2425 goto errout;
2426 }
Jakub Kicinskiee5d0322017-06-21 18:25:04 -07002427 if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
Daniel Borkmann0489df92017-05-12 01:04:45 +02002428 err = -EINVAL;
2429 goto errout;
2430 }
Daniel Borkmann85de8572016-11-28 23:16:54 +01002431 }
2432
Brenden Blancod1fdd912016-07-19 12:16:49 -07002433 if (xdp[IFLA_XDP_FD]) {
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002434 err = dev_change_xdp_fd(dev, extack,
Daniel Borkmann85de8572016-11-28 23:16:54 +01002435 nla_get_s32(xdp[IFLA_XDP_FD]),
2436 xdp_flags);
Brenden Blancod1fdd912016-07-19 12:16:49 -07002437 if (err)
2438 goto errout;
2439 status |= DO_SETLINK_NOTIFY;
2440 }
2441 }
2442
Patrick McHardy0157f602007-06-13 12:03:36 -07002443errout:
Nicolas Dichtelba998902014-09-01 16:07:29 +02002444 if (status & DO_SETLINK_MODIFIED) {
Xin Long64ff90c2017-10-15 18:13:45 +08002445 if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
Nicolas Dichtelba998902014-09-01 16:07:29 +02002446 netdev_state_change(dev);
2447
2448 if (err < 0)
2449 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",
2450 dev->name);
2451 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002452
Patrick McHardy0157f602007-06-13 12:03:36 -07002453 return err;
2454}
2455
David Ahernc21ef3e2017-04-16 09:48:24 -07002456static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2457 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002459 struct net *net = sock_net(skb->sk);
Thomas Grafda5e0492006-08-10 21:17:37 -07002460 struct ifinfomsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 struct net_device *dev;
Patrick McHardy0157f602007-06-13 12:03:36 -07002462 int err;
Thomas Grafda5e0492006-08-10 21:17:37 -07002463 struct nlattr *tb[IFLA_MAX+1];
2464 char ifname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
David Ahernc21ef3e2017-04-16 09:48:24 -07002466 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy,
2467 extack);
Thomas Grafda5e0492006-08-10 21:17:37 -07002468 if (err < 0)
2469 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
Jiri Benc79e1ad12017-11-02 17:04:38 -02002471 if (tb[IFLA_IF_NETNSID])
2472 return -EOPNOTSUPP;
2473
Thomas Graf5176f912006-08-26 20:13:18 -07002474 if (tb[IFLA_IFNAME])
2475 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
Patrick McHardy78e5b8912006-09-13 20:35:36 -07002476 else
2477 ifname[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 err = -EINVAL;
Thomas Grafda5e0492006-08-10 21:17:37 -07002480 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -07002481 if (ifm->ifi_index > 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00002482 dev = __dev_get_by_index(net, ifm->ifi_index);
Thomas Grafda5e0492006-08-10 21:17:37 -07002483 else if (tb[IFLA_IFNAME])
Eric Dumazeta3d12892009-10-21 10:59:31 +00002484 dev = __dev_get_by_name(net, ifname);
Thomas Grafda5e0492006-08-10 21:17:37 -07002485 else
2486 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
Thomas Grafda5e0492006-08-10 21:17:37 -07002488 if (dev == NULL) {
2489 err = -ENODEV;
2490 goto errout;
2491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
Eric Dumazete0d087a2009-11-07 01:26:17 -08002493 err = validate_linkmsg(dev, tb);
2494 if (err < 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00002495 goto errout;
Thomas Grafda5e0492006-08-10 21:17:37 -07002496
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002497 err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
Thomas Grafda5e0492006-08-10 21:17:37 -07002498errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 return err;
2500}
2501
WANG Cong66400d52015-03-24 11:53:31 -07002502static int rtnl_group_dellink(const struct net *net, int group)
2503{
2504 struct net_device *dev, *aux;
2505 LIST_HEAD(list_kill);
2506 bool found = false;
2507
2508 if (!group)
2509 return -EPERM;
2510
2511 for_each_netdev(net, dev) {
2512 if (dev->group == group) {
2513 const struct rtnl_link_ops *ops;
2514
2515 found = true;
2516 ops = dev->rtnl_link_ops;
2517 if (!ops || !ops->dellink)
2518 return -EOPNOTSUPP;
2519 }
2520 }
2521
2522 if (!found)
2523 return -ENODEV;
2524
2525 for_each_netdev_safe(net, dev, aux) {
2526 if (dev->group == group) {
2527 const struct rtnl_link_ops *ops;
2528
2529 ops = dev->rtnl_link_ops;
2530 ops->dellink(dev, &list_kill);
2531 }
2532 }
2533 unregister_netdevice_many(&list_kill);
2534
2535 return 0;
2536}
2537
Thomas Graf614732e2015-07-21 10:44:06 +02002538int rtnl_delete_link(struct net_device *dev)
2539{
2540 const struct rtnl_link_ops *ops;
2541 LIST_HEAD(list_kill);
2542
2543 ops = dev->rtnl_link_ops;
2544 if (!ops || !ops->dellink)
2545 return -EOPNOTSUPP;
2546
2547 ops->dellink(dev, &list_kill);
2548 unregister_netdevice_many(&list_kill);
2549
2550 return 0;
2551}
2552EXPORT_SYMBOL_GPL(rtnl_delete_link);
2553
David Ahernc21ef3e2017-04-16 09:48:24 -07002554static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
2555 struct netlink_ext_ack *extack)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002556{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002557 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002558 struct net_device *dev;
2559 struct ifinfomsg *ifm;
2560 char ifname[IFNAMSIZ];
2561 struct nlattr *tb[IFLA_MAX+1];
2562 int err;
2563
David Ahernc21ef3e2017-04-16 09:48:24 -07002564 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002565 if (err < 0)
2566 return err;
2567
Jiri Benc79e1ad12017-11-02 17:04:38 -02002568 if (tb[IFLA_IF_NETNSID])
2569 return -EOPNOTSUPP;
2570
Patrick McHardy38f7b872007-06-13 12:03:51 -07002571 if (tb[IFLA_IFNAME])
2572 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2573
2574 ifm = nlmsg_data(nlh);
2575 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07002576 dev = __dev_get_by_index(net, ifm->ifi_index);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002577 else if (tb[IFLA_IFNAME])
Eric W. Biederman881d9662007-09-17 11:56:21 -07002578 dev = __dev_get_by_name(net, ifname);
WANG Cong66400d52015-03-24 11:53:31 -07002579 else if (tb[IFLA_GROUP])
2580 return rtnl_group_dellink(net, nla_get_u32(tb[IFLA_GROUP]));
Patrick McHardy38f7b872007-06-13 12:03:51 -07002581 else
2582 return -EINVAL;
2583
2584 if (!dev)
2585 return -ENODEV;
2586
Thomas Graf614732e2015-07-21 10:44:06 +02002587 return rtnl_delete_link(dev);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002588}
2589
Patrick McHardy3729d502010-02-26 06:34:54 +00002590int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
2591{
2592 unsigned int old_flags;
2593 int err;
2594
2595 old_flags = dev->flags;
2596 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
2597 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
2598 if (err < 0)
2599 return err;
2600 }
2601
2602 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
Patrick McHardy3729d502010-02-26 06:34:54 +00002603
Nicolas Dichtela528c212013-09-25 12:02:44 +02002604 __dev_notify_flags(dev, old_flags, ~0U);
Patrick McHardy3729d502010-02-26 06:34:54 +00002605 return 0;
2606}
2607EXPORT_SYMBOL(rtnl_configure_link);
2608
Rami Rosenc0713562012-11-30 01:08:47 +00002609struct net_device *rtnl_create_link(struct net *net,
Thomas Graf78ebb0d2015-04-10 01:45:53 +02002610 const char *ifname, unsigned char name_assign_type,
Tom Gundersen55177502014-07-14 16:37:25 +02002611 const struct rtnl_link_ops *ops, struct nlattr *tb[])
Pavel Emelianove7199282007-08-08 22:16:38 -07002612{
Pavel Emelianove7199282007-08-08 22:16:38 -07002613 struct net_device *dev;
Jiri Pirkod40156a2012-07-20 02:28:47 +00002614 unsigned int num_tx_queues = 1;
2615 unsigned int num_rx_queues = 1;
Pavel Emelianove7199282007-08-08 22:16:38 -07002616
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002617 if (tb[IFLA_NUM_TX_QUEUES])
2618 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
2619 else if (ops->get_num_tx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002620 num_tx_queues = ops->get_num_tx_queues();
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002621
2622 if (tb[IFLA_NUM_RX_QUEUES])
2623 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
2624 else if (ops->get_num_rx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002625 num_rx_queues = ops->get_num_rx_queues();
stephen hemmingerefacb302012-04-10 18:34:43 +00002626
Tom Gundersen55177502014-07-14 16:37:25 +02002627 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
Tom Gundersenc835a672014-07-14 16:37:24 +02002628 ops->setup, num_tx_queues, num_rx_queues);
Pavel Emelianove7199282007-08-08 22:16:38 -07002629 if (!dev)
Tobias Klauserd1892e42017-02-20 16:32:06 +01002630 return ERR_PTR(-ENOMEM);
Pavel Emelianove7199282007-08-08 22:16:38 -07002631
Eric W. Biederman81adee42009-11-08 00:53:51 -08002632 dev_net_set(dev, net);
2633 dev->rtnl_link_ops = ops;
Patrick McHardy3729d502010-02-26 06:34:54 +00002634 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
Eric W. Biederman81adee42009-11-08 00:53:51 -08002635
Pavel Emelianove7199282007-08-08 22:16:38 -07002636 if (tb[IFLA_MTU])
2637 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002638 if (tb[IFLA_ADDRESS]) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002639 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
2640 nla_len(tb[IFLA_ADDRESS]));
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002641 dev->addr_assign_type = NET_ADDR_SET;
2642 }
Pavel Emelianove7199282007-08-08 22:16:38 -07002643 if (tb[IFLA_BROADCAST])
2644 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
2645 nla_len(tb[IFLA_BROADCAST]));
2646 if (tb[IFLA_TXQLEN])
2647 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
2648 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002649 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Pavel Emelianove7199282007-08-08 22:16:38 -07002650 if (tb[IFLA_LINKMODE])
2651 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
Patrick McHardyffa934f2011-01-20 03:00:42 +00002652 if (tb[IFLA_GROUP])
2653 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Pavel Emelianove7199282007-08-08 22:16:38 -07002654
2655 return dev;
Pavel Emelianove7199282007-08-08 22:16:38 -07002656}
Eric Dumazete0d087a2009-11-07 01:26:17 -08002657EXPORT_SYMBOL(rtnl_create_link);
Pavel Emelianove7199282007-08-08 22:16:38 -07002658
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002659static int rtnl_group_changelink(const struct sk_buff *skb,
2660 struct net *net, int group,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002661 struct ifinfomsg *ifm,
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002662 struct netlink_ext_ack *extack,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002663 struct nlattr **tb)
2664{
WANG Congd0795352015-03-23 16:31:09 -07002665 struct net_device *dev, *aux;
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002666 int err;
2667
WANG Congd0795352015-03-23 16:31:09 -07002668 for_each_netdev_safe(net, dev, aux) {
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002669 if (dev->group == group) {
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002670 err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002671 if (err < 0)
2672 return err;
2673 }
2674 }
2675
2676 return 0;
2677}
2678
David Ahernc21ef3e2017-04-16 09:48:24 -07002679static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2680 struct netlink_ext_ack *extack)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002681{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002682 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002683 const struct rtnl_link_ops *ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002684 const struct rtnl_link_ops *m_ops = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002685 struct net_device *dev;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002686 struct net_device *master_dev = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002687 struct ifinfomsg *ifm;
2688 char kind[MODULE_NAME_LEN];
2689 char ifname[IFNAMSIZ];
2690 struct nlattr *tb[IFLA_MAX+1];
2691 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
Tom Gundersen55177502014-07-14 16:37:25 +02002692 unsigned char name_assign_type = NET_NAME_USER;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002693 int err;
2694
Johannes Berg95a5afc2008-10-16 15:24:51 -07002695#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002696replay:
Thomas Graf8072f082007-07-31 14:13:50 -07002697#endif
David Ahernc21ef3e2017-04-16 09:48:24 -07002698 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002699 if (err < 0)
2700 return err;
2701
Jiri Benc79e1ad12017-11-02 17:04:38 -02002702 if (tb[IFLA_IF_NETNSID])
2703 return -EOPNOTSUPP;
2704
Patrick McHardy38f7b872007-06-13 12:03:51 -07002705 if (tb[IFLA_IFNAME])
2706 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2707 else
2708 ifname[0] = '\0';
2709
2710 ifm = nlmsg_data(nlh);
2711 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07002712 dev = __dev_get_by_index(net, ifm->ifi_index);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002713 else {
2714 if (ifname[0])
2715 dev = __dev_get_by_name(net, ifname);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002716 else
2717 dev = NULL;
2718 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002719
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002720 if (dev) {
2721 master_dev = netdev_master_upper_dev_get(dev);
2722 if (master_dev)
2723 m_ops = master_dev->rtnl_link_ops;
2724 }
2725
Eric Dumazete0d087a2009-11-07 01:26:17 -08002726 err = validate_linkmsg(dev, tb);
2727 if (err < 0)
Thomas Graf1840bb12008-02-23 19:54:36 -08002728 return err;
2729
Patrick McHardy38f7b872007-06-13 12:03:51 -07002730 if (tb[IFLA_LINKINFO]) {
2731 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +02002732 tb[IFLA_LINKINFO], ifla_info_policy,
2733 NULL);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002734 if (err < 0)
2735 return err;
2736 } else
2737 memset(linkinfo, 0, sizeof(linkinfo));
2738
2739 if (linkinfo[IFLA_INFO_KIND]) {
2740 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
2741 ops = rtnl_link_ops_get(kind);
2742 } else {
2743 kind[0] = '\0';
2744 ops = NULL;
2745 }
2746
2747 if (1) {
Sasha Levin4e10fd52015-02-24 14:14:35 -05002748 struct nlattr *attr[ops ? ops->maxtype + 1 : 1];
2749 struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1];
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002750 struct nlattr **data = NULL;
2751 struct nlattr **slave_data = NULL;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002752 struct net *dest_net, *link_net = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002753
2754 if (ops) {
2755 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
2756 err = nla_parse_nested(attr, ops->maxtype,
2757 linkinfo[IFLA_INFO_DATA],
Johannes Bergfceb6432017-04-12 14:34:07 +02002758 ops->policy, NULL);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002759 if (err < 0)
2760 return err;
2761 data = attr;
2762 }
2763 if (ops->validate) {
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02002764 err = ops->validate(tb, data, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002765 if (err < 0)
2766 return err;
2767 }
2768 }
2769
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002770 if (m_ops) {
2771 if (m_ops->slave_maxtype &&
2772 linkinfo[IFLA_INFO_SLAVE_DATA]) {
2773 err = nla_parse_nested(slave_attr,
2774 m_ops->slave_maxtype,
2775 linkinfo[IFLA_INFO_SLAVE_DATA],
Johannes Bergfceb6432017-04-12 14:34:07 +02002776 m_ops->slave_policy,
2777 NULL);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002778 if (err < 0)
2779 return err;
2780 slave_data = slave_attr;
2781 }
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002782 }
2783
Patrick McHardy38f7b872007-06-13 12:03:51 -07002784 if (dev) {
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002785 int status = 0;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002786
2787 if (nlh->nlmsg_flags & NLM_F_EXCL)
2788 return -EEXIST;
2789 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2790 return -EOPNOTSUPP;
2791
2792 if (linkinfo[IFLA_INFO_DATA]) {
2793 if (!ops || ops != dev->rtnl_link_ops ||
2794 !ops->changelink)
2795 return -EOPNOTSUPP;
2796
Matthias Schifferad744b22017-06-25 23:56:00 +02002797 err = ops->changelink(dev, tb, data, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002798 if (err < 0)
2799 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002800 status |= DO_SETLINK_NOTIFY;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002801 }
2802
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002803 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2804 if (!m_ops || !m_ops->slave_changelink)
2805 return -EOPNOTSUPP;
2806
2807 err = m_ops->slave_changelink(master_dev, dev,
Matthias Schiffer17dd0ec2017-06-25 23:56:02 +02002808 tb, slave_data,
2809 extack);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002810 if (err < 0)
2811 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002812 status |= DO_SETLINK_NOTIFY;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002813 }
2814
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002815 return do_setlink(skb, dev, ifm, extack, tb, ifname,
2816 status);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002817 }
2818
Patrick McHardyffa934f2011-01-20 03:00:42 +00002819 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2820 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002821 return rtnl_group_changelink(skb, net,
Patrick McHardyffa934f2011-01-20 03:00:42 +00002822 nla_get_u32(tb[IFLA_GROUP]),
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002823 ifm, extack, tb);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002824 return -ENODEV;
Patrick McHardyffa934f2011-01-20 03:00:42 +00002825 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002826
Theuns Verwoerd160ca012017-01-31 12:23:46 +13002827 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
Patrick McHardy38f7b872007-06-13 12:03:51 -07002828 return -EOPNOTSUPP;
2829
2830 if (!ops) {
Johannes Berg95a5afc2008-10-16 15:24:51 -07002831#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002832 if (kind[0]) {
2833 __rtnl_unlock();
2834 request_module("rtnl-link-%s", kind);
2835 rtnl_lock();
2836 ops = rtnl_link_ops_get(kind);
2837 if (ops)
2838 goto replay;
2839 }
2840#endif
2841 return -EOPNOTSUPP;
2842 }
2843
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02002844 if (!ops->setup)
2845 return -EOPNOTSUPP;
2846
Tom Gundersen55177502014-07-14 16:37:25 +02002847 if (!ifname[0]) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07002848 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
Tom Gundersen55177502014-07-14 16:37:25 +02002849 name_assign_type = NET_NAME_ENUM;
2850 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002851
Eric W. Biederman81adee42009-11-08 00:53:51 -08002852 dest_net = rtnl_link_get_net(net, tb);
Eric W. Biederman13ad1772011-01-29 14:57:22 +00002853 if (IS_ERR(dest_net))
2854 return PTR_ERR(dest_net);
2855
Eric W. Biederman505ce412015-02-26 16:19:00 -06002856 err = -EPERM;
2857 if (!netlink_ns_capable(skb, dest_net->user_ns, CAP_NET_ADMIN))
2858 goto out;
2859
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002860 if (tb[IFLA_LINK_NETNSID]) {
2861 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
2862
2863 link_net = get_net_ns_by_id(dest_net, id);
2864 if (!link_net) {
2865 err = -EINVAL;
2866 goto out;
2867 }
Eric W. Biederman06615be2015-02-26 16:20:07 -06002868 err = -EPERM;
2869 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
2870 goto out;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002871 }
2872
2873 dev = rtnl_create_link(link_net ? : dest_net, ifname,
2874 name_assign_type, ops, tb);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002875 if (IS_ERR(dev)) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002876 err = PTR_ERR(dev);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002877 goto out;
2878 }
2879
2880 dev->ifindex = ifm->ifi_index;
2881
Cong Wang0e0eee22014-02-11 15:51:30 -08002882 if (ops->newlink) {
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02002883 err = ops->newlink(link_net ? : net, dev, tb, data,
2884 extack);
Cong Wang0e0eee22014-02-11 15:51:30 -08002885 /* Drivers should call free_netdev() in ->destructor
Cong Wange51fb152014-06-03 16:40:47 -07002886 * and unregister it on failure after registration
2887 * so that device could be finally freed in rtnl_unlock.
Cong Wang0e0eee22014-02-11 15:51:30 -08002888 */
Cong Wange51fb152014-06-03 16:40:47 -07002889 if (err < 0) {
2890 /* If device is not registered at all, free it now */
2891 if (dev->reg_state == NETREG_UNINITIALIZED)
2892 free_netdev(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002893 goto out;
Cong Wange51fb152014-06-03 16:40:47 -07002894 }
Cong Wang0e0eee22014-02-11 15:51:30 -08002895 } else {
Patrick McHardy2d85cba2007-07-11 19:42:13 -07002896 err = register_netdevice(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002897 if (err < 0) {
2898 free_netdev(dev);
2899 goto out;
2900 }
Dan Carpenterfce9b9b2013-08-14 12:35:42 +03002901 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002902 err = rtnl_configure_link(dev, ifm);
David S. Miller43638902015-03-10 21:58:32 -04002903 if (err < 0)
2904 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002905 if (link_net) {
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002906 err = dev_change_net_namespace(dev, dest_net, ifname);
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002907 if (err < 0)
David S. Miller43638902015-03-10 21:58:32 -04002908 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002909 }
Theuns Verwoerd160ca012017-01-31 12:23:46 +13002910 if (tb[IFLA_MASTER]) {
David Ahern33eaf2a2017-10-04 17:48:46 -07002911 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]),
2912 extack);
Theuns Verwoerd160ca012017-01-31 12:23:46 +13002913 if (err)
2914 goto out_unregister;
2915 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002916out:
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002917 if (link_net)
2918 put_net(link_net);
Eric W. Biederman81adee42009-11-08 00:53:51 -08002919 put_net(dest_net);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002920 return err;
David S. Miller43638902015-03-10 21:58:32 -04002921out_unregister:
2922 if (ops->newlink) {
2923 LIST_HEAD(list_kill);
2924
2925 ops->dellink(dev, &list_kill);
2926 unregister_netdevice_many(&list_kill);
2927 } else {
2928 unregister_netdevice(dev);
2929 }
2930 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002931 }
2932}
2933
David Ahernc21ef3e2017-04-16 09:48:24 -07002934static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2935 struct netlink_ext_ack *extack)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002936{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002937 struct net *net = sock_net(skb->sk);
Jiri Benc79e1ad12017-11-02 17:04:38 -02002938 struct net *tgt_net = net;
Thomas Grafb60c5112006-08-04 23:05:34 -07002939 struct ifinfomsg *ifm;
Eric Dumazeta3d12892009-10-21 10:59:31 +00002940 char ifname[IFNAMSIZ];
Thomas Grafb60c5112006-08-04 23:05:34 -07002941 struct nlattr *tb[IFLA_MAX+1];
2942 struct net_device *dev = NULL;
2943 struct sk_buff *nskb;
Jiri Benc79e1ad12017-11-02 17:04:38 -02002944 int netnsid = -1;
Thomas Graf339bf982006-11-10 14:10:15 -08002945 int err;
Greg Rose115c9b82012-02-21 16:54:48 -05002946 u32 ext_filter_mask = 0;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002947
David Ahernc21ef3e2017-04-16 09:48:24 -07002948 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Thomas Grafb60c5112006-08-04 23:05:34 -07002949 if (err < 0)
Eric Sesterhenn9918f232006-09-26 23:26:38 -07002950 return err;
Thomas Grafb60c5112006-08-04 23:05:34 -07002951
Jiri Benc79e1ad12017-11-02 17:04:38 -02002952 if (tb[IFLA_IF_NETNSID]) {
2953 netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]);
2954 tgt_net = get_target_net(skb, netnsid);
2955 if (IS_ERR(tgt_net))
2956 return PTR_ERR(tgt_net);
2957 }
2958
Eric Dumazeta3d12892009-10-21 10:59:31 +00002959 if (tb[IFLA_IFNAME])
2960 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2961
Greg Rose115c9b82012-02-21 16:54:48 -05002962 if (tb[IFLA_EXT_MASK])
2963 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2964
Jiri Benc79e1ad12017-11-02 17:04:38 -02002965 err = -EINVAL;
Thomas Grafb60c5112006-08-04 23:05:34 -07002966 ifm = nlmsg_data(nlh);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002967 if (ifm->ifi_index > 0)
Jiri Benc79e1ad12017-11-02 17:04:38 -02002968 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002969 else if (tb[IFLA_IFNAME])
Jiri Benc79e1ad12017-11-02 17:04:38 -02002970 dev = __dev_get_by_name(tgt_net, ifname);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002971 else
Jiri Benc79e1ad12017-11-02 17:04:38 -02002972 goto out;
Thomas Grafb60c5112006-08-04 23:05:34 -07002973
Jiri Benc79e1ad12017-11-02 17:04:38 -02002974 err = -ENODEV;
Eric Dumazeta3d12892009-10-21 10:59:31 +00002975 if (dev == NULL)
Jiri Benc79e1ad12017-11-02 17:04:38 -02002976 goto out;
Eric Dumazeta3d12892009-10-21 10:59:31 +00002977
Jiri Benc79e1ad12017-11-02 17:04:38 -02002978 err = -ENOBUFS;
Greg Rose115c9b82012-02-21 16:54:48 -05002979 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002980 if (nskb == NULL)
Jiri Benc79e1ad12017-11-02 17:04:38 -02002981 goto out;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002982
Jiri Benc79e1ad12017-11-02 17:04:38 -02002983 err = rtnl_fill_ifinfo(nskb, dev, net,
2984 RTM_NEWLINK, NETLINK_CB(skb).portid,
2985 nlh->nlmsg_seq, 0, 0, ext_filter_mask,
2986 0, NULL, netnsid);
Patrick McHardy26932562007-01-31 23:16:40 -08002987 if (err < 0) {
2988 /* -EMSGSIZE implies BUG in if_nlmsg_size */
2989 WARN_ON(err == -EMSGSIZE);
2990 kfree_skb(nskb);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002991 } else
Eric W. Biederman15e47302012-09-07 20:12:54 +00002992 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
Jiri Benc79e1ad12017-11-02 17:04:38 -02002993out:
2994 if (netnsid >= 0)
2995 put_net(tgt_net);
Thomas Grafb60c5112006-08-04 23:05:34 -07002996
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002997 return err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002998}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002999
Greg Rose115c9b82012-02-21 16:54:48 -05003000static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
Greg Rosec7ac8672011-06-10 01:27:09 +00003001{
Greg Rose115c9b82012-02-21 16:54:48 -05003002 struct net *net = sock_net(skb->sk);
3003 struct net_device *dev;
3004 struct nlattr *tb[IFLA_MAX+1];
3005 u32 ext_filter_mask = 0;
3006 u16 min_ifinfo_dump_size = 0;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02003007 int hdrlen;
Greg Rose115c9b82012-02-21 16:54:48 -05003008
Michal Schmidte5eca6d2014-05-28 14:15:19 +02003009 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3010 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
3011 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
3012
Johannes Bergfceb6432017-04-12 14:34:07 +02003013 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00003014 if (tb[IFLA_EXT_MASK])
3015 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3016 }
Greg Rose115c9b82012-02-21 16:54:48 -05003017
3018 if (!ext_filter_mask)
3019 return NLMSG_GOODSIZE;
3020 /*
3021 * traverse the list of net devices and compute the minimum
3022 * buffer size based upon the filter mask.
3023 */
Florian Westphal6853dd42017-08-09 20:41:51 +02003024 rcu_read_lock();
3025 for_each_netdev_rcu(net, dev) {
Greg Rose115c9b82012-02-21 16:54:48 -05003026 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
3027 if_nlmsg_size(dev,
3028 ext_filter_mask));
3029 }
Florian Westphal6853dd42017-08-09 20:41:51 +02003030 rcu_read_unlock();
Greg Rose115c9b82012-02-21 16:54:48 -05003031
Zhang Shengju93af2052016-11-22 14:14:28 +08003032 return nlmsg_total_size(min_ifinfo_dump_size);
Greg Rosec7ac8672011-06-10 01:27:09 +00003033}
3034
Adrian Bunk42bad1d2007-04-26 00:57:41 -07003035static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036{
3037 int idx;
3038 int s_idx = cb->family;
3039
3040 if (s_idx == 0)
3041 s_idx = 1;
Florian Westphal6853dd42017-08-09 20:41:51 +02003042
Patrick McHardy25239ce2010-04-26 16:02:05 +02003043 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
Florian Westphaladdf9b92017-12-02 21:44:05 +01003044 struct rtnl_link **tab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 int type = cb->nlh->nlmsg_type-RTM_BASE;
Florian Westphaladdf9b92017-12-02 21:44:05 +01003046 struct rtnl_link *link;
Florian Westphal6853dd42017-08-09 20:41:51 +02003047 rtnl_dumpit_func dumpit;
3048
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 if (idx < s_idx || idx == PF_PACKET)
3050 continue;
Florian Westphal6853dd42017-08-09 20:41:51 +02003051
Florian Westphaladdf9b92017-12-02 21:44:05 +01003052 if (type < 0 || type >= RTM_NR_MSGTYPES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 continue;
Florian Westphal6853dd42017-08-09 20:41:51 +02003054
Florian Westphaladdf9b92017-12-02 21:44:05 +01003055 tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]);
3056 if (!tab)
3057 continue;
3058
3059 link = tab[type];
3060 if (!link)
3061 continue;
3062
3063 dumpit = link->dumpit;
Florian Westphal6853dd42017-08-09 20:41:51 +02003064 if (!dumpit)
3065 continue;
3066
Nicolas Dichtel04652772013-03-22 06:28:42 +00003067 if (idx > s_idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 memset(&cb->args[0], 0, sizeof(cb->args));
Nicolas Dichtel04652772013-03-22 06:28:42 +00003069 cb->prev_seq = 0;
3070 cb->seq = 0;
3071 }
Florian Westphal6853dd42017-08-09 20:41:51 +02003072 if (dumpit(skb, cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 break;
3074 }
3075 cb->family = idx;
3076
3077 return skb->len;
3078}
3079
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003080struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003081 unsigned int change,
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003082 u32 event, gfp_t flags, int *new_nsid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003084 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 struct sk_buff *skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003086 int err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00003087 size_t if_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088
Alexei Starovoitov7f294052013-10-23 16:02:42 -07003089 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003090 if (skb == NULL)
3091 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092
Jiri Benc79e1ad12017-11-02 17:04:38 -02003093 err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
3094 type, 0, 0, change, 0, 0, event,
3095 new_nsid, -1);
Patrick McHardy26932562007-01-31 23:16:40 -08003096 if (err < 0) {
3097 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
3098 WARN_ON(err == -EMSGSIZE);
3099 kfree_skb(skb);
3100 goto errout;
3101 }
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003102 return skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003103errout:
3104 if (err < 0)
Eric W. Biederman4b3da702007-11-19 22:27:40 -08003105 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003106 return NULL;
3107}
3108
3109void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
3110{
3111 struct net *net = dev_net(dev);
3112
3113 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
3114}
3115
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003116static void rtmsg_ifinfo_event(int type, struct net_device *dev,
3117 unsigned int change, u32 event,
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003118 gfp_t flags, int *new_nsid)
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003119{
3120 struct sk_buff *skb;
3121
Nicolas Dichteled2a80a2015-05-13 14:19:42 +02003122 if (dev->reg_state != NETREG_REGISTERED)
3123 return;
3124
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003125 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003126 if (skb)
3127 rtmsg_ifinfo_send(skb, dev, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128}
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003129
3130void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
3131 gfp_t flags)
3132{
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003133 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, NULL);
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003134}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003136void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
3137 gfp_t flags, int *new_nsid)
3138{
3139 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3140 new_nsid);
3141}
3142
John Fastabendd83b0602012-04-15 06:44:08 +00003143static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
3144 struct net_device *dev,
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003145 u8 *addr, u16 vid, u32 pid, u32 seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003146 int type, unsigned int flags,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003147 int nlflags, u16 ndm_state)
John Fastabendd83b0602012-04-15 06:44:08 +00003148{
3149 struct nlmsghdr *nlh;
3150 struct ndmsg *ndm;
3151
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003152 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
John Fastabendd83b0602012-04-15 06:44:08 +00003153 if (!nlh)
3154 return -EMSGSIZE;
3155
3156 ndm = nlmsg_data(nlh);
3157 ndm->ndm_family = AF_BRIDGE;
3158 ndm->ndm_pad1 = 0;
3159 ndm->ndm_pad2 = 0;
3160 ndm->ndm_flags = flags;
3161 ndm->ndm_type = 0;
3162 ndm->ndm_ifindex = dev->ifindex;
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003163 ndm->ndm_state = ndm_state;
John Fastabendd83b0602012-04-15 06:44:08 +00003164
3165 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
3166 goto nla_put_failure;
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003167 if (vid)
3168 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
3169 goto nla_put_failure;
John Fastabendd83b0602012-04-15 06:44:08 +00003170
Johannes Berg053c0952015-01-16 22:09:00 +01003171 nlmsg_end(skb, nlh);
3172 return 0;
John Fastabendd83b0602012-04-15 06:44:08 +00003173
3174nla_put_failure:
3175 nlmsg_cancel(skb, nlh);
3176 return -EMSGSIZE;
3177}
3178
John Fastabend3ff661c2012-04-15 06:44:14 +00003179static inline size_t rtnl_fdb_nlmsg_size(void)
3180{
Sabrina Dubrocaf82ef3e2016-11-18 15:50:39 +01003181 return NLMSG_ALIGN(sizeof(struct ndmsg)) +
3182 nla_total_size(ETH_ALEN) + /* NDA_LLADDR */
3183 nla_total_size(sizeof(u16)) + /* NDA_VLAN */
3184 0;
John Fastabend3ff661c2012-04-15 06:44:14 +00003185}
3186
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003187static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
3188 u16 ndm_state)
John Fastabend3ff661c2012-04-15 06:44:14 +00003189{
3190 struct net *net = dev_net(dev);
3191 struct sk_buff *skb;
3192 int err = -ENOBUFS;
3193
3194 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
3195 if (!skb)
3196 goto errout;
3197
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003198 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003199 0, 0, type, NTF_SELF, 0, ndm_state);
John Fastabend3ff661c2012-04-15 06:44:14 +00003200 if (err < 0) {
3201 kfree_skb(skb);
3202 goto errout;
3203 }
3204
3205 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
3206 return;
3207errout:
3208 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
3209}
3210
Vlad Yasevich090096b2013-03-06 15:39:42 +00003211/**
3212 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3213 */
3214int ndo_dflt_fdb_add(struct ndmsg *ndm,
3215 struct nlattr *tb[],
3216 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003217 const unsigned char *addr, u16 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003218 u16 flags)
3219{
3220 int err = -EINVAL;
3221
3222 /* If aging addresses are supported device will need to
3223 * implement its own handler for this.
3224 */
3225 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
3226 pr_info("%s: FDB only supports static addresses\n", dev->name);
3227 return err;
3228 }
3229
Or Gerlitz65891fe2014-12-14 18:19:05 +02003230 if (vid) {
3231 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
3232 return err;
3233 }
3234
Vlad Yasevich090096b2013-03-06 15:39:42 +00003235 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3236 err = dev_uc_add_excl(dev, addr);
3237 else if (is_multicast_ether_addr(addr))
3238 err = dev_mc_add_excl(dev, addr);
3239
3240 /* Only return duplicate errors if NLM_F_EXCL is set */
3241 if (err == -EEXIST && !(flags & NLM_F_EXCL))
3242 err = 0;
3243
3244 return err;
3245}
3246EXPORT_SYMBOL(ndo_dflt_fdb_add);
3247
Florian Westphalb88d12e2017-10-10 17:10:04 +02003248static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
3249 struct netlink_ext_ack *extack)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003250{
3251 u16 vid = 0;
3252
3253 if (vlan_attr) {
3254 if (nla_len(vlan_attr) != sizeof(u16)) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003255 NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
Jiri Pirkof6f64242014-11-28 14:34:15 +01003256 return -EINVAL;
3257 }
3258
3259 vid = nla_get_u16(vlan_attr);
3260
3261 if (!vid || vid >= VLAN_VID_MASK) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003262 NL_SET_ERR_MSG(extack, "invalid vlan id");
Jiri Pirkof6f64242014-11-28 14:34:15 +01003263 return -EINVAL;
3264 }
3265 }
3266 *p_vid = vid;
3267 return 0;
3268}
3269
David Ahernc21ef3e2017-04-16 09:48:24 -07003270static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
3271 struct netlink_ext_ack *extack)
John Fastabend77162022012-04-15 06:43:56 +00003272{
3273 struct net *net = sock_net(skb->sk);
John Fastabend77162022012-04-15 06:43:56 +00003274 struct ndmsg *ndm;
3275 struct nlattr *tb[NDA_MAX+1];
3276 struct net_device *dev;
3277 u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01003278 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00003279 int err;
3280
David Ahernc21ef3e2017-04-16 09:48:24 -07003281 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
John Fastabend77162022012-04-15 06:43:56 +00003282 if (err < 0)
3283 return err;
3284
3285 ndm = nlmsg_data(nlh);
3286 if (ndm->ndm_ifindex == 0) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003287 NL_SET_ERR_MSG(extack, "invalid ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003288 return -EINVAL;
3289 }
3290
3291 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3292 if (dev == NULL) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003293 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003294 return -ENODEV;
3295 }
3296
3297 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003298 NL_SET_ERR_MSG(extack, "invalid address");
John Fastabend77162022012-04-15 06:43:56 +00003299 return -EINVAL;
3300 }
3301
3302 addr = nla_data(tb[NDA_LLADDR]);
John Fastabend77162022012-04-15 06:43:56 +00003303
Florian Westphalb88d12e2017-10-10 17:10:04 +02003304 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
Jiri Pirkof6f64242014-11-28 14:34:15 +01003305 if (err)
3306 return err;
3307
John Fastabend77162022012-04-15 06:43:56 +00003308 err = -EOPNOTSUPP;
3309
3310 /* Support fdb on master device the net/bridge default case */
3311 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3312 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003313 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3314 const struct net_device_ops *ops = br_dev->netdev_ops;
3315
Jiri Pirkof6f64242014-11-28 14:34:15 +01003316 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
3317 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00003318 if (err)
3319 goto out;
3320 else
3321 ndm->ndm_flags &= ~NTF_MASTER;
3322 }
3323
3324 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00003325 if ((ndm->ndm_flags & NTF_SELF)) {
3326 if (dev->netdev_ops->ndo_fdb_add)
3327 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003328 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003329 nlh->nlmsg_flags);
3330 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01003331 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003332 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00003333
John Fastabend3ff661c2012-04-15 06:44:14 +00003334 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003335 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
3336 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00003337 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00003338 }
John Fastabend77162022012-04-15 06:43:56 +00003339 }
3340out:
3341 return err;
3342}
3343
Vlad Yasevich090096b2013-03-06 15:39:42 +00003344/**
3345 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3346 */
3347int ndo_dflt_fdb_del(struct ndmsg *ndm,
3348 struct nlattr *tb[],
3349 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003350 const unsigned char *addr, u16 vid)
Vlad Yasevich090096b2013-03-06 15:39:42 +00003351{
Alexander Duyckc8a89c42014-07-15 15:15:20 -07003352 int err = -EINVAL;
Vlad Yasevich090096b2013-03-06 15:39:42 +00003353
3354 /* If aging addresses are supported device will need to
3355 * implement its own handler for this.
3356 */
Sridhar Samudrala64535992013-08-08 15:19:48 -07003357 if (!(ndm->ndm_state & NUD_PERMANENT)) {
Vlad Yasevich090096b2013-03-06 15:39:42 +00003358 pr_info("%s: FDB only supports static addresses\n", dev->name);
Alexander Duyckc8a89c42014-07-15 15:15:20 -07003359 return err;
Vlad Yasevich090096b2013-03-06 15:39:42 +00003360 }
3361
3362 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3363 err = dev_uc_del(dev, addr);
3364 else if (is_multicast_ether_addr(addr))
3365 err = dev_mc_del(dev, addr);
Vlad Yasevich090096b2013-03-06 15:39:42 +00003366
3367 return err;
3368}
3369EXPORT_SYMBOL(ndo_dflt_fdb_del);
3370
David Ahernc21ef3e2017-04-16 09:48:24 -07003371static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
3372 struct netlink_ext_ack *extack)
John Fastabend77162022012-04-15 06:43:56 +00003373{
3374 struct net *net = sock_net(skb->sk);
3375 struct ndmsg *ndm;
Vlad Yasevich1690be62013-02-13 12:00:18 +00003376 struct nlattr *tb[NDA_MAX+1];
John Fastabend77162022012-04-15 06:43:56 +00003377 struct net_device *dev;
3378 int err = -EINVAL;
3379 __u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01003380 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00003381
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07003382 if (!netlink_capable(skb, CAP_NET_ADMIN))
Vlad Yasevich1690be62013-02-13 12:00:18 +00003383 return -EPERM;
3384
David Ahernc21ef3e2017-04-16 09:48:24 -07003385 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
Vlad Yasevich1690be62013-02-13 12:00:18 +00003386 if (err < 0)
3387 return err;
John Fastabend77162022012-04-15 06:43:56 +00003388
3389 ndm = nlmsg_data(nlh);
3390 if (ndm->ndm_ifindex == 0) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003391 NL_SET_ERR_MSG(extack, "invalid ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003392 return -EINVAL;
3393 }
3394
3395 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3396 if (dev == NULL) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003397 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003398 return -ENODEV;
3399 }
3400
Vlad Yasevich1690be62013-02-13 12:00:18 +00003401 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003402 NL_SET_ERR_MSG(extack, "invalid address");
John Fastabend77162022012-04-15 06:43:56 +00003403 return -EINVAL;
3404 }
3405
Vlad Yasevich1690be62013-02-13 12:00:18 +00003406 addr = nla_data(tb[NDA_LLADDR]);
Vlad Yasevich1690be62013-02-13 12:00:18 +00003407
Florian Westphalb88d12e2017-10-10 17:10:04 +02003408 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
Jiri Pirkof6f64242014-11-28 14:34:15 +01003409 if (err)
3410 return err;
3411
John Fastabend77162022012-04-15 06:43:56 +00003412 err = -EOPNOTSUPP;
3413
3414 /* Support fdb on master device the net/bridge default case */
3415 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3416 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003417 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3418 const struct net_device_ops *ops = br_dev->netdev_ops;
John Fastabend77162022012-04-15 06:43:56 +00003419
Jiri Pirko898e5062013-01-03 22:48:52 +00003420 if (ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003421 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00003422
3423 if (err)
3424 goto out;
3425 else
3426 ndm->ndm_flags &= ~NTF_MASTER;
3427 }
3428
3429 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00003430 if (ndm->ndm_flags & NTF_SELF) {
3431 if (dev->netdev_ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003432 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
3433 vid);
Vlad Yasevich090096b2013-03-06 15:39:42 +00003434 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01003435 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00003436
John Fastabend3ff661c2012-04-15 06:44:14 +00003437 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003438 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
3439 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00003440 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00003441 }
John Fastabend77162022012-04-15 06:43:56 +00003442 }
3443out:
3444 return err;
3445}
3446
John Fastabendd83b0602012-04-15 06:44:08 +00003447static int nlmsg_populate_fdb(struct sk_buff *skb,
3448 struct netlink_callback *cb,
3449 struct net_device *dev,
3450 int *idx,
3451 struct netdev_hw_addr_list *list)
3452{
3453 struct netdev_hw_addr *ha;
3454 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00003455 u32 portid, seq;
John Fastabendd83b0602012-04-15 06:44:08 +00003456
Eric W. Biederman15e47302012-09-07 20:12:54 +00003457 portid = NETLINK_CB(cb->skb).portid;
John Fastabendd83b0602012-04-15 06:44:08 +00003458 seq = cb->nlh->nlmsg_seq;
3459
3460 list_for_each_entry(ha, &list->list, list) {
Roopa Prabhud2976532016-08-30 21:56:45 -07003461 if (*idx < cb->args[2])
John Fastabendd83b0602012-04-15 06:44:08 +00003462 goto skip;
3463
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003464 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
John Fastabenda7a558f2012-11-01 16:23:10 +00003465 portid, seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003466 RTM_NEWNEIGH, NTF_SELF,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003467 NLM_F_MULTI, NUD_PERMANENT);
John Fastabendd83b0602012-04-15 06:44:08 +00003468 if (err < 0)
3469 return err;
3470skip:
3471 *idx += 1;
3472 }
3473 return 0;
3474}
3475
3476/**
Ben Hutchings2c530402012-07-10 10:55:09 +00003477 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
John Fastabendd83b0602012-04-15 06:44:08 +00003478 * @nlh: netlink message header
3479 * @dev: netdevice
3480 *
3481 * Default netdevice operation to dump the existing unicast address list.
John Fastabend91f3e7b2013-03-29 08:18:37 +00003482 * Returns number of addresses from list put in skb.
John Fastabendd83b0602012-04-15 06:44:08 +00003483 */
3484int ndo_dflt_fdb_dump(struct sk_buff *skb,
3485 struct netlink_callback *cb,
3486 struct net_device *dev,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04003487 struct net_device *filter_dev,
Roopa Prabhud2976532016-08-30 21:56:45 -07003488 int *idx)
John Fastabendd83b0602012-04-15 06:44:08 +00003489{
3490 int err;
3491
3492 netif_addr_lock_bh(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -07003493 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
John Fastabendd83b0602012-04-15 06:44:08 +00003494 if (err)
3495 goto out;
Zhang Shengju2934c9d2016-11-30 16:37:34 +08003496 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
John Fastabendd83b0602012-04-15 06:44:08 +00003497out:
3498 netif_addr_unlock_bh(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -07003499 return err;
John Fastabendd83b0602012-04-15 06:44:08 +00003500}
3501EXPORT_SYMBOL(ndo_dflt_fdb_dump);
3502
John Fastabend77162022012-04-15 06:43:56 +00003503static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
3504{
John Fastabend77162022012-04-15 06:43:56 +00003505 struct net_device *dev;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003506 struct nlattr *tb[IFLA_MAX+1];
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003507 struct net_device *br_dev = NULL;
3508 const struct net_device_ops *ops = NULL;
3509 const struct net_device_ops *cops = NULL;
3510 struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
3511 struct net *net = sock_net(skb->sk);
Roopa Prabhud2976532016-08-30 21:56:45 -07003512 struct hlist_head *head;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003513 int brport_idx = 0;
3514 int br_idx = 0;
Roopa Prabhud2976532016-08-30 21:56:45 -07003515 int h, s_h;
3516 int idx = 0, s_idx;
3517 int err = 0;
3518 int fidx = 0;
John Fastabend77162022012-04-15 06:43:56 +00003519
Alexander Potapenko0ff50e82017-05-23 13:20:28 +02003520 err = nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb,
3521 IFLA_MAX, ifla_policy, NULL);
3522 if (err < 0) {
3523 return -EINVAL;
3524 } else if (err == 0) {
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003525 if (tb[IFLA_MASTER])
3526 br_idx = nla_get_u32(tb[IFLA_MASTER]);
3527 }
John Fastabend77162022012-04-15 06:43:56 +00003528
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003529 brport_idx = ifm->ifi_index;
3530
3531 if (br_idx) {
3532 br_dev = __dev_get_by_index(net, br_idx);
3533 if (!br_dev)
3534 return -ENODEV;
3535
3536 ops = br_dev->netdev_ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003537 }
3538
Roopa Prabhud2976532016-08-30 21:56:45 -07003539 s_h = cb->args[0];
3540 s_idx = cb->args[1];
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003541
Roopa Prabhud2976532016-08-30 21:56:45 -07003542 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
3543 idx = 0;
3544 head = &net->dev_index_head[h];
3545 hlist_for_each_entry(dev, head, index_hlist) {
3546
3547 if (brport_idx && (dev->ifindex != brport_idx))
3548 continue;
3549
3550 if (!br_idx) { /* user did not specify a specific bridge */
3551 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3552 br_dev = netdev_master_upper_dev_get(dev);
3553 cops = br_dev->netdev_ops;
3554 }
3555 } else {
3556 if (dev != br_dev &&
3557 !(dev->priv_flags & IFF_BRIDGE_PORT))
3558 continue;
3559
3560 if (br_dev != netdev_master_upper_dev_get(dev) &&
3561 !(dev->priv_flags & IFF_EBRIDGE))
3562 continue;
3563 cops = ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003564 }
3565
Roopa Prabhud2976532016-08-30 21:56:45 -07003566 if (idx < s_idx)
3567 goto cont;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003568
Roopa Prabhud2976532016-08-30 21:56:45 -07003569 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3570 if (cops && cops->ndo_fdb_dump) {
3571 err = cops->ndo_fdb_dump(skb, cb,
3572 br_dev, dev,
3573 &fidx);
3574 if (err == -EMSGSIZE)
3575 goto out;
3576 }
3577 }
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003578
Roopa Prabhud2976532016-08-30 21:56:45 -07003579 if (dev->netdev_ops->ndo_fdb_dump)
3580 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
3581 dev, NULL,
3582 &fidx);
3583 else
3584 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
3585 &fidx);
3586 if (err == -EMSGSIZE)
3587 goto out;
3588
3589 cops = NULL;
3590
3591 /* reset fdb offset to 0 for rest of the interfaces */
3592 cb->args[2] = 0;
3593 fidx = 0;
3594cont:
3595 idx++;
John Fastabend77162022012-04-15 06:43:56 +00003596 }
John Fastabend77162022012-04-15 06:43:56 +00003597 }
John Fastabend77162022012-04-15 06:43:56 +00003598
Roopa Prabhud2976532016-08-30 21:56:45 -07003599out:
3600 cb->args[0] = h;
3601 cb->args[1] = idx;
3602 cb->args[2] = fidx;
3603
John Fastabend77162022012-04-15 06:43:56 +00003604 return skb->len;
3605}
3606
Scott Feldman2c3c0312014-11-28 14:34:25 +01003607static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
3608 unsigned int attrnum, unsigned int flag)
3609{
3610 if (mask & flag)
3611 return nla_put_u8(skb, attrnum, !!(flags & flag));
3612 return 0;
3613}
3614
John Fastabend815cccb2012-10-24 08:13:09 +00003615int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Scott Feldman2c3c0312014-11-28 14:34:25 +01003616 struct net_device *dev, u16 mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003617 u32 flags, u32 mask, int nlflags,
3618 u32 filter_mask,
3619 int (*vlan_fill)(struct sk_buff *skb,
3620 struct net_device *dev,
3621 u32 filter_mask))
John Fastabend815cccb2012-10-24 08:13:09 +00003622{
3623 struct nlmsghdr *nlh;
3624 struct ifinfomsg *ifm;
3625 struct nlattr *br_afspec;
Scott Feldman2c3c0312014-11-28 14:34:25 +01003626 struct nlattr *protinfo;
John Fastabend815cccb2012-10-24 08:13:09 +00003627 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Jiri Pirko898e5062013-01-03 22:48:52 +00003628 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003629 int err = 0;
John Fastabend815cccb2012-10-24 08:13:09 +00003630
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02003631 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
John Fastabend815cccb2012-10-24 08:13:09 +00003632 if (nlh == NULL)
3633 return -EMSGSIZE;
3634
3635 ifm = nlmsg_data(nlh);
3636 ifm->ifi_family = AF_BRIDGE;
3637 ifm->__ifi_pad = 0;
3638 ifm->ifi_type = dev->type;
3639 ifm->ifi_index = dev->ifindex;
3640 ifm->ifi_flags = dev_get_flags(dev);
3641 ifm->ifi_change = 0;
3642
3643
3644 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
3645 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
3646 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00003647 (br_dev &&
3648 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
John Fastabend815cccb2012-10-24 08:13:09 +00003649 (dev->addr_len &&
3650 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02003651 (dev->ifindex != dev_get_iflink(dev) &&
3652 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
John Fastabend815cccb2012-10-24 08:13:09 +00003653 goto nla_put_failure;
3654
3655 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
3656 if (!br_afspec)
3657 goto nla_put_failure;
3658
Roopa Prabhu1d460b92014-12-08 14:04:20 -08003659 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
John Fastabend815cccb2012-10-24 08:13:09 +00003660 nla_nest_cancel(skb, br_afspec);
3661 goto nla_put_failure;
3662 }
Roopa Prabhu1d460b92014-12-08 14:04:20 -08003663
3664 if (mode != BRIDGE_MODE_UNDEF) {
3665 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
3666 nla_nest_cancel(skb, br_afspec);
3667 goto nla_put_failure;
3668 }
3669 }
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003670 if (vlan_fill) {
3671 err = vlan_fill(skb, dev, filter_mask);
3672 if (err) {
3673 nla_nest_cancel(skb, br_afspec);
3674 goto nla_put_failure;
3675 }
3676 }
John Fastabend815cccb2012-10-24 08:13:09 +00003677 nla_nest_end(skb, br_afspec);
3678
Scott Feldman2c3c0312014-11-28 14:34:25 +01003679 protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
3680 if (!protinfo)
3681 goto nla_put_failure;
3682
3683 if (brport_nla_put_flag(skb, flags, mask,
3684 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
3685 brport_nla_put_flag(skb, flags, mask,
3686 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
3687 brport_nla_put_flag(skb, flags, mask,
3688 IFLA_BRPORT_FAST_LEAVE,
3689 BR_MULTICAST_FAST_LEAVE) ||
3690 brport_nla_put_flag(skb, flags, mask,
3691 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
3692 brport_nla_put_flag(skb, flags, mask,
3693 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
3694 brport_nla_put_flag(skb, flags, mask,
3695 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
3696 brport_nla_put_flag(skb, flags, mask,
3697 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
3698 brport_nla_put_flag(skb, flags, mask,
3699 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
3700 nla_nest_cancel(skb, protinfo);
3701 goto nla_put_failure;
3702 }
3703
3704 nla_nest_end(skb, protinfo);
3705
Johannes Berg053c0952015-01-16 22:09:00 +01003706 nlmsg_end(skb, nlh);
3707 return 0;
John Fastabend815cccb2012-10-24 08:13:09 +00003708nla_put_failure:
3709 nlmsg_cancel(skb, nlh);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003710 return err ? err : -EMSGSIZE;
John Fastabend815cccb2012-10-24 08:13:09 +00003711}
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003712EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
John Fastabend815cccb2012-10-24 08:13:09 +00003713
John Fastabende5a55a82012-10-24 08:12:57 +00003714static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
3715{
3716 struct net *net = sock_net(skb->sk);
3717 struct net_device *dev;
3718 int idx = 0;
3719 u32 portid = NETLINK_CB(cb->skb).portid;
3720 u32 seq = cb->nlh->nlmsg_seq;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00003721 u32 filter_mask = 0;
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003722 int err;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00003723
Thomas Grafaa68c202014-11-26 13:42:20 +01003724 if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
3725 struct nlattr *extfilt;
3726
3727 extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
3728 IFLA_EXT_MASK);
3729 if (extfilt) {
3730 if (nla_len(extfilt) < sizeof(filter_mask))
3731 return -EINVAL;
3732
3733 filter_mask = nla_get_u32(extfilt);
3734 }
3735 }
John Fastabende5a55a82012-10-24 08:12:57 +00003736
3737 rcu_read_lock();
3738 for_each_netdev_rcu(net, dev) {
3739 const struct net_device_ops *ops = dev->netdev_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00003740 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
John Fastabende5a55a82012-10-24 08:12:57 +00003741
Jiri Pirko898e5062013-01-03 22:48:52 +00003742 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003743 if (idx >= cb->args[0]) {
3744 err = br_dev->netdev_ops->ndo_bridge_getlink(
3745 skb, portid, seq, dev,
3746 filter_mask, NLM_F_MULTI);
David Ahernf6c57752017-05-15 23:19:17 -07003747 if (err < 0 && err != -EOPNOTSUPP) {
3748 if (likely(skb->len))
3749 break;
3750
3751 goto out_err;
3752 }
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003753 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00003754 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00003755 }
3756
3757 if (ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003758 if (idx >= cb->args[0]) {
3759 err = ops->ndo_bridge_getlink(skb, portid,
3760 seq, dev,
3761 filter_mask,
3762 NLM_F_MULTI);
David Ahernf6c57752017-05-15 23:19:17 -07003763 if (err < 0 && err != -EOPNOTSUPP) {
3764 if (likely(skb->len))
3765 break;
3766
3767 goto out_err;
3768 }
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003769 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00003770 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00003771 }
3772 }
David Ahernf6c57752017-05-15 23:19:17 -07003773 err = skb->len;
3774out_err:
John Fastabende5a55a82012-10-24 08:12:57 +00003775 rcu_read_unlock();
3776 cb->args[0] = idx;
3777
David Ahernf6c57752017-05-15 23:19:17 -07003778 return err;
John Fastabende5a55a82012-10-24 08:12:57 +00003779}
3780
John Fastabend2469ffd2012-10-24 08:13:03 +00003781static inline size_t bridge_nlmsg_size(void)
3782{
3783 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3784 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3785 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3786 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
3787 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
3788 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
3789 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
3790 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
3791 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
3792 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
3793 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
3794}
3795
Roopa Prabhu02dba432015-01-14 20:02:25 -08003796static int rtnl_bridge_notify(struct net_device *dev)
John Fastabend2469ffd2012-10-24 08:13:03 +00003797{
3798 struct net *net = dev_net(dev);
John Fastabend2469ffd2012-10-24 08:13:03 +00003799 struct sk_buff *skb;
3800 int err = -EOPNOTSUPP;
3801
Roopa Prabhu02dba432015-01-14 20:02:25 -08003802 if (!dev->netdev_ops->ndo_bridge_getlink)
3803 return 0;
3804
John Fastabend2469ffd2012-10-24 08:13:03 +00003805 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
3806 if (!skb) {
3807 err = -ENOMEM;
3808 goto errout;
3809 }
3810
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02003811 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
Roopa Prabhu02dba432015-01-14 20:02:25 -08003812 if (err < 0)
3813 goto errout;
John Fastabend2469ffd2012-10-24 08:13:03 +00003814
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08003815 if (!skb->len)
3816 goto errout;
3817
John Fastabend2469ffd2012-10-24 08:13:03 +00003818 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
3819 return 0;
3820errout:
3821 WARN_ON(err == -EMSGSIZE);
3822 kfree_skb(skb);
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08003823 if (err)
3824 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
John Fastabend2469ffd2012-10-24 08:13:03 +00003825 return err;
3826}
3827
David Ahernc21ef3e2017-04-16 09:48:24 -07003828static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3829 struct netlink_ext_ack *extack)
John Fastabende5a55a82012-10-24 08:12:57 +00003830{
3831 struct net *net = sock_net(skb->sk);
3832 struct ifinfomsg *ifm;
3833 struct net_device *dev;
John Fastabend2469ffd2012-10-24 08:13:03 +00003834 struct nlattr *br_spec, *attr = NULL;
3835 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02003836 u16 flags = 0;
John Fastabendc38e01b2012-11-02 16:32:36 +00003837 bool have_flags = false;
John Fastabende5a55a82012-10-24 08:12:57 +00003838
3839 if (nlmsg_len(nlh) < sizeof(*ifm))
3840 return -EINVAL;
3841
3842 ifm = nlmsg_data(nlh);
3843 if (ifm->ifi_family != AF_BRIDGE)
3844 return -EPFNOSUPPORT;
3845
3846 dev = __dev_get_by_index(net, ifm->ifi_index);
3847 if (!dev) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003848 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabende5a55a82012-10-24 08:12:57 +00003849 return -ENODEV;
3850 }
3851
John Fastabend2469ffd2012-10-24 08:13:03 +00003852 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3853 if (br_spec) {
3854 nla_for_each_nested(attr, br_spec, rem) {
3855 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01003856 if (nla_len(attr) < sizeof(flags))
3857 return -EINVAL;
3858
John Fastabendc38e01b2012-11-02 16:32:36 +00003859 have_flags = true;
John Fastabend2469ffd2012-10-24 08:13:03 +00003860 flags = nla_get_u16(attr);
3861 break;
3862 }
3863 }
3864 }
3865
3866 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003867 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3868
3869 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
John Fastabend2469ffd2012-10-24 08:13:03 +00003870 err = -EOPNOTSUPP;
3871 goto out;
3872 }
3873
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003874 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
John Fastabende5a55a82012-10-24 08:12:57 +00003875 if (err)
3876 goto out;
John Fastabend2469ffd2012-10-24 08:13:03 +00003877
3878 flags &= ~BRIDGE_FLAGS_MASTER;
John Fastabende5a55a82012-10-24 08:12:57 +00003879 }
3880
John Fastabend2469ffd2012-10-24 08:13:03 +00003881 if ((flags & BRIDGE_FLAGS_SELF)) {
3882 if (!dev->netdev_ops->ndo_bridge_setlink)
3883 err = -EOPNOTSUPP;
3884 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003885 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
3886 flags);
Roopa Prabhu02dba432015-01-14 20:02:25 -08003887 if (!err) {
John Fastabend2469ffd2012-10-24 08:13:03 +00003888 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08003889
3890 /* Generate event to notify upper layer of bridge
3891 * change
3892 */
3893 err = rtnl_bridge_notify(dev);
3894 }
John Fastabend2469ffd2012-10-24 08:13:03 +00003895 }
3896
John Fastabendc38e01b2012-11-02 16:32:36 +00003897 if (have_flags)
John Fastabend2469ffd2012-10-24 08:13:03 +00003898 memcpy(nla_data(attr), &flags, sizeof(flags));
John Fastabende5a55a82012-10-24 08:12:57 +00003899out:
3900 return err;
3901}
3902
David Ahernc21ef3e2017-04-16 09:48:24 -07003903static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
3904 struct netlink_ext_ack *extack)
Vlad Yasevich407af322013-02-13 12:00:12 +00003905{
3906 struct net *net = sock_net(skb->sk);
3907 struct ifinfomsg *ifm;
3908 struct net_device *dev;
3909 struct nlattr *br_spec, *attr = NULL;
3910 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02003911 u16 flags = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +00003912 bool have_flags = false;
3913
3914 if (nlmsg_len(nlh) < sizeof(*ifm))
3915 return -EINVAL;
3916
3917 ifm = nlmsg_data(nlh);
3918 if (ifm->ifi_family != AF_BRIDGE)
3919 return -EPFNOSUPPORT;
3920
3921 dev = __dev_get_by_index(net, ifm->ifi_index);
3922 if (!dev) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003923 NL_SET_ERR_MSG(extack, "unknown ifindex");
Vlad Yasevich407af322013-02-13 12:00:12 +00003924 return -ENODEV;
3925 }
3926
3927 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3928 if (br_spec) {
3929 nla_for_each_nested(attr, br_spec, rem) {
3930 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01003931 if (nla_len(attr) < sizeof(flags))
3932 return -EINVAL;
3933
Vlad Yasevich407af322013-02-13 12:00:12 +00003934 have_flags = true;
3935 flags = nla_get_u16(attr);
3936 break;
3937 }
3938 }
3939 }
3940
Vlad Yasevich407af322013-02-13 12:00:12 +00003941 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3942 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3943
3944 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
3945 err = -EOPNOTSUPP;
3946 goto out;
3947 }
3948
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003949 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00003950 if (err)
3951 goto out;
3952
3953 flags &= ~BRIDGE_FLAGS_MASTER;
3954 }
3955
3956 if ((flags & BRIDGE_FLAGS_SELF)) {
3957 if (!dev->netdev_ops->ndo_bridge_dellink)
3958 err = -EOPNOTSUPP;
3959 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003960 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
3961 flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00003962
Roopa Prabhu02dba432015-01-14 20:02:25 -08003963 if (!err) {
Vlad Yasevich407af322013-02-13 12:00:12 +00003964 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08003965
3966 /* Generate event to notify upper layer of bridge
3967 * change
3968 */
3969 err = rtnl_bridge_notify(dev);
3970 }
Vlad Yasevich407af322013-02-13 12:00:12 +00003971 }
3972
3973 if (have_flags)
3974 memcpy(nla_data(attr), &flags, sizeof(flags));
Vlad Yasevich407af322013-02-13 12:00:12 +00003975out:
3976 return err;
3977}
3978
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003979static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
3980{
3981 return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
3982 (!idxattr || idxattr == attrid);
3983}
3984
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02003985#define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
3986static int rtnl_get_offload_stats_attr_size(int attr_id)
3987{
3988 switch (attr_id) {
3989 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
3990 return sizeof(struct rtnl_link_stats64);
3991 }
3992
3993 return 0;
3994}
3995
3996static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
3997 int *prividx)
3998{
3999 struct nlattr *attr = NULL;
4000 int attr_id, size;
4001 void *attr_data;
4002 int err;
4003
4004 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
4005 dev->netdev_ops->ndo_get_offload_stats))
4006 return -ENODATA;
4007
4008 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
4009 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
4010 if (attr_id < *prividx)
4011 continue;
4012
4013 size = rtnl_get_offload_stats_attr_size(attr_id);
4014 if (!size)
4015 continue;
4016
Or Gerlitz3df5b3c2016-11-22 23:09:54 +02004017 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004018 continue;
4019
4020 attr = nla_reserve_64bit(skb, attr_id, size,
4021 IFLA_OFFLOAD_XSTATS_UNSPEC);
4022 if (!attr)
4023 goto nla_put_failure;
4024
4025 attr_data = nla_data(attr);
4026 memset(attr_data, 0, size);
4027 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
4028 attr_data);
4029 if (err)
4030 goto get_offload_stats_failure;
4031 }
4032
4033 if (!attr)
4034 return -ENODATA;
4035
4036 *prividx = 0;
4037 return 0;
4038
4039nla_put_failure:
4040 err = -EMSGSIZE;
4041get_offload_stats_failure:
4042 *prividx = attr_id;
4043 return err;
4044}
4045
4046static int rtnl_get_offload_stats_size(const struct net_device *dev)
4047{
4048 int nla_size = 0;
4049 int attr_id;
4050 int size;
4051
4052 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
4053 dev->netdev_ops->ndo_get_offload_stats))
4054 return 0;
4055
4056 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
4057 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
Or Gerlitz3df5b3c2016-11-22 23:09:54 +02004058 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004059 continue;
4060 size = rtnl_get_offload_stats_attr_size(attr_id);
4061 nla_size += nla_total_size_64bit(size);
4062 }
4063
4064 if (nla_size != 0)
4065 nla_size += nla_total_size(0);
4066
4067 return nla_size;
4068}
4069
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004070static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
4071 int type, u32 pid, u32 seq, u32 change,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004072 unsigned int flags, unsigned int filter_mask,
4073 int *idxattr, int *prividx)
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004074{
4075 struct if_stats_msg *ifsm;
4076 struct nlmsghdr *nlh;
4077 struct nlattr *attr;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004078 int s_prividx = *prividx;
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004079 int err;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004080
4081 ASSERT_RTNL();
4082
4083 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
4084 if (!nlh)
4085 return -EMSGSIZE;
4086
4087 ifsm = nlmsg_data(nlh);
Nikolay Aleksandrovce024f42017-10-03 13:20:48 +03004088 ifsm->family = PF_UNSPEC;
4089 ifsm->pad1 = 0;
4090 ifsm->pad2 = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004091 ifsm->ifindex = dev->ifindex;
4092 ifsm->filter_mask = filter_mask;
4093
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004094 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004095 struct rtnl_link_stats64 *sp;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004096
Nicolas Dichtel58414d32016-04-21 18:58:25 +02004097 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
4098 sizeof(struct rtnl_link_stats64),
4099 IFLA_STATS_UNSPEC);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004100 if (!attr)
4101 goto nla_put_failure;
4102
4103 sp = nla_data(attr);
4104 dev_get_stats(dev, sp);
4105 }
4106
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004107 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
4108 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
4109
4110 if (ops && ops->fill_linkxstats) {
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004111 *idxattr = IFLA_STATS_LINK_XSTATS;
4112 attr = nla_nest_start(skb,
4113 IFLA_STATS_LINK_XSTATS);
4114 if (!attr)
4115 goto nla_put_failure;
4116
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004117 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
4118 nla_nest_end(skb, attr);
4119 if (err)
4120 goto nla_put_failure;
4121 *idxattr = 0;
4122 }
4123 }
4124
4125 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
4126 *idxattr)) {
4127 const struct rtnl_link_ops *ops = NULL;
4128 const struct net_device *master;
4129
4130 master = netdev_master_upper_dev_get(dev);
4131 if (master)
4132 ops = master->rtnl_link_ops;
4133 if (ops && ops->fill_linkxstats) {
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004134 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
4135 attr = nla_nest_start(skb,
4136 IFLA_STATS_LINK_XSTATS_SLAVE);
4137 if (!attr)
4138 goto nla_put_failure;
4139
4140 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004141 nla_nest_end(skb, attr);
4142 if (err)
4143 goto nla_put_failure;
4144 *idxattr = 0;
4145 }
4146 }
4147
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004148 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
4149 *idxattr)) {
4150 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
4151 attr = nla_nest_start(skb, IFLA_STATS_LINK_OFFLOAD_XSTATS);
4152 if (!attr)
4153 goto nla_put_failure;
4154
4155 err = rtnl_get_offload_stats(skb, dev, prividx);
4156 if (err == -ENODATA)
4157 nla_nest_cancel(skb, attr);
4158 else
4159 nla_nest_end(skb, attr);
4160
4161 if (err && err != -ENODATA)
4162 goto nla_put_failure;
4163 *idxattr = 0;
4164 }
4165
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004166 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
4167 struct rtnl_af_ops *af_ops;
4168
4169 *idxattr = IFLA_STATS_AF_SPEC;
4170 attr = nla_nest_start(skb, IFLA_STATS_AF_SPEC);
4171 if (!attr)
4172 goto nla_put_failure;
4173
Florian Westphal5fa85a02017-10-16 15:44:36 +02004174 rcu_read_lock();
4175 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004176 if (af_ops->fill_stats_af) {
4177 struct nlattr *af;
4178 int err;
4179
4180 af = nla_nest_start(skb, af_ops->family);
Florian Westphal5fa85a02017-10-16 15:44:36 +02004181 if (!af) {
4182 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004183 goto nla_put_failure;
Florian Westphal5fa85a02017-10-16 15:44:36 +02004184 }
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004185 err = af_ops->fill_stats_af(skb, dev);
4186
Florian Westphal5fa85a02017-10-16 15:44:36 +02004187 if (err == -ENODATA) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004188 nla_nest_cancel(skb, af);
Florian Westphal5fa85a02017-10-16 15:44:36 +02004189 } else if (err < 0) {
4190 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004191 goto nla_put_failure;
Florian Westphal5fa85a02017-10-16 15:44:36 +02004192 }
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004193
4194 nla_nest_end(skb, af);
4195 }
4196 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02004197 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004198
4199 nla_nest_end(skb, attr);
4200
4201 *idxattr = 0;
4202 }
4203
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004204 nlmsg_end(skb, nlh);
4205
4206 return 0;
4207
4208nla_put_failure:
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004209 /* not a multi message or no progress mean a real error */
4210 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
4211 nlmsg_cancel(skb, nlh);
4212 else
4213 nlmsg_end(skb, nlh);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004214
4215 return -EMSGSIZE;
4216}
4217
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004218static size_t if_nlmsg_stats_size(const struct net_device *dev,
4219 u32 filter_mask)
4220{
4221 size_t size = 0;
4222
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004223 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004224 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
4225
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004226 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
4227 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004228 int attr = IFLA_STATS_LINK_XSTATS;
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004229
4230 if (ops && ops->get_linkxstats_size) {
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004231 size += nla_total_size(ops->get_linkxstats_size(dev,
4232 attr));
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004233 /* for IFLA_STATS_LINK_XSTATS */
4234 size += nla_total_size(0);
4235 }
4236 }
4237
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004238 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
4239 struct net_device *_dev = (struct net_device *)dev;
4240 const struct rtnl_link_ops *ops = NULL;
4241 const struct net_device *master;
4242
4243 /* netdev_master_upper_dev_get can't take const */
4244 master = netdev_master_upper_dev_get(_dev);
4245 if (master)
4246 ops = master->rtnl_link_ops;
4247 if (ops && ops->get_linkxstats_size) {
4248 int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
4249
4250 size += nla_total_size(ops->get_linkxstats_size(dev,
4251 attr));
4252 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
4253 size += nla_total_size(0);
4254 }
4255 }
4256
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004257 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
4258 size += rtnl_get_offload_stats_size(dev);
4259
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004260 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
4261 struct rtnl_af_ops *af_ops;
4262
4263 /* for IFLA_STATS_AF_SPEC */
4264 size += nla_total_size(0);
4265
Florian Westphal5fa85a02017-10-16 15:44:36 +02004266 rcu_read_lock();
4267 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004268 if (af_ops->get_stats_af_size) {
4269 size += nla_total_size(
4270 af_ops->get_stats_af_size(dev));
4271
4272 /* for AF_* */
4273 size += nla_total_size(0);
4274 }
4275 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02004276 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004277 }
4278
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004279 return size;
4280}
4281
David Ahernc21ef3e2017-04-16 09:48:24 -07004282static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
4283 struct netlink_ext_ack *extack)
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004284{
4285 struct net *net = sock_net(skb->sk);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004286 struct net_device *dev = NULL;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004287 int idxattr = 0, prividx = 0;
4288 struct if_stats_msg *ifsm;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004289 struct sk_buff *nskb;
4290 u32 filter_mask;
4291 int err;
4292
Mathias Krause4775cc12016-12-28 17:52:15 +01004293 if (nlmsg_len(nlh) < sizeof(*ifsm))
4294 return -EINVAL;
4295
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004296 ifsm = nlmsg_data(nlh);
4297 if (ifsm->ifindex > 0)
4298 dev = __dev_get_by_index(net, ifsm->ifindex);
4299 else
4300 return -EINVAL;
4301
4302 if (!dev)
4303 return -ENODEV;
4304
4305 filter_mask = ifsm->filter_mask;
4306 if (!filter_mask)
4307 return -EINVAL;
4308
4309 nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
4310 if (!nskb)
4311 return -ENOBUFS;
4312
4313 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
4314 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004315 0, filter_mask, &idxattr, &prividx);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004316 if (err < 0) {
4317 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
4318 WARN_ON(err == -EMSGSIZE);
4319 kfree_skb(nskb);
4320 } else {
4321 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
4322 }
4323
4324 return err;
4325}
4326
4327static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
4328{
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004329 int h, s_h, err, s_idx, s_idxattr, s_prividx;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004330 struct net *net = sock_net(skb->sk);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004331 unsigned int flags = NLM_F_MULTI;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004332 struct if_stats_msg *ifsm;
4333 struct hlist_head *head;
4334 struct net_device *dev;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004335 u32 filter_mask = 0;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004336 int idx = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004337
4338 s_h = cb->args[0];
4339 s_idx = cb->args[1];
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004340 s_idxattr = cb->args[2];
4341 s_prividx = cb->args[3];
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004342
4343 cb->seq = net->dev_base_seq;
4344
Mathias Krause4775cc12016-12-28 17:52:15 +01004345 if (nlmsg_len(cb->nlh) < sizeof(*ifsm))
4346 return -EINVAL;
4347
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004348 ifsm = nlmsg_data(cb->nlh);
4349 filter_mask = ifsm->filter_mask;
4350 if (!filter_mask)
4351 return -EINVAL;
4352
4353 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4354 idx = 0;
4355 head = &net->dev_index_head[h];
4356 hlist_for_each_entry(dev, head, index_hlist) {
4357 if (idx < s_idx)
4358 goto cont;
4359 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
4360 NETLINK_CB(cb->skb).portid,
4361 cb->nlh->nlmsg_seq, 0,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004362 flags, filter_mask,
4363 &s_idxattr, &s_prividx);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004364 /* If we ran out of room on the first message,
4365 * we're in trouble
4366 */
4367 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
4368
4369 if (err < 0)
4370 goto out;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004371 s_prividx = 0;
4372 s_idxattr = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004373 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
4374cont:
4375 idx++;
4376 }
4377 }
4378out:
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004379 cb->args[3] = s_prividx;
4380 cb->args[2] = s_idxattr;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004381 cb->args[1] = idx;
4382 cb->args[0] = h;
4383
4384 return skb->len;
4385}
4386
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387/* Process one rtnetlink message. */
4388
Johannes Berg2d4bc932017-04-12 14:34:04 +02004389static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
4390 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004392 struct net *net = sock_net(skb->sk);
Florian Westphaladdf9b92017-12-02 21:44:05 +01004393 struct rtnl_link *link;
Florian Westphale4202512017-12-02 21:44:06 +01004394 struct module *owner;
Florian Westphal6853dd42017-08-09 20:41:51 +02004395 int err = -EOPNOTSUPP;
Thomas Grafe2849862007-03-22 11:48:11 -07004396 rtnl_doit_func doit;
Florian Westphal62256f92017-08-09 20:41:52 +02004397 unsigned int flags;
Alexander Kuleshov617cfc72016-01-10 21:26:57 +06004398 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 int family;
4400 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 if (type > RTM_MAX)
Thomas Graf038890f2007-04-05 14:35:52 -07004404 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405
4406 type -= RTM_BASE;
4407
4408 /* All the messages must have at least 1 byte length */
Hong zhi guo573ce262013-03-27 06:47:04 +00004409 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 return 0;
4411
Hong zhi guo573ce262013-03-27 06:47:04 +00004412 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 kind = type&3;
4414
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07004415 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07004416 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417
Florian Westphal6853dd42017-08-09 20:41:51 +02004418 rcu_read_lock();
David S. Millerb8f3ab42011-01-18 12:40:38 -08004419 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004420 struct sock *rtnl;
Thomas Grafe2849862007-03-22 11:48:11 -07004421 rtnl_dumpit_func dumpit;
Greg Rosec7ac8672011-06-10 01:27:09 +00004422 u16 min_dump_alloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423
Florian Westphaladdf9b92017-12-02 21:44:05 +01004424 link = rtnl_get_link(family, type);
4425 if (!link || !link->dumpit) {
Florian Westphal6853dd42017-08-09 20:41:51 +02004426 family = PF_UNSPEC;
Florian Westphaladdf9b92017-12-02 21:44:05 +01004427 link = rtnl_get_link(family, type);
4428 if (!link || !link->dumpit)
Florian Westphal6853dd42017-08-09 20:41:51 +02004429 goto err_unlock;
4430 }
Florian Westphale4202512017-12-02 21:44:06 +01004431 owner = link->owner;
Florian Westphaladdf9b92017-12-02 21:44:05 +01004432 dumpit = link->dumpit;
Florian Westphale1fa6d22017-08-09 20:41:47 +02004433
Florian Westphal5c2bb9b2017-08-10 16:52:58 +02004434 if (type == RTM_GETLINK - RTM_BASE)
Florian Westphale1fa6d22017-08-09 20:41:47 +02004435 min_dump_alloc = rtnl_calcit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436
Florian Westphale4202512017-12-02 21:44:06 +01004437 err = 0;
4438 /* need to do this before rcu_read_unlock() */
4439 if (!try_module_get(owner))
4440 err = -EPROTONOSUPPORT;
4441
Florian Westphal6853dd42017-08-09 20:41:51 +02004442 rcu_read_unlock();
4443
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004444 rtnl = net->rtnl;
Florian Westphale4202512017-12-02 21:44:06 +01004445 if (err == 0) {
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00004446 struct netlink_dump_control c = {
4447 .dump = dumpit,
4448 .min_dump_alloc = min_dump_alloc,
Florian Westphale4202512017-12-02 21:44:06 +01004449 .module = owner,
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00004450 };
4451 err = netlink_dump_start(rtnl, skb, nlh, &c);
Florian Westphale4202512017-12-02 21:44:06 +01004452 /* netlink_dump_start() will keep a reference on
4453 * module if dump is still in progress.
4454 */
4455 module_put(owner);
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00004456 }
Eric Dumazet2907c352011-05-25 07:34:04 +00004457 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 }
4459
Florian Westphaladdf9b92017-12-02 21:44:05 +01004460 link = rtnl_get_link(family, type);
4461 if (!link || !link->doit) {
Florian Westphal8caa38b2017-08-10 16:53:01 +02004462 family = PF_UNSPEC;
Florian Westphaladdf9b92017-12-02 21:44:05 +01004463 link = rtnl_get_link(PF_UNSPEC, type);
4464 if (!link || !link->doit)
4465 goto out_unlock;
Florian Westphal8caa38b2017-08-10 16:53:01 +02004466 }
4467
Florian Westphale4202512017-12-02 21:44:06 +01004468 owner = link->owner;
4469 if (!try_module_get(owner)) {
4470 err = -EPROTONOSUPPORT;
4471 goto out_unlock;
4472 }
4473
Florian Westphaladdf9b92017-12-02 21:44:05 +01004474 flags = link->flags;
Florian Westphal62256f92017-08-09 20:41:52 +02004475 if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
Florian Westphaladdf9b92017-12-02 21:44:05 +01004476 doit = link->doit;
Florian Westphal62256f92017-08-09 20:41:52 +02004477 rcu_read_unlock();
4478 if (doit)
4479 err = doit(skb, nlh, extack);
Florian Westphale4202512017-12-02 21:44:06 +01004480 module_put(owner);
Florian Westphal62256f92017-08-09 20:41:52 +02004481 return err;
4482 }
Florian Westphal6853dd42017-08-09 20:41:51 +02004483 rcu_read_unlock();
4484
Florian Westphal0cc09022017-08-09 20:41:50 +02004485 rtnl_lock();
Florian Westphaladdf9b92017-12-02 21:44:05 +01004486 link = rtnl_get_link(family, type);
4487 if (link && link->doit)
4488 err = link->doit(skb, nlh, extack);
Florian Westphal0cc09022017-08-09 20:41:50 +02004489 rtnl_unlock();
Florian Westphaladdf9b92017-12-02 21:44:05 +01004490
Florian Westphale4202512017-12-02 21:44:06 +01004491 module_put(owner);
4492
Florian Westphaladdf9b92017-12-02 21:44:05 +01004493 return err;
4494
4495out_unlock:
4496 rcu_read_unlock();
Florian Westphal0cc09022017-08-09 20:41:50 +02004497 return err;
4498
4499err_unlock:
Florian Westphal6853dd42017-08-09 20:41:51 +02004500 rcu_read_unlock();
Florian Westphal0cc09022017-08-09 20:41:50 +02004501 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502}
4503
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07004504static void rtnetlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07004506 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507}
4508
Julien Gomes5f729ea2017-06-20 13:54:16 -07004509static int rtnetlink_bind(struct net *net, int group)
4510{
4511 switch (group) {
4512 case RTNLGRP_IPV4_MROUTE_R:
4513 case RTNLGRP_IPV6_MROUTE_R:
4514 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
4515 return -EPERM;
4516 break;
4517 }
4518 return 0;
4519}
4520
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
4522{
Jiri Pirko351638e2013-05-28 01:30:21 +00004523 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02004524
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 switch (event) {
Vlad Yasevich5138e862017-04-04 09:23:41 -04004526 case NETDEV_REBOOT:
Xin Long8a212582017-10-15 18:13:41 +08004527 case NETDEV_CHANGEMTU:
David Ahern37536542017-07-19 10:22:40 -07004528 case NETDEV_CHANGEADDR:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004529 case NETDEV_CHANGENAME:
4530 case NETDEV_FEAT_CHANGE:
4531 case NETDEV_BONDING_FAILOVER:
Xin Longe6e66592017-10-15 18:13:43 +08004532 case NETDEV_POST_TYPE_CHANGE:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004533 case NETDEV_NOTIFY_PEERS:
Xin Longdc709f32017-10-15 18:13:44 +08004534 case NETDEV_CHANGEUPPER:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004535 case NETDEV_RESEND_IGMP:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004536 case NETDEV_CHANGEINFODATA:
Xin Longeeda3fb2017-10-24 13:54:19 +08004537 case NETDEV_CHANGELOWERSTATE:
Xin Longebdcf042017-10-15 18:13:42 +08004538 case NETDEV_CHANGE_TX_QUEUE_LEN:
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04004539 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02004540 GFP_KERNEL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 break;
4542 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 break;
4544 }
4545 return NOTIFY_DONE;
4546}
4547
4548static struct notifier_block rtnetlink_dev_notifier = {
4549 .notifier_call = rtnetlink_event,
4550};
4551
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004552
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004553static int __net_init rtnetlink_net_init(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004554{
4555 struct sock *sk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00004556 struct netlink_kernel_cfg cfg = {
4557 .groups = RTNLGRP_MAX,
4558 .input = rtnetlink_rcv,
4559 .cb_mutex = &rtnl_mutex,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00004560 .flags = NL_CFG_F_NONROOT_RECV,
Julien Gomes5f729ea2017-06-20 13:54:16 -07004561 .bind = rtnetlink_bind,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00004562 };
4563
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00004564 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004565 if (!sk)
4566 return -ENOMEM;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004567 net->rtnl = sk;
4568 return 0;
4569}
4570
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004571static void __net_exit rtnetlink_net_exit(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004572{
Denis V. Lunev775516b2008-01-18 23:55:19 -08004573 netlink_kernel_release(net->rtnl);
4574 net->rtnl = NULL;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004575}
4576
4577static struct pernet_operations rtnetlink_net_ops = {
4578 .init = rtnetlink_net_init,
4579 .exit = rtnetlink_net_exit,
4580};
4581
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582void __init rtnetlink_init(void)
4583{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004584 if (register_pernet_subsys(&rtnetlink_net_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 panic("rtnetlink_init: cannot initialize rtnetlink\n");
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004586
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 register_netdevice_notifier(&rtnetlink_dev_notifier);
Thomas Graf340d17f2007-03-22 11:49:22 -07004588
Greg Rosec7ac8672011-06-10 01:27:09 +00004589 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
Florian Westphalb97bac62017-08-09 20:41:48 +02004590 rtnl_dump_ifinfo, 0);
4591 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
4592 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
4593 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
Thomas Graf687ad8c2007-03-22 11:59:42 -07004594
Florian Westphalb97bac62017-08-09 20:41:48 +02004595 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
4596 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
4597 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
John Fastabend77162022012-04-15 06:43:56 +00004598
Florian Westphalb97bac62017-08-09 20:41:48 +02004599 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
4600 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
4601 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, 0);
John Fastabende5a55a82012-10-24 08:12:57 +00004602
Florian Westphalb97bac62017-08-09 20:41:48 +02004603 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
4604 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
4605 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004606
4607 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
Florian Westphalb97bac62017-08-09 20:41:48 +02004608 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609}