blob: 5421a3fd3ba1287d56d85f2167706c71c252b90a [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)) +
Eugenia Emantayevc5a9f6f2017-07-17 13:47:07 +0300907 /* IFLA_VF_STATS_RX_DROPPED */
908 nla_total_size_64bit(sizeof(__u64)) +
909 /* IFLA_VF_STATS_TX_DROPPED */
910 nla_total_size_64bit(sizeof(__u64)) +
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +0000911 nla_total_size(sizeof(struct ifla_vf_trust)));
Chris Wrightc02db8c2010-05-16 01:05:45 -0700912 return size;
913 } else
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000914 return 0;
915}
916
David Gibsonc53864f2014-04-24 10:22:36 +1000917static size_t rtnl_port_size(const struct net_device *dev,
918 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -0700919{
920 size_t port_size = nla_total_size(4) /* PORT_VF */
921 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
Scott Feldman57b61082010-05-17 22:49:55 -0700922 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
923 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
924 + nla_total_size(1) /* PROT_VDP_REQUEST */
925 + nla_total_size(2); /* PORT_VDP_RESPONSE */
926 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
927 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
928 + port_size;
929 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
930 + port_size;
931
David Gibsonc53864f2014-04-24 10:22:36 +1000932 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
933 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -0700934 return 0;
935 if (dev_num_vf(dev->dev.parent))
936 return port_self_size + vf_ports_size +
937 vf_port_size * dev_num_vf(dev->dev.parent);
938 else
939 return port_self_size;
940}
941
David S. Millerb5cdae32017-04-18 15:36:58 -0400942static size_t rtnl_xdp_size(void)
Brenden Blancod1fdd912016-07-19 12:16:49 -0700943{
Sabrina Dubrocab3cfaa32016-11-15 11:16:35 +0100944 size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */
Martin KaFai Lau58038692017-06-15 17:29:09 -0700945 nla_total_size(1) + /* XDP_ATTACHED */
946 nla_total_size(4); /* XDP_PROG_ID */
Brenden Blancod1fdd912016-07-19 12:16:49 -0700947
David S. Millerb5cdae32017-04-18 15:36:58 -0400948 return xdp_size;
Brenden Blancod1fdd912016-07-19 12:16:49 -0700949}
950
Greg Rose115c9b82012-02-21 16:54:48 -0500951static noinline size_t if_nlmsg_size(const struct net_device *dev,
952 u32 ext_filter_mask)
Thomas Graf339bf982006-11-10 14:10:15 -0800953{
954 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
955 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700956 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
Thomas Graf339bf982006-11-10 14:10:15 -0800957 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
Nicolas Dichtel270cb4d2016-04-26 10:06:16 +0200958 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
Thomas Graf339bf982006-11-10 14:10:15 -0800959 + nla_total_size(sizeof(struct rtnl_link_stats))
David S. Miller35c58452016-04-19 19:49:29 -0400960 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
Thomas Graf339bf982006-11-10 14:10:15 -0800961 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
962 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
963 + nla_total_size(4) /* IFLA_TXQLEN */
964 + nla_total_size(4) /* IFLA_WEIGHT */
965 + nla_total_size(4) /* IFLA_MTU */
966 + nla_total_size(4) /* IFLA_LINK */
967 + nla_total_size(4) /* IFLA_MASTER */
Jiri Pirko9a572472012-12-27 23:49:39 +0000968 + nla_total_size(1) /* IFLA_CARRIER */
Ben Greearedbc0bb2012-03-29 12:51:30 +0000969 + nla_total_size(4) /* IFLA_PROMISCUITY */
Jiri Pirko76ff5cc2012-07-20 02:28:48 +0000970 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
971 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
Tobias Klauser69197562016-11-30 14:30:37 +0100972 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
973 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
Thomas Graf339bf982006-11-10 14:10:15 -0800974 + nla_total_size(1) /* IFLA_OPERSTATE */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700975 + nla_total_size(1) /* IFLA_LINKMODE */
david decotigny2d3b4792014-03-29 09:48:35 -0700976 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
Nicolas Dichteld37512a2015-01-15 15:11:16 +0100977 + nla_total_size(4) /* IFLA_LINK_NETNSID */
Serhey Popovychdb833d42017-06-20 14:35:23 +0300978 + nla_total_size(4) /* IFLA_GROUP */
Greg Rose115c9b82012-02-21 16:54:48 -0500979 + nla_total_size(ext_filter_mask
980 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
981 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
David Gibsonc53864f2014-04-24 10:22:36 +1000982 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
Thomas Graff8ff1822010-11-16 04:30:14 +0000983 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700984 + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
Jiri Pirko82f28412014-11-28 14:34:18 +0100985 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
Anuradha Karuppiah88d63782015-07-14 13:43:20 -0700986 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
Nicolas Dichtelc57c7a92016-03-31 18:10:31 +0200987 + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
David S. Millerb5cdae32017-04-18 15:36:58 -0400988 + rtnl_xdp_size() /* IFLA_XDP */
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -0400989 + nla_total_size(4) /* IFLA_EVENT */
Nicolas Dichtel6621dd22017-10-03 13:53:23 +0200990 + nla_total_size(4) /* IFLA_NEW_NETNSID */
Colin Ian King03ac7382017-11-06 15:04:54 +0000991 + nla_total_size(1) /* IFLA_PROTO_DOWN */
Jiri Benc79e1ad12017-11-02 17:04:38 -0200992 + nla_total_size(4) /* IFLA_IF_NETNSID */
993 + 0;
Thomas Graf339bf982006-11-10 14:10:15 -0800994}
995
Scott Feldman57b61082010-05-17 22:49:55 -0700996static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
997{
998 struct nlattr *vf_ports;
999 struct nlattr *vf_port;
1000 int vf;
1001 int err;
1002
1003 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
1004 if (!vf_ports)
1005 return -EMSGSIZE;
1006
1007 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
1008 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
Scott Feldman8ca94182010-05-28 03:42:18 -07001009 if (!vf_port)
1010 goto nla_put_failure;
David S. Millera6574342012-04-01 20:12:00 -04001011 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
1012 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001013 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
Scott Feldman8ca94182010-05-28 03:42:18 -07001014 if (err == -EMSGSIZE)
1015 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001016 if (err) {
Scott Feldman57b61082010-05-17 22:49:55 -07001017 nla_nest_cancel(skb, vf_port);
1018 continue;
1019 }
1020 nla_nest_end(skb, vf_port);
1021 }
1022
1023 nla_nest_end(skb, vf_ports);
1024
1025 return 0;
Scott Feldman8ca94182010-05-28 03:42:18 -07001026
1027nla_put_failure:
1028 nla_nest_cancel(skb, vf_ports);
1029 return -EMSGSIZE;
Scott Feldman57b61082010-05-17 22:49:55 -07001030}
1031
1032static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
1033{
1034 struct nlattr *port_self;
1035 int err;
1036
1037 port_self = nla_nest_start(skb, IFLA_PORT_SELF);
1038 if (!port_self)
1039 return -EMSGSIZE;
1040
1041 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
1042 if (err) {
1043 nla_nest_cancel(skb, port_self);
Scott Feldman8ca94182010-05-28 03:42:18 -07001044 return (err == -EMSGSIZE) ? err : 0;
Scott Feldman57b61082010-05-17 22:49:55 -07001045 }
1046
1047 nla_nest_end(skb, port_self);
1048
1049 return 0;
1050}
1051
David Gibsonc53864f2014-04-24 10:22:36 +10001052static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1053 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -07001054{
1055 int err;
1056
David Gibsonc53864f2014-04-24 10:22:36 +10001057 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1058 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -07001059 return 0;
1060
1061 err = rtnl_port_self_fill(skb, dev);
1062 if (err)
1063 return err;
1064
1065 if (dev_num_vf(dev->dev.parent)) {
1066 err = rtnl_vf_ports_fill(skb, dev);
1067 if (err)
1068 return err;
1069 }
1070
1071 return 0;
1072}
1073
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001074static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
1075{
1076 int err;
Jiri Pirko02637fc2014-11-28 14:34:16 +01001077 struct netdev_phys_item_id ppid;
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001078
1079 err = dev_get_phys_port_id(dev, &ppid);
1080 if (err) {
1081 if (err == -EOPNOTSUPP)
1082 return 0;
1083 return err;
1084 }
1085
1086 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1087 return -EMSGSIZE;
1088
1089 return 0;
1090}
1091
David Aherndb24a902015-03-17 20:23:15 -06001092static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1093{
1094 char name[IFNAMSIZ];
1095 int err;
1096
1097 err = dev_get_phys_port_name(dev, name, sizeof(name));
1098 if (err) {
1099 if (err == -EOPNOTSUPP)
1100 return 0;
1101 return err;
1102 }
1103
Michal Schmidt77ef0332017-05-04 16:48:58 +02001104 if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
David Aherndb24a902015-03-17 20:23:15 -06001105 return -EMSGSIZE;
1106
1107 return 0;
1108}
1109
Jiri Pirko82f28412014-11-28 14:34:18 +01001110static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1111{
1112 int err;
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001113 struct switchdev_attr attr = {
Ido Schimmel6ff64f62015-12-15 16:03:35 +01001114 .orig_dev = dev,
Jiri Pirko1f868392015-10-01 11:03:42 +02001115 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001116 .flags = SWITCHDEV_F_NO_RECURSE,
1117 };
Jiri Pirko82f28412014-11-28 14:34:18 +01001118
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001119 err = switchdev_port_attr_get(dev, &attr);
Jiri Pirko82f28412014-11-28 14:34:18 +01001120 if (err) {
1121 if (err == -EOPNOTSUPP)
1122 return 0;
1123 return err;
1124 }
1125
Scott Feldman42275bd2015-05-13 11:16:50 -07001126 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len,
1127 attr.u.ppid.id))
Jiri Pirko82f28412014-11-28 14:34:18 +01001128 return -EMSGSIZE;
1129
1130 return 0;
1131}
1132
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001133static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1134 struct net_device *dev)
1135{
Roopa Prabhu550bce52016-04-15 20:36:25 -07001136 struct rtnl_link_stats64 *sp;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001137 struct nlattr *attr;
1138
Nicolas Dichtel58414d32016-04-21 18:58:25 +02001139 attr = nla_reserve_64bit(skb, IFLA_STATS64,
1140 sizeof(struct rtnl_link_stats64), IFLA_PAD);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001141 if (!attr)
1142 return -EMSGSIZE;
1143
Roopa Prabhu550bce52016-04-15 20:36:25 -07001144 sp = nla_data(attr);
1145 dev_get_stats(dev, sp);
1146
1147 attr = nla_reserve(skb, IFLA_STATS,
1148 sizeof(struct rtnl_link_stats));
1149 if (!attr)
1150 return -EMSGSIZE;
1151
1152 copy_rtnl_link_stats(nla_data(attr), sp);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001153
1154 return 0;
1155}
1156
1157static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1158 struct net_device *dev,
1159 int vfs_num,
1160 struct nlattr *vfinfo)
1161{
1162 struct ifla_vf_rss_query_en vf_rss_query_en;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001163 struct nlattr *vf, *vfstats, *vfvlanlist;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001164 struct ifla_vf_link_state vf_linkstate;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001165 struct ifla_vf_vlan_info vf_vlan_info;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001166 struct ifla_vf_spoofchk vf_spoofchk;
1167 struct ifla_vf_tx_rate vf_tx_rate;
1168 struct ifla_vf_stats vf_stats;
1169 struct ifla_vf_trust vf_trust;
1170 struct ifla_vf_vlan vf_vlan;
1171 struct ifla_vf_rate vf_rate;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001172 struct ifla_vf_mac vf_mac;
1173 struct ifla_vf_info ivi;
1174
Mintz, Yuval0eed9cf2017-06-07 21:00:33 +03001175 memset(&ivi, 0, sizeof(ivi));
1176
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001177 /* Not all SR-IOV capable drivers support the
1178 * spoofcheck and "RSS query enable" query. Preset to
1179 * -1 so the user space tool can detect that the driver
1180 * didn't report anything.
1181 */
1182 ivi.spoofchk = -1;
1183 ivi.rss_query_en = -1;
1184 ivi.trusted = -1;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001185 /* The default value for VF link state is "auto"
1186 * IFLA_VF_LINK_STATE_AUTO which equals zero
1187 */
1188 ivi.linkstate = 0;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001189 /* VLAN Protocol by default is 802.1Q */
1190 ivi.vlan_proto = htons(ETH_P_8021Q);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001191 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1192 return 0;
1193
Dan Carpenter775f4f02016-10-13 11:45:28 +03001194 memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
1195
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001196 vf_mac.vf =
1197 vf_vlan.vf =
Moshe Shemesh79aab092016-09-22 12:11:15 +03001198 vf_vlan_info.vf =
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001199 vf_rate.vf =
1200 vf_tx_rate.vf =
1201 vf_spoofchk.vf =
1202 vf_linkstate.vf =
1203 vf_rss_query_en.vf =
1204 vf_trust.vf = ivi.vf;
1205
1206 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1207 vf_vlan.vlan = ivi.vlan;
1208 vf_vlan.qos = ivi.qos;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001209 vf_vlan_info.vlan = ivi.vlan;
1210 vf_vlan_info.qos = ivi.qos;
1211 vf_vlan_info.vlan_proto = ivi.vlan_proto;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001212 vf_tx_rate.rate = ivi.max_tx_rate;
1213 vf_rate.min_tx_rate = ivi.min_tx_rate;
1214 vf_rate.max_tx_rate = ivi.max_tx_rate;
1215 vf_spoofchk.setting = ivi.spoofchk;
1216 vf_linkstate.link_state = ivi.linkstate;
1217 vf_rss_query_en.setting = ivi.rss_query_en;
1218 vf_trust.setting = ivi.trusted;
1219 vf = nla_nest_start(skb, IFLA_VF_INFO);
Moshe Shemesh79aab092016-09-22 12:11:15 +03001220 if (!vf)
1221 goto nla_put_vfinfo_failure;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001222 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1223 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1224 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1225 &vf_rate) ||
1226 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1227 &vf_tx_rate) ||
1228 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1229 &vf_spoofchk) ||
1230 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1231 &vf_linkstate) ||
1232 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1233 sizeof(vf_rss_query_en),
1234 &vf_rss_query_en) ||
1235 nla_put(skb, IFLA_VF_TRUST,
1236 sizeof(vf_trust), &vf_trust))
Moshe Shemesh79aab092016-09-22 12:11:15 +03001237 goto nla_put_vf_failure;
1238 vfvlanlist = nla_nest_start(skb, IFLA_VF_VLAN_LIST);
1239 if (!vfvlanlist)
1240 goto nla_put_vf_failure;
1241 if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
1242 &vf_vlan_info)) {
1243 nla_nest_cancel(skb, vfvlanlist);
1244 goto nla_put_vf_failure;
1245 }
1246 nla_nest_end(skb, vfvlanlist);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001247 memset(&vf_stats, 0, sizeof(vf_stats));
1248 if (dev->netdev_ops->ndo_get_vf_stats)
1249 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1250 &vf_stats);
1251 vfstats = nla_nest_start(skb, IFLA_VF_STATS);
Moshe Shemesh79aab092016-09-22 12:11:15 +03001252 if (!vfstats)
1253 goto nla_put_vf_failure;
Nicolas Dichtel343a6d82016-04-25 10:25:14 +02001254 if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1255 vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1256 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1257 vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1258 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1259 vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1260 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1261 vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1262 nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1263 vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1264 nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
Eugenia Emantayevc5a9f6f2017-07-17 13:47:07 +03001265 vf_stats.multicast, IFLA_VF_STATS_PAD) ||
1266 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED,
1267 vf_stats.rx_dropped, IFLA_VF_STATS_PAD) ||
1268 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED,
1269 vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) {
Moshe Shemesh79aab092016-09-22 12:11:15 +03001270 nla_nest_cancel(skb, vfstats);
1271 goto nla_put_vf_failure;
1272 }
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001273 nla_nest_end(skb, vfstats);
1274 nla_nest_end(skb, vf);
1275 return 0;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001276
1277nla_put_vf_failure:
1278 nla_nest_cancel(skb, vf);
1279nla_put_vfinfo_failure:
1280 nla_nest_cancel(skb, vfinfo);
1281 return -EMSGSIZE;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001282}
1283
Florian Westphal250fc3d2017-09-26 13:58:41 +02001284static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb,
1285 struct net_device *dev,
1286 u32 ext_filter_mask)
1287{
1288 struct nlattr *vfinfo;
1289 int i, num_vfs;
1290
1291 if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0))
1292 return 0;
1293
1294 num_vfs = dev_num_vf(dev->dev.parent);
1295 if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs))
1296 return -EMSGSIZE;
1297
1298 if (!dev->netdev_ops->ndo_get_vf_config)
1299 return 0;
1300
1301 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1302 if (!vfinfo)
1303 return -EMSGSIZE;
1304
1305 for (i = 0; i < num_vfs; i++) {
1306 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1307 return -EMSGSIZE;
1308 }
1309
1310 nla_nest_end(skb, vfinfo);
1311 return 0;
1312}
1313
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001314static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1315{
Kangjie Lu5f8e4472016-05-03 16:46:24 -04001316 struct rtnl_link_ifmap map;
1317
1318 memset(&map, 0, sizeof(map));
1319 map.mem_start = dev->mem_start;
1320 map.mem_end = dev->mem_end;
1321 map.base_addr = dev->base_addr;
1322 map.irq = dev->irq;
1323 map.dma = dev->dma;
1324 map.port = dev->if_port;
1325
Nicolas Dichtel270cb4d2016-04-26 10:06:16 +02001326 if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001327 return -EMSGSIZE;
1328
1329 return 0;
1330}
1331
Martin KaFai Lau58038692017-06-15 17:29:09 -07001332static u8 rtnl_xdp_attached_mode(struct net_device *dev, u32 *prog_id)
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001333{
1334 const struct net_device_ops *ops = dev->netdev_ops;
Martin KaFai Lau58038692017-06-15 17:29:09 -07001335 const struct bpf_prog *generic_xdp_prog;
Jakub Kicinski118b4aa2017-12-01 15:08:55 -08001336 struct netdev_bpf xdp;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001337
1338 ASSERT_RTNL();
1339
Martin KaFai Lau58038692017-06-15 17:29:09 -07001340 *prog_id = 0;
1341 generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
1342 if (generic_xdp_prog) {
1343 *prog_id = generic_xdp_prog->aux->id;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001344 return XDP_ATTACHED_SKB;
Martin KaFai Lau58038692017-06-15 17:29:09 -07001345 }
Jakub Kicinskif4e63522017-11-03 13:56:16 -07001346 if (!ops->ndo_bpf)
Jakub Kicinskice158e52017-06-21 18:25:09 -07001347 return XDP_ATTACHED_NONE;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001348
Jakub Kicinski118b4aa2017-12-01 15:08:55 -08001349 __dev_xdp_query(dev, ops->ndo_bpf, &xdp);
1350 *prog_id = xdp.prog_id;
1351
1352 return xdp.prog_attached;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001353}
1354
Brenden Blancod1fdd912016-07-19 12:16:49 -07001355static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1356{
Brenden Blancod1fdd912016-07-19 12:16:49 -07001357 struct nlattr *xdp;
Martin KaFai Lau58038692017-06-15 17:29:09 -07001358 u32 prog_id;
Brenden Blancod1fdd912016-07-19 12:16:49 -07001359 int err;
1360
Brenden Blancod1fdd912016-07-19 12:16:49 -07001361 xdp = nla_nest_start(skb, IFLA_XDP);
1362 if (!xdp)
1363 return -EMSGSIZE;
David S. Millerb5cdae32017-04-18 15:36:58 -04001364
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001365 err = nla_put_u8(skb, IFLA_XDP_ATTACHED,
Martin KaFai Lau58038692017-06-15 17:29:09 -07001366 rtnl_xdp_attached_mode(dev, &prog_id));
Brenden Blancod1fdd912016-07-19 12:16:49 -07001367 if (err)
1368 goto err_cancel;
1369
Martin KaFai Lau58038692017-06-15 17:29:09 -07001370 if (prog_id) {
1371 err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
1372 if (err)
1373 goto err_cancel;
1374 }
1375
Brenden Blancod1fdd912016-07-19 12:16:49 -07001376 nla_nest_end(skb, xdp);
1377 return 0;
1378
1379err_cancel:
1380 nla_nest_cancel(skb, xdp);
1381 return err;
1382}
1383
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001384static u32 rtnl_get_event(unsigned long event)
1385{
1386 u32 rtnl_event_type = IFLA_EVENT_NONE;
1387
1388 switch (event) {
1389 case NETDEV_REBOOT:
1390 rtnl_event_type = IFLA_EVENT_REBOOT;
1391 break;
1392 case NETDEV_FEAT_CHANGE:
1393 rtnl_event_type = IFLA_EVENT_FEATURES;
1394 break;
1395 case NETDEV_BONDING_FAILOVER:
1396 rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
1397 break;
1398 case NETDEV_NOTIFY_PEERS:
1399 rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
1400 break;
1401 case NETDEV_RESEND_IGMP:
1402 rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
1403 break;
1404 case NETDEV_CHANGEINFODATA:
1405 rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
1406 break;
1407 default:
1408 break;
1409 }
1410
1411 return rtnl_event_type;
1412}
1413
Florian Westphal79110a02017-09-26 13:58:40 +02001414static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
1415{
1416 const struct net_device *upper_dev;
1417 int ret = 0;
1418
1419 rcu_read_lock();
1420
1421 upper_dev = netdev_master_upper_dev_get_rcu(dev);
1422 if (upper_dev)
1423 ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);
1424
1425 rcu_read_unlock();
1426 return ret;
1427}
1428
1429static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev)
1430{
1431 int ifindex = dev_get_iflink(dev);
1432
1433 if (dev->ifindex == ifindex)
1434 return 0;
1435
1436 return nla_put_u32(skb, IFLA_LINK, ifindex);
1437}
1438
Florian Westphal6c557002017-10-02 23:50:05 +02001439static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
1440 struct net_device *dev)
1441{
1442 char buf[IFALIASZ];
1443 int ret;
1444
1445 ret = dev_get_alias(dev, buf, sizeof(buf));
1446 return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
1447}
1448
Florian Westphalb1e66b92017-09-26 13:58:42 +02001449static int rtnl_fill_link_netnsid(struct sk_buff *skb,
Jiri Benc79e1ad12017-11-02 17:04:38 -02001450 const struct net_device *dev,
1451 struct net *src_net)
Florian Westphalb1e66b92017-09-26 13:58:42 +02001452{
1453 if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
1454 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1455
1456 if (!net_eq(dev_net(dev), link_net)) {
Jiri Benc79e1ad12017-11-02 17:04:38 -02001457 int id = peernet2id_alloc(src_net, link_net);
Florian Westphalb1e66b92017-09-26 13:58:42 +02001458
1459 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1460 return -EMSGSIZE;
1461 }
1462 }
1463
1464 return 0;
1465}
1466
Florian Westphal070cbf5b2017-10-16 15:44:35 +02001467static int rtnl_fill_link_af(struct sk_buff *skb,
1468 const struct net_device *dev,
1469 u32 ext_filter_mask)
1470{
1471 const struct rtnl_af_ops *af_ops;
1472 struct nlattr *af_spec;
1473
1474 af_spec = nla_nest_start(skb, IFLA_AF_SPEC);
1475 if (!af_spec)
1476 return -EMSGSIZE;
1477
Florian Westphal5fa85a02017-10-16 15:44:36 +02001478 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Florian Westphal070cbf5b2017-10-16 15:44:35 +02001479 struct nlattr *af;
1480 int err;
1481
1482 if (!af_ops->fill_link_af)
1483 continue;
1484
1485 af = nla_nest_start(skb, af_ops->family);
1486 if (!af)
1487 return -EMSGSIZE;
1488
1489 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1490 /*
1491 * Caller may return ENODATA to indicate that there
1492 * was no data to be dumped. This is not an error, it
1493 * means we should trim the attribute header and
1494 * continue.
1495 */
1496 if (err == -ENODATA)
1497 nla_nest_cancel(skb, af);
1498 else if (err < 0)
1499 return -EMSGSIZE;
1500
1501 nla_nest_end(skb, af);
1502 }
1503
1504 nla_nest_end(skb, af_spec);
1505 return 0;
1506}
1507
Jiri Benc79e1ad12017-11-02 17:04:38 -02001508static int rtnl_fill_ifinfo(struct sk_buff *skb,
1509 struct net_device *dev, struct net *src_net,
Patrick McHardy575c3e22007-05-22 17:00:49 -07001510 int type, u32 pid, u32 seq, u32 change,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001511 unsigned int flags, u32 ext_filter_mask,
Jiri Benc79e1ad12017-11-02 17:04:38 -02001512 u32 event, int *new_nsid, int tgt_netnsid)
Thomas Grafb60c5112006-08-04 23:05:34 -07001513{
1514 struct ifinfomsg *ifm;
1515 struct nlmsghdr *nlh;
1516
Eric Dumazet2907c352011-05-25 07:34:04 +00001517 ASSERT_RTNL();
Thomas Grafb60c5112006-08-04 23:05:34 -07001518 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1519 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08001520 return -EMSGSIZE;
Thomas Grafb60c5112006-08-04 23:05:34 -07001521
1522 ifm = nlmsg_data(nlh);
1523 ifm->ifi_family = AF_UNSPEC;
1524 ifm->__ifi_pad = 0;
1525 ifm->ifi_type = dev->type;
1526 ifm->ifi_index = dev->ifindex;
1527 ifm->ifi_flags = dev_get_flags(dev);
1528 ifm->ifi_change = change;
1529
Jiri Benc79e1ad12017-11-02 17:04:38 -02001530 if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_IF_NETNSID, tgt_netnsid))
1531 goto nla_put_failure;
1532
David S. Millera6574342012-04-01 20:12:00 -04001533 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1534 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1535 nla_put_u8(skb, IFLA_OPERSTATE,
1536 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1537 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1538 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1539 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
Ben Greearedbc0bb2012-03-29 12:51:30 +00001540 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001541 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
Eric Dumazetc70ce022016-03-21 09:55:10 -07001542 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1543 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001544#ifdef CONFIG_RPS
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001545 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001546#endif
Florian Westphal79110a02017-09-26 13:58:40 +02001547 nla_put_iflink(skb, dev) ||
1548 put_master_ifindex(skb, dev) ||
Jiri Pirko9a572472012-12-27 23:49:39 +00001549 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
David S. Millera6574342012-04-01 20:12:00 -04001550 (dev->qdisc &&
1551 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
Florian Westphal6c557002017-10-02 23:50:05 +02001552 nla_put_ifalias(skb, dev) ||
david decotigny2d3b4792014-03-29 09:48:35 -07001553 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001554 atomic_read(&dev->carrier_changes)) ||
1555 nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
David S. Millera6574342012-04-01 20:12:00 -04001556 goto nla_put_failure;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001557
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001558 if (event != IFLA_EVENT_NONE) {
1559 if (nla_put_u32(skb, IFLA_EVENT, event))
1560 goto nla_put_failure;
1561 }
1562
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001563 if (rtnl_fill_link_ifmap(skb, dev))
1564 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
1566 if (dev->addr_len) {
David S. Millera6574342012-04-01 20:12:00 -04001567 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1568 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1569 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 }
1571
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001572 if (rtnl_phys_port_id_fill(skb, dev))
1573 goto nla_put_failure;
1574
David Aherndb24a902015-03-17 20:23:15 -06001575 if (rtnl_phys_port_name_fill(skb, dev))
1576 goto nla_put_failure;
1577
Jiri Pirko82f28412014-11-28 14:34:18 +01001578 if (rtnl_phys_switch_id_fill(skb, dev))
1579 goto nla_put_failure;
1580
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001581 if (rtnl_fill_stats(skb, dev))
Pavel Emelyanov96e74082008-05-21 14:12:46 -07001582 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
Florian Westphal250fc3d2017-09-26 13:58:41 +02001584 if (rtnl_fill_vf(skb, dev, ext_filter_mask))
David S. Millera6574342012-04-01 20:12:00 -04001585 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001586
David Gibsonc53864f2014-04-24 10:22:36 +10001587 if (rtnl_port_fill(skb, dev, ext_filter_mask))
Scott Feldman57b61082010-05-17 22:49:55 -07001588 goto nla_put_failure;
1589
Brenden Blancod1fdd912016-07-19 12:16:49 -07001590 if (rtnl_xdp_fill(skb, dev))
1591 goto nla_put_failure;
1592
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001593 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07001594 if (rtnl_link_fill(skb, dev) < 0)
1595 goto nla_put_failure;
1596 }
1597
Jiri Benc79e1ad12017-11-02 17:04:38 -02001598 if (rtnl_fill_link_netnsid(skb, dev, src_net))
Florian Westphalb1e66b92017-09-26 13:58:42 +02001599 goto nla_put_failure;
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001600
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02001601 if (new_nsid &&
1602 nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0)
1603 goto nla_put_failure;
1604
Florian Westphal5fa85a02017-10-16 15:44:36 +02001605 rcu_read_lock();
Florian Westphal070cbf5b2017-10-16 15:44:35 +02001606 if (rtnl_fill_link_af(skb, dev, ext_filter_mask))
Florian Westphal5fa85a02017-10-16 15:44:36 +02001607 goto nla_put_failure_rcu;
1608 rcu_read_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +00001609
Johannes Berg053c0952015-01-16 22:09:00 +01001610 nlmsg_end(skb, nlh);
1611 return 0;
Thomas Grafb60c5112006-08-04 23:05:34 -07001612
Florian Westphal5fa85a02017-10-16 15:44:36 +02001613nla_put_failure_rcu:
1614 rcu_read_unlock();
Thomas Grafb60c5112006-08-04 23:05:34 -07001615nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001616 nlmsg_cancel(skb, nlh);
1617 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618}
1619
Jiri Pirkof7b12602014-02-18 20:53:18 +01001620static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1621 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1622 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1623 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1624 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1625 [IFLA_MTU] = { .type = NLA_U32 },
1626 [IFLA_LINK] = { .type = NLA_U32 },
1627 [IFLA_MASTER] = { .type = NLA_U32 },
1628 [IFLA_CARRIER] = { .type = NLA_U8 },
1629 [IFLA_TXQLEN] = { .type = NLA_U32 },
1630 [IFLA_WEIGHT] = { .type = NLA_U32 },
1631 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1632 [IFLA_LINKMODE] = { .type = NLA_U8 },
1633 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1634 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1635 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
Nicolas Dichtel2459b4c2017-10-11 16:24:48 +02001636 /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
1637 * allow 0-length string (needed to remove an alias).
1638 */
1639 [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001640 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1641 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1642 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1643 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1644 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1645 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1646 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1647 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
Stephen Hemminger46e6b992017-12-07 15:40:19 -08001648 [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 },
1649 [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 },
Jiri Pirko02637fc2014-11-28 14:34:16 +01001650 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
david decotigny2d3b4792014-03-29 09:48:35 -07001651 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
Jiri Pirko82f28412014-11-28 14:34:18 +01001652 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
Nicolas Dichtel317f4812015-01-15 15:11:18 +01001653 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001654 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001655 [IFLA_XDP] = { .type = NLA_NESTED },
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001656 [IFLA_EVENT] = { .type = NLA_U32 },
Serhey Popovychdb833d42017-06-20 14:35:23 +03001657 [IFLA_GROUP] = { .type = NLA_U32 },
Jiri Benc79e1ad12017-11-02 17:04:38 -02001658 [IFLA_IF_NETNSID] = { .type = NLA_S32 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001659};
1660
1661static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1662 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1663 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1664 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1665 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1666};
1667
Jiri Pirkof7b12602014-02-18 20:53:18 +01001668static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
Daniel Borkmann364d5712015-02-05 18:44:04 +01001669 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1670 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
Moshe Shemesh79aab092016-09-22 12:11:15 +03001671 [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED },
Daniel Borkmann364d5712015-02-05 18:44:04 +01001672 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1673 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1674 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1675 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001676 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001677 [IFLA_VF_STATS] = { .type = NLA_NESTED },
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00001678 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
Eli Cohencc8e27c2016-03-11 22:58:34 +02001679 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1680 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001681};
1682
Jiri Pirkof7b12602014-02-18 20:53:18 +01001683static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1684 [IFLA_PORT_VF] = { .type = NLA_U32 },
1685 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1686 .len = PORT_PROFILE_MAX },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001687 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1688 .len = PORT_UUID_MAX },
1689 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1690 .len = PORT_UUID_MAX },
1691 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1692 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
Daniel Borkmann025331d2017-02-17 01:56:11 +01001693
1694 /* Unused, but we need to keep it here since user space could
1695 * fill it. It's also broken with regard to NLA_BINARY use in
1696 * combination with structs.
1697 */
1698 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1699 .len = sizeof(struct ifla_port_vsi) },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001700};
1701
Brenden Blancod1fdd912016-07-19 12:16:49 -07001702static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
1703 [IFLA_XDP_FD] = { .type = NLA_S32 },
1704 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 },
Daniel Borkmann85de8572016-11-28 23:16:54 +01001705 [IFLA_XDP_FLAGS] = { .type = NLA_U32 },
Martin KaFai Lau58038692017-06-15 17:29:09 -07001706 [IFLA_XDP_PROG_ID] = { .type = NLA_U32 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001707};
1708
David Aherndc599f72016-02-02 08:17:07 -08001709static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1710{
1711 const struct rtnl_link_ops *ops = NULL;
1712 struct nlattr *linfo[IFLA_INFO_MAX + 1];
1713
Johannes Bergfceb6432017-04-12 14:34:07 +02001714 if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla,
1715 ifla_info_policy, NULL) < 0)
David Aherndc599f72016-02-02 08:17:07 -08001716 return NULL;
1717
1718 if (linfo[IFLA_INFO_KIND]) {
1719 char kind[MODULE_NAME_LEN];
1720
1721 nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1722 ops = rtnl_link_ops_get(kind);
1723 }
1724
1725 return ops;
1726}
1727
1728static bool link_master_filtered(struct net_device *dev, int master_idx)
1729{
1730 struct net_device *master;
1731
1732 if (!master_idx)
1733 return false;
1734
1735 master = netdev_master_upper_dev_get(dev);
1736 if (!master || master->ifindex != master_idx)
1737 return true;
1738
1739 return false;
1740}
1741
1742static bool link_kind_filtered(const struct net_device *dev,
1743 const struct rtnl_link_ops *kind_ops)
1744{
1745 if (kind_ops && dev->rtnl_link_ops != kind_ops)
1746 return true;
1747
1748 return false;
1749}
1750
1751static bool link_dump_filtered(struct net_device *dev,
1752 int master_idx,
1753 const struct rtnl_link_ops *kind_ops)
1754{
1755 if (link_master_filtered(dev, master_idx) ||
1756 link_kind_filtered(dev, kind_ops))
1757 return true;
1758
1759 return false;
1760}
1761
Jiri Benc79e1ad12017-11-02 17:04:38 -02001762static struct net *get_target_net(struct sk_buff *skb, int netnsid)
1763{
1764 struct net *net;
1765
1766 net = get_net_ns_by_id(sock_net(skb->sk), netnsid);
1767 if (!net)
1768 return ERR_PTR(-EINVAL);
1769
1770 /* For now, the caller is required to have CAP_NET_ADMIN in
1771 * the user namespace owning the target net ns.
1772 */
1773 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
1774 put_net(net);
1775 return ERR_PTR(-EACCES);
1776 }
1777 return net;
1778}
1779
Thomas Grafb60c5112006-08-04 23:05:34 -07001780static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001782 struct net *net = sock_net(skb->sk);
Jiri Benc79e1ad12017-11-02 17:04:38 -02001783 struct net *tgt_net = net;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001784 int h, s_h;
1785 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 struct net_device *dev;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001787 struct hlist_head *head;
Greg Rose115c9b82012-02-21 16:54:48 -05001788 struct nlattr *tb[IFLA_MAX+1];
1789 u32 ext_filter_mask = 0;
David Aherndc599f72016-02-02 08:17:07 -08001790 const struct rtnl_link_ops *kind_ops = NULL;
1791 unsigned int flags = NLM_F_MULTI;
1792 int master_idx = 0;
Jiri Benc79e1ad12017-11-02 17:04:38 -02001793 int netnsid = -1;
David Gibson973462b2014-04-24 10:22:35 +10001794 int err;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001795 int hdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001797 s_h = cb->args[0];
1798 s_idx = cb->args[1];
1799
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001800 /* A hack to preserve kernel<->userspace interface.
1801 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1802 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1803 * what iproute2 < v3.9.0 used.
1804 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1805 * attribute, its netlink message is shorter than struct ifinfomsg.
1806 */
1807 hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1808 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1809
Johannes Bergfceb6432017-04-12 14:34:07 +02001810 if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX,
1811 ifla_policy, NULL) >= 0) {
Jiri Benc79e1ad12017-11-02 17:04:38 -02001812 if (tb[IFLA_IF_NETNSID]) {
1813 netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]);
1814 tgt_net = get_target_net(skb, netnsid);
1815 if (IS_ERR(tgt_net)) {
1816 tgt_net = net;
1817 netnsid = -1;
1818 }
1819 }
1820
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001821 if (tb[IFLA_EXT_MASK])
1822 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
David Aherndc599f72016-02-02 08:17:07 -08001823
1824 if (tb[IFLA_MASTER])
1825 master_idx = nla_get_u32(tb[IFLA_MASTER]);
1826
1827 if (tb[IFLA_LINKINFO])
1828 kind_ops = linkinfo_to_kind_ops(tb[IFLA_LINKINFO]);
1829
1830 if (master_idx || kind_ops)
1831 flags |= NLM_F_DUMP_FILTERED;
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001832 }
Greg Rose115c9b82012-02-21 16:54:48 -05001833
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001834 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1835 idx = 0;
Jiri Benc79e1ad12017-11-02 17:04:38 -02001836 head = &tgt_net->dev_index_head[h];
Eric Dumazetcac5e652015-02-27 09:42:50 -08001837 hlist_for_each_entry(dev, head, index_hlist) {
David Aherndc599f72016-02-02 08:17:07 -08001838 if (link_dump_filtered(dev, master_idx, kind_ops))
Zhang Shengju3f0ae052016-11-19 23:28:32 +08001839 goto cont;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001840 if (idx < s_idx)
1841 goto cont;
Jiri Benc79e1ad12017-11-02 17:04:38 -02001842 err = rtnl_fill_ifinfo(skb, dev, net,
1843 RTM_NEWLINK,
David Gibson973462b2014-04-24 10:22:35 +10001844 NETLINK_CB(cb->skb).portid,
1845 cb->nlh->nlmsg_seq, 0,
David Aherndc599f72016-02-02 08:17:07 -08001846 flags,
Jiri Benc79e1ad12017-11-02 17:04:38 -02001847 ext_filter_mask, 0, NULL,
1848 netnsid);
David Gibson973462b2014-04-24 10:22:35 +10001849
David Ahernf6c57752017-05-15 23:19:17 -07001850 if (err < 0) {
1851 if (likely(skb->len))
1852 goto out;
1853
1854 goto out_err;
1855 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07001856cont:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001857 idx++;
1858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 }
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001860out:
David Ahernf6c57752017-05-15 23:19:17 -07001861 err = skb->len;
1862out_err:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001863 cb->args[1] = idx;
1864 cb->args[0] = h;
Jakub Sitnickid0225782017-08-09 17:39:12 +02001865 cb->seq = net->dev_base_seq;
1866 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Jiri Benc79e1ad12017-11-02 17:04:38 -02001867 if (netnsid >= 0)
1868 put_net(tgt_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
David Ahernf6c57752017-05-15 23:19:17 -07001870 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871}
1872
Johannes Bergfceb6432017-04-12 14:34:07 +02001873int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
1874 struct netlink_ext_ack *exterr)
Jiri Pirkof7b12602014-02-18 20:53:18 +01001875{
Johannes Bergfceb6432017-04-12 14:34:07 +02001876 return nla_parse(tb, IFLA_MAX, head, len, ifla_policy, exterr);
Jiri Pirkof7b12602014-02-18 20:53:18 +01001877}
1878EXPORT_SYMBOL(rtnl_nla_parse_ifla);
Scott Feldman57b61082010-05-17 22:49:55 -07001879
Eric W. Biederman81adee42009-11-08 00:53:51 -08001880struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1881{
1882 struct net *net;
1883 /* Examine the link attributes and figure out which
1884 * network namespace we are talking about.
1885 */
1886 if (tb[IFLA_NET_NS_PID])
1887 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001888 else if (tb[IFLA_NET_NS_FD])
1889 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
Eric W. Biederman81adee42009-11-08 00:53:51 -08001890 else
1891 net = get_net(src_net);
1892 return net;
1893}
1894EXPORT_SYMBOL(rtnl_link_get_net);
1895
Thomas Graf1840bb12008-02-23 19:54:36 -08001896static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1897{
1898 if (dev) {
1899 if (tb[IFLA_ADDRESS] &&
1900 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1901 return -EINVAL;
1902
1903 if (tb[IFLA_BROADCAST] &&
1904 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1905 return -EINVAL;
1906 }
1907
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001908 if (tb[IFLA_AF_SPEC]) {
1909 struct nlattr *af;
1910 int rem, err;
1911
1912 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1913 const struct rtnl_af_ops *af_ops;
1914
Florian Westphal5fa85a02017-10-16 15:44:36 +02001915 rcu_read_lock();
1916 af_ops = rtnl_af_lookup(nla_type(af));
1917 if (!af_ops) {
1918 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001919 return -EAFNOSUPPORT;
Florian Westphal5fa85a02017-10-16 15:44:36 +02001920 }
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001921
Florian Westphal5fa85a02017-10-16 15:44:36 +02001922 if (!af_ops->set_link_af) {
1923 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001924 return -EOPNOTSUPP;
Florian Westphal5fa85a02017-10-16 15:44:36 +02001925 }
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001926
1927 if (af_ops->validate_link_af) {
Kurt Van Dijck6d3a9a62011-01-26 04:55:24 +00001928 err = af_ops->validate_link_af(dev, af);
Florian Westphal5fa85a02017-10-16 15:44:36 +02001929 if (err < 0) {
1930 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001931 return err;
Florian Westphal5fa85a02017-10-16 15:44:36 +02001932 }
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001933 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02001934
1935 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001936 }
1937 }
1938
Thomas Graf1840bb12008-02-23 19:54:36 -08001939 return 0;
1940}
1941
Eli Cohencc8e27c2016-03-11 22:58:34 +02001942static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
1943 int guid_type)
1944{
1945 const struct net_device_ops *ops = dev->netdev_ops;
1946
1947 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
1948}
1949
1950static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
1951{
1952 if (dev->type != ARPHRD_INFINIBAND)
1953 return -EOPNOTSUPP;
1954
1955 return handle_infiniband_guid(dev, ivt, guid_type);
1956}
1957
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001958static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
Chris Wrightc02db8c2010-05-16 01:05:45 -07001959{
Chris Wrightc02db8c2010-05-16 01:05:45 -07001960 const struct net_device_ops *ops = dev->netdev_ops;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001961 int err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001962
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001963 if (tb[IFLA_VF_MAC]) {
1964 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001965
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001966 err = -EOPNOTSUPP;
1967 if (ops->ndo_set_vf_mac)
1968 err = ops->ndo_set_vf_mac(dev, ivm->vf,
1969 ivm->mac);
1970 if (err < 0)
1971 return err;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001972 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001973
1974 if (tb[IFLA_VF_VLAN]) {
1975 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
1976
1977 err = -EOPNOTSUPP;
1978 if (ops->ndo_set_vf_vlan)
1979 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
Moshe Shemesh79aab092016-09-22 12:11:15 +03001980 ivv->qos,
1981 htons(ETH_P_8021Q));
1982 if (err < 0)
1983 return err;
1984 }
1985
1986 if (tb[IFLA_VF_VLAN_LIST]) {
1987 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
1988 struct nlattr *attr;
1989 int rem, len = 0;
1990
1991 err = -EOPNOTSUPP;
1992 if (!ops->ndo_set_vf_vlan)
1993 return err;
1994
1995 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
1996 if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
1997 nla_len(attr) < NLA_HDRLEN) {
1998 return -EINVAL;
1999 }
2000 if (len >= MAX_VLAN_LIST_LEN)
2001 return -EOPNOTSUPP;
2002 ivvl[len] = nla_data(attr);
2003
2004 len++;
2005 }
Arnd Bergmannfa34cd92016-09-30 18:13:49 +02002006 if (len == 0)
2007 return -EINVAL;
2008
Moshe Shemesh79aab092016-09-22 12:11:15 +03002009 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
2010 ivvl[0]->qos, ivvl[0]->vlan_proto);
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002011 if (err < 0)
2012 return err;
2013 }
2014
2015 if (tb[IFLA_VF_TX_RATE]) {
2016 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
2017 struct ifla_vf_info ivf;
2018
2019 err = -EOPNOTSUPP;
2020 if (ops->ndo_get_vf_config)
2021 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
2022 if (err < 0)
2023 return err;
2024
2025 err = -EOPNOTSUPP;
2026 if (ops->ndo_set_vf_rate)
2027 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2028 ivf.min_tx_rate,
2029 ivt->rate);
2030 if (err < 0)
2031 return err;
2032 }
2033
2034 if (tb[IFLA_VF_RATE]) {
2035 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
2036
2037 err = -EOPNOTSUPP;
2038 if (ops->ndo_set_vf_rate)
2039 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2040 ivt->min_tx_rate,
2041 ivt->max_tx_rate);
2042 if (err < 0)
2043 return err;
2044 }
2045
2046 if (tb[IFLA_VF_SPOOFCHK]) {
2047 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
2048
2049 err = -EOPNOTSUPP;
2050 if (ops->ndo_set_vf_spoofchk)
2051 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
2052 ivs->setting);
2053 if (err < 0)
2054 return err;
2055 }
2056
2057 if (tb[IFLA_VF_LINK_STATE]) {
2058 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
2059
2060 err = -EOPNOTSUPP;
2061 if (ops->ndo_set_vf_link_state)
2062 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
2063 ivl->link_state);
2064 if (err < 0)
2065 return err;
2066 }
2067
2068 if (tb[IFLA_VF_RSS_QUERY_EN]) {
2069 struct ifla_vf_rss_query_en *ivrssq_en;
2070
2071 err = -EOPNOTSUPP;
2072 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
2073 if (ops->ndo_set_vf_rss_query_en)
2074 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
2075 ivrssq_en->setting);
2076 if (err < 0)
2077 return err;
2078 }
2079
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00002080 if (tb[IFLA_VF_TRUST]) {
2081 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
2082
2083 err = -EOPNOTSUPP;
2084 if (ops->ndo_set_vf_trust)
2085 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
2086 if (err < 0)
2087 return err;
2088 }
2089
Eli Cohencc8e27c2016-03-11 22:58:34 +02002090 if (tb[IFLA_VF_IB_NODE_GUID]) {
2091 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
2092
2093 if (!ops->ndo_set_vf_guid)
2094 return -EOPNOTSUPP;
2095
2096 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
2097 }
2098
2099 if (tb[IFLA_VF_IB_PORT_GUID]) {
2100 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
2101
2102 if (!ops->ndo_set_vf_guid)
2103 return -EOPNOTSUPP;
2104
2105 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
2106 }
2107
Chris Wrightc02db8c2010-05-16 01:05:45 -07002108 return err;
2109}
2110
David Ahern33eaf2a2017-10-04 17:48:46 -07002111static int do_set_master(struct net_device *dev, int ifindex,
2112 struct netlink_ext_ack *extack)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002113{
Jiri Pirko898e5062013-01-03 22:48:52 +00002114 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002115 const struct net_device_ops *ops;
2116 int err;
2117
Jiri Pirko898e5062013-01-03 22:48:52 +00002118 if (upper_dev) {
2119 if (upper_dev->ifindex == ifindex)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002120 return 0;
Jiri Pirko898e5062013-01-03 22:48:52 +00002121 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002122 if (ops->ndo_del_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002123 err = ops->ndo_del_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002124 if (err)
2125 return err;
2126 } else {
2127 return -EOPNOTSUPP;
2128 }
2129 }
2130
2131 if (ifindex) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002132 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
2133 if (!upper_dev)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002134 return -EINVAL;
Jiri Pirko898e5062013-01-03 22:48:52 +00002135 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002136 if (ops->ndo_add_slave) {
David Ahern33eaf2a2017-10-04 17:48:46 -07002137 err = ops->ndo_add_slave(upper_dev, dev, extack);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002138 if (err)
2139 return err;
2140 } else {
2141 return -EOPNOTSUPP;
2142 }
2143 }
2144 return 0;
2145}
2146
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002147#define DO_SETLINK_MODIFIED 0x01
Nicolas Dichtelba998902014-09-01 16:07:29 +02002148/* notify flag means notify + modified. */
2149#define DO_SETLINK_NOTIFY 0x03
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002150static int do_setlink(const struct sk_buff *skb,
2151 struct net_device *dev, struct ifinfomsg *ifm,
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002152 struct netlink_ext_ack *extack,
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002153 struct nlattr **tb, char *ifname, int status)
Patrick McHardy0157f602007-06-13 12:03:36 -07002154{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002155 const struct net_device_ops *ops = dev->netdev_ops;
Patrick McHardy0157f602007-06-13 12:03:36 -07002156 int err;
2157
Eric W. Biedermanf0630522011-05-04 17:51:50 -07002158 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
Eric W. Biederman81adee42009-11-08 00:53:51 -08002159 struct net *net = rtnl_link_get_net(dev_net(dev), tb);
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002160 if (IS_ERR(net)) {
2161 err = PTR_ERR(net);
2162 goto errout;
2163 }
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002164 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
Nicolas Dichtele0ebde02014-11-27 10:16:15 +01002165 put_net(net);
Eric W. Biedermanb51642f2012-11-16 03:03:11 +00002166 err = -EPERM;
2167 goto errout;
2168 }
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002169 err = dev_change_net_namespace(dev, net, ifname);
2170 put_net(net);
2171 if (err)
2172 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002173 status |= DO_SETLINK_MODIFIED;
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002174 }
2175
Patrick McHardy0157f602007-06-13 12:03:36 -07002176 if (tb[IFLA_MAP]) {
2177 struct rtnl_link_ifmap *u_map;
2178 struct ifmap k_map;
2179
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002180 if (!ops->ndo_set_config) {
Patrick McHardy0157f602007-06-13 12:03:36 -07002181 err = -EOPNOTSUPP;
2182 goto errout;
2183 }
2184
2185 if (!netif_device_present(dev)) {
2186 err = -ENODEV;
2187 goto errout;
2188 }
2189
2190 u_map = nla_data(tb[IFLA_MAP]);
2191 k_map.mem_start = (unsigned long) u_map->mem_start;
2192 k_map.mem_end = (unsigned long) u_map->mem_end;
2193 k_map.base_addr = (unsigned short) u_map->base_addr;
2194 k_map.irq = (unsigned char) u_map->irq;
2195 k_map.dma = (unsigned char) u_map->dma;
2196 k_map.port = (unsigned char) u_map->port;
2197
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002198 err = ops->ndo_set_config(dev, &k_map);
Patrick McHardy0157f602007-06-13 12:03:36 -07002199 if (err < 0)
2200 goto errout;
2201
Nicolas Dichtelba998902014-09-01 16:07:29 +02002202 status |= DO_SETLINK_NOTIFY;
Patrick McHardy0157f602007-06-13 12:03:36 -07002203 }
2204
2205 if (tb[IFLA_ADDRESS]) {
2206 struct sockaddr *sa;
2207 int len;
2208
WANG Cong153711f2017-07-20 11:27:57 -07002209 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2210 sizeof(*sa));
Patrick McHardy0157f602007-06-13 12:03:36 -07002211 sa = kmalloc(len, GFP_KERNEL);
2212 if (!sa) {
2213 err = -ENOMEM;
2214 goto errout;
2215 }
2216 sa->sa_family = dev->type;
2217 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
2218 dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00002219 err = dev_set_mac_address(dev, sa);
Patrick McHardy0157f602007-06-13 12:03:36 -07002220 kfree(sa);
2221 if (err)
2222 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002223 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002224 }
2225
2226 if (tb[IFLA_MTU]) {
2227 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
2228 if (err < 0)
2229 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002230 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002231 }
2232
Vlad Dogarucbda10f2011-01-13 23:38:30 +00002233 if (tb[IFLA_GROUP]) {
2234 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Nicolas Dichtelba998902014-09-01 16:07:29 +02002235 status |= DO_SETLINK_NOTIFY;
Vlad Dogarucbda10f2011-01-13 23:38:30 +00002236 }
2237
Patrick McHardy0157f602007-06-13 12:03:36 -07002238 /*
2239 * Interface selected by interface index but interface
2240 * name provided implies that a name change has been
2241 * requested.
2242 */
2243 if (ifm->ifi_index > 0 && ifname[0]) {
2244 err = dev_change_name(dev, ifname);
2245 if (err < 0)
2246 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002247 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002248 }
2249
Stephen Hemminger0b815a12008-09-22 21:28:11 -07002250 if (tb[IFLA_IFALIAS]) {
2251 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2252 nla_len(tb[IFLA_IFALIAS]));
2253 if (err < 0)
2254 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002255 status |= DO_SETLINK_NOTIFY;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07002256 }
2257
Patrick McHardy0157f602007-06-13 12:03:36 -07002258 if (tb[IFLA_BROADCAST]) {
2259 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00002260 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Patrick McHardy0157f602007-06-13 12:03:36 -07002261 }
2262
2263 if (ifm->ifi_flags || ifm->ifi_change) {
Patrick McHardy3729d502010-02-26 06:34:54 +00002264 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
Johannes Berg5f9021c2008-11-16 23:20:31 -08002265 if (err < 0)
2266 goto errout;
Patrick McHardy0157f602007-06-13 12:03:36 -07002267 }
2268
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002269 if (tb[IFLA_MASTER]) {
David Ahern33eaf2a2017-10-04 17:48:46 -07002270 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002271 if (err)
2272 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002273 status |= DO_SETLINK_MODIFIED;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002274 }
2275
Jiri Pirko9a572472012-12-27 23:49:39 +00002276 if (tb[IFLA_CARRIER]) {
2277 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2278 if (err)
2279 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002280 status |= DO_SETLINK_MODIFIED;
Jiri Pirko9a572472012-12-27 23:49:39 +00002281 }
2282
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002283 if (tb[IFLA_TXQLEN]) {
Alexey Dobriyan0cd29502017-05-17 13:30:44 +03002284 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
2285 unsigned int orig_len = dev->tx_queue_len;
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002286
Jason Wang08294a22016-06-30 14:45:35 +08002287 if (dev->tx_queue_len ^ value) {
2288 dev->tx_queue_len = value;
2289 err = call_netdevice_notifiers(
2290 NETDEV_CHANGE_TX_QUEUE_LEN, dev);
2291 err = notifier_to_errno(err);
2292 if (err) {
2293 dev->tx_queue_len = orig_len;
2294 goto errout;
2295 }
Xin Long2d7f6692017-10-15 18:13:46 +08002296 status |= DO_SETLINK_MODIFIED;
Jason Wang08294a22016-06-30 14:45:35 +08002297 }
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002298 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002299
Stephen Hemminger46e6b992017-12-07 15:40:19 -08002300 if (tb[IFLA_GSO_MAX_SIZE]) {
2301 u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]);
2302
2303 if (max_size > GSO_MAX_SIZE) {
2304 err = -EINVAL;
2305 goto errout;
2306 }
2307
2308 if (dev->gso_max_size ^ max_size) {
2309 netif_set_gso_max_size(dev, max_size);
2310 status |= DO_SETLINK_MODIFIED;
2311 }
2312 }
2313
2314 if (tb[IFLA_GSO_MAX_SEGS]) {
2315 u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
2316
2317 if (max_segs > GSO_MAX_SEGS) {
2318 err = -EINVAL;
2319 goto errout;
2320 }
2321
2322 if (dev->gso_max_segs ^ max_segs) {
2323 dev->gso_max_segs = max_segs;
2324 status |= DO_SETLINK_MODIFIED;
2325 }
2326 }
2327
Patrick McHardy0157f602007-06-13 12:03:36 -07002328 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002329 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Patrick McHardy0157f602007-06-13 12:03:36 -07002330
2331 if (tb[IFLA_LINKMODE]) {
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002332 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2333
David S. Miller93b2d4a2008-02-17 18:35:07 -08002334 write_lock_bh(&dev_base_lock);
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002335 if (dev->link_mode ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02002336 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002337 dev->link_mode = value;
David S. Miller93b2d4a2008-02-17 18:35:07 -08002338 write_unlock_bh(&dev_base_lock);
Patrick McHardy0157f602007-06-13 12:03:36 -07002339 }
2340
Chris Wrightc02db8c2010-05-16 01:05:45 -07002341 if (tb[IFLA_VFINFO_LIST]) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002342 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
Chris Wrightc02db8c2010-05-16 01:05:45 -07002343 struct nlattr *attr;
2344 int rem;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002345
Chris Wrightc02db8c2010-05-16 01:05:45 -07002346 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002347 if (nla_type(attr) != IFLA_VF_INFO ||
2348 nla_len(attr) < NLA_HDRLEN) {
David Howells253683b2010-05-21 02:25:27 +00002349 err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002350 goto errout;
David Howells253683b2010-05-21 02:25:27 +00002351 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002352 err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr,
Johannes Bergfceb6432017-04-12 14:34:07 +02002353 ifla_vf_policy, NULL);
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002354 if (err < 0)
2355 goto errout;
2356 err = do_setvfinfo(dev, vfinfo);
Chris Wrightc02db8c2010-05-16 01:05:45 -07002357 if (err < 0)
2358 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002359 status |= DO_SETLINK_NOTIFY;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002360 }
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00002361 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002362 err = 0;
2363
Scott Feldman57b61082010-05-17 22:49:55 -07002364 if (tb[IFLA_VF_PORTS]) {
2365 struct nlattr *port[IFLA_PORT_MAX+1];
2366 struct nlattr *attr;
2367 int vf;
2368 int rem;
2369
2370 err = -EOPNOTSUPP;
2371 if (!ops->ndo_set_vf_port)
2372 goto errout;
2373
2374 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
Daniel Borkmann035d2102015-07-13 00:06:02 +02002375 if (nla_type(attr) != IFLA_VF_PORT ||
2376 nla_len(attr) < NLA_HDRLEN) {
2377 err = -EINVAL;
2378 goto errout;
2379 }
2380 err = nla_parse_nested(port, IFLA_PORT_MAX, attr,
Johannes Bergfceb6432017-04-12 14:34:07 +02002381 ifla_port_policy, NULL);
Scott Feldman57b61082010-05-17 22:49:55 -07002382 if (err < 0)
2383 goto errout;
2384 if (!port[IFLA_PORT_VF]) {
2385 err = -EOPNOTSUPP;
2386 goto errout;
2387 }
2388 vf = nla_get_u32(port[IFLA_PORT_VF]);
2389 err = ops->ndo_set_vf_port(dev, vf, port);
2390 if (err < 0)
2391 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002392 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07002393 }
2394 }
2395 err = 0;
2396
2397 if (tb[IFLA_PORT_SELF]) {
2398 struct nlattr *port[IFLA_PORT_MAX+1];
2399
2400 err = nla_parse_nested(port, IFLA_PORT_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +02002401 tb[IFLA_PORT_SELF], ifla_port_policy,
2402 NULL);
Scott Feldman57b61082010-05-17 22:49:55 -07002403 if (err < 0)
2404 goto errout;
2405
2406 err = -EOPNOTSUPP;
2407 if (ops->ndo_set_vf_port)
2408 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
2409 if (err < 0)
2410 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002411 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07002412 }
Thomas Graff8ff1822010-11-16 04:30:14 +00002413
2414 if (tb[IFLA_AF_SPEC]) {
2415 struct nlattr *af;
2416 int rem;
2417
2418 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2419 const struct rtnl_af_ops *af_ops;
2420
Florian Westphal5fa85a02017-10-16 15:44:36 +02002421 rcu_read_lock();
2422
Gustavo A. R. Silva058c8d52017-10-20 19:43:11 -05002423 BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
Thomas Graff8ff1822010-11-16 04:30:14 +00002424
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002425 err = af_ops->set_link_af(dev, af);
Florian Westphal5fa85a02017-10-16 15:44:36 +02002426 if (err < 0) {
2427 rcu_read_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +00002428 goto errout;
Florian Westphal5fa85a02017-10-16 15:44:36 +02002429 }
Thomas Graff8ff1822010-11-16 04:30:14 +00002430
Florian Westphal5fa85a02017-10-16 15:44:36 +02002431 rcu_read_unlock();
Nicolas Dichtelba998902014-09-01 16:07:29 +02002432 status |= DO_SETLINK_NOTIFY;
Thomas Graff8ff1822010-11-16 04:30:14 +00002433 }
2434 }
Scott Feldman57b61082010-05-17 22:49:55 -07002435 err = 0;
2436
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07002437 if (tb[IFLA_PROTO_DOWN]) {
2438 err = dev_change_proto_down(dev,
2439 nla_get_u8(tb[IFLA_PROTO_DOWN]));
2440 if (err)
2441 goto errout;
2442 status |= DO_SETLINK_NOTIFY;
2443 }
2444
Brenden Blancod1fdd912016-07-19 12:16:49 -07002445 if (tb[IFLA_XDP]) {
2446 struct nlattr *xdp[IFLA_XDP_MAX + 1];
Daniel Borkmann85de8572016-11-28 23:16:54 +01002447 u32 xdp_flags = 0;
Brenden Blancod1fdd912016-07-19 12:16:49 -07002448
2449 err = nla_parse_nested(xdp, IFLA_XDP_MAX, tb[IFLA_XDP],
Johannes Bergfceb6432017-04-12 14:34:07 +02002450 ifla_xdp_policy, NULL);
Brenden Blancod1fdd912016-07-19 12:16:49 -07002451 if (err < 0)
2452 goto errout;
2453
Martin KaFai Lau58038692017-06-15 17:29:09 -07002454 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
Brenden Blanco262d8622016-07-20 17:22:34 -07002455 err = -EINVAL;
2456 goto errout;
2457 }
Daniel Borkmann85de8572016-11-28 23:16:54 +01002458
2459 if (xdp[IFLA_XDP_FLAGS]) {
2460 xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
2461 if (xdp_flags & ~XDP_FLAGS_MASK) {
2462 err = -EINVAL;
2463 goto errout;
2464 }
Jakub Kicinskiee5d0322017-06-21 18:25:04 -07002465 if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
Daniel Borkmann0489df92017-05-12 01:04:45 +02002466 err = -EINVAL;
2467 goto errout;
2468 }
Daniel Borkmann85de8572016-11-28 23:16:54 +01002469 }
2470
Brenden Blancod1fdd912016-07-19 12:16:49 -07002471 if (xdp[IFLA_XDP_FD]) {
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002472 err = dev_change_xdp_fd(dev, extack,
Daniel Borkmann85de8572016-11-28 23:16:54 +01002473 nla_get_s32(xdp[IFLA_XDP_FD]),
2474 xdp_flags);
Brenden Blancod1fdd912016-07-19 12:16:49 -07002475 if (err)
2476 goto errout;
2477 status |= DO_SETLINK_NOTIFY;
2478 }
2479 }
2480
Patrick McHardy0157f602007-06-13 12:03:36 -07002481errout:
Nicolas Dichtelba998902014-09-01 16:07:29 +02002482 if (status & DO_SETLINK_MODIFIED) {
Xin Long64ff90c2017-10-15 18:13:45 +08002483 if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
Nicolas Dichtelba998902014-09-01 16:07:29 +02002484 netdev_state_change(dev);
2485
2486 if (err < 0)
2487 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",
2488 dev->name);
2489 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002490
Patrick McHardy0157f602007-06-13 12:03:36 -07002491 return err;
2492}
2493
David Ahernc21ef3e2017-04-16 09:48:24 -07002494static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2495 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002497 struct net *net = sock_net(skb->sk);
Thomas Grafda5e0492006-08-10 21:17:37 -07002498 struct ifinfomsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 struct net_device *dev;
Patrick McHardy0157f602007-06-13 12:03:36 -07002500 int err;
Thomas Grafda5e0492006-08-10 21:17:37 -07002501 struct nlattr *tb[IFLA_MAX+1];
2502 char ifname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503
David Ahernc21ef3e2017-04-16 09:48:24 -07002504 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy,
2505 extack);
Thomas Grafda5e0492006-08-10 21:17:37 -07002506 if (err < 0)
2507 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
Jiri Benc79e1ad12017-11-02 17:04:38 -02002509 if (tb[IFLA_IF_NETNSID])
2510 return -EOPNOTSUPP;
2511
Thomas Graf5176f912006-08-26 20:13:18 -07002512 if (tb[IFLA_IFNAME])
2513 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
Patrick McHardy78e5b8912006-09-13 20:35:36 -07002514 else
2515 ifname[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 err = -EINVAL;
Thomas Grafda5e0492006-08-10 21:17:37 -07002518 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -07002519 if (ifm->ifi_index > 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00002520 dev = __dev_get_by_index(net, ifm->ifi_index);
Thomas Grafda5e0492006-08-10 21:17:37 -07002521 else if (tb[IFLA_IFNAME])
Eric Dumazeta3d12892009-10-21 10:59:31 +00002522 dev = __dev_get_by_name(net, ifname);
Thomas Grafda5e0492006-08-10 21:17:37 -07002523 else
2524 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
Thomas Grafda5e0492006-08-10 21:17:37 -07002526 if (dev == NULL) {
2527 err = -ENODEV;
2528 goto errout;
2529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530
Eric Dumazete0d087a2009-11-07 01:26:17 -08002531 err = validate_linkmsg(dev, tb);
2532 if (err < 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00002533 goto errout;
Thomas Grafda5e0492006-08-10 21:17:37 -07002534
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002535 err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
Thomas Grafda5e0492006-08-10 21:17:37 -07002536errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 return err;
2538}
2539
WANG Cong66400d52015-03-24 11:53:31 -07002540static int rtnl_group_dellink(const struct net *net, int group)
2541{
2542 struct net_device *dev, *aux;
2543 LIST_HEAD(list_kill);
2544 bool found = false;
2545
2546 if (!group)
2547 return -EPERM;
2548
2549 for_each_netdev(net, dev) {
2550 if (dev->group == group) {
2551 const struct rtnl_link_ops *ops;
2552
2553 found = true;
2554 ops = dev->rtnl_link_ops;
2555 if (!ops || !ops->dellink)
2556 return -EOPNOTSUPP;
2557 }
2558 }
2559
2560 if (!found)
2561 return -ENODEV;
2562
2563 for_each_netdev_safe(net, dev, aux) {
2564 if (dev->group == group) {
2565 const struct rtnl_link_ops *ops;
2566
2567 ops = dev->rtnl_link_ops;
2568 ops->dellink(dev, &list_kill);
2569 }
2570 }
2571 unregister_netdevice_many(&list_kill);
2572
2573 return 0;
2574}
2575
Thomas Graf614732e2015-07-21 10:44:06 +02002576int rtnl_delete_link(struct net_device *dev)
2577{
2578 const struct rtnl_link_ops *ops;
2579 LIST_HEAD(list_kill);
2580
2581 ops = dev->rtnl_link_ops;
2582 if (!ops || !ops->dellink)
2583 return -EOPNOTSUPP;
2584
2585 ops->dellink(dev, &list_kill);
2586 unregister_netdevice_many(&list_kill);
2587
2588 return 0;
2589}
2590EXPORT_SYMBOL_GPL(rtnl_delete_link);
2591
David Ahernc21ef3e2017-04-16 09:48:24 -07002592static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
2593 struct netlink_ext_ack *extack)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002594{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002595 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002596 struct net_device *dev;
2597 struct ifinfomsg *ifm;
2598 char ifname[IFNAMSIZ];
2599 struct nlattr *tb[IFLA_MAX+1];
2600 int err;
2601
David Ahernc21ef3e2017-04-16 09:48:24 -07002602 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002603 if (err < 0)
2604 return err;
2605
Jiri Benc79e1ad12017-11-02 17:04:38 -02002606 if (tb[IFLA_IF_NETNSID])
2607 return -EOPNOTSUPP;
2608
Patrick McHardy38f7b872007-06-13 12:03:51 -07002609 if (tb[IFLA_IFNAME])
2610 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2611
2612 ifm = nlmsg_data(nlh);
2613 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07002614 dev = __dev_get_by_index(net, ifm->ifi_index);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002615 else if (tb[IFLA_IFNAME])
Eric W. Biederman881d9662007-09-17 11:56:21 -07002616 dev = __dev_get_by_name(net, ifname);
WANG Cong66400d52015-03-24 11:53:31 -07002617 else if (tb[IFLA_GROUP])
2618 return rtnl_group_dellink(net, nla_get_u32(tb[IFLA_GROUP]));
Patrick McHardy38f7b872007-06-13 12:03:51 -07002619 else
2620 return -EINVAL;
2621
2622 if (!dev)
2623 return -ENODEV;
2624
Thomas Graf614732e2015-07-21 10:44:06 +02002625 return rtnl_delete_link(dev);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002626}
2627
Patrick McHardy3729d502010-02-26 06:34:54 +00002628int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
2629{
2630 unsigned int old_flags;
2631 int err;
2632
2633 old_flags = dev->flags;
2634 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
2635 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
2636 if (err < 0)
2637 return err;
2638 }
2639
2640 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
Patrick McHardy3729d502010-02-26 06:34:54 +00002641
Nicolas Dichtela528c212013-09-25 12:02:44 +02002642 __dev_notify_flags(dev, old_flags, ~0U);
Patrick McHardy3729d502010-02-26 06:34:54 +00002643 return 0;
2644}
2645EXPORT_SYMBOL(rtnl_configure_link);
2646
Rami Rosenc0713562012-11-30 01:08:47 +00002647struct net_device *rtnl_create_link(struct net *net,
Thomas Graf78ebb0d2015-04-10 01:45:53 +02002648 const char *ifname, unsigned char name_assign_type,
Tom Gundersen55177502014-07-14 16:37:25 +02002649 const struct rtnl_link_ops *ops, struct nlattr *tb[])
Pavel Emelianove7199282007-08-08 22:16:38 -07002650{
Pavel Emelianove7199282007-08-08 22:16:38 -07002651 struct net_device *dev;
Jiri Pirkod40156a2012-07-20 02:28:47 +00002652 unsigned int num_tx_queues = 1;
2653 unsigned int num_rx_queues = 1;
Pavel Emelianove7199282007-08-08 22:16:38 -07002654
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002655 if (tb[IFLA_NUM_TX_QUEUES])
2656 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
2657 else if (ops->get_num_tx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002658 num_tx_queues = ops->get_num_tx_queues();
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002659
2660 if (tb[IFLA_NUM_RX_QUEUES])
2661 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
2662 else if (ops->get_num_rx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002663 num_rx_queues = ops->get_num_rx_queues();
stephen hemmingerefacb302012-04-10 18:34:43 +00002664
Tom Gundersen55177502014-07-14 16:37:25 +02002665 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
Tom Gundersenc835a672014-07-14 16:37:24 +02002666 ops->setup, num_tx_queues, num_rx_queues);
Pavel Emelianove7199282007-08-08 22:16:38 -07002667 if (!dev)
Tobias Klauserd1892e42017-02-20 16:32:06 +01002668 return ERR_PTR(-ENOMEM);
Pavel Emelianove7199282007-08-08 22:16:38 -07002669
Eric W. Biederman81adee42009-11-08 00:53:51 -08002670 dev_net_set(dev, net);
2671 dev->rtnl_link_ops = ops;
Patrick McHardy3729d502010-02-26 06:34:54 +00002672 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
Eric W. Biederman81adee42009-11-08 00:53:51 -08002673
Pavel Emelianove7199282007-08-08 22:16:38 -07002674 if (tb[IFLA_MTU])
2675 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002676 if (tb[IFLA_ADDRESS]) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002677 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
2678 nla_len(tb[IFLA_ADDRESS]));
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002679 dev->addr_assign_type = NET_ADDR_SET;
2680 }
Pavel Emelianove7199282007-08-08 22:16:38 -07002681 if (tb[IFLA_BROADCAST])
2682 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
2683 nla_len(tb[IFLA_BROADCAST]));
2684 if (tb[IFLA_TXQLEN])
2685 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
2686 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002687 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Pavel Emelianove7199282007-08-08 22:16:38 -07002688 if (tb[IFLA_LINKMODE])
2689 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
Patrick McHardyffa934f2011-01-20 03:00:42 +00002690 if (tb[IFLA_GROUP])
2691 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Stephen Hemminger46e6b992017-12-07 15:40:19 -08002692 if (tb[IFLA_GSO_MAX_SIZE])
2693 netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE]));
2694 if (tb[IFLA_GSO_MAX_SEGS])
Stephen Hemmingera0b586f2017-12-08 15:34:13 -08002695 dev->gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
Pavel Emelianove7199282007-08-08 22:16:38 -07002696
2697 return dev;
Pavel Emelianove7199282007-08-08 22:16:38 -07002698}
Eric Dumazete0d087a2009-11-07 01:26:17 -08002699EXPORT_SYMBOL(rtnl_create_link);
Pavel Emelianove7199282007-08-08 22:16:38 -07002700
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002701static int rtnl_group_changelink(const struct sk_buff *skb,
2702 struct net *net, int group,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002703 struct ifinfomsg *ifm,
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002704 struct netlink_ext_ack *extack,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002705 struct nlattr **tb)
2706{
WANG Congd0795352015-03-23 16:31:09 -07002707 struct net_device *dev, *aux;
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002708 int err;
2709
WANG Congd0795352015-03-23 16:31:09 -07002710 for_each_netdev_safe(net, dev, aux) {
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002711 if (dev->group == group) {
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002712 err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002713 if (err < 0)
2714 return err;
2715 }
2716 }
2717
2718 return 0;
2719}
2720
David Ahernc21ef3e2017-04-16 09:48:24 -07002721static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2722 struct netlink_ext_ack *extack)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002723{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002724 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002725 const struct rtnl_link_ops *ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002726 const struct rtnl_link_ops *m_ops = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002727 struct net_device *dev;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002728 struct net_device *master_dev = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002729 struct ifinfomsg *ifm;
2730 char kind[MODULE_NAME_LEN];
2731 char ifname[IFNAMSIZ];
2732 struct nlattr *tb[IFLA_MAX+1];
2733 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
Tom Gundersen55177502014-07-14 16:37:25 +02002734 unsigned char name_assign_type = NET_NAME_USER;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002735 int err;
2736
Johannes Berg95a5afc2008-10-16 15:24:51 -07002737#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002738replay:
Thomas Graf8072f082007-07-31 14:13:50 -07002739#endif
David Ahernc21ef3e2017-04-16 09:48:24 -07002740 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002741 if (err < 0)
2742 return err;
2743
Jiri Benc79e1ad12017-11-02 17:04:38 -02002744 if (tb[IFLA_IF_NETNSID])
2745 return -EOPNOTSUPP;
2746
Patrick McHardy38f7b872007-06-13 12:03:51 -07002747 if (tb[IFLA_IFNAME])
2748 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2749 else
2750 ifname[0] = '\0';
2751
2752 ifm = nlmsg_data(nlh);
2753 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07002754 dev = __dev_get_by_index(net, ifm->ifi_index);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002755 else {
2756 if (ifname[0])
2757 dev = __dev_get_by_name(net, ifname);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002758 else
2759 dev = NULL;
2760 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002761
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002762 if (dev) {
2763 master_dev = netdev_master_upper_dev_get(dev);
2764 if (master_dev)
2765 m_ops = master_dev->rtnl_link_ops;
2766 }
2767
Eric Dumazete0d087a2009-11-07 01:26:17 -08002768 err = validate_linkmsg(dev, tb);
2769 if (err < 0)
Thomas Graf1840bb12008-02-23 19:54:36 -08002770 return err;
2771
Patrick McHardy38f7b872007-06-13 12:03:51 -07002772 if (tb[IFLA_LINKINFO]) {
2773 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +02002774 tb[IFLA_LINKINFO], ifla_info_policy,
2775 NULL);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002776 if (err < 0)
2777 return err;
2778 } else
2779 memset(linkinfo, 0, sizeof(linkinfo));
2780
2781 if (linkinfo[IFLA_INFO_KIND]) {
2782 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
2783 ops = rtnl_link_ops_get(kind);
2784 } else {
2785 kind[0] = '\0';
2786 ops = NULL;
2787 }
2788
2789 if (1) {
Sasha Levin4e10fd52015-02-24 14:14:35 -05002790 struct nlattr *attr[ops ? ops->maxtype + 1 : 1];
2791 struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1];
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002792 struct nlattr **data = NULL;
2793 struct nlattr **slave_data = NULL;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002794 struct net *dest_net, *link_net = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002795
2796 if (ops) {
2797 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
2798 err = nla_parse_nested(attr, ops->maxtype,
2799 linkinfo[IFLA_INFO_DATA],
Johannes Bergfceb6432017-04-12 14:34:07 +02002800 ops->policy, NULL);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002801 if (err < 0)
2802 return err;
2803 data = attr;
2804 }
2805 if (ops->validate) {
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02002806 err = ops->validate(tb, data, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002807 if (err < 0)
2808 return err;
2809 }
2810 }
2811
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002812 if (m_ops) {
2813 if (m_ops->slave_maxtype &&
2814 linkinfo[IFLA_INFO_SLAVE_DATA]) {
2815 err = nla_parse_nested(slave_attr,
2816 m_ops->slave_maxtype,
2817 linkinfo[IFLA_INFO_SLAVE_DATA],
Johannes Bergfceb6432017-04-12 14:34:07 +02002818 m_ops->slave_policy,
2819 NULL);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002820 if (err < 0)
2821 return err;
2822 slave_data = slave_attr;
2823 }
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002824 }
2825
Patrick McHardy38f7b872007-06-13 12:03:51 -07002826 if (dev) {
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002827 int status = 0;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002828
2829 if (nlh->nlmsg_flags & NLM_F_EXCL)
2830 return -EEXIST;
2831 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2832 return -EOPNOTSUPP;
2833
2834 if (linkinfo[IFLA_INFO_DATA]) {
2835 if (!ops || ops != dev->rtnl_link_ops ||
2836 !ops->changelink)
2837 return -EOPNOTSUPP;
2838
Matthias Schifferad744b22017-06-25 23:56:00 +02002839 err = ops->changelink(dev, tb, data, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002840 if (err < 0)
2841 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002842 status |= DO_SETLINK_NOTIFY;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002843 }
2844
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002845 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2846 if (!m_ops || !m_ops->slave_changelink)
2847 return -EOPNOTSUPP;
2848
2849 err = m_ops->slave_changelink(master_dev, dev,
Matthias Schiffer17dd0ec2017-06-25 23:56:02 +02002850 tb, slave_data,
2851 extack);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002852 if (err < 0)
2853 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002854 status |= DO_SETLINK_NOTIFY;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002855 }
2856
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002857 return do_setlink(skb, dev, ifm, extack, tb, ifname,
2858 status);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002859 }
2860
Patrick McHardyffa934f2011-01-20 03:00:42 +00002861 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2862 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002863 return rtnl_group_changelink(skb, net,
Patrick McHardyffa934f2011-01-20 03:00:42 +00002864 nla_get_u32(tb[IFLA_GROUP]),
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002865 ifm, extack, tb);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002866 return -ENODEV;
Patrick McHardyffa934f2011-01-20 03:00:42 +00002867 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002868
Theuns Verwoerd160ca012017-01-31 12:23:46 +13002869 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
Patrick McHardy38f7b872007-06-13 12:03:51 -07002870 return -EOPNOTSUPP;
2871
2872 if (!ops) {
Johannes Berg95a5afc2008-10-16 15:24:51 -07002873#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002874 if (kind[0]) {
2875 __rtnl_unlock();
2876 request_module("rtnl-link-%s", kind);
2877 rtnl_lock();
2878 ops = rtnl_link_ops_get(kind);
2879 if (ops)
2880 goto replay;
2881 }
2882#endif
2883 return -EOPNOTSUPP;
2884 }
2885
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02002886 if (!ops->setup)
2887 return -EOPNOTSUPP;
2888
Tom Gundersen55177502014-07-14 16:37:25 +02002889 if (!ifname[0]) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07002890 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
Tom Gundersen55177502014-07-14 16:37:25 +02002891 name_assign_type = NET_NAME_ENUM;
2892 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002893
Eric W. Biederman81adee42009-11-08 00:53:51 -08002894 dest_net = rtnl_link_get_net(net, tb);
Eric W. Biederman13ad1772011-01-29 14:57:22 +00002895 if (IS_ERR(dest_net))
2896 return PTR_ERR(dest_net);
2897
Eric W. Biederman505ce412015-02-26 16:19:00 -06002898 err = -EPERM;
2899 if (!netlink_ns_capable(skb, dest_net->user_ns, CAP_NET_ADMIN))
2900 goto out;
2901
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002902 if (tb[IFLA_LINK_NETNSID]) {
2903 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
2904
2905 link_net = get_net_ns_by_id(dest_net, id);
2906 if (!link_net) {
2907 err = -EINVAL;
2908 goto out;
2909 }
Eric W. Biederman06615be2015-02-26 16:20:07 -06002910 err = -EPERM;
2911 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
2912 goto out;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002913 }
2914
2915 dev = rtnl_create_link(link_net ? : dest_net, ifname,
2916 name_assign_type, ops, tb);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002917 if (IS_ERR(dev)) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002918 err = PTR_ERR(dev);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002919 goto out;
2920 }
2921
2922 dev->ifindex = ifm->ifi_index;
2923
Cong Wang0e0eee22014-02-11 15:51:30 -08002924 if (ops->newlink) {
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02002925 err = ops->newlink(link_net ? : net, dev, tb, data,
2926 extack);
Cong Wang0e0eee22014-02-11 15:51:30 -08002927 /* Drivers should call free_netdev() in ->destructor
Cong Wange51fb152014-06-03 16:40:47 -07002928 * and unregister it on failure after registration
2929 * so that device could be finally freed in rtnl_unlock.
Cong Wang0e0eee22014-02-11 15:51:30 -08002930 */
Cong Wange51fb152014-06-03 16:40:47 -07002931 if (err < 0) {
2932 /* If device is not registered at all, free it now */
2933 if (dev->reg_state == NETREG_UNINITIALIZED)
2934 free_netdev(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002935 goto out;
Cong Wange51fb152014-06-03 16:40:47 -07002936 }
Cong Wang0e0eee22014-02-11 15:51:30 -08002937 } else {
Patrick McHardy2d85cba2007-07-11 19:42:13 -07002938 err = register_netdevice(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002939 if (err < 0) {
2940 free_netdev(dev);
2941 goto out;
2942 }
Dan Carpenterfce9b9b2013-08-14 12:35:42 +03002943 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002944 err = rtnl_configure_link(dev, ifm);
David S. Miller43638902015-03-10 21:58:32 -04002945 if (err < 0)
2946 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002947 if (link_net) {
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002948 err = dev_change_net_namespace(dev, dest_net, ifname);
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002949 if (err < 0)
David S. Miller43638902015-03-10 21:58:32 -04002950 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002951 }
Theuns Verwoerd160ca012017-01-31 12:23:46 +13002952 if (tb[IFLA_MASTER]) {
David Ahern33eaf2a2017-10-04 17:48:46 -07002953 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]),
2954 extack);
Theuns Verwoerd160ca012017-01-31 12:23:46 +13002955 if (err)
2956 goto out_unregister;
2957 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002958out:
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002959 if (link_net)
2960 put_net(link_net);
Eric W. Biederman81adee42009-11-08 00:53:51 -08002961 put_net(dest_net);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002962 return err;
David S. Miller43638902015-03-10 21:58:32 -04002963out_unregister:
2964 if (ops->newlink) {
2965 LIST_HEAD(list_kill);
2966
2967 ops->dellink(dev, &list_kill);
2968 unregister_netdevice_many(&list_kill);
2969 } else {
2970 unregister_netdevice(dev);
2971 }
2972 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002973 }
2974}
2975
David Ahernc21ef3e2017-04-16 09:48:24 -07002976static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2977 struct netlink_ext_ack *extack)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002978{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002979 struct net *net = sock_net(skb->sk);
Jiri Benc79e1ad12017-11-02 17:04:38 -02002980 struct net *tgt_net = net;
Thomas Grafb60c5112006-08-04 23:05:34 -07002981 struct ifinfomsg *ifm;
Eric Dumazeta3d12892009-10-21 10:59:31 +00002982 char ifname[IFNAMSIZ];
Thomas Grafb60c5112006-08-04 23:05:34 -07002983 struct nlattr *tb[IFLA_MAX+1];
2984 struct net_device *dev = NULL;
2985 struct sk_buff *nskb;
Jiri Benc79e1ad12017-11-02 17:04:38 -02002986 int netnsid = -1;
Thomas Graf339bf982006-11-10 14:10:15 -08002987 int err;
Greg Rose115c9b82012-02-21 16:54:48 -05002988 u32 ext_filter_mask = 0;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002989
David Ahernc21ef3e2017-04-16 09:48:24 -07002990 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Thomas Grafb60c5112006-08-04 23:05:34 -07002991 if (err < 0)
Eric Sesterhenn9918f232006-09-26 23:26:38 -07002992 return err;
Thomas Grafb60c5112006-08-04 23:05:34 -07002993
Jiri Benc79e1ad12017-11-02 17:04:38 -02002994 if (tb[IFLA_IF_NETNSID]) {
2995 netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]);
2996 tgt_net = get_target_net(skb, netnsid);
2997 if (IS_ERR(tgt_net))
2998 return PTR_ERR(tgt_net);
2999 }
3000
Eric Dumazeta3d12892009-10-21 10:59:31 +00003001 if (tb[IFLA_IFNAME])
3002 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
3003
Greg Rose115c9b82012-02-21 16:54:48 -05003004 if (tb[IFLA_EXT_MASK])
3005 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3006
Jiri Benc79e1ad12017-11-02 17:04:38 -02003007 err = -EINVAL;
Thomas Grafb60c5112006-08-04 23:05:34 -07003008 ifm = nlmsg_data(nlh);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003009 if (ifm->ifi_index > 0)
Jiri Benc79e1ad12017-11-02 17:04:38 -02003010 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003011 else if (tb[IFLA_IFNAME])
Jiri Benc79e1ad12017-11-02 17:04:38 -02003012 dev = __dev_get_by_name(tgt_net, ifname);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003013 else
Jiri Benc79e1ad12017-11-02 17:04:38 -02003014 goto out;
Thomas Grafb60c5112006-08-04 23:05:34 -07003015
Jiri Benc79e1ad12017-11-02 17:04:38 -02003016 err = -ENODEV;
Eric Dumazeta3d12892009-10-21 10:59:31 +00003017 if (dev == NULL)
Jiri Benc79e1ad12017-11-02 17:04:38 -02003018 goto out;
Eric Dumazeta3d12892009-10-21 10:59:31 +00003019
Jiri Benc79e1ad12017-11-02 17:04:38 -02003020 err = -ENOBUFS;
Greg Rose115c9b82012-02-21 16:54:48 -05003021 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003022 if (nskb == NULL)
Jiri Benc79e1ad12017-11-02 17:04:38 -02003023 goto out;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003024
Jiri Benc79e1ad12017-11-02 17:04:38 -02003025 err = rtnl_fill_ifinfo(nskb, dev, net,
3026 RTM_NEWLINK, NETLINK_CB(skb).portid,
3027 nlh->nlmsg_seq, 0, 0, ext_filter_mask,
3028 0, NULL, netnsid);
Patrick McHardy26932562007-01-31 23:16:40 -08003029 if (err < 0) {
3030 /* -EMSGSIZE implies BUG in if_nlmsg_size */
3031 WARN_ON(err == -EMSGSIZE);
3032 kfree_skb(nskb);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003033 } else
Eric W. Biederman15e47302012-09-07 20:12:54 +00003034 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
Jiri Benc79e1ad12017-11-02 17:04:38 -02003035out:
3036 if (netnsid >= 0)
3037 put_net(tgt_net);
Thomas Grafb60c5112006-08-04 23:05:34 -07003038
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003039 return err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003040}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003041
Greg Rose115c9b82012-02-21 16:54:48 -05003042static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
Greg Rosec7ac8672011-06-10 01:27:09 +00003043{
Greg Rose115c9b82012-02-21 16:54:48 -05003044 struct net *net = sock_net(skb->sk);
3045 struct net_device *dev;
3046 struct nlattr *tb[IFLA_MAX+1];
3047 u32 ext_filter_mask = 0;
3048 u16 min_ifinfo_dump_size = 0;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02003049 int hdrlen;
Greg Rose115c9b82012-02-21 16:54:48 -05003050
Michal Schmidte5eca6d2014-05-28 14:15:19 +02003051 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3052 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
3053 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
3054
Johannes Bergfceb6432017-04-12 14:34:07 +02003055 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00003056 if (tb[IFLA_EXT_MASK])
3057 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3058 }
Greg Rose115c9b82012-02-21 16:54:48 -05003059
3060 if (!ext_filter_mask)
3061 return NLMSG_GOODSIZE;
3062 /*
3063 * traverse the list of net devices and compute the minimum
3064 * buffer size based upon the filter mask.
3065 */
Florian Westphal6853dd42017-08-09 20:41:51 +02003066 rcu_read_lock();
3067 for_each_netdev_rcu(net, dev) {
Greg Rose115c9b82012-02-21 16:54:48 -05003068 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
3069 if_nlmsg_size(dev,
3070 ext_filter_mask));
3071 }
Florian Westphal6853dd42017-08-09 20:41:51 +02003072 rcu_read_unlock();
Greg Rose115c9b82012-02-21 16:54:48 -05003073
Zhang Shengju93af2052016-11-22 14:14:28 +08003074 return nlmsg_total_size(min_ifinfo_dump_size);
Greg Rosec7ac8672011-06-10 01:27:09 +00003075}
3076
Adrian Bunk42bad1d2007-04-26 00:57:41 -07003077static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078{
3079 int idx;
3080 int s_idx = cb->family;
3081
3082 if (s_idx == 0)
3083 s_idx = 1;
Florian Westphal6853dd42017-08-09 20:41:51 +02003084
Patrick McHardy25239ce2010-04-26 16:02:05 +02003085 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
Florian Westphaladdf9b92017-12-02 21:44:05 +01003086 struct rtnl_link **tab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 int type = cb->nlh->nlmsg_type-RTM_BASE;
Florian Westphaladdf9b92017-12-02 21:44:05 +01003088 struct rtnl_link *link;
Florian Westphal6853dd42017-08-09 20:41:51 +02003089 rtnl_dumpit_func dumpit;
3090
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 if (idx < s_idx || idx == PF_PACKET)
3092 continue;
Florian Westphal6853dd42017-08-09 20:41:51 +02003093
Florian Westphaladdf9b92017-12-02 21:44:05 +01003094 if (type < 0 || type >= RTM_NR_MSGTYPES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 continue;
Florian Westphal6853dd42017-08-09 20:41:51 +02003096
Florian Westphaladdf9b92017-12-02 21:44:05 +01003097 tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]);
3098 if (!tab)
3099 continue;
3100
3101 link = tab[type];
3102 if (!link)
3103 continue;
3104
3105 dumpit = link->dumpit;
Florian Westphal6853dd42017-08-09 20:41:51 +02003106 if (!dumpit)
3107 continue;
3108
Nicolas Dichtel04652772013-03-22 06:28:42 +00003109 if (idx > s_idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 memset(&cb->args[0], 0, sizeof(cb->args));
Nicolas Dichtel04652772013-03-22 06:28:42 +00003111 cb->prev_seq = 0;
3112 cb->seq = 0;
3113 }
Florian Westphal6853dd42017-08-09 20:41:51 +02003114 if (dumpit(skb, cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 break;
3116 }
3117 cb->family = idx;
3118
3119 return skb->len;
3120}
3121
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003122struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003123 unsigned int change,
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003124 u32 event, gfp_t flags, int *new_nsid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003126 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 struct sk_buff *skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003128 int err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00003129 size_t if_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130
Alexei Starovoitov7f294052013-10-23 16:02:42 -07003131 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003132 if (skb == NULL)
3133 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134
Jiri Benc79e1ad12017-11-02 17:04:38 -02003135 err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
3136 type, 0, 0, change, 0, 0, event,
3137 new_nsid, -1);
Patrick McHardy26932562007-01-31 23:16:40 -08003138 if (err < 0) {
3139 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
3140 WARN_ON(err == -EMSGSIZE);
3141 kfree_skb(skb);
3142 goto errout;
3143 }
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003144 return skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003145errout:
3146 if (err < 0)
Eric W. Biederman4b3da702007-11-19 22:27:40 -08003147 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003148 return NULL;
3149}
3150
3151void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
3152{
3153 struct net *net = dev_net(dev);
3154
3155 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
3156}
3157
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003158static void rtmsg_ifinfo_event(int type, struct net_device *dev,
3159 unsigned int change, u32 event,
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003160 gfp_t flags, int *new_nsid)
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003161{
3162 struct sk_buff *skb;
3163
Nicolas Dichteled2a80a2015-05-13 14:19:42 +02003164 if (dev->reg_state != NETREG_REGISTERED)
3165 return;
3166
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003167 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003168 if (skb)
3169 rtmsg_ifinfo_send(skb, dev, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170}
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003171
3172void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
3173 gfp_t flags)
3174{
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003175 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, NULL);
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003176}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003178void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
3179 gfp_t flags, int *new_nsid)
3180{
3181 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3182 new_nsid);
3183}
3184
John Fastabendd83b0602012-04-15 06:44:08 +00003185static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
3186 struct net_device *dev,
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003187 u8 *addr, u16 vid, u32 pid, u32 seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003188 int type, unsigned int flags,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003189 int nlflags, u16 ndm_state)
John Fastabendd83b0602012-04-15 06:44:08 +00003190{
3191 struct nlmsghdr *nlh;
3192 struct ndmsg *ndm;
3193
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003194 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
John Fastabendd83b0602012-04-15 06:44:08 +00003195 if (!nlh)
3196 return -EMSGSIZE;
3197
3198 ndm = nlmsg_data(nlh);
3199 ndm->ndm_family = AF_BRIDGE;
3200 ndm->ndm_pad1 = 0;
3201 ndm->ndm_pad2 = 0;
3202 ndm->ndm_flags = flags;
3203 ndm->ndm_type = 0;
3204 ndm->ndm_ifindex = dev->ifindex;
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003205 ndm->ndm_state = ndm_state;
John Fastabendd83b0602012-04-15 06:44:08 +00003206
3207 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
3208 goto nla_put_failure;
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003209 if (vid)
3210 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
3211 goto nla_put_failure;
John Fastabendd83b0602012-04-15 06:44:08 +00003212
Johannes Berg053c0952015-01-16 22:09:00 +01003213 nlmsg_end(skb, nlh);
3214 return 0;
John Fastabendd83b0602012-04-15 06:44:08 +00003215
3216nla_put_failure:
3217 nlmsg_cancel(skb, nlh);
3218 return -EMSGSIZE;
3219}
3220
John Fastabend3ff661c2012-04-15 06:44:14 +00003221static inline size_t rtnl_fdb_nlmsg_size(void)
3222{
Sabrina Dubrocaf82ef3e2016-11-18 15:50:39 +01003223 return NLMSG_ALIGN(sizeof(struct ndmsg)) +
3224 nla_total_size(ETH_ALEN) + /* NDA_LLADDR */
3225 nla_total_size(sizeof(u16)) + /* NDA_VLAN */
3226 0;
John Fastabend3ff661c2012-04-15 06:44:14 +00003227}
3228
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003229static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
3230 u16 ndm_state)
John Fastabend3ff661c2012-04-15 06:44:14 +00003231{
3232 struct net *net = dev_net(dev);
3233 struct sk_buff *skb;
3234 int err = -ENOBUFS;
3235
3236 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
3237 if (!skb)
3238 goto errout;
3239
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003240 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003241 0, 0, type, NTF_SELF, 0, ndm_state);
John Fastabend3ff661c2012-04-15 06:44:14 +00003242 if (err < 0) {
3243 kfree_skb(skb);
3244 goto errout;
3245 }
3246
3247 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
3248 return;
3249errout:
3250 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
3251}
3252
Vlad Yasevich090096b2013-03-06 15:39:42 +00003253/**
3254 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3255 */
3256int ndo_dflt_fdb_add(struct ndmsg *ndm,
3257 struct nlattr *tb[],
3258 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003259 const unsigned char *addr, u16 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003260 u16 flags)
3261{
3262 int err = -EINVAL;
3263
3264 /* If aging addresses are supported device will need to
3265 * implement its own handler for this.
3266 */
3267 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
3268 pr_info("%s: FDB only supports static addresses\n", dev->name);
3269 return err;
3270 }
3271
Or Gerlitz65891fe2014-12-14 18:19:05 +02003272 if (vid) {
3273 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
3274 return err;
3275 }
3276
Vlad Yasevich090096b2013-03-06 15:39:42 +00003277 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3278 err = dev_uc_add_excl(dev, addr);
3279 else if (is_multicast_ether_addr(addr))
3280 err = dev_mc_add_excl(dev, addr);
3281
3282 /* Only return duplicate errors if NLM_F_EXCL is set */
3283 if (err == -EEXIST && !(flags & NLM_F_EXCL))
3284 err = 0;
3285
3286 return err;
3287}
3288EXPORT_SYMBOL(ndo_dflt_fdb_add);
3289
Florian Westphalb88d12e2017-10-10 17:10:04 +02003290static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
3291 struct netlink_ext_ack *extack)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003292{
3293 u16 vid = 0;
3294
3295 if (vlan_attr) {
3296 if (nla_len(vlan_attr) != sizeof(u16)) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003297 NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
Jiri Pirkof6f64242014-11-28 14:34:15 +01003298 return -EINVAL;
3299 }
3300
3301 vid = nla_get_u16(vlan_attr);
3302
3303 if (!vid || vid >= VLAN_VID_MASK) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003304 NL_SET_ERR_MSG(extack, "invalid vlan id");
Jiri Pirkof6f64242014-11-28 14:34:15 +01003305 return -EINVAL;
3306 }
3307 }
3308 *p_vid = vid;
3309 return 0;
3310}
3311
David Ahernc21ef3e2017-04-16 09:48:24 -07003312static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
3313 struct netlink_ext_ack *extack)
John Fastabend77162022012-04-15 06:43:56 +00003314{
3315 struct net *net = sock_net(skb->sk);
John Fastabend77162022012-04-15 06:43:56 +00003316 struct ndmsg *ndm;
3317 struct nlattr *tb[NDA_MAX+1];
3318 struct net_device *dev;
3319 u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01003320 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00003321 int err;
3322
David Ahernc21ef3e2017-04-16 09:48:24 -07003323 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
John Fastabend77162022012-04-15 06:43:56 +00003324 if (err < 0)
3325 return err;
3326
3327 ndm = nlmsg_data(nlh);
3328 if (ndm->ndm_ifindex == 0) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003329 NL_SET_ERR_MSG(extack, "invalid ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003330 return -EINVAL;
3331 }
3332
3333 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3334 if (dev == NULL) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003335 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003336 return -ENODEV;
3337 }
3338
3339 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003340 NL_SET_ERR_MSG(extack, "invalid address");
John Fastabend77162022012-04-15 06:43:56 +00003341 return -EINVAL;
3342 }
3343
3344 addr = nla_data(tb[NDA_LLADDR]);
John Fastabend77162022012-04-15 06:43:56 +00003345
Florian Westphalb88d12e2017-10-10 17:10:04 +02003346 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
Jiri Pirkof6f64242014-11-28 14:34:15 +01003347 if (err)
3348 return err;
3349
John Fastabend77162022012-04-15 06:43:56 +00003350 err = -EOPNOTSUPP;
3351
3352 /* Support fdb on master device the net/bridge default case */
3353 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3354 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003355 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3356 const struct net_device_ops *ops = br_dev->netdev_ops;
3357
Jiri Pirkof6f64242014-11-28 14:34:15 +01003358 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
3359 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00003360 if (err)
3361 goto out;
3362 else
3363 ndm->ndm_flags &= ~NTF_MASTER;
3364 }
3365
3366 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00003367 if ((ndm->ndm_flags & NTF_SELF)) {
3368 if (dev->netdev_ops->ndo_fdb_add)
3369 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003370 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003371 nlh->nlmsg_flags);
3372 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01003373 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003374 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00003375
John Fastabend3ff661c2012-04-15 06:44:14 +00003376 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003377 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
3378 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00003379 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00003380 }
John Fastabend77162022012-04-15 06:43:56 +00003381 }
3382out:
3383 return err;
3384}
3385
Vlad Yasevich090096b2013-03-06 15:39:42 +00003386/**
3387 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3388 */
3389int ndo_dflt_fdb_del(struct ndmsg *ndm,
3390 struct nlattr *tb[],
3391 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003392 const unsigned char *addr, u16 vid)
Vlad Yasevich090096b2013-03-06 15:39:42 +00003393{
Alexander Duyckc8a89c42014-07-15 15:15:20 -07003394 int err = -EINVAL;
Vlad Yasevich090096b2013-03-06 15:39:42 +00003395
3396 /* If aging addresses are supported device will need to
3397 * implement its own handler for this.
3398 */
Sridhar Samudrala64535992013-08-08 15:19:48 -07003399 if (!(ndm->ndm_state & NUD_PERMANENT)) {
Vlad Yasevich090096b2013-03-06 15:39:42 +00003400 pr_info("%s: FDB only supports static addresses\n", dev->name);
Alexander Duyckc8a89c42014-07-15 15:15:20 -07003401 return err;
Vlad Yasevich090096b2013-03-06 15:39:42 +00003402 }
3403
3404 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3405 err = dev_uc_del(dev, addr);
3406 else if (is_multicast_ether_addr(addr))
3407 err = dev_mc_del(dev, addr);
Vlad Yasevich090096b2013-03-06 15:39:42 +00003408
3409 return err;
3410}
3411EXPORT_SYMBOL(ndo_dflt_fdb_del);
3412
David Ahernc21ef3e2017-04-16 09:48:24 -07003413static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
3414 struct netlink_ext_ack *extack)
John Fastabend77162022012-04-15 06:43:56 +00003415{
3416 struct net *net = sock_net(skb->sk);
3417 struct ndmsg *ndm;
Vlad Yasevich1690be62013-02-13 12:00:18 +00003418 struct nlattr *tb[NDA_MAX+1];
John Fastabend77162022012-04-15 06:43:56 +00003419 struct net_device *dev;
3420 int err = -EINVAL;
3421 __u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01003422 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00003423
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07003424 if (!netlink_capable(skb, CAP_NET_ADMIN))
Vlad Yasevich1690be62013-02-13 12:00:18 +00003425 return -EPERM;
3426
David Ahernc21ef3e2017-04-16 09:48:24 -07003427 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
Vlad Yasevich1690be62013-02-13 12:00:18 +00003428 if (err < 0)
3429 return err;
John Fastabend77162022012-04-15 06:43:56 +00003430
3431 ndm = nlmsg_data(nlh);
3432 if (ndm->ndm_ifindex == 0) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003433 NL_SET_ERR_MSG(extack, "invalid ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003434 return -EINVAL;
3435 }
3436
3437 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3438 if (dev == NULL) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003439 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003440 return -ENODEV;
3441 }
3442
Vlad Yasevich1690be62013-02-13 12:00:18 +00003443 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003444 NL_SET_ERR_MSG(extack, "invalid address");
John Fastabend77162022012-04-15 06:43:56 +00003445 return -EINVAL;
3446 }
3447
Vlad Yasevich1690be62013-02-13 12:00:18 +00003448 addr = nla_data(tb[NDA_LLADDR]);
Vlad Yasevich1690be62013-02-13 12:00:18 +00003449
Florian Westphalb88d12e2017-10-10 17:10:04 +02003450 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
Jiri Pirkof6f64242014-11-28 14:34:15 +01003451 if (err)
3452 return err;
3453
John Fastabend77162022012-04-15 06:43:56 +00003454 err = -EOPNOTSUPP;
3455
3456 /* Support fdb on master device the net/bridge default case */
3457 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3458 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003459 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3460 const struct net_device_ops *ops = br_dev->netdev_ops;
John Fastabend77162022012-04-15 06:43:56 +00003461
Jiri Pirko898e5062013-01-03 22:48:52 +00003462 if (ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003463 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00003464
3465 if (err)
3466 goto out;
3467 else
3468 ndm->ndm_flags &= ~NTF_MASTER;
3469 }
3470
3471 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00003472 if (ndm->ndm_flags & NTF_SELF) {
3473 if (dev->netdev_ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003474 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
3475 vid);
Vlad Yasevich090096b2013-03-06 15:39:42 +00003476 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01003477 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00003478
John Fastabend3ff661c2012-04-15 06:44:14 +00003479 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003480 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
3481 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00003482 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00003483 }
John Fastabend77162022012-04-15 06:43:56 +00003484 }
3485out:
3486 return err;
3487}
3488
John Fastabendd83b0602012-04-15 06:44:08 +00003489static int nlmsg_populate_fdb(struct sk_buff *skb,
3490 struct netlink_callback *cb,
3491 struct net_device *dev,
3492 int *idx,
3493 struct netdev_hw_addr_list *list)
3494{
3495 struct netdev_hw_addr *ha;
3496 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00003497 u32 portid, seq;
John Fastabendd83b0602012-04-15 06:44:08 +00003498
Eric W. Biederman15e47302012-09-07 20:12:54 +00003499 portid = NETLINK_CB(cb->skb).portid;
John Fastabendd83b0602012-04-15 06:44:08 +00003500 seq = cb->nlh->nlmsg_seq;
3501
3502 list_for_each_entry(ha, &list->list, list) {
Roopa Prabhud2976532016-08-30 21:56:45 -07003503 if (*idx < cb->args[2])
John Fastabendd83b0602012-04-15 06:44:08 +00003504 goto skip;
3505
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003506 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
John Fastabenda7a558f2012-11-01 16:23:10 +00003507 portid, seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003508 RTM_NEWNEIGH, NTF_SELF,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003509 NLM_F_MULTI, NUD_PERMANENT);
John Fastabendd83b0602012-04-15 06:44:08 +00003510 if (err < 0)
3511 return err;
3512skip:
3513 *idx += 1;
3514 }
3515 return 0;
3516}
3517
3518/**
Ben Hutchings2c530402012-07-10 10:55:09 +00003519 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
John Fastabendd83b0602012-04-15 06:44:08 +00003520 * @nlh: netlink message header
3521 * @dev: netdevice
3522 *
3523 * Default netdevice operation to dump the existing unicast address list.
John Fastabend91f3e7b2013-03-29 08:18:37 +00003524 * Returns number of addresses from list put in skb.
John Fastabendd83b0602012-04-15 06:44:08 +00003525 */
3526int ndo_dflt_fdb_dump(struct sk_buff *skb,
3527 struct netlink_callback *cb,
3528 struct net_device *dev,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04003529 struct net_device *filter_dev,
Roopa Prabhud2976532016-08-30 21:56:45 -07003530 int *idx)
John Fastabendd83b0602012-04-15 06:44:08 +00003531{
3532 int err;
3533
3534 netif_addr_lock_bh(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -07003535 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
John Fastabendd83b0602012-04-15 06:44:08 +00003536 if (err)
3537 goto out;
Zhang Shengju2934c9d2016-11-30 16:37:34 +08003538 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
John Fastabendd83b0602012-04-15 06:44:08 +00003539out:
3540 netif_addr_unlock_bh(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -07003541 return err;
John Fastabendd83b0602012-04-15 06:44:08 +00003542}
3543EXPORT_SYMBOL(ndo_dflt_fdb_dump);
3544
John Fastabend77162022012-04-15 06:43:56 +00003545static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
3546{
John Fastabend77162022012-04-15 06:43:56 +00003547 struct net_device *dev;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003548 struct nlattr *tb[IFLA_MAX+1];
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003549 struct net_device *br_dev = NULL;
3550 const struct net_device_ops *ops = NULL;
3551 const struct net_device_ops *cops = NULL;
3552 struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
3553 struct net *net = sock_net(skb->sk);
Roopa Prabhud2976532016-08-30 21:56:45 -07003554 struct hlist_head *head;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003555 int brport_idx = 0;
3556 int br_idx = 0;
Roopa Prabhud2976532016-08-30 21:56:45 -07003557 int h, s_h;
3558 int idx = 0, s_idx;
3559 int err = 0;
3560 int fidx = 0;
John Fastabend77162022012-04-15 06:43:56 +00003561
Alexander Potapenko0ff50e82017-05-23 13:20:28 +02003562 err = nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb,
3563 IFLA_MAX, ifla_policy, NULL);
3564 if (err < 0) {
3565 return -EINVAL;
3566 } else if (err == 0) {
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003567 if (tb[IFLA_MASTER])
3568 br_idx = nla_get_u32(tb[IFLA_MASTER]);
3569 }
John Fastabend77162022012-04-15 06:43:56 +00003570
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003571 brport_idx = ifm->ifi_index;
3572
3573 if (br_idx) {
3574 br_dev = __dev_get_by_index(net, br_idx);
3575 if (!br_dev)
3576 return -ENODEV;
3577
3578 ops = br_dev->netdev_ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003579 }
3580
Roopa Prabhud2976532016-08-30 21:56:45 -07003581 s_h = cb->args[0];
3582 s_idx = cb->args[1];
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003583
Roopa Prabhud2976532016-08-30 21:56:45 -07003584 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
3585 idx = 0;
3586 head = &net->dev_index_head[h];
3587 hlist_for_each_entry(dev, head, index_hlist) {
3588
3589 if (brport_idx && (dev->ifindex != brport_idx))
3590 continue;
3591
3592 if (!br_idx) { /* user did not specify a specific bridge */
3593 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3594 br_dev = netdev_master_upper_dev_get(dev);
3595 cops = br_dev->netdev_ops;
3596 }
3597 } else {
3598 if (dev != br_dev &&
3599 !(dev->priv_flags & IFF_BRIDGE_PORT))
3600 continue;
3601
3602 if (br_dev != netdev_master_upper_dev_get(dev) &&
3603 !(dev->priv_flags & IFF_EBRIDGE))
3604 continue;
3605 cops = ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003606 }
3607
Roopa Prabhud2976532016-08-30 21:56:45 -07003608 if (idx < s_idx)
3609 goto cont;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003610
Roopa Prabhud2976532016-08-30 21:56:45 -07003611 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3612 if (cops && cops->ndo_fdb_dump) {
3613 err = cops->ndo_fdb_dump(skb, cb,
3614 br_dev, dev,
3615 &fidx);
3616 if (err == -EMSGSIZE)
3617 goto out;
3618 }
3619 }
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003620
Roopa Prabhud2976532016-08-30 21:56:45 -07003621 if (dev->netdev_ops->ndo_fdb_dump)
3622 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
3623 dev, NULL,
3624 &fidx);
3625 else
3626 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
3627 &fidx);
3628 if (err == -EMSGSIZE)
3629 goto out;
3630
3631 cops = NULL;
3632
3633 /* reset fdb offset to 0 for rest of the interfaces */
3634 cb->args[2] = 0;
3635 fidx = 0;
3636cont:
3637 idx++;
John Fastabend77162022012-04-15 06:43:56 +00003638 }
John Fastabend77162022012-04-15 06:43:56 +00003639 }
John Fastabend77162022012-04-15 06:43:56 +00003640
Roopa Prabhud2976532016-08-30 21:56:45 -07003641out:
3642 cb->args[0] = h;
3643 cb->args[1] = idx;
3644 cb->args[2] = fidx;
3645
John Fastabend77162022012-04-15 06:43:56 +00003646 return skb->len;
3647}
3648
Scott Feldman2c3c0312014-11-28 14:34:25 +01003649static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
3650 unsigned int attrnum, unsigned int flag)
3651{
3652 if (mask & flag)
3653 return nla_put_u8(skb, attrnum, !!(flags & flag));
3654 return 0;
3655}
3656
John Fastabend815cccb2012-10-24 08:13:09 +00003657int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Scott Feldman2c3c0312014-11-28 14:34:25 +01003658 struct net_device *dev, u16 mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003659 u32 flags, u32 mask, int nlflags,
3660 u32 filter_mask,
3661 int (*vlan_fill)(struct sk_buff *skb,
3662 struct net_device *dev,
3663 u32 filter_mask))
John Fastabend815cccb2012-10-24 08:13:09 +00003664{
3665 struct nlmsghdr *nlh;
3666 struct ifinfomsg *ifm;
3667 struct nlattr *br_afspec;
Scott Feldman2c3c0312014-11-28 14:34:25 +01003668 struct nlattr *protinfo;
John Fastabend815cccb2012-10-24 08:13:09 +00003669 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Jiri Pirko898e5062013-01-03 22:48:52 +00003670 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003671 int err = 0;
John Fastabend815cccb2012-10-24 08:13:09 +00003672
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02003673 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
John Fastabend815cccb2012-10-24 08:13:09 +00003674 if (nlh == NULL)
3675 return -EMSGSIZE;
3676
3677 ifm = nlmsg_data(nlh);
3678 ifm->ifi_family = AF_BRIDGE;
3679 ifm->__ifi_pad = 0;
3680 ifm->ifi_type = dev->type;
3681 ifm->ifi_index = dev->ifindex;
3682 ifm->ifi_flags = dev_get_flags(dev);
3683 ifm->ifi_change = 0;
3684
3685
3686 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
3687 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
3688 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00003689 (br_dev &&
3690 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
John Fastabend815cccb2012-10-24 08:13:09 +00003691 (dev->addr_len &&
3692 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02003693 (dev->ifindex != dev_get_iflink(dev) &&
3694 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
John Fastabend815cccb2012-10-24 08:13:09 +00003695 goto nla_put_failure;
3696
3697 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
3698 if (!br_afspec)
3699 goto nla_put_failure;
3700
Roopa Prabhu1d460b92014-12-08 14:04:20 -08003701 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
John Fastabend815cccb2012-10-24 08:13:09 +00003702 nla_nest_cancel(skb, br_afspec);
3703 goto nla_put_failure;
3704 }
Roopa Prabhu1d460b92014-12-08 14:04:20 -08003705
3706 if (mode != BRIDGE_MODE_UNDEF) {
3707 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
3708 nla_nest_cancel(skb, br_afspec);
3709 goto nla_put_failure;
3710 }
3711 }
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003712 if (vlan_fill) {
3713 err = vlan_fill(skb, dev, filter_mask);
3714 if (err) {
3715 nla_nest_cancel(skb, br_afspec);
3716 goto nla_put_failure;
3717 }
3718 }
John Fastabend815cccb2012-10-24 08:13:09 +00003719 nla_nest_end(skb, br_afspec);
3720
Scott Feldman2c3c0312014-11-28 14:34:25 +01003721 protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
3722 if (!protinfo)
3723 goto nla_put_failure;
3724
3725 if (brport_nla_put_flag(skb, flags, mask,
3726 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
3727 brport_nla_put_flag(skb, flags, mask,
3728 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
3729 brport_nla_put_flag(skb, flags, mask,
3730 IFLA_BRPORT_FAST_LEAVE,
3731 BR_MULTICAST_FAST_LEAVE) ||
3732 brport_nla_put_flag(skb, flags, mask,
3733 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
3734 brport_nla_put_flag(skb, flags, mask,
3735 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
3736 brport_nla_put_flag(skb, flags, mask,
3737 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
3738 brport_nla_put_flag(skb, flags, mask,
3739 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
3740 brport_nla_put_flag(skb, flags, mask,
3741 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
3742 nla_nest_cancel(skb, protinfo);
3743 goto nla_put_failure;
3744 }
3745
3746 nla_nest_end(skb, protinfo);
3747
Johannes Berg053c0952015-01-16 22:09:00 +01003748 nlmsg_end(skb, nlh);
3749 return 0;
John Fastabend815cccb2012-10-24 08:13:09 +00003750nla_put_failure:
3751 nlmsg_cancel(skb, nlh);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003752 return err ? err : -EMSGSIZE;
John Fastabend815cccb2012-10-24 08:13:09 +00003753}
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003754EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
John Fastabend815cccb2012-10-24 08:13:09 +00003755
John Fastabende5a55a82012-10-24 08:12:57 +00003756static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
3757{
3758 struct net *net = sock_net(skb->sk);
3759 struct net_device *dev;
3760 int idx = 0;
3761 u32 portid = NETLINK_CB(cb->skb).portid;
3762 u32 seq = cb->nlh->nlmsg_seq;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00003763 u32 filter_mask = 0;
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003764 int err;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00003765
Thomas Grafaa68c202014-11-26 13:42:20 +01003766 if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
3767 struct nlattr *extfilt;
3768
3769 extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
3770 IFLA_EXT_MASK);
3771 if (extfilt) {
3772 if (nla_len(extfilt) < sizeof(filter_mask))
3773 return -EINVAL;
3774
3775 filter_mask = nla_get_u32(extfilt);
3776 }
3777 }
John Fastabende5a55a82012-10-24 08:12:57 +00003778
3779 rcu_read_lock();
3780 for_each_netdev_rcu(net, dev) {
3781 const struct net_device_ops *ops = dev->netdev_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00003782 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
John Fastabende5a55a82012-10-24 08:12:57 +00003783
Jiri Pirko898e5062013-01-03 22:48:52 +00003784 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003785 if (idx >= cb->args[0]) {
3786 err = br_dev->netdev_ops->ndo_bridge_getlink(
3787 skb, portid, seq, dev,
3788 filter_mask, NLM_F_MULTI);
David Ahernf6c57752017-05-15 23:19:17 -07003789 if (err < 0 && err != -EOPNOTSUPP) {
3790 if (likely(skb->len))
3791 break;
3792
3793 goto out_err;
3794 }
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003795 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00003796 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00003797 }
3798
3799 if (ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003800 if (idx >= cb->args[0]) {
3801 err = ops->ndo_bridge_getlink(skb, portid,
3802 seq, dev,
3803 filter_mask,
3804 NLM_F_MULTI);
David Ahernf6c57752017-05-15 23:19:17 -07003805 if (err < 0 && err != -EOPNOTSUPP) {
3806 if (likely(skb->len))
3807 break;
3808
3809 goto out_err;
3810 }
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003811 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00003812 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00003813 }
3814 }
David Ahernf6c57752017-05-15 23:19:17 -07003815 err = skb->len;
3816out_err:
John Fastabende5a55a82012-10-24 08:12:57 +00003817 rcu_read_unlock();
3818 cb->args[0] = idx;
3819
David Ahernf6c57752017-05-15 23:19:17 -07003820 return err;
John Fastabende5a55a82012-10-24 08:12:57 +00003821}
3822
John Fastabend2469ffd2012-10-24 08:13:03 +00003823static inline size_t bridge_nlmsg_size(void)
3824{
3825 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3826 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3827 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3828 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
3829 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
3830 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
3831 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
3832 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
3833 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
3834 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
3835 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
3836}
3837
Roopa Prabhu02dba432015-01-14 20:02:25 -08003838static int rtnl_bridge_notify(struct net_device *dev)
John Fastabend2469ffd2012-10-24 08:13:03 +00003839{
3840 struct net *net = dev_net(dev);
John Fastabend2469ffd2012-10-24 08:13:03 +00003841 struct sk_buff *skb;
3842 int err = -EOPNOTSUPP;
3843
Roopa Prabhu02dba432015-01-14 20:02:25 -08003844 if (!dev->netdev_ops->ndo_bridge_getlink)
3845 return 0;
3846
John Fastabend2469ffd2012-10-24 08:13:03 +00003847 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
3848 if (!skb) {
3849 err = -ENOMEM;
3850 goto errout;
3851 }
3852
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02003853 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
Roopa Prabhu02dba432015-01-14 20:02:25 -08003854 if (err < 0)
3855 goto errout;
John Fastabend2469ffd2012-10-24 08:13:03 +00003856
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08003857 if (!skb->len)
3858 goto errout;
3859
John Fastabend2469ffd2012-10-24 08:13:03 +00003860 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
3861 return 0;
3862errout:
3863 WARN_ON(err == -EMSGSIZE);
3864 kfree_skb(skb);
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08003865 if (err)
3866 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
John Fastabend2469ffd2012-10-24 08:13:03 +00003867 return err;
3868}
3869
David Ahernc21ef3e2017-04-16 09:48:24 -07003870static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3871 struct netlink_ext_ack *extack)
John Fastabende5a55a82012-10-24 08:12:57 +00003872{
3873 struct net *net = sock_net(skb->sk);
3874 struct ifinfomsg *ifm;
3875 struct net_device *dev;
John Fastabend2469ffd2012-10-24 08:13:03 +00003876 struct nlattr *br_spec, *attr = NULL;
3877 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02003878 u16 flags = 0;
John Fastabendc38e01b2012-11-02 16:32:36 +00003879 bool have_flags = false;
John Fastabende5a55a82012-10-24 08:12:57 +00003880
3881 if (nlmsg_len(nlh) < sizeof(*ifm))
3882 return -EINVAL;
3883
3884 ifm = nlmsg_data(nlh);
3885 if (ifm->ifi_family != AF_BRIDGE)
3886 return -EPFNOSUPPORT;
3887
3888 dev = __dev_get_by_index(net, ifm->ifi_index);
3889 if (!dev) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003890 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabende5a55a82012-10-24 08:12:57 +00003891 return -ENODEV;
3892 }
3893
John Fastabend2469ffd2012-10-24 08:13:03 +00003894 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3895 if (br_spec) {
3896 nla_for_each_nested(attr, br_spec, rem) {
3897 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01003898 if (nla_len(attr) < sizeof(flags))
3899 return -EINVAL;
3900
John Fastabendc38e01b2012-11-02 16:32:36 +00003901 have_flags = true;
John Fastabend2469ffd2012-10-24 08:13:03 +00003902 flags = nla_get_u16(attr);
3903 break;
3904 }
3905 }
3906 }
3907
3908 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003909 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3910
3911 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
John Fastabend2469ffd2012-10-24 08:13:03 +00003912 err = -EOPNOTSUPP;
3913 goto out;
3914 }
3915
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003916 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
John Fastabende5a55a82012-10-24 08:12:57 +00003917 if (err)
3918 goto out;
John Fastabend2469ffd2012-10-24 08:13:03 +00003919
3920 flags &= ~BRIDGE_FLAGS_MASTER;
John Fastabende5a55a82012-10-24 08:12:57 +00003921 }
3922
John Fastabend2469ffd2012-10-24 08:13:03 +00003923 if ((flags & BRIDGE_FLAGS_SELF)) {
3924 if (!dev->netdev_ops->ndo_bridge_setlink)
3925 err = -EOPNOTSUPP;
3926 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003927 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
3928 flags);
Roopa Prabhu02dba432015-01-14 20:02:25 -08003929 if (!err) {
John Fastabend2469ffd2012-10-24 08:13:03 +00003930 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08003931
3932 /* Generate event to notify upper layer of bridge
3933 * change
3934 */
3935 err = rtnl_bridge_notify(dev);
3936 }
John Fastabend2469ffd2012-10-24 08:13:03 +00003937 }
3938
John Fastabendc38e01b2012-11-02 16:32:36 +00003939 if (have_flags)
John Fastabend2469ffd2012-10-24 08:13:03 +00003940 memcpy(nla_data(attr), &flags, sizeof(flags));
John Fastabende5a55a82012-10-24 08:12:57 +00003941out:
3942 return err;
3943}
3944
David Ahernc21ef3e2017-04-16 09:48:24 -07003945static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
3946 struct netlink_ext_ack *extack)
Vlad Yasevich407af322013-02-13 12:00:12 +00003947{
3948 struct net *net = sock_net(skb->sk);
3949 struct ifinfomsg *ifm;
3950 struct net_device *dev;
3951 struct nlattr *br_spec, *attr = NULL;
3952 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02003953 u16 flags = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +00003954 bool have_flags = false;
3955
3956 if (nlmsg_len(nlh) < sizeof(*ifm))
3957 return -EINVAL;
3958
3959 ifm = nlmsg_data(nlh);
3960 if (ifm->ifi_family != AF_BRIDGE)
3961 return -EPFNOSUPPORT;
3962
3963 dev = __dev_get_by_index(net, ifm->ifi_index);
3964 if (!dev) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003965 NL_SET_ERR_MSG(extack, "unknown ifindex");
Vlad Yasevich407af322013-02-13 12:00:12 +00003966 return -ENODEV;
3967 }
3968
3969 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3970 if (br_spec) {
3971 nla_for_each_nested(attr, br_spec, rem) {
3972 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01003973 if (nla_len(attr) < sizeof(flags))
3974 return -EINVAL;
3975
Vlad Yasevich407af322013-02-13 12:00:12 +00003976 have_flags = true;
3977 flags = nla_get_u16(attr);
3978 break;
3979 }
3980 }
3981 }
3982
Vlad Yasevich407af322013-02-13 12:00:12 +00003983 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3984 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3985
3986 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
3987 err = -EOPNOTSUPP;
3988 goto out;
3989 }
3990
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003991 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00003992 if (err)
3993 goto out;
3994
3995 flags &= ~BRIDGE_FLAGS_MASTER;
3996 }
3997
3998 if ((flags & BRIDGE_FLAGS_SELF)) {
3999 if (!dev->netdev_ops->ndo_bridge_dellink)
4000 err = -EOPNOTSUPP;
4001 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08004002 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
4003 flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00004004
Roopa Prabhu02dba432015-01-14 20:02:25 -08004005 if (!err) {
Vlad Yasevich407af322013-02-13 12:00:12 +00004006 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08004007
4008 /* Generate event to notify upper layer of bridge
4009 * change
4010 */
4011 err = rtnl_bridge_notify(dev);
4012 }
Vlad Yasevich407af322013-02-13 12:00:12 +00004013 }
4014
4015 if (have_flags)
4016 memcpy(nla_data(attr), &flags, sizeof(flags));
Vlad Yasevich407af322013-02-13 12:00:12 +00004017out:
4018 return err;
4019}
4020
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004021static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
4022{
4023 return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
4024 (!idxattr || idxattr == attrid);
4025}
4026
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004027#define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
4028static int rtnl_get_offload_stats_attr_size(int attr_id)
4029{
4030 switch (attr_id) {
4031 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
4032 return sizeof(struct rtnl_link_stats64);
4033 }
4034
4035 return 0;
4036}
4037
4038static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
4039 int *prividx)
4040{
4041 struct nlattr *attr = NULL;
4042 int attr_id, size;
4043 void *attr_data;
4044 int err;
4045
4046 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
4047 dev->netdev_ops->ndo_get_offload_stats))
4048 return -ENODATA;
4049
4050 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
4051 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
4052 if (attr_id < *prividx)
4053 continue;
4054
4055 size = rtnl_get_offload_stats_attr_size(attr_id);
4056 if (!size)
4057 continue;
4058
Or Gerlitz3df5b3c2016-11-22 23:09:54 +02004059 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004060 continue;
4061
4062 attr = nla_reserve_64bit(skb, attr_id, size,
4063 IFLA_OFFLOAD_XSTATS_UNSPEC);
4064 if (!attr)
4065 goto nla_put_failure;
4066
4067 attr_data = nla_data(attr);
4068 memset(attr_data, 0, size);
4069 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
4070 attr_data);
4071 if (err)
4072 goto get_offload_stats_failure;
4073 }
4074
4075 if (!attr)
4076 return -ENODATA;
4077
4078 *prividx = 0;
4079 return 0;
4080
4081nla_put_failure:
4082 err = -EMSGSIZE;
4083get_offload_stats_failure:
4084 *prividx = attr_id;
4085 return err;
4086}
4087
4088static int rtnl_get_offload_stats_size(const struct net_device *dev)
4089{
4090 int nla_size = 0;
4091 int attr_id;
4092 int size;
4093
4094 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
4095 dev->netdev_ops->ndo_get_offload_stats))
4096 return 0;
4097
4098 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
4099 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
Or Gerlitz3df5b3c2016-11-22 23:09:54 +02004100 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004101 continue;
4102 size = rtnl_get_offload_stats_attr_size(attr_id);
4103 nla_size += nla_total_size_64bit(size);
4104 }
4105
4106 if (nla_size != 0)
4107 nla_size += nla_total_size(0);
4108
4109 return nla_size;
4110}
4111
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004112static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
4113 int type, u32 pid, u32 seq, u32 change,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004114 unsigned int flags, unsigned int filter_mask,
4115 int *idxattr, int *prividx)
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004116{
4117 struct if_stats_msg *ifsm;
4118 struct nlmsghdr *nlh;
4119 struct nlattr *attr;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004120 int s_prividx = *prividx;
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004121 int err;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004122
4123 ASSERT_RTNL();
4124
4125 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
4126 if (!nlh)
4127 return -EMSGSIZE;
4128
4129 ifsm = nlmsg_data(nlh);
Nikolay Aleksandrovce024f42017-10-03 13:20:48 +03004130 ifsm->family = PF_UNSPEC;
4131 ifsm->pad1 = 0;
4132 ifsm->pad2 = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004133 ifsm->ifindex = dev->ifindex;
4134 ifsm->filter_mask = filter_mask;
4135
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004136 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004137 struct rtnl_link_stats64 *sp;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004138
Nicolas Dichtel58414d32016-04-21 18:58:25 +02004139 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
4140 sizeof(struct rtnl_link_stats64),
4141 IFLA_STATS_UNSPEC);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004142 if (!attr)
4143 goto nla_put_failure;
4144
4145 sp = nla_data(attr);
4146 dev_get_stats(dev, sp);
4147 }
4148
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004149 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
4150 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
4151
4152 if (ops && ops->fill_linkxstats) {
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004153 *idxattr = IFLA_STATS_LINK_XSTATS;
4154 attr = nla_nest_start(skb,
4155 IFLA_STATS_LINK_XSTATS);
4156 if (!attr)
4157 goto nla_put_failure;
4158
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004159 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
4160 nla_nest_end(skb, attr);
4161 if (err)
4162 goto nla_put_failure;
4163 *idxattr = 0;
4164 }
4165 }
4166
4167 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
4168 *idxattr)) {
4169 const struct rtnl_link_ops *ops = NULL;
4170 const struct net_device *master;
4171
4172 master = netdev_master_upper_dev_get(dev);
4173 if (master)
4174 ops = master->rtnl_link_ops;
4175 if (ops && ops->fill_linkxstats) {
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004176 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
4177 attr = nla_nest_start(skb,
4178 IFLA_STATS_LINK_XSTATS_SLAVE);
4179 if (!attr)
4180 goto nla_put_failure;
4181
4182 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004183 nla_nest_end(skb, attr);
4184 if (err)
4185 goto nla_put_failure;
4186 *idxattr = 0;
4187 }
4188 }
4189
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004190 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
4191 *idxattr)) {
4192 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
4193 attr = nla_nest_start(skb, IFLA_STATS_LINK_OFFLOAD_XSTATS);
4194 if (!attr)
4195 goto nla_put_failure;
4196
4197 err = rtnl_get_offload_stats(skb, dev, prividx);
4198 if (err == -ENODATA)
4199 nla_nest_cancel(skb, attr);
4200 else
4201 nla_nest_end(skb, attr);
4202
4203 if (err && err != -ENODATA)
4204 goto nla_put_failure;
4205 *idxattr = 0;
4206 }
4207
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004208 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
4209 struct rtnl_af_ops *af_ops;
4210
4211 *idxattr = IFLA_STATS_AF_SPEC;
4212 attr = nla_nest_start(skb, IFLA_STATS_AF_SPEC);
4213 if (!attr)
4214 goto nla_put_failure;
4215
Florian Westphal5fa85a02017-10-16 15:44:36 +02004216 rcu_read_lock();
4217 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004218 if (af_ops->fill_stats_af) {
4219 struct nlattr *af;
4220 int err;
4221
4222 af = nla_nest_start(skb, af_ops->family);
Florian Westphal5fa85a02017-10-16 15:44:36 +02004223 if (!af) {
4224 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004225 goto nla_put_failure;
Florian Westphal5fa85a02017-10-16 15:44:36 +02004226 }
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004227 err = af_ops->fill_stats_af(skb, dev);
4228
Florian Westphal5fa85a02017-10-16 15:44:36 +02004229 if (err == -ENODATA) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004230 nla_nest_cancel(skb, af);
Florian Westphal5fa85a02017-10-16 15:44:36 +02004231 } else if (err < 0) {
4232 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004233 goto nla_put_failure;
Florian Westphal5fa85a02017-10-16 15:44:36 +02004234 }
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004235
4236 nla_nest_end(skb, af);
4237 }
4238 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02004239 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004240
4241 nla_nest_end(skb, attr);
4242
4243 *idxattr = 0;
4244 }
4245
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004246 nlmsg_end(skb, nlh);
4247
4248 return 0;
4249
4250nla_put_failure:
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004251 /* not a multi message or no progress mean a real error */
4252 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
4253 nlmsg_cancel(skb, nlh);
4254 else
4255 nlmsg_end(skb, nlh);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004256
4257 return -EMSGSIZE;
4258}
4259
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004260static size_t if_nlmsg_stats_size(const struct net_device *dev,
4261 u32 filter_mask)
4262{
4263 size_t size = 0;
4264
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004265 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004266 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
4267
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004268 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
4269 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004270 int attr = IFLA_STATS_LINK_XSTATS;
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004271
4272 if (ops && ops->get_linkxstats_size) {
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004273 size += nla_total_size(ops->get_linkxstats_size(dev,
4274 attr));
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004275 /* for IFLA_STATS_LINK_XSTATS */
4276 size += nla_total_size(0);
4277 }
4278 }
4279
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004280 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
4281 struct net_device *_dev = (struct net_device *)dev;
4282 const struct rtnl_link_ops *ops = NULL;
4283 const struct net_device *master;
4284
4285 /* netdev_master_upper_dev_get can't take const */
4286 master = netdev_master_upper_dev_get(_dev);
4287 if (master)
4288 ops = master->rtnl_link_ops;
4289 if (ops && ops->get_linkxstats_size) {
4290 int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
4291
4292 size += nla_total_size(ops->get_linkxstats_size(dev,
4293 attr));
4294 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
4295 size += nla_total_size(0);
4296 }
4297 }
4298
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004299 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
4300 size += rtnl_get_offload_stats_size(dev);
4301
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004302 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
4303 struct rtnl_af_ops *af_ops;
4304
4305 /* for IFLA_STATS_AF_SPEC */
4306 size += nla_total_size(0);
4307
Florian Westphal5fa85a02017-10-16 15:44:36 +02004308 rcu_read_lock();
4309 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004310 if (af_ops->get_stats_af_size) {
4311 size += nla_total_size(
4312 af_ops->get_stats_af_size(dev));
4313
4314 /* for AF_* */
4315 size += nla_total_size(0);
4316 }
4317 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02004318 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004319 }
4320
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004321 return size;
4322}
4323
David Ahernc21ef3e2017-04-16 09:48:24 -07004324static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
4325 struct netlink_ext_ack *extack)
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004326{
4327 struct net *net = sock_net(skb->sk);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004328 struct net_device *dev = NULL;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004329 int idxattr = 0, prividx = 0;
4330 struct if_stats_msg *ifsm;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004331 struct sk_buff *nskb;
4332 u32 filter_mask;
4333 int err;
4334
Mathias Krause4775cc12016-12-28 17:52:15 +01004335 if (nlmsg_len(nlh) < sizeof(*ifsm))
4336 return -EINVAL;
4337
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004338 ifsm = nlmsg_data(nlh);
4339 if (ifsm->ifindex > 0)
4340 dev = __dev_get_by_index(net, ifsm->ifindex);
4341 else
4342 return -EINVAL;
4343
4344 if (!dev)
4345 return -ENODEV;
4346
4347 filter_mask = ifsm->filter_mask;
4348 if (!filter_mask)
4349 return -EINVAL;
4350
4351 nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
4352 if (!nskb)
4353 return -ENOBUFS;
4354
4355 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
4356 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004357 0, filter_mask, &idxattr, &prividx);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004358 if (err < 0) {
4359 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
4360 WARN_ON(err == -EMSGSIZE);
4361 kfree_skb(nskb);
4362 } else {
4363 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
4364 }
4365
4366 return err;
4367}
4368
4369static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
4370{
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004371 int h, s_h, err, s_idx, s_idxattr, s_prividx;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004372 struct net *net = sock_net(skb->sk);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004373 unsigned int flags = NLM_F_MULTI;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004374 struct if_stats_msg *ifsm;
4375 struct hlist_head *head;
4376 struct net_device *dev;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004377 u32 filter_mask = 0;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004378 int idx = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004379
4380 s_h = cb->args[0];
4381 s_idx = cb->args[1];
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004382 s_idxattr = cb->args[2];
4383 s_prividx = cb->args[3];
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004384
4385 cb->seq = net->dev_base_seq;
4386
Mathias Krause4775cc12016-12-28 17:52:15 +01004387 if (nlmsg_len(cb->nlh) < sizeof(*ifsm))
4388 return -EINVAL;
4389
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004390 ifsm = nlmsg_data(cb->nlh);
4391 filter_mask = ifsm->filter_mask;
4392 if (!filter_mask)
4393 return -EINVAL;
4394
4395 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4396 idx = 0;
4397 head = &net->dev_index_head[h];
4398 hlist_for_each_entry(dev, head, index_hlist) {
4399 if (idx < s_idx)
4400 goto cont;
4401 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
4402 NETLINK_CB(cb->skb).portid,
4403 cb->nlh->nlmsg_seq, 0,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004404 flags, filter_mask,
4405 &s_idxattr, &s_prividx);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004406 /* If we ran out of room on the first message,
4407 * we're in trouble
4408 */
4409 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
4410
4411 if (err < 0)
4412 goto out;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004413 s_prividx = 0;
4414 s_idxattr = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004415 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
4416cont:
4417 idx++;
4418 }
4419 }
4420out:
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004421 cb->args[3] = s_prividx;
4422 cb->args[2] = s_idxattr;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004423 cb->args[1] = idx;
4424 cb->args[0] = h;
4425
4426 return skb->len;
4427}
4428
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429/* Process one rtnetlink message. */
4430
Johannes Berg2d4bc932017-04-12 14:34:04 +02004431static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
4432 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004434 struct net *net = sock_net(skb->sk);
Florian Westphaladdf9b92017-12-02 21:44:05 +01004435 struct rtnl_link *link;
Florian Westphale4202512017-12-02 21:44:06 +01004436 struct module *owner;
Florian Westphal6853dd42017-08-09 20:41:51 +02004437 int err = -EOPNOTSUPP;
Thomas Grafe2849862007-03-22 11:48:11 -07004438 rtnl_doit_func doit;
Florian Westphal62256f92017-08-09 20:41:52 +02004439 unsigned int flags;
Alexander Kuleshov617cfc72016-01-10 21:26:57 +06004440 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 int family;
4442 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 if (type > RTM_MAX)
Thomas Graf038890f2007-04-05 14:35:52 -07004446 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447
4448 type -= RTM_BASE;
4449
4450 /* All the messages must have at least 1 byte length */
Hong zhi guo573ce262013-03-27 06:47:04 +00004451 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 return 0;
4453
Hong zhi guo573ce262013-03-27 06:47:04 +00004454 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 kind = type&3;
4456
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07004457 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07004458 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459
Florian Westphal6853dd42017-08-09 20:41:51 +02004460 rcu_read_lock();
David S. Millerb8f3ab42011-01-18 12:40:38 -08004461 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004462 struct sock *rtnl;
Thomas Grafe2849862007-03-22 11:48:11 -07004463 rtnl_dumpit_func dumpit;
Greg Rosec7ac8672011-06-10 01:27:09 +00004464 u16 min_dump_alloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465
Florian Westphaladdf9b92017-12-02 21:44:05 +01004466 link = rtnl_get_link(family, type);
4467 if (!link || !link->dumpit) {
Florian Westphal6853dd42017-08-09 20:41:51 +02004468 family = PF_UNSPEC;
Florian Westphaladdf9b92017-12-02 21:44:05 +01004469 link = rtnl_get_link(family, type);
4470 if (!link || !link->dumpit)
Florian Westphal6853dd42017-08-09 20:41:51 +02004471 goto err_unlock;
4472 }
Florian Westphale4202512017-12-02 21:44:06 +01004473 owner = link->owner;
Florian Westphaladdf9b92017-12-02 21:44:05 +01004474 dumpit = link->dumpit;
Florian Westphale1fa6d22017-08-09 20:41:47 +02004475
Florian Westphal5c2bb9b2017-08-10 16:52:58 +02004476 if (type == RTM_GETLINK - RTM_BASE)
Florian Westphale1fa6d22017-08-09 20:41:47 +02004477 min_dump_alloc = rtnl_calcit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478
Florian Westphale4202512017-12-02 21:44:06 +01004479 err = 0;
4480 /* need to do this before rcu_read_unlock() */
4481 if (!try_module_get(owner))
4482 err = -EPROTONOSUPPORT;
4483
Florian Westphal6853dd42017-08-09 20:41:51 +02004484 rcu_read_unlock();
4485
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004486 rtnl = net->rtnl;
Florian Westphale4202512017-12-02 21:44:06 +01004487 if (err == 0) {
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00004488 struct netlink_dump_control c = {
4489 .dump = dumpit,
4490 .min_dump_alloc = min_dump_alloc,
Florian Westphale4202512017-12-02 21:44:06 +01004491 .module = owner,
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00004492 };
4493 err = netlink_dump_start(rtnl, skb, nlh, &c);
Florian Westphale4202512017-12-02 21:44:06 +01004494 /* netlink_dump_start() will keep a reference on
4495 * module if dump is still in progress.
4496 */
4497 module_put(owner);
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00004498 }
Eric Dumazet2907c352011-05-25 07:34:04 +00004499 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 }
4501
Florian Westphaladdf9b92017-12-02 21:44:05 +01004502 link = rtnl_get_link(family, type);
4503 if (!link || !link->doit) {
Florian Westphal8caa38b2017-08-10 16:53:01 +02004504 family = PF_UNSPEC;
Florian Westphaladdf9b92017-12-02 21:44:05 +01004505 link = rtnl_get_link(PF_UNSPEC, type);
4506 if (!link || !link->doit)
4507 goto out_unlock;
Florian Westphal8caa38b2017-08-10 16:53:01 +02004508 }
4509
Florian Westphale4202512017-12-02 21:44:06 +01004510 owner = link->owner;
4511 if (!try_module_get(owner)) {
4512 err = -EPROTONOSUPPORT;
4513 goto out_unlock;
4514 }
4515
Florian Westphaladdf9b92017-12-02 21:44:05 +01004516 flags = link->flags;
Florian Westphal62256f92017-08-09 20:41:52 +02004517 if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
Florian Westphaladdf9b92017-12-02 21:44:05 +01004518 doit = link->doit;
Florian Westphal62256f92017-08-09 20:41:52 +02004519 rcu_read_unlock();
4520 if (doit)
4521 err = doit(skb, nlh, extack);
Florian Westphale4202512017-12-02 21:44:06 +01004522 module_put(owner);
Florian Westphal62256f92017-08-09 20:41:52 +02004523 return err;
4524 }
Florian Westphal6853dd42017-08-09 20:41:51 +02004525 rcu_read_unlock();
4526
Florian Westphal0cc09022017-08-09 20:41:50 +02004527 rtnl_lock();
Florian Westphaladdf9b92017-12-02 21:44:05 +01004528 link = rtnl_get_link(family, type);
4529 if (link && link->doit)
4530 err = link->doit(skb, nlh, extack);
Florian Westphal0cc09022017-08-09 20:41:50 +02004531 rtnl_unlock();
Florian Westphaladdf9b92017-12-02 21:44:05 +01004532
Florian Westphale4202512017-12-02 21:44:06 +01004533 module_put(owner);
4534
Florian Westphaladdf9b92017-12-02 21:44:05 +01004535 return err;
4536
4537out_unlock:
4538 rcu_read_unlock();
Florian Westphal0cc09022017-08-09 20:41:50 +02004539 return err;
4540
4541err_unlock:
Florian Westphal6853dd42017-08-09 20:41:51 +02004542 rcu_read_unlock();
Florian Westphal0cc09022017-08-09 20:41:50 +02004543 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544}
4545
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07004546static void rtnetlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07004548 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549}
4550
Julien Gomes5f729ea2017-06-20 13:54:16 -07004551static int rtnetlink_bind(struct net *net, int group)
4552{
4553 switch (group) {
4554 case RTNLGRP_IPV4_MROUTE_R:
4555 case RTNLGRP_IPV6_MROUTE_R:
4556 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
4557 return -EPERM;
4558 break;
4559 }
4560 return 0;
4561}
4562
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
4564{
Jiri Pirko351638e2013-05-28 01:30:21 +00004565 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02004566
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 switch (event) {
Vlad Yasevich5138e862017-04-04 09:23:41 -04004568 case NETDEV_REBOOT:
Xin Long8a212582017-10-15 18:13:41 +08004569 case NETDEV_CHANGEMTU:
David Ahern37536542017-07-19 10:22:40 -07004570 case NETDEV_CHANGEADDR:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004571 case NETDEV_CHANGENAME:
4572 case NETDEV_FEAT_CHANGE:
4573 case NETDEV_BONDING_FAILOVER:
Xin Longe6e66592017-10-15 18:13:43 +08004574 case NETDEV_POST_TYPE_CHANGE:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004575 case NETDEV_NOTIFY_PEERS:
Xin Longdc709f32017-10-15 18:13:44 +08004576 case NETDEV_CHANGEUPPER:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004577 case NETDEV_RESEND_IGMP:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004578 case NETDEV_CHANGEINFODATA:
Xin Longeeda3fb2017-10-24 13:54:19 +08004579 case NETDEV_CHANGELOWERSTATE:
Xin Longebdcf042017-10-15 18:13:42 +08004580 case NETDEV_CHANGE_TX_QUEUE_LEN:
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04004581 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02004582 GFP_KERNEL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 break;
4584 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 break;
4586 }
4587 return NOTIFY_DONE;
4588}
4589
4590static struct notifier_block rtnetlink_dev_notifier = {
4591 .notifier_call = rtnetlink_event,
4592};
4593
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004594
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004595static int __net_init rtnetlink_net_init(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004596{
4597 struct sock *sk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00004598 struct netlink_kernel_cfg cfg = {
4599 .groups = RTNLGRP_MAX,
4600 .input = rtnetlink_rcv,
4601 .cb_mutex = &rtnl_mutex,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00004602 .flags = NL_CFG_F_NONROOT_RECV,
Julien Gomes5f729ea2017-06-20 13:54:16 -07004603 .bind = rtnetlink_bind,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00004604 };
4605
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00004606 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004607 if (!sk)
4608 return -ENOMEM;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004609 net->rtnl = sk;
4610 return 0;
4611}
4612
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004613static void __net_exit rtnetlink_net_exit(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004614{
Denis V. Lunev775516b2008-01-18 23:55:19 -08004615 netlink_kernel_release(net->rtnl);
4616 net->rtnl = NULL;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004617}
4618
4619static struct pernet_operations rtnetlink_net_ops = {
4620 .init = rtnetlink_net_init,
4621 .exit = rtnetlink_net_exit,
4622};
4623
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624void __init rtnetlink_init(void)
4625{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004626 if (register_pernet_subsys(&rtnetlink_net_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 panic("rtnetlink_init: cannot initialize rtnetlink\n");
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004628
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 register_netdevice_notifier(&rtnetlink_dev_notifier);
Thomas Graf340d17f2007-03-22 11:49:22 -07004630
Greg Rosec7ac8672011-06-10 01:27:09 +00004631 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
Florian Westphalb97bac62017-08-09 20:41:48 +02004632 rtnl_dump_ifinfo, 0);
4633 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
4634 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
4635 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
Thomas Graf687ad8c2007-03-22 11:59:42 -07004636
Florian Westphalb97bac62017-08-09 20:41:48 +02004637 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
4638 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
4639 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
John Fastabend77162022012-04-15 06:43:56 +00004640
Florian Westphalb97bac62017-08-09 20:41:48 +02004641 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
4642 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
4643 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, 0);
John Fastabende5a55a82012-10-24 08:12:57 +00004644
Florian Westphalb97bac62017-08-09 20:41:48 +02004645 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
4646 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
4647 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004648
4649 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
Florian Westphalb97bac62017-08-09 20:41:48 +02004650 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651}