blob: 73011a60434c21ca982014abc48ab01a88b99955 [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
Kirill Tkhai79ffdfc2018-03-14 22:17:20 +030078int rtnl_lock_killable(void)
79{
80 return mutex_lock_killable(&rtnl_mutex);
81}
82EXPORT_SYMBOL(rtnl_lock_killable);
83
Eric Dumazet1b5c5492016-06-13 20:21:50 -070084static struct sk_buff *defer_kfree_skb_list;
85void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail)
86{
87 if (head && tail) {
88 tail->next = defer_kfree_skb_list;
89 defer_kfree_skb_list = head;
90 }
91}
92EXPORT_SYMBOL(rtnl_kfree_skbs);
93
Stephen Hemminger6756ae42006-03-20 22:23:58 -080094void __rtnl_unlock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Eric Dumazet1b5c5492016-06-13 20:21:50 -070096 struct sk_buff *head = defer_kfree_skb_list;
97
98 defer_kfree_skb_list = NULL;
99
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800100 mutex_unlock(&rtnl_mutex);
Eric Dumazet1b5c5492016-06-13 20:21:50 -0700101
102 while (head) {
103 struct sk_buff *next = head->next;
104
105 kfree_skb(head);
106 cond_resched();
107 head = next;
108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111void rtnl_unlock(void)
112{
Herbert Xu58ec3b42008-10-07 15:50:03 -0700113 /* This fellow will unlock it for us. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 netdev_run_todo();
115}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800116EXPORT_SYMBOL(rtnl_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800118int rtnl_trylock(void)
119{
120 return mutex_trylock(&rtnl_mutex);
121}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800122EXPORT_SYMBOL(rtnl_trylock);
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800123
Patrick McHardyc9c10142008-04-23 22:10:48 -0700124int rtnl_is_locked(void)
125{
126 return mutex_is_locked(&rtnl_mutex);
127}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800128EXPORT_SYMBOL(rtnl_is_locked);
Patrick McHardyc9c10142008-04-23 22:10:48 -0700129
Paul E. McKenneya898def2010-02-22 17:04:49 -0800130#ifdef CONFIG_PROVE_LOCKING
Yaowei Bai0cbf3342015-10-08 21:29:02 +0800131bool lockdep_rtnl_is_held(void)
Paul E. McKenneya898def2010-02-22 17:04:49 -0800132{
133 return lockdep_is_held(&rtnl_mutex);
134}
135EXPORT_SYMBOL(lockdep_rtnl_is_held);
136#endif /* #ifdef CONFIG_PROVE_LOCKING */
137
Florian Westphalb0e9fe12017-12-04 22:42:30 +0100138static struct rtnl_link *__rcu *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
Thomas Grafe2849862007-03-22 11:48:11 -0700139
140static inline int rtm_msgindex(int msgtype)
141{
142 int msgindex = msgtype - RTM_BASE;
143
144 /*
145 * msgindex < 0 implies someone tried to register a netlink
146 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
147 * the message type has not been added to linux/rtnetlink.h
148 */
149 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
150
151 return msgindex;
152}
153
Florian Westphaladdf9b92017-12-02 21:44:05 +0100154static struct rtnl_link *rtnl_get_link(int protocol, int msgtype)
155{
156 struct rtnl_link **tab;
157
158 if (protocol >= ARRAY_SIZE(rtnl_msg_handlers))
159 protocol = PF_UNSPEC;
160
161 tab = rcu_dereference_rtnl(rtnl_msg_handlers[protocol]);
162 if (!tab)
163 tab = rcu_dereference_rtnl(rtnl_msg_handlers[PF_UNSPEC]);
164
165 return tab[msgtype];
166}
167
Florian Westphale4202512017-12-02 21:44:06 +0100168static int rtnl_register_internal(struct module *owner,
169 int protocol, int msgtype,
170 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
171 unsigned int flags)
Thomas Grafe2849862007-03-22 11:48:11 -0700172{
Florian Westphalb0e9fe12017-12-04 22:42:30 +0100173 struct rtnl_link *link, *old;
174 struct rtnl_link __rcu **tab;
Thomas Grafe2849862007-03-22 11:48:11 -0700175 int msgindex;
Florian Westphaladdf9b92017-12-02 21:44:05 +0100176 int ret = -ENOBUFS;
Thomas Grafe2849862007-03-22 11:48:11 -0700177
Patrick McHardy25239ce2010-04-26 16:02:05 +0200178 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700179 msgindex = rtm_msgindex(msgtype);
180
Florian Westphaladdf9b92017-12-02 21:44:05 +0100181 rtnl_lock();
182 tab = rtnl_msg_handlers[protocol];
Thomas Grafe2849862007-03-22 11:48:11 -0700183 if (tab == NULL) {
Florian Westphaladdf9b92017-12-02 21:44:05 +0100184 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(void *), GFP_KERNEL);
185 if (!tab)
186 goto unlock;
Thomas Grafe2849862007-03-22 11:48:11 -0700187
Florian Westphaladdf9b92017-12-02 21:44:05 +0100188 /* ensures we see the 0 stores */
Florian Westphal6853dd42017-08-09 20:41:51 +0200189 rcu_assign_pointer(rtnl_msg_handlers[protocol], tab);
Thomas Grafe2849862007-03-22 11:48:11 -0700190 }
191
Florian Westphaladdf9b92017-12-02 21:44:05 +0100192 old = rtnl_dereference(tab[msgindex]);
193 if (old) {
194 link = kmemdup(old, sizeof(*old), GFP_KERNEL);
195 if (!link)
196 goto unlock;
197 } else {
198 link = kzalloc(sizeof(*link), GFP_KERNEL);
199 if (!link)
200 goto unlock;
201 }
Thomas Grafe2849862007-03-22 11:48:11 -0700202
Florian Westphale4202512017-12-02 21:44:06 +0100203 WARN_ON(link->owner && link->owner != owner);
204 link->owner = owner;
205
Florian Westphaladdf9b92017-12-02 21:44:05 +0100206 WARN_ON(doit && link->doit && link->doit != doit);
207 if (doit)
208 link->doit = doit;
209 WARN_ON(dumpit && link->dumpit && link->dumpit != dumpit);
210 if (dumpit)
211 link->dumpit = dumpit;
212
213 link->flags |= flags;
214
215 /* publish protocol:msgtype */
216 rcu_assign_pointer(tab[msgindex], link);
217 ret = 0;
218 if (old)
219 kfree_rcu(old, rcu);
220unlock:
221 rtnl_unlock();
222 return ret;
Thomas Grafe2849862007-03-22 11:48:11 -0700223}
Florian Westphale4202512017-12-02 21:44:06 +0100224
225/**
226 * rtnl_register_module - Register a rtnetlink message type
227 *
228 * @owner: module registering the hook (THIS_MODULE)
229 * @protocol: Protocol family or PF_UNSPEC
230 * @msgtype: rtnetlink message type
231 * @doit: Function pointer called for each request message
232 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
233 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
234 *
235 * Like rtnl_register, but for use by removable modules.
236 */
237int rtnl_register_module(struct module *owner,
238 int protocol, int msgtype,
239 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
240 unsigned int flags)
241{
242 return rtnl_register_internal(owner, protocol, msgtype,
243 doit, dumpit, flags);
244}
245EXPORT_SYMBOL_GPL(rtnl_register_module);
246
247/**
Florian Westphal16feebc2017-12-02 21:44:08 +0100248 * rtnl_register - Register a rtnetlink message type
Florian Westphale4202512017-12-02 21:44:06 +0100249 * @protocol: Protocol family or PF_UNSPEC
250 * @msgtype: rtnetlink message type
251 * @doit: Function pointer called for each request message
252 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
253 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
254 *
255 * Registers the specified function pointers (at least one of them has
256 * to be non-NULL) to be called whenever a request message for the
257 * specified protocol family and message type is received.
258 *
259 * The special protocol family PF_UNSPEC may be used to define fallback
260 * function pointers for the case when no entry for the specific protocol
261 * family exists.
Thomas Grafe2849862007-03-22 11:48:11 -0700262 */
263void rtnl_register(int protocol, int msgtype,
Greg Rosec7ac8672011-06-10 01:27:09 +0000264 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
Florian Westphalb97bac62017-08-09 20:41:48 +0200265 unsigned int flags)
Thomas Grafe2849862007-03-22 11:48:11 -0700266{
Florian Westphal16feebc2017-12-02 21:44:08 +0100267 int err;
268
269 err = rtnl_register_internal(NULL, protocol, msgtype, doit, dumpit,
270 flags);
271 if (err)
272 pr_err("Unable to register rtnetlink message handler, "
273 "protocol = %d, message type = %d\n", protocol, msgtype);
Thomas Grafe2849862007-03-22 11:48:11 -0700274}
Thomas Grafe2849862007-03-22 11:48:11 -0700275
276/**
277 * rtnl_unregister - Unregister a rtnetlink message type
278 * @protocol: Protocol family or PF_UNSPEC
279 * @msgtype: rtnetlink message type
280 *
281 * Returns 0 on success or a negative error code.
282 */
283int rtnl_unregister(int protocol, int msgtype)
284{
Florian Westphaladdf9b92017-12-02 21:44:05 +0100285 struct rtnl_link **tab, *link;
Thomas Grafe2849862007-03-22 11:48:11 -0700286 int msgindex;
287
Patrick McHardy25239ce2010-04-26 16:02:05 +0200288 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700289 msgindex = rtm_msgindex(msgtype);
290
Florian Westphal6853dd42017-08-09 20:41:51 +0200291 rtnl_lock();
Florian Westphaladdf9b92017-12-02 21:44:05 +0100292 tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
293 if (!tab) {
Florian Westphal6853dd42017-08-09 20:41:51 +0200294 rtnl_unlock();
Thomas Grafe2849862007-03-22 11:48:11 -0700295 return -ENOENT;
Florian Westphal6853dd42017-08-09 20:41:51 +0200296 }
Thomas Grafe2849862007-03-22 11:48:11 -0700297
Florian Westphaladdf9b92017-12-02 21:44:05 +0100298 link = tab[msgindex];
299 rcu_assign_pointer(tab[msgindex], NULL);
Florian Westphal6853dd42017-08-09 20:41:51 +0200300 rtnl_unlock();
Thomas Grafe2849862007-03-22 11:48:11 -0700301
Florian Westphaladdf9b92017-12-02 21:44:05 +0100302 kfree_rcu(link, rcu);
303
Thomas Grafe2849862007-03-22 11:48:11 -0700304 return 0;
305}
Thomas Grafe2849862007-03-22 11:48:11 -0700306EXPORT_SYMBOL_GPL(rtnl_unregister);
307
308/**
309 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
310 * @protocol : Protocol family or PF_UNSPEC
311 *
312 * Identical to calling rtnl_unregster() for all registered message types
313 * of a certain protocol family.
314 */
315void rtnl_unregister_all(int protocol)
316{
Florian Westphaladdf9b92017-12-02 21:44:05 +0100317 struct rtnl_link **tab, *link;
318 int msgindex;
Florian Westphal019a3162017-08-09 20:41:49 +0200319
Patrick McHardy25239ce2010-04-26 16:02:05 +0200320 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700321
Florian Westphal019a3162017-08-09 20:41:49 +0200322 rtnl_lock();
Florian Westphaladdf9b92017-12-02 21:44:05 +0100323 tab = rtnl_msg_handlers[protocol];
Florian Westphal6853dd42017-08-09 20:41:51 +0200324 RCU_INIT_POINTER(rtnl_msg_handlers[protocol], NULL);
Florian Westphaladdf9b92017-12-02 21:44:05 +0100325 for (msgindex = 0; msgindex < RTM_NR_MSGTYPES; msgindex++) {
326 link = tab[msgindex];
327 if (!link)
328 continue;
329
330 rcu_assign_pointer(tab[msgindex], NULL);
331 kfree_rcu(link, rcu);
332 }
Florian Westphal019a3162017-08-09 20:41:49 +0200333 rtnl_unlock();
334
Florian Westphal6853dd42017-08-09 20:41:51 +0200335 synchronize_net();
336
Florian Westphaladdf9b92017-12-02 21:44:05 +0100337 kfree(tab);
Thomas Grafe2849862007-03-22 11:48:11 -0700338}
Thomas Grafe2849862007-03-22 11:48:11 -0700339EXPORT_SYMBOL_GPL(rtnl_unregister_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Patrick McHardy38f7b872007-06-13 12:03:51 -0700341static LIST_HEAD(link_ops);
342
Eric Dumazetc63044f2011-12-13 11:38:00 +0000343static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
344{
345 const struct rtnl_link_ops *ops;
346
347 list_for_each_entry(ops, &link_ops, list) {
348 if (!strcmp(ops->kind, kind))
349 return ops;
350 }
351 return NULL;
352}
353
Patrick McHardy38f7b872007-06-13 12:03:51 -0700354/**
355 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
356 * @ops: struct rtnl_link_ops * to register
357 *
358 * The caller must hold the rtnl_mutex. This function should be used
359 * by drivers that create devices during module initialization. It
360 * must be called before registering the devices.
361 *
362 * Returns 0 on success or a negative error code.
363 */
364int __rtnl_link_register(struct rtnl_link_ops *ops)
365{
Eric Dumazetc63044f2011-12-13 11:38:00 +0000366 if (rtnl_link_ops_get(ops->kind))
367 return -EEXIST;
368
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +0200369 /* The check for setup is here because if ops
370 * does not have that filled up, it is not possible
371 * to use the ops for creating device. So do not
372 * fill up dellink as well. That disables rtnl_dellink.
373 */
374 if (ops->setup && !ops->dellink)
Eric Dumazet23289a32009-10-27 07:06:36 +0000375 ops->dellink = unregister_netdevice_queue;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700376
Patrick McHardy38f7b872007-06-13 12:03:51 -0700377 list_add_tail(&ops->list, &link_ops);
378 return 0;
379}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700380EXPORT_SYMBOL_GPL(__rtnl_link_register);
381
382/**
383 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
384 * @ops: struct rtnl_link_ops * to register
385 *
386 * Returns 0 on success or a negative error code.
387 */
388int rtnl_link_register(struct rtnl_link_ops *ops)
389{
390 int err;
391
392 rtnl_lock();
393 err = __rtnl_link_register(ops);
394 rtnl_unlock();
395 return err;
396}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700397EXPORT_SYMBOL_GPL(rtnl_link_register);
398
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700399static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
400{
401 struct net_device *dev;
Eric Dumazet23289a32009-10-27 07:06:36 +0000402 LIST_HEAD(list_kill);
403
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700404 for_each_netdev(net, dev) {
Eric Dumazet23289a32009-10-27 07:06:36 +0000405 if (dev->rtnl_link_ops == ops)
406 ops->dellink(dev, &list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700407 }
Eric Dumazet23289a32009-10-27 07:06:36 +0000408 unregister_netdevice_many(&list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700409}
410
Patrick McHardy38f7b872007-06-13 12:03:51 -0700411/**
412 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
413 * @ops: struct rtnl_link_ops * to unregister
414 *
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700415 * The caller must hold the rtnl_mutex.
Patrick McHardy38f7b872007-06-13 12:03:51 -0700416 */
417void __rtnl_link_unregister(struct rtnl_link_ops *ops)
418{
Eric W. Biederman881d9662007-09-17 11:56:21 -0700419 struct net *net;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700420
Eric W. Biederman881d9662007-09-17 11:56:21 -0700421 for_each_net(net) {
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700422 __rtnl_kill_links(net, ops);
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700423 }
Patrick McHardy38f7b872007-06-13 12:03:51 -0700424 list_del(&ops->list);
425}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700426EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
427
Cong Wang200b9162014-05-12 15:11:20 -0700428/* Return with the rtnl_lock held when there are no network
429 * devices unregistering in any network namespace.
430 */
431static void rtnl_lock_unregistering_all(void)
432{
433 struct net *net;
434 bool unregistering;
Peter Zijlstraff960a72014-10-29 17:04:56 +0100435 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Cong Wang200b9162014-05-12 15:11:20 -0700436
Peter Zijlstraff960a72014-10-29 17:04:56 +0100437 add_wait_queue(&netdev_unregistering_wq, &wait);
Cong Wang200b9162014-05-12 15:11:20 -0700438 for (;;) {
Cong Wang200b9162014-05-12 15:11:20 -0700439 unregistering = false;
440 rtnl_lock();
441 for_each_net(net) {
442 if (net->dev_unreg_count > 0) {
443 unregistering = true;
444 break;
445 }
446 }
447 if (!unregistering)
448 break;
449 __rtnl_unlock();
Peter Zijlstraff960a72014-10-29 17:04:56 +0100450
451 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Cong Wang200b9162014-05-12 15:11:20 -0700452 }
Peter Zijlstraff960a72014-10-29 17:04:56 +0100453 remove_wait_queue(&netdev_unregistering_wq, &wait);
Cong Wang200b9162014-05-12 15:11:20 -0700454}
455
Patrick McHardy38f7b872007-06-13 12:03:51 -0700456/**
457 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
458 * @ops: struct rtnl_link_ops * to unregister
459 */
460void rtnl_link_unregister(struct rtnl_link_ops *ops)
461{
Cong Wang200b9162014-05-12 15:11:20 -0700462 /* Close the race with cleanup_net() */
Kirill Tkhai4420bf22018-03-27 18:02:23 +0300463 down_write(&pernet_ops_rwsem);
Cong Wang200b9162014-05-12 15:11:20 -0700464 rtnl_lock_unregistering_all();
Patrick McHardy38f7b872007-06-13 12:03:51 -0700465 __rtnl_link_unregister(ops);
466 rtnl_unlock();
Kirill Tkhai4420bf22018-03-27 18:02:23 +0300467 up_write(&pernet_ops_rwsem);
Patrick McHardy38f7b872007-06-13 12:03:51 -0700468}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700469EXPORT_SYMBOL_GPL(rtnl_link_unregister);
470
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100471static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
472{
473 struct net_device *master_dev;
474 const struct rtnl_link_ops *ops;
Florian Westphal8515ae32017-08-10 16:52:59 +0200475 size_t size = 0;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100476
Florian Westphal8515ae32017-08-10 16:52:59 +0200477 rcu_read_lock();
478
479 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100480 if (!master_dev)
Florian Westphal8515ae32017-08-10 16:52:59 +0200481 goto out;
482
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100483 ops = master_dev->rtnl_link_ops;
Fernando Luis Vazquez Cao6049f252014-02-04 19:35:02 +0900484 if (!ops || !ops->get_slave_size)
Florian Westphal8515ae32017-08-10 16:52:59 +0200485 goto out;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100486 /* IFLA_INFO_SLAVE_DATA + nested data */
Florian Westphal8515ae32017-08-10 16:52:59 +0200487 size = nla_total_size(sizeof(struct nlattr)) +
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100488 ops->get_slave_size(master_dev, dev);
Florian Westphal8515ae32017-08-10 16:52:59 +0200489
490out:
491 rcu_read_unlock();
492 return size;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100493}
494
Patrick McHardy38f7b872007-06-13 12:03:51 -0700495static size_t rtnl_link_get_size(const struct net_device *dev)
496{
497 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
498 size_t size;
499
500 if (!ops)
501 return 0;
502
Thomas Graf369cf772010-11-11 15:47:59 +0000503 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
504 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700505
506 if (ops->get_size)
507 /* IFLA_INFO_DATA + nested data */
Thomas Graf369cf772010-11-11 15:47:59 +0000508 size += nla_total_size(sizeof(struct nlattr)) +
Patrick McHardy38f7b872007-06-13 12:03:51 -0700509 ops->get_size(dev);
510
511 if (ops->get_xstats_size)
Thomas Graf369cf772010-11-11 15:47:59 +0000512 /* IFLA_INFO_XSTATS */
513 size += nla_total_size(ops->get_xstats_size(dev));
Patrick McHardy38f7b872007-06-13 12:03:51 -0700514
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100515 size += rtnl_link_get_slave_info_data_size(dev);
516
Patrick McHardy38f7b872007-06-13 12:03:51 -0700517 return size;
518}
519
Thomas Graff8ff1822010-11-16 04:30:14 +0000520static LIST_HEAD(rtnl_af_ops);
521
522static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
523{
524 const struct rtnl_af_ops *ops;
525
Florian Westphal5fa85a02017-10-16 15:44:36 +0200526 list_for_each_entry_rcu(ops, &rtnl_af_ops, list) {
Thomas Graff8ff1822010-11-16 04:30:14 +0000527 if (ops->family == family)
528 return ops;
529 }
530
531 return NULL;
532}
533
534/**
Thomas Graff8ff1822010-11-16 04:30:14 +0000535 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
536 * @ops: struct rtnl_af_ops * to register
537 *
538 * Returns 0 on success or a negative error code.
539 */
stephen hemminger3678a9d2013-12-30 10:41:32 -0800540void rtnl_af_register(struct rtnl_af_ops *ops)
Thomas Graff8ff1822010-11-16 04:30:14 +0000541{
Thomas Graff8ff1822010-11-16 04:30:14 +0000542 rtnl_lock();
Florian Westphal5fa85a02017-10-16 15:44:36 +0200543 list_add_tail_rcu(&ops->list, &rtnl_af_ops);
Thomas Graff8ff1822010-11-16 04:30:14 +0000544 rtnl_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +0000545}
546EXPORT_SYMBOL_GPL(rtnl_af_register);
547
548/**
Thomas Graff8ff1822010-11-16 04:30:14 +0000549 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
550 * @ops: struct rtnl_af_ops * to unregister
551 */
552void rtnl_af_unregister(struct rtnl_af_ops *ops)
553{
554 rtnl_lock();
Florian Westphal5fa85a02017-10-16 15:44:36 +0200555 list_del_rcu(&ops->list);
Thomas Graff8ff1822010-11-16 04:30:14 +0000556 rtnl_unlock();
Florian Westphal5fa85a02017-10-16 15:44:36 +0200557
558 synchronize_rcu();
Thomas Graff8ff1822010-11-16 04:30:14 +0000559}
560EXPORT_SYMBOL_GPL(rtnl_af_unregister);
561
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700562static size_t rtnl_link_get_af_size(const struct net_device *dev,
563 u32 ext_filter_mask)
Thomas Graff8ff1822010-11-16 04:30:14 +0000564{
565 struct rtnl_af_ops *af_ops;
566 size_t size;
567
568 /* IFLA_AF_SPEC */
569 size = nla_total_size(sizeof(struct nlattr));
570
Florian Westphal5fa85a02017-10-16 15:44:36 +0200571 rcu_read_lock();
572 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Thomas Graff8ff1822010-11-16 04:30:14 +0000573 if (af_ops->get_link_af_size) {
574 /* AF_* + nested data */
575 size += nla_total_size(sizeof(struct nlattr)) +
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700576 af_ops->get_link_af_size(dev, ext_filter_mask);
Thomas Graff8ff1822010-11-16 04:30:14 +0000577 }
578 }
Florian Westphal5fa85a02017-10-16 15:44:36 +0200579 rcu_read_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +0000580
581 return size;
582}
583
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100584static bool rtnl_have_link_slave_info(const struct net_device *dev)
585{
586 struct net_device *master_dev;
Florian Westphal4c82a952017-09-26 13:58:43 +0200587 bool ret = false;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100588
Florian Westphal4c82a952017-09-26 13:58:43 +0200589 rcu_read_lock();
590
591 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
Jiri Pirko813f0202014-01-23 19:19:21 +0100592 if (master_dev && master_dev->rtnl_link_ops)
Florian Westphal4c82a952017-09-26 13:58:43 +0200593 ret = true;
594 rcu_read_unlock();
595 return ret;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100596}
597
598static int rtnl_link_slave_info_fill(struct sk_buff *skb,
599 const struct net_device *dev)
600{
601 struct net_device *master_dev;
602 const struct rtnl_link_ops *ops;
603 struct nlattr *slave_data;
604 int err;
605
606 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
607 if (!master_dev)
608 return 0;
609 ops = master_dev->rtnl_link_ops;
610 if (!ops)
611 return 0;
612 if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
613 return -EMSGSIZE;
614 if (ops->fill_slave_info) {
615 slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA);
616 if (!slave_data)
617 return -EMSGSIZE;
618 err = ops->fill_slave_info(skb, master_dev, dev);
619 if (err < 0)
620 goto err_cancel_slave_data;
621 nla_nest_end(skb, slave_data);
622 }
623 return 0;
624
625err_cancel_slave_data:
626 nla_nest_cancel(skb, slave_data);
627 return err;
628}
629
630static int rtnl_link_info_fill(struct sk_buff *skb,
631 const struct net_device *dev)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700632{
633 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100634 struct nlattr *data;
635 int err;
636
637 if (!ops)
638 return 0;
639 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
640 return -EMSGSIZE;
641 if (ops->fill_xstats) {
642 err = ops->fill_xstats(skb, dev);
643 if (err < 0)
644 return err;
645 }
646 if (ops->fill_info) {
647 data = nla_nest_start(skb, IFLA_INFO_DATA);
648 if (data == NULL)
649 return -EMSGSIZE;
650 err = ops->fill_info(skb, dev);
651 if (err < 0)
652 goto err_cancel_data;
653 nla_nest_end(skb, data);
654 }
655 return 0;
656
657err_cancel_data:
658 nla_nest_cancel(skb, data);
659 return err;
660}
661
662static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
663{
664 struct nlattr *linkinfo;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700665 int err = -EMSGSIZE;
666
667 linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
668 if (linkinfo == NULL)
669 goto out;
670
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100671 err = rtnl_link_info_fill(skb, dev);
672 if (err < 0)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700673 goto err_cancel_link;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100674
675 err = rtnl_link_slave_info_fill(skb, dev);
676 if (err < 0)
677 goto err_cancel_link;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700678
679 nla_nest_end(skb, linkinfo);
680 return 0;
681
Patrick McHardy38f7b872007-06-13 12:03:51 -0700682err_cancel_link:
683 nla_nest_cancel(skb, linkinfo);
684out:
685 return err;
686}
687
Eric Dumazet95c96172012-04-15 05:58:06 +0000688int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800690 struct sock *rtnl = net->rtnl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 int err = 0;
692
Patrick McHardyac6d4392005-08-14 19:29:52 -0700693 NETLINK_CB(skb).dst_group = group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (echo)
Reshetova, Elena63354792017-06-30 13:07:58 +0300695 refcount_inc(&skb->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
697 if (echo)
698 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
699 return err;
700}
701
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800702int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
Thomas Graf2942e902006-08-15 00:30:25 -0700703{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800704 struct sock *rtnl = net->rtnl;
705
Thomas Graf2942e902006-08-15 00:30:25 -0700706 return nlmsg_unicast(rtnl, skb, pid);
707}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800708EXPORT_SYMBOL(rtnl_unicast);
Thomas Graf2942e902006-08-15 00:30:25 -0700709
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800710void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
711 struct nlmsghdr *nlh, gfp_t flags)
Thomas Graf97676b62006-08-15 00:31:41 -0700712{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800713 struct sock *rtnl = net->rtnl;
Thomas Graf97676b62006-08-15 00:31:41 -0700714 int report = 0;
715
716 if (nlh)
717 report = nlmsg_report(nlh);
718
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800719 nlmsg_notify(rtnl, skb, pid, group, report, flags);
Thomas Graf97676b62006-08-15 00:31:41 -0700720}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800721EXPORT_SYMBOL(rtnl_notify);
Thomas Graf97676b62006-08-15 00:31:41 -0700722
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800723void rtnl_set_sk_err(struct net *net, u32 group, int error)
Thomas Graf97676b62006-08-15 00:31:41 -0700724{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800725 struct sock *rtnl = net->rtnl;
726
Thomas Graf97676b62006-08-15 00:31:41 -0700727 netlink_set_err(rtnl, 0, group, error);
728}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800729EXPORT_SYMBOL(rtnl_set_sk_err);
Thomas Graf97676b62006-08-15 00:31:41 -0700730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
732{
Thomas Graf2d7202b2006-08-22 00:01:27 -0700733 struct nlattr *mx;
734 int i, valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Thomas Graf2d7202b2006-08-22 00:01:27 -0700736 mx = nla_nest_start(skb, RTA_METRICS);
737 if (mx == NULL)
738 return -ENOBUFS;
739
740 for (i = 0; i < RTAX_MAX; i++) {
741 if (metrics[i]) {
Daniel Borkmannea697632015-01-05 23:57:47 +0100742 if (i == RTAX_CC_ALGO - 1) {
743 char tmp[TCP_CA_NAME_MAX], *name;
744
745 name = tcp_ca_get_name_by_key(metrics[i], tmp);
746 if (!name)
747 continue;
748 if (nla_put_string(skb, i + 1, name))
749 goto nla_put_failure;
Daniel Borkmannc3a8d942015-08-31 15:58:47 +0200750 } else if (i == RTAX_FEATURES - 1) {
751 u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
752
Phil Sutterf8edcd12016-08-23 13:14:31 +0200753 if (!user_features)
754 continue;
Daniel Borkmannc3a8d942015-08-31 15:58:47 +0200755 BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
756 if (nla_put_u32(skb, i + 1, user_features))
757 goto nla_put_failure;
Daniel Borkmannea697632015-01-05 23:57:47 +0100758 } else {
759 if (nla_put_u32(skb, i + 1, metrics[i]))
760 goto nla_put_failure;
761 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700762 valid++;
Thomas Graf2d7202b2006-08-22 00:01:27 -0700763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
David S. Millera57d27f2006-08-22 22:20:14 -0700766 if (!valid) {
767 nla_nest_cancel(skb, mx);
768 return 0;
769 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700770
771 return nla_nest_end(skb, mx);
772
773nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700774 nla_nest_cancel(skb, mx);
775 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800777EXPORT_SYMBOL(rtnetlink_put_metrics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Thomas Grafe3703b32006-11-27 09:27:07 -0800779int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
David S. Miller87a50692012-07-10 05:06:14 -0700780 long expires, u32 error)
Thomas Grafe3703b32006-11-27 09:27:07 -0800781{
782 struct rta_cacheinfo ci = {
Eric Dumazeta399a802012-08-08 21:13:53 +0000783 .rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse),
Thomas Grafe3703b32006-11-27 09:27:07 -0800784 .rta_used = dst->__use,
785 .rta_clntref = atomic_read(&(dst->__refcnt)),
786 .rta_error = error,
787 .rta_id = id,
Thomas Grafe3703b32006-11-27 09:27:07 -0800788 };
789
Li Wei82539472012-07-29 16:01:30 +0000790 if (expires) {
791 unsigned long clock;
Thomas Grafe3703b32006-11-27 09:27:07 -0800792
Li Wei82539472012-07-29 16:01:30 +0000793 clock = jiffies_to_clock_t(abs(expires));
794 clock = min_t(unsigned long, clock, INT_MAX);
795 ci.rta_expires = (expires > 0) ? clock : -clock;
796 }
Thomas Grafe3703b32006-11-27 09:27:07 -0800797 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
798}
Thomas Grafe3703b32006-11-27 09:27:07 -0800799EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
David S. Miller93b2d4a2008-02-17 18:35:07 -0800801static void set_operstate(struct net_device *dev, unsigned char transition)
Stefan Rompfb00055a2006-03-20 17:09:11 -0800802{
803 unsigned char operstate = dev->operstate;
804
Eric Dumazete0d087a2009-11-07 01:26:17 -0800805 switch (transition) {
Stefan Rompfb00055a2006-03-20 17:09:11 -0800806 case IF_OPER_UP:
807 if ((operstate == IF_OPER_DORMANT ||
808 operstate == IF_OPER_UNKNOWN) &&
809 !netif_dormant(dev))
810 operstate = IF_OPER_UP;
811 break;
812
813 case IF_OPER_DORMANT:
814 if (operstate == IF_OPER_UP ||
815 operstate == IF_OPER_UNKNOWN)
816 operstate = IF_OPER_DORMANT;
817 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700818 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800819
820 if (dev->operstate != operstate) {
821 write_lock_bh(&dev_base_lock);
822 dev->operstate = operstate;
823 write_unlock_bh(&dev_base_lock);
David S. Miller93b2d4a2008-02-17 18:35:07 -0800824 netdev_state_change(dev);
825 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800826}
827
Jiri Bencb1beb682012-07-27 02:58:22 +0000828static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
829{
830 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
831 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
832}
833
Patrick McHardy3729d502010-02-26 06:34:54 +0000834static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
835 const struct ifinfomsg *ifm)
836{
837 unsigned int flags = ifm->ifi_flags;
838
839 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
840 if (ifm->ifi_change)
841 flags = (flags & ifm->ifi_change) |
Jiri Bencb1beb682012-07-27 02:58:22 +0000842 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
Patrick McHardy3729d502010-02-26 06:34:54 +0000843
844 return flags;
845}
846
Thomas Grafb60c5112006-08-04 23:05:34 -0700847static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000848 const struct rtnl_link_stats64 *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
Thomas Grafb60c5112006-08-04 23:05:34 -0700850 a->rx_packets = b->rx_packets;
851 a->tx_packets = b->tx_packets;
852 a->rx_bytes = b->rx_bytes;
853 a->tx_bytes = b->tx_bytes;
854 a->rx_errors = b->rx_errors;
855 a->tx_errors = b->tx_errors;
856 a->rx_dropped = b->rx_dropped;
857 a->tx_dropped = b->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Thomas Grafb60c5112006-08-04 23:05:34 -0700859 a->multicast = b->multicast;
860 a->collisions = b->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Thomas Grafb60c5112006-08-04 23:05:34 -0700862 a->rx_length_errors = b->rx_length_errors;
863 a->rx_over_errors = b->rx_over_errors;
864 a->rx_crc_errors = b->rx_crc_errors;
865 a->rx_frame_errors = b->rx_frame_errors;
866 a->rx_fifo_errors = b->rx_fifo_errors;
867 a->rx_missed_errors = b->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Thomas Grafb60c5112006-08-04 23:05:34 -0700869 a->tx_aborted_errors = b->tx_aborted_errors;
870 a->tx_carrier_errors = b->tx_carrier_errors;
871 a->tx_fifo_errors = b->tx_fifo_errors;
872 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
873 a->tx_window_errors = b->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Thomas Grafb60c5112006-08-04 23:05:34 -0700875 a->rx_compressed = b->rx_compressed;
876 a->tx_compressed = b->tx_compressed;
Jarod Wilson6e7333d2016-02-01 18:51:05 -0500877
878 a->rx_nohandler = b->rx_nohandler;
Jan Engelhardt10708f32010-03-11 09:57:29 +0000879}
880
Chris Wrightc02db8c2010-05-16 01:05:45 -0700881/* All VF info */
Greg Rose115c9b82012-02-21 16:54:48 -0500882static inline int rtnl_vfinfo_size(const struct net_device *dev,
883 u32 ext_filter_mask)
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000884{
Phil Sutter9af15c32017-01-18 14:04:39 +0100885 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
Chris Wrightc02db8c2010-05-16 01:05:45 -0700886 int num_vfs = dev_num_vf(dev->dev.parent);
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100887 size_t size = nla_total_size(0);
Scott Feldman045de012010-05-28 03:42:43 -0700888 size += num_vfs *
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100889 (nla_total_size(0) +
890 nla_total_size(sizeof(struct ifla_vf_mac)) +
891 nla_total_size(sizeof(struct ifla_vf_vlan)) +
892 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
Moshe Shemesh79aab092016-09-22 12:11:15 +0300893 nla_total_size(MAX_VLAN_LIST_LEN *
894 sizeof(struct ifla_vf_vlan_info)) +
Sucheta Chakrabortyed616682014-05-22 09:59:05 -0400895 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100896 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
Jiri Benc945a3672014-08-08 16:44:32 +0200897 nla_total_size(sizeof(struct ifla_vf_rate)) +
Vlad Zolotarov01a3d792015-03-30 21:35:23 +0300898 nla_total_size(sizeof(struct ifla_vf_link_state)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300899 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100900 nla_total_size(0) + /* nest IFLA_VF_STATS */
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300901 /* IFLA_VF_STATS_RX_PACKETS */
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_TX_PACKETS */
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_RX_BYTES */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200906 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300907 /* IFLA_VF_STATS_TX_BYTES */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200908 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300909 /* IFLA_VF_STATS_BROADCAST */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200910 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300911 /* IFLA_VF_STATS_MULTICAST */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200912 nla_total_size_64bit(sizeof(__u64)) +
Eugenia Emantayevc5a9f6f2017-07-17 13:47:07 +0300913 /* IFLA_VF_STATS_RX_DROPPED */
914 nla_total_size_64bit(sizeof(__u64)) +
915 /* IFLA_VF_STATS_TX_DROPPED */
916 nla_total_size_64bit(sizeof(__u64)) +
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +0000917 nla_total_size(sizeof(struct ifla_vf_trust)));
Chris Wrightc02db8c2010-05-16 01:05:45 -0700918 return size;
919 } else
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000920 return 0;
921}
922
David Gibsonc53864f2014-04-24 10:22:36 +1000923static size_t rtnl_port_size(const struct net_device *dev,
924 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -0700925{
926 size_t port_size = nla_total_size(4) /* PORT_VF */
927 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
Scott Feldman57b61082010-05-17 22:49:55 -0700928 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
929 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
930 + nla_total_size(1) /* PROT_VDP_REQUEST */
931 + nla_total_size(2); /* PORT_VDP_RESPONSE */
932 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
933 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
934 + port_size;
935 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
936 + port_size;
937
David Gibsonc53864f2014-04-24 10:22:36 +1000938 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
939 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -0700940 return 0;
941 if (dev_num_vf(dev->dev.parent))
942 return port_self_size + vf_ports_size +
943 vf_port_size * dev_num_vf(dev->dev.parent);
944 else
945 return port_self_size;
946}
947
David S. Millerb5cdae32017-04-18 15:36:58 -0400948static size_t rtnl_xdp_size(void)
Brenden Blancod1fdd912016-07-19 12:16:49 -0700949{
Sabrina Dubrocab3cfaa32016-11-15 11:16:35 +0100950 size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */
Martin KaFai Lau58038692017-06-15 17:29:09 -0700951 nla_total_size(1) + /* XDP_ATTACHED */
952 nla_total_size(4); /* XDP_PROG_ID */
Brenden Blancod1fdd912016-07-19 12:16:49 -0700953
David S. Millerb5cdae32017-04-18 15:36:58 -0400954 return xdp_size;
Brenden Blancod1fdd912016-07-19 12:16:49 -0700955}
956
Greg Rose115c9b82012-02-21 16:54:48 -0500957static noinline size_t if_nlmsg_size(const struct net_device *dev,
958 u32 ext_filter_mask)
Thomas Graf339bf982006-11-10 14:10:15 -0800959{
960 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
961 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700962 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
Thomas Graf339bf982006-11-10 14:10:15 -0800963 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
Nicolas Dichtel270cb4d2016-04-26 10:06:16 +0200964 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
Thomas Graf339bf982006-11-10 14:10:15 -0800965 + nla_total_size(sizeof(struct rtnl_link_stats))
David S. Miller35c58452016-04-19 19:49:29 -0400966 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
Thomas Graf339bf982006-11-10 14:10:15 -0800967 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
968 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
969 + nla_total_size(4) /* IFLA_TXQLEN */
970 + nla_total_size(4) /* IFLA_WEIGHT */
971 + nla_total_size(4) /* IFLA_MTU */
972 + nla_total_size(4) /* IFLA_LINK */
973 + nla_total_size(4) /* IFLA_MASTER */
Jiri Pirko9a572472012-12-27 23:49:39 +0000974 + nla_total_size(1) /* IFLA_CARRIER */
Ben Greearedbc0bb2012-03-29 12:51:30 +0000975 + nla_total_size(4) /* IFLA_PROMISCUITY */
Jiri Pirko76ff5cc2012-07-20 02:28:48 +0000976 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
977 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
Tobias Klauser69197562016-11-30 14:30:37 +0100978 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
979 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
Thomas Graf339bf982006-11-10 14:10:15 -0800980 + nla_total_size(1) /* IFLA_OPERSTATE */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700981 + nla_total_size(1) /* IFLA_LINKMODE */
david decotigny2d3b4792014-03-29 09:48:35 -0700982 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
Nicolas Dichteld37512a2015-01-15 15:11:16 +0100983 + nla_total_size(4) /* IFLA_LINK_NETNSID */
Serhey Popovychdb833d42017-06-20 14:35:23 +0300984 + nla_total_size(4) /* IFLA_GROUP */
Greg Rose115c9b82012-02-21 16:54:48 -0500985 + nla_total_size(ext_filter_mask
986 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
987 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
David Gibsonc53864f2014-04-24 10:22:36 +1000988 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
Thomas Graff8ff1822010-11-16 04:30:14 +0000989 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700990 + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
Jiri Pirko82f28412014-11-28 14:34:18 +0100991 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
Anuradha Karuppiah88d63782015-07-14 13:43:20 -0700992 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
Nicolas Dichtelc57c7a92016-03-31 18:10:31 +0200993 + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
David S. Millerb5cdae32017-04-18 15:36:58 -0400994 + rtnl_xdp_size() /* IFLA_XDP */
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -0400995 + nla_total_size(4) /* IFLA_EVENT */
Nicolas Dichtel6621dd22017-10-03 13:53:23 +0200996 + nla_total_size(4) /* IFLA_NEW_NETNSID */
Nicolas Dichtel38e01b32018-01-25 15:01:39 +0100997 + nla_total_size(4) /* IFLA_NEW_IFINDEX */
Colin Ian King03ac7382017-11-06 15:04:54 +0000998 + nla_total_size(1) /* IFLA_PROTO_DOWN */
Jiri Benc79e1ad12017-11-02 17:04:38 -0200999 + nla_total_size(4) /* IFLA_IF_NETNSID */
David Decotignyb2d3bcf2018-01-18 09:59:13 -08001000 + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */
1001 + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */
Jiri Benc79e1ad12017-11-02 17:04:38 -02001002 + 0;
Thomas Graf339bf982006-11-10 14:10:15 -08001003}
1004
Scott Feldman57b61082010-05-17 22:49:55 -07001005static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
1006{
1007 struct nlattr *vf_ports;
1008 struct nlattr *vf_port;
1009 int vf;
1010 int err;
1011
1012 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
1013 if (!vf_ports)
1014 return -EMSGSIZE;
1015
1016 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
1017 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
Scott Feldman8ca94182010-05-28 03:42:18 -07001018 if (!vf_port)
1019 goto nla_put_failure;
David S. Millera6574342012-04-01 20:12:00 -04001020 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
1021 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001022 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
Scott Feldman8ca94182010-05-28 03:42:18 -07001023 if (err == -EMSGSIZE)
1024 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001025 if (err) {
Scott Feldman57b61082010-05-17 22:49:55 -07001026 nla_nest_cancel(skb, vf_port);
1027 continue;
1028 }
1029 nla_nest_end(skb, vf_port);
1030 }
1031
1032 nla_nest_end(skb, vf_ports);
1033
1034 return 0;
Scott Feldman8ca94182010-05-28 03:42:18 -07001035
1036nla_put_failure:
1037 nla_nest_cancel(skb, vf_ports);
1038 return -EMSGSIZE;
Scott Feldman57b61082010-05-17 22:49:55 -07001039}
1040
1041static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
1042{
1043 struct nlattr *port_self;
1044 int err;
1045
1046 port_self = nla_nest_start(skb, IFLA_PORT_SELF);
1047 if (!port_self)
1048 return -EMSGSIZE;
1049
1050 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
1051 if (err) {
1052 nla_nest_cancel(skb, port_self);
Scott Feldman8ca94182010-05-28 03:42:18 -07001053 return (err == -EMSGSIZE) ? err : 0;
Scott Feldman57b61082010-05-17 22:49:55 -07001054 }
1055
1056 nla_nest_end(skb, port_self);
1057
1058 return 0;
1059}
1060
David Gibsonc53864f2014-04-24 10:22:36 +10001061static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1062 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -07001063{
1064 int err;
1065
David Gibsonc53864f2014-04-24 10:22:36 +10001066 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1067 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -07001068 return 0;
1069
1070 err = rtnl_port_self_fill(skb, dev);
1071 if (err)
1072 return err;
1073
1074 if (dev_num_vf(dev->dev.parent)) {
1075 err = rtnl_vf_ports_fill(skb, dev);
1076 if (err)
1077 return err;
1078 }
1079
1080 return 0;
1081}
1082
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001083static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
1084{
1085 int err;
Jiri Pirko02637fc2014-11-28 14:34:16 +01001086 struct netdev_phys_item_id ppid;
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001087
1088 err = dev_get_phys_port_id(dev, &ppid);
1089 if (err) {
1090 if (err == -EOPNOTSUPP)
1091 return 0;
1092 return err;
1093 }
1094
1095 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1096 return -EMSGSIZE;
1097
1098 return 0;
1099}
1100
David Aherndb24a902015-03-17 20:23:15 -06001101static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1102{
1103 char name[IFNAMSIZ];
1104 int err;
1105
1106 err = dev_get_phys_port_name(dev, name, sizeof(name));
1107 if (err) {
1108 if (err == -EOPNOTSUPP)
1109 return 0;
1110 return err;
1111 }
1112
Michal Schmidt77ef0332017-05-04 16:48:58 +02001113 if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
David Aherndb24a902015-03-17 20:23:15 -06001114 return -EMSGSIZE;
1115
1116 return 0;
1117}
1118
Jiri Pirko82f28412014-11-28 14:34:18 +01001119static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1120{
1121 int err;
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001122 struct switchdev_attr attr = {
Ido Schimmel6ff64f62015-12-15 16:03:35 +01001123 .orig_dev = dev,
Jiri Pirko1f868392015-10-01 11:03:42 +02001124 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001125 .flags = SWITCHDEV_F_NO_RECURSE,
1126 };
Jiri Pirko82f28412014-11-28 14:34:18 +01001127
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001128 err = switchdev_port_attr_get(dev, &attr);
Jiri Pirko82f28412014-11-28 14:34:18 +01001129 if (err) {
1130 if (err == -EOPNOTSUPP)
1131 return 0;
1132 return err;
1133 }
1134
Scott Feldman42275bd2015-05-13 11:16:50 -07001135 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len,
1136 attr.u.ppid.id))
Jiri Pirko82f28412014-11-28 14:34:18 +01001137 return -EMSGSIZE;
1138
1139 return 0;
1140}
1141
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001142static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1143 struct net_device *dev)
1144{
Roopa Prabhu550bce52016-04-15 20:36:25 -07001145 struct rtnl_link_stats64 *sp;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001146 struct nlattr *attr;
1147
Nicolas Dichtel58414d32016-04-21 18:58:25 +02001148 attr = nla_reserve_64bit(skb, IFLA_STATS64,
1149 sizeof(struct rtnl_link_stats64), IFLA_PAD);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001150 if (!attr)
1151 return -EMSGSIZE;
1152
Roopa Prabhu550bce52016-04-15 20:36:25 -07001153 sp = nla_data(attr);
1154 dev_get_stats(dev, sp);
1155
1156 attr = nla_reserve(skb, IFLA_STATS,
1157 sizeof(struct rtnl_link_stats));
1158 if (!attr)
1159 return -EMSGSIZE;
1160
1161 copy_rtnl_link_stats(nla_data(attr), sp);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001162
1163 return 0;
1164}
1165
1166static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1167 struct net_device *dev,
1168 int vfs_num,
1169 struct nlattr *vfinfo)
1170{
1171 struct ifla_vf_rss_query_en vf_rss_query_en;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001172 struct nlattr *vf, *vfstats, *vfvlanlist;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001173 struct ifla_vf_link_state vf_linkstate;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001174 struct ifla_vf_vlan_info vf_vlan_info;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001175 struct ifla_vf_spoofchk vf_spoofchk;
1176 struct ifla_vf_tx_rate vf_tx_rate;
1177 struct ifla_vf_stats vf_stats;
1178 struct ifla_vf_trust vf_trust;
1179 struct ifla_vf_vlan vf_vlan;
1180 struct ifla_vf_rate vf_rate;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001181 struct ifla_vf_mac vf_mac;
1182 struct ifla_vf_info ivi;
1183
Mintz, Yuval0eed9cf2017-06-07 21:00:33 +03001184 memset(&ivi, 0, sizeof(ivi));
1185
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001186 /* Not all SR-IOV capable drivers support the
1187 * spoofcheck and "RSS query enable" query. Preset to
1188 * -1 so the user space tool can detect that the driver
1189 * didn't report anything.
1190 */
1191 ivi.spoofchk = -1;
1192 ivi.rss_query_en = -1;
1193 ivi.trusted = -1;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001194 /* The default value for VF link state is "auto"
1195 * IFLA_VF_LINK_STATE_AUTO which equals zero
1196 */
1197 ivi.linkstate = 0;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001198 /* VLAN Protocol by default is 802.1Q */
1199 ivi.vlan_proto = htons(ETH_P_8021Q);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001200 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1201 return 0;
1202
Dan Carpenter775f4f02016-10-13 11:45:28 +03001203 memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
1204
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001205 vf_mac.vf =
1206 vf_vlan.vf =
Moshe Shemesh79aab092016-09-22 12:11:15 +03001207 vf_vlan_info.vf =
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001208 vf_rate.vf =
1209 vf_tx_rate.vf =
1210 vf_spoofchk.vf =
1211 vf_linkstate.vf =
1212 vf_rss_query_en.vf =
1213 vf_trust.vf = ivi.vf;
1214
1215 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1216 vf_vlan.vlan = ivi.vlan;
1217 vf_vlan.qos = ivi.qos;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001218 vf_vlan_info.vlan = ivi.vlan;
1219 vf_vlan_info.qos = ivi.qos;
1220 vf_vlan_info.vlan_proto = ivi.vlan_proto;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001221 vf_tx_rate.rate = ivi.max_tx_rate;
1222 vf_rate.min_tx_rate = ivi.min_tx_rate;
1223 vf_rate.max_tx_rate = ivi.max_tx_rate;
1224 vf_spoofchk.setting = ivi.spoofchk;
1225 vf_linkstate.link_state = ivi.linkstate;
1226 vf_rss_query_en.setting = ivi.rss_query_en;
1227 vf_trust.setting = ivi.trusted;
1228 vf = nla_nest_start(skb, IFLA_VF_INFO);
Moshe Shemesh79aab092016-09-22 12:11:15 +03001229 if (!vf)
1230 goto nla_put_vfinfo_failure;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001231 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1232 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1233 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1234 &vf_rate) ||
1235 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1236 &vf_tx_rate) ||
1237 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1238 &vf_spoofchk) ||
1239 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1240 &vf_linkstate) ||
1241 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1242 sizeof(vf_rss_query_en),
1243 &vf_rss_query_en) ||
1244 nla_put(skb, IFLA_VF_TRUST,
1245 sizeof(vf_trust), &vf_trust))
Moshe Shemesh79aab092016-09-22 12:11:15 +03001246 goto nla_put_vf_failure;
1247 vfvlanlist = nla_nest_start(skb, IFLA_VF_VLAN_LIST);
1248 if (!vfvlanlist)
1249 goto nla_put_vf_failure;
1250 if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
1251 &vf_vlan_info)) {
1252 nla_nest_cancel(skb, vfvlanlist);
1253 goto nla_put_vf_failure;
1254 }
1255 nla_nest_end(skb, vfvlanlist);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001256 memset(&vf_stats, 0, sizeof(vf_stats));
1257 if (dev->netdev_ops->ndo_get_vf_stats)
1258 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1259 &vf_stats);
1260 vfstats = nla_nest_start(skb, IFLA_VF_STATS);
Moshe Shemesh79aab092016-09-22 12:11:15 +03001261 if (!vfstats)
1262 goto nla_put_vf_failure;
Nicolas Dichtel343a6d82016-04-25 10:25:14 +02001263 if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1264 vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1265 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1266 vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1267 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1268 vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1269 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1270 vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1271 nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1272 vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1273 nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
Eugenia Emantayevc5a9f6f2017-07-17 13:47:07 +03001274 vf_stats.multicast, IFLA_VF_STATS_PAD) ||
1275 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED,
1276 vf_stats.rx_dropped, IFLA_VF_STATS_PAD) ||
1277 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED,
1278 vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) {
Moshe Shemesh79aab092016-09-22 12:11:15 +03001279 nla_nest_cancel(skb, vfstats);
1280 goto nla_put_vf_failure;
1281 }
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001282 nla_nest_end(skb, vfstats);
1283 nla_nest_end(skb, vf);
1284 return 0;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001285
1286nla_put_vf_failure:
1287 nla_nest_cancel(skb, vf);
1288nla_put_vfinfo_failure:
1289 nla_nest_cancel(skb, vfinfo);
1290 return -EMSGSIZE;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001291}
1292
Florian Westphal250fc3d2017-09-26 13:58:41 +02001293static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb,
1294 struct net_device *dev,
1295 u32 ext_filter_mask)
1296{
1297 struct nlattr *vfinfo;
1298 int i, num_vfs;
1299
1300 if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0))
1301 return 0;
1302
1303 num_vfs = dev_num_vf(dev->dev.parent);
1304 if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs))
1305 return -EMSGSIZE;
1306
1307 if (!dev->netdev_ops->ndo_get_vf_config)
1308 return 0;
1309
1310 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1311 if (!vfinfo)
1312 return -EMSGSIZE;
1313
1314 for (i = 0; i < num_vfs; i++) {
1315 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1316 return -EMSGSIZE;
1317 }
1318
1319 nla_nest_end(skb, vfinfo);
1320 return 0;
1321}
1322
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001323static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1324{
Kangjie Lu5f8e4472016-05-03 16:46:24 -04001325 struct rtnl_link_ifmap map;
1326
1327 memset(&map, 0, sizeof(map));
1328 map.mem_start = dev->mem_start;
1329 map.mem_end = dev->mem_end;
1330 map.base_addr = dev->base_addr;
1331 map.irq = dev->irq;
1332 map.dma = dev->dma;
1333 map.port = dev->if_port;
1334
Nicolas Dichtel270cb4d2016-04-26 10:06:16 +02001335 if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001336 return -EMSGSIZE;
1337
1338 return 0;
1339}
1340
Martin KaFai Lau58038692017-06-15 17:29:09 -07001341static u8 rtnl_xdp_attached_mode(struct net_device *dev, u32 *prog_id)
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001342{
1343 const struct net_device_ops *ops = dev->netdev_ops;
Martin KaFai Lau58038692017-06-15 17:29:09 -07001344 const struct bpf_prog *generic_xdp_prog;
Jakub Kicinski118b4aa2017-12-01 15:08:55 -08001345 struct netdev_bpf xdp;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001346
1347 ASSERT_RTNL();
1348
Martin KaFai Lau58038692017-06-15 17:29:09 -07001349 *prog_id = 0;
1350 generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
1351 if (generic_xdp_prog) {
1352 *prog_id = generic_xdp_prog->aux->id;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001353 return XDP_ATTACHED_SKB;
Martin KaFai Lau58038692017-06-15 17:29:09 -07001354 }
Jakub Kicinskif4e63522017-11-03 13:56:16 -07001355 if (!ops->ndo_bpf)
Jakub Kicinskice158e52017-06-21 18:25:09 -07001356 return XDP_ATTACHED_NONE;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001357
Jakub Kicinski118b4aa2017-12-01 15:08:55 -08001358 __dev_xdp_query(dev, ops->ndo_bpf, &xdp);
1359 *prog_id = xdp.prog_id;
1360
1361 return xdp.prog_attached;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001362}
1363
Brenden Blancod1fdd912016-07-19 12:16:49 -07001364static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1365{
Brenden Blancod1fdd912016-07-19 12:16:49 -07001366 struct nlattr *xdp;
Martin KaFai Lau58038692017-06-15 17:29:09 -07001367 u32 prog_id;
Brenden Blancod1fdd912016-07-19 12:16:49 -07001368 int err;
1369
Brenden Blancod1fdd912016-07-19 12:16:49 -07001370 xdp = nla_nest_start(skb, IFLA_XDP);
1371 if (!xdp)
1372 return -EMSGSIZE;
David S. Millerb5cdae32017-04-18 15:36:58 -04001373
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001374 err = nla_put_u8(skb, IFLA_XDP_ATTACHED,
Martin KaFai Lau58038692017-06-15 17:29:09 -07001375 rtnl_xdp_attached_mode(dev, &prog_id));
Brenden Blancod1fdd912016-07-19 12:16:49 -07001376 if (err)
1377 goto err_cancel;
1378
Martin KaFai Lau58038692017-06-15 17:29:09 -07001379 if (prog_id) {
1380 err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
1381 if (err)
1382 goto err_cancel;
1383 }
1384
Brenden Blancod1fdd912016-07-19 12:16:49 -07001385 nla_nest_end(skb, xdp);
1386 return 0;
1387
1388err_cancel:
1389 nla_nest_cancel(skb, xdp);
1390 return err;
1391}
1392
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001393static u32 rtnl_get_event(unsigned long event)
1394{
1395 u32 rtnl_event_type = IFLA_EVENT_NONE;
1396
1397 switch (event) {
1398 case NETDEV_REBOOT:
1399 rtnl_event_type = IFLA_EVENT_REBOOT;
1400 break;
1401 case NETDEV_FEAT_CHANGE:
1402 rtnl_event_type = IFLA_EVENT_FEATURES;
1403 break;
1404 case NETDEV_BONDING_FAILOVER:
1405 rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
1406 break;
1407 case NETDEV_NOTIFY_PEERS:
1408 rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
1409 break;
1410 case NETDEV_RESEND_IGMP:
1411 rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
1412 break;
1413 case NETDEV_CHANGEINFODATA:
1414 rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
1415 break;
1416 default:
1417 break;
1418 }
1419
1420 return rtnl_event_type;
1421}
1422
Florian Westphal79110a02017-09-26 13:58:40 +02001423static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
1424{
1425 const struct net_device *upper_dev;
1426 int ret = 0;
1427
1428 rcu_read_lock();
1429
1430 upper_dev = netdev_master_upper_dev_get_rcu(dev);
1431 if (upper_dev)
1432 ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);
1433
1434 rcu_read_unlock();
1435 return ret;
1436}
1437
1438static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev)
1439{
1440 int ifindex = dev_get_iflink(dev);
1441
1442 if (dev->ifindex == ifindex)
1443 return 0;
1444
1445 return nla_put_u32(skb, IFLA_LINK, ifindex);
1446}
1447
Florian Westphal6c557002017-10-02 23:50:05 +02001448static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
1449 struct net_device *dev)
1450{
1451 char buf[IFALIASZ];
1452 int ret;
1453
1454 ret = dev_get_alias(dev, buf, sizeof(buf));
1455 return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
1456}
1457
Florian Westphalb1e66b92017-09-26 13:58:42 +02001458static int rtnl_fill_link_netnsid(struct sk_buff *skb,
Jiri Benc79e1ad12017-11-02 17:04:38 -02001459 const struct net_device *dev,
1460 struct net *src_net)
Florian Westphalb1e66b92017-09-26 13:58:42 +02001461{
1462 if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
1463 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1464
1465 if (!net_eq(dev_net(dev), link_net)) {
Jiri Benc79e1ad12017-11-02 17:04:38 -02001466 int id = peernet2id_alloc(src_net, link_net);
Florian Westphalb1e66b92017-09-26 13:58:42 +02001467
1468 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1469 return -EMSGSIZE;
1470 }
1471 }
1472
1473 return 0;
1474}
1475
Florian Westphal070cbf52017-10-16 15:44:35 +02001476static int rtnl_fill_link_af(struct sk_buff *skb,
1477 const struct net_device *dev,
1478 u32 ext_filter_mask)
1479{
1480 const struct rtnl_af_ops *af_ops;
1481 struct nlattr *af_spec;
1482
1483 af_spec = nla_nest_start(skb, IFLA_AF_SPEC);
1484 if (!af_spec)
1485 return -EMSGSIZE;
1486
Florian Westphal5fa85a02017-10-16 15:44:36 +02001487 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Florian Westphal070cbf52017-10-16 15:44:35 +02001488 struct nlattr *af;
1489 int err;
1490
1491 if (!af_ops->fill_link_af)
1492 continue;
1493
1494 af = nla_nest_start(skb, af_ops->family);
1495 if (!af)
1496 return -EMSGSIZE;
1497
1498 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1499 /*
1500 * Caller may return ENODATA to indicate that there
1501 * was no data to be dumped. This is not an error, it
1502 * means we should trim the attribute header and
1503 * continue.
1504 */
1505 if (err == -ENODATA)
1506 nla_nest_cancel(skb, af);
1507 else if (err < 0)
1508 return -EMSGSIZE;
1509
1510 nla_nest_end(skb, af);
1511 }
1512
1513 nla_nest_end(skb, af_spec);
1514 return 0;
1515}
1516
Jiri Benc79e1ad12017-11-02 17:04:38 -02001517static int rtnl_fill_ifinfo(struct sk_buff *skb,
1518 struct net_device *dev, struct net *src_net,
Patrick McHardy575c3e22007-05-22 17:00:49 -07001519 int type, u32 pid, u32 seq, u32 change,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001520 unsigned int flags, u32 ext_filter_mask,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01001521 u32 event, int *new_nsid, int new_ifindex,
1522 int tgt_netnsid)
Thomas Grafb60c5112006-08-04 23:05:34 -07001523{
1524 struct ifinfomsg *ifm;
1525 struct nlmsghdr *nlh;
1526
Eric Dumazet2907c352011-05-25 07:34:04 +00001527 ASSERT_RTNL();
Thomas Grafb60c5112006-08-04 23:05:34 -07001528 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1529 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08001530 return -EMSGSIZE;
Thomas Grafb60c5112006-08-04 23:05:34 -07001531
1532 ifm = nlmsg_data(nlh);
1533 ifm->ifi_family = AF_UNSPEC;
1534 ifm->__ifi_pad = 0;
1535 ifm->ifi_type = dev->type;
1536 ifm->ifi_index = dev->ifindex;
1537 ifm->ifi_flags = dev_get_flags(dev);
1538 ifm->ifi_change = change;
1539
Jiri Benc79e1ad12017-11-02 17:04:38 -02001540 if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_IF_NETNSID, tgt_netnsid))
1541 goto nla_put_failure;
1542
David S. Millera6574342012-04-01 20:12:00 -04001543 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1544 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1545 nla_put_u8(skb, IFLA_OPERSTATE,
1546 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1547 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1548 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1549 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
Ben Greearedbc0bb2012-03-29 12:51:30 +00001550 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001551 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
Eric Dumazetc70ce022016-03-21 09:55:10 -07001552 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1553 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001554#ifdef CONFIG_RPS
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001555 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001556#endif
Florian Westphal79110a02017-09-26 13:58:40 +02001557 nla_put_iflink(skb, dev) ||
1558 put_master_ifindex(skb, dev) ||
Jiri Pirko9a572472012-12-27 23:49:39 +00001559 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
David S. Millera6574342012-04-01 20:12:00 -04001560 (dev->qdisc &&
1561 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
Florian Westphal6c557002017-10-02 23:50:05 +02001562 nla_put_ifalias(skb, dev) ||
david decotigny2d3b4792014-03-29 09:48:35 -07001563 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
David Decotignyb2d3bcf2018-01-18 09:59:13 -08001564 atomic_read(&dev->carrier_up_count) +
1565 atomic_read(&dev->carrier_down_count)) ||
1566 nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down) ||
1567 nla_put_u32(skb, IFLA_CARRIER_UP_COUNT,
1568 atomic_read(&dev->carrier_up_count)) ||
1569 nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT,
1570 atomic_read(&dev->carrier_down_count)))
David S. Millera6574342012-04-01 20:12:00 -04001571 goto nla_put_failure;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001572
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001573 if (event != IFLA_EVENT_NONE) {
1574 if (nla_put_u32(skb, IFLA_EVENT, event))
1575 goto nla_put_failure;
1576 }
1577
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001578 if (rtnl_fill_link_ifmap(skb, dev))
1579 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
1581 if (dev->addr_len) {
David S. Millera6574342012-04-01 20:12:00 -04001582 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1583 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1584 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 }
1586
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001587 if (rtnl_phys_port_id_fill(skb, dev))
1588 goto nla_put_failure;
1589
David Aherndb24a902015-03-17 20:23:15 -06001590 if (rtnl_phys_port_name_fill(skb, dev))
1591 goto nla_put_failure;
1592
Jiri Pirko82f28412014-11-28 14:34:18 +01001593 if (rtnl_phys_switch_id_fill(skb, dev))
1594 goto nla_put_failure;
1595
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001596 if (rtnl_fill_stats(skb, dev))
Pavel Emelyanov96e74082008-05-21 14:12:46 -07001597 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
Florian Westphal250fc3d2017-09-26 13:58:41 +02001599 if (rtnl_fill_vf(skb, dev, ext_filter_mask))
David S. Millera6574342012-04-01 20:12:00 -04001600 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001601
David Gibsonc53864f2014-04-24 10:22:36 +10001602 if (rtnl_port_fill(skb, dev, ext_filter_mask))
Scott Feldman57b61082010-05-17 22:49:55 -07001603 goto nla_put_failure;
1604
Brenden Blancod1fdd912016-07-19 12:16:49 -07001605 if (rtnl_xdp_fill(skb, dev))
1606 goto nla_put_failure;
1607
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001608 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07001609 if (rtnl_link_fill(skb, dev) < 0)
1610 goto nla_put_failure;
1611 }
1612
Jiri Benc79e1ad12017-11-02 17:04:38 -02001613 if (rtnl_fill_link_netnsid(skb, dev, src_net))
Florian Westphalb1e66b92017-09-26 13:58:42 +02001614 goto nla_put_failure;
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001615
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02001616 if (new_nsid &&
1617 nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0)
1618 goto nla_put_failure;
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01001619 if (new_ifindex &&
1620 nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0)
1621 goto nla_put_failure;
1622
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02001623
Florian Westphal5fa85a02017-10-16 15:44:36 +02001624 rcu_read_lock();
Florian Westphal070cbf52017-10-16 15:44:35 +02001625 if (rtnl_fill_link_af(skb, dev, ext_filter_mask))
Florian Westphal5fa85a02017-10-16 15:44:36 +02001626 goto nla_put_failure_rcu;
1627 rcu_read_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +00001628
Johannes Berg053c0952015-01-16 22:09:00 +01001629 nlmsg_end(skb, nlh);
1630 return 0;
Thomas Grafb60c5112006-08-04 23:05:34 -07001631
Florian Westphal5fa85a02017-10-16 15:44:36 +02001632nla_put_failure_rcu:
1633 rcu_read_unlock();
Thomas Grafb60c5112006-08-04 23:05:34 -07001634nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001635 nlmsg_cancel(skb, nlh);
1636 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637}
1638
Jiri Pirkof7b12602014-02-18 20:53:18 +01001639static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1640 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1641 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1642 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1643 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1644 [IFLA_MTU] = { .type = NLA_U32 },
1645 [IFLA_LINK] = { .type = NLA_U32 },
1646 [IFLA_MASTER] = { .type = NLA_U32 },
1647 [IFLA_CARRIER] = { .type = NLA_U8 },
1648 [IFLA_TXQLEN] = { .type = NLA_U32 },
1649 [IFLA_WEIGHT] = { .type = NLA_U32 },
1650 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1651 [IFLA_LINKMODE] = { .type = NLA_U8 },
1652 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1653 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1654 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
Nicolas Dichtel2459b4c2017-10-11 16:24:48 +02001655 /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
1656 * allow 0-length string (needed to remove an alias).
1657 */
1658 [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001659 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1660 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1661 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1662 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1663 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1664 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1665 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1666 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
Stephen Hemminger46e6b992017-12-07 15:40:19 -08001667 [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 },
1668 [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 },
Jiri Pirko02637fc2014-11-28 14:34:16 +01001669 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
david decotigny2d3b4792014-03-29 09:48:35 -07001670 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
Jiri Pirko82f28412014-11-28 14:34:18 +01001671 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
Nicolas Dichtel317f4812015-01-15 15:11:18 +01001672 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001673 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001674 [IFLA_XDP] = { .type = NLA_NESTED },
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001675 [IFLA_EVENT] = { .type = NLA_U32 },
Serhey Popovychdb833d42017-06-20 14:35:23 +03001676 [IFLA_GROUP] = { .type = NLA_U32 },
Jiri Benc79e1ad12017-11-02 17:04:38 -02001677 [IFLA_IF_NETNSID] = { .type = NLA_S32 },
David Decotignyb2d3bcf2018-01-18 09:59:13 -08001678 [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 },
1679 [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001680};
1681
1682static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1683 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1684 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1685 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1686 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1687};
1688
Jiri Pirkof7b12602014-02-18 20:53:18 +01001689static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
Daniel Borkmann364d5712015-02-05 18:44:04 +01001690 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1691 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
Moshe Shemesh79aab092016-09-22 12:11:15 +03001692 [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED },
Daniel Borkmann364d5712015-02-05 18:44:04 +01001693 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1694 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1695 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1696 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001697 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001698 [IFLA_VF_STATS] = { .type = NLA_NESTED },
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00001699 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
Eli Cohencc8e27c2016-03-11 22:58:34 +02001700 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1701 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001702};
1703
Jiri Pirkof7b12602014-02-18 20:53:18 +01001704static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1705 [IFLA_PORT_VF] = { .type = NLA_U32 },
1706 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1707 .len = PORT_PROFILE_MAX },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001708 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1709 .len = PORT_UUID_MAX },
1710 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1711 .len = PORT_UUID_MAX },
1712 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1713 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
Daniel Borkmann025331d2017-02-17 01:56:11 +01001714
1715 /* Unused, but we need to keep it here since user space could
1716 * fill it. It's also broken with regard to NLA_BINARY use in
1717 * combination with structs.
1718 */
1719 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1720 .len = sizeof(struct ifla_port_vsi) },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001721};
1722
Brenden Blancod1fdd912016-07-19 12:16:49 -07001723static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
1724 [IFLA_XDP_FD] = { .type = NLA_S32 },
1725 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 },
Daniel Borkmann85de8572016-11-28 23:16:54 +01001726 [IFLA_XDP_FLAGS] = { .type = NLA_U32 },
Martin KaFai Lau58038692017-06-15 17:29:09 -07001727 [IFLA_XDP_PROG_ID] = { .type = NLA_U32 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001728};
1729
David Aherndc599f72016-02-02 08:17:07 -08001730static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1731{
1732 const struct rtnl_link_ops *ops = NULL;
1733 struct nlattr *linfo[IFLA_INFO_MAX + 1];
1734
Johannes Bergfceb6432017-04-12 14:34:07 +02001735 if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla,
1736 ifla_info_policy, NULL) < 0)
David Aherndc599f72016-02-02 08:17:07 -08001737 return NULL;
1738
1739 if (linfo[IFLA_INFO_KIND]) {
1740 char kind[MODULE_NAME_LEN];
1741
1742 nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1743 ops = rtnl_link_ops_get(kind);
1744 }
1745
1746 return ops;
1747}
1748
1749static bool link_master_filtered(struct net_device *dev, int master_idx)
1750{
1751 struct net_device *master;
1752
1753 if (!master_idx)
1754 return false;
1755
1756 master = netdev_master_upper_dev_get(dev);
1757 if (!master || master->ifindex != master_idx)
1758 return true;
1759
1760 return false;
1761}
1762
1763static bool link_kind_filtered(const struct net_device *dev,
1764 const struct rtnl_link_ops *kind_ops)
1765{
1766 if (kind_ops && dev->rtnl_link_ops != kind_ops)
1767 return true;
1768
1769 return false;
1770}
1771
1772static bool link_dump_filtered(struct net_device *dev,
1773 int master_idx,
1774 const struct rtnl_link_ops *kind_ops)
1775{
1776 if (link_master_filtered(dev, master_idx) ||
1777 link_kind_filtered(dev, kind_ops))
1778 return true;
1779
1780 return false;
1781}
1782
Andrei Vaginf428fe42018-01-02 23:27:33 -08001783static struct net *get_target_net(struct sock *sk, int netnsid)
Jiri Benc79e1ad12017-11-02 17:04:38 -02001784{
1785 struct net *net;
1786
Andrei Vaginf428fe42018-01-02 23:27:33 -08001787 net = get_net_ns_by_id(sock_net(sk), netnsid);
Jiri Benc79e1ad12017-11-02 17:04:38 -02001788 if (!net)
1789 return ERR_PTR(-EINVAL);
1790
1791 /* For now, the caller is required to have CAP_NET_ADMIN in
1792 * the user namespace owning the target net ns.
1793 */
Andrei Vaginf428fe42018-01-02 23:27:33 -08001794 if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) {
Jiri Benc79e1ad12017-11-02 17:04:38 -02001795 put_net(net);
1796 return ERR_PTR(-EACCES);
1797 }
1798 return net;
1799}
1800
Thomas Grafb60c5112006-08-04 23:05:34 -07001801static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001803 struct net *net = sock_net(skb->sk);
Jiri Benc79e1ad12017-11-02 17:04:38 -02001804 struct net *tgt_net = net;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001805 int h, s_h;
1806 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 struct net_device *dev;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001808 struct hlist_head *head;
Greg Rose115c9b82012-02-21 16:54:48 -05001809 struct nlattr *tb[IFLA_MAX+1];
1810 u32 ext_filter_mask = 0;
David Aherndc599f72016-02-02 08:17:07 -08001811 const struct rtnl_link_ops *kind_ops = NULL;
1812 unsigned int flags = NLM_F_MULTI;
1813 int master_idx = 0;
Jiri Benc79e1ad12017-11-02 17:04:38 -02001814 int netnsid = -1;
David Gibson973462b2014-04-24 10:22:35 +10001815 int err;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001816 int hdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001818 s_h = cb->args[0];
1819 s_idx = cb->args[1];
1820
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001821 /* A hack to preserve kernel<->userspace interface.
1822 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1823 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1824 * what iproute2 < v3.9.0 used.
1825 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1826 * attribute, its netlink message is shorter than struct ifinfomsg.
1827 */
1828 hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1829 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1830
Johannes Bergfceb6432017-04-12 14:34:07 +02001831 if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX,
1832 ifla_policy, NULL) >= 0) {
Jiri Benc79e1ad12017-11-02 17:04:38 -02001833 if (tb[IFLA_IF_NETNSID]) {
1834 netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]);
Andrei Vaginf428fe42018-01-02 23:27:33 -08001835 tgt_net = get_target_net(skb->sk, netnsid);
Jiri Benc79e1ad12017-11-02 17:04:38 -02001836 if (IS_ERR(tgt_net)) {
1837 tgt_net = net;
1838 netnsid = -1;
1839 }
1840 }
1841
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001842 if (tb[IFLA_EXT_MASK])
1843 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
David Aherndc599f72016-02-02 08:17:07 -08001844
1845 if (tb[IFLA_MASTER])
1846 master_idx = nla_get_u32(tb[IFLA_MASTER]);
1847
1848 if (tb[IFLA_LINKINFO])
1849 kind_ops = linkinfo_to_kind_ops(tb[IFLA_LINKINFO]);
1850
1851 if (master_idx || kind_ops)
1852 flags |= NLM_F_DUMP_FILTERED;
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001853 }
Greg Rose115c9b82012-02-21 16:54:48 -05001854
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001855 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1856 idx = 0;
Jiri Benc79e1ad12017-11-02 17:04:38 -02001857 head = &tgt_net->dev_index_head[h];
Eric Dumazetcac5e652015-02-27 09:42:50 -08001858 hlist_for_each_entry(dev, head, index_hlist) {
David Aherndc599f72016-02-02 08:17:07 -08001859 if (link_dump_filtered(dev, master_idx, kind_ops))
Zhang Shengju3f0ae052016-11-19 23:28:32 +08001860 goto cont;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001861 if (idx < s_idx)
1862 goto cont;
Jiri Benc79e1ad12017-11-02 17:04:38 -02001863 err = rtnl_fill_ifinfo(skb, dev, net,
1864 RTM_NEWLINK,
David Gibson973462b2014-04-24 10:22:35 +10001865 NETLINK_CB(cb->skb).portid,
1866 cb->nlh->nlmsg_seq, 0,
David Aherndc599f72016-02-02 08:17:07 -08001867 flags,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01001868 ext_filter_mask, 0, NULL, 0,
Jiri Benc79e1ad12017-11-02 17:04:38 -02001869 netnsid);
David Gibson973462b2014-04-24 10:22:35 +10001870
David Ahernf6c57752017-05-15 23:19:17 -07001871 if (err < 0) {
1872 if (likely(skb->len))
1873 goto out;
1874
1875 goto out_err;
1876 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07001877cont:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001878 idx++;
1879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 }
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001881out:
David Ahernf6c57752017-05-15 23:19:17 -07001882 err = skb->len;
1883out_err:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001884 cb->args[1] = idx;
1885 cb->args[0] = h;
Jakub Sitnickid0225782017-08-09 17:39:12 +02001886 cb->seq = net->dev_base_seq;
1887 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Jiri Benc79e1ad12017-11-02 17:04:38 -02001888 if (netnsid >= 0)
1889 put_net(tgt_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
David Ahernf6c57752017-05-15 23:19:17 -07001891 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892}
1893
Johannes Bergfceb6432017-04-12 14:34:07 +02001894int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
1895 struct netlink_ext_ack *exterr)
Jiri Pirkof7b12602014-02-18 20:53:18 +01001896{
Johannes Bergfceb6432017-04-12 14:34:07 +02001897 return nla_parse(tb, IFLA_MAX, head, len, ifla_policy, exterr);
Jiri Pirkof7b12602014-02-18 20:53:18 +01001898}
1899EXPORT_SYMBOL(rtnl_nla_parse_ifla);
Scott Feldman57b61082010-05-17 22:49:55 -07001900
Eric W. Biederman81adee42009-11-08 00:53:51 -08001901struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1902{
1903 struct net *net;
1904 /* Examine the link attributes and figure out which
1905 * network namespace we are talking about.
1906 */
1907 if (tb[IFLA_NET_NS_PID])
1908 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001909 else if (tb[IFLA_NET_NS_FD])
1910 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
Eric W. Biederman81adee42009-11-08 00:53:51 -08001911 else
1912 net = get_net(src_net);
1913 return net;
1914}
1915EXPORT_SYMBOL(rtnl_link_get_net);
1916
Christian Brauner7c4f63b2018-01-24 15:26:32 +01001917/* Figure out which network namespace we are talking about by
1918 * examining the link attributes in the following order:
1919 *
1920 * 1. IFLA_NET_NS_PID
1921 * 2. IFLA_NET_NS_FD
1922 * 3. IFLA_IF_NETNSID
1923 */
1924static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net,
1925 struct nlattr *tb[])
1926{
1927 struct net *net;
1928
1929 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])
1930 return rtnl_link_get_net(src_net, tb);
1931
1932 if (!tb[IFLA_IF_NETNSID])
1933 return get_net(src_net);
1934
1935 net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_IF_NETNSID]));
1936 if (!net)
1937 return ERR_PTR(-EINVAL);
1938
1939 return net;
1940}
1941
1942static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb,
1943 struct net *src_net,
1944 struct nlattr *tb[], int cap)
1945{
1946 struct net *net;
1947
1948 net = rtnl_link_get_net_by_nlattr(src_net, tb);
1949 if (IS_ERR(net))
1950 return net;
1951
1952 if (!netlink_ns_capable(skb, net->user_ns, cap)) {
1953 put_net(net);
1954 return ERR_PTR(-EPERM);
1955 }
1956
1957 return net;
1958}
1959
Christian Brauner4ff66ca2018-02-07 13:53:20 +01001960/* Verify that rtnetlink requests do not pass additional properties
1961 * potentially referring to different network namespaces.
1962 */
1963static int rtnl_ensure_unique_netns(struct nlattr *tb[],
1964 struct netlink_ext_ack *extack,
1965 bool netns_id_only)
1966{
1967
1968 if (netns_id_only) {
1969 if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD])
1970 return 0;
1971
1972 NL_SET_ERR_MSG(extack, "specified netns attribute not supported");
1973 return -EOPNOTSUPP;
1974 }
1975
1976 if (tb[IFLA_IF_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]))
1977 goto invalid_attr;
1978
1979 if (tb[IFLA_NET_NS_PID] && (tb[IFLA_IF_NETNSID] || tb[IFLA_NET_NS_FD]))
1980 goto invalid_attr;
1981
1982 if (tb[IFLA_NET_NS_FD] && (tb[IFLA_IF_NETNSID] || tb[IFLA_NET_NS_PID]))
1983 goto invalid_attr;
1984
1985 return 0;
1986
1987invalid_attr:
1988 NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified");
1989 return -EINVAL;
1990}
1991
Thomas Graf1840bb12008-02-23 19:54:36 -08001992static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1993{
1994 if (dev) {
1995 if (tb[IFLA_ADDRESS] &&
1996 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1997 return -EINVAL;
1998
1999 if (tb[IFLA_BROADCAST] &&
2000 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
2001 return -EINVAL;
2002 }
2003
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002004 if (tb[IFLA_AF_SPEC]) {
2005 struct nlattr *af;
2006 int rem, err;
2007
2008 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2009 const struct rtnl_af_ops *af_ops;
2010
Florian Westphal5fa85a02017-10-16 15:44:36 +02002011 rcu_read_lock();
2012 af_ops = rtnl_af_lookup(nla_type(af));
2013 if (!af_ops) {
2014 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002015 return -EAFNOSUPPORT;
Florian Westphal5fa85a02017-10-16 15:44:36 +02002016 }
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002017
Florian Westphal5fa85a02017-10-16 15:44:36 +02002018 if (!af_ops->set_link_af) {
2019 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002020 return -EOPNOTSUPP;
Florian Westphal5fa85a02017-10-16 15:44:36 +02002021 }
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002022
2023 if (af_ops->validate_link_af) {
Kurt Van Dijck6d3a9a62011-01-26 04:55:24 +00002024 err = af_ops->validate_link_af(dev, af);
Florian Westphal5fa85a02017-10-16 15:44:36 +02002025 if (err < 0) {
2026 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002027 return err;
Florian Westphal5fa85a02017-10-16 15:44:36 +02002028 }
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002029 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02002030
2031 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002032 }
2033 }
2034
Thomas Graf1840bb12008-02-23 19:54:36 -08002035 return 0;
2036}
2037
Eli Cohencc8e27c2016-03-11 22:58:34 +02002038static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
2039 int guid_type)
2040{
2041 const struct net_device_ops *ops = dev->netdev_ops;
2042
2043 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
2044}
2045
2046static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
2047{
2048 if (dev->type != ARPHRD_INFINIBAND)
2049 return -EOPNOTSUPP;
2050
2051 return handle_infiniband_guid(dev, ivt, guid_type);
2052}
2053
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002054static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
Chris Wrightc02db8c2010-05-16 01:05:45 -07002055{
Chris Wrightc02db8c2010-05-16 01:05:45 -07002056 const struct net_device_ops *ops = dev->netdev_ops;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002057 int err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002058
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002059 if (tb[IFLA_VF_MAC]) {
2060 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03002061
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002062 err = -EOPNOTSUPP;
2063 if (ops->ndo_set_vf_mac)
2064 err = ops->ndo_set_vf_mac(dev, ivm->vf,
2065 ivm->mac);
2066 if (err < 0)
2067 return err;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002068 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002069
2070 if (tb[IFLA_VF_VLAN]) {
2071 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
2072
2073 err = -EOPNOTSUPP;
2074 if (ops->ndo_set_vf_vlan)
2075 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
Moshe Shemesh79aab092016-09-22 12:11:15 +03002076 ivv->qos,
2077 htons(ETH_P_8021Q));
2078 if (err < 0)
2079 return err;
2080 }
2081
2082 if (tb[IFLA_VF_VLAN_LIST]) {
2083 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
2084 struct nlattr *attr;
2085 int rem, len = 0;
2086
2087 err = -EOPNOTSUPP;
2088 if (!ops->ndo_set_vf_vlan)
2089 return err;
2090
2091 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
2092 if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
2093 nla_len(attr) < NLA_HDRLEN) {
2094 return -EINVAL;
2095 }
2096 if (len >= MAX_VLAN_LIST_LEN)
2097 return -EOPNOTSUPP;
2098 ivvl[len] = nla_data(attr);
2099
2100 len++;
2101 }
Arnd Bergmannfa34cd92016-09-30 18:13:49 +02002102 if (len == 0)
2103 return -EINVAL;
2104
Moshe Shemesh79aab092016-09-22 12:11:15 +03002105 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
2106 ivvl[0]->qos, ivvl[0]->vlan_proto);
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002107 if (err < 0)
2108 return err;
2109 }
2110
2111 if (tb[IFLA_VF_TX_RATE]) {
2112 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
2113 struct ifla_vf_info ivf;
2114
2115 err = -EOPNOTSUPP;
2116 if (ops->ndo_get_vf_config)
2117 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
2118 if (err < 0)
2119 return err;
2120
2121 err = -EOPNOTSUPP;
2122 if (ops->ndo_set_vf_rate)
2123 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2124 ivf.min_tx_rate,
2125 ivt->rate);
2126 if (err < 0)
2127 return err;
2128 }
2129
2130 if (tb[IFLA_VF_RATE]) {
2131 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
2132
2133 err = -EOPNOTSUPP;
2134 if (ops->ndo_set_vf_rate)
2135 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2136 ivt->min_tx_rate,
2137 ivt->max_tx_rate);
2138 if (err < 0)
2139 return err;
2140 }
2141
2142 if (tb[IFLA_VF_SPOOFCHK]) {
2143 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
2144
2145 err = -EOPNOTSUPP;
2146 if (ops->ndo_set_vf_spoofchk)
2147 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
2148 ivs->setting);
2149 if (err < 0)
2150 return err;
2151 }
2152
2153 if (tb[IFLA_VF_LINK_STATE]) {
2154 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
2155
2156 err = -EOPNOTSUPP;
2157 if (ops->ndo_set_vf_link_state)
2158 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
2159 ivl->link_state);
2160 if (err < 0)
2161 return err;
2162 }
2163
2164 if (tb[IFLA_VF_RSS_QUERY_EN]) {
2165 struct ifla_vf_rss_query_en *ivrssq_en;
2166
2167 err = -EOPNOTSUPP;
2168 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
2169 if (ops->ndo_set_vf_rss_query_en)
2170 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
2171 ivrssq_en->setting);
2172 if (err < 0)
2173 return err;
2174 }
2175
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00002176 if (tb[IFLA_VF_TRUST]) {
2177 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
2178
2179 err = -EOPNOTSUPP;
2180 if (ops->ndo_set_vf_trust)
2181 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
2182 if (err < 0)
2183 return err;
2184 }
2185
Eli Cohencc8e27c2016-03-11 22:58:34 +02002186 if (tb[IFLA_VF_IB_NODE_GUID]) {
2187 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
2188
2189 if (!ops->ndo_set_vf_guid)
2190 return -EOPNOTSUPP;
2191
2192 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
2193 }
2194
2195 if (tb[IFLA_VF_IB_PORT_GUID]) {
2196 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
2197
2198 if (!ops->ndo_set_vf_guid)
2199 return -EOPNOTSUPP;
2200
2201 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
2202 }
2203
Chris Wrightc02db8c2010-05-16 01:05:45 -07002204 return err;
2205}
2206
David Ahern33eaf2a2017-10-04 17:48:46 -07002207static int do_set_master(struct net_device *dev, int ifindex,
2208 struct netlink_ext_ack *extack)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002209{
Jiri Pirko898e5062013-01-03 22:48:52 +00002210 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002211 const struct net_device_ops *ops;
2212 int err;
2213
Jiri Pirko898e5062013-01-03 22:48:52 +00002214 if (upper_dev) {
2215 if (upper_dev->ifindex == ifindex)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002216 return 0;
Jiri Pirko898e5062013-01-03 22:48:52 +00002217 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002218 if (ops->ndo_del_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002219 err = ops->ndo_del_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002220 if (err)
2221 return err;
2222 } else {
2223 return -EOPNOTSUPP;
2224 }
2225 }
2226
2227 if (ifindex) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002228 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
2229 if (!upper_dev)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002230 return -EINVAL;
Jiri Pirko898e5062013-01-03 22:48:52 +00002231 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002232 if (ops->ndo_add_slave) {
David Ahern33eaf2a2017-10-04 17:48:46 -07002233 err = ops->ndo_add_slave(upper_dev, dev, extack);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002234 if (err)
2235 return err;
2236 } else {
2237 return -EOPNOTSUPP;
2238 }
2239 }
2240 return 0;
2241}
2242
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002243#define DO_SETLINK_MODIFIED 0x01
Nicolas Dichtelba998902014-09-01 16:07:29 +02002244/* notify flag means notify + modified. */
2245#define DO_SETLINK_NOTIFY 0x03
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002246static int do_setlink(const struct sk_buff *skb,
2247 struct net_device *dev, struct ifinfomsg *ifm,
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002248 struct netlink_ext_ack *extack,
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002249 struct nlattr **tb, char *ifname, int status)
Patrick McHardy0157f602007-06-13 12:03:36 -07002250{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002251 const struct net_device_ops *ops = dev->netdev_ops;
Patrick McHardy0157f602007-06-13 12:03:36 -07002252 int err;
2253
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002254 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_IF_NETNSID]) {
2255 struct net *net = rtnl_link_get_net_capable(skb, dev_net(dev),
2256 tb, CAP_NET_ADMIN);
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002257 if (IS_ERR(net)) {
2258 err = PTR_ERR(net);
2259 goto errout;
2260 }
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002261
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002262 err = dev_change_net_namespace(dev, net, ifname);
2263 put_net(net);
2264 if (err)
2265 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002266 status |= DO_SETLINK_MODIFIED;
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002267 }
2268
Patrick McHardy0157f602007-06-13 12:03:36 -07002269 if (tb[IFLA_MAP]) {
2270 struct rtnl_link_ifmap *u_map;
2271 struct ifmap k_map;
2272
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002273 if (!ops->ndo_set_config) {
Patrick McHardy0157f602007-06-13 12:03:36 -07002274 err = -EOPNOTSUPP;
2275 goto errout;
2276 }
2277
2278 if (!netif_device_present(dev)) {
2279 err = -ENODEV;
2280 goto errout;
2281 }
2282
2283 u_map = nla_data(tb[IFLA_MAP]);
2284 k_map.mem_start = (unsigned long) u_map->mem_start;
2285 k_map.mem_end = (unsigned long) u_map->mem_end;
2286 k_map.base_addr = (unsigned short) u_map->base_addr;
2287 k_map.irq = (unsigned char) u_map->irq;
2288 k_map.dma = (unsigned char) u_map->dma;
2289 k_map.port = (unsigned char) u_map->port;
2290
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002291 err = ops->ndo_set_config(dev, &k_map);
Patrick McHardy0157f602007-06-13 12:03:36 -07002292 if (err < 0)
2293 goto errout;
2294
Nicolas Dichtelba998902014-09-01 16:07:29 +02002295 status |= DO_SETLINK_NOTIFY;
Patrick McHardy0157f602007-06-13 12:03:36 -07002296 }
2297
2298 if (tb[IFLA_ADDRESS]) {
2299 struct sockaddr *sa;
2300 int len;
2301
WANG Cong153711f2017-07-20 11:27:57 -07002302 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2303 sizeof(*sa));
Patrick McHardy0157f602007-06-13 12:03:36 -07002304 sa = kmalloc(len, GFP_KERNEL);
2305 if (!sa) {
2306 err = -ENOMEM;
2307 goto errout;
2308 }
2309 sa->sa_family = dev->type;
2310 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
2311 dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00002312 err = dev_set_mac_address(dev, sa);
Patrick McHardy0157f602007-06-13 12:03:36 -07002313 kfree(sa);
2314 if (err)
2315 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002316 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002317 }
2318
2319 if (tb[IFLA_MTU]) {
2320 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
2321 if (err < 0)
2322 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002323 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002324 }
2325
Vlad Dogarucbda10f2011-01-13 23:38:30 +00002326 if (tb[IFLA_GROUP]) {
2327 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Nicolas Dichtelba998902014-09-01 16:07:29 +02002328 status |= DO_SETLINK_NOTIFY;
Vlad Dogarucbda10f2011-01-13 23:38:30 +00002329 }
2330
Patrick McHardy0157f602007-06-13 12:03:36 -07002331 /*
2332 * Interface selected by interface index but interface
2333 * name provided implies that a name change has been
2334 * requested.
2335 */
2336 if (ifm->ifi_index > 0 && ifname[0]) {
2337 err = dev_change_name(dev, ifname);
2338 if (err < 0)
2339 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002340 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002341 }
2342
Stephen Hemminger0b815a12008-09-22 21:28:11 -07002343 if (tb[IFLA_IFALIAS]) {
2344 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2345 nla_len(tb[IFLA_IFALIAS]));
2346 if (err < 0)
2347 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002348 status |= DO_SETLINK_NOTIFY;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07002349 }
2350
Patrick McHardy0157f602007-06-13 12:03:36 -07002351 if (tb[IFLA_BROADCAST]) {
2352 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00002353 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Patrick McHardy0157f602007-06-13 12:03:36 -07002354 }
2355
2356 if (ifm->ifi_flags || ifm->ifi_change) {
Patrick McHardy3729d502010-02-26 06:34:54 +00002357 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
Johannes Berg5f9021c2008-11-16 23:20:31 -08002358 if (err < 0)
2359 goto errout;
Patrick McHardy0157f602007-06-13 12:03:36 -07002360 }
2361
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002362 if (tb[IFLA_MASTER]) {
David Ahern33eaf2a2017-10-04 17:48:46 -07002363 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002364 if (err)
2365 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002366 status |= DO_SETLINK_MODIFIED;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002367 }
2368
Jiri Pirko9a572472012-12-27 23:49:39 +00002369 if (tb[IFLA_CARRIER]) {
2370 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2371 if (err)
2372 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002373 status |= DO_SETLINK_MODIFIED;
Jiri Pirko9a572472012-12-27 23:49:39 +00002374 }
2375
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002376 if (tb[IFLA_TXQLEN]) {
Alexey Dobriyan0cd29502017-05-17 13:30:44 +03002377 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002378
Cong Wang6a643dd2018-01-25 18:26:22 -08002379 err = dev_change_tx_queue_len(dev, value);
2380 if (err)
2381 goto errout;
2382 status |= DO_SETLINK_MODIFIED;
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002383 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002384
Stephen Hemminger46e6b992017-12-07 15:40:19 -08002385 if (tb[IFLA_GSO_MAX_SIZE]) {
2386 u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]);
2387
2388 if (max_size > GSO_MAX_SIZE) {
2389 err = -EINVAL;
2390 goto errout;
2391 }
2392
2393 if (dev->gso_max_size ^ max_size) {
2394 netif_set_gso_max_size(dev, max_size);
2395 status |= DO_SETLINK_MODIFIED;
2396 }
2397 }
2398
2399 if (tb[IFLA_GSO_MAX_SEGS]) {
2400 u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
2401
2402 if (max_segs > GSO_MAX_SEGS) {
2403 err = -EINVAL;
2404 goto errout;
2405 }
2406
2407 if (dev->gso_max_segs ^ max_segs) {
2408 dev->gso_max_segs = max_segs;
2409 status |= DO_SETLINK_MODIFIED;
2410 }
2411 }
2412
Patrick McHardy0157f602007-06-13 12:03:36 -07002413 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002414 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Patrick McHardy0157f602007-06-13 12:03:36 -07002415
2416 if (tb[IFLA_LINKMODE]) {
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002417 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2418
David S. Miller93b2d4a2008-02-17 18:35:07 -08002419 write_lock_bh(&dev_base_lock);
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002420 if (dev->link_mode ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02002421 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002422 dev->link_mode = value;
David S. Miller93b2d4a2008-02-17 18:35:07 -08002423 write_unlock_bh(&dev_base_lock);
Patrick McHardy0157f602007-06-13 12:03:36 -07002424 }
2425
Chris Wrightc02db8c2010-05-16 01:05:45 -07002426 if (tb[IFLA_VFINFO_LIST]) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002427 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
Chris Wrightc02db8c2010-05-16 01:05:45 -07002428 struct nlattr *attr;
2429 int rem;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002430
Chris Wrightc02db8c2010-05-16 01:05:45 -07002431 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002432 if (nla_type(attr) != IFLA_VF_INFO ||
2433 nla_len(attr) < NLA_HDRLEN) {
David Howells253683b2010-05-21 02:25:27 +00002434 err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002435 goto errout;
David Howells253683b2010-05-21 02:25:27 +00002436 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002437 err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr,
Johannes Bergfceb6432017-04-12 14:34:07 +02002438 ifla_vf_policy, NULL);
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002439 if (err < 0)
2440 goto errout;
2441 err = do_setvfinfo(dev, vfinfo);
Chris Wrightc02db8c2010-05-16 01:05:45 -07002442 if (err < 0)
2443 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002444 status |= DO_SETLINK_NOTIFY;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002445 }
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00002446 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002447 err = 0;
2448
Scott Feldman57b61082010-05-17 22:49:55 -07002449 if (tb[IFLA_VF_PORTS]) {
2450 struct nlattr *port[IFLA_PORT_MAX+1];
2451 struct nlattr *attr;
2452 int vf;
2453 int rem;
2454
2455 err = -EOPNOTSUPP;
2456 if (!ops->ndo_set_vf_port)
2457 goto errout;
2458
2459 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
Daniel Borkmann035d2102015-07-13 00:06:02 +02002460 if (nla_type(attr) != IFLA_VF_PORT ||
2461 nla_len(attr) < NLA_HDRLEN) {
2462 err = -EINVAL;
2463 goto errout;
2464 }
2465 err = nla_parse_nested(port, IFLA_PORT_MAX, attr,
Johannes Bergfceb6432017-04-12 14:34:07 +02002466 ifla_port_policy, NULL);
Scott Feldman57b61082010-05-17 22:49:55 -07002467 if (err < 0)
2468 goto errout;
2469 if (!port[IFLA_PORT_VF]) {
2470 err = -EOPNOTSUPP;
2471 goto errout;
2472 }
2473 vf = nla_get_u32(port[IFLA_PORT_VF]);
2474 err = ops->ndo_set_vf_port(dev, vf, port);
2475 if (err < 0)
2476 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002477 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07002478 }
2479 }
2480 err = 0;
2481
2482 if (tb[IFLA_PORT_SELF]) {
2483 struct nlattr *port[IFLA_PORT_MAX+1];
2484
2485 err = nla_parse_nested(port, IFLA_PORT_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +02002486 tb[IFLA_PORT_SELF], ifla_port_policy,
2487 NULL);
Scott Feldman57b61082010-05-17 22:49:55 -07002488 if (err < 0)
2489 goto errout;
2490
2491 err = -EOPNOTSUPP;
2492 if (ops->ndo_set_vf_port)
2493 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
2494 if (err < 0)
2495 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002496 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07002497 }
Thomas Graff8ff1822010-11-16 04:30:14 +00002498
2499 if (tb[IFLA_AF_SPEC]) {
2500 struct nlattr *af;
2501 int rem;
2502
2503 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2504 const struct rtnl_af_ops *af_ops;
2505
Florian Westphal5fa85a02017-10-16 15:44:36 +02002506 rcu_read_lock();
2507
Gustavo A. R. Silva058c8d52017-10-20 19:43:11 -05002508 BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
Thomas Graff8ff1822010-11-16 04:30:14 +00002509
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002510 err = af_ops->set_link_af(dev, af);
Florian Westphal5fa85a02017-10-16 15:44:36 +02002511 if (err < 0) {
2512 rcu_read_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +00002513 goto errout;
Florian Westphal5fa85a02017-10-16 15:44:36 +02002514 }
Thomas Graff8ff1822010-11-16 04:30:14 +00002515
Florian Westphal5fa85a02017-10-16 15:44:36 +02002516 rcu_read_unlock();
Nicolas Dichtelba998902014-09-01 16:07:29 +02002517 status |= DO_SETLINK_NOTIFY;
Thomas Graff8ff1822010-11-16 04:30:14 +00002518 }
2519 }
Scott Feldman57b61082010-05-17 22:49:55 -07002520 err = 0;
2521
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07002522 if (tb[IFLA_PROTO_DOWN]) {
2523 err = dev_change_proto_down(dev,
2524 nla_get_u8(tb[IFLA_PROTO_DOWN]));
2525 if (err)
2526 goto errout;
2527 status |= DO_SETLINK_NOTIFY;
2528 }
2529
Brenden Blancod1fdd912016-07-19 12:16:49 -07002530 if (tb[IFLA_XDP]) {
2531 struct nlattr *xdp[IFLA_XDP_MAX + 1];
Daniel Borkmann85de8572016-11-28 23:16:54 +01002532 u32 xdp_flags = 0;
Brenden Blancod1fdd912016-07-19 12:16:49 -07002533
2534 err = nla_parse_nested(xdp, IFLA_XDP_MAX, tb[IFLA_XDP],
Johannes Bergfceb6432017-04-12 14:34:07 +02002535 ifla_xdp_policy, NULL);
Brenden Blancod1fdd912016-07-19 12:16:49 -07002536 if (err < 0)
2537 goto errout;
2538
Martin KaFai Lau58038692017-06-15 17:29:09 -07002539 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
Brenden Blanco262d8622016-07-20 17:22:34 -07002540 err = -EINVAL;
2541 goto errout;
2542 }
Daniel Borkmann85de8572016-11-28 23:16:54 +01002543
2544 if (xdp[IFLA_XDP_FLAGS]) {
2545 xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
2546 if (xdp_flags & ~XDP_FLAGS_MASK) {
2547 err = -EINVAL;
2548 goto errout;
2549 }
Jakub Kicinskiee5d0322017-06-21 18:25:04 -07002550 if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
Daniel Borkmann0489df92017-05-12 01:04:45 +02002551 err = -EINVAL;
2552 goto errout;
2553 }
Daniel Borkmann85de8572016-11-28 23:16:54 +01002554 }
2555
Brenden Blancod1fdd912016-07-19 12:16:49 -07002556 if (xdp[IFLA_XDP_FD]) {
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002557 err = dev_change_xdp_fd(dev, extack,
Daniel Borkmann85de8572016-11-28 23:16:54 +01002558 nla_get_s32(xdp[IFLA_XDP_FD]),
2559 xdp_flags);
Brenden Blancod1fdd912016-07-19 12:16:49 -07002560 if (err)
2561 goto errout;
2562 status |= DO_SETLINK_NOTIFY;
2563 }
2564 }
2565
Patrick McHardy0157f602007-06-13 12:03:36 -07002566errout:
Nicolas Dichtelba998902014-09-01 16:07:29 +02002567 if (status & DO_SETLINK_MODIFIED) {
Xin Long64ff90c2017-10-15 18:13:45 +08002568 if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
Nicolas Dichtelba998902014-09-01 16:07:29 +02002569 netdev_state_change(dev);
2570
2571 if (err < 0)
2572 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",
2573 dev->name);
2574 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002575
Patrick McHardy0157f602007-06-13 12:03:36 -07002576 return err;
2577}
2578
David Ahernc21ef3e2017-04-16 09:48:24 -07002579static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2580 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002582 struct net *net = sock_net(skb->sk);
Thomas Grafda5e0492006-08-10 21:17:37 -07002583 struct ifinfomsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 struct net_device *dev;
Patrick McHardy0157f602007-06-13 12:03:36 -07002585 int err;
Thomas Grafda5e0492006-08-10 21:17:37 -07002586 struct nlattr *tb[IFLA_MAX+1];
2587 char ifname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588
David Ahernc21ef3e2017-04-16 09:48:24 -07002589 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy,
2590 extack);
Thomas Grafda5e0492006-08-10 21:17:37 -07002591 if (err < 0)
2592 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002594 err = rtnl_ensure_unique_netns(tb, extack, false);
2595 if (err < 0)
2596 goto errout;
2597
Thomas Graf5176f912006-08-26 20:13:18 -07002598 if (tb[IFLA_IFNAME])
2599 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
Patrick McHardy78e5b8912006-09-13 20:35:36 -07002600 else
2601 ifname[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 err = -EINVAL;
Thomas Grafda5e0492006-08-10 21:17:37 -07002604 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -07002605 if (ifm->ifi_index > 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00002606 dev = __dev_get_by_index(net, ifm->ifi_index);
Thomas Grafda5e0492006-08-10 21:17:37 -07002607 else if (tb[IFLA_IFNAME])
Eric Dumazeta3d12892009-10-21 10:59:31 +00002608 dev = __dev_get_by_name(net, ifname);
Thomas Grafda5e0492006-08-10 21:17:37 -07002609 else
2610 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611
Thomas Grafda5e0492006-08-10 21:17:37 -07002612 if (dev == NULL) {
2613 err = -ENODEV;
2614 goto errout;
2615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
Eric Dumazete0d087a2009-11-07 01:26:17 -08002617 err = validate_linkmsg(dev, tb);
2618 if (err < 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00002619 goto errout;
Thomas Grafda5e0492006-08-10 21:17:37 -07002620
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002621 err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
Thomas Grafda5e0492006-08-10 21:17:37 -07002622errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 return err;
2624}
2625
WANG Cong66400d52015-03-24 11:53:31 -07002626static int rtnl_group_dellink(const struct net *net, int group)
2627{
2628 struct net_device *dev, *aux;
2629 LIST_HEAD(list_kill);
2630 bool found = false;
2631
2632 if (!group)
2633 return -EPERM;
2634
2635 for_each_netdev(net, dev) {
2636 if (dev->group == group) {
2637 const struct rtnl_link_ops *ops;
2638
2639 found = true;
2640 ops = dev->rtnl_link_ops;
2641 if (!ops || !ops->dellink)
2642 return -EOPNOTSUPP;
2643 }
2644 }
2645
2646 if (!found)
2647 return -ENODEV;
2648
2649 for_each_netdev_safe(net, dev, aux) {
2650 if (dev->group == group) {
2651 const struct rtnl_link_ops *ops;
2652
2653 ops = dev->rtnl_link_ops;
2654 ops->dellink(dev, &list_kill);
2655 }
2656 }
2657 unregister_netdevice_many(&list_kill);
2658
2659 return 0;
2660}
2661
Thomas Graf614732e2015-07-21 10:44:06 +02002662int rtnl_delete_link(struct net_device *dev)
2663{
2664 const struct rtnl_link_ops *ops;
2665 LIST_HEAD(list_kill);
2666
2667 ops = dev->rtnl_link_ops;
2668 if (!ops || !ops->dellink)
2669 return -EOPNOTSUPP;
2670
2671 ops->dellink(dev, &list_kill);
2672 unregister_netdevice_many(&list_kill);
2673
2674 return 0;
2675}
2676EXPORT_SYMBOL_GPL(rtnl_delete_link);
2677
David Ahernc21ef3e2017-04-16 09:48:24 -07002678static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
2679 struct netlink_ext_ack *extack)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002680{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002681 struct net *net = sock_net(skb->sk);
Christian Braunerb61ad682018-01-24 15:26:34 +01002682 struct net *tgt_net = net;
2683 struct net_device *dev = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002684 struct ifinfomsg *ifm;
2685 char ifname[IFNAMSIZ];
2686 struct nlattr *tb[IFLA_MAX+1];
2687 int err;
Christian Braunerb61ad682018-01-24 15:26:34 +01002688 int netnsid = -1;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002689
David Ahernc21ef3e2017-04-16 09:48:24 -07002690 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002691 if (err < 0)
2692 return err;
2693
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002694 err = rtnl_ensure_unique_netns(tb, extack, true);
2695 if (err < 0)
2696 return err;
2697
Patrick McHardy38f7b872007-06-13 12:03:51 -07002698 if (tb[IFLA_IFNAME])
2699 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2700
Christian Braunerb61ad682018-01-24 15:26:34 +01002701 if (tb[IFLA_IF_NETNSID]) {
2702 netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]);
2703 tgt_net = get_target_net(NETLINK_CB(skb).sk, netnsid);
2704 if (IS_ERR(tgt_net))
2705 return PTR_ERR(tgt_net);
2706 }
2707
2708 err = -EINVAL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002709 ifm = nlmsg_data(nlh);
2710 if (ifm->ifi_index > 0)
Christian Braunerb61ad682018-01-24 15:26:34 +01002711 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002712 else if (tb[IFLA_IFNAME])
Christian Braunerb61ad682018-01-24 15:26:34 +01002713 dev = __dev_get_by_name(tgt_net, ifname);
WANG Cong66400d52015-03-24 11:53:31 -07002714 else if (tb[IFLA_GROUP])
Christian Braunerb61ad682018-01-24 15:26:34 +01002715 err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP]));
Patrick McHardy38f7b872007-06-13 12:03:51 -07002716 else
Christian Braunerb61ad682018-01-24 15:26:34 +01002717 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002718
Christian Braunerb61ad682018-01-24 15:26:34 +01002719 if (!dev) {
2720 if (tb[IFLA_IFNAME] || ifm->ifi_index > 0)
2721 err = -ENODEV;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002722
Christian Braunerb61ad682018-01-24 15:26:34 +01002723 goto out;
2724 }
2725
2726 err = rtnl_delete_link(dev);
2727
2728out:
2729 if (netnsid >= 0)
2730 put_net(tgt_net);
2731
2732 return err;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002733}
2734
Patrick McHardy3729d502010-02-26 06:34:54 +00002735int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
2736{
2737 unsigned int old_flags;
2738 int err;
2739
2740 old_flags = dev->flags;
2741 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
2742 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
2743 if (err < 0)
2744 return err;
2745 }
2746
2747 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
Patrick McHardy3729d502010-02-26 06:34:54 +00002748
Nicolas Dichtela528c212013-09-25 12:02:44 +02002749 __dev_notify_flags(dev, old_flags, ~0U);
Patrick McHardy3729d502010-02-26 06:34:54 +00002750 return 0;
2751}
2752EXPORT_SYMBOL(rtnl_configure_link);
2753
Rami Rosenc0713562012-11-30 01:08:47 +00002754struct net_device *rtnl_create_link(struct net *net,
Thomas Graf78ebb0d2015-04-10 01:45:53 +02002755 const char *ifname, unsigned char name_assign_type,
Tom Gundersen55177502014-07-14 16:37:25 +02002756 const struct rtnl_link_ops *ops, struct nlattr *tb[])
Pavel Emelianove7199282007-08-08 22:16:38 -07002757{
Pavel Emelianove7199282007-08-08 22:16:38 -07002758 struct net_device *dev;
Jiri Pirkod40156a2012-07-20 02:28:47 +00002759 unsigned int num_tx_queues = 1;
2760 unsigned int num_rx_queues = 1;
Pavel Emelianove7199282007-08-08 22:16:38 -07002761
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002762 if (tb[IFLA_NUM_TX_QUEUES])
2763 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
2764 else if (ops->get_num_tx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002765 num_tx_queues = ops->get_num_tx_queues();
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002766
2767 if (tb[IFLA_NUM_RX_QUEUES])
2768 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
2769 else if (ops->get_num_rx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002770 num_rx_queues = ops->get_num_rx_queues();
stephen hemmingerefacb302012-04-10 18:34:43 +00002771
Tom Gundersen55177502014-07-14 16:37:25 +02002772 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
Tom Gundersenc835a672014-07-14 16:37:24 +02002773 ops->setup, num_tx_queues, num_rx_queues);
Pavel Emelianove7199282007-08-08 22:16:38 -07002774 if (!dev)
Tobias Klauserd1892e42017-02-20 16:32:06 +01002775 return ERR_PTR(-ENOMEM);
Pavel Emelianove7199282007-08-08 22:16:38 -07002776
Eric W. Biederman81adee42009-11-08 00:53:51 -08002777 dev_net_set(dev, net);
2778 dev->rtnl_link_ops = ops;
Patrick McHardy3729d502010-02-26 06:34:54 +00002779 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
Eric W. Biederman81adee42009-11-08 00:53:51 -08002780
Pavel Emelianove7199282007-08-08 22:16:38 -07002781 if (tb[IFLA_MTU])
2782 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002783 if (tb[IFLA_ADDRESS]) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002784 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
2785 nla_len(tb[IFLA_ADDRESS]));
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002786 dev->addr_assign_type = NET_ADDR_SET;
2787 }
Pavel Emelianove7199282007-08-08 22:16:38 -07002788 if (tb[IFLA_BROADCAST])
2789 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
2790 nla_len(tb[IFLA_BROADCAST]));
2791 if (tb[IFLA_TXQLEN])
2792 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
2793 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002794 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Pavel Emelianove7199282007-08-08 22:16:38 -07002795 if (tb[IFLA_LINKMODE])
2796 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
Patrick McHardyffa934f2011-01-20 03:00:42 +00002797 if (tb[IFLA_GROUP])
2798 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Stephen Hemminger46e6b992017-12-07 15:40:19 -08002799 if (tb[IFLA_GSO_MAX_SIZE])
2800 netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE]));
2801 if (tb[IFLA_GSO_MAX_SEGS])
Stephen Hemmingera0b586f2017-12-08 15:34:13 -08002802 dev->gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
Pavel Emelianove7199282007-08-08 22:16:38 -07002803
2804 return dev;
Pavel Emelianove7199282007-08-08 22:16:38 -07002805}
Eric Dumazete0d087a2009-11-07 01:26:17 -08002806EXPORT_SYMBOL(rtnl_create_link);
Pavel Emelianove7199282007-08-08 22:16:38 -07002807
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002808static int rtnl_group_changelink(const struct sk_buff *skb,
2809 struct net *net, int group,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002810 struct ifinfomsg *ifm,
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002811 struct netlink_ext_ack *extack,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002812 struct nlattr **tb)
2813{
WANG Congd0795352015-03-23 16:31:09 -07002814 struct net_device *dev, *aux;
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002815 int err;
2816
WANG Congd0795352015-03-23 16:31:09 -07002817 for_each_netdev_safe(net, dev, aux) {
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002818 if (dev->group == group) {
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002819 err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002820 if (err < 0)
2821 return err;
2822 }
2823 }
2824
2825 return 0;
2826}
2827
David Ahernc21ef3e2017-04-16 09:48:24 -07002828static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2829 struct netlink_ext_ack *extack)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002830{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002831 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002832 const struct rtnl_link_ops *ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002833 const struct rtnl_link_ops *m_ops = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002834 struct net_device *dev;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002835 struct net_device *master_dev = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002836 struct ifinfomsg *ifm;
2837 char kind[MODULE_NAME_LEN];
2838 char ifname[IFNAMSIZ];
2839 struct nlattr *tb[IFLA_MAX+1];
2840 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
Tom Gundersen55177502014-07-14 16:37:25 +02002841 unsigned char name_assign_type = NET_NAME_USER;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002842 int err;
2843
Johannes Berg95a5afc2008-10-16 15:24:51 -07002844#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002845replay:
Thomas Graf8072f082007-07-31 14:13:50 -07002846#endif
David Ahernc21ef3e2017-04-16 09:48:24 -07002847 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002848 if (err < 0)
2849 return err;
2850
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002851 err = rtnl_ensure_unique_netns(tb, extack, false);
2852 if (err < 0)
2853 return err;
2854
Patrick McHardy38f7b872007-06-13 12:03:51 -07002855 if (tb[IFLA_IFNAME])
2856 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2857 else
2858 ifname[0] = '\0';
2859
2860 ifm = nlmsg_data(nlh);
2861 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07002862 dev = __dev_get_by_index(net, ifm->ifi_index);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002863 else {
2864 if (ifname[0])
2865 dev = __dev_get_by_name(net, ifname);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002866 else
2867 dev = NULL;
2868 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002869
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002870 if (dev) {
2871 master_dev = netdev_master_upper_dev_get(dev);
2872 if (master_dev)
2873 m_ops = master_dev->rtnl_link_ops;
2874 }
2875
Eric Dumazete0d087a2009-11-07 01:26:17 -08002876 err = validate_linkmsg(dev, tb);
2877 if (err < 0)
Thomas Graf1840bb12008-02-23 19:54:36 -08002878 return err;
2879
Patrick McHardy38f7b872007-06-13 12:03:51 -07002880 if (tb[IFLA_LINKINFO]) {
2881 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +02002882 tb[IFLA_LINKINFO], ifla_info_policy,
2883 NULL);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002884 if (err < 0)
2885 return err;
2886 } else
2887 memset(linkinfo, 0, sizeof(linkinfo));
2888
2889 if (linkinfo[IFLA_INFO_KIND]) {
2890 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
2891 ops = rtnl_link_ops_get(kind);
2892 } else {
2893 kind[0] = '\0';
2894 ops = NULL;
2895 }
2896
2897 if (1) {
Sasha Levin4e10fd52015-02-24 14:14:35 -05002898 struct nlattr *attr[ops ? ops->maxtype + 1 : 1];
2899 struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1];
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002900 struct nlattr **data = NULL;
2901 struct nlattr **slave_data = NULL;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002902 struct net *dest_net, *link_net = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002903
2904 if (ops) {
2905 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
2906 err = nla_parse_nested(attr, ops->maxtype,
2907 linkinfo[IFLA_INFO_DATA],
Johannes Bergfceb6432017-04-12 14:34:07 +02002908 ops->policy, NULL);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002909 if (err < 0)
2910 return err;
2911 data = attr;
2912 }
2913 if (ops->validate) {
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02002914 err = ops->validate(tb, data, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002915 if (err < 0)
2916 return err;
2917 }
2918 }
2919
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002920 if (m_ops) {
2921 if (m_ops->slave_maxtype &&
2922 linkinfo[IFLA_INFO_SLAVE_DATA]) {
2923 err = nla_parse_nested(slave_attr,
2924 m_ops->slave_maxtype,
2925 linkinfo[IFLA_INFO_SLAVE_DATA],
Johannes Bergfceb6432017-04-12 14:34:07 +02002926 m_ops->slave_policy,
2927 NULL);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002928 if (err < 0)
2929 return err;
2930 slave_data = slave_attr;
2931 }
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002932 }
2933
Patrick McHardy38f7b872007-06-13 12:03:51 -07002934 if (dev) {
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002935 int status = 0;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002936
2937 if (nlh->nlmsg_flags & NLM_F_EXCL)
2938 return -EEXIST;
2939 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2940 return -EOPNOTSUPP;
2941
2942 if (linkinfo[IFLA_INFO_DATA]) {
2943 if (!ops || ops != dev->rtnl_link_ops ||
2944 !ops->changelink)
2945 return -EOPNOTSUPP;
2946
Matthias Schifferad744b22017-06-25 23:56:00 +02002947 err = ops->changelink(dev, tb, data, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002948 if (err < 0)
2949 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002950 status |= DO_SETLINK_NOTIFY;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002951 }
2952
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002953 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2954 if (!m_ops || !m_ops->slave_changelink)
2955 return -EOPNOTSUPP;
2956
2957 err = m_ops->slave_changelink(master_dev, dev,
Matthias Schiffer17dd0ec2017-06-25 23:56:02 +02002958 tb, slave_data,
2959 extack);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002960 if (err < 0)
2961 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002962 status |= DO_SETLINK_NOTIFY;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002963 }
2964
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002965 return do_setlink(skb, dev, ifm, extack, tb, ifname,
2966 status);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002967 }
2968
Patrick McHardyffa934f2011-01-20 03:00:42 +00002969 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2970 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002971 return rtnl_group_changelink(skb, net,
Patrick McHardyffa934f2011-01-20 03:00:42 +00002972 nla_get_u32(tb[IFLA_GROUP]),
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002973 ifm, extack, tb);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002974 return -ENODEV;
Patrick McHardyffa934f2011-01-20 03:00:42 +00002975 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002976
Theuns Verwoerd160ca012017-01-31 12:23:46 +13002977 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
Patrick McHardy38f7b872007-06-13 12:03:51 -07002978 return -EOPNOTSUPP;
2979
2980 if (!ops) {
Johannes Berg95a5afc2008-10-16 15:24:51 -07002981#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002982 if (kind[0]) {
2983 __rtnl_unlock();
2984 request_module("rtnl-link-%s", kind);
2985 rtnl_lock();
2986 ops = rtnl_link_ops_get(kind);
2987 if (ops)
2988 goto replay;
2989 }
2990#endif
2991 return -EOPNOTSUPP;
2992 }
2993
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02002994 if (!ops->setup)
2995 return -EOPNOTSUPP;
2996
Tom Gundersen55177502014-07-14 16:37:25 +02002997 if (!ifname[0]) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07002998 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
Tom Gundersen55177502014-07-14 16:37:25 +02002999 name_assign_type = NET_NAME_ENUM;
3000 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07003001
Christian Brauner5bb8ed02018-01-29 18:07:20 +01003002 dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN);
Eric W. Biederman13ad1772011-01-29 14:57:22 +00003003 if (IS_ERR(dest_net))
3004 return PTR_ERR(dest_net);
3005
Nicolas Dichtel317f4812015-01-15 15:11:18 +01003006 if (tb[IFLA_LINK_NETNSID]) {
3007 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
3008
3009 link_net = get_net_ns_by_id(dest_net, id);
3010 if (!link_net) {
3011 err = -EINVAL;
3012 goto out;
3013 }
Eric W. Biederman06615be2015-02-26 16:20:07 -06003014 err = -EPERM;
3015 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
3016 goto out;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01003017 }
3018
3019 dev = rtnl_create_link(link_net ? : dest_net, ifname,
3020 name_assign_type, ops, tb);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00003021 if (IS_ERR(dev)) {
Pavel Emelianove7199282007-08-08 22:16:38 -07003022 err = PTR_ERR(dev);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00003023 goto out;
3024 }
3025
3026 dev->ifindex = ifm->ifi_index;
3027
Cong Wang0e0eee22014-02-11 15:51:30 -08003028 if (ops->newlink) {
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02003029 err = ops->newlink(link_net ? : net, dev, tb, data,
3030 extack);
Cong Wang0e0eee22014-02-11 15:51:30 -08003031 /* Drivers should call free_netdev() in ->destructor
Cong Wange51fb152014-06-03 16:40:47 -07003032 * and unregister it on failure after registration
3033 * so that device could be finally freed in rtnl_unlock.
Cong Wang0e0eee22014-02-11 15:51:30 -08003034 */
Cong Wange51fb152014-06-03 16:40:47 -07003035 if (err < 0) {
3036 /* If device is not registered at all, free it now */
3037 if (dev->reg_state == NETREG_UNINITIALIZED)
3038 free_netdev(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08003039 goto out;
Cong Wange51fb152014-06-03 16:40:47 -07003040 }
Cong Wang0e0eee22014-02-11 15:51:30 -08003041 } else {
Patrick McHardy2d85cba2007-07-11 19:42:13 -07003042 err = register_netdevice(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08003043 if (err < 0) {
3044 free_netdev(dev);
3045 goto out;
3046 }
Dan Carpenterfce9b9b2013-08-14 12:35:42 +03003047 }
Patrick McHardy3729d502010-02-26 06:34:54 +00003048 err = rtnl_configure_link(dev, ifm);
David S. Miller43638902015-03-10 21:58:32 -04003049 if (err < 0)
3050 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01003051 if (link_net) {
Nicolas Dichtel317f4812015-01-15 15:11:18 +01003052 err = dev_change_net_namespace(dev, dest_net, ifname);
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01003053 if (err < 0)
David S. Miller43638902015-03-10 21:58:32 -04003054 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01003055 }
Theuns Verwoerd160ca012017-01-31 12:23:46 +13003056 if (tb[IFLA_MASTER]) {
David Ahern33eaf2a2017-10-04 17:48:46 -07003057 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]),
3058 extack);
Theuns Verwoerd160ca012017-01-31 12:23:46 +13003059 if (err)
3060 goto out_unregister;
3061 }
Patrick McHardy3729d502010-02-26 06:34:54 +00003062out:
Nicolas Dichtel317f4812015-01-15 15:11:18 +01003063 if (link_net)
3064 put_net(link_net);
Eric W. Biederman81adee42009-11-08 00:53:51 -08003065 put_net(dest_net);
Patrick McHardy38f7b872007-06-13 12:03:51 -07003066 return err;
David S. Miller43638902015-03-10 21:58:32 -04003067out_unregister:
3068 if (ops->newlink) {
3069 LIST_HEAD(list_kill);
3070
3071 ops->dellink(dev, &list_kill);
3072 unregister_netdevice_many(&list_kill);
3073 } else {
3074 unregister_netdevice(dev);
3075 }
3076 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003077 }
3078}
3079
David Ahernc21ef3e2017-04-16 09:48:24 -07003080static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3081 struct netlink_ext_ack *extack)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003082{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003083 struct net *net = sock_net(skb->sk);
Jiri Benc79e1ad12017-11-02 17:04:38 -02003084 struct net *tgt_net = net;
Thomas Grafb60c5112006-08-04 23:05:34 -07003085 struct ifinfomsg *ifm;
Eric Dumazeta3d12892009-10-21 10:59:31 +00003086 char ifname[IFNAMSIZ];
Thomas Grafb60c5112006-08-04 23:05:34 -07003087 struct nlattr *tb[IFLA_MAX+1];
3088 struct net_device *dev = NULL;
3089 struct sk_buff *nskb;
Jiri Benc79e1ad12017-11-02 17:04:38 -02003090 int netnsid = -1;
Thomas Graf339bf982006-11-10 14:10:15 -08003091 int err;
Greg Rose115c9b82012-02-21 16:54:48 -05003092 u32 ext_filter_mask = 0;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003093
David Ahernc21ef3e2017-04-16 09:48:24 -07003094 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Thomas Grafb60c5112006-08-04 23:05:34 -07003095 if (err < 0)
Eric Sesterhenn9918f232006-09-26 23:26:38 -07003096 return err;
Thomas Grafb60c5112006-08-04 23:05:34 -07003097
Christian Brauner4ff66ca2018-02-07 13:53:20 +01003098 err = rtnl_ensure_unique_netns(tb, extack, true);
3099 if (err < 0)
3100 return err;
3101
Jiri Benc79e1ad12017-11-02 17:04:38 -02003102 if (tb[IFLA_IF_NETNSID]) {
3103 netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]);
Andrei Vaginf428fe42018-01-02 23:27:33 -08003104 tgt_net = get_target_net(NETLINK_CB(skb).sk, netnsid);
Jiri Benc79e1ad12017-11-02 17:04:38 -02003105 if (IS_ERR(tgt_net))
3106 return PTR_ERR(tgt_net);
3107 }
3108
Eric Dumazeta3d12892009-10-21 10:59:31 +00003109 if (tb[IFLA_IFNAME])
3110 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
3111
Greg Rose115c9b82012-02-21 16:54:48 -05003112 if (tb[IFLA_EXT_MASK])
3113 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3114
Jiri Benc79e1ad12017-11-02 17:04:38 -02003115 err = -EINVAL;
Thomas Grafb60c5112006-08-04 23:05:34 -07003116 ifm = nlmsg_data(nlh);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003117 if (ifm->ifi_index > 0)
Jiri Benc79e1ad12017-11-02 17:04:38 -02003118 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003119 else if (tb[IFLA_IFNAME])
Jiri Benc79e1ad12017-11-02 17:04:38 -02003120 dev = __dev_get_by_name(tgt_net, ifname);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003121 else
Jiri Benc79e1ad12017-11-02 17:04:38 -02003122 goto out;
Thomas Grafb60c5112006-08-04 23:05:34 -07003123
Jiri Benc79e1ad12017-11-02 17:04:38 -02003124 err = -ENODEV;
Eric Dumazeta3d12892009-10-21 10:59:31 +00003125 if (dev == NULL)
Jiri Benc79e1ad12017-11-02 17:04:38 -02003126 goto out;
Eric Dumazeta3d12892009-10-21 10:59:31 +00003127
Jiri Benc79e1ad12017-11-02 17:04:38 -02003128 err = -ENOBUFS;
Greg Rose115c9b82012-02-21 16:54:48 -05003129 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003130 if (nskb == NULL)
Jiri Benc79e1ad12017-11-02 17:04:38 -02003131 goto out;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003132
Jiri Benc79e1ad12017-11-02 17:04:38 -02003133 err = rtnl_fill_ifinfo(nskb, dev, net,
3134 RTM_NEWLINK, NETLINK_CB(skb).portid,
3135 nlh->nlmsg_seq, 0, 0, ext_filter_mask,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003136 0, NULL, 0, netnsid);
Patrick McHardy26932562007-01-31 23:16:40 -08003137 if (err < 0) {
3138 /* -EMSGSIZE implies BUG in if_nlmsg_size */
3139 WARN_ON(err == -EMSGSIZE);
3140 kfree_skb(nskb);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003141 } else
Eric W. Biederman15e47302012-09-07 20:12:54 +00003142 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
Jiri Benc79e1ad12017-11-02 17:04:38 -02003143out:
3144 if (netnsid >= 0)
3145 put_net(tgt_net);
Thomas Grafb60c5112006-08-04 23:05:34 -07003146
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003147 return err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003148}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003149
Greg Rose115c9b82012-02-21 16:54:48 -05003150static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
Greg Rosec7ac8672011-06-10 01:27:09 +00003151{
Greg Rose115c9b82012-02-21 16:54:48 -05003152 struct net *net = sock_net(skb->sk);
3153 struct net_device *dev;
3154 struct nlattr *tb[IFLA_MAX+1];
3155 u32 ext_filter_mask = 0;
3156 u16 min_ifinfo_dump_size = 0;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02003157 int hdrlen;
Greg Rose115c9b82012-02-21 16:54:48 -05003158
Michal Schmidte5eca6d2014-05-28 14:15:19 +02003159 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3160 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
3161 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
3162
Johannes Bergfceb6432017-04-12 14:34:07 +02003163 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00003164 if (tb[IFLA_EXT_MASK])
3165 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3166 }
Greg Rose115c9b82012-02-21 16:54:48 -05003167
3168 if (!ext_filter_mask)
3169 return NLMSG_GOODSIZE;
3170 /*
3171 * traverse the list of net devices and compute the minimum
3172 * buffer size based upon the filter mask.
3173 */
Florian Westphal6853dd42017-08-09 20:41:51 +02003174 rcu_read_lock();
3175 for_each_netdev_rcu(net, dev) {
Greg Rose115c9b82012-02-21 16:54:48 -05003176 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
3177 if_nlmsg_size(dev,
3178 ext_filter_mask));
3179 }
Florian Westphal6853dd42017-08-09 20:41:51 +02003180 rcu_read_unlock();
Greg Rose115c9b82012-02-21 16:54:48 -05003181
Zhang Shengju93af2052016-11-22 14:14:28 +08003182 return nlmsg_total_size(min_ifinfo_dump_size);
Greg Rosec7ac8672011-06-10 01:27:09 +00003183}
3184
Adrian Bunk42bad1d2007-04-26 00:57:41 -07003185static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186{
3187 int idx;
3188 int s_idx = cb->family;
3189
3190 if (s_idx == 0)
3191 s_idx = 1;
Florian Westphal6853dd42017-08-09 20:41:51 +02003192
Patrick McHardy25239ce2010-04-26 16:02:05 +02003193 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
Florian Westphaladdf9b92017-12-02 21:44:05 +01003194 struct rtnl_link **tab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 int type = cb->nlh->nlmsg_type-RTM_BASE;
Florian Westphaladdf9b92017-12-02 21:44:05 +01003196 struct rtnl_link *link;
Florian Westphal6853dd42017-08-09 20:41:51 +02003197 rtnl_dumpit_func dumpit;
3198
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 if (idx < s_idx || idx == PF_PACKET)
3200 continue;
Florian Westphal6853dd42017-08-09 20:41:51 +02003201
Florian Westphaladdf9b92017-12-02 21:44:05 +01003202 if (type < 0 || type >= RTM_NR_MSGTYPES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 continue;
Florian Westphal6853dd42017-08-09 20:41:51 +02003204
Florian Westphaladdf9b92017-12-02 21:44:05 +01003205 tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]);
3206 if (!tab)
3207 continue;
3208
3209 link = tab[type];
3210 if (!link)
3211 continue;
3212
3213 dumpit = link->dumpit;
Florian Westphal6853dd42017-08-09 20:41:51 +02003214 if (!dumpit)
3215 continue;
3216
Nicolas Dichtel04652772013-03-22 06:28:42 +00003217 if (idx > s_idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 memset(&cb->args[0], 0, sizeof(cb->args));
Nicolas Dichtel04652772013-03-22 06:28:42 +00003219 cb->prev_seq = 0;
3220 cb->seq = 0;
3221 }
Florian Westphal6853dd42017-08-09 20:41:51 +02003222 if (dumpit(skb, cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 break;
3224 }
3225 cb->family = idx;
3226
3227 return skb->len;
3228}
3229
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003230struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003231 unsigned int change,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003232 u32 event, gfp_t flags, int *new_nsid,
3233 int new_ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003235 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 struct sk_buff *skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003237 int err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00003238 size_t if_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239
Alexei Starovoitov7f294052013-10-23 16:02:42 -07003240 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003241 if (skb == NULL)
3242 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243
Jiri Benc79e1ad12017-11-02 17:04:38 -02003244 err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
3245 type, 0, 0, change, 0, 0, event,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003246 new_nsid, new_ifindex, -1);
Patrick McHardy26932562007-01-31 23:16:40 -08003247 if (err < 0) {
3248 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
3249 WARN_ON(err == -EMSGSIZE);
3250 kfree_skb(skb);
3251 goto errout;
3252 }
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003253 return skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003254errout:
3255 if (err < 0)
Eric W. Biederman4b3da702007-11-19 22:27:40 -08003256 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003257 return NULL;
3258}
3259
3260void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
3261{
3262 struct net *net = dev_net(dev);
3263
3264 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
3265}
3266
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003267static void rtmsg_ifinfo_event(int type, struct net_device *dev,
3268 unsigned int change, u32 event,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003269 gfp_t flags, int *new_nsid, int new_ifindex)
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003270{
3271 struct sk_buff *skb;
3272
Nicolas Dichteled2a80a2015-05-13 14:19:42 +02003273 if (dev->reg_state != NETREG_REGISTERED)
3274 return;
3275
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003276 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid,
3277 new_ifindex);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003278 if (skb)
3279 rtmsg_ifinfo_send(skb, dev, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280}
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003281
3282void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
3283 gfp_t flags)
3284{
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003285 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3286 NULL, 0);
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003287}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003289void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003290 gfp_t flags, int *new_nsid, int new_ifindex)
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003291{
3292 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003293 new_nsid, new_ifindex);
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003294}
3295
John Fastabendd83b0602012-04-15 06:44:08 +00003296static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
3297 struct net_device *dev,
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003298 u8 *addr, u16 vid, u32 pid, u32 seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003299 int type, unsigned int flags,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003300 int nlflags, u16 ndm_state)
John Fastabendd83b0602012-04-15 06:44:08 +00003301{
3302 struct nlmsghdr *nlh;
3303 struct ndmsg *ndm;
3304
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003305 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
John Fastabendd83b0602012-04-15 06:44:08 +00003306 if (!nlh)
3307 return -EMSGSIZE;
3308
3309 ndm = nlmsg_data(nlh);
3310 ndm->ndm_family = AF_BRIDGE;
3311 ndm->ndm_pad1 = 0;
3312 ndm->ndm_pad2 = 0;
3313 ndm->ndm_flags = flags;
3314 ndm->ndm_type = 0;
3315 ndm->ndm_ifindex = dev->ifindex;
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003316 ndm->ndm_state = ndm_state;
John Fastabendd83b0602012-04-15 06:44:08 +00003317
3318 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
3319 goto nla_put_failure;
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003320 if (vid)
3321 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
3322 goto nla_put_failure;
John Fastabendd83b0602012-04-15 06:44:08 +00003323
Johannes Berg053c0952015-01-16 22:09:00 +01003324 nlmsg_end(skb, nlh);
3325 return 0;
John Fastabendd83b0602012-04-15 06:44:08 +00003326
3327nla_put_failure:
3328 nlmsg_cancel(skb, nlh);
3329 return -EMSGSIZE;
3330}
3331
John Fastabend3ff661c2012-04-15 06:44:14 +00003332static inline size_t rtnl_fdb_nlmsg_size(void)
3333{
Sabrina Dubrocaf82ef3e2016-11-18 15:50:39 +01003334 return NLMSG_ALIGN(sizeof(struct ndmsg)) +
3335 nla_total_size(ETH_ALEN) + /* NDA_LLADDR */
3336 nla_total_size(sizeof(u16)) + /* NDA_VLAN */
3337 0;
John Fastabend3ff661c2012-04-15 06:44:14 +00003338}
3339
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003340static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
3341 u16 ndm_state)
John Fastabend3ff661c2012-04-15 06:44:14 +00003342{
3343 struct net *net = dev_net(dev);
3344 struct sk_buff *skb;
3345 int err = -ENOBUFS;
3346
3347 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
3348 if (!skb)
3349 goto errout;
3350
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003351 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003352 0, 0, type, NTF_SELF, 0, ndm_state);
John Fastabend3ff661c2012-04-15 06:44:14 +00003353 if (err < 0) {
3354 kfree_skb(skb);
3355 goto errout;
3356 }
3357
3358 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
3359 return;
3360errout:
3361 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
3362}
3363
Vlad Yasevich090096b2013-03-06 15:39:42 +00003364/**
3365 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3366 */
3367int ndo_dflt_fdb_add(struct ndmsg *ndm,
3368 struct nlattr *tb[],
3369 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003370 const unsigned char *addr, u16 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003371 u16 flags)
3372{
3373 int err = -EINVAL;
3374
3375 /* If aging addresses are supported device will need to
3376 * implement its own handler for this.
3377 */
3378 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
3379 pr_info("%s: FDB only supports static addresses\n", dev->name);
3380 return err;
3381 }
3382
Or Gerlitz65891fe2014-12-14 18:19:05 +02003383 if (vid) {
3384 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
3385 return err;
3386 }
3387
Vlad Yasevich090096b2013-03-06 15:39:42 +00003388 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3389 err = dev_uc_add_excl(dev, addr);
3390 else if (is_multicast_ether_addr(addr))
3391 err = dev_mc_add_excl(dev, addr);
3392
3393 /* Only return duplicate errors if NLM_F_EXCL is set */
3394 if (err == -EEXIST && !(flags & NLM_F_EXCL))
3395 err = 0;
3396
3397 return err;
3398}
3399EXPORT_SYMBOL(ndo_dflt_fdb_add);
3400
Florian Westphalb88d12e2017-10-10 17:10:04 +02003401static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
3402 struct netlink_ext_ack *extack)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003403{
3404 u16 vid = 0;
3405
3406 if (vlan_attr) {
3407 if (nla_len(vlan_attr) != sizeof(u16)) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003408 NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
Jiri Pirkof6f64242014-11-28 14:34:15 +01003409 return -EINVAL;
3410 }
3411
3412 vid = nla_get_u16(vlan_attr);
3413
3414 if (!vid || vid >= VLAN_VID_MASK) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003415 NL_SET_ERR_MSG(extack, "invalid vlan id");
Jiri Pirkof6f64242014-11-28 14:34:15 +01003416 return -EINVAL;
3417 }
3418 }
3419 *p_vid = vid;
3420 return 0;
3421}
3422
David Ahernc21ef3e2017-04-16 09:48:24 -07003423static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
3424 struct netlink_ext_ack *extack)
John Fastabend77162022012-04-15 06:43:56 +00003425{
3426 struct net *net = sock_net(skb->sk);
John Fastabend77162022012-04-15 06:43:56 +00003427 struct ndmsg *ndm;
3428 struct nlattr *tb[NDA_MAX+1];
3429 struct net_device *dev;
3430 u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01003431 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00003432 int err;
3433
David Ahernc21ef3e2017-04-16 09:48:24 -07003434 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
John Fastabend77162022012-04-15 06:43:56 +00003435 if (err < 0)
3436 return err;
3437
3438 ndm = nlmsg_data(nlh);
3439 if (ndm->ndm_ifindex == 0) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003440 NL_SET_ERR_MSG(extack, "invalid ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003441 return -EINVAL;
3442 }
3443
3444 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3445 if (dev == NULL) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003446 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003447 return -ENODEV;
3448 }
3449
3450 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003451 NL_SET_ERR_MSG(extack, "invalid address");
John Fastabend77162022012-04-15 06:43:56 +00003452 return -EINVAL;
3453 }
3454
3455 addr = nla_data(tb[NDA_LLADDR]);
John Fastabend77162022012-04-15 06:43:56 +00003456
Florian Westphalb88d12e2017-10-10 17:10:04 +02003457 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
Jiri Pirkof6f64242014-11-28 14:34:15 +01003458 if (err)
3459 return err;
3460
John Fastabend77162022012-04-15 06:43:56 +00003461 err = -EOPNOTSUPP;
3462
3463 /* Support fdb on master device the net/bridge default case */
3464 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3465 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003466 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3467 const struct net_device_ops *ops = br_dev->netdev_ops;
3468
Jiri Pirkof6f64242014-11-28 14:34:15 +01003469 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
3470 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00003471 if (err)
3472 goto out;
3473 else
3474 ndm->ndm_flags &= ~NTF_MASTER;
3475 }
3476
3477 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00003478 if ((ndm->ndm_flags & NTF_SELF)) {
3479 if (dev->netdev_ops->ndo_fdb_add)
3480 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003481 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003482 nlh->nlmsg_flags);
3483 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01003484 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003485 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00003486
John Fastabend3ff661c2012-04-15 06:44:14 +00003487 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003488 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
3489 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00003490 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00003491 }
John Fastabend77162022012-04-15 06:43:56 +00003492 }
3493out:
3494 return err;
3495}
3496
Vlad Yasevich090096b2013-03-06 15:39:42 +00003497/**
3498 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3499 */
3500int ndo_dflt_fdb_del(struct ndmsg *ndm,
3501 struct nlattr *tb[],
3502 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003503 const unsigned char *addr, u16 vid)
Vlad Yasevich090096b2013-03-06 15:39:42 +00003504{
Alexander Duyckc8a89c42014-07-15 15:15:20 -07003505 int err = -EINVAL;
Vlad Yasevich090096b2013-03-06 15:39:42 +00003506
3507 /* If aging addresses are supported device will need to
3508 * implement its own handler for this.
3509 */
Sridhar Samudrala64535992013-08-08 15:19:48 -07003510 if (!(ndm->ndm_state & NUD_PERMANENT)) {
Vlad Yasevich090096b2013-03-06 15:39:42 +00003511 pr_info("%s: FDB only supports static addresses\n", dev->name);
Alexander Duyckc8a89c42014-07-15 15:15:20 -07003512 return err;
Vlad Yasevich090096b2013-03-06 15:39:42 +00003513 }
3514
3515 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3516 err = dev_uc_del(dev, addr);
3517 else if (is_multicast_ether_addr(addr))
3518 err = dev_mc_del(dev, addr);
Vlad Yasevich090096b2013-03-06 15:39:42 +00003519
3520 return err;
3521}
3522EXPORT_SYMBOL(ndo_dflt_fdb_del);
3523
David Ahernc21ef3e2017-04-16 09:48:24 -07003524static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
3525 struct netlink_ext_ack *extack)
John Fastabend77162022012-04-15 06:43:56 +00003526{
3527 struct net *net = sock_net(skb->sk);
3528 struct ndmsg *ndm;
Vlad Yasevich1690be62013-02-13 12:00:18 +00003529 struct nlattr *tb[NDA_MAX+1];
John Fastabend77162022012-04-15 06:43:56 +00003530 struct net_device *dev;
3531 int err = -EINVAL;
3532 __u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01003533 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00003534
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07003535 if (!netlink_capable(skb, CAP_NET_ADMIN))
Vlad Yasevich1690be62013-02-13 12:00:18 +00003536 return -EPERM;
3537
David Ahernc21ef3e2017-04-16 09:48:24 -07003538 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
Vlad Yasevich1690be62013-02-13 12:00:18 +00003539 if (err < 0)
3540 return err;
John Fastabend77162022012-04-15 06:43:56 +00003541
3542 ndm = nlmsg_data(nlh);
3543 if (ndm->ndm_ifindex == 0) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003544 NL_SET_ERR_MSG(extack, "invalid ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003545 return -EINVAL;
3546 }
3547
3548 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3549 if (dev == NULL) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003550 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003551 return -ENODEV;
3552 }
3553
Vlad Yasevich1690be62013-02-13 12:00:18 +00003554 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003555 NL_SET_ERR_MSG(extack, "invalid address");
John Fastabend77162022012-04-15 06:43:56 +00003556 return -EINVAL;
3557 }
3558
Vlad Yasevich1690be62013-02-13 12:00:18 +00003559 addr = nla_data(tb[NDA_LLADDR]);
Vlad Yasevich1690be62013-02-13 12:00:18 +00003560
Florian Westphalb88d12e2017-10-10 17:10:04 +02003561 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
Jiri Pirkof6f64242014-11-28 14:34:15 +01003562 if (err)
3563 return err;
3564
John Fastabend77162022012-04-15 06:43:56 +00003565 err = -EOPNOTSUPP;
3566
3567 /* Support fdb on master device the net/bridge default case */
3568 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3569 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003570 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3571 const struct net_device_ops *ops = br_dev->netdev_ops;
John Fastabend77162022012-04-15 06:43:56 +00003572
Jiri Pirko898e5062013-01-03 22:48:52 +00003573 if (ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003574 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00003575
3576 if (err)
3577 goto out;
3578 else
3579 ndm->ndm_flags &= ~NTF_MASTER;
3580 }
3581
3582 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00003583 if (ndm->ndm_flags & NTF_SELF) {
3584 if (dev->netdev_ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003585 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
3586 vid);
Vlad Yasevich090096b2013-03-06 15:39:42 +00003587 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01003588 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00003589
John Fastabend3ff661c2012-04-15 06:44:14 +00003590 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003591 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
3592 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00003593 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00003594 }
John Fastabend77162022012-04-15 06:43:56 +00003595 }
3596out:
3597 return err;
3598}
3599
John Fastabendd83b0602012-04-15 06:44:08 +00003600static int nlmsg_populate_fdb(struct sk_buff *skb,
3601 struct netlink_callback *cb,
3602 struct net_device *dev,
3603 int *idx,
3604 struct netdev_hw_addr_list *list)
3605{
3606 struct netdev_hw_addr *ha;
3607 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00003608 u32 portid, seq;
John Fastabendd83b0602012-04-15 06:44:08 +00003609
Eric W. Biederman15e47302012-09-07 20:12:54 +00003610 portid = NETLINK_CB(cb->skb).portid;
John Fastabendd83b0602012-04-15 06:44:08 +00003611 seq = cb->nlh->nlmsg_seq;
3612
3613 list_for_each_entry(ha, &list->list, list) {
Roopa Prabhud2976532016-08-30 21:56:45 -07003614 if (*idx < cb->args[2])
John Fastabendd83b0602012-04-15 06:44:08 +00003615 goto skip;
3616
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003617 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
John Fastabenda7a558f2012-11-01 16:23:10 +00003618 portid, seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003619 RTM_NEWNEIGH, NTF_SELF,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003620 NLM_F_MULTI, NUD_PERMANENT);
John Fastabendd83b0602012-04-15 06:44:08 +00003621 if (err < 0)
3622 return err;
3623skip:
3624 *idx += 1;
3625 }
3626 return 0;
3627}
3628
3629/**
Ben Hutchings2c530402012-07-10 10:55:09 +00003630 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
John Fastabendd83b0602012-04-15 06:44:08 +00003631 * @nlh: netlink message header
3632 * @dev: netdevice
3633 *
3634 * Default netdevice operation to dump the existing unicast address list.
John Fastabend91f3e7b2013-03-29 08:18:37 +00003635 * Returns number of addresses from list put in skb.
John Fastabendd83b0602012-04-15 06:44:08 +00003636 */
3637int ndo_dflt_fdb_dump(struct sk_buff *skb,
3638 struct netlink_callback *cb,
3639 struct net_device *dev,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04003640 struct net_device *filter_dev,
Roopa Prabhud2976532016-08-30 21:56:45 -07003641 int *idx)
John Fastabendd83b0602012-04-15 06:44:08 +00003642{
3643 int err;
3644
3645 netif_addr_lock_bh(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -07003646 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
John Fastabendd83b0602012-04-15 06:44:08 +00003647 if (err)
3648 goto out;
Zhang Shengju2934c9d2016-11-30 16:37:34 +08003649 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
John Fastabendd83b0602012-04-15 06:44:08 +00003650out:
3651 netif_addr_unlock_bh(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -07003652 return err;
John Fastabendd83b0602012-04-15 06:44:08 +00003653}
3654EXPORT_SYMBOL(ndo_dflt_fdb_dump);
3655
John Fastabend77162022012-04-15 06:43:56 +00003656static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
3657{
John Fastabend77162022012-04-15 06:43:56 +00003658 struct net_device *dev;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003659 struct nlattr *tb[IFLA_MAX+1];
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003660 struct net_device *br_dev = NULL;
3661 const struct net_device_ops *ops = NULL;
3662 const struct net_device_ops *cops = NULL;
3663 struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
3664 struct net *net = sock_net(skb->sk);
Roopa Prabhud2976532016-08-30 21:56:45 -07003665 struct hlist_head *head;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003666 int brport_idx = 0;
3667 int br_idx = 0;
Roopa Prabhud2976532016-08-30 21:56:45 -07003668 int h, s_h;
3669 int idx = 0, s_idx;
3670 int err = 0;
3671 int fidx = 0;
John Fastabend77162022012-04-15 06:43:56 +00003672
Alexander Potapenko0ff50e82017-05-23 13:20:28 +02003673 err = nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb,
3674 IFLA_MAX, ifla_policy, NULL);
3675 if (err < 0) {
3676 return -EINVAL;
3677 } else if (err == 0) {
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003678 if (tb[IFLA_MASTER])
3679 br_idx = nla_get_u32(tb[IFLA_MASTER]);
3680 }
John Fastabend77162022012-04-15 06:43:56 +00003681
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003682 brport_idx = ifm->ifi_index;
3683
3684 if (br_idx) {
3685 br_dev = __dev_get_by_index(net, br_idx);
3686 if (!br_dev)
3687 return -ENODEV;
3688
3689 ops = br_dev->netdev_ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003690 }
3691
Roopa Prabhud2976532016-08-30 21:56:45 -07003692 s_h = cb->args[0];
3693 s_idx = cb->args[1];
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003694
Roopa Prabhud2976532016-08-30 21:56:45 -07003695 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
3696 idx = 0;
3697 head = &net->dev_index_head[h];
3698 hlist_for_each_entry(dev, head, index_hlist) {
3699
3700 if (brport_idx && (dev->ifindex != brport_idx))
3701 continue;
3702
3703 if (!br_idx) { /* user did not specify a specific bridge */
3704 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3705 br_dev = netdev_master_upper_dev_get(dev);
3706 cops = br_dev->netdev_ops;
3707 }
3708 } else {
3709 if (dev != br_dev &&
3710 !(dev->priv_flags & IFF_BRIDGE_PORT))
3711 continue;
3712
3713 if (br_dev != netdev_master_upper_dev_get(dev) &&
3714 !(dev->priv_flags & IFF_EBRIDGE))
3715 continue;
3716 cops = ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003717 }
3718
Roopa Prabhud2976532016-08-30 21:56:45 -07003719 if (idx < s_idx)
3720 goto cont;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003721
Roopa Prabhud2976532016-08-30 21:56:45 -07003722 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3723 if (cops && cops->ndo_fdb_dump) {
3724 err = cops->ndo_fdb_dump(skb, cb,
3725 br_dev, dev,
3726 &fidx);
3727 if (err == -EMSGSIZE)
3728 goto out;
3729 }
3730 }
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003731
Roopa Prabhud2976532016-08-30 21:56:45 -07003732 if (dev->netdev_ops->ndo_fdb_dump)
3733 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
3734 dev, NULL,
3735 &fidx);
3736 else
3737 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
3738 &fidx);
3739 if (err == -EMSGSIZE)
3740 goto out;
3741
3742 cops = NULL;
3743
3744 /* reset fdb offset to 0 for rest of the interfaces */
3745 cb->args[2] = 0;
3746 fidx = 0;
3747cont:
3748 idx++;
John Fastabend77162022012-04-15 06:43:56 +00003749 }
John Fastabend77162022012-04-15 06:43:56 +00003750 }
John Fastabend77162022012-04-15 06:43:56 +00003751
Roopa Prabhud2976532016-08-30 21:56:45 -07003752out:
3753 cb->args[0] = h;
3754 cb->args[1] = idx;
3755 cb->args[2] = fidx;
3756
John Fastabend77162022012-04-15 06:43:56 +00003757 return skb->len;
3758}
3759
Scott Feldman2c3c0312014-11-28 14:34:25 +01003760static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
3761 unsigned int attrnum, unsigned int flag)
3762{
3763 if (mask & flag)
3764 return nla_put_u8(skb, attrnum, !!(flags & flag));
3765 return 0;
3766}
3767
John Fastabend815cccb2012-10-24 08:13:09 +00003768int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Scott Feldman2c3c0312014-11-28 14:34:25 +01003769 struct net_device *dev, u16 mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003770 u32 flags, u32 mask, int nlflags,
3771 u32 filter_mask,
3772 int (*vlan_fill)(struct sk_buff *skb,
3773 struct net_device *dev,
3774 u32 filter_mask))
John Fastabend815cccb2012-10-24 08:13:09 +00003775{
3776 struct nlmsghdr *nlh;
3777 struct ifinfomsg *ifm;
3778 struct nlattr *br_afspec;
Scott Feldman2c3c0312014-11-28 14:34:25 +01003779 struct nlattr *protinfo;
John Fastabend815cccb2012-10-24 08:13:09 +00003780 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Jiri Pirko898e5062013-01-03 22:48:52 +00003781 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003782 int err = 0;
John Fastabend815cccb2012-10-24 08:13:09 +00003783
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02003784 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
John Fastabend815cccb2012-10-24 08:13:09 +00003785 if (nlh == NULL)
3786 return -EMSGSIZE;
3787
3788 ifm = nlmsg_data(nlh);
3789 ifm->ifi_family = AF_BRIDGE;
3790 ifm->__ifi_pad = 0;
3791 ifm->ifi_type = dev->type;
3792 ifm->ifi_index = dev->ifindex;
3793 ifm->ifi_flags = dev_get_flags(dev);
3794 ifm->ifi_change = 0;
3795
3796
3797 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
3798 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
3799 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00003800 (br_dev &&
3801 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
John Fastabend815cccb2012-10-24 08:13:09 +00003802 (dev->addr_len &&
3803 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02003804 (dev->ifindex != dev_get_iflink(dev) &&
3805 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
John Fastabend815cccb2012-10-24 08:13:09 +00003806 goto nla_put_failure;
3807
3808 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
3809 if (!br_afspec)
3810 goto nla_put_failure;
3811
Roopa Prabhu1d460b92014-12-08 14:04:20 -08003812 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
John Fastabend815cccb2012-10-24 08:13:09 +00003813 nla_nest_cancel(skb, br_afspec);
3814 goto nla_put_failure;
3815 }
Roopa Prabhu1d460b92014-12-08 14:04:20 -08003816
3817 if (mode != BRIDGE_MODE_UNDEF) {
3818 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
3819 nla_nest_cancel(skb, br_afspec);
3820 goto nla_put_failure;
3821 }
3822 }
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003823 if (vlan_fill) {
3824 err = vlan_fill(skb, dev, filter_mask);
3825 if (err) {
3826 nla_nest_cancel(skb, br_afspec);
3827 goto nla_put_failure;
3828 }
3829 }
John Fastabend815cccb2012-10-24 08:13:09 +00003830 nla_nest_end(skb, br_afspec);
3831
Scott Feldman2c3c0312014-11-28 14:34:25 +01003832 protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
3833 if (!protinfo)
3834 goto nla_put_failure;
3835
3836 if (brport_nla_put_flag(skb, flags, mask,
3837 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
3838 brport_nla_put_flag(skb, flags, mask,
3839 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
3840 brport_nla_put_flag(skb, flags, mask,
3841 IFLA_BRPORT_FAST_LEAVE,
3842 BR_MULTICAST_FAST_LEAVE) ||
3843 brport_nla_put_flag(skb, flags, mask,
3844 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
3845 brport_nla_put_flag(skb, flags, mask,
3846 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
3847 brport_nla_put_flag(skb, flags, mask,
3848 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
3849 brport_nla_put_flag(skb, flags, mask,
3850 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
3851 brport_nla_put_flag(skb, flags, mask,
3852 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
3853 nla_nest_cancel(skb, protinfo);
3854 goto nla_put_failure;
3855 }
3856
3857 nla_nest_end(skb, protinfo);
3858
Johannes Berg053c0952015-01-16 22:09:00 +01003859 nlmsg_end(skb, nlh);
3860 return 0;
John Fastabend815cccb2012-10-24 08:13:09 +00003861nla_put_failure:
3862 nlmsg_cancel(skb, nlh);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003863 return err ? err : -EMSGSIZE;
John Fastabend815cccb2012-10-24 08:13:09 +00003864}
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003865EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
John Fastabend815cccb2012-10-24 08:13:09 +00003866
John Fastabende5a55a82012-10-24 08:12:57 +00003867static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
3868{
3869 struct net *net = sock_net(skb->sk);
3870 struct net_device *dev;
3871 int idx = 0;
3872 u32 portid = NETLINK_CB(cb->skb).portid;
3873 u32 seq = cb->nlh->nlmsg_seq;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00003874 u32 filter_mask = 0;
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003875 int err;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00003876
Thomas Grafaa68c202014-11-26 13:42:20 +01003877 if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
3878 struct nlattr *extfilt;
3879
3880 extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
3881 IFLA_EXT_MASK);
3882 if (extfilt) {
3883 if (nla_len(extfilt) < sizeof(filter_mask))
3884 return -EINVAL;
3885
3886 filter_mask = nla_get_u32(extfilt);
3887 }
3888 }
John Fastabende5a55a82012-10-24 08:12:57 +00003889
3890 rcu_read_lock();
3891 for_each_netdev_rcu(net, dev) {
3892 const struct net_device_ops *ops = dev->netdev_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00003893 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
John Fastabende5a55a82012-10-24 08:12:57 +00003894
Jiri Pirko898e5062013-01-03 22:48:52 +00003895 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003896 if (idx >= cb->args[0]) {
3897 err = br_dev->netdev_ops->ndo_bridge_getlink(
3898 skb, portid, seq, dev,
3899 filter_mask, NLM_F_MULTI);
David Ahernf6c57752017-05-15 23:19:17 -07003900 if (err < 0 && err != -EOPNOTSUPP) {
3901 if (likely(skb->len))
3902 break;
3903
3904 goto out_err;
3905 }
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003906 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00003907 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00003908 }
3909
3910 if (ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003911 if (idx >= cb->args[0]) {
3912 err = ops->ndo_bridge_getlink(skb, portid,
3913 seq, dev,
3914 filter_mask,
3915 NLM_F_MULTI);
David Ahernf6c57752017-05-15 23:19:17 -07003916 if (err < 0 && err != -EOPNOTSUPP) {
3917 if (likely(skb->len))
3918 break;
3919
3920 goto out_err;
3921 }
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003922 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00003923 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00003924 }
3925 }
David Ahernf6c57752017-05-15 23:19:17 -07003926 err = skb->len;
3927out_err:
John Fastabende5a55a82012-10-24 08:12:57 +00003928 rcu_read_unlock();
3929 cb->args[0] = idx;
3930
David Ahernf6c57752017-05-15 23:19:17 -07003931 return err;
John Fastabende5a55a82012-10-24 08:12:57 +00003932}
3933
John Fastabend2469ffd2012-10-24 08:13:03 +00003934static inline size_t bridge_nlmsg_size(void)
3935{
3936 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3937 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3938 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3939 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
3940 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
3941 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
3942 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
3943 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
3944 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
3945 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
3946 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
3947}
3948
Roopa Prabhu02dba432015-01-14 20:02:25 -08003949static int rtnl_bridge_notify(struct net_device *dev)
John Fastabend2469ffd2012-10-24 08:13:03 +00003950{
3951 struct net *net = dev_net(dev);
John Fastabend2469ffd2012-10-24 08:13:03 +00003952 struct sk_buff *skb;
3953 int err = -EOPNOTSUPP;
3954
Roopa Prabhu02dba432015-01-14 20:02:25 -08003955 if (!dev->netdev_ops->ndo_bridge_getlink)
3956 return 0;
3957
John Fastabend2469ffd2012-10-24 08:13:03 +00003958 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
3959 if (!skb) {
3960 err = -ENOMEM;
3961 goto errout;
3962 }
3963
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02003964 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
Roopa Prabhu02dba432015-01-14 20:02:25 -08003965 if (err < 0)
3966 goto errout;
John Fastabend2469ffd2012-10-24 08:13:03 +00003967
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08003968 if (!skb->len)
3969 goto errout;
3970
John Fastabend2469ffd2012-10-24 08:13:03 +00003971 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
3972 return 0;
3973errout:
3974 WARN_ON(err == -EMSGSIZE);
3975 kfree_skb(skb);
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08003976 if (err)
3977 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
John Fastabend2469ffd2012-10-24 08:13:03 +00003978 return err;
3979}
3980
David Ahernc21ef3e2017-04-16 09:48:24 -07003981static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3982 struct netlink_ext_ack *extack)
John Fastabende5a55a82012-10-24 08:12:57 +00003983{
3984 struct net *net = sock_net(skb->sk);
3985 struct ifinfomsg *ifm;
3986 struct net_device *dev;
John Fastabend2469ffd2012-10-24 08:13:03 +00003987 struct nlattr *br_spec, *attr = NULL;
3988 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02003989 u16 flags = 0;
John Fastabendc38e01b2012-11-02 16:32:36 +00003990 bool have_flags = false;
John Fastabende5a55a82012-10-24 08:12:57 +00003991
3992 if (nlmsg_len(nlh) < sizeof(*ifm))
3993 return -EINVAL;
3994
3995 ifm = nlmsg_data(nlh);
3996 if (ifm->ifi_family != AF_BRIDGE)
3997 return -EPFNOSUPPORT;
3998
3999 dev = __dev_get_by_index(net, ifm->ifi_index);
4000 if (!dev) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004001 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabende5a55a82012-10-24 08:12:57 +00004002 return -ENODEV;
4003 }
4004
John Fastabend2469ffd2012-10-24 08:13:03 +00004005 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4006 if (br_spec) {
4007 nla_for_each_nested(attr, br_spec, rem) {
4008 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01004009 if (nla_len(attr) < sizeof(flags))
4010 return -EINVAL;
4011
John Fastabendc38e01b2012-11-02 16:32:36 +00004012 have_flags = true;
John Fastabend2469ffd2012-10-24 08:13:03 +00004013 flags = nla_get_u16(attr);
4014 break;
4015 }
4016 }
4017 }
4018
4019 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00004020 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4021
4022 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
John Fastabend2469ffd2012-10-24 08:13:03 +00004023 err = -EOPNOTSUPP;
4024 goto out;
4025 }
4026
Roopa Prabhuadd511b2015-01-29 22:40:12 -08004027 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
John Fastabende5a55a82012-10-24 08:12:57 +00004028 if (err)
4029 goto out;
John Fastabend2469ffd2012-10-24 08:13:03 +00004030
4031 flags &= ~BRIDGE_FLAGS_MASTER;
John Fastabende5a55a82012-10-24 08:12:57 +00004032 }
4033
John Fastabend2469ffd2012-10-24 08:13:03 +00004034 if ((flags & BRIDGE_FLAGS_SELF)) {
4035 if (!dev->netdev_ops->ndo_bridge_setlink)
4036 err = -EOPNOTSUPP;
4037 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08004038 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
4039 flags);
Roopa Prabhu02dba432015-01-14 20:02:25 -08004040 if (!err) {
John Fastabend2469ffd2012-10-24 08:13:03 +00004041 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08004042
4043 /* Generate event to notify upper layer of bridge
4044 * change
4045 */
4046 err = rtnl_bridge_notify(dev);
4047 }
John Fastabend2469ffd2012-10-24 08:13:03 +00004048 }
4049
John Fastabendc38e01b2012-11-02 16:32:36 +00004050 if (have_flags)
John Fastabend2469ffd2012-10-24 08:13:03 +00004051 memcpy(nla_data(attr), &flags, sizeof(flags));
John Fastabende5a55a82012-10-24 08:12:57 +00004052out:
4053 return err;
4054}
4055
David Ahernc21ef3e2017-04-16 09:48:24 -07004056static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
4057 struct netlink_ext_ack *extack)
Vlad Yasevich407af322013-02-13 12:00:12 +00004058{
4059 struct net *net = sock_net(skb->sk);
4060 struct ifinfomsg *ifm;
4061 struct net_device *dev;
4062 struct nlattr *br_spec, *attr = NULL;
4063 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02004064 u16 flags = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +00004065 bool have_flags = false;
4066
4067 if (nlmsg_len(nlh) < sizeof(*ifm))
4068 return -EINVAL;
4069
4070 ifm = nlmsg_data(nlh);
4071 if (ifm->ifi_family != AF_BRIDGE)
4072 return -EPFNOSUPPORT;
4073
4074 dev = __dev_get_by_index(net, ifm->ifi_index);
4075 if (!dev) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004076 NL_SET_ERR_MSG(extack, "unknown ifindex");
Vlad Yasevich407af322013-02-13 12:00:12 +00004077 return -ENODEV;
4078 }
4079
4080 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4081 if (br_spec) {
4082 nla_for_each_nested(attr, br_spec, rem) {
4083 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01004084 if (nla_len(attr) < sizeof(flags))
4085 return -EINVAL;
4086
Vlad Yasevich407af322013-02-13 12:00:12 +00004087 have_flags = true;
4088 flags = nla_get_u16(attr);
4089 break;
4090 }
4091 }
4092 }
4093
Vlad Yasevich407af322013-02-13 12:00:12 +00004094 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
4095 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4096
4097 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
4098 err = -EOPNOTSUPP;
4099 goto out;
4100 }
4101
Roopa Prabhuadd511b2015-01-29 22:40:12 -08004102 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00004103 if (err)
4104 goto out;
4105
4106 flags &= ~BRIDGE_FLAGS_MASTER;
4107 }
4108
4109 if ((flags & BRIDGE_FLAGS_SELF)) {
4110 if (!dev->netdev_ops->ndo_bridge_dellink)
4111 err = -EOPNOTSUPP;
4112 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08004113 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
4114 flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00004115
Roopa Prabhu02dba432015-01-14 20:02:25 -08004116 if (!err) {
Vlad Yasevich407af322013-02-13 12:00:12 +00004117 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08004118
4119 /* Generate event to notify upper layer of bridge
4120 * change
4121 */
4122 err = rtnl_bridge_notify(dev);
4123 }
Vlad Yasevich407af322013-02-13 12:00:12 +00004124 }
4125
4126 if (have_flags)
4127 memcpy(nla_data(attr), &flags, sizeof(flags));
Vlad Yasevich407af322013-02-13 12:00:12 +00004128out:
4129 return err;
4130}
4131
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004132static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
4133{
4134 return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
4135 (!idxattr || idxattr == attrid);
4136}
4137
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004138#define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
4139static int rtnl_get_offload_stats_attr_size(int attr_id)
4140{
4141 switch (attr_id) {
4142 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
4143 return sizeof(struct rtnl_link_stats64);
4144 }
4145
4146 return 0;
4147}
4148
4149static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
4150 int *prividx)
4151{
4152 struct nlattr *attr = NULL;
4153 int attr_id, size;
4154 void *attr_data;
4155 int err;
4156
4157 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
4158 dev->netdev_ops->ndo_get_offload_stats))
4159 return -ENODATA;
4160
4161 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
4162 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
4163 if (attr_id < *prividx)
4164 continue;
4165
4166 size = rtnl_get_offload_stats_attr_size(attr_id);
4167 if (!size)
4168 continue;
4169
Or Gerlitz3df5b3c2016-11-22 23:09:54 +02004170 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004171 continue;
4172
4173 attr = nla_reserve_64bit(skb, attr_id, size,
4174 IFLA_OFFLOAD_XSTATS_UNSPEC);
4175 if (!attr)
4176 goto nla_put_failure;
4177
4178 attr_data = nla_data(attr);
4179 memset(attr_data, 0, size);
4180 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
4181 attr_data);
4182 if (err)
4183 goto get_offload_stats_failure;
4184 }
4185
4186 if (!attr)
4187 return -ENODATA;
4188
4189 *prividx = 0;
4190 return 0;
4191
4192nla_put_failure:
4193 err = -EMSGSIZE;
4194get_offload_stats_failure:
4195 *prividx = attr_id;
4196 return err;
4197}
4198
4199static int rtnl_get_offload_stats_size(const struct net_device *dev)
4200{
4201 int nla_size = 0;
4202 int attr_id;
4203 int size;
4204
4205 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
4206 dev->netdev_ops->ndo_get_offload_stats))
4207 return 0;
4208
4209 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
4210 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
Or Gerlitz3df5b3c2016-11-22 23:09:54 +02004211 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004212 continue;
4213 size = rtnl_get_offload_stats_attr_size(attr_id);
4214 nla_size += nla_total_size_64bit(size);
4215 }
4216
4217 if (nla_size != 0)
4218 nla_size += nla_total_size(0);
4219
4220 return nla_size;
4221}
4222
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004223static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
4224 int type, u32 pid, u32 seq, u32 change,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004225 unsigned int flags, unsigned int filter_mask,
4226 int *idxattr, int *prividx)
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004227{
4228 struct if_stats_msg *ifsm;
4229 struct nlmsghdr *nlh;
4230 struct nlattr *attr;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004231 int s_prividx = *prividx;
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004232 int err;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004233
4234 ASSERT_RTNL();
4235
4236 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
4237 if (!nlh)
4238 return -EMSGSIZE;
4239
4240 ifsm = nlmsg_data(nlh);
Nikolay Aleksandrovce024f42017-10-03 13:20:48 +03004241 ifsm->family = PF_UNSPEC;
4242 ifsm->pad1 = 0;
4243 ifsm->pad2 = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004244 ifsm->ifindex = dev->ifindex;
4245 ifsm->filter_mask = filter_mask;
4246
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004247 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004248 struct rtnl_link_stats64 *sp;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004249
Nicolas Dichtel58414d32016-04-21 18:58:25 +02004250 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
4251 sizeof(struct rtnl_link_stats64),
4252 IFLA_STATS_UNSPEC);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004253 if (!attr)
4254 goto nla_put_failure;
4255
4256 sp = nla_data(attr);
4257 dev_get_stats(dev, sp);
4258 }
4259
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004260 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
4261 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
4262
4263 if (ops && ops->fill_linkxstats) {
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004264 *idxattr = IFLA_STATS_LINK_XSTATS;
4265 attr = nla_nest_start(skb,
4266 IFLA_STATS_LINK_XSTATS);
4267 if (!attr)
4268 goto nla_put_failure;
4269
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004270 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
4271 nla_nest_end(skb, attr);
4272 if (err)
4273 goto nla_put_failure;
4274 *idxattr = 0;
4275 }
4276 }
4277
4278 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
4279 *idxattr)) {
4280 const struct rtnl_link_ops *ops = NULL;
4281 const struct net_device *master;
4282
4283 master = netdev_master_upper_dev_get(dev);
4284 if (master)
4285 ops = master->rtnl_link_ops;
4286 if (ops && ops->fill_linkxstats) {
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004287 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
4288 attr = nla_nest_start(skb,
4289 IFLA_STATS_LINK_XSTATS_SLAVE);
4290 if (!attr)
4291 goto nla_put_failure;
4292
4293 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004294 nla_nest_end(skb, attr);
4295 if (err)
4296 goto nla_put_failure;
4297 *idxattr = 0;
4298 }
4299 }
4300
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004301 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
4302 *idxattr)) {
4303 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
4304 attr = nla_nest_start(skb, IFLA_STATS_LINK_OFFLOAD_XSTATS);
4305 if (!attr)
4306 goto nla_put_failure;
4307
4308 err = rtnl_get_offload_stats(skb, dev, prividx);
4309 if (err == -ENODATA)
4310 nla_nest_cancel(skb, attr);
4311 else
4312 nla_nest_end(skb, attr);
4313
4314 if (err && err != -ENODATA)
4315 goto nla_put_failure;
4316 *idxattr = 0;
4317 }
4318
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004319 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
4320 struct rtnl_af_ops *af_ops;
4321
4322 *idxattr = IFLA_STATS_AF_SPEC;
4323 attr = nla_nest_start(skb, IFLA_STATS_AF_SPEC);
4324 if (!attr)
4325 goto nla_put_failure;
4326
Florian Westphal5fa85a02017-10-16 15:44:36 +02004327 rcu_read_lock();
4328 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004329 if (af_ops->fill_stats_af) {
4330 struct nlattr *af;
4331 int err;
4332
4333 af = nla_nest_start(skb, af_ops->family);
Florian Westphal5fa85a02017-10-16 15:44:36 +02004334 if (!af) {
4335 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004336 goto nla_put_failure;
Florian Westphal5fa85a02017-10-16 15:44:36 +02004337 }
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004338 err = af_ops->fill_stats_af(skb, dev);
4339
Florian Westphal5fa85a02017-10-16 15:44:36 +02004340 if (err == -ENODATA) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004341 nla_nest_cancel(skb, af);
Florian Westphal5fa85a02017-10-16 15:44:36 +02004342 } else if (err < 0) {
4343 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004344 goto nla_put_failure;
Florian Westphal5fa85a02017-10-16 15:44:36 +02004345 }
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004346
4347 nla_nest_end(skb, af);
4348 }
4349 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02004350 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004351
4352 nla_nest_end(skb, attr);
4353
4354 *idxattr = 0;
4355 }
4356
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004357 nlmsg_end(skb, nlh);
4358
4359 return 0;
4360
4361nla_put_failure:
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004362 /* not a multi message or no progress mean a real error */
4363 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
4364 nlmsg_cancel(skb, nlh);
4365 else
4366 nlmsg_end(skb, nlh);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004367
4368 return -EMSGSIZE;
4369}
4370
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004371static size_t if_nlmsg_stats_size(const struct net_device *dev,
4372 u32 filter_mask)
4373{
4374 size_t size = 0;
4375
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004376 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004377 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
4378
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004379 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
4380 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004381 int attr = IFLA_STATS_LINK_XSTATS;
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004382
4383 if (ops && ops->get_linkxstats_size) {
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004384 size += nla_total_size(ops->get_linkxstats_size(dev,
4385 attr));
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004386 /* for IFLA_STATS_LINK_XSTATS */
4387 size += nla_total_size(0);
4388 }
4389 }
4390
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004391 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
4392 struct net_device *_dev = (struct net_device *)dev;
4393 const struct rtnl_link_ops *ops = NULL;
4394 const struct net_device *master;
4395
4396 /* netdev_master_upper_dev_get can't take const */
4397 master = netdev_master_upper_dev_get(_dev);
4398 if (master)
4399 ops = master->rtnl_link_ops;
4400 if (ops && ops->get_linkxstats_size) {
4401 int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
4402
4403 size += nla_total_size(ops->get_linkxstats_size(dev,
4404 attr));
4405 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
4406 size += nla_total_size(0);
4407 }
4408 }
4409
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004410 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
4411 size += rtnl_get_offload_stats_size(dev);
4412
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004413 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
4414 struct rtnl_af_ops *af_ops;
4415
4416 /* for IFLA_STATS_AF_SPEC */
4417 size += nla_total_size(0);
4418
Florian Westphal5fa85a02017-10-16 15:44:36 +02004419 rcu_read_lock();
4420 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004421 if (af_ops->get_stats_af_size) {
4422 size += nla_total_size(
4423 af_ops->get_stats_af_size(dev));
4424
4425 /* for AF_* */
4426 size += nla_total_size(0);
4427 }
4428 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02004429 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004430 }
4431
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004432 return size;
4433}
4434
David Ahernc21ef3e2017-04-16 09:48:24 -07004435static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
4436 struct netlink_ext_ack *extack)
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004437{
4438 struct net *net = sock_net(skb->sk);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004439 struct net_device *dev = NULL;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004440 int idxattr = 0, prividx = 0;
4441 struct if_stats_msg *ifsm;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004442 struct sk_buff *nskb;
4443 u32 filter_mask;
4444 int err;
4445
Mathias Krause4775cc12016-12-28 17:52:15 +01004446 if (nlmsg_len(nlh) < sizeof(*ifsm))
4447 return -EINVAL;
4448
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004449 ifsm = nlmsg_data(nlh);
4450 if (ifsm->ifindex > 0)
4451 dev = __dev_get_by_index(net, ifsm->ifindex);
4452 else
4453 return -EINVAL;
4454
4455 if (!dev)
4456 return -ENODEV;
4457
4458 filter_mask = ifsm->filter_mask;
4459 if (!filter_mask)
4460 return -EINVAL;
4461
4462 nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
4463 if (!nskb)
4464 return -ENOBUFS;
4465
4466 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
4467 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004468 0, filter_mask, &idxattr, &prividx);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004469 if (err < 0) {
4470 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
4471 WARN_ON(err == -EMSGSIZE);
4472 kfree_skb(nskb);
4473 } else {
4474 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
4475 }
4476
4477 return err;
4478}
4479
4480static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
4481{
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004482 int h, s_h, err, s_idx, s_idxattr, s_prividx;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004483 struct net *net = sock_net(skb->sk);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004484 unsigned int flags = NLM_F_MULTI;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004485 struct if_stats_msg *ifsm;
4486 struct hlist_head *head;
4487 struct net_device *dev;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004488 u32 filter_mask = 0;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004489 int idx = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004490
4491 s_h = cb->args[0];
4492 s_idx = cb->args[1];
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004493 s_idxattr = cb->args[2];
4494 s_prividx = cb->args[3];
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004495
4496 cb->seq = net->dev_base_seq;
4497
Mathias Krause4775cc12016-12-28 17:52:15 +01004498 if (nlmsg_len(cb->nlh) < sizeof(*ifsm))
4499 return -EINVAL;
4500
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004501 ifsm = nlmsg_data(cb->nlh);
4502 filter_mask = ifsm->filter_mask;
4503 if (!filter_mask)
4504 return -EINVAL;
4505
4506 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4507 idx = 0;
4508 head = &net->dev_index_head[h];
4509 hlist_for_each_entry(dev, head, index_hlist) {
4510 if (idx < s_idx)
4511 goto cont;
4512 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
4513 NETLINK_CB(cb->skb).portid,
4514 cb->nlh->nlmsg_seq, 0,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004515 flags, filter_mask,
4516 &s_idxattr, &s_prividx);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004517 /* If we ran out of room on the first message,
4518 * we're in trouble
4519 */
4520 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
4521
4522 if (err < 0)
4523 goto out;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004524 s_prividx = 0;
4525 s_idxattr = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004526 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
4527cont:
4528 idx++;
4529 }
4530 }
4531out:
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004532 cb->args[3] = s_prividx;
4533 cb->args[2] = s_idxattr;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004534 cb->args[1] = idx;
4535 cb->args[0] = h;
4536
4537 return skb->len;
4538}
4539
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540/* Process one rtnetlink message. */
4541
Johannes Berg2d4bc932017-04-12 14:34:04 +02004542static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
4543 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004545 struct net *net = sock_net(skb->sk);
Florian Westphaladdf9b92017-12-02 21:44:05 +01004546 struct rtnl_link *link;
Florian Westphale4202512017-12-02 21:44:06 +01004547 struct module *owner;
Florian Westphal6853dd42017-08-09 20:41:51 +02004548 int err = -EOPNOTSUPP;
Thomas Grafe2849862007-03-22 11:48:11 -07004549 rtnl_doit_func doit;
Florian Westphal62256f92017-08-09 20:41:52 +02004550 unsigned int flags;
Alexander Kuleshov617cfc72016-01-10 21:26:57 +06004551 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 int family;
4553 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 if (type > RTM_MAX)
Thomas Graf038890f2007-04-05 14:35:52 -07004557 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558
4559 type -= RTM_BASE;
4560
4561 /* All the messages must have at least 1 byte length */
Hong zhi guo573ce262013-03-27 06:47:04 +00004562 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 return 0;
4564
Hong zhi guo573ce262013-03-27 06:47:04 +00004565 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 kind = type&3;
4567
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07004568 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07004569 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570
Florian Westphal6853dd42017-08-09 20:41:51 +02004571 rcu_read_lock();
David S. Millerb8f3ab42011-01-18 12:40:38 -08004572 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004573 struct sock *rtnl;
Thomas Grafe2849862007-03-22 11:48:11 -07004574 rtnl_dumpit_func dumpit;
Greg Rosec7ac8672011-06-10 01:27:09 +00004575 u16 min_dump_alloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576
Florian Westphaladdf9b92017-12-02 21:44:05 +01004577 link = rtnl_get_link(family, type);
4578 if (!link || !link->dumpit) {
Florian Westphal6853dd42017-08-09 20:41:51 +02004579 family = PF_UNSPEC;
Florian Westphaladdf9b92017-12-02 21:44:05 +01004580 link = rtnl_get_link(family, type);
4581 if (!link || !link->dumpit)
Florian Westphal6853dd42017-08-09 20:41:51 +02004582 goto err_unlock;
4583 }
Florian Westphale4202512017-12-02 21:44:06 +01004584 owner = link->owner;
Florian Westphaladdf9b92017-12-02 21:44:05 +01004585 dumpit = link->dumpit;
Florian Westphale1fa6d22017-08-09 20:41:47 +02004586
Florian Westphal5c2bb9b2017-08-10 16:52:58 +02004587 if (type == RTM_GETLINK - RTM_BASE)
Florian Westphale1fa6d22017-08-09 20:41:47 +02004588 min_dump_alloc = rtnl_calcit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589
Florian Westphale4202512017-12-02 21:44:06 +01004590 err = 0;
4591 /* need to do this before rcu_read_unlock() */
4592 if (!try_module_get(owner))
4593 err = -EPROTONOSUPPORT;
4594
Florian Westphal6853dd42017-08-09 20:41:51 +02004595 rcu_read_unlock();
4596
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004597 rtnl = net->rtnl;
Florian Westphale4202512017-12-02 21:44:06 +01004598 if (err == 0) {
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00004599 struct netlink_dump_control c = {
4600 .dump = dumpit,
4601 .min_dump_alloc = min_dump_alloc,
Florian Westphale4202512017-12-02 21:44:06 +01004602 .module = owner,
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00004603 };
4604 err = netlink_dump_start(rtnl, skb, nlh, &c);
Florian Westphale4202512017-12-02 21:44:06 +01004605 /* netlink_dump_start() will keep a reference on
4606 * module if dump is still in progress.
4607 */
4608 module_put(owner);
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00004609 }
Eric Dumazet2907c352011-05-25 07:34:04 +00004610 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 }
4612
Florian Westphaladdf9b92017-12-02 21:44:05 +01004613 link = rtnl_get_link(family, type);
4614 if (!link || !link->doit) {
Florian Westphal8caa38b2017-08-10 16:53:01 +02004615 family = PF_UNSPEC;
Florian Westphaladdf9b92017-12-02 21:44:05 +01004616 link = rtnl_get_link(PF_UNSPEC, type);
4617 if (!link || !link->doit)
4618 goto out_unlock;
Florian Westphal8caa38b2017-08-10 16:53:01 +02004619 }
4620
Florian Westphale4202512017-12-02 21:44:06 +01004621 owner = link->owner;
4622 if (!try_module_get(owner)) {
4623 err = -EPROTONOSUPPORT;
4624 goto out_unlock;
4625 }
4626
Florian Westphaladdf9b92017-12-02 21:44:05 +01004627 flags = link->flags;
Florian Westphal62256f92017-08-09 20:41:52 +02004628 if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
Florian Westphaladdf9b92017-12-02 21:44:05 +01004629 doit = link->doit;
Florian Westphal62256f92017-08-09 20:41:52 +02004630 rcu_read_unlock();
4631 if (doit)
4632 err = doit(skb, nlh, extack);
Florian Westphale4202512017-12-02 21:44:06 +01004633 module_put(owner);
Florian Westphal62256f92017-08-09 20:41:52 +02004634 return err;
4635 }
Florian Westphal6853dd42017-08-09 20:41:51 +02004636 rcu_read_unlock();
4637
Florian Westphal0cc09022017-08-09 20:41:50 +02004638 rtnl_lock();
Florian Westphaladdf9b92017-12-02 21:44:05 +01004639 link = rtnl_get_link(family, type);
4640 if (link && link->doit)
4641 err = link->doit(skb, nlh, extack);
Florian Westphal0cc09022017-08-09 20:41:50 +02004642 rtnl_unlock();
Florian Westphaladdf9b92017-12-02 21:44:05 +01004643
Florian Westphale4202512017-12-02 21:44:06 +01004644 module_put(owner);
4645
Florian Westphaladdf9b92017-12-02 21:44:05 +01004646 return err;
4647
4648out_unlock:
4649 rcu_read_unlock();
Florian Westphal0cc09022017-08-09 20:41:50 +02004650 return err;
4651
4652err_unlock:
Florian Westphal6853dd42017-08-09 20:41:51 +02004653 rcu_read_unlock();
Florian Westphal0cc09022017-08-09 20:41:50 +02004654 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655}
4656
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07004657static void rtnetlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07004659 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660}
4661
Julien Gomes5f729ea2017-06-20 13:54:16 -07004662static int rtnetlink_bind(struct net *net, int group)
4663{
4664 switch (group) {
4665 case RTNLGRP_IPV4_MROUTE_R:
4666 case RTNLGRP_IPV6_MROUTE_R:
4667 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
4668 return -EPERM;
4669 break;
4670 }
4671 return 0;
4672}
4673
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
4675{
Jiri Pirko351638e2013-05-28 01:30:21 +00004676 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02004677
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 switch (event) {
Vlad Yasevich5138e862017-04-04 09:23:41 -04004679 case NETDEV_REBOOT:
Xin Long8a212582017-10-15 18:13:41 +08004680 case NETDEV_CHANGEMTU:
David Ahern37536542017-07-19 10:22:40 -07004681 case NETDEV_CHANGEADDR:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004682 case NETDEV_CHANGENAME:
4683 case NETDEV_FEAT_CHANGE:
4684 case NETDEV_BONDING_FAILOVER:
Xin Longe6e66592017-10-15 18:13:43 +08004685 case NETDEV_POST_TYPE_CHANGE:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004686 case NETDEV_NOTIFY_PEERS:
Xin Longdc709f32017-10-15 18:13:44 +08004687 case NETDEV_CHANGEUPPER:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004688 case NETDEV_RESEND_IGMP:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004689 case NETDEV_CHANGEINFODATA:
Xin Longeeda3fb2017-10-24 13:54:19 +08004690 case NETDEV_CHANGELOWERSTATE:
Xin Longebdcf042017-10-15 18:13:42 +08004691 case NETDEV_CHANGE_TX_QUEUE_LEN:
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04004692 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01004693 GFP_KERNEL, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 break;
4695 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 break;
4697 }
4698 return NOTIFY_DONE;
4699}
4700
4701static struct notifier_block rtnetlink_dev_notifier = {
4702 .notifier_call = rtnetlink_event,
4703};
4704
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004705
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004706static int __net_init rtnetlink_net_init(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004707{
4708 struct sock *sk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00004709 struct netlink_kernel_cfg cfg = {
4710 .groups = RTNLGRP_MAX,
4711 .input = rtnetlink_rcv,
4712 .cb_mutex = &rtnl_mutex,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00004713 .flags = NL_CFG_F_NONROOT_RECV,
Julien Gomes5f729ea2017-06-20 13:54:16 -07004714 .bind = rtnetlink_bind,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00004715 };
4716
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00004717 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004718 if (!sk)
4719 return -ENOMEM;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004720 net->rtnl = sk;
4721 return 0;
4722}
4723
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004724static void __net_exit rtnetlink_net_exit(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004725{
Denis V. Lunev775516b2008-01-18 23:55:19 -08004726 netlink_kernel_release(net->rtnl);
4727 net->rtnl = NULL;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004728}
4729
4730static struct pernet_operations rtnetlink_net_ops = {
4731 .init = rtnetlink_net_init,
4732 .exit = rtnetlink_net_exit,
4733};
4734
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735void __init rtnetlink_init(void)
4736{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004737 if (register_pernet_subsys(&rtnetlink_net_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 panic("rtnetlink_init: cannot initialize rtnetlink\n");
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004739
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 register_netdevice_notifier(&rtnetlink_dev_notifier);
Thomas Graf340d17f2007-03-22 11:49:22 -07004741
Greg Rosec7ac8672011-06-10 01:27:09 +00004742 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
Florian Westphalb97bac62017-08-09 20:41:48 +02004743 rtnl_dump_ifinfo, 0);
4744 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
4745 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
4746 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
Thomas Graf687ad8c2007-03-22 11:59:42 -07004747
Florian Westphalb97bac62017-08-09 20:41:48 +02004748 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
4749 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
4750 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
John Fastabend77162022012-04-15 06:43:56 +00004751
Florian Westphalb97bac62017-08-09 20:41:48 +02004752 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
4753 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
4754 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, 0);
John Fastabende5a55a82012-10-24 08:12:57 +00004755
Florian Westphalb97bac62017-08-09 20:41:48 +02004756 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
4757 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
4758 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004759
4760 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
Florian Westphalb97bac62017-08-09 20:41:48 +02004761 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762}