blob: d0c02943c05a5543f4fa168dfbcff81d21d2eb98 [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 */
David Decotignyb2d3bcf2018-01-18 09:59:13 -0800993 + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */
994 + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */
Jiri Benc79e1ad12017-11-02 17:04:38 -0200995 + 0;
Thomas Graf339bf982006-11-10 14:10:15 -0800996}
997
Scott Feldman57b61082010-05-17 22:49:55 -0700998static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
999{
1000 struct nlattr *vf_ports;
1001 struct nlattr *vf_port;
1002 int vf;
1003 int err;
1004
1005 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
1006 if (!vf_ports)
1007 return -EMSGSIZE;
1008
1009 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
1010 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
Scott Feldman8ca94182010-05-28 03:42:18 -07001011 if (!vf_port)
1012 goto nla_put_failure;
David S. Millera6574342012-04-01 20:12:00 -04001013 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
1014 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001015 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
Scott Feldman8ca94182010-05-28 03:42:18 -07001016 if (err == -EMSGSIZE)
1017 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001018 if (err) {
Scott Feldman57b61082010-05-17 22:49:55 -07001019 nla_nest_cancel(skb, vf_port);
1020 continue;
1021 }
1022 nla_nest_end(skb, vf_port);
1023 }
1024
1025 nla_nest_end(skb, vf_ports);
1026
1027 return 0;
Scott Feldman8ca94182010-05-28 03:42:18 -07001028
1029nla_put_failure:
1030 nla_nest_cancel(skb, vf_ports);
1031 return -EMSGSIZE;
Scott Feldman57b61082010-05-17 22:49:55 -07001032}
1033
1034static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
1035{
1036 struct nlattr *port_self;
1037 int err;
1038
1039 port_self = nla_nest_start(skb, IFLA_PORT_SELF);
1040 if (!port_self)
1041 return -EMSGSIZE;
1042
1043 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
1044 if (err) {
1045 nla_nest_cancel(skb, port_self);
Scott Feldman8ca94182010-05-28 03:42:18 -07001046 return (err == -EMSGSIZE) ? err : 0;
Scott Feldman57b61082010-05-17 22:49:55 -07001047 }
1048
1049 nla_nest_end(skb, port_self);
1050
1051 return 0;
1052}
1053
David Gibsonc53864f2014-04-24 10:22:36 +10001054static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1055 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -07001056{
1057 int err;
1058
David Gibsonc53864f2014-04-24 10:22:36 +10001059 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1060 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -07001061 return 0;
1062
1063 err = rtnl_port_self_fill(skb, dev);
1064 if (err)
1065 return err;
1066
1067 if (dev_num_vf(dev->dev.parent)) {
1068 err = rtnl_vf_ports_fill(skb, dev);
1069 if (err)
1070 return err;
1071 }
1072
1073 return 0;
1074}
1075
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001076static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
1077{
1078 int err;
Jiri Pirko02637fc2014-11-28 14:34:16 +01001079 struct netdev_phys_item_id ppid;
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001080
1081 err = dev_get_phys_port_id(dev, &ppid);
1082 if (err) {
1083 if (err == -EOPNOTSUPP)
1084 return 0;
1085 return err;
1086 }
1087
1088 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1089 return -EMSGSIZE;
1090
1091 return 0;
1092}
1093
David Aherndb24a902015-03-17 20:23:15 -06001094static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1095{
1096 char name[IFNAMSIZ];
1097 int err;
1098
1099 err = dev_get_phys_port_name(dev, name, sizeof(name));
1100 if (err) {
1101 if (err == -EOPNOTSUPP)
1102 return 0;
1103 return err;
1104 }
1105
Michal Schmidt77ef0332017-05-04 16:48:58 +02001106 if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
David Aherndb24a902015-03-17 20:23:15 -06001107 return -EMSGSIZE;
1108
1109 return 0;
1110}
1111
Jiri Pirko82f28412014-11-28 14:34:18 +01001112static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1113{
1114 int err;
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001115 struct switchdev_attr attr = {
Ido Schimmel6ff64f62015-12-15 16:03:35 +01001116 .orig_dev = dev,
Jiri Pirko1f868392015-10-01 11:03:42 +02001117 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001118 .flags = SWITCHDEV_F_NO_RECURSE,
1119 };
Jiri Pirko82f28412014-11-28 14:34:18 +01001120
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001121 err = switchdev_port_attr_get(dev, &attr);
Jiri Pirko82f28412014-11-28 14:34:18 +01001122 if (err) {
1123 if (err == -EOPNOTSUPP)
1124 return 0;
1125 return err;
1126 }
1127
Scott Feldman42275bd2015-05-13 11:16:50 -07001128 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len,
1129 attr.u.ppid.id))
Jiri Pirko82f28412014-11-28 14:34:18 +01001130 return -EMSGSIZE;
1131
1132 return 0;
1133}
1134
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001135static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1136 struct net_device *dev)
1137{
Roopa Prabhu550bce52016-04-15 20:36:25 -07001138 struct rtnl_link_stats64 *sp;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001139 struct nlattr *attr;
1140
Nicolas Dichtel58414d32016-04-21 18:58:25 +02001141 attr = nla_reserve_64bit(skb, IFLA_STATS64,
1142 sizeof(struct rtnl_link_stats64), IFLA_PAD);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001143 if (!attr)
1144 return -EMSGSIZE;
1145
Roopa Prabhu550bce52016-04-15 20:36:25 -07001146 sp = nla_data(attr);
1147 dev_get_stats(dev, sp);
1148
1149 attr = nla_reserve(skb, IFLA_STATS,
1150 sizeof(struct rtnl_link_stats));
1151 if (!attr)
1152 return -EMSGSIZE;
1153
1154 copy_rtnl_link_stats(nla_data(attr), sp);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001155
1156 return 0;
1157}
1158
1159static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1160 struct net_device *dev,
1161 int vfs_num,
1162 struct nlattr *vfinfo)
1163{
1164 struct ifla_vf_rss_query_en vf_rss_query_en;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001165 struct nlattr *vf, *vfstats, *vfvlanlist;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001166 struct ifla_vf_link_state vf_linkstate;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001167 struct ifla_vf_vlan_info vf_vlan_info;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001168 struct ifla_vf_spoofchk vf_spoofchk;
1169 struct ifla_vf_tx_rate vf_tx_rate;
1170 struct ifla_vf_stats vf_stats;
1171 struct ifla_vf_trust vf_trust;
1172 struct ifla_vf_vlan vf_vlan;
1173 struct ifla_vf_rate vf_rate;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001174 struct ifla_vf_mac vf_mac;
1175 struct ifla_vf_info ivi;
1176
Mintz, Yuval0eed9cf2017-06-07 21:00:33 +03001177 memset(&ivi, 0, sizeof(ivi));
1178
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001179 /* Not all SR-IOV capable drivers support the
1180 * spoofcheck and "RSS query enable" query. Preset to
1181 * -1 so the user space tool can detect that the driver
1182 * didn't report anything.
1183 */
1184 ivi.spoofchk = -1;
1185 ivi.rss_query_en = -1;
1186 ivi.trusted = -1;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001187 /* The default value for VF link state is "auto"
1188 * IFLA_VF_LINK_STATE_AUTO which equals zero
1189 */
1190 ivi.linkstate = 0;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001191 /* VLAN Protocol by default is 802.1Q */
1192 ivi.vlan_proto = htons(ETH_P_8021Q);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001193 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1194 return 0;
1195
Dan Carpenter775f4f02016-10-13 11:45:28 +03001196 memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
1197
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001198 vf_mac.vf =
1199 vf_vlan.vf =
Moshe Shemesh79aab092016-09-22 12:11:15 +03001200 vf_vlan_info.vf =
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001201 vf_rate.vf =
1202 vf_tx_rate.vf =
1203 vf_spoofchk.vf =
1204 vf_linkstate.vf =
1205 vf_rss_query_en.vf =
1206 vf_trust.vf = ivi.vf;
1207
1208 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1209 vf_vlan.vlan = ivi.vlan;
1210 vf_vlan.qos = ivi.qos;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001211 vf_vlan_info.vlan = ivi.vlan;
1212 vf_vlan_info.qos = ivi.qos;
1213 vf_vlan_info.vlan_proto = ivi.vlan_proto;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001214 vf_tx_rate.rate = ivi.max_tx_rate;
1215 vf_rate.min_tx_rate = ivi.min_tx_rate;
1216 vf_rate.max_tx_rate = ivi.max_tx_rate;
1217 vf_spoofchk.setting = ivi.spoofchk;
1218 vf_linkstate.link_state = ivi.linkstate;
1219 vf_rss_query_en.setting = ivi.rss_query_en;
1220 vf_trust.setting = ivi.trusted;
1221 vf = nla_nest_start(skb, IFLA_VF_INFO);
Moshe Shemesh79aab092016-09-22 12:11:15 +03001222 if (!vf)
1223 goto nla_put_vfinfo_failure;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001224 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1225 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1226 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1227 &vf_rate) ||
1228 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1229 &vf_tx_rate) ||
1230 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1231 &vf_spoofchk) ||
1232 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1233 &vf_linkstate) ||
1234 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1235 sizeof(vf_rss_query_en),
1236 &vf_rss_query_en) ||
1237 nla_put(skb, IFLA_VF_TRUST,
1238 sizeof(vf_trust), &vf_trust))
Moshe Shemesh79aab092016-09-22 12:11:15 +03001239 goto nla_put_vf_failure;
1240 vfvlanlist = nla_nest_start(skb, IFLA_VF_VLAN_LIST);
1241 if (!vfvlanlist)
1242 goto nla_put_vf_failure;
1243 if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
1244 &vf_vlan_info)) {
1245 nla_nest_cancel(skb, vfvlanlist);
1246 goto nla_put_vf_failure;
1247 }
1248 nla_nest_end(skb, vfvlanlist);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001249 memset(&vf_stats, 0, sizeof(vf_stats));
1250 if (dev->netdev_ops->ndo_get_vf_stats)
1251 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1252 &vf_stats);
1253 vfstats = nla_nest_start(skb, IFLA_VF_STATS);
Moshe Shemesh79aab092016-09-22 12:11:15 +03001254 if (!vfstats)
1255 goto nla_put_vf_failure;
Nicolas Dichtel343a6d82016-04-25 10:25:14 +02001256 if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1257 vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1258 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1259 vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1260 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1261 vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1262 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1263 vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1264 nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1265 vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1266 nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
Eugenia Emantayevc5a9f6f2017-07-17 13:47:07 +03001267 vf_stats.multicast, IFLA_VF_STATS_PAD) ||
1268 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED,
1269 vf_stats.rx_dropped, IFLA_VF_STATS_PAD) ||
1270 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED,
1271 vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) {
Moshe Shemesh79aab092016-09-22 12:11:15 +03001272 nla_nest_cancel(skb, vfstats);
1273 goto nla_put_vf_failure;
1274 }
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001275 nla_nest_end(skb, vfstats);
1276 nla_nest_end(skb, vf);
1277 return 0;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001278
1279nla_put_vf_failure:
1280 nla_nest_cancel(skb, vf);
1281nla_put_vfinfo_failure:
1282 nla_nest_cancel(skb, vfinfo);
1283 return -EMSGSIZE;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001284}
1285
Florian Westphal250fc3d2017-09-26 13:58:41 +02001286static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb,
1287 struct net_device *dev,
1288 u32 ext_filter_mask)
1289{
1290 struct nlattr *vfinfo;
1291 int i, num_vfs;
1292
1293 if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0))
1294 return 0;
1295
1296 num_vfs = dev_num_vf(dev->dev.parent);
1297 if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs))
1298 return -EMSGSIZE;
1299
1300 if (!dev->netdev_ops->ndo_get_vf_config)
1301 return 0;
1302
1303 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1304 if (!vfinfo)
1305 return -EMSGSIZE;
1306
1307 for (i = 0; i < num_vfs; i++) {
1308 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1309 return -EMSGSIZE;
1310 }
1311
1312 nla_nest_end(skb, vfinfo);
1313 return 0;
1314}
1315
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001316static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1317{
Kangjie Lu5f8e4472016-05-03 16:46:24 -04001318 struct rtnl_link_ifmap map;
1319
1320 memset(&map, 0, sizeof(map));
1321 map.mem_start = dev->mem_start;
1322 map.mem_end = dev->mem_end;
1323 map.base_addr = dev->base_addr;
1324 map.irq = dev->irq;
1325 map.dma = dev->dma;
1326 map.port = dev->if_port;
1327
Nicolas Dichtel270cb4d2016-04-26 10:06:16 +02001328 if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001329 return -EMSGSIZE;
1330
1331 return 0;
1332}
1333
Martin KaFai Lau58038692017-06-15 17:29:09 -07001334static u8 rtnl_xdp_attached_mode(struct net_device *dev, u32 *prog_id)
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001335{
1336 const struct net_device_ops *ops = dev->netdev_ops;
Martin KaFai Lau58038692017-06-15 17:29:09 -07001337 const struct bpf_prog *generic_xdp_prog;
Jakub Kicinski118b4aa2017-12-01 15:08:55 -08001338 struct netdev_bpf xdp;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001339
1340 ASSERT_RTNL();
1341
Martin KaFai Lau58038692017-06-15 17:29:09 -07001342 *prog_id = 0;
1343 generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
1344 if (generic_xdp_prog) {
1345 *prog_id = generic_xdp_prog->aux->id;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001346 return XDP_ATTACHED_SKB;
Martin KaFai Lau58038692017-06-15 17:29:09 -07001347 }
Jakub Kicinskif4e63522017-11-03 13:56:16 -07001348 if (!ops->ndo_bpf)
Jakub Kicinskice158e52017-06-21 18:25:09 -07001349 return XDP_ATTACHED_NONE;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001350
Jakub Kicinski118b4aa2017-12-01 15:08:55 -08001351 __dev_xdp_query(dev, ops->ndo_bpf, &xdp);
1352 *prog_id = xdp.prog_id;
1353
1354 return xdp.prog_attached;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001355}
1356
Brenden Blancod1fdd912016-07-19 12:16:49 -07001357static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1358{
Brenden Blancod1fdd912016-07-19 12:16:49 -07001359 struct nlattr *xdp;
Martin KaFai Lau58038692017-06-15 17:29:09 -07001360 u32 prog_id;
Brenden Blancod1fdd912016-07-19 12:16:49 -07001361 int err;
1362
Brenden Blancod1fdd912016-07-19 12:16:49 -07001363 xdp = nla_nest_start(skb, IFLA_XDP);
1364 if (!xdp)
1365 return -EMSGSIZE;
David S. Millerb5cdae32017-04-18 15:36:58 -04001366
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001367 err = nla_put_u8(skb, IFLA_XDP_ATTACHED,
Martin KaFai Lau58038692017-06-15 17:29:09 -07001368 rtnl_xdp_attached_mode(dev, &prog_id));
Brenden Blancod1fdd912016-07-19 12:16:49 -07001369 if (err)
1370 goto err_cancel;
1371
Martin KaFai Lau58038692017-06-15 17:29:09 -07001372 if (prog_id) {
1373 err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
1374 if (err)
1375 goto err_cancel;
1376 }
1377
Brenden Blancod1fdd912016-07-19 12:16:49 -07001378 nla_nest_end(skb, xdp);
1379 return 0;
1380
1381err_cancel:
1382 nla_nest_cancel(skb, xdp);
1383 return err;
1384}
1385
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001386static u32 rtnl_get_event(unsigned long event)
1387{
1388 u32 rtnl_event_type = IFLA_EVENT_NONE;
1389
1390 switch (event) {
1391 case NETDEV_REBOOT:
1392 rtnl_event_type = IFLA_EVENT_REBOOT;
1393 break;
1394 case NETDEV_FEAT_CHANGE:
1395 rtnl_event_type = IFLA_EVENT_FEATURES;
1396 break;
1397 case NETDEV_BONDING_FAILOVER:
1398 rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
1399 break;
1400 case NETDEV_NOTIFY_PEERS:
1401 rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
1402 break;
1403 case NETDEV_RESEND_IGMP:
1404 rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
1405 break;
1406 case NETDEV_CHANGEINFODATA:
1407 rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
1408 break;
1409 default:
1410 break;
1411 }
1412
1413 return rtnl_event_type;
1414}
1415
Florian Westphal79110a02017-09-26 13:58:40 +02001416static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
1417{
1418 const struct net_device *upper_dev;
1419 int ret = 0;
1420
1421 rcu_read_lock();
1422
1423 upper_dev = netdev_master_upper_dev_get_rcu(dev);
1424 if (upper_dev)
1425 ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);
1426
1427 rcu_read_unlock();
1428 return ret;
1429}
1430
1431static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev)
1432{
1433 int ifindex = dev_get_iflink(dev);
1434
1435 if (dev->ifindex == ifindex)
1436 return 0;
1437
1438 return nla_put_u32(skb, IFLA_LINK, ifindex);
1439}
1440
Florian Westphal6c557002017-10-02 23:50:05 +02001441static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
1442 struct net_device *dev)
1443{
1444 char buf[IFALIASZ];
1445 int ret;
1446
1447 ret = dev_get_alias(dev, buf, sizeof(buf));
1448 return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
1449}
1450
Florian Westphalb1e66b92017-09-26 13:58:42 +02001451static int rtnl_fill_link_netnsid(struct sk_buff *skb,
Jiri Benc79e1ad12017-11-02 17:04:38 -02001452 const struct net_device *dev,
1453 struct net *src_net)
Florian Westphalb1e66b92017-09-26 13:58:42 +02001454{
1455 if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
1456 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1457
1458 if (!net_eq(dev_net(dev), link_net)) {
Jiri Benc79e1ad12017-11-02 17:04:38 -02001459 int id = peernet2id_alloc(src_net, link_net);
Florian Westphalb1e66b92017-09-26 13:58:42 +02001460
1461 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1462 return -EMSGSIZE;
1463 }
1464 }
1465
1466 return 0;
1467}
1468
Florian Westphal070cbf52017-10-16 15:44:35 +02001469static int rtnl_fill_link_af(struct sk_buff *skb,
1470 const struct net_device *dev,
1471 u32 ext_filter_mask)
1472{
1473 const struct rtnl_af_ops *af_ops;
1474 struct nlattr *af_spec;
1475
1476 af_spec = nla_nest_start(skb, IFLA_AF_SPEC);
1477 if (!af_spec)
1478 return -EMSGSIZE;
1479
Florian Westphal5fa85a02017-10-16 15:44:36 +02001480 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Florian Westphal070cbf52017-10-16 15:44:35 +02001481 struct nlattr *af;
1482 int err;
1483
1484 if (!af_ops->fill_link_af)
1485 continue;
1486
1487 af = nla_nest_start(skb, af_ops->family);
1488 if (!af)
1489 return -EMSGSIZE;
1490
1491 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1492 /*
1493 * Caller may return ENODATA to indicate that there
1494 * was no data to be dumped. This is not an error, it
1495 * means we should trim the attribute header and
1496 * continue.
1497 */
1498 if (err == -ENODATA)
1499 nla_nest_cancel(skb, af);
1500 else if (err < 0)
1501 return -EMSGSIZE;
1502
1503 nla_nest_end(skb, af);
1504 }
1505
1506 nla_nest_end(skb, af_spec);
1507 return 0;
1508}
1509
Jiri Benc79e1ad12017-11-02 17:04:38 -02001510static int rtnl_fill_ifinfo(struct sk_buff *skb,
1511 struct net_device *dev, struct net *src_net,
Patrick McHardy575c3e22007-05-22 17:00:49 -07001512 int type, u32 pid, u32 seq, u32 change,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001513 unsigned int flags, u32 ext_filter_mask,
Jiri Benc79e1ad12017-11-02 17:04:38 -02001514 u32 event, int *new_nsid, int tgt_netnsid)
Thomas Grafb60c5112006-08-04 23:05:34 -07001515{
1516 struct ifinfomsg *ifm;
1517 struct nlmsghdr *nlh;
1518
Eric Dumazet2907c352011-05-25 07:34:04 +00001519 ASSERT_RTNL();
Thomas Grafb60c5112006-08-04 23:05:34 -07001520 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1521 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08001522 return -EMSGSIZE;
Thomas Grafb60c5112006-08-04 23:05:34 -07001523
1524 ifm = nlmsg_data(nlh);
1525 ifm->ifi_family = AF_UNSPEC;
1526 ifm->__ifi_pad = 0;
1527 ifm->ifi_type = dev->type;
1528 ifm->ifi_index = dev->ifindex;
1529 ifm->ifi_flags = dev_get_flags(dev);
1530 ifm->ifi_change = change;
1531
Jiri Benc79e1ad12017-11-02 17:04:38 -02001532 if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_IF_NETNSID, tgt_netnsid))
1533 goto nla_put_failure;
1534
David S. Millera6574342012-04-01 20:12:00 -04001535 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1536 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1537 nla_put_u8(skb, IFLA_OPERSTATE,
1538 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1539 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1540 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1541 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
Ben Greearedbc0bb2012-03-29 12:51:30 +00001542 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001543 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
Eric Dumazetc70ce022016-03-21 09:55:10 -07001544 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1545 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001546#ifdef CONFIG_RPS
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001547 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001548#endif
Florian Westphal79110a02017-09-26 13:58:40 +02001549 nla_put_iflink(skb, dev) ||
1550 put_master_ifindex(skb, dev) ||
Jiri Pirko9a572472012-12-27 23:49:39 +00001551 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
David S. Millera6574342012-04-01 20:12:00 -04001552 (dev->qdisc &&
1553 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
Florian Westphal6c557002017-10-02 23:50:05 +02001554 nla_put_ifalias(skb, dev) ||
david decotigny2d3b4792014-03-29 09:48:35 -07001555 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
David Decotignyb2d3bcf2018-01-18 09:59:13 -08001556 atomic_read(&dev->carrier_up_count) +
1557 atomic_read(&dev->carrier_down_count)) ||
1558 nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down) ||
1559 nla_put_u32(skb, IFLA_CARRIER_UP_COUNT,
1560 atomic_read(&dev->carrier_up_count)) ||
1561 nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT,
1562 atomic_read(&dev->carrier_down_count)))
David S. Millera6574342012-04-01 20:12:00 -04001563 goto nla_put_failure;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001564
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001565 if (event != IFLA_EVENT_NONE) {
1566 if (nla_put_u32(skb, IFLA_EVENT, event))
1567 goto nla_put_failure;
1568 }
1569
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001570 if (rtnl_fill_link_ifmap(skb, dev))
1571 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
1573 if (dev->addr_len) {
David S. Millera6574342012-04-01 20:12:00 -04001574 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1575 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1576 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 }
1578
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001579 if (rtnl_phys_port_id_fill(skb, dev))
1580 goto nla_put_failure;
1581
David Aherndb24a902015-03-17 20:23:15 -06001582 if (rtnl_phys_port_name_fill(skb, dev))
1583 goto nla_put_failure;
1584
Jiri Pirko82f28412014-11-28 14:34:18 +01001585 if (rtnl_phys_switch_id_fill(skb, dev))
1586 goto nla_put_failure;
1587
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001588 if (rtnl_fill_stats(skb, dev))
Pavel Emelyanov96e74082008-05-21 14:12:46 -07001589 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Florian Westphal250fc3d2017-09-26 13:58:41 +02001591 if (rtnl_fill_vf(skb, dev, ext_filter_mask))
David S. Millera6574342012-04-01 20:12:00 -04001592 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001593
David Gibsonc53864f2014-04-24 10:22:36 +10001594 if (rtnl_port_fill(skb, dev, ext_filter_mask))
Scott Feldman57b61082010-05-17 22:49:55 -07001595 goto nla_put_failure;
1596
Brenden Blancod1fdd912016-07-19 12:16:49 -07001597 if (rtnl_xdp_fill(skb, dev))
1598 goto nla_put_failure;
1599
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001600 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07001601 if (rtnl_link_fill(skb, dev) < 0)
1602 goto nla_put_failure;
1603 }
1604
Jiri Benc79e1ad12017-11-02 17:04:38 -02001605 if (rtnl_fill_link_netnsid(skb, dev, src_net))
Florian Westphalb1e66b92017-09-26 13:58:42 +02001606 goto nla_put_failure;
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001607
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02001608 if (new_nsid &&
1609 nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0)
1610 goto nla_put_failure;
1611
Florian Westphal5fa85a02017-10-16 15:44:36 +02001612 rcu_read_lock();
Florian Westphal070cbf52017-10-16 15:44:35 +02001613 if (rtnl_fill_link_af(skb, dev, ext_filter_mask))
Florian Westphal5fa85a02017-10-16 15:44:36 +02001614 goto nla_put_failure_rcu;
1615 rcu_read_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +00001616
Johannes Berg053c0952015-01-16 22:09:00 +01001617 nlmsg_end(skb, nlh);
1618 return 0;
Thomas Grafb60c5112006-08-04 23:05:34 -07001619
Florian Westphal5fa85a02017-10-16 15:44:36 +02001620nla_put_failure_rcu:
1621 rcu_read_unlock();
Thomas Grafb60c5112006-08-04 23:05:34 -07001622nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001623 nlmsg_cancel(skb, nlh);
1624 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625}
1626
Jiri Pirkof7b12602014-02-18 20:53:18 +01001627static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1628 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1629 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1630 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1631 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1632 [IFLA_MTU] = { .type = NLA_U32 },
1633 [IFLA_LINK] = { .type = NLA_U32 },
1634 [IFLA_MASTER] = { .type = NLA_U32 },
1635 [IFLA_CARRIER] = { .type = NLA_U8 },
1636 [IFLA_TXQLEN] = { .type = NLA_U32 },
1637 [IFLA_WEIGHT] = { .type = NLA_U32 },
1638 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1639 [IFLA_LINKMODE] = { .type = NLA_U8 },
1640 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1641 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1642 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
Nicolas Dichtel2459b4c2017-10-11 16:24:48 +02001643 /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
1644 * allow 0-length string (needed to remove an alias).
1645 */
1646 [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001647 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1648 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1649 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1650 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1651 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1652 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1653 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1654 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
Stephen Hemminger46e6b992017-12-07 15:40:19 -08001655 [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 },
1656 [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 },
Jiri Pirko02637fc2014-11-28 14:34:16 +01001657 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
david decotigny2d3b4792014-03-29 09:48:35 -07001658 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
Jiri Pirko82f28412014-11-28 14:34:18 +01001659 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
Nicolas Dichtel317f4812015-01-15 15:11:18 +01001660 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001661 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001662 [IFLA_XDP] = { .type = NLA_NESTED },
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001663 [IFLA_EVENT] = { .type = NLA_U32 },
Serhey Popovychdb833d42017-06-20 14:35:23 +03001664 [IFLA_GROUP] = { .type = NLA_U32 },
Jiri Benc79e1ad12017-11-02 17:04:38 -02001665 [IFLA_IF_NETNSID] = { .type = NLA_S32 },
David Decotignyb2d3bcf2018-01-18 09:59:13 -08001666 [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 },
1667 [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001668};
1669
1670static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1671 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1672 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1673 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1674 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1675};
1676
Jiri Pirkof7b12602014-02-18 20:53:18 +01001677static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
Daniel Borkmann364d5712015-02-05 18:44:04 +01001678 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1679 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
Moshe Shemesh79aab092016-09-22 12:11:15 +03001680 [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED },
Daniel Borkmann364d5712015-02-05 18:44:04 +01001681 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1682 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1683 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1684 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001685 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001686 [IFLA_VF_STATS] = { .type = NLA_NESTED },
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00001687 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
Eli Cohencc8e27c2016-03-11 22:58:34 +02001688 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1689 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001690};
1691
Jiri Pirkof7b12602014-02-18 20:53:18 +01001692static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1693 [IFLA_PORT_VF] = { .type = NLA_U32 },
1694 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1695 .len = PORT_PROFILE_MAX },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001696 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1697 .len = PORT_UUID_MAX },
1698 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1699 .len = PORT_UUID_MAX },
1700 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1701 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
Daniel Borkmann025331d2017-02-17 01:56:11 +01001702
1703 /* Unused, but we need to keep it here since user space could
1704 * fill it. It's also broken with regard to NLA_BINARY use in
1705 * combination with structs.
1706 */
1707 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1708 .len = sizeof(struct ifla_port_vsi) },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001709};
1710
Brenden Blancod1fdd912016-07-19 12:16:49 -07001711static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
1712 [IFLA_XDP_FD] = { .type = NLA_S32 },
1713 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 },
Daniel Borkmann85de8572016-11-28 23:16:54 +01001714 [IFLA_XDP_FLAGS] = { .type = NLA_U32 },
Martin KaFai Lau58038692017-06-15 17:29:09 -07001715 [IFLA_XDP_PROG_ID] = { .type = NLA_U32 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001716};
1717
David Aherndc599f72016-02-02 08:17:07 -08001718static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1719{
1720 const struct rtnl_link_ops *ops = NULL;
1721 struct nlattr *linfo[IFLA_INFO_MAX + 1];
1722
Johannes Bergfceb6432017-04-12 14:34:07 +02001723 if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla,
1724 ifla_info_policy, NULL) < 0)
David Aherndc599f72016-02-02 08:17:07 -08001725 return NULL;
1726
1727 if (linfo[IFLA_INFO_KIND]) {
1728 char kind[MODULE_NAME_LEN];
1729
1730 nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1731 ops = rtnl_link_ops_get(kind);
1732 }
1733
1734 return ops;
1735}
1736
1737static bool link_master_filtered(struct net_device *dev, int master_idx)
1738{
1739 struct net_device *master;
1740
1741 if (!master_idx)
1742 return false;
1743
1744 master = netdev_master_upper_dev_get(dev);
1745 if (!master || master->ifindex != master_idx)
1746 return true;
1747
1748 return false;
1749}
1750
1751static bool link_kind_filtered(const struct net_device *dev,
1752 const struct rtnl_link_ops *kind_ops)
1753{
1754 if (kind_ops && dev->rtnl_link_ops != kind_ops)
1755 return true;
1756
1757 return false;
1758}
1759
1760static bool link_dump_filtered(struct net_device *dev,
1761 int master_idx,
1762 const struct rtnl_link_ops *kind_ops)
1763{
1764 if (link_master_filtered(dev, master_idx) ||
1765 link_kind_filtered(dev, kind_ops))
1766 return true;
1767
1768 return false;
1769}
1770
Andrei Vaginf428fe42018-01-02 23:27:33 -08001771static struct net *get_target_net(struct sock *sk, int netnsid)
Jiri Benc79e1ad12017-11-02 17:04:38 -02001772{
1773 struct net *net;
1774
Andrei Vaginf428fe42018-01-02 23:27:33 -08001775 net = get_net_ns_by_id(sock_net(sk), netnsid);
Jiri Benc79e1ad12017-11-02 17:04:38 -02001776 if (!net)
1777 return ERR_PTR(-EINVAL);
1778
1779 /* For now, the caller is required to have CAP_NET_ADMIN in
1780 * the user namespace owning the target net ns.
1781 */
Andrei Vaginf428fe42018-01-02 23:27:33 -08001782 if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) {
Jiri Benc79e1ad12017-11-02 17:04:38 -02001783 put_net(net);
1784 return ERR_PTR(-EACCES);
1785 }
1786 return net;
1787}
1788
Thomas Grafb60c5112006-08-04 23:05:34 -07001789static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001791 struct net *net = sock_net(skb->sk);
Jiri Benc79e1ad12017-11-02 17:04:38 -02001792 struct net *tgt_net = net;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001793 int h, s_h;
1794 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 struct net_device *dev;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001796 struct hlist_head *head;
Greg Rose115c9b82012-02-21 16:54:48 -05001797 struct nlattr *tb[IFLA_MAX+1];
1798 u32 ext_filter_mask = 0;
David Aherndc599f72016-02-02 08:17:07 -08001799 const struct rtnl_link_ops *kind_ops = NULL;
1800 unsigned int flags = NLM_F_MULTI;
1801 int master_idx = 0;
Jiri Benc79e1ad12017-11-02 17:04:38 -02001802 int netnsid = -1;
David Gibson973462b2014-04-24 10:22:35 +10001803 int err;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001804 int hdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001806 s_h = cb->args[0];
1807 s_idx = cb->args[1];
1808
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001809 /* A hack to preserve kernel<->userspace interface.
1810 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1811 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1812 * what iproute2 < v3.9.0 used.
1813 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1814 * attribute, its netlink message is shorter than struct ifinfomsg.
1815 */
1816 hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1817 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1818
Johannes Bergfceb6432017-04-12 14:34:07 +02001819 if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX,
1820 ifla_policy, NULL) >= 0) {
Jiri Benc79e1ad12017-11-02 17:04:38 -02001821 if (tb[IFLA_IF_NETNSID]) {
1822 netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]);
Andrei Vaginf428fe42018-01-02 23:27:33 -08001823 tgt_net = get_target_net(skb->sk, netnsid);
Jiri Benc79e1ad12017-11-02 17:04:38 -02001824 if (IS_ERR(tgt_net)) {
1825 tgt_net = net;
1826 netnsid = -1;
1827 }
1828 }
1829
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001830 if (tb[IFLA_EXT_MASK])
1831 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
David Aherndc599f72016-02-02 08:17:07 -08001832
1833 if (tb[IFLA_MASTER])
1834 master_idx = nla_get_u32(tb[IFLA_MASTER]);
1835
1836 if (tb[IFLA_LINKINFO])
1837 kind_ops = linkinfo_to_kind_ops(tb[IFLA_LINKINFO]);
1838
1839 if (master_idx || kind_ops)
1840 flags |= NLM_F_DUMP_FILTERED;
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001841 }
Greg Rose115c9b82012-02-21 16:54:48 -05001842
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001843 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1844 idx = 0;
Jiri Benc79e1ad12017-11-02 17:04:38 -02001845 head = &tgt_net->dev_index_head[h];
Eric Dumazetcac5e652015-02-27 09:42:50 -08001846 hlist_for_each_entry(dev, head, index_hlist) {
David Aherndc599f72016-02-02 08:17:07 -08001847 if (link_dump_filtered(dev, master_idx, kind_ops))
Zhang Shengju3f0ae052016-11-19 23:28:32 +08001848 goto cont;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001849 if (idx < s_idx)
1850 goto cont;
Jiri Benc79e1ad12017-11-02 17:04:38 -02001851 err = rtnl_fill_ifinfo(skb, dev, net,
1852 RTM_NEWLINK,
David Gibson973462b2014-04-24 10:22:35 +10001853 NETLINK_CB(cb->skb).portid,
1854 cb->nlh->nlmsg_seq, 0,
David Aherndc599f72016-02-02 08:17:07 -08001855 flags,
Jiri Benc79e1ad12017-11-02 17:04:38 -02001856 ext_filter_mask, 0, NULL,
1857 netnsid);
David Gibson973462b2014-04-24 10:22:35 +10001858
David Ahernf6c57752017-05-15 23:19:17 -07001859 if (err < 0) {
1860 if (likely(skb->len))
1861 goto out;
1862
1863 goto out_err;
1864 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07001865cont:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001866 idx++;
1867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 }
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001869out:
David Ahernf6c57752017-05-15 23:19:17 -07001870 err = skb->len;
1871out_err:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001872 cb->args[1] = idx;
1873 cb->args[0] = h;
Jakub Sitnickid0225782017-08-09 17:39:12 +02001874 cb->seq = net->dev_base_seq;
1875 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Jiri Benc79e1ad12017-11-02 17:04:38 -02001876 if (netnsid >= 0)
1877 put_net(tgt_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
David Ahernf6c57752017-05-15 23:19:17 -07001879 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880}
1881
Johannes Bergfceb6432017-04-12 14:34:07 +02001882int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
1883 struct netlink_ext_ack *exterr)
Jiri Pirkof7b12602014-02-18 20:53:18 +01001884{
Johannes Bergfceb6432017-04-12 14:34:07 +02001885 return nla_parse(tb, IFLA_MAX, head, len, ifla_policy, exterr);
Jiri Pirkof7b12602014-02-18 20:53:18 +01001886}
1887EXPORT_SYMBOL(rtnl_nla_parse_ifla);
Scott Feldman57b61082010-05-17 22:49:55 -07001888
Eric W. Biederman81adee42009-11-08 00:53:51 -08001889struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1890{
1891 struct net *net;
1892 /* Examine the link attributes and figure out which
1893 * network namespace we are talking about.
1894 */
1895 if (tb[IFLA_NET_NS_PID])
1896 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001897 else if (tb[IFLA_NET_NS_FD])
1898 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
Eric W. Biederman81adee42009-11-08 00:53:51 -08001899 else
1900 net = get_net(src_net);
1901 return net;
1902}
1903EXPORT_SYMBOL(rtnl_link_get_net);
1904
Christian Brauner7c4f63b2018-01-24 15:26:32 +01001905/* Figure out which network namespace we are talking about by
1906 * examining the link attributes in the following order:
1907 *
1908 * 1. IFLA_NET_NS_PID
1909 * 2. IFLA_NET_NS_FD
1910 * 3. IFLA_IF_NETNSID
1911 */
1912static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net,
1913 struct nlattr *tb[])
1914{
1915 struct net *net;
1916
1917 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])
1918 return rtnl_link_get_net(src_net, tb);
1919
1920 if (!tb[IFLA_IF_NETNSID])
1921 return get_net(src_net);
1922
1923 net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_IF_NETNSID]));
1924 if (!net)
1925 return ERR_PTR(-EINVAL);
1926
1927 return net;
1928}
1929
1930static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb,
1931 struct net *src_net,
1932 struct nlattr *tb[], int cap)
1933{
1934 struct net *net;
1935
1936 net = rtnl_link_get_net_by_nlattr(src_net, tb);
1937 if (IS_ERR(net))
1938 return net;
1939
1940 if (!netlink_ns_capable(skb, net->user_ns, cap)) {
1941 put_net(net);
1942 return ERR_PTR(-EPERM);
1943 }
1944
1945 return net;
1946}
1947
Thomas Graf1840bb12008-02-23 19:54:36 -08001948static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1949{
1950 if (dev) {
1951 if (tb[IFLA_ADDRESS] &&
1952 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1953 return -EINVAL;
1954
1955 if (tb[IFLA_BROADCAST] &&
1956 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1957 return -EINVAL;
1958 }
1959
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001960 if (tb[IFLA_AF_SPEC]) {
1961 struct nlattr *af;
1962 int rem, err;
1963
1964 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1965 const struct rtnl_af_ops *af_ops;
1966
Florian Westphal5fa85a02017-10-16 15:44:36 +02001967 rcu_read_lock();
1968 af_ops = rtnl_af_lookup(nla_type(af));
1969 if (!af_ops) {
1970 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001971 return -EAFNOSUPPORT;
Florian Westphal5fa85a02017-10-16 15:44:36 +02001972 }
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001973
Florian Westphal5fa85a02017-10-16 15:44:36 +02001974 if (!af_ops->set_link_af) {
1975 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001976 return -EOPNOTSUPP;
Florian Westphal5fa85a02017-10-16 15:44:36 +02001977 }
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001978
1979 if (af_ops->validate_link_af) {
Kurt Van Dijck6d3a9a62011-01-26 04:55:24 +00001980 err = af_ops->validate_link_af(dev, af);
Florian Westphal5fa85a02017-10-16 15:44:36 +02001981 if (err < 0) {
1982 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001983 return err;
Florian Westphal5fa85a02017-10-16 15:44:36 +02001984 }
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001985 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02001986
1987 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001988 }
1989 }
1990
Thomas Graf1840bb12008-02-23 19:54:36 -08001991 return 0;
1992}
1993
Eli Cohencc8e27c2016-03-11 22:58:34 +02001994static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
1995 int guid_type)
1996{
1997 const struct net_device_ops *ops = dev->netdev_ops;
1998
1999 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
2000}
2001
2002static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
2003{
2004 if (dev->type != ARPHRD_INFINIBAND)
2005 return -EOPNOTSUPP;
2006
2007 return handle_infiniband_guid(dev, ivt, guid_type);
2008}
2009
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002010static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
Chris Wrightc02db8c2010-05-16 01:05:45 -07002011{
Chris Wrightc02db8c2010-05-16 01:05:45 -07002012 const struct net_device_ops *ops = dev->netdev_ops;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002013 int err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002014
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002015 if (tb[IFLA_VF_MAC]) {
2016 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03002017
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002018 err = -EOPNOTSUPP;
2019 if (ops->ndo_set_vf_mac)
2020 err = ops->ndo_set_vf_mac(dev, ivm->vf,
2021 ivm->mac);
2022 if (err < 0)
2023 return err;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002024 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002025
2026 if (tb[IFLA_VF_VLAN]) {
2027 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
2028
2029 err = -EOPNOTSUPP;
2030 if (ops->ndo_set_vf_vlan)
2031 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
Moshe Shemesh79aab092016-09-22 12:11:15 +03002032 ivv->qos,
2033 htons(ETH_P_8021Q));
2034 if (err < 0)
2035 return err;
2036 }
2037
2038 if (tb[IFLA_VF_VLAN_LIST]) {
2039 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
2040 struct nlattr *attr;
2041 int rem, len = 0;
2042
2043 err = -EOPNOTSUPP;
2044 if (!ops->ndo_set_vf_vlan)
2045 return err;
2046
2047 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
2048 if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
2049 nla_len(attr) < NLA_HDRLEN) {
2050 return -EINVAL;
2051 }
2052 if (len >= MAX_VLAN_LIST_LEN)
2053 return -EOPNOTSUPP;
2054 ivvl[len] = nla_data(attr);
2055
2056 len++;
2057 }
Arnd Bergmannfa34cd92016-09-30 18:13:49 +02002058 if (len == 0)
2059 return -EINVAL;
2060
Moshe Shemesh79aab092016-09-22 12:11:15 +03002061 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
2062 ivvl[0]->qos, ivvl[0]->vlan_proto);
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002063 if (err < 0)
2064 return err;
2065 }
2066
2067 if (tb[IFLA_VF_TX_RATE]) {
2068 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
2069 struct ifla_vf_info ivf;
2070
2071 err = -EOPNOTSUPP;
2072 if (ops->ndo_get_vf_config)
2073 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
2074 if (err < 0)
2075 return err;
2076
2077 err = -EOPNOTSUPP;
2078 if (ops->ndo_set_vf_rate)
2079 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2080 ivf.min_tx_rate,
2081 ivt->rate);
2082 if (err < 0)
2083 return err;
2084 }
2085
2086 if (tb[IFLA_VF_RATE]) {
2087 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
2088
2089 err = -EOPNOTSUPP;
2090 if (ops->ndo_set_vf_rate)
2091 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2092 ivt->min_tx_rate,
2093 ivt->max_tx_rate);
2094 if (err < 0)
2095 return err;
2096 }
2097
2098 if (tb[IFLA_VF_SPOOFCHK]) {
2099 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
2100
2101 err = -EOPNOTSUPP;
2102 if (ops->ndo_set_vf_spoofchk)
2103 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
2104 ivs->setting);
2105 if (err < 0)
2106 return err;
2107 }
2108
2109 if (tb[IFLA_VF_LINK_STATE]) {
2110 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
2111
2112 err = -EOPNOTSUPP;
2113 if (ops->ndo_set_vf_link_state)
2114 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
2115 ivl->link_state);
2116 if (err < 0)
2117 return err;
2118 }
2119
2120 if (tb[IFLA_VF_RSS_QUERY_EN]) {
2121 struct ifla_vf_rss_query_en *ivrssq_en;
2122
2123 err = -EOPNOTSUPP;
2124 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
2125 if (ops->ndo_set_vf_rss_query_en)
2126 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
2127 ivrssq_en->setting);
2128 if (err < 0)
2129 return err;
2130 }
2131
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00002132 if (tb[IFLA_VF_TRUST]) {
2133 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
2134
2135 err = -EOPNOTSUPP;
2136 if (ops->ndo_set_vf_trust)
2137 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
2138 if (err < 0)
2139 return err;
2140 }
2141
Eli Cohencc8e27c2016-03-11 22:58:34 +02002142 if (tb[IFLA_VF_IB_NODE_GUID]) {
2143 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
2144
2145 if (!ops->ndo_set_vf_guid)
2146 return -EOPNOTSUPP;
2147
2148 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
2149 }
2150
2151 if (tb[IFLA_VF_IB_PORT_GUID]) {
2152 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
2153
2154 if (!ops->ndo_set_vf_guid)
2155 return -EOPNOTSUPP;
2156
2157 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
2158 }
2159
Chris Wrightc02db8c2010-05-16 01:05:45 -07002160 return err;
2161}
2162
David Ahern33eaf2a2017-10-04 17:48:46 -07002163static int do_set_master(struct net_device *dev, int ifindex,
2164 struct netlink_ext_ack *extack)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002165{
Jiri Pirko898e5062013-01-03 22:48:52 +00002166 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002167 const struct net_device_ops *ops;
2168 int err;
2169
Jiri Pirko898e5062013-01-03 22:48:52 +00002170 if (upper_dev) {
2171 if (upper_dev->ifindex == ifindex)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002172 return 0;
Jiri Pirko898e5062013-01-03 22:48:52 +00002173 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002174 if (ops->ndo_del_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002175 err = ops->ndo_del_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002176 if (err)
2177 return err;
2178 } else {
2179 return -EOPNOTSUPP;
2180 }
2181 }
2182
2183 if (ifindex) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002184 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
2185 if (!upper_dev)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002186 return -EINVAL;
Jiri Pirko898e5062013-01-03 22:48:52 +00002187 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002188 if (ops->ndo_add_slave) {
David Ahern33eaf2a2017-10-04 17:48:46 -07002189 err = ops->ndo_add_slave(upper_dev, dev, extack);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002190 if (err)
2191 return err;
2192 } else {
2193 return -EOPNOTSUPP;
2194 }
2195 }
2196 return 0;
2197}
2198
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002199#define DO_SETLINK_MODIFIED 0x01
Nicolas Dichtelba998902014-09-01 16:07:29 +02002200/* notify flag means notify + modified. */
2201#define DO_SETLINK_NOTIFY 0x03
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002202static int do_setlink(const struct sk_buff *skb,
2203 struct net_device *dev, struct ifinfomsg *ifm,
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002204 struct netlink_ext_ack *extack,
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002205 struct nlattr **tb, char *ifname, int status)
Patrick McHardy0157f602007-06-13 12:03:36 -07002206{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002207 const struct net_device_ops *ops = dev->netdev_ops;
Patrick McHardy0157f602007-06-13 12:03:36 -07002208 int err;
2209
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002210 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_IF_NETNSID]) {
2211 struct net *net = rtnl_link_get_net_capable(skb, dev_net(dev),
2212 tb, CAP_NET_ADMIN);
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002213 if (IS_ERR(net)) {
2214 err = PTR_ERR(net);
2215 goto errout;
2216 }
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002217
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002218 err = dev_change_net_namespace(dev, net, ifname);
2219 put_net(net);
2220 if (err)
2221 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002222 status |= DO_SETLINK_MODIFIED;
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002223 }
2224
Patrick McHardy0157f602007-06-13 12:03:36 -07002225 if (tb[IFLA_MAP]) {
2226 struct rtnl_link_ifmap *u_map;
2227 struct ifmap k_map;
2228
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002229 if (!ops->ndo_set_config) {
Patrick McHardy0157f602007-06-13 12:03:36 -07002230 err = -EOPNOTSUPP;
2231 goto errout;
2232 }
2233
2234 if (!netif_device_present(dev)) {
2235 err = -ENODEV;
2236 goto errout;
2237 }
2238
2239 u_map = nla_data(tb[IFLA_MAP]);
2240 k_map.mem_start = (unsigned long) u_map->mem_start;
2241 k_map.mem_end = (unsigned long) u_map->mem_end;
2242 k_map.base_addr = (unsigned short) u_map->base_addr;
2243 k_map.irq = (unsigned char) u_map->irq;
2244 k_map.dma = (unsigned char) u_map->dma;
2245 k_map.port = (unsigned char) u_map->port;
2246
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002247 err = ops->ndo_set_config(dev, &k_map);
Patrick McHardy0157f602007-06-13 12:03:36 -07002248 if (err < 0)
2249 goto errout;
2250
Nicolas Dichtelba998902014-09-01 16:07:29 +02002251 status |= DO_SETLINK_NOTIFY;
Patrick McHardy0157f602007-06-13 12:03:36 -07002252 }
2253
2254 if (tb[IFLA_ADDRESS]) {
2255 struct sockaddr *sa;
2256 int len;
2257
WANG Cong153711f2017-07-20 11:27:57 -07002258 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2259 sizeof(*sa));
Patrick McHardy0157f602007-06-13 12:03:36 -07002260 sa = kmalloc(len, GFP_KERNEL);
2261 if (!sa) {
2262 err = -ENOMEM;
2263 goto errout;
2264 }
2265 sa->sa_family = dev->type;
2266 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
2267 dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00002268 err = dev_set_mac_address(dev, sa);
Patrick McHardy0157f602007-06-13 12:03:36 -07002269 kfree(sa);
2270 if (err)
2271 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002272 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002273 }
2274
2275 if (tb[IFLA_MTU]) {
2276 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
2277 if (err < 0)
2278 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002279 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002280 }
2281
Vlad Dogarucbda10f2011-01-13 23:38:30 +00002282 if (tb[IFLA_GROUP]) {
2283 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Nicolas Dichtelba998902014-09-01 16:07:29 +02002284 status |= DO_SETLINK_NOTIFY;
Vlad Dogarucbda10f2011-01-13 23:38:30 +00002285 }
2286
Patrick McHardy0157f602007-06-13 12:03:36 -07002287 /*
2288 * Interface selected by interface index but interface
2289 * name provided implies that a name change has been
2290 * requested.
2291 */
2292 if (ifm->ifi_index > 0 && ifname[0]) {
2293 err = dev_change_name(dev, ifname);
2294 if (err < 0)
2295 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002296 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002297 }
2298
Stephen Hemminger0b815a12008-09-22 21:28:11 -07002299 if (tb[IFLA_IFALIAS]) {
2300 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2301 nla_len(tb[IFLA_IFALIAS]));
2302 if (err < 0)
2303 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002304 status |= DO_SETLINK_NOTIFY;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07002305 }
2306
Patrick McHardy0157f602007-06-13 12:03:36 -07002307 if (tb[IFLA_BROADCAST]) {
2308 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00002309 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Patrick McHardy0157f602007-06-13 12:03:36 -07002310 }
2311
2312 if (ifm->ifi_flags || ifm->ifi_change) {
Patrick McHardy3729d502010-02-26 06:34:54 +00002313 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
Johannes Berg5f9021c2008-11-16 23:20:31 -08002314 if (err < 0)
2315 goto errout;
Patrick McHardy0157f602007-06-13 12:03:36 -07002316 }
2317
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002318 if (tb[IFLA_MASTER]) {
David Ahern33eaf2a2017-10-04 17:48:46 -07002319 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002320 if (err)
2321 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002322 status |= DO_SETLINK_MODIFIED;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002323 }
2324
Jiri Pirko9a572472012-12-27 23:49:39 +00002325 if (tb[IFLA_CARRIER]) {
2326 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2327 if (err)
2328 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002329 status |= DO_SETLINK_MODIFIED;
Jiri Pirko9a572472012-12-27 23:49:39 +00002330 }
2331
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002332 if (tb[IFLA_TXQLEN]) {
Alexey Dobriyan0cd29502017-05-17 13:30:44 +03002333 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
2334 unsigned int orig_len = dev->tx_queue_len;
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002335
Jason Wang08294a22016-06-30 14:45:35 +08002336 if (dev->tx_queue_len ^ value) {
2337 dev->tx_queue_len = value;
2338 err = call_netdevice_notifiers(
2339 NETDEV_CHANGE_TX_QUEUE_LEN, dev);
2340 err = notifier_to_errno(err);
2341 if (err) {
2342 dev->tx_queue_len = orig_len;
2343 goto errout;
2344 }
Xin Long2d7f6692017-10-15 18:13:46 +08002345 status |= DO_SETLINK_MODIFIED;
Jason Wang08294a22016-06-30 14:45:35 +08002346 }
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002347 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002348
Stephen Hemminger46e6b992017-12-07 15:40:19 -08002349 if (tb[IFLA_GSO_MAX_SIZE]) {
2350 u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]);
2351
2352 if (max_size > GSO_MAX_SIZE) {
2353 err = -EINVAL;
2354 goto errout;
2355 }
2356
2357 if (dev->gso_max_size ^ max_size) {
2358 netif_set_gso_max_size(dev, max_size);
2359 status |= DO_SETLINK_MODIFIED;
2360 }
2361 }
2362
2363 if (tb[IFLA_GSO_MAX_SEGS]) {
2364 u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
2365
2366 if (max_segs > GSO_MAX_SEGS) {
2367 err = -EINVAL;
2368 goto errout;
2369 }
2370
2371 if (dev->gso_max_segs ^ max_segs) {
2372 dev->gso_max_segs = max_segs;
2373 status |= DO_SETLINK_MODIFIED;
2374 }
2375 }
2376
Patrick McHardy0157f602007-06-13 12:03:36 -07002377 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002378 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Patrick McHardy0157f602007-06-13 12:03:36 -07002379
2380 if (tb[IFLA_LINKMODE]) {
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002381 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2382
David S. Miller93b2d4a2008-02-17 18:35:07 -08002383 write_lock_bh(&dev_base_lock);
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002384 if (dev->link_mode ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02002385 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002386 dev->link_mode = value;
David S. Miller93b2d4a2008-02-17 18:35:07 -08002387 write_unlock_bh(&dev_base_lock);
Patrick McHardy0157f602007-06-13 12:03:36 -07002388 }
2389
Chris Wrightc02db8c2010-05-16 01:05:45 -07002390 if (tb[IFLA_VFINFO_LIST]) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002391 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
Chris Wrightc02db8c2010-05-16 01:05:45 -07002392 struct nlattr *attr;
2393 int rem;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002394
Chris Wrightc02db8c2010-05-16 01:05:45 -07002395 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002396 if (nla_type(attr) != IFLA_VF_INFO ||
2397 nla_len(attr) < NLA_HDRLEN) {
David Howells253683b2010-05-21 02:25:27 +00002398 err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002399 goto errout;
David Howells253683b2010-05-21 02:25:27 +00002400 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002401 err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr,
Johannes Bergfceb6432017-04-12 14:34:07 +02002402 ifla_vf_policy, NULL);
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002403 if (err < 0)
2404 goto errout;
2405 err = do_setvfinfo(dev, vfinfo);
Chris Wrightc02db8c2010-05-16 01:05:45 -07002406 if (err < 0)
2407 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002408 status |= DO_SETLINK_NOTIFY;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002409 }
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00002410 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002411 err = 0;
2412
Scott Feldman57b61082010-05-17 22:49:55 -07002413 if (tb[IFLA_VF_PORTS]) {
2414 struct nlattr *port[IFLA_PORT_MAX+1];
2415 struct nlattr *attr;
2416 int vf;
2417 int rem;
2418
2419 err = -EOPNOTSUPP;
2420 if (!ops->ndo_set_vf_port)
2421 goto errout;
2422
2423 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
Daniel Borkmann035d2102015-07-13 00:06:02 +02002424 if (nla_type(attr) != IFLA_VF_PORT ||
2425 nla_len(attr) < NLA_HDRLEN) {
2426 err = -EINVAL;
2427 goto errout;
2428 }
2429 err = nla_parse_nested(port, IFLA_PORT_MAX, attr,
Johannes Bergfceb6432017-04-12 14:34:07 +02002430 ifla_port_policy, NULL);
Scott Feldman57b61082010-05-17 22:49:55 -07002431 if (err < 0)
2432 goto errout;
2433 if (!port[IFLA_PORT_VF]) {
2434 err = -EOPNOTSUPP;
2435 goto errout;
2436 }
2437 vf = nla_get_u32(port[IFLA_PORT_VF]);
2438 err = ops->ndo_set_vf_port(dev, vf, port);
2439 if (err < 0)
2440 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002441 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07002442 }
2443 }
2444 err = 0;
2445
2446 if (tb[IFLA_PORT_SELF]) {
2447 struct nlattr *port[IFLA_PORT_MAX+1];
2448
2449 err = nla_parse_nested(port, IFLA_PORT_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +02002450 tb[IFLA_PORT_SELF], ifla_port_policy,
2451 NULL);
Scott Feldman57b61082010-05-17 22:49:55 -07002452 if (err < 0)
2453 goto errout;
2454
2455 err = -EOPNOTSUPP;
2456 if (ops->ndo_set_vf_port)
2457 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
2458 if (err < 0)
2459 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002460 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07002461 }
Thomas Graff8ff1822010-11-16 04:30:14 +00002462
2463 if (tb[IFLA_AF_SPEC]) {
2464 struct nlattr *af;
2465 int rem;
2466
2467 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2468 const struct rtnl_af_ops *af_ops;
2469
Florian Westphal5fa85a02017-10-16 15:44:36 +02002470 rcu_read_lock();
2471
Gustavo A. R. Silva058c8d52017-10-20 19:43:11 -05002472 BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
Thomas Graff8ff1822010-11-16 04:30:14 +00002473
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002474 err = af_ops->set_link_af(dev, af);
Florian Westphal5fa85a02017-10-16 15:44:36 +02002475 if (err < 0) {
2476 rcu_read_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +00002477 goto errout;
Florian Westphal5fa85a02017-10-16 15:44:36 +02002478 }
Thomas Graff8ff1822010-11-16 04:30:14 +00002479
Florian Westphal5fa85a02017-10-16 15:44:36 +02002480 rcu_read_unlock();
Nicolas Dichtelba998902014-09-01 16:07:29 +02002481 status |= DO_SETLINK_NOTIFY;
Thomas Graff8ff1822010-11-16 04:30:14 +00002482 }
2483 }
Scott Feldman57b61082010-05-17 22:49:55 -07002484 err = 0;
2485
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07002486 if (tb[IFLA_PROTO_DOWN]) {
2487 err = dev_change_proto_down(dev,
2488 nla_get_u8(tb[IFLA_PROTO_DOWN]));
2489 if (err)
2490 goto errout;
2491 status |= DO_SETLINK_NOTIFY;
2492 }
2493
Brenden Blancod1fdd912016-07-19 12:16:49 -07002494 if (tb[IFLA_XDP]) {
2495 struct nlattr *xdp[IFLA_XDP_MAX + 1];
Daniel Borkmann85de8572016-11-28 23:16:54 +01002496 u32 xdp_flags = 0;
Brenden Blancod1fdd912016-07-19 12:16:49 -07002497
2498 err = nla_parse_nested(xdp, IFLA_XDP_MAX, tb[IFLA_XDP],
Johannes Bergfceb6432017-04-12 14:34:07 +02002499 ifla_xdp_policy, NULL);
Brenden Blancod1fdd912016-07-19 12:16:49 -07002500 if (err < 0)
2501 goto errout;
2502
Martin KaFai Lau58038692017-06-15 17:29:09 -07002503 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
Brenden Blanco262d8622016-07-20 17:22:34 -07002504 err = -EINVAL;
2505 goto errout;
2506 }
Daniel Borkmann85de8572016-11-28 23:16:54 +01002507
2508 if (xdp[IFLA_XDP_FLAGS]) {
2509 xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
2510 if (xdp_flags & ~XDP_FLAGS_MASK) {
2511 err = -EINVAL;
2512 goto errout;
2513 }
Jakub Kicinskiee5d0322017-06-21 18:25:04 -07002514 if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
Daniel Borkmann0489df92017-05-12 01:04:45 +02002515 err = -EINVAL;
2516 goto errout;
2517 }
Daniel Borkmann85de8572016-11-28 23:16:54 +01002518 }
2519
Brenden Blancod1fdd912016-07-19 12:16:49 -07002520 if (xdp[IFLA_XDP_FD]) {
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002521 err = dev_change_xdp_fd(dev, extack,
Daniel Borkmann85de8572016-11-28 23:16:54 +01002522 nla_get_s32(xdp[IFLA_XDP_FD]),
2523 xdp_flags);
Brenden Blancod1fdd912016-07-19 12:16:49 -07002524 if (err)
2525 goto errout;
2526 status |= DO_SETLINK_NOTIFY;
2527 }
2528 }
2529
Patrick McHardy0157f602007-06-13 12:03:36 -07002530errout:
Nicolas Dichtelba998902014-09-01 16:07:29 +02002531 if (status & DO_SETLINK_MODIFIED) {
Xin Long64ff90c2017-10-15 18:13:45 +08002532 if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
Nicolas Dichtelba998902014-09-01 16:07:29 +02002533 netdev_state_change(dev);
2534
2535 if (err < 0)
2536 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",
2537 dev->name);
2538 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002539
Patrick McHardy0157f602007-06-13 12:03:36 -07002540 return err;
2541}
2542
David Ahernc21ef3e2017-04-16 09:48:24 -07002543static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2544 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002546 struct net *net = sock_net(skb->sk);
Thomas Grafda5e0492006-08-10 21:17:37 -07002547 struct ifinfomsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 struct net_device *dev;
Patrick McHardy0157f602007-06-13 12:03:36 -07002549 int err;
Thomas Grafda5e0492006-08-10 21:17:37 -07002550 struct nlattr *tb[IFLA_MAX+1];
2551 char ifname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552
David Ahernc21ef3e2017-04-16 09:48:24 -07002553 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy,
2554 extack);
Thomas Grafda5e0492006-08-10 21:17:37 -07002555 if (err < 0)
2556 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557
Thomas Graf5176f912006-08-26 20:13:18 -07002558 if (tb[IFLA_IFNAME])
2559 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
Patrick McHardy78e5b8912006-09-13 20:35:36 -07002560 else
2561 ifname[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 err = -EINVAL;
Thomas Grafda5e0492006-08-10 21:17:37 -07002564 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -07002565 if (ifm->ifi_index > 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00002566 dev = __dev_get_by_index(net, ifm->ifi_index);
Thomas Grafda5e0492006-08-10 21:17:37 -07002567 else if (tb[IFLA_IFNAME])
Eric Dumazeta3d12892009-10-21 10:59:31 +00002568 dev = __dev_get_by_name(net, ifname);
Thomas Grafda5e0492006-08-10 21:17:37 -07002569 else
2570 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
Thomas Grafda5e0492006-08-10 21:17:37 -07002572 if (dev == NULL) {
2573 err = -ENODEV;
2574 goto errout;
2575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
Eric Dumazete0d087a2009-11-07 01:26:17 -08002577 err = validate_linkmsg(dev, tb);
2578 if (err < 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00002579 goto errout;
Thomas Grafda5e0492006-08-10 21:17:37 -07002580
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002581 err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
Thomas Grafda5e0492006-08-10 21:17:37 -07002582errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 return err;
2584}
2585
WANG Cong66400d52015-03-24 11:53:31 -07002586static int rtnl_group_dellink(const struct net *net, int group)
2587{
2588 struct net_device *dev, *aux;
2589 LIST_HEAD(list_kill);
2590 bool found = false;
2591
2592 if (!group)
2593 return -EPERM;
2594
2595 for_each_netdev(net, dev) {
2596 if (dev->group == group) {
2597 const struct rtnl_link_ops *ops;
2598
2599 found = true;
2600 ops = dev->rtnl_link_ops;
2601 if (!ops || !ops->dellink)
2602 return -EOPNOTSUPP;
2603 }
2604 }
2605
2606 if (!found)
2607 return -ENODEV;
2608
2609 for_each_netdev_safe(net, dev, aux) {
2610 if (dev->group == group) {
2611 const struct rtnl_link_ops *ops;
2612
2613 ops = dev->rtnl_link_ops;
2614 ops->dellink(dev, &list_kill);
2615 }
2616 }
2617 unregister_netdevice_many(&list_kill);
2618
2619 return 0;
2620}
2621
Thomas Graf614732e2015-07-21 10:44:06 +02002622int rtnl_delete_link(struct net_device *dev)
2623{
2624 const struct rtnl_link_ops *ops;
2625 LIST_HEAD(list_kill);
2626
2627 ops = dev->rtnl_link_ops;
2628 if (!ops || !ops->dellink)
2629 return -EOPNOTSUPP;
2630
2631 ops->dellink(dev, &list_kill);
2632 unregister_netdevice_many(&list_kill);
2633
2634 return 0;
2635}
2636EXPORT_SYMBOL_GPL(rtnl_delete_link);
2637
David Ahernc21ef3e2017-04-16 09:48:24 -07002638static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
2639 struct netlink_ext_ack *extack)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002640{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002641 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002642 struct net_device *dev;
2643 struct ifinfomsg *ifm;
2644 char ifname[IFNAMSIZ];
2645 struct nlattr *tb[IFLA_MAX+1];
2646 int err;
2647
David Ahernc21ef3e2017-04-16 09:48:24 -07002648 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002649 if (err < 0)
2650 return err;
2651
Jiri Benc79e1ad12017-11-02 17:04:38 -02002652 if (tb[IFLA_IF_NETNSID])
2653 return -EOPNOTSUPP;
2654
Patrick McHardy38f7b872007-06-13 12:03:51 -07002655 if (tb[IFLA_IFNAME])
2656 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2657
2658 ifm = nlmsg_data(nlh);
2659 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07002660 dev = __dev_get_by_index(net, ifm->ifi_index);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002661 else if (tb[IFLA_IFNAME])
Eric W. Biederman881d9662007-09-17 11:56:21 -07002662 dev = __dev_get_by_name(net, ifname);
WANG Cong66400d52015-03-24 11:53:31 -07002663 else if (tb[IFLA_GROUP])
2664 return rtnl_group_dellink(net, nla_get_u32(tb[IFLA_GROUP]));
Patrick McHardy38f7b872007-06-13 12:03:51 -07002665 else
2666 return -EINVAL;
2667
2668 if (!dev)
2669 return -ENODEV;
2670
Thomas Graf614732e2015-07-21 10:44:06 +02002671 return rtnl_delete_link(dev);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002672}
2673
Patrick McHardy3729d502010-02-26 06:34:54 +00002674int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
2675{
2676 unsigned int old_flags;
2677 int err;
2678
2679 old_flags = dev->flags;
2680 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
2681 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
2682 if (err < 0)
2683 return err;
2684 }
2685
2686 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
Patrick McHardy3729d502010-02-26 06:34:54 +00002687
Nicolas Dichtela528c212013-09-25 12:02:44 +02002688 __dev_notify_flags(dev, old_flags, ~0U);
Patrick McHardy3729d502010-02-26 06:34:54 +00002689 return 0;
2690}
2691EXPORT_SYMBOL(rtnl_configure_link);
2692
Rami Rosenc0713562012-11-30 01:08:47 +00002693struct net_device *rtnl_create_link(struct net *net,
Thomas Graf78ebb0d2015-04-10 01:45:53 +02002694 const char *ifname, unsigned char name_assign_type,
Tom Gundersen55177502014-07-14 16:37:25 +02002695 const struct rtnl_link_ops *ops, struct nlattr *tb[])
Pavel Emelianove7199282007-08-08 22:16:38 -07002696{
Pavel Emelianove7199282007-08-08 22:16:38 -07002697 struct net_device *dev;
Jiri Pirkod40156a2012-07-20 02:28:47 +00002698 unsigned int num_tx_queues = 1;
2699 unsigned int num_rx_queues = 1;
Pavel Emelianove7199282007-08-08 22:16:38 -07002700
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002701 if (tb[IFLA_NUM_TX_QUEUES])
2702 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
2703 else if (ops->get_num_tx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002704 num_tx_queues = ops->get_num_tx_queues();
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002705
2706 if (tb[IFLA_NUM_RX_QUEUES])
2707 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
2708 else if (ops->get_num_rx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002709 num_rx_queues = ops->get_num_rx_queues();
stephen hemmingerefacb302012-04-10 18:34:43 +00002710
Tom Gundersen55177502014-07-14 16:37:25 +02002711 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
Tom Gundersenc835a672014-07-14 16:37:24 +02002712 ops->setup, num_tx_queues, num_rx_queues);
Pavel Emelianove7199282007-08-08 22:16:38 -07002713 if (!dev)
Tobias Klauserd1892e42017-02-20 16:32:06 +01002714 return ERR_PTR(-ENOMEM);
Pavel Emelianove7199282007-08-08 22:16:38 -07002715
Eric W. Biederman81adee42009-11-08 00:53:51 -08002716 dev_net_set(dev, net);
2717 dev->rtnl_link_ops = ops;
Patrick McHardy3729d502010-02-26 06:34:54 +00002718 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
Eric W. Biederman81adee42009-11-08 00:53:51 -08002719
Pavel Emelianove7199282007-08-08 22:16:38 -07002720 if (tb[IFLA_MTU])
2721 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002722 if (tb[IFLA_ADDRESS]) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002723 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
2724 nla_len(tb[IFLA_ADDRESS]));
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002725 dev->addr_assign_type = NET_ADDR_SET;
2726 }
Pavel Emelianove7199282007-08-08 22:16:38 -07002727 if (tb[IFLA_BROADCAST])
2728 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
2729 nla_len(tb[IFLA_BROADCAST]));
2730 if (tb[IFLA_TXQLEN])
2731 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
2732 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002733 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Pavel Emelianove7199282007-08-08 22:16:38 -07002734 if (tb[IFLA_LINKMODE])
2735 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
Patrick McHardyffa934f2011-01-20 03:00:42 +00002736 if (tb[IFLA_GROUP])
2737 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Stephen Hemminger46e6b992017-12-07 15:40:19 -08002738 if (tb[IFLA_GSO_MAX_SIZE])
2739 netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE]));
2740 if (tb[IFLA_GSO_MAX_SEGS])
Stephen Hemmingera0b586f2017-12-08 15:34:13 -08002741 dev->gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
Pavel Emelianove7199282007-08-08 22:16:38 -07002742
2743 return dev;
Pavel Emelianove7199282007-08-08 22:16:38 -07002744}
Eric Dumazete0d087a2009-11-07 01:26:17 -08002745EXPORT_SYMBOL(rtnl_create_link);
Pavel Emelianove7199282007-08-08 22:16:38 -07002746
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002747static int rtnl_group_changelink(const struct sk_buff *skb,
2748 struct net *net, int group,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002749 struct ifinfomsg *ifm,
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002750 struct netlink_ext_ack *extack,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002751 struct nlattr **tb)
2752{
WANG Congd0795352015-03-23 16:31:09 -07002753 struct net_device *dev, *aux;
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002754 int err;
2755
WANG Congd0795352015-03-23 16:31:09 -07002756 for_each_netdev_safe(net, dev, aux) {
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002757 if (dev->group == group) {
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002758 err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002759 if (err < 0)
2760 return err;
2761 }
2762 }
2763
2764 return 0;
2765}
2766
David Ahernc21ef3e2017-04-16 09:48:24 -07002767static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2768 struct netlink_ext_ack *extack)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002769{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002770 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002771 const struct rtnl_link_ops *ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002772 const struct rtnl_link_ops *m_ops = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002773 struct net_device *dev;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002774 struct net_device *master_dev = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002775 struct ifinfomsg *ifm;
2776 char kind[MODULE_NAME_LEN];
2777 char ifname[IFNAMSIZ];
2778 struct nlattr *tb[IFLA_MAX+1];
2779 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
Tom Gundersen55177502014-07-14 16:37:25 +02002780 unsigned char name_assign_type = NET_NAME_USER;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002781 int err;
2782
Johannes Berg95a5afc2008-10-16 15:24:51 -07002783#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002784replay:
Thomas Graf8072f082007-07-31 14:13:50 -07002785#endif
David Ahernc21ef3e2017-04-16 09:48:24 -07002786 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002787 if (err < 0)
2788 return err;
2789
Jiri Benc79e1ad12017-11-02 17:04:38 -02002790 if (tb[IFLA_IF_NETNSID])
2791 return -EOPNOTSUPP;
2792
Patrick McHardy38f7b872007-06-13 12:03:51 -07002793 if (tb[IFLA_IFNAME])
2794 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2795 else
2796 ifname[0] = '\0';
2797
2798 ifm = nlmsg_data(nlh);
2799 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07002800 dev = __dev_get_by_index(net, ifm->ifi_index);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002801 else {
2802 if (ifname[0])
2803 dev = __dev_get_by_name(net, ifname);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002804 else
2805 dev = NULL;
2806 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002807
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002808 if (dev) {
2809 master_dev = netdev_master_upper_dev_get(dev);
2810 if (master_dev)
2811 m_ops = master_dev->rtnl_link_ops;
2812 }
2813
Eric Dumazete0d087a2009-11-07 01:26:17 -08002814 err = validate_linkmsg(dev, tb);
2815 if (err < 0)
Thomas Graf1840bb12008-02-23 19:54:36 -08002816 return err;
2817
Patrick McHardy38f7b872007-06-13 12:03:51 -07002818 if (tb[IFLA_LINKINFO]) {
2819 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +02002820 tb[IFLA_LINKINFO], ifla_info_policy,
2821 NULL);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002822 if (err < 0)
2823 return err;
2824 } else
2825 memset(linkinfo, 0, sizeof(linkinfo));
2826
2827 if (linkinfo[IFLA_INFO_KIND]) {
2828 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
2829 ops = rtnl_link_ops_get(kind);
2830 } else {
2831 kind[0] = '\0';
2832 ops = NULL;
2833 }
2834
2835 if (1) {
Sasha Levin4e10fd52015-02-24 14:14:35 -05002836 struct nlattr *attr[ops ? ops->maxtype + 1 : 1];
2837 struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1];
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002838 struct nlattr **data = NULL;
2839 struct nlattr **slave_data = NULL;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002840 struct net *dest_net, *link_net = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002841
2842 if (ops) {
2843 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
2844 err = nla_parse_nested(attr, ops->maxtype,
2845 linkinfo[IFLA_INFO_DATA],
Johannes Bergfceb6432017-04-12 14:34:07 +02002846 ops->policy, NULL);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002847 if (err < 0)
2848 return err;
2849 data = attr;
2850 }
2851 if (ops->validate) {
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02002852 err = ops->validate(tb, data, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002853 if (err < 0)
2854 return err;
2855 }
2856 }
2857
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002858 if (m_ops) {
2859 if (m_ops->slave_maxtype &&
2860 linkinfo[IFLA_INFO_SLAVE_DATA]) {
2861 err = nla_parse_nested(slave_attr,
2862 m_ops->slave_maxtype,
2863 linkinfo[IFLA_INFO_SLAVE_DATA],
Johannes Bergfceb6432017-04-12 14:34:07 +02002864 m_ops->slave_policy,
2865 NULL);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002866 if (err < 0)
2867 return err;
2868 slave_data = slave_attr;
2869 }
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002870 }
2871
Patrick McHardy38f7b872007-06-13 12:03:51 -07002872 if (dev) {
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002873 int status = 0;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002874
2875 if (nlh->nlmsg_flags & NLM_F_EXCL)
2876 return -EEXIST;
2877 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2878 return -EOPNOTSUPP;
2879
2880 if (linkinfo[IFLA_INFO_DATA]) {
2881 if (!ops || ops != dev->rtnl_link_ops ||
2882 !ops->changelink)
2883 return -EOPNOTSUPP;
2884
Matthias Schifferad744b22017-06-25 23:56:00 +02002885 err = ops->changelink(dev, tb, data, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002886 if (err < 0)
2887 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002888 status |= DO_SETLINK_NOTIFY;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002889 }
2890
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002891 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2892 if (!m_ops || !m_ops->slave_changelink)
2893 return -EOPNOTSUPP;
2894
2895 err = m_ops->slave_changelink(master_dev, dev,
Matthias Schiffer17dd0ec2017-06-25 23:56:02 +02002896 tb, slave_data,
2897 extack);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002898 if (err < 0)
2899 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002900 status |= DO_SETLINK_NOTIFY;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002901 }
2902
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002903 return do_setlink(skb, dev, ifm, extack, tb, ifname,
2904 status);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002905 }
2906
Patrick McHardyffa934f2011-01-20 03:00:42 +00002907 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2908 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002909 return rtnl_group_changelink(skb, net,
Patrick McHardyffa934f2011-01-20 03:00:42 +00002910 nla_get_u32(tb[IFLA_GROUP]),
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002911 ifm, extack, tb);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002912 return -ENODEV;
Patrick McHardyffa934f2011-01-20 03:00:42 +00002913 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002914
Theuns Verwoerd160ca012017-01-31 12:23:46 +13002915 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
Patrick McHardy38f7b872007-06-13 12:03:51 -07002916 return -EOPNOTSUPP;
2917
2918 if (!ops) {
Johannes Berg95a5afc2008-10-16 15:24:51 -07002919#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002920 if (kind[0]) {
2921 __rtnl_unlock();
2922 request_module("rtnl-link-%s", kind);
2923 rtnl_lock();
2924 ops = rtnl_link_ops_get(kind);
2925 if (ops)
2926 goto replay;
2927 }
2928#endif
2929 return -EOPNOTSUPP;
2930 }
2931
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02002932 if (!ops->setup)
2933 return -EOPNOTSUPP;
2934
Tom Gundersen55177502014-07-14 16:37:25 +02002935 if (!ifname[0]) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07002936 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
Tom Gundersen55177502014-07-14 16:37:25 +02002937 name_assign_type = NET_NAME_ENUM;
2938 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002939
Eric W. Biederman81adee42009-11-08 00:53:51 -08002940 dest_net = rtnl_link_get_net(net, tb);
Eric W. Biederman13ad1772011-01-29 14:57:22 +00002941 if (IS_ERR(dest_net))
2942 return PTR_ERR(dest_net);
2943
Eric W. Biederman505ce412015-02-26 16:19:00 -06002944 err = -EPERM;
2945 if (!netlink_ns_capable(skb, dest_net->user_ns, CAP_NET_ADMIN))
2946 goto out;
2947
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002948 if (tb[IFLA_LINK_NETNSID]) {
2949 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
2950
2951 link_net = get_net_ns_by_id(dest_net, id);
2952 if (!link_net) {
2953 err = -EINVAL;
2954 goto out;
2955 }
Eric W. Biederman06615be2015-02-26 16:20:07 -06002956 err = -EPERM;
2957 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
2958 goto out;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002959 }
2960
2961 dev = rtnl_create_link(link_net ? : dest_net, ifname,
2962 name_assign_type, ops, tb);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002963 if (IS_ERR(dev)) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002964 err = PTR_ERR(dev);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002965 goto out;
2966 }
2967
2968 dev->ifindex = ifm->ifi_index;
2969
Cong Wang0e0eee22014-02-11 15:51:30 -08002970 if (ops->newlink) {
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02002971 err = ops->newlink(link_net ? : net, dev, tb, data,
2972 extack);
Cong Wang0e0eee22014-02-11 15:51:30 -08002973 /* Drivers should call free_netdev() in ->destructor
Cong Wange51fb152014-06-03 16:40:47 -07002974 * and unregister it on failure after registration
2975 * so that device could be finally freed in rtnl_unlock.
Cong Wang0e0eee22014-02-11 15:51:30 -08002976 */
Cong Wange51fb152014-06-03 16:40:47 -07002977 if (err < 0) {
2978 /* If device is not registered at all, free it now */
2979 if (dev->reg_state == NETREG_UNINITIALIZED)
2980 free_netdev(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002981 goto out;
Cong Wange51fb152014-06-03 16:40:47 -07002982 }
Cong Wang0e0eee22014-02-11 15:51:30 -08002983 } else {
Patrick McHardy2d85cba2007-07-11 19:42:13 -07002984 err = register_netdevice(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002985 if (err < 0) {
2986 free_netdev(dev);
2987 goto out;
2988 }
Dan Carpenterfce9b9b2013-08-14 12:35:42 +03002989 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002990 err = rtnl_configure_link(dev, ifm);
David S. Miller43638902015-03-10 21:58:32 -04002991 if (err < 0)
2992 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002993 if (link_net) {
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002994 err = dev_change_net_namespace(dev, dest_net, ifname);
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002995 if (err < 0)
David S. Miller43638902015-03-10 21:58:32 -04002996 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002997 }
Theuns Verwoerd160ca012017-01-31 12:23:46 +13002998 if (tb[IFLA_MASTER]) {
David Ahern33eaf2a2017-10-04 17:48:46 -07002999 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]),
3000 extack);
Theuns Verwoerd160ca012017-01-31 12:23:46 +13003001 if (err)
3002 goto out_unregister;
3003 }
Patrick McHardy3729d502010-02-26 06:34:54 +00003004out:
Nicolas Dichtel317f4812015-01-15 15:11:18 +01003005 if (link_net)
3006 put_net(link_net);
Eric W. Biederman81adee42009-11-08 00:53:51 -08003007 put_net(dest_net);
Patrick McHardy38f7b872007-06-13 12:03:51 -07003008 return err;
David S. Miller43638902015-03-10 21:58:32 -04003009out_unregister:
3010 if (ops->newlink) {
3011 LIST_HEAD(list_kill);
3012
3013 ops->dellink(dev, &list_kill);
3014 unregister_netdevice_many(&list_kill);
3015 } else {
3016 unregister_netdevice(dev);
3017 }
3018 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003019 }
3020}
3021
David Ahernc21ef3e2017-04-16 09:48:24 -07003022static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3023 struct netlink_ext_ack *extack)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003024{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003025 struct net *net = sock_net(skb->sk);
Jiri Benc79e1ad12017-11-02 17:04:38 -02003026 struct net *tgt_net = net;
Thomas Grafb60c5112006-08-04 23:05:34 -07003027 struct ifinfomsg *ifm;
Eric Dumazeta3d12892009-10-21 10:59:31 +00003028 char ifname[IFNAMSIZ];
Thomas Grafb60c5112006-08-04 23:05:34 -07003029 struct nlattr *tb[IFLA_MAX+1];
3030 struct net_device *dev = NULL;
3031 struct sk_buff *nskb;
Jiri Benc79e1ad12017-11-02 17:04:38 -02003032 int netnsid = -1;
Thomas Graf339bf982006-11-10 14:10:15 -08003033 int err;
Greg Rose115c9b82012-02-21 16:54:48 -05003034 u32 ext_filter_mask = 0;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003035
David Ahernc21ef3e2017-04-16 09:48:24 -07003036 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Thomas Grafb60c5112006-08-04 23:05:34 -07003037 if (err < 0)
Eric Sesterhenn9918f232006-09-26 23:26:38 -07003038 return err;
Thomas Grafb60c5112006-08-04 23:05:34 -07003039
Jiri Benc79e1ad12017-11-02 17:04:38 -02003040 if (tb[IFLA_IF_NETNSID]) {
3041 netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]);
Andrei Vaginf428fe42018-01-02 23:27:33 -08003042 tgt_net = get_target_net(NETLINK_CB(skb).sk, netnsid);
Jiri Benc79e1ad12017-11-02 17:04:38 -02003043 if (IS_ERR(tgt_net))
3044 return PTR_ERR(tgt_net);
3045 }
3046
Eric Dumazeta3d12892009-10-21 10:59:31 +00003047 if (tb[IFLA_IFNAME])
3048 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
3049
Greg Rose115c9b82012-02-21 16:54:48 -05003050 if (tb[IFLA_EXT_MASK])
3051 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3052
Jiri Benc79e1ad12017-11-02 17:04:38 -02003053 err = -EINVAL;
Thomas Grafb60c5112006-08-04 23:05:34 -07003054 ifm = nlmsg_data(nlh);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003055 if (ifm->ifi_index > 0)
Jiri Benc79e1ad12017-11-02 17:04:38 -02003056 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003057 else if (tb[IFLA_IFNAME])
Jiri Benc79e1ad12017-11-02 17:04:38 -02003058 dev = __dev_get_by_name(tgt_net, ifname);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003059 else
Jiri Benc79e1ad12017-11-02 17:04:38 -02003060 goto out;
Thomas Grafb60c5112006-08-04 23:05:34 -07003061
Jiri Benc79e1ad12017-11-02 17:04:38 -02003062 err = -ENODEV;
Eric Dumazeta3d12892009-10-21 10:59:31 +00003063 if (dev == NULL)
Jiri Benc79e1ad12017-11-02 17:04:38 -02003064 goto out;
Eric Dumazeta3d12892009-10-21 10:59:31 +00003065
Jiri Benc79e1ad12017-11-02 17:04:38 -02003066 err = -ENOBUFS;
Greg Rose115c9b82012-02-21 16:54:48 -05003067 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003068 if (nskb == NULL)
Jiri Benc79e1ad12017-11-02 17:04:38 -02003069 goto out;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003070
Jiri Benc79e1ad12017-11-02 17:04:38 -02003071 err = rtnl_fill_ifinfo(nskb, dev, net,
3072 RTM_NEWLINK, NETLINK_CB(skb).portid,
3073 nlh->nlmsg_seq, 0, 0, ext_filter_mask,
3074 0, NULL, netnsid);
Patrick McHardy26932562007-01-31 23:16:40 -08003075 if (err < 0) {
3076 /* -EMSGSIZE implies BUG in if_nlmsg_size */
3077 WARN_ON(err == -EMSGSIZE);
3078 kfree_skb(nskb);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003079 } else
Eric W. Biederman15e47302012-09-07 20:12:54 +00003080 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
Jiri Benc79e1ad12017-11-02 17:04:38 -02003081out:
3082 if (netnsid >= 0)
3083 put_net(tgt_net);
Thomas Grafb60c5112006-08-04 23:05:34 -07003084
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003085 return err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003086}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003087
Greg Rose115c9b82012-02-21 16:54:48 -05003088static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
Greg Rosec7ac8672011-06-10 01:27:09 +00003089{
Greg Rose115c9b82012-02-21 16:54:48 -05003090 struct net *net = sock_net(skb->sk);
3091 struct net_device *dev;
3092 struct nlattr *tb[IFLA_MAX+1];
3093 u32 ext_filter_mask = 0;
3094 u16 min_ifinfo_dump_size = 0;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02003095 int hdrlen;
Greg Rose115c9b82012-02-21 16:54:48 -05003096
Michal Schmidte5eca6d2014-05-28 14:15:19 +02003097 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3098 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
3099 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
3100
Johannes Bergfceb6432017-04-12 14:34:07 +02003101 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00003102 if (tb[IFLA_EXT_MASK])
3103 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3104 }
Greg Rose115c9b82012-02-21 16:54:48 -05003105
3106 if (!ext_filter_mask)
3107 return NLMSG_GOODSIZE;
3108 /*
3109 * traverse the list of net devices and compute the minimum
3110 * buffer size based upon the filter mask.
3111 */
Florian Westphal6853dd42017-08-09 20:41:51 +02003112 rcu_read_lock();
3113 for_each_netdev_rcu(net, dev) {
Greg Rose115c9b82012-02-21 16:54:48 -05003114 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
3115 if_nlmsg_size(dev,
3116 ext_filter_mask));
3117 }
Florian Westphal6853dd42017-08-09 20:41:51 +02003118 rcu_read_unlock();
Greg Rose115c9b82012-02-21 16:54:48 -05003119
Zhang Shengju93af2052016-11-22 14:14:28 +08003120 return nlmsg_total_size(min_ifinfo_dump_size);
Greg Rosec7ac8672011-06-10 01:27:09 +00003121}
3122
Adrian Bunk42bad1d2007-04-26 00:57:41 -07003123static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124{
3125 int idx;
3126 int s_idx = cb->family;
3127
3128 if (s_idx == 0)
3129 s_idx = 1;
Florian Westphal6853dd42017-08-09 20:41:51 +02003130
Patrick McHardy25239ce2010-04-26 16:02:05 +02003131 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
Florian Westphaladdf9b92017-12-02 21:44:05 +01003132 struct rtnl_link **tab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 int type = cb->nlh->nlmsg_type-RTM_BASE;
Florian Westphaladdf9b92017-12-02 21:44:05 +01003134 struct rtnl_link *link;
Florian Westphal6853dd42017-08-09 20:41:51 +02003135 rtnl_dumpit_func dumpit;
3136
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 if (idx < s_idx || idx == PF_PACKET)
3138 continue;
Florian Westphal6853dd42017-08-09 20:41:51 +02003139
Florian Westphaladdf9b92017-12-02 21:44:05 +01003140 if (type < 0 || type >= RTM_NR_MSGTYPES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 continue;
Florian Westphal6853dd42017-08-09 20:41:51 +02003142
Florian Westphaladdf9b92017-12-02 21:44:05 +01003143 tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]);
3144 if (!tab)
3145 continue;
3146
3147 link = tab[type];
3148 if (!link)
3149 continue;
3150
3151 dumpit = link->dumpit;
Florian Westphal6853dd42017-08-09 20:41:51 +02003152 if (!dumpit)
3153 continue;
3154
Nicolas Dichtel04652772013-03-22 06:28:42 +00003155 if (idx > s_idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 memset(&cb->args[0], 0, sizeof(cb->args));
Nicolas Dichtel04652772013-03-22 06:28:42 +00003157 cb->prev_seq = 0;
3158 cb->seq = 0;
3159 }
Florian Westphal6853dd42017-08-09 20:41:51 +02003160 if (dumpit(skb, cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 break;
3162 }
3163 cb->family = idx;
3164
3165 return skb->len;
3166}
3167
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003168struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003169 unsigned int change,
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003170 u32 event, gfp_t flags, int *new_nsid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003172 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 struct sk_buff *skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003174 int err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00003175 size_t if_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176
Alexei Starovoitov7f294052013-10-23 16:02:42 -07003177 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003178 if (skb == NULL)
3179 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180
Jiri Benc79e1ad12017-11-02 17:04:38 -02003181 err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
3182 type, 0, 0, change, 0, 0, event,
3183 new_nsid, -1);
Patrick McHardy26932562007-01-31 23:16:40 -08003184 if (err < 0) {
3185 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
3186 WARN_ON(err == -EMSGSIZE);
3187 kfree_skb(skb);
3188 goto errout;
3189 }
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003190 return skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003191errout:
3192 if (err < 0)
Eric W. Biederman4b3da702007-11-19 22:27:40 -08003193 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003194 return NULL;
3195}
3196
3197void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
3198{
3199 struct net *net = dev_net(dev);
3200
3201 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
3202}
3203
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003204static void rtmsg_ifinfo_event(int type, struct net_device *dev,
3205 unsigned int change, u32 event,
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003206 gfp_t flags, int *new_nsid)
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003207{
3208 struct sk_buff *skb;
3209
Nicolas Dichteled2a80a2015-05-13 14:19:42 +02003210 if (dev->reg_state != NETREG_REGISTERED)
3211 return;
3212
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003213 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003214 if (skb)
3215 rtmsg_ifinfo_send(skb, dev, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216}
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003217
3218void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
3219 gfp_t flags)
3220{
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003221 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, NULL);
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003222}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003224void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
3225 gfp_t flags, int *new_nsid)
3226{
3227 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3228 new_nsid);
3229}
3230
John Fastabendd83b0602012-04-15 06:44:08 +00003231static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
3232 struct net_device *dev,
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003233 u8 *addr, u16 vid, u32 pid, u32 seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003234 int type, unsigned int flags,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003235 int nlflags, u16 ndm_state)
John Fastabendd83b0602012-04-15 06:44:08 +00003236{
3237 struct nlmsghdr *nlh;
3238 struct ndmsg *ndm;
3239
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003240 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
John Fastabendd83b0602012-04-15 06:44:08 +00003241 if (!nlh)
3242 return -EMSGSIZE;
3243
3244 ndm = nlmsg_data(nlh);
3245 ndm->ndm_family = AF_BRIDGE;
3246 ndm->ndm_pad1 = 0;
3247 ndm->ndm_pad2 = 0;
3248 ndm->ndm_flags = flags;
3249 ndm->ndm_type = 0;
3250 ndm->ndm_ifindex = dev->ifindex;
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003251 ndm->ndm_state = ndm_state;
John Fastabendd83b0602012-04-15 06:44:08 +00003252
3253 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
3254 goto nla_put_failure;
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003255 if (vid)
3256 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
3257 goto nla_put_failure;
John Fastabendd83b0602012-04-15 06:44:08 +00003258
Johannes Berg053c0952015-01-16 22:09:00 +01003259 nlmsg_end(skb, nlh);
3260 return 0;
John Fastabendd83b0602012-04-15 06:44:08 +00003261
3262nla_put_failure:
3263 nlmsg_cancel(skb, nlh);
3264 return -EMSGSIZE;
3265}
3266
John Fastabend3ff661c2012-04-15 06:44:14 +00003267static inline size_t rtnl_fdb_nlmsg_size(void)
3268{
Sabrina Dubrocaf82ef3e2016-11-18 15:50:39 +01003269 return NLMSG_ALIGN(sizeof(struct ndmsg)) +
3270 nla_total_size(ETH_ALEN) + /* NDA_LLADDR */
3271 nla_total_size(sizeof(u16)) + /* NDA_VLAN */
3272 0;
John Fastabend3ff661c2012-04-15 06:44:14 +00003273}
3274
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003275static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
3276 u16 ndm_state)
John Fastabend3ff661c2012-04-15 06:44:14 +00003277{
3278 struct net *net = dev_net(dev);
3279 struct sk_buff *skb;
3280 int err = -ENOBUFS;
3281
3282 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
3283 if (!skb)
3284 goto errout;
3285
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003286 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003287 0, 0, type, NTF_SELF, 0, ndm_state);
John Fastabend3ff661c2012-04-15 06:44:14 +00003288 if (err < 0) {
3289 kfree_skb(skb);
3290 goto errout;
3291 }
3292
3293 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
3294 return;
3295errout:
3296 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
3297}
3298
Vlad Yasevich090096b2013-03-06 15:39:42 +00003299/**
3300 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3301 */
3302int ndo_dflt_fdb_add(struct ndmsg *ndm,
3303 struct nlattr *tb[],
3304 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003305 const unsigned char *addr, u16 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003306 u16 flags)
3307{
3308 int err = -EINVAL;
3309
3310 /* If aging addresses are supported device will need to
3311 * implement its own handler for this.
3312 */
3313 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
3314 pr_info("%s: FDB only supports static addresses\n", dev->name);
3315 return err;
3316 }
3317
Or Gerlitz65891fe2014-12-14 18:19:05 +02003318 if (vid) {
3319 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
3320 return err;
3321 }
3322
Vlad Yasevich090096b2013-03-06 15:39:42 +00003323 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3324 err = dev_uc_add_excl(dev, addr);
3325 else if (is_multicast_ether_addr(addr))
3326 err = dev_mc_add_excl(dev, addr);
3327
3328 /* Only return duplicate errors if NLM_F_EXCL is set */
3329 if (err == -EEXIST && !(flags & NLM_F_EXCL))
3330 err = 0;
3331
3332 return err;
3333}
3334EXPORT_SYMBOL(ndo_dflt_fdb_add);
3335
Florian Westphalb88d12e2017-10-10 17:10:04 +02003336static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
3337 struct netlink_ext_ack *extack)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003338{
3339 u16 vid = 0;
3340
3341 if (vlan_attr) {
3342 if (nla_len(vlan_attr) != sizeof(u16)) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003343 NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
Jiri Pirkof6f64242014-11-28 14:34:15 +01003344 return -EINVAL;
3345 }
3346
3347 vid = nla_get_u16(vlan_attr);
3348
3349 if (!vid || vid >= VLAN_VID_MASK) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003350 NL_SET_ERR_MSG(extack, "invalid vlan id");
Jiri Pirkof6f64242014-11-28 14:34:15 +01003351 return -EINVAL;
3352 }
3353 }
3354 *p_vid = vid;
3355 return 0;
3356}
3357
David Ahernc21ef3e2017-04-16 09:48:24 -07003358static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
3359 struct netlink_ext_ack *extack)
John Fastabend77162022012-04-15 06:43:56 +00003360{
3361 struct net *net = sock_net(skb->sk);
John Fastabend77162022012-04-15 06:43:56 +00003362 struct ndmsg *ndm;
3363 struct nlattr *tb[NDA_MAX+1];
3364 struct net_device *dev;
3365 u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01003366 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00003367 int err;
3368
David Ahernc21ef3e2017-04-16 09:48:24 -07003369 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
John Fastabend77162022012-04-15 06:43:56 +00003370 if (err < 0)
3371 return err;
3372
3373 ndm = nlmsg_data(nlh);
3374 if (ndm->ndm_ifindex == 0) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003375 NL_SET_ERR_MSG(extack, "invalid ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003376 return -EINVAL;
3377 }
3378
3379 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3380 if (dev == NULL) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003381 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003382 return -ENODEV;
3383 }
3384
3385 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003386 NL_SET_ERR_MSG(extack, "invalid address");
John Fastabend77162022012-04-15 06:43:56 +00003387 return -EINVAL;
3388 }
3389
3390 addr = nla_data(tb[NDA_LLADDR]);
John Fastabend77162022012-04-15 06:43:56 +00003391
Florian Westphalb88d12e2017-10-10 17:10:04 +02003392 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
Jiri Pirkof6f64242014-11-28 14:34:15 +01003393 if (err)
3394 return err;
3395
John Fastabend77162022012-04-15 06:43:56 +00003396 err = -EOPNOTSUPP;
3397
3398 /* Support fdb on master device the net/bridge default case */
3399 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3400 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003401 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3402 const struct net_device_ops *ops = br_dev->netdev_ops;
3403
Jiri Pirkof6f64242014-11-28 14:34:15 +01003404 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
3405 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00003406 if (err)
3407 goto out;
3408 else
3409 ndm->ndm_flags &= ~NTF_MASTER;
3410 }
3411
3412 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00003413 if ((ndm->ndm_flags & NTF_SELF)) {
3414 if (dev->netdev_ops->ndo_fdb_add)
3415 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003416 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003417 nlh->nlmsg_flags);
3418 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01003419 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003420 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00003421
John Fastabend3ff661c2012-04-15 06:44:14 +00003422 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003423 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
3424 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00003425 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00003426 }
John Fastabend77162022012-04-15 06:43:56 +00003427 }
3428out:
3429 return err;
3430}
3431
Vlad Yasevich090096b2013-03-06 15:39:42 +00003432/**
3433 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3434 */
3435int ndo_dflt_fdb_del(struct ndmsg *ndm,
3436 struct nlattr *tb[],
3437 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003438 const unsigned char *addr, u16 vid)
Vlad Yasevich090096b2013-03-06 15:39:42 +00003439{
Alexander Duyckc8a89c42014-07-15 15:15:20 -07003440 int err = -EINVAL;
Vlad Yasevich090096b2013-03-06 15:39:42 +00003441
3442 /* If aging addresses are supported device will need to
3443 * implement its own handler for this.
3444 */
Sridhar Samudrala64535992013-08-08 15:19:48 -07003445 if (!(ndm->ndm_state & NUD_PERMANENT)) {
Vlad Yasevich090096b2013-03-06 15:39:42 +00003446 pr_info("%s: FDB only supports static addresses\n", dev->name);
Alexander Duyckc8a89c42014-07-15 15:15:20 -07003447 return err;
Vlad Yasevich090096b2013-03-06 15:39:42 +00003448 }
3449
3450 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3451 err = dev_uc_del(dev, addr);
3452 else if (is_multicast_ether_addr(addr))
3453 err = dev_mc_del(dev, addr);
Vlad Yasevich090096b2013-03-06 15:39:42 +00003454
3455 return err;
3456}
3457EXPORT_SYMBOL(ndo_dflt_fdb_del);
3458
David Ahernc21ef3e2017-04-16 09:48:24 -07003459static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
3460 struct netlink_ext_ack *extack)
John Fastabend77162022012-04-15 06:43:56 +00003461{
3462 struct net *net = sock_net(skb->sk);
3463 struct ndmsg *ndm;
Vlad Yasevich1690be62013-02-13 12:00:18 +00003464 struct nlattr *tb[NDA_MAX+1];
John Fastabend77162022012-04-15 06:43:56 +00003465 struct net_device *dev;
3466 int err = -EINVAL;
3467 __u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01003468 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00003469
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07003470 if (!netlink_capable(skb, CAP_NET_ADMIN))
Vlad Yasevich1690be62013-02-13 12:00:18 +00003471 return -EPERM;
3472
David Ahernc21ef3e2017-04-16 09:48:24 -07003473 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
Vlad Yasevich1690be62013-02-13 12:00:18 +00003474 if (err < 0)
3475 return err;
John Fastabend77162022012-04-15 06:43:56 +00003476
3477 ndm = nlmsg_data(nlh);
3478 if (ndm->ndm_ifindex == 0) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003479 NL_SET_ERR_MSG(extack, "invalid ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003480 return -EINVAL;
3481 }
3482
3483 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3484 if (dev == NULL) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003485 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003486 return -ENODEV;
3487 }
3488
Vlad Yasevich1690be62013-02-13 12:00:18 +00003489 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003490 NL_SET_ERR_MSG(extack, "invalid address");
John Fastabend77162022012-04-15 06:43:56 +00003491 return -EINVAL;
3492 }
3493
Vlad Yasevich1690be62013-02-13 12:00:18 +00003494 addr = nla_data(tb[NDA_LLADDR]);
Vlad Yasevich1690be62013-02-13 12:00:18 +00003495
Florian Westphalb88d12e2017-10-10 17:10:04 +02003496 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
Jiri Pirkof6f64242014-11-28 14:34:15 +01003497 if (err)
3498 return err;
3499
John Fastabend77162022012-04-15 06:43:56 +00003500 err = -EOPNOTSUPP;
3501
3502 /* Support fdb on master device the net/bridge default case */
3503 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3504 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003505 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3506 const struct net_device_ops *ops = br_dev->netdev_ops;
John Fastabend77162022012-04-15 06:43:56 +00003507
Jiri Pirko898e5062013-01-03 22:48:52 +00003508 if (ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003509 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00003510
3511 if (err)
3512 goto out;
3513 else
3514 ndm->ndm_flags &= ~NTF_MASTER;
3515 }
3516
3517 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00003518 if (ndm->ndm_flags & NTF_SELF) {
3519 if (dev->netdev_ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003520 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
3521 vid);
Vlad Yasevich090096b2013-03-06 15:39:42 +00003522 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01003523 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00003524
John Fastabend3ff661c2012-04-15 06:44:14 +00003525 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003526 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
3527 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00003528 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00003529 }
John Fastabend77162022012-04-15 06:43:56 +00003530 }
3531out:
3532 return err;
3533}
3534
John Fastabendd83b0602012-04-15 06:44:08 +00003535static int nlmsg_populate_fdb(struct sk_buff *skb,
3536 struct netlink_callback *cb,
3537 struct net_device *dev,
3538 int *idx,
3539 struct netdev_hw_addr_list *list)
3540{
3541 struct netdev_hw_addr *ha;
3542 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00003543 u32 portid, seq;
John Fastabendd83b0602012-04-15 06:44:08 +00003544
Eric W. Biederman15e47302012-09-07 20:12:54 +00003545 portid = NETLINK_CB(cb->skb).portid;
John Fastabendd83b0602012-04-15 06:44:08 +00003546 seq = cb->nlh->nlmsg_seq;
3547
3548 list_for_each_entry(ha, &list->list, list) {
Roopa Prabhud2976532016-08-30 21:56:45 -07003549 if (*idx < cb->args[2])
John Fastabendd83b0602012-04-15 06:44:08 +00003550 goto skip;
3551
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003552 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
John Fastabenda7a558f2012-11-01 16:23:10 +00003553 portid, seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003554 RTM_NEWNEIGH, NTF_SELF,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003555 NLM_F_MULTI, NUD_PERMANENT);
John Fastabendd83b0602012-04-15 06:44:08 +00003556 if (err < 0)
3557 return err;
3558skip:
3559 *idx += 1;
3560 }
3561 return 0;
3562}
3563
3564/**
Ben Hutchings2c530402012-07-10 10:55:09 +00003565 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
John Fastabendd83b0602012-04-15 06:44:08 +00003566 * @nlh: netlink message header
3567 * @dev: netdevice
3568 *
3569 * Default netdevice operation to dump the existing unicast address list.
John Fastabend91f3e7b2013-03-29 08:18:37 +00003570 * Returns number of addresses from list put in skb.
John Fastabendd83b0602012-04-15 06:44:08 +00003571 */
3572int ndo_dflt_fdb_dump(struct sk_buff *skb,
3573 struct netlink_callback *cb,
3574 struct net_device *dev,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04003575 struct net_device *filter_dev,
Roopa Prabhud2976532016-08-30 21:56:45 -07003576 int *idx)
John Fastabendd83b0602012-04-15 06:44:08 +00003577{
3578 int err;
3579
3580 netif_addr_lock_bh(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -07003581 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
John Fastabendd83b0602012-04-15 06:44:08 +00003582 if (err)
3583 goto out;
Zhang Shengju2934c9d2016-11-30 16:37:34 +08003584 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
John Fastabendd83b0602012-04-15 06:44:08 +00003585out:
3586 netif_addr_unlock_bh(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -07003587 return err;
John Fastabendd83b0602012-04-15 06:44:08 +00003588}
3589EXPORT_SYMBOL(ndo_dflt_fdb_dump);
3590
John Fastabend77162022012-04-15 06:43:56 +00003591static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
3592{
John Fastabend77162022012-04-15 06:43:56 +00003593 struct net_device *dev;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003594 struct nlattr *tb[IFLA_MAX+1];
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003595 struct net_device *br_dev = NULL;
3596 const struct net_device_ops *ops = NULL;
3597 const struct net_device_ops *cops = NULL;
3598 struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
3599 struct net *net = sock_net(skb->sk);
Roopa Prabhud2976532016-08-30 21:56:45 -07003600 struct hlist_head *head;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003601 int brport_idx = 0;
3602 int br_idx = 0;
Roopa Prabhud2976532016-08-30 21:56:45 -07003603 int h, s_h;
3604 int idx = 0, s_idx;
3605 int err = 0;
3606 int fidx = 0;
John Fastabend77162022012-04-15 06:43:56 +00003607
Alexander Potapenko0ff50e82017-05-23 13:20:28 +02003608 err = nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb,
3609 IFLA_MAX, ifla_policy, NULL);
3610 if (err < 0) {
3611 return -EINVAL;
3612 } else if (err == 0) {
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003613 if (tb[IFLA_MASTER])
3614 br_idx = nla_get_u32(tb[IFLA_MASTER]);
3615 }
John Fastabend77162022012-04-15 06:43:56 +00003616
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003617 brport_idx = ifm->ifi_index;
3618
3619 if (br_idx) {
3620 br_dev = __dev_get_by_index(net, br_idx);
3621 if (!br_dev)
3622 return -ENODEV;
3623
3624 ops = br_dev->netdev_ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003625 }
3626
Roopa Prabhud2976532016-08-30 21:56:45 -07003627 s_h = cb->args[0];
3628 s_idx = cb->args[1];
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003629
Roopa Prabhud2976532016-08-30 21:56:45 -07003630 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
3631 idx = 0;
3632 head = &net->dev_index_head[h];
3633 hlist_for_each_entry(dev, head, index_hlist) {
3634
3635 if (brport_idx && (dev->ifindex != brport_idx))
3636 continue;
3637
3638 if (!br_idx) { /* user did not specify a specific bridge */
3639 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3640 br_dev = netdev_master_upper_dev_get(dev);
3641 cops = br_dev->netdev_ops;
3642 }
3643 } else {
3644 if (dev != br_dev &&
3645 !(dev->priv_flags & IFF_BRIDGE_PORT))
3646 continue;
3647
3648 if (br_dev != netdev_master_upper_dev_get(dev) &&
3649 !(dev->priv_flags & IFF_EBRIDGE))
3650 continue;
3651 cops = ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003652 }
3653
Roopa Prabhud2976532016-08-30 21:56:45 -07003654 if (idx < s_idx)
3655 goto cont;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003656
Roopa Prabhud2976532016-08-30 21:56:45 -07003657 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3658 if (cops && cops->ndo_fdb_dump) {
3659 err = cops->ndo_fdb_dump(skb, cb,
3660 br_dev, dev,
3661 &fidx);
3662 if (err == -EMSGSIZE)
3663 goto out;
3664 }
3665 }
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003666
Roopa Prabhud2976532016-08-30 21:56:45 -07003667 if (dev->netdev_ops->ndo_fdb_dump)
3668 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
3669 dev, NULL,
3670 &fidx);
3671 else
3672 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
3673 &fidx);
3674 if (err == -EMSGSIZE)
3675 goto out;
3676
3677 cops = NULL;
3678
3679 /* reset fdb offset to 0 for rest of the interfaces */
3680 cb->args[2] = 0;
3681 fidx = 0;
3682cont:
3683 idx++;
John Fastabend77162022012-04-15 06:43:56 +00003684 }
John Fastabend77162022012-04-15 06:43:56 +00003685 }
John Fastabend77162022012-04-15 06:43:56 +00003686
Roopa Prabhud2976532016-08-30 21:56:45 -07003687out:
3688 cb->args[0] = h;
3689 cb->args[1] = idx;
3690 cb->args[2] = fidx;
3691
John Fastabend77162022012-04-15 06:43:56 +00003692 return skb->len;
3693}
3694
Scott Feldman2c3c0312014-11-28 14:34:25 +01003695static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
3696 unsigned int attrnum, unsigned int flag)
3697{
3698 if (mask & flag)
3699 return nla_put_u8(skb, attrnum, !!(flags & flag));
3700 return 0;
3701}
3702
John Fastabend815cccb2012-10-24 08:13:09 +00003703int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Scott Feldman2c3c0312014-11-28 14:34:25 +01003704 struct net_device *dev, u16 mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003705 u32 flags, u32 mask, int nlflags,
3706 u32 filter_mask,
3707 int (*vlan_fill)(struct sk_buff *skb,
3708 struct net_device *dev,
3709 u32 filter_mask))
John Fastabend815cccb2012-10-24 08:13:09 +00003710{
3711 struct nlmsghdr *nlh;
3712 struct ifinfomsg *ifm;
3713 struct nlattr *br_afspec;
Scott Feldman2c3c0312014-11-28 14:34:25 +01003714 struct nlattr *protinfo;
John Fastabend815cccb2012-10-24 08:13:09 +00003715 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Jiri Pirko898e5062013-01-03 22:48:52 +00003716 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003717 int err = 0;
John Fastabend815cccb2012-10-24 08:13:09 +00003718
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02003719 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
John Fastabend815cccb2012-10-24 08:13:09 +00003720 if (nlh == NULL)
3721 return -EMSGSIZE;
3722
3723 ifm = nlmsg_data(nlh);
3724 ifm->ifi_family = AF_BRIDGE;
3725 ifm->__ifi_pad = 0;
3726 ifm->ifi_type = dev->type;
3727 ifm->ifi_index = dev->ifindex;
3728 ifm->ifi_flags = dev_get_flags(dev);
3729 ifm->ifi_change = 0;
3730
3731
3732 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
3733 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
3734 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00003735 (br_dev &&
3736 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
John Fastabend815cccb2012-10-24 08:13:09 +00003737 (dev->addr_len &&
3738 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02003739 (dev->ifindex != dev_get_iflink(dev) &&
3740 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
John Fastabend815cccb2012-10-24 08:13:09 +00003741 goto nla_put_failure;
3742
3743 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
3744 if (!br_afspec)
3745 goto nla_put_failure;
3746
Roopa Prabhu1d460b92014-12-08 14:04:20 -08003747 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
John Fastabend815cccb2012-10-24 08:13:09 +00003748 nla_nest_cancel(skb, br_afspec);
3749 goto nla_put_failure;
3750 }
Roopa Prabhu1d460b92014-12-08 14:04:20 -08003751
3752 if (mode != BRIDGE_MODE_UNDEF) {
3753 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
3754 nla_nest_cancel(skb, br_afspec);
3755 goto nla_put_failure;
3756 }
3757 }
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003758 if (vlan_fill) {
3759 err = vlan_fill(skb, dev, filter_mask);
3760 if (err) {
3761 nla_nest_cancel(skb, br_afspec);
3762 goto nla_put_failure;
3763 }
3764 }
John Fastabend815cccb2012-10-24 08:13:09 +00003765 nla_nest_end(skb, br_afspec);
3766
Scott Feldman2c3c0312014-11-28 14:34:25 +01003767 protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
3768 if (!protinfo)
3769 goto nla_put_failure;
3770
3771 if (brport_nla_put_flag(skb, flags, mask,
3772 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
3773 brport_nla_put_flag(skb, flags, mask,
3774 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
3775 brport_nla_put_flag(skb, flags, mask,
3776 IFLA_BRPORT_FAST_LEAVE,
3777 BR_MULTICAST_FAST_LEAVE) ||
3778 brport_nla_put_flag(skb, flags, mask,
3779 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
3780 brport_nla_put_flag(skb, flags, mask,
3781 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
3782 brport_nla_put_flag(skb, flags, mask,
3783 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
3784 brport_nla_put_flag(skb, flags, mask,
3785 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
3786 brport_nla_put_flag(skb, flags, mask,
3787 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
3788 nla_nest_cancel(skb, protinfo);
3789 goto nla_put_failure;
3790 }
3791
3792 nla_nest_end(skb, protinfo);
3793
Johannes Berg053c0952015-01-16 22:09:00 +01003794 nlmsg_end(skb, nlh);
3795 return 0;
John Fastabend815cccb2012-10-24 08:13:09 +00003796nla_put_failure:
3797 nlmsg_cancel(skb, nlh);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003798 return err ? err : -EMSGSIZE;
John Fastabend815cccb2012-10-24 08:13:09 +00003799}
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003800EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
John Fastabend815cccb2012-10-24 08:13:09 +00003801
John Fastabende5a55a82012-10-24 08:12:57 +00003802static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
3803{
3804 struct net *net = sock_net(skb->sk);
3805 struct net_device *dev;
3806 int idx = 0;
3807 u32 portid = NETLINK_CB(cb->skb).portid;
3808 u32 seq = cb->nlh->nlmsg_seq;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00003809 u32 filter_mask = 0;
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003810 int err;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00003811
Thomas Grafaa68c202014-11-26 13:42:20 +01003812 if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
3813 struct nlattr *extfilt;
3814
3815 extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
3816 IFLA_EXT_MASK);
3817 if (extfilt) {
3818 if (nla_len(extfilt) < sizeof(filter_mask))
3819 return -EINVAL;
3820
3821 filter_mask = nla_get_u32(extfilt);
3822 }
3823 }
John Fastabende5a55a82012-10-24 08:12:57 +00003824
3825 rcu_read_lock();
3826 for_each_netdev_rcu(net, dev) {
3827 const struct net_device_ops *ops = dev->netdev_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00003828 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
John Fastabende5a55a82012-10-24 08:12:57 +00003829
Jiri Pirko898e5062013-01-03 22:48:52 +00003830 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003831 if (idx >= cb->args[0]) {
3832 err = br_dev->netdev_ops->ndo_bridge_getlink(
3833 skb, portid, seq, dev,
3834 filter_mask, NLM_F_MULTI);
David Ahernf6c57752017-05-15 23:19:17 -07003835 if (err < 0 && err != -EOPNOTSUPP) {
3836 if (likely(skb->len))
3837 break;
3838
3839 goto out_err;
3840 }
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003841 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00003842 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00003843 }
3844
3845 if (ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003846 if (idx >= cb->args[0]) {
3847 err = ops->ndo_bridge_getlink(skb, portid,
3848 seq, dev,
3849 filter_mask,
3850 NLM_F_MULTI);
David Ahernf6c57752017-05-15 23:19:17 -07003851 if (err < 0 && err != -EOPNOTSUPP) {
3852 if (likely(skb->len))
3853 break;
3854
3855 goto out_err;
3856 }
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003857 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00003858 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00003859 }
3860 }
David Ahernf6c57752017-05-15 23:19:17 -07003861 err = skb->len;
3862out_err:
John Fastabende5a55a82012-10-24 08:12:57 +00003863 rcu_read_unlock();
3864 cb->args[0] = idx;
3865
David Ahernf6c57752017-05-15 23:19:17 -07003866 return err;
John Fastabende5a55a82012-10-24 08:12:57 +00003867}
3868
John Fastabend2469ffd2012-10-24 08:13:03 +00003869static inline size_t bridge_nlmsg_size(void)
3870{
3871 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3872 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3873 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3874 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
3875 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
3876 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
3877 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
3878 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
3879 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
3880 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
3881 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
3882}
3883
Roopa Prabhu02dba432015-01-14 20:02:25 -08003884static int rtnl_bridge_notify(struct net_device *dev)
John Fastabend2469ffd2012-10-24 08:13:03 +00003885{
3886 struct net *net = dev_net(dev);
John Fastabend2469ffd2012-10-24 08:13:03 +00003887 struct sk_buff *skb;
3888 int err = -EOPNOTSUPP;
3889
Roopa Prabhu02dba432015-01-14 20:02:25 -08003890 if (!dev->netdev_ops->ndo_bridge_getlink)
3891 return 0;
3892
John Fastabend2469ffd2012-10-24 08:13:03 +00003893 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
3894 if (!skb) {
3895 err = -ENOMEM;
3896 goto errout;
3897 }
3898
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02003899 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
Roopa Prabhu02dba432015-01-14 20:02:25 -08003900 if (err < 0)
3901 goto errout;
John Fastabend2469ffd2012-10-24 08:13:03 +00003902
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08003903 if (!skb->len)
3904 goto errout;
3905
John Fastabend2469ffd2012-10-24 08:13:03 +00003906 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
3907 return 0;
3908errout:
3909 WARN_ON(err == -EMSGSIZE);
3910 kfree_skb(skb);
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08003911 if (err)
3912 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
John Fastabend2469ffd2012-10-24 08:13:03 +00003913 return err;
3914}
3915
David Ahernc21ef3e2017-04-16 09:48:24 -07003916static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3917 struct netlink_ext_ack *extack)
John Fastabende5a55a82012-10-24 08:12:57 +00003918{
3919 struct net *net = sock_net(skb->sk);
3920 struct ifinfomsg *ifm;
3921 struct net_device *dev;
John Fastabend2469ffd2012-10-24 08:13:03 +00003922 struct nlattr *br_spec, *attr = NULL;
3923 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02003924 u16 flags = 0;
John Fastabendc38e01b2012-11-02 16:32:36 +00003925 bool have_flags = false;
John Fastabende5a55a82012-10-24 08:12:57 +00003926
3927 if (nlmsg_len(nlh) < sizeof(*ifm))
3928 return -EINVAL;
3929
3930 ifm = nlmsg_data(nlh);
3931 if (ifm->ifi_family != AF_BRIDGE)
3932 return -EPFNOSUPPORT;
3933
3934 dev = __dev_get_by_index(net, ifm->ifi_index);
3935 if (!dev) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003936 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabende5a55a82012-10-24 08:12:57 +00003937 return -ENODEV;
3938 }
3939
John Fastabend2469ffd2012-10-24 08:13:03 +00003940 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3941 if (br_spec) {
3942 nla_for_each_nested(attr, br_spec, rem) {
3943 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01003944 if (nla_len(attr) < sizeof(flags))
3945 return -EINVAL;
3946
John Fastabendc38e01b2012-11-02 16:32:36 +00003947 have_flags = true;
John Fastabend2469ffd2012-10-24 08:13:03 +00003948 flags = nla_get_u16(attr);
3949 break;
3950 }
3951 }
3952 }
3953
3954 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003955 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3956
3957 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
John Fastabend2469ffd2012-10-24 08:13:03 +00003958 err = -EOPNOTSUPP;
3959 goto out;
3960 }
3961
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003962 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
John Fastabende5a55a82012-10-24 08:12:57 +00003963 if (err)
3964 goto out;
John Fastabend2469ffd2012-10-24 08:13:03 +00003965
3966 flags &= ~BRIDGE_FLAGS_MASTER;
John Fastabende5a55a82012-10-24 08:12:57 +00003967 }
3968
John Fastabend2469ffd2012-10-24 08:13:03 +00003969 if ((flags & BRIDGE_FLAGS_SELF)) {
3970 if (!dev->netdev_ops->ndo_bridge_setlink)
3971 err = -EOPNOTSUPP;
3972 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003973 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
3974 flags);
Roopa Prabhu02dba432015-01-14 20:02:25 -08003975 if (!err) {
John Fastabend2469ffd2012-10-24 08:13:03 +00003976 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08003977
3978 /* Generate event to notify upper layer of bridge
3979 * change
3980 */
3981 err = rtnl_bridge_notify(dev);
3982 }
John Fastabend2469ffd2012-10-24 08:13:03 +00003983 }
3984
John Fastabendc38e01b2012-11-02 16:32:36 +00003985 if (have_flags)
John Fastabend2469ffd2012-10-24 08:13:03 +00003986 memcpy(nla_data(attr), &flags, sizeof(flags));
John Fastabende5a55a82012-10-24 08:12:57 +00003987out:
3988 return err;
3989}
3990
David Ahernc21ef3e2017-04-16 09:48:24 -07003991static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
3992 struct netlink_ext_ack *extack)
Vlad Yasevich407af322013-02-13 12:00:12 +00003993{
3994 struct net *net = sock_net(skb->sk);
3995 struct ifinfomsg *ifm;
3996 struct net_device *dev;
3997 struct nlattr *br_spec, *attr = NULL;
3998 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02003999 u16 flags = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +00004000 bool have_flags = false;
4001
4002 if (nlmsg_len(nlh) < sizeof(*ifm))
4003 return -EINVAL;
4004
4005 ifm = nlmsg_data(nlh);
4006 if (ifm->ifi_family != AF_BRIDGE)
4007 return -EPFNOSUPPORT;
4008
4009 dev = __dev_get_by_index(net, ifm->ifi_index);
4010 if (!dev) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004011 NL_SET_ERR_MSG(extack, "unknown ifindex");
Vlad Yasevich407af322013-02-13 12:00:12 +00004012 return -ENODEV;
4013 }
4014
4015 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4016 if (br_spec) {
4017 nla_for_each_nested(attr, br_spec, rem) {
4018 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01004019 if (nla_len(attr) < sizeof(flags))
4020 return -EINVAL;
4021
Vlad Yasevich407af322013-02-13 12:00:12 +00004022 have_flags = true;
4023 flags = nla_get_u16(attr);
4024 break;
4025 }
4026 }
4027 }
4028
Vlad Yasevich407af322013-02-13 12:00:12 +00004029 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
4030 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4031
4032 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
4033 err = -EOPNOTSUPP;
4034 goto out;
4035 }
4036
Roopa Prabhuadd511b2015-01-29 22:40:12 -08004037 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00004038 if (err)
4039 goto out;
4040
4041 flags &= ~BRIDGE_FLAGS_MASTER;
4042 }
4043
4044 if ((flags & BRIDGE_FLAGS_SELF)) {
4045 if (!dev->netdev_ops->ndo_bridge_dellink)
4046 err = -EOPNOTSUPP;
4047 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08004048 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
4049 flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00004050
Roopa Prabhu02dba432015-01-14 20:02:25 -08004051 if (!err) {
Vlad Yasevich407af322013-02-13 12:00:12 +00004052 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08004053
4054 /* Generate event to notify upper layer of bridge
4055 * change
4056 */
4057 err = rtnl_bridge_notify(dev);
4058 }
Vlad Yasevich407af322013-02-13 12:00:12 +00004059 }
4060
4061 if (have_flags)
4062 memcpy(nla_data(attr), &flags, sizeof(flags));
Vlad Yasevich407af322013-02-13 12:00:12 +00004063out:
4064 return err;
4065}
4066
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004067static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
4068{
4069 return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
4070 (!idxattr || idxattr == attrid);
4071}
4072
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004073#define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
4074static int rtnl_get_offload_stats_attr_size(int attr_id)
4075{
4076 switch (attr_id) {
4077 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
4078 return sizeof(struct rtnl_link_stats64);
4079 }
4080
4081 return 0;
4082}
4083
4084static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
4085 int *prividx)
4086{
4087 struct nlattr *attr = NULL;
4088 int attr_id, size;
4089 void *attr_data;
4090 int err;
4091
4092 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
4093 dev->netdev_ops->ndo_get_offload_stats))
4094 return -ENODATA;
4095
4096 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
4097 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
4098 if (attr_id < *prividx)
4099 continue;
4100
4101 size = rtnl_get_offload_stats_attr_size(attr_id);
4102 if (!size)
4103 continue;
4104
Or Gerlitz3df5b3c2016-11-22 23:09:54 +02004105 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004106 continue;
4107
4108 attr = nla_reserve_64bit(skb, attr_id, size,
4109 IFLA_OFFLOAD_XSTATS_UNSPEC);
4110 if (!attr)
4111 goto nla_put_failure;
4112
4113 attr_data = nla_data(attr);
4114 memset(attr_data, 0, size);
4115 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
4116 attr_data);
4117 if (err)
4118 goto get_offload_stats_failure;
4119 }
4120
4121 if (!attr)
4122 return -ENODATA;
4123
4124 *prividx = 0;
4125 return 0;
4126
4127nla_put_failure:
4128 err = -EMSGSIZE;
4129get_offload_stats_failure:
4130 *prividx = attr_id;
4131 return err;
4132}
4133
4134static int rtnl_get_offload_stats_size(const struct net_device *dev)
4135{
4136 int nla_size = 0;
4137 int attr_id;
4138 int size;
4139
4140 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
4141 dev->netdev_ops->ndo_get_offload_stats))
4142 return 0;
4143
4144 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
4145 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
Or Gerlitz3df5b3c2016-11-22 23:09:54 +02004146 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004147 continue;
4148 size = rtnl_get_offload_stats_attr_size(attr_id);
4149 nla_size += nla_total_size_64bit(size);
4150 }
4151
4152 if (nla_size != 0)
4153 nla_size += nla_total_size(0);
4154
4155 return nla_size;
4156}
4157
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004158static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
4159 int type, u32 pid, u32 seq, u32 change,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004160 unsigned int flags, unsigned int filter_mask,
4161 int *idxattr, int *prividx)
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004162{
4163 struct if_stats_msg *ifsm;
4164 struct nlmsghdr *nlh;
4165 struct nlattr *attr;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004166 int s_prividx = *prividx;
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004167 int err;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004168
4169 ASSERT_RTNL();
4170
4171 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
4172 if (!nlh)
4173 return -EMSGSIZE;
4174
4175 ifsm = nlmsg_data(nlh);
Nikolay Aleksandrovce024f42017-10-03 13:20:48 +03004176 ifsm->family = PF_UNSPEC;
4177 ifsm->pad1 = 0;
4178 ifsm->pad2 = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004179 ifsm->ifindex = dev->ifindex;
4180 ifsm->filter_mask = filter_mask;
4181
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004182 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004183 struct rtnl_link_stats64 *sp;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004184
Nicolas Dichtel58414d32016-04-21 18:58:25 +02004185 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
4186 sizeof(struct rtnl_link_stats64),
4187 IFLA_STATS_UNSPEC);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004188 if (!attr)
4189 goto nla_put_failure;
4190
4191 sp = nla_data(attr);
4192 dev_get_stats(dev, sp);
4193 }
4194
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004195 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
4196 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
4197
4198 if (ops && ops->fill_linkxstats) {
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004199 *idxattr = IFLA_STATS_LINK_XSTATS;
4200 attr = nla_nest_start(skb,
4201 IFLA_STATS_LINK_XSTATS);
4202 if (!attr)
4203 goto nla_put_failure;
4204
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004205 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
4206 nla_nest_end(skb, attr);
4207 if (err)
4208 goto nla_put_failure;
4209 *idxattr = 0;
4210 }
4211 }
4212
4213 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
4214 *idxattr)) {
4215 const struct rtnl_link_ops *ops = NULL;
4216 const struct net_device *master;
4217
4218 master = netdev_master_upper_dev_get(dev);
4219 if (master)
4220 ops = master->rtnl_link_ops;
4221 if (ops && ops->fill_linkxstats) {
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004222 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
4223 attr = nla_nest_start(skb,
4224 IFLA_STATS_LINK_XSTATS_SLAVE);
4225 if (!attr)
4226 goto nla_put_failure;
4227
4228 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004229 nla_nest_end(skb, attr);
4230 if (err)
4231 goto nla_put_failure;
4232 *idxattr = 0;
4233 }
4234 }
4235
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004236 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
4237 *idxattr)) {
4238 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
4239 attr = nla_nest_start(skb, IFLA_STATS_LINK_OFFLOAD_XSTATS);
4240 if (!attr)
4241 goto nla_put_failure;
4242
4243 err = rtnl_get_offload_stats(skb, dev, prividx);
4244 if (err == -ENODATA)
4245 nla_nest_cancel(skb, attr);
4246 else
4247 nla_nest_end(skb, attr);
4248
4249 if (err && err != -ENODATA)
4250 goto nla_put_failure;
4251 *idxattr = 0;
4252 }
4253
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004254 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
4255 struct rtnl_af_ops *af_ops;
4256
4257 *idxattr = IFLA_STATS_AF_SPEC;
4258 attr = nla_nest_start(skb, IFLA_STATS_AF_SPEC);
4259 if (!attr)
4260 goto nla_put_failure;
4261
Florian Westphal5fa85a02017-10-16 15:44:36 +02004262 rcu_read_lock();
4263 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004264 if (af_ops->fill_stats_af) {
4265 struct nlattr *af;
4266 int err;
4267
4268 af = nla_nest_start(skb, af_ops->family);
Florian Westphal5fa85a02017-10-16 15:44:36 +02004269 if (!af) {
4270 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004271 goto nla_put_failure;
Florian Westphal5fa85a02017-10-16 15:44:36 +02004272 }
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004273 err = af_ops->fill_stats_af(skb, dev);
4274
Florian Westphal5fa85a02017-10-16 15:44:36 +02004275 if (err == -ENODATA) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004276 nla_nest_cancel(skb, af);
Florian Westphal5fa85a02017-10-16 15:44:36 +02004277 } else if (err < 0) {
4278 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004279 goto nla_put_failure;
Florian Westphal5fa85a02017-10-16 15:44:36 +02004280 }
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004281
4282 nla_nest_end(skb, af);
4283 }
4284 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02004285 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004286
4287 nla_nest_end(skb, attr);
4288
4289 *idxattr = 0;
4290 }
4291
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004292 nlmsg_end(skb, nlh);
4293
4294 return 0;
4295
4296nla_put_failure:
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004297 /* not a multi message or no progress mean a real error */
4298 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
4299 nlmsg_cancel(skb, nlh);
4300 else
4301 nlmsg_end(skb, nlh);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004302
4303 return -EMSGSIZE;
4304}
4305
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004306static size_t if_nlmsg_stats_size(const struct net_device *dev,
4307 u32 filter_mask)
4308{
4309 size_t size = 0;
4310
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004311 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004312 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
4313
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004314 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
4315 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004316 int attr = IFLA_STATS_LINK_XSTATS;
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004317
4318 if (ops && ops->get_linkxstats_size) {
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004319 size += nla_total_size(ops->get_linkxstats_size(dev,
4320 attr));
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004321 /* for IFLA_STATS_LINK_XSTATS */
4322 size += nla_total_size(0);
4323 }
4324 }
4325
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004326 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
4327 struct net_device *_dev = (struct net_device *)dev;
4328 const struct rtnl_link_ops *ops = NULL;
4329 const struct net_device *master;
4330
4331 /* netdev_master_upper_dev_get can't take const */
4332 master = netdev_master_upper_dev_get(_dev);
4333 if (master)
4334 ops = master->rtnl_link_ops;
4335 if (ops && ops->get_linkxstats_size) {
4336 int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
4337
4338 size += nla_total_size(ops->get_linkxstats_size(dev,
4339 attr));
4340 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
4341 size += nla_total_size(0);
4342 }
4343 }
4344
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004345 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
4346 size += rtnl_get_offload_stats_size(dev);
4347
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004348 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
4349 struct rtnl_af_ops *af_ops;
4350
4351 /* for IFLA_STATS_AF_SPEC */
4352 size += nla_total_size(0);
4353
Florian Westphal5fa85a02017-10-16 15:44:36 +02004354 rcu_read_lock();
4355 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004356 if (af_ops->get_stats_af_size) {
4357 size += nla_total_size(
4358 af_ops->get_stats_af_size(dev));
4359
4360 /* for AF_* */
4361 size += nla_total_size(0);
4362 }
4363 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02004364 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004365 }
4366
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004367 return size;
4368}
4369
David Ahernc21ef3e2017-04-16 09:48:24 -07004370static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
4371 struct netlink_ext_ack *extack)
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004372{
4373 struct net *net = sock_net(skb->sk);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004374 struct net_device *dev = NULL;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004375 int idxattr = 0, prividx = 0;
4376 struct if_stats_msg *ifsm;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004377 struct sk_buff *nskb;
4378 u32 filter_mask;
4379 int err;
4380
Mathias Krause4775cc12016-12-28 17:52:15 +01004381 if (nlmsg_len(nlh) < sizeof(*ifsm))
4382 return -EINVAL;
4383
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004384 ifsm = nlmsg_data(nlh);
4385 if (ifsm->ifindex > 0)
4386 dev = __dev_get_by_index(net, ifsm->ifindex);
4387 else
4388 return -EINVAL;
4389
4390 if (!dev)
4391 return -ENODEV;
4392
4393 filter_mask = ifsm->filter_mask;
4394 if (!filter_mask)
4395 return -EINVAL;
4396
4397 nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
4398 if (!nskb)
4399 return -ENOBUFS;
4400
4401 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
4402 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004403 0, filter_mask, &idxattr, &prividx);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004404 if (err < 0) {
4405 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
4406 WARN_ON(err == -EMSGSIZE);
4407 kfree_skb(nskb);
4408 } else {
4409 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
4410 }
4411
4412 return err;
4413}
4414
4415static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
4416{
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004417 int h, s_h, err, s_idx, s_idxattr, s_prividx;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004418 struct net *net = sock_net(skb->sk);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004419 unsigned int flags = NLM_F_MULTI;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004420 struct if_stats_msg *ifsm;
4421 struct hlist_head *head;
4422 struct net_device *dev;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004423 u32 filter_mask = 0;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004424 int idx = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004425
4426 s_h = cb->args[0];
4427 s_idx = cb->args[1];
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004428 s_idxattr = cb->args[2];
4429 s_prividx = cb->args[3];
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004430
4431 cb->seq = net->dev_base_seq;
4432
Mathias Krause4775cc12016-12-28 17:52:15 +01004433 if (nlmsg_len(cb->nlh) < sizeof(*ifsm))
4434 return -EINVAL;
4435
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004436 ifsm = nlmsg_data(cb->nlh);
4437 filter_mask = ifsm->filter_mask;
4438 if (!filter_mask)
4439 return -EINVAL;
4440
4441 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4442 idx = 0;
4443 head = &net->dev_index_head[h];
4444 hlist_for_each_entry(dev, head, index_hlist) {
4445 if (idx < s_idx)
4446 goto cont;
4447 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
4448 NETLINK_CB(cb->skb).portid,
4449 cb->nlh->nlmsg_seq, 0,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004450 flags, filter_mask,
4451 &s_idxattr, &s_prividx);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004452 /* If we ran out of room on the first message,
4453 * we're in trouble
4454 */
4455 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
4456
4457 if (err < 0)
4458 goto out;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004459 s_prividx = 0;
4460 s_idxattr = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004461 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
4462cont:
4463 idx++;
4464 }
4465 }
4466out:
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004467 cb->args[3] = s_prividx;
4468 cb->args[2] = s_idxattr;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004469 cb->args[1] = idx;
4470 cb->args[0] = h;
4471
4472 return skb->len;
4473}
4474
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475/* Process one rtnetlink message. */
4476
Johannes Berg2d4bc932017-04-12 14:34:04 +02004477static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
4478 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004480 struct net *net = sock_net(skb->sk);
Florian Westphaladdf9b92017-12-02 21:44:05 +01004481 struct rtnl_link *link;
Florian Westphale4202512017-12-02 21:44:06 +01004482 struct module *owner;
Florian Westphal6853dd42017-08-09 20:41:51 +02004483 int err = -EOPNOTSUPP;
Thomas Grafe2849862007-03-22 11:48:11 -07004484 rtnl_doit_func doit;
Florian Westphal62256f92017-08-09 20:41:52 +02004485 unsigned int flags;
Alexander Kuleshov617cfc72016-01-10 21:26:57 +06004486 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 int family;
4488 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 if (type > RTM_MAX)
Thomas Graf038890f2007-04-05 14:35:52 -07004492 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493
4494 type -= RTM_BASE;
4495
4496 /* All the messages must have at least 1 byte length */
Hong zhi guo573ce262013-03-27 06:47:04 +00004497 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 return 0;
4499
Hong zhi guo573ce262013-03-27 06:47:04 +00004500 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 kind = type&3;
4502
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07004503 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07004504 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505
Florian Westphal6853dd42017-08-09 20:41:51 +02004506 rcu_read_lock();
David S. Millerb8f3ab42011-01-18 12:40:38 -08004507 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004508 struct sock *rtnl;
Thomas Grafe2849862007-03-22 11:48:11 -07004509 rtnl_dumpit_func dumpit;
Greg Rosec7ac8672011-06-10 01:27:09 +00004510 u16 min_dump_alloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511
Florian Westphaladdf9b92017-12-02 21:44:05 +01004512 link = rtnl_get_link(family, type);
4513 if (!link || !link->dumpit) {
Florian Westphal6853dd42017-08-09 20:41:51 +02004514 family = PF_UNSPEC;
Florian Westphaladdf9b92017-12-02 21:44:05 +01004515 link = rtnl_get_link(family, type);
4516 if (!link || !link->dumpit)
Florian Westphal6853dd42017-08-09 20:41:51 +02004517 goto err_unlock;
4518 }
Florian Westphale4202512017-12-02 21:44:06 +01004519 owner = link->owner;
Florian Westphaladdf9b92017-12-02 21:44:05 +01004520 dumpit = link->dumpit;
Florian Westphale1fa6d22017-08-09 20:41:47 +02004521
Florian Westphal5c2bb9b2017-08-10 16:52:58 +02004522 if (type == RTM_GETLINK - RTM_BASE)
Florian Westphale1fa6d22017-08-09 20:41:47 +02004523 min_dump_alloc = rtnl_calcit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524
Florian Westphale4202512017-12-02 21:44:06 +01004525 err = 0;
4526 /* need to do this before rcu_read_unlock() */
4527 if (!try_module_get(owner))
4528 err = -EPROTONOSUPPORT;
4529
Florian Westphal6853dd42017-08-09 20:41:51 +02004530 rcu_read_unlock();
4531
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004532 rtnl = net->rtnl;
Florian Westphale4202512017-12-02 21:44:06 +01004533 if (err == 0) {
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00004534 struct netlink_dump_control c = {
4535 .dump = dumpit,
4536 .min_dump_alloc = min_dump_alloc,
Florian Westphale4202512017-12-02 21:44:06 +01004537 .module = owner,
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00004538 };
4539 err = netlink_dump_start(rtnl, skb, nlh, &c);
Florian Westphale4202512017-12-02 21:44:06 +01004540 /* netlink_dump_start() will keep a reference on
4541 * module if dump is still in progress.
4542 */
4543 module_put(owner);
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00004544 }
Eric Dumazet2907c352011-05-25 07:34:04 +00004545 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 }
4547
Florian Westphaladdf9b92017-12-02 21:44:05 +01004548 link = rtnl_get_link(family, type);
4549 if (!link || !link->doit) {
Florian Westphal8caa38b2017-08-10 16:53:01 +02004550 family = PF_UNSPEC;
Florian Westphaladdf9b92017-12-02 21:44:05 +01004551 link = rtnl_get_link(PF_UNSPEC, type);
4552 if (!link || !link->doit)
4553 goto out_unlock;
Florian Westphal8caa38b2017-08-10 16:53:01 +02004554 }
4555
Florian Westphale4202512017-12-02 21:44:06 +01004556 owner = link->owner;
4557 if (!try_module_get(owner)) {
4558 err = -EPROTONOSUPPORT;
4559 goto out_unlock;
4560 }
4561
Florian Westphaladdf9b92017-12-02 21:44:05 +01004562 flags = link->flags;
Florian Westphal62256f92017-08-09 20:41:52 +02004563 if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
Florian Westphaladdf9b92017-12-02 21:44:05 +01004564 doit = link->doit;
Florian Westphal62256f92017-08-09 20:41:52 +02004565 rcu_read_unlock();
4566 if (doit)
4567 err = doit(skb, nlh, extack);
Florian Westphale4202512017-12-02 21:44:06 +01004568 module_put(owner);
Florian Westphal62256f92017-08-09 20:41:52 +02004569 return err;
4570 }
Florian Westphal6853dd42017-08-09 20:41:51 +02004571 rcu_read_unlock();
4572
Florian Westphal0cc09022017-08-09 20:41:50 +02004573 rtnl_lock();
Florian Westphaladdf9b92017-12-02 21:44:05 +01004574 link = rtnl_get_link(family, type);
4575 if (link && link->doit)
4576 err = link->doit(skb, nlh, extack);
Florian Westphal0cc09022017-08-09 20:41:50 +02004577 rtnl_unlock();
Florian Westphaladdf9b92017-12-02 21:44:05 +01004578
Florian Westphale4202512017-12-02 21:44:06 +01004579 module_put(owner);
4580
Florian Westphaladdf9b92017-12-02 21:44:05 +01004581 return err;
4582
4583out_unlock:
4584 rcu_read_unlock();
Florian Westphal0cc09022017-08-09 20:41:50 +02004585 return err;
4586
4587err_unlock:
Florian Westphal6853dd42017-08-09 20:41:51 +02004588 rcu_read_unlock();
Florian Westphal0cc09022017-08-09 20:41:50 +02004589 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590}
4591
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07004592static void rtnetlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07004594 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595}
4596
Julien Gomes5f729ea2017-06-20 13:54:16 -07004597static int rtnetlink_bind(struct net *net, int group)
4598{
4599 switch (group) {
4600 case RTNLGRP_IPV4_MROUTE_R:
4601 case RTNLGRP_IPV6_MROUTE_R:
4602 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
4603 return -EPERM;
4604 break;
4605 }
4606 return 0;
4607}
4608
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
4610{
Jiri Pirko351638e2013-05-28 01:30:21 +00004611 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02004612
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 switch (event) {
Vlad Yasevich5138e862017-04-04 09:23:41 -04004614 case NETDEV_REBOOT:
Xin Long8a212582017-10-15 18:13:41 +08004615 case NETDEV_CHANGEMTU:
David Ahern37536542017-07-19 10:22:40 -07004616 case NETDEV_CHANGEADDR:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004617 case NETDEV_CHANGENAME:
4618 case NETDEV_FEAT_CHANGE:
4619 case NETDEV_BONDING_FAILOVER:
Xin Longe6e66592017-10-15 18:13:43 +08004620 case NETDEV_POST_TYPE_CHANGE:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004621 case NETDEV_NOTIFY_PEERS:
Xin Longdc709f32017-10-15 18:13:44 +08004622 case NETDEV_CHANGEUPPER:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004623 case NETDEV_RESEND_IGMP:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004624 case NETDEV_CHANGEINFODATA:
Xin Longeeda3fb2017-10-24 13:54:19 +08004625 case NETDEV_CHANGELOWERSTATE:
Xin Longebdcf042017-10-15 18:13:42 +08004626 case NETDEV_CHANGE_TX_QUEUE_LEN:
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04004627 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02004628 GFP_KERNEL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 break;
4630 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 break;
4632 }
4633 return NOTIFY_DONE;
4634}
4635
4636static struct notifier_block rtnetlink_dev_notifier = {
4637 .notifier_call = rtnetlink_event,
4638};
4639
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004640
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004641static int __net_init rtnetlink_net_init(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004642{
4643 struct sock *sk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00004644 struct netlink_kernel_cfg cfg = {
4645 .groups = RTNLGRP_MAX,
4646 .input = rtnetlink_rcv,
4647 .cb_mutex = &rtnl_mutex,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00004648 .flags = NL_CFG_F_NONROOT_RECV,
Julien Gomes5f729ea2017-06-20 13:54:16 -07004649 .bind = rtnetlink_bind,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00004650 };
4651
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00004652 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004653 if (!sk)
4654 return -ENOMEM;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004655 net->rtnl = sk;
4656 return 0;
4657}
4658
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004659static void __net_exit rtnetlink_net_exit(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004660{
Denis V. Lunev775516b2008-01-18 23:55:19 -08004661 netlink_kernel_release(net->rtnl);
4662 net->rtnl = NULL;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004663}
4664
4665static struct pernet_operations rtnetlink_net_ops = {
4666 .init = rtnetlink_net_init,
4667 .exit = rtnetlink_net_exit,
4668};
4669
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670void __init rtnetlink_init(void)
4671{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004672 if (register_pernet_subsys(&rtnetlink_net_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 panic("rtnetlink_init: cannot initialize rtnetlink\n");
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004674
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 register_netdevice_notifier(&rtnetlink_dev_notifier);
Thomas Graf340d17f2007-03-22 11:49:22 -07004676
Greg Rosec7ac8672011-06-10 01:27:09 +00004677 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
Florian Westphalb97bac62017-08-09 20:41:48 +02004678 rtnl_dump_ifinfo, 0);
4679 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
4680 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
4681 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
Thomas Graf687ad8c2007-03-22 11:59:42 -07004682
Florian Westphalb97bac62017-08-09 20:41:48 +02004683 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
4684 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
4685 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
John Fastabend77162022012-04-15 06:43:56 +00004686
Florian Westphalb97bac62017-08-09 20:41:48 +02004687 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
4688 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
4689 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, 0);
John Fastabende5a55a82012-10-24 08:12:57 +00004690
Florian Westphalb97bac62017-08-09 20:41:48 +02004691 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
4692 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
4693 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004694
4695 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
Florian Westphalb97bac62017-08-09 20:41:48 +02004696 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697}