blob: 80802546c279722fe7a110704a4b006e15aa35ec [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 *
Kirill Tkhai554873e2018-03-30 19:38:37 +0300415 * The caller must hold the rtnl_mutex and guarantee net_namespace_list
416 * integrity (hold pernet_ops_rwsem for writing to close the race
417 * with setup_net() and cleanup_net()).
Patrick McHardy38f7b872007-06-13 12:03:51 -0700418 */
419void __rtnl_link_unregister(struct rtnl_link_ops *ops)
420{
Eric W. Biederman881d9662007-09-17 11:56:21 -0700421 struct net *net;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700422
Eric W. Biederman881d9662007-09-17 11:56:21 -0700423 for_each_net(net) {
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700424 __rtnl_kill_links(net, ops);
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700425 }
Patrick McHardy38f7b872007-06-13 12:03:51 -0700426 list_del(&ops->list);
427}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700428EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
429
Cong Wang200b9162014-05-12 15:11:20 -0700430/* Return with the rtnl_lock held when there are no network
431 * devices unregistering in any network namespace.
432 */
433static void rtnl_lock_unregistering_all(void)
434{
435 struct net *net;
436 bool unregistering;
Peter Zijlstraff960a72014-10-29 17:04:56 +0100437 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Cong Wang200b9162014-05-12 15:11:20 -0700438
Peter Zijlstraff960a72014-10-29 17:04:56 +0100439 add_wait_queue(&netdev_unregistering_wq, &wait);
Cong Wang200b9162014-05-12 15:11:20 -0700440 for (;;) {
Cong Wang200b9162014-05-12 15:11:20 -0700441 unregistering = false;
442 rtnl_lock();
Kirill Tkhaif0b07bb12018-03-29 19:20:32 +0300443 /* We held write locked pernet_ops_rwsem, and parallel
444 * setup_net() and cleanup_net() are not possible.
445 */
Cong Wang200b9162014-05-12 15:11:20 -0700446 for_each_net(net) {
447 if (net->dev_unreg_count > 0) {
448 unregistering = true;
449 break;
450 }
451 }
452 if (!unregistering)
453 break;
454 __rtnl_unlock();
Peter Zijlstraff960a72014-10-29 17:04:56 +0100455
456 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Cong Wang200b9162014-05-12 15:11:20 -0700457 }
Peter Zijlstraff960a72014-10-29 17:04:56 +0100458 remove_wait_queue(&netdev_unregistering_wq, &wait);
Cong Wang200b9162014-05-12 15:11:20 -0700459}
460
Patrick McHardy38f7b872007-06-13 12:03:51 -0700461/**
462 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
463 * @ops: struct rtnl_link_ops * to unregister
464 */
465void rtnl_link_unregister(struct rtnl_link_ops *ops)
466{
Kirill Tkhai8518e9b2018-03-27 18:02:32 +0300467 /* Close the race with setup_net() and cleanup_net() */
Kirill Tkhai4420bf22018-03-27 18:02:23 +0300468 down_write(&pernet_ops_rwsem);
Cong Wang200b9162014-05-12 15:11:20 -0700469 rtnl_lock_unregistering_all();
Patrick McHardy38f7b872007-06-13 12:03:51 -0700470 __rtnl_link_unregister(ops);
471 rtnl_unlock();
Kirill Tkhai4420bf22018-03-27 18:02:23 +0300472 up_write(&pernet_ops_rwsem);
Patrick McHardy38f7b872007-06-13 12:03:51 -0700473}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700474EXPORT_SYMBOL_GPL(rtnl_link_unregister);
475
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100476static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
477{
478 struct net_device *master_dev;
479 const struct rtnl_link_ops *ops;
Florian Westphal8515ae32017-08-10 16:52:59 +0200480 size_t size = 0;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100481
Florian Westphal8515ae32017-08-10 16:52:59 +0200482 rcu_read_lock();
483
484 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100485 if (!master_dev)
Florian Westphal8515ae32017-08-10 16:52:59 +0200486 goto out;
487
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100488 ops = master_dev->rtnl_link_ops;
Fernando Luis Vazquez Cao6049f252014-02-04 19:35:02 +0900489 if (!ops || !ops->get_slave_size)
Florian Westphal8515ae32017-08-10 16:52:59 +0200490 goto out;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100491 /* IFLA_INFO_SLAVE_DATA + nested data */
Florian Westphal8515ae32017-08-10 16:52:59 +0200492 size = nla_total_size(sizeof(struct nlattr)) +
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100493 ops->get_slave_size(master_dev, dev);
Florian Westphal8515ae32017-08-10 16:52:59 +0200494
495out:
496 rcu_read_unlock();
497 return size;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100498}
499
Patrick McHardy38f7b872007-06-13 12:03:51 -0700500static size_t rtnl_link_get_size(const struct net_device *dev)
501{
502 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
503 size_t size;
504
505 if (!ops)
506 return 0;
507
Thomas Graf369cf772010-11-11 15:47:59 +0000508 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
509 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700510
511 if (ops->get_size)
512 /* IFLA_INFO_DATA + nested data */
Thomas Graf369cf772010-11-11 15:47:59 +0000513 size += nla_total_size(sizeof(struct nlattr)) +
Patrick McHardy38f7b872007-06-13 12:03:51 -0700514 ops->get_size(dev);
515
516 if (ops->get_xstats_size)
Thomas Graf369cf772010-11-11 15:47:59 +0000517 /* IFLA_INFO_XSTATS */
518 size += nla_total_size(ops->get_xstats_size(dev));
Patrick McHardy38f7b872007-06-13 12:03:51 -0700519
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100520 size += rtnl_link_get_slave_info_data_size(dev);
521
Patrick McHardy38f7b872007-06-13 12:03:51 -0700522 return size;
523}
524
Thomas Graff8ff1822010-11-16 04:30:14 +0000525static LIST_HEAD(rtnl_af_ops);
526
527static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
528{
529 const struct rtnl_af_ops *ops;
530
Florian Westphal5fa85a02017-10-16 15:44:36 +0200531 list_for_each_entry_rcu(ops, &rtnl_af_ops, list) {
Thomas Graff8ff1822010-11-16 04:30:14 +0000532 if (ops->family == family)
533 return ops;
534 }
535
536 return NULL;
537}
538
539/**
Thomas Graff8ff1822010-11-16 04:30:14 +0000540 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
541 * @ops: struct rtnl_af_ops * to register
542 *
543 * Returns 0 on success or a negative error code.
544 */
stephen hemminger3678a9d2013-12-30 10:41:32 -0800545void rtnl_af_register(struct rtnl_af_ops *ops)
Thomas Graff8ff1822010-11-16 04:30:14 +0000546{
Thomas Graff8ff1822010-11-16 04:30:14 +0000547 rtnl_lock();
Florian Westphal5fa85a02017-10-16 15:44:36 +0200548 list_add_tail_rcu(&ops->list, &rtnl_af_ops);
Thomas Graff8ff1822010-11-16 04:30:14 +0000549 rtnl_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +0000550}
551EXPORT_SYMBOL_GPL(rtnl_af_register);
552
553/**
Thomas Graff8ff1822010-11-16 04:30:14 +0000554 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
555 * @ops: struct rtnl_af_ops * to unregister
556 */
557void rtnl_af_unregister(struct rtnl_af_ops *ops)
558{
559 rtnl_lock();
Florian Westphal5fa85a02017-10-16 15:44:36 +0200560 list_del_rcu(&ops->list);
Thomas Graff8ff1822010-11-16 04:30:14 +0000561 rtnl_unlock();
Florian Westphal5fa85a02017-10-16 15:44:36 +0200562
563 synchronize_rcu();
Thomas Graff8ff1822010-11-16 04:30:14 +0000564}
565EXPORT_SYMBOL_GPL(rtnl_af_unregister);
566
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700567static size_t rtnl_link_get_af_size(const struct net_device *dev,
568 u32 ext_filter_mask)
Thomas Graff8ff1822010-11-16 04:30:14 +0000569{
570 struct rtnl_af_ops *af_ops;
571 size_t size;
572
573 /* IFLA_AF_SPEC */
574 size = nla_total_size(sizeof(struct nlattr));
575
Florian Westphal5fa85a02017-10-16 15:44:36 +0200576 rcu_read_lock();
577 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Thomas Graff8ff1822010-11-16 04:30:14 +0000578 if (af_ops->get_link_af_size) {
579 /* AF_* + nested data */
580 size += nla_total_size(sizeof(struct nlattr)) +
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700581 af_ops->get_link_af_size(dev, ext_filter_mask);
Thomas Graff8ff1822010-11-16 04:30:14 +0000582 }
583 }
Florian Westphal5fa85a02017-10-16 15:44:36 +0200584 rcu_read_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +0000585
586 return size;
587}
588
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100589static bool rtnl_have_link_slave_info(const struct net_device *dev)
590{
591 struct net_device *master_dev;
Florian Westphal4c82a952017-09-26 13:58:43 +0200592 bool ret = false;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100593
Florian Westphal4c82a952017-09-26 13:58:43 +0200594 rcu_read_lock();
595
596 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
Jiri Pirko813f0202014-01-23 19:19:21 +0100597 if (master_dev && master_dev->rtnl_link_ops)
Florian Westphal4c82a952017-09-26 13:58:43 +0200598 ret = true;
599 rcu_read_unlock();
600 return ret;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100601}
602
603static int rtnl_link_slave_info_fill(struct sk_buff *skb,
604 const struct net_device *dev)
605{
606 struct net_device *master_dev;
607 const struct rtnl_link_ops *ops;
608 struct nlattr *slave_data;
609 int err;
610
611 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
612 if (!master_dev)
613 return 0;
614 ops = master_dev->rtnl_link_ops;
615 if (!ops)
616 return 0;
617 if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
618 return -EMSGSIZE;
619 if (ops->fill_slave_info) {
620 slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA);
621 if (!slave_data)
622 return -EMSGSIZE;
623 err = ops->fill_slave_info(skb, master_dev, dev);
624 if (err < 0)
625 goto err_cancel_slave_data;
626 nla_nest_end(skb, slave_data);
627 }
628 return 0;
629
630err_cancel_slave_data:
631 nla_nest_cancel(skb, slave_data);
632 return err;
633}
634
635static int rtnl_link_info_fill(struct sk_buff *skb,
636 const struct net_device *dev)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700637{
638 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100639 struct nlattr *data;
640 int err;
641
642 if (!ops)
643 return 0;
644 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
645 return -EMSGSIZE;
646 if (ops->fill_xstats) {
647 err = ops->fill_xstats(skb, dev);
648 if (err < 0)
649 return err;
650 }
651 if (ops->fill_info) {
652 data = nla_nest_start(skb, IFLA_INFO_DATA);
653 if (data == NULL)
654 return -EMSGSIZE;
655 err = ops->fill_info(skb, dev);
656 if (err < 0)
657 goto err_cancel_data;
658 nla_nest_end(skb, data);
659 }
660 return 0;
661
662err_cancel_data:
663 nla_nest_cancel(skb, data);
664 return err;
665}
666
667static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
668{
669 struct nlattr *linkinfo;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700670 int err = -EMSGSIZE;
671
672 linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
673 if (linkinfo == NULL)
674 goto out;
675
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100676 err = rtnl_link_info_fill(skb, dev);
677 if (err < 0)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700678 goto err_cancel_link;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100679
680 err = rtnl_link_slave_info_fill(skb, dev);
681 if (err < 0)
682 goto err_cancel_link;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700683
684 nla_nest_end(skb, linkinfo);
685 return 0;
686
Patrick McHardy38f7b872007-06-13 12:03:51 -0700687err_cancel_link:
688 nla_nest_cancel(skb, linkinfo);
689out:
690 return err;
691}
692
Eric Dumazet95c96172012-04-15 05:58:06 +0000693int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800695 struct sock *rtnl = net->rtnl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 int err = 0;
697
Patrick McHardyac6d4392005-08-14 19:29:52 -0700698 NETLINK_CB(skb).dst_group = group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 if (echo)
Reshetova, Elena63354792017-06-30 13:07:58 +0300700 refcount_inc(&skb->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
702 if (echo)
703 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
704 return err;
705}
706
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800707int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
Thomas Graf2942e902006-08-15 00:30:25 -0700708{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800709 struct sock *rtnl = net->rtnl;
710
Thomas Graf2942e902006-08-15 00:30:25 -0700711 return nlmsg_unicast(rtnl, skb, pid);
712}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800713EXPORT_SYMBOL(rtnl_unicast);
Thomas Graf2942e902006-08-15 00:30:25 -0700714
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800715void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
716 struct nlmsghdr *nlh, gfp_t flags)
Thomas Graf97676b62006-08-15 00:31:41 -0700717{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800718 struct sock *rtnl = net->rtnl;
Thomas Graf97676b62006-08-15 00:31:41 -0700719 int report = 0;
720
721 if (nlh)
722 report = nlmsg_report(nlh);
723
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800724 nlmsg_notify(rtnl, skb, pid, group, report, flags);
Thomas Graf97676b62006-08-15 00:31:41 -0700725}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800726EXPORT_SYMBOL(rtnl_notify);
Thomas Graf97676b62006-08-15 00:31:41 -0700727
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800728void rtnl_set_sk_err(struct net *net, u32 group, int error)
Thomas Graf97676b62006-08-15 00:31:41 -0700729{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800730 struct sock *rtnl = net->rtnl;
731
Thomas Graf97676b62006-08-15 00:31:41 -0700732 netlink_set_err(rtnl, 0, group, error);
733}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800734EXPORT_SYMBOL(rtnl_set_sk_err);
Thomas Graf97676b62006-08-15 00:31:41 -0700735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
737{
Thomas Graf2d7202b2006-08-22 00:01:27 -0700738 struct nlattr *mx;
739 int i, valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Thomas Graf2d7202b2006-08-22 00:01:27 -0700741 mx = nla_nest_start(skb, RTA_METRICS);
742 if (mx == NULL)
743 return -ENOBUFS;
744
745 for (i = 0; i < RTAX_MAX; i++) {
746 if (metrics[i]) {
Daniel Borkmannea697632015-01-05 23:57:47 +0100747 if (i == RTAX_CC_ALGO - 1) {
748 char tmp[TCP_CA_NAME_MAX], *name;
749
750 name = tcp_ca_get_name_by_key(metrics[i], tmp);
751 if (!name)
752 continue;
753 if (nla_put_string(skb, i + 1, name))
754 goto nla_put_failure;
Daniel Borkmannc3a8d942015-08-31 15:58:47 +0200755 } else if (i == RTAX_FEATURES - 1) {
756 u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
757
Phil Sutterf8edcd12016-08-23 13:14:31 +0200758 if (!user_features)
759 continue;
Daniel Borkmannc3a8d942015-08-31 15:58:47 +0200760 BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
761 if (nla_put_u32(skb, i + 1, user_features))
762 goto nla_put_failure;
Daniel Borkmannea697632015-01-05 23:57:47 +0100763 } else {
764 if (nla_put_u32(skb, i + 1, metrics[i]))
765 goto nla_put_failure;
766 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700767 valid++;
Thomas Graf2d7202b2006-08-22 00:01:27 -0700768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
David S. Millera57d27f2006-08-22 22:20:14 -0700771 if (!valid) {
772 nla_nest_cancel(skb, mx);
773 return 0;
774 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700775
776 return nla_nest_end(skb, mx);
777
778nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700779 nla_nest_cancel(skb, mx);
780 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800782EXPORT_SYMBOL(rtnetlink_put_metrics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Thomas Grafe3703b32006-11-27 09:27:07 -0800784int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
David S. Miller87a50692012-07-10 05:06:14 -0700785 long expires, u32 error)
Thomas Grafe3703b32006-11-27 09:27:07 -0800786{
787 struct rta_cacheinfo ci = {
Thomas Grafe3703b32006-11-27 09:27:07 -0800788 .rta_error = error,
789 .rta_id = id,
Thomas Grafe3703b32006-11-27 09:27:07 -0800790 };
791
David Ahern39407462018-04-17 17:33:08 -0700792 if (dst) {
793 ci.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse);
794 ci.rta_used = dst->__use;
795 ci.rta_clntref = atomic_read(&dst->__refcnt);
796 }
Li Wei82539472012-07-29 16:01:30 +0000797 if (expires) {
798 unsigned long clock;
Thomas Grafe3703b32006-11-27 09:27:07 -0800799
Li Wei82539472012-07-29 16:01:30 +0000800 clock = jiffies_to_clock_t(abs(expires));
801 clock = min_t(unsigned long, clock, INT_MAX);
802 ci.rta_expires = (expires > 0) ? clock : -clock;
803 }
Thomas Grafe3703b32006-11-27 09:27:07 -0800804 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
805}
Thomas Grafe3703b32006-11-27 09:27:07 -0800806EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
David S. Miller93b2d4a2008-02-17 18:35:07 -0800808static void set_operstate(struct net_device *dev, unsigned char transition)
Stefan Rompfb00055a2006-03-20 17:09:11 -0800809{
810 unsigned char operstate = dev->operstate;
811
Eric Dumazete0d087a2009-11-07 01:26:17 -0800812 switch (transition) {
Stefan Rompfb00055a2006-03-20 17:09:11 -0800813 case IF_OPER_UP:
814 if ((operstate == IF_OPER_DORMANT ||
815 operstate == IF_OPER_UNKNOWN) &&
816 !netif_dormant(dev))
817 operstate = IF_OPER_UP;
818 break;
819
820 case IF_OPER_DORMANT:
821 if (operstate == IF_OPER_UP ||
822 operstate == IF_OPER_UNKNOWN)
823 operstate = IF_OPER_DORMANT;
824 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700825 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800826
827 if (dev->operstate != operstate) {
828 write_lock_bh(&dev_base_lock);
829 dev->operstate = operstate;
830 write_unlock_bh(&dev_base_lock);
David S. Miller93b2d4a2008-02-17 18:35:07 -0800831 netdev_state_change(dev);
832 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800833}
834
Jiri Bencb1beb682012-07-27 02:58:22 +0000835static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
836{
837 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
838 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
839}
840
Patrick McHardy3729d502010-02-26 06:34:54 +0000841static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
842 const struct ifinfomsg *ifm)
843{
844 unsigned int flags = ifm->ifi_flags;
845
846 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
847 if (ifm->ifi_change)
848 flags = (flags & ifm->ifi_change) |
Jiri Bencb1beb682012-07-27 02:58:22 +0000849 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
Patrick McHardy3729d502010-02-26 06:34:54 +0000850
851 return flags;
852}
853
Thomas Grafb60c5112006-08-04 23:05:34 -0700854static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000855 const struct rtnl_link_stats64 *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
Thomas Grafb60c5112006-08-04 23:05:34 -0700857 a->rx_packets = b->rx_packets;
858 a->tx_packets = b->tx_packets;
859 a->rx_bytes = b->rx_bytes;
860 a->tx_bytes = b->tx_bytes;
861 a->rx_errors = b->rx_errors;
862 a->tx_errors = b->tx_errors;
863 a->rx_dropped = b->rx_dropped;
864 a->tx_dropped = b->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Thomas Grafb60c5112006-08-04 23:05:34 -0700866 a->multicast = b->multicast;
867 a->collisions = b->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Thomas Grafb60c5112006-08-04 23:05:34 -0700869 a->rx_length_errors = b->rx_length_errors;
870 a->rx_over_errors = b->rx_over_errors;
871 a->rx_crc_errors = b->rx_crc_errors;
872 a->rx_frame_errors = b->rx_frame_errors;
873 a->rx_fifo_errors = b->rx_fifo_errors;
874 a->rx_missed_errors = b->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Thomas Grafb60c5112006-08-04 23:05:34 -0700876 a->tx_aborted_errors = b->tx_aborted_errors;
877 a->tx_carrier_errors = b->tx_carrier_errors;
878 a->tx_fifo_errors = b->tx_fifo_errors;
879 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
880 a->tx_window_errors = b->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Thomas Grafb60c5112006-08-04 23:05:34 -0700882 a->rx_compressed = b->rx_compressed;
883 a->tx_compressed = b->tx_compressed;
Jarod Wilson6e7333d2016-02-01 18:51:05 -0500884
885 a->rx_nohandler = b->rx_nohandler;
Jan Engelhardt10708f32010-03-11 09:57:29 +0000886}
887
Chris Wrightc02db8c2010-05-16 01:05:45 -0700888/* All VF info */
Greg Rose115c9b82012-02-21 16:54:48 -0500889static inline int rtnl_vfinfo_size(const struct net_device *dev,
890 u32 ext_filter_mask)
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000891{
Phil Sutter9af15c32017-01-18 14:04:39 +0100892 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
Chris Wrightc02db8c2010-05-16 01:05:45 -0700893 int num_vfs = dev_num_vf(dev->dev.parent);
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100894 size_t size = nla_total_size(0);
Scott Feldman045de012010-05-28 03:42:43 -0700895 size += num_vfs *
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100896 (nla_total_size(0) +
897 nla_total_size(sizeof(struct ifla_vf_mac)) +
898 nla_total_size(sizeof(struct ifla_vf_vlan)) +
899 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
Moshe Shemesh79aab092016-09-22 12:11:15 +0300900 nla_total_size(MAX_VLAN_LIST_LEN *
901 sizeof(struct ifla_vf_vlan_info)) +
Sucheta Chakrabortyed616682014-05-22 09:59:05 -0400902 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100903 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
Jiri Benc945a3672014-08-08 16:44:32 +0200904 nla_total_size(sizeof(struct ifla_vf_rate)) +
Vlad Zolotarov01a3d792015-03-30 21:35:23 +0300905 nla_total_size(sizeof(struct ifla_vf_link_state)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300906 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100907 nla_total_size(0) + /* nest IFLA_VF_STATS */
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300908 /* IFLA_VF_STATS_RX_PACKETS */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200909 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300910 /* IFLA_VF_STATS_TX_PACKETS */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200911 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300912 /* IFLA_VF_STATS_RX_BYTES */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200913 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300914 /* IFLA_VF_STATS_TX_BYTES */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200915 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300916 /* IFLA_VF_STATS_BROADCAST */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200917 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300918 /* IFLA_VF_STATS_MULTICAST */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200919 nla_total_size_64bit(sizeof(__u64)) +
Eugenia Emantayevc5a9f6f2017-07-17 13:47:07 +0300920 /* IFLA_VF_STATS_RX_DROPPED */
921 nla_total_size_64bit(sizeof(__u64)) +
922 /* IFLA_VF_STATS_TX_DROPPED */
923 nla_total_size_64bit(sizeof(__u64)) +
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +0000924 nla_total_size(sizeof(struct ifla_vf_trust)));
Chris Wrightc02db8c2010-05-16 01:05:45 -0700925 return size;
926 } else
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000927 return 0;
928}
929
David Gibsonc53864f2014-04-24 10:22:36 +1000930static size_t rtnl_port_size(const struct net_device *dev,
931 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -0700932{
933 size_t port_size = nla_total_size(4) /* PORT_VF */
934 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
Scott Feldman57b61082010-05-17 22:49:55 -0700935 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
936 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
937 + nla_total_size(1) /* PROT_VDP_REQUEST */
938 + nla_total_size(2); /* PORT_VDP_RESPONSE */
939 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
940 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
941 + port_size;
942 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
943 + port_size;
944
David Gibsonc53864f2014-04-24 10:22:36 +1000945 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
946 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -0700947 return 0;
948 if (dev_num_vf(dev->dev.parent))
949 return port_self_size + vf_ports_size +
950 vf_port_size * dev_num_vf(dev->dev.parent);
951 else
952 return port_self_size;
953}
954
David S. Millerb5cdae32017-04-18 15:36:58 -0400955static size_t rtnl_xdp_size(void)
Brenden Blancod1fdd912016-07-19 12:16:49 -0700956{
Sabrina Dubrocab3cfaa32016-11-15 11:16:35 +0100957 size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */
Martin KaFai Lau58038692017-06-15 17:29:09 -0700958 nla_total_size(1) + /* XDP_ATTACHED */
959 nla_total_size(4); /* XDP_PROG_ID */
Brenden Blancod1fdd912016-07-19 12:16:49 -0700960
David S. Millerb5cdae32017-04-18 15:36:58 -0400961 return xdp_size;
Brenden Blancod1fdd912016-07-19 12:16:49 -0700962}
963
Greg Rose115c9b82012-02-21 16:54:48 -0500964static noinline size_t if_nlmsg_size(const struct net_device *dev,
965 u32 ext_filter_mask)
Thomas Graf339bf982006-11-10 14:10:15 -0800966{
967 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
968 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700969 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
Thomas Graf339bf982006-11-10 14:10:15 -0800970 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
Nicolas Dichtel270cb4d2016-04-26 10:06:16 +0200971 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
Thomas Graf339bf982006-11-10 14:10:15 -0800972 + nla_total_size(sizeof(struct rtnl_link_stats))
David S. Miller35c58452016-04-19 19:49:29 -0400973 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
Thomas Graf339bf982006-11-10 14:10:15 -0800974 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
975 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
976 + nla_total_size(4) /* IFLA_TXQLEN */
977 + nla_total_size(4) /* IFLA_WEIGHT */
978 + nla_total_size(4) /* IFLA_MTU */
979 + nla_total_size(4) /* IFLA_LINK */
980 + nla_total_size(4) /* IFLA_MASTER */
Jiri Pirko9a572472012-12-27 23:49:39 +0000981 + nla_total_size(1) /* IFLA_CARRIER */
Ben Greearedbc0bb2012-03-29 12:51:30 +0000982 + nla_total_size(4) /* IFLA_PROMISCUITY */
Jiri Pirko76ff5cc2012-07-20 02:28:48 +0000983 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
984 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
Tobias Klauser69197562016-11-30 14:30:37 +0100985 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
986 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
Thomas Graf339bf982006-11-10 14:10:15 -0800987 + nla_total_size(1) /* IFLA_OPERSTATE */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700988 + nla_total_size(1) /* IFLA_LINKMODE */
david decotigny2d3b4792014-03-29 09:48:35 -0700989 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
Nicolas Dichteld37512a2015-01-15 15:11:16 +0100990 + nla_total_size(4) /* IFLA_LINK_NETNSID */
Serhey Popovychdb833d42017-06-20 14:35:23 +0300991 + nla_total_size(4) /* IFLA_GROUP */
Greg Rose115c9b82012-02-21 16:54:48 -0500992 + nla_total_size(ext_filter_mask
993 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
994 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
David Gibsonc53864f2014-04-24 10:22:36 +1000995 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
Thomas Graff8ff1822010-11-16 04:30:14 +0000996 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700997 + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
Jiri Pirko82f28412014-11-28 14:34:18 +0100998 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
Anuradha Karuppiah88d63782015-07-14 13:43:20 -0700999 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
Nicolas Dichtelc57c7a92016-03-31 18:10:31 +02001000 + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
David S. Millerb5cdae32017-04-18 15:36:58 -04001001 + rtnl_xdp_size() /* IFLA_XDP */
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001002 + nla_total_size(4) /* IFLA_EVENT */
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02001003 + nla_total_size(4) /* IFLA_NEW_NETNSID */
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01001004 + nla_total_size(4) /* IFLA_NEW_IFINDEX */
Colin Ian King03ac7382017-11-06 15:04:54 +00001005 + nla_total_size(1) /* IFLA_PROTO_DOWN */
Jiri Benc79e1ad12017-11-02 17:04:38 -02001006 + nla_total_size(4) /* IFLA_IF_NETNSID */
David Decotignyb2d3bcf2018-01-18 09:59:13 -08001007 + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */
1008 + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */
Jiri Benc79e1ad12017-11-02 17:04:38 -02001009 + 0;
Thomas Graf339bf982006-11-10 14:10:15 -08001010}
1011
Scott Feldman57b61082010-05-17 22:49:55 -07001012static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
1013{
1014 struct nlattr *vf_ports;
1015 struct nlattr *vf_port;
1016 int vf;
1017 int err;
1018
1019 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
1020 if (!vf_ports)
1021 return -EMSGSIZE;
1022
1023 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
1024 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
Scott Feldman8ca94182010-05-28 03:42:18 -07001025 if (!vf_port)
1026 goto nla_put_failure;
David S. Millera6574342012-04-01 20:12:00 -04001027 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
1028 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001029 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
Scott Feldman8ca94182010-05-28 03:42:18 -07001030 if (err == -EMSGSIZE)
1031 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001032 if (err) {
Scott Feldman57b61082010-05-17 22:49:55 -07001033 nla_nest_cancel(skb, vf_port);
1034 continue;
1035 }
1036 nla_nest_end(skb, vf_port);
1037 }
1038
1039 nla_nest_end(skb, vf_ports);
1040
1041 return 0;
Scott Feldman8ca94182010-05-28 03:42:18 -07001042
1043nla_put_failure:
1044 nla_nest_cancel(skb, vf_ports);
1045 return -EMSGSIZE;
Scott Feldman57b61082010-05-17 22:49:55 -07001046}
1047
1048static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
1049{
1050 struct nlattr *port_self;
1051 int err;
1052
1053 port_self = nla_nest_start(skb, IFLA_PORT_SELF);
1054 if (!port_self)
1055 return -EMSGSIZE;
1056
1057 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
1058 if (err) {
1059 nla_nest_cancel(skb, port_self);
Scott Feldman8ca94182010-05-28 03:42:18 -07001060 return (err == -EMSGSIZE) ? err : 0;
Scott Feldman57b61082010-05-17 22:49:55 -07001061 }
1062
1063 nla_nest_end(skb, port_self);
1064
1065 return 0;
1066}
1067
David Gibsonc53864f2014-04-24 10:22:36 +10001068static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1069 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -07001070{
1071 int err;
1072
David Gibsonc53864f2014-04-24 10:22:36 +10001073 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1074 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -07001075 return 0;
1076
1077 err = rtnl_port_self_fill(skb, dev);
1078 if (err)
1079 return err;
1080
1081 if (dev_num_vf(dev->dev.parent)) {
1082 err = rtnl_vf_ports_fill(skb, dev);
1083 if (err)
1084 return err;
1085 }
1086
1087 return 0;
1088}
1089
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001090static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
1091{
1092 int err;
Jiri Pirko02637fc2014-11-28 14:34:16 +01001093 struct netdev_phys_item_id ppid;
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001094
1095 err = dev_get_phys_port_id(dev, &ppid);
1096 if (err) {
1097 if (err == -EOPNOTSUPP)
1098 return 0;
1099 return err;
1100 }
1101
1102 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1103 return -EMSGSIZE;
1104
1105 return 0;
1106}
1107
David Aherndb24a902015-03-17 20:23:15 -06001108static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1109{
1110 char name[IFNAMSIZ];
1111 int err;
1112
1113 err = dev_get_phys_port_name(dev, name, sizeof(name));
1114 if (err) {
1115 if (err == -EOPNOTSUPP)
1116 return 0;
1117 return err;
1118 }
1119
Michal Schmidt77ef0332017-05-04 16:48:58 +02001120 if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
David Aherndb24a902015-03-17 20:23:15 -06001121 return -EMSGSIZE;
1122
1123 return 0;
1124}
1125
Jiri Pirko82f28412014-11-28 14:34:18 +01001126static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1127{
1128 int err;
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001129 struct switchdev_attr attr = {
Ido Schimmel6ff64f62015-12-15 16:03:35 +01001130 .orig_dev = dev,
Jiri Pirko1f868392015-10-01 11:03:42 +02001131 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001132 .flags = SWITCHDEV_F_NO_RECURSE,
1133 };
Jiri Pirko82f28412014-11-28 14:34:18 +01001134
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001135 err = switchdev_port_attr_get(dev, &attr);
Jiri Pirko82f28412014-11-28 14:34:18 +01001136 if (err) {
1137 if (err == -EOPNOTSUPP)
1138 return 0;
1139 return err;
1140 }
1141
Scott Feldman42275bd2015-05-13 11:16:50 -07001142 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len,
1143 attr.u.ppid.id))
Jiri Pirko82f28412014-11-28 14:34:18 +01001144 return -EMSGSIZE;
1145
1146 return 0;
1147}
1148
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001149static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1150 struct net_device *dev)
1151{
Roopa Prabhu550bce52016-04-15 20:36:25 -07001152 struct rtnl_link_stats64 *sp;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001153 struct nlattr *attr;
1154
Nicolas Dichtel58414d32016-04-21 18:58:25 +02001155 attr = nla_reserve_64bit(skb, IFLA_STATS64,
1156 sizeof(struct rtnl_link_stats64), IFLA_PAD);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001157 if (!attr)
1158 return -EMSGSIZE;
1159
Roopa Prabhu550bce52016-04-15 20:36:25 -07001160 sp = nla_data(attr);
1161 dev_get_stats(dev, sp);
1162
1163 attr = nla_reserve(skb, IFLA_STATS,
1164 sizeof(struct rtnl_link_stats));
1165 if (!attr)
1166 return -EMSGSIZE;
1167
1168 copy_rtnl_link_stats(nla_data(attr), sp);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001169
1170 return 0;
1171}
1172
1173static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1174 struct net_device *dev,
1175 int vfs_num,
1176 struct nlattr *vfinfo)
1177{
1178 struct ifla_vf_rss_query_en vf_rss_query_en;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001179 struct nlattr *vf, *vfstats, *vfvlanlist;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001180 struct ifla_vf_link_state vf_linkstate;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001181 struct ifla_vf_vlan_info vf_vlan_info;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001182 struct ifla_vf_spoofchk vf_spoofchk;
1183 struct ifla_vf_tx_rate vf_tx_rate;
1184 struct ifla_vf_stats vf_stats;
1185 struct ifla_vf_trust vf_trust;
1186 struct ifla_vf_vlan vf_vlan;
1187 struct ifla_vf_rate vf_rate;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001188 struct ifla_vf_mac vf_mac;
1189 struct ifla_vf_info ivi;
1190
Mintz, Yuval0eed9cf2017-06-07 21:00:33 +03001191 memset(&ivi, 0, sizeof(ivi));
1192
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001193 /* Not all SR-IOV capable drivers support the
1194 * spoofcheck and "RSS query enable" query. Preset to
1195 * -1 so the user space tool can detect that the driver
1196 * didn't report anything.
1197 */
1198 ivi.spoofchk = -1;
1199 ivi.rss_query_en = -1;
1200 ivi.trusted = -1;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001201 /* The default value for VF link state is "auto"
1202 * IFLA_VF_LINK_STATE_AUTO which equals zero
1203 */
1204 ivi.linkstate = 0;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001205 /* VLAN Protocol by default is 802.1Q */
1206 ivi.vlan_proto = htons(ETH_P_8021Q);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001207 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1208 return 0;
1209
Dan Carpenter775f4f02016-10-13 11:45:28 +03001210 memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
1211
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001212 vf_mac.vf =
1213 vf_vlan.vf =
Moshe Shemesh79aab092016-09-22 12:11:15 +03001214 vf_vlan_info.vf =
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001215 vf_rate.vf =
1216 vf_tx_rate.vf =
1217 vf_spoofchk.vf =
1218 vf_linkstate.vf =
1219 vf_rss_query_en.vf =
1220 vf_trust.vf = ivi.vf;
1221
1222 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1223 vf_vlan.vlan = ivi.vlan;
1224 vf_vlan.qos = ivi.qos;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001225 vf_vlan_info.vlan = ivi.vlan;
1226 vf_vlan_info.qos = ivi.qos;
1227 vf_vlan_info.vlan_proto = ivi.vlan_proto;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001228 vf_tx_rate.rate = ivi.max_tx_rate;
1229 vf_rate.min_tx_rate = ivi.min_tx_rate;
1230 vf_rate.max_tx_rate = ivi.max_tx_rate;
1231 vf_spoofchk.setting = ivi.spoofchk;
1232 vf_linkstate.link_state = ivi.linkstate;
1233 vf_rss_query_en.setting = ivi.rss_query_en;
1234 vf_trust.setting = ivi.trusted;
1235 vf = nla_nest_start(skb, IFLA_VF_INFO);
Moshe Shemesh79aab092016-09-22 12:11:15 +03001236 if (!vf)
1237 goto nla_put_vfinfo_failure;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001238 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1239 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1240 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1241 &vf_rate) ||
1242 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1243 &vf_tx_rate) ||
1244 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1245 &vf_spoofchk) ||
1246 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1247 &vf_linkstate) ||
1248 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1249 sizeof(vf_rss_query_en),
1250 &vf_rss_query_en) ||
1251 nla_put(skb, IFLA_VF_TRUST,
1252 sizeof(vf_trust), &vf_trust))
Moshe Shemesh79aab092016-09-22 12:11:15 +03001253 goto nla_put_vf_failure;
1254 vfvlanlist = nla_nest_start(skb, IFLA_VF_VLAN_LIST);
1255 if (!vfvlanlist)
1256 goto nla_put_vf_failure;
1257 if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
1258 &vf_vlan_info)) {
1259 nla_nest_cancel(skb, vfvlanlist);
1260 goto nla_put_vf_failure;
1261 }
1262 nla_nest_end(skb, vfvlanlist);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001263 memset(&vf_stats, 0, sizeof(vf_stats));
1264 if (dev->netdev_ops->ndo_get_vf_stats)
1265 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1266 &vf_stats);
1267 vfstats = nla_nest_start(skb, IFLA_VF_STATS);
Moshe Shemesh79aab092016-09-22 12:11:15 +03001268 if (!vfstats)
1269 goto nla_put_vf_failure;
Nicolas Dichtel343a6d82016-04-25 10:25:14 +02001270 if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1271 vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1272 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1273 vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1274 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1275 vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1276 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1277 vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1278 nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1279 vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1280 nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
Eugenia Emantayevc5a9f6f2017-07-17 13:47:07 +03001281 vf_stats.multicast, IFLA_VF_STATS_PAD) ||
1282 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED,
1283 vf_stats.rx_dropped, IFLA_VF_STATS_PAD) ||
1284 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED,
1285 vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) {
Moshe Shemesh79aab092016-09-22 12:11:15 +03001286 nla_nest_cancel(skb, vfstats);
1287 goto nla_put_vf_failure;
1288 }
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001289 nla_nest_end(skb, vfstats);
1290 nla_nest_end(skb, vf);
1291 return 0;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001292
1293nla_put_vf_failure:
1294 nla_nest_cancel(skb, vf);
1295nla_put_vfinfo_failure:
1296 nla_nest_cancel(skb, vfinfo);
1297 return -EMSGSIZE;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001298}
1299
Florian Westphal250fc3d2017-09-26 13:58:41 +02001300static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb,
1301 struct net_device *dev,
1302 u32 ext_filter_mask)
1303{
1304 struct nlattr *vfinfo;
1305 int i, num_vfs;
1306
1307 if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0))
1308 return 0;
1309
1310 num_vfs = dev_num_vf(dev->dev.parent);
1311 if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs))
1312 return -EMSGSIZE;
1313
1314 if (!dev->netdev_ops->ndo_get_vf_config)
1315 return 0;
1316
1317 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1318 if (!vfinfo)
1319 return -EMSGSIZE;
1320
1321 for (i = 0; i < num_vfs; i++) {
1322 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1323 return -EMSGSIZE;
1324 }
1325
1326 nla_nest_end(skb, vfinfo);
1327 return 0;
1328}
1329
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001330static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1331{
Kangjie Lu5f8e4472016-05-03 16:46:24 -04001332 struct rtnl_link_ifmap map;
1333
1334 memset(&map, 0, sizeof(map));
1335 map.mem_start = dev->mem_start;
1336 map.mem_end = dev->mem_end;
1337 map.base_addr = dev->base_addr;
1338 map.irq = dev->irq;
1339 map.dma = dev->dma;
1340 map.port = dev->if_port;
1341
Nicolas Dichtel270cb4d2016-04-26 10:06:16 +02001342 if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001343 return -EMSGSIZE;
1344
1345 return 0;
1346}
1347
Martin KaFai Lau58038692017-06-15 17:29:09 -07001348static u8 rtnl_xdp_attached_mode(struct net_device *dev, u32 *prog_id)
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001349{
1350 const struct net_device_ops *ops = dev->netdev_ops;
Martin KaFai Lau58038692017-06-15 17:29:09 -07001351 const struct bpf_prog *generic_xdp_prog;
Jakub Kicinski118b4aa2017-12-01 15:08:55 -08001352 struct netdev_bpf xdp;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001353
1354 ASSERT_RTNL();
1355
Martin KaFai Lau58038692017-06-15 17:29:09 -07001356 *prog_id = 0;
1357 generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
1358 if (generic_xdp_prog) {
1359 *prog_id = generic_xdp_prog->aux->id;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001360 return XDP_ATTACHED_SKB;
Martin KaFai Lau58038692017-06-15 17:29:09 -07001361 }
Jakub Kicinskif4e63522017-11-03 13:56:16 -07001362 if (!ops->ndo_bpf)
Jakub Kicinskice158e52017-06-21 18:25:09 -07001363 return XDP_ATTACHED_NONE;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001364
Jakub Kicinski118b4aa2017-12-01 15:08:55 -08001365 __dev_xdp_query(dev, ops->ndo_bpf, &xdp);
1366 *prog_id = xdp.prog_id;
1367
1368 return xdp.prog_attached;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001369}
1370
Brenden Blancod1fdd912016-07-19 12:16:49 -07001371static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1372{
Brenden Blancod1fdd912016-07-19 12:16:49 -07001373 struct nlattr *xdp;
Martin KaFai Lau58038692017-06-15 17:29:09 -07001374 u32 prog_id;
Brenden Blancod1fdd912016-07-19 12:16:49 -07001375 int err;
1376
Brenden Blancod1fdd912016-07-19 12:16:49 -07001377 xdp = nla_nest_start(skb, IFLA_XDP);
1378 if (!xdp)
1379 return -EMSGSIZE;
David S. Millerb5cdae32017-04-18 15:36:58 -04001380
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001381 err = nla_put_u8(skb, IFLA_XDP_ATTACHED,
Martin KaFai Lau58038692017-06-15 17:29:09 -07001382 rtnl_xdp_attached_mode(dev, &prog_id));
Brenden Blancod1fdd912016-07-19 12:16:49 -07001383 if (err)
1384 goto err_cancel;
1385
Martin KaFai Lau58038692017-06-15 17:29:09 -07001386 if (prog_id) {
1387 err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
1388 if (err)
1389 goto err_cancel;
1390 }
1391
Brenden Blancod1fdd912016-07-19 12:16:49 -07001392 nla_nest_end(skb, xdp);
1393 return 0;
1394
1395err_cancel:
1396 nla_nest_cancel(skb, xdp);
1397 return err;
1398}
1399
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001400static u32 rtnl_get_event(unsigned long event)
1401{
1402 u32 rtnl_event_type = IFLA_EVENT_NONE;
1403
1404 switch (event) {
1405 case NETDEV_REBOOT:
1406 rtnl_event_type = IFLA_EVENT_REBOOT;
1407 break;
1408 case NETDEV_FEAT_CHANGE:
1409 rtnl_event_type = IFLA_EVENT_FEATURES;
1410 break;
1411 case NETDEV_BONDING_FAILOVER:
1412 rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
1413 break;
1414 case NETDEV_NOTIFY_PEERS:
1415 rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
1416 break;
1417 case NETDEV_RESEND_IGMP:
1418 rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
1419 break;
1420 case NETDEV_CHANGEINFODATA:
1421 rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
1422 break;
1423 default:
1424 break;
1425 }
1426
1427 return rtnl_event_type;
1428}
1429
Florian Westphal79110a02017-09-26 13:58:40 +02001430static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
1431{
1432 const struct net_device *upper_dev;
1433 int ret = 0;
1434
1435 rcu_read_lock();
1436
1437 upper_dev = netdev_master_upper_dev_get_rcu(dev);
1438 if (upper_dev)
1439 ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);
1440
1441 rcu_read_unlock();
1442 return ret;
1443}
1444
1445static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev)
1446{
1447 int ifindex = dev_get_iflink(dev);
1448
1449 if (dev->ifindex == ifindex)
1450 return 0;
1451
1452 return nla_put_u32(skb, IFLA_LINK, ifindex);
1453}
1454
Florian Westphal6c557002017-10-02 23:50:05 +02001455static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
1456 struct net_device *dev)
1457{
1458 char buf[IFALIASZ];
1459 int ret;
1460
1461 ret = dev_get_alias(dev, buf, sizeof(buf));
1462 return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
1463}
1464
Florian Westphalb1e66b92017-09-26 13:58:42 +02001465static int rtnl_fill_link_netnsid(struct sk_buff *skb,
Jiri Benc79e1ad12017-11-02 17:04:38 -02001466 const struct net_device *dev,
1467 struct net *src_net)
Florian Westphalb1e66b92017-09-26 13:58:42 +02001468{
1469 if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
1470 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1471
1472 if (!net_eq(dev_net(dev), link_net)) {
Jiri Benc79e1ad12017-11-02 17:04:38 -02001473 int id = peernet2id_alloc(src_net, link_net);
Florian Westphalb1e66b92017-09-26 13:58:42 +02001474
1475 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1476 return -EMSGSIZE;
1477 }
1478 }
1479
1480 return 0;
1481}
1482
Florian Westphal070cbf52017-10-16 15:44:35 +02001483static int rtnl_fill_link_af(struct sk_buff *skb,
1484 const struct net_device *dev,
1485 u32 ext_filter_mask)
1486{
1487 const struct rtnl_af_ops *af_ops;
1488 struct nlattr *af_spec;
1489
1490 af_spec = nla_nest_start(skb, IFLA_AF_SPEC);
1491 if (!af_spec)
1492 return -EMSGSIZE;
1493
Florian Westphal5fa85a02017-10-16 15:44:36 +02001494 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Florian Westphal070cbf52017-10-16 15:44:35 +02001495 struct nlattr *af;
1496 int err;
1497
1498 if (!af_ops->fill_link_af)
1499 continue;
1500
1501 af = nla_nest_start(skb, af_ops->family);
1502 if (!af)
1503 return -EMSGSIZE;
1504
1505 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1506 /*
1507 * Caller may return ENODATA to indicate that there
1508 * was no data to be dumped. This is not an error, it
1509 * means we should trim the attribute header and
1510 * continue.
1511 */
1512 if (err == -ENODATA)
1513 nla_nest_cancel(skb, af);
1514 else if (err < 0)
1515 return -EMSGSIZE;
1516
1517 nla_nest_end(skb, af);
1518 }
1519
1520 nla_nest_end(skb, af_spec);
1521 return 0;
1522}
1523
Jiri Benc79e1ad12017-11-02 17:04:38 -02001524static int rtnl_fill_ifinfo(struct sk_buff *skb,
1525 struct net_device *dev, struct net *src_net,
Patrick McHardy575c3e22007-05-22 17:00:49 -07001526 int type, u32 pid, u32 seq, u32 change,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001527 unsigned int flags, u32 ext_filter_mask,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01001528 u32 event, int *new_nsid, int new_ifindex,
1529 int tgt_netnsid)
Thomas Grafb60c5112006-08-04 23:05:34 -07001530{
1531 struct ifinfomsg *ifm;
1532 struct nlmsghdr *nlh;
1533
Eric Dumazet2907c352011-05-25 07:34:04 +00001534 ASSERT_RTNL();
Thomas Grafb60c5112006-08-04 23:05:34 -07001535 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1536 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08001537 return -EMSGSIZE;
Thomas Grafb60c5112006-08-04 23:05:34 -07001538
1539 ifm = nlmsg_data(nlh);
1540 ifm->ifi_family = AF_UNSPEC;
1541 ifm->__ifi_pad = 0;
1542 ifm->ifi_type = dev->type;
1543 ifm->ifi_index = dev->ifindex;
1544 ifm->ifi_flags = dev_get_flags(dev);
1545 ifm->ifi_change = change;
1546
Jiri Benc79e1ad12017-11-02 17:04:38 -02001547 if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_IF_NETNSID, tgt_netnsid))
1548 goto nla_put_failure;
1549
David S. Millera6574342012-04-01 20:12:00 -04001550 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1551 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1552 nla_put_u8(skb, IFLA_OPERSTATE,
1553 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1554 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1555 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1556 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
Ben Greearedbc0bb2012-03-29 12:51:30 +00001557 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001558 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
Eric Dumazetc70ce022016-03-21 09:55:10 -07001559 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1560 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001561#ifdef CONFIG_RPS
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001562 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001563#endif
Florian Westphal79110a02017-09-26 13:58:40 +02001564 nla_put_iflink(skb, dev) ||
1565 put_master_ifindex(skb, dev) ||
Jiri Pirko9a572472012-12-27 23:49:39 +00001566 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
David S. Millera6574342012-04-01 20:12:00 -04001567 (dev->qdisc &&
1568 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
Florian Westphal6c557002017-10-02 23:50:05 +02001569 nla_put_ifalias(skb, dev) ||
david decotigny2d3b4792014-03-29 09:48:35 -07001570 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
David Decotignyb2d3bcf2018-01-18 09:59:13 -08001571 atomic_read(&dev->carrier_up_count) +
1572 atomic_read(&dev->carrier_down_count)) ||
1573 nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down) ||
1574 nla_put_u32(skb, IFLA_CARRIER_UP_COUNT,
1575 atomic_read(&dev->carrier_up_count)) ||
1576 nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT,
1577 atomic_read(&dev->carrier_down_count)))
David S. Millera6574342012-04-01 20:12:00 -04001578 goto nla_put_failure;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001579
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001580 if (event != IFLA_EVENT_NONE) {
1581 if (nla_put_u32(skb, IFLA_EVENT, event))
1582 goto nla_put_failure;
1583 }
1584
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001585 if (rtnl_fill_link_ifmap(skb, dev))
1586 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
1588 if (dev->addr_len) {
David S. Millera6574342012-04-01 20:12:00 -04001589 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1590 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1591 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 }
1593
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001594 if (rtnl_phys_port_id_fill(skb, dev))
1595 goto nla_put_failure;
1596
David Aherndb24a902015-03-17 20:23:15 -06001597 if (rtnl_phys_port_name_fill(skb, dev))
1598 goto nla_put_failure;
1599
Jiri Pirko82f28412014-11-28 14:34:18 +01001600 if (rtnl_phys_switch_id_fill(skb, dev))
1601 goto nla_put_failure;
1602
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001603 if (rtnl_fill_stats(skb, dev))
Pavel Emelyanov96e74082008-05-21 14:12:46 -07001604 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
Florian Westphal250fc3d2017-09-26 13:58:41 +02001606 if (rtnl_fill_vf(skb, dev, ext_filter_mask))
David S. Millera6574342012-04-01 20:12:00 -04001607 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001608
David Gibsonc53864f2014-04-24 10:22:36 +10001609 if (rtnl_port_fill(skb, dev, ext_filter_mask))
Scott Feldman57b61082010-05-17 22:49:55 -07001610 goto nla_put_failure;
1611
Brenden Blancod1fdd912016-07-19 12:16:49 -07001612 if (rtnl_xdp_fill(skb, dev))
1613 goto nla_put_failure;
1614
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001615 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07001616 if (rtnl_link_fill(skb, dev) < 0)
1617 goto nla_put_failure;
1618 }
1619
Jiri Benc79e1ad12017-11-02 17:04:38 -02001620 if (rtnl_fill_link_netnsid(skb, dev, src_net))
Florian Westphalb1e66b92017-09-26 13:58:42 +02001621 goto nla_put_failure;
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001622
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02001623 if (new_nsid &&
1624 nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0)
1625 goto nla_put_failure;
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01001626 if (new_ifindex &&
1627 nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0)
1628 goto nla_put_failure;
1629
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02001630
Florian Westphal5fa85a02017-10-16 15:44:36 +02001631 rcu_read_lock();
Florian Westphal070cbf52017-10-16 15:44:35 +02001632 if (rtnl_fill_link_af(skb, dev, ext_filter_mask))
Florian Westphal5fa85a02017-10-16 15:44:36 +02001633 goto nla_put_failure_rcu;
1634 rcu_read_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +00001635
Johannes Berg053c0952015-01-16 22:09:00 +01001636 nlmsg_end(skb, nlh);
1637 return 0;
Thomas Grafb60c5112006-08-04 23:05:34 -07001638
Florian Westphal5fa85a02017-10-16 15:44:36 +02001639nla_put_failure_rcu:
1640 rcu_read_unlock();
Thomas Grafb60c5112006-08-04 23:05:34 -07001641nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001642 nlmsg_cancel(skb, nlh);
1643 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644}
1645
Jiri Pirkof7b12602014-02-18 20:53:18 +01001646static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1647 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1648 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1649 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1650 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1651 [IFLA_MTU] = { .type = NLA_U32 },
1652 [IFLA_LINK] = { .type = NLA_U32 },
1653 [IFLA_MASTER] = { .type = NLA_U32 },
1654 [IFLA_CARRIER] = { .type = NLA_U8 },
1655 [IFLA_TXQLEN] = { .type = NLA_U32 },
1656 [IFLA_WEIGHT] = { .type = NLA_U32 },
1657 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1658 [IFLA_LINKMODE] = { .type = NLA_U8 },
1659 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1660 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1661 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
Nicolas Dichtel2459b4c2017-10-11 16:24:48 +02001662 /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
1663 * allow 0-length string (needed to remove an alias).
1664 */
1665 [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001666 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1667 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1668 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1669 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1670 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1671 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1672 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1673 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
Stephen Hemminger46e6b992017-12-07 15:40:19 -08001674 [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 },
1675 [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 },
Jiri Pirko02637fc2014-11-28 14:34:16 +01001676 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
david decotigny2d3b4792014-03-29 09:48:35 -07001677 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
Jiri Pirko82f28412014-11-28 14:34:18 +01001678 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
Nicolas Dichtel317f4812015-01-15 15:11:18 +01001679 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001680 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001681 [IFLA_XDP] = { .type = NLA_NESTED },
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001682 [IFLA_EVENT] = { .type = NLA_U32 },
Serhey Popovychdb833d42017-06-20 14:35:23 +03001683 [IFLA_GROUP] = { .type = NLA_U32 },
Jiri Benc79e1ad12017-11-02 17:04:38 -02001684 [IFLA_IF_NETNSID] = { .type = NLA_S32 },
David Decotignyb2d3bcf2018-01-18 09:59:13 -08001685 [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 },
1686 [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001687};
1688
1689static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1690 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1691 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1692 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1693 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1694};
1695
Jiri Pirkof7b12602014-02-18 20:53:18 +01001696static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
Daniel Borkmann364d5712015-02-05 18:44:04 +01001697 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1698 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
Moshe Shemesh79aab092016-09-22 12:11:15 +03001699 [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED },
Daniel Borkmann364d5712015-02-05 18:44:04 +01001700 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1701 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1702 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1703 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001704 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001705 [IFLA_VF_STATS] = { .type = NLA_NESTED },
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00001706 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
Eli Cohencc8e27c2016-03-11 22:58:34 +02001707 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1708 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001709};
1710
Jiri Pirkof7b12602014-02-18 20:53:18 +01001711static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1712 [IFLA_PORT_VF] = { .type = NLA_U32 },
1713 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1714 .len = PORT_PROFILE_MAX },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001715 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1716 .len = PORT_UUID_MAX },
1717 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1718 .len = PORT_UUID_MAX },
1719 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1720 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
Daniel Borkmann025331d2017-02-17 01:56:11 +01001721
1722 /* Unused, but we need to keep it here since user space could
1723 * fill it. It's also broken with regard to NLA_BINARY use in
1724 * combination with structs.
1725 */
1726 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1727 .len = sizeof(struct ifla_port_vsi) },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001728};
1729
Brenden Blancod1fdd912016-07-19 12:16:49 -07001730static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
1731 [IFLA_XDP_FD] = { .type = NLA_S32 },
1732 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 },
Daniel Borkmann85de8572016-11-28 23:16:54 +01001733 [IFLA_XDP_FLAGS] = { .type = NLA_U32 },
Martin KaFai Lau58038692017-06-15 17:29:09 -07001734 [IFLA_XDP_PROG_ID] = { .type = NLA_U32 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001735};
1736
David Aherndc599f72016-02-02 08:17:07 -08001737static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1738{
1739 const struct rtnl_link_ops *ops = NULL;
1740 struct nlattr *linfo[IFLA_INFO_MAX + 1];
1741
Johannes Bergfceb6432017-04-12 14:34:07 +02001742 if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla,
1743 ifla_info_policy, NULL) < 0)
David Aherndc599f72016-02-02 08:17:07 -08001744 return NULL;
1745
1746 if (linfo[IFLA_INFO_KIND]) {
1747 char kind[MODULE_NAME_LEN];
1748
1749 nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1750 ops = rtnl_link_ops_get(kind);
1751 }
1752
1753 return ops;
1754}
1755
1756static bool link_master_filtered(struct net_device *dev, int master_idx)
1757{
1758 struct net_device *master;
1759
1760 if (!master_idx)
1761 return false;
1762
1763 master = netdev_master_upper_dev_get(dev);
1764 if (!master || master->ifindex != master_idx)
1765 return true;
1766
1767 return false;
1768}
1769
1770static bool link_kind_filtered(const struct net_device *dev,
1771 const struct rtnl_link_ops *kind_ops)
1772{
1773 if (kind_ops && dev->rtnl_link_ops != kind_ops)
1774 return true;
1775
1776 return false;
1777}
1778
1779static bool link_dump_filtered(struct net_device *dev,
1780 int master_idx,
1781 const struct rtnl_link_ops *kind_ops)
1782{
1783 if (link_master_filtered(dev, master_idx) ||
1784 link_kind_filtered(dev, kind_ops))
1785 return true;
1786
1787 return false;
1788}
1789
Andrei Vaginf428fe42018-01-02 23:27:33 -08001790static struct net *get_target_net(struct sock *sk, int netnsid)
Jiri Benc79e1ad12017-11-02 17:04:38 -02001791{
1792 struct net *net;
1793
Andrei Vaginf428fe42018-01-02 23:27:33 -08001794 net = get_net_ns_by_id(sock_net(sk), netnsid);
Jiri Benc79e1ad12017-11-02 17:04:38 -02001795 if (!net)
1796 return ERR_PTR(-EINVAL);
1797
1798 /* For now, the caller is required to have CAP_NET_ADMIN in
1799 * the user namespace owning the target net ns.
1800 */
Andrei Vaginf428fe42018-01-02 23:27:33 -08001801 if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) {
Jiri Benc79e1ad12017-11-02 17:04:38 -02001802 put_net(net);
1803 return ERR_PTR(-EACCES);
1804 }
1805 return net;
1806}
1807
Thomas Grafb60c5112006-08-04 23:05:34 -07001808static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001810 struct net *net = sock_net(skb->sk);
Jiri Benc79e1ad12017-11-02 17:04:38 -02001811 struct net *tgt_net = net;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001812 int h, s_h;
1813 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 struct net_device *dev;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001815 struct hlist_head *head;
Greg Rose115c9b82012-02-21 16:54:48 -05001816 struct nlattr *tb[IFLA_MAX+1];
1817 u32 ext_filter_mask = 0;
David Aherndc599f72016-02-02 08:17:07 -08001818 const struct rtnl_link_ops *kind_ops = NULL;
1819 unsigned int flags = NLM_F_MULTI;
1820 int master_idx = 0;
Jiri Benc79e1ad12017-11-02 17:04:38 -02001821 int netnsid = -1;
David Gibson973462b2014-04-24 10:22:35 +10001822 int err;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001823 int hdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001825 s_h = cb->args[0];
1826 s_idx = cb->args[1];
1827
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001828 /* A hack to preserve kernel<->userspace interface.
1829 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1830 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1831 * what iproute2 < v3.9.0 used.
1832 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1833 * attribute, its netlink message is shorter than struct ifinfomsg.
1834 */
1835 hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1836 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1837
Johannes Bergfceb6432017-04-12 14:34:07 +02001838 if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX,
1839 ifla_policy, NULL) >= 0) {
Jiri Benc79e1ad12017-11-02 17:04:38 -02001840 if (tb[IFLA_IF_NETNSID]) {
1841 netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]);
Andrei Vaginf428fe42018-01-02 23:27:33 -08001842 tgt_net = get_target_net(skb->sk, netnsid);
Jiri Benc79e1ad12017-11-02 17:04:38 -02001843 if (IS_ERR(tgt_net)) {
1844 tgt_net = net;
1845 netnsid = -1;
1846 }
1847 }
1848
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001849 if (tb[IFLA_EXT_MASK])
1850 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
David Aherndc599f72016-02-02 08:17:07 -08001851
1852 if (tb[IFLA_MASTER])
1853 master_idx = nla_get_u32(tb[IFLA_MASTER]);
1854
1855 if (tb[IFLA_LINKINFO])
1856 kind_ops = linkinfo_to_kind_ops(tb[IFLA_LINKINFO]);
1857
1858 if (master_idx || kind_ops)
1859 flags |= NLM_F_DUMP_FILTERED;
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001860 }
Greg Rose115c9b82012-02-21 16:54:48 -05001861
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001862 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1863 idx = 0;
Jiri Benc79e1ad12017-11-02 17:04:38 -02001864 head = &tgt_net->dev_index_head[h];
Eric Dumazetcac5e652015-02-27 09:42:50 -08001865 hlist_for_each_entry(dev, head, index_hlist) {
David Aherndc599f72016-02-02 08:17:07 -08001866 if (link_dump_filtered(dev, master_idx, kind_ops))
Zhang Shengju3f0ae052016-11-19 23:28:32 +08001867 goto cont;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001868 if (idx < s_idx)
1869 goto cont;
Jiri Benc79e1ad12017-11-02 17:04:38 -02001870 err = rtnl_fill_ifinfo(skb, dev, net,
1871 RTM_NEWLINK,
David Gibson973462b2014-04-24 10:22:35 +10001872 NETLINK_CB(cb->skb).portid,
1873 cb->nlh->nlmsg_seq, 0,
David Aherndc599f72016-02-02 08:17:07 -08001874 flags,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01001875 ext_filter_mask, 0, NULL, 0,
Jiri Benc79e1ad12017-11-02 17:04:38 -02001876 netnsid);
David Gibson973462b2014-04-24 10:22:35 +10001877
David Ahernf6c57752017-05-15 23:19:17 -07001878 if (err < 0) {
1879 if (likely(skb->len))
1880 goto out;
1881
1882 goto out_err;
1883 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07001884cont:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001885 idx++;
1886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 }
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001888out:
David Ahernf6c57752017-05-15 23:19:17 -07001889 err = skb->len;
1890out_err:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001891 cb->args[1] = idx;
1892 cb->args[0] = h;
Jakub Sitnickid0225782017-08-09 17:39:12 +02001893 cb->seq = net->dev_base_seq;
1894 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Jiri Benc79e1ad12017-11-02 17:04:38 -02001895 if (netnsid >= 0)
1896 put_net(tgt_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
David Ahernf6c57752017-05-15 23:19:17 -07001898 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899}
1900
Johannes Bergfceb6432017-04-12 14:34:07 +02001901int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
1902 struct netlink_ext_ack *exterr)
Jiri Pirkof7b12602014-02-18 20:53:18 +01001903{
Johannes Bergfceb6432017-04-12 14:34:07 +02001904 return nla_parse(tb, IFLA_MAX, head, len, ifla_policy, exterr);
Jiri Pirkof7b12602014-02-18 20:53:18 +01001905}
1906EXPORT_SYMBOL(rtnl_nla_parse_ifla);
Scott Feldman57b61082010-05-17 22:49:55 -07001907
Eric W. Biederman81adee42009-11-08 00:53:51 -08001908struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1909{
1910 struct net *net;
1911 /* Examine the link attributes and figure out which
1912 * network namespace we are talking about.
1913 */
1914 if (tb[IFLA_NET_NS_PID])
1915 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001916 else if (tb[IFLA_NET_NS_FD])
1917 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
Eric W. Biederman81adee42009-11-08 00:53:51 -08001918 else
1919 net = get_net(src_net);
1920 return net;
1921}
1922EXPORT_SYMBOL(rtnl_link_get_net);
1923
Christian Brauner7c4f63b2018-01-24 15:26:32 +01001924/* Figure out which network namespace we are talking about by
1925 * examining the link attributes in the following order:
1926 *
1927 * 1. IFLA_NET_NS_PID
1928 * 2. IFLA_NET_NS_FD
1929 * 3. IFLA_IF_NETNSID
1930 */
1931static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net,
1932 struct nlattr *tb[])
1933{
1934 struct net *net;
1935
1936 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])
1937 return rtnl_link_get_net(src_net, tb);
1938
1939 if (!tb[IFLA_IF_NETNSID])
1940 return get_net(src_net);
1941
1942 net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_IF_NETNSID]));
1943 if (!net)
1944 return ERR_PTR(-EINVAL);
1945
1946 return net;
1947}
1948
1949static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb,
1950 struct net *src_net,
1951 struct nlattr *tb[], int cap)
1952{
1953 struct net *net;
1954
1955 net = rtnl_link_get_net_by_nlattr(src_net, tb);
1956 if (IS_ERR(net))
1957 return net;
1958
1959 if (!netlink_ns_capable(skb, net->user_ns, cap)) {
1960 put_net(net);
1961 return ERR_PTR(-EPERM);
1962 }
1963
1964 return net;
1965}
1966
Christian Brauner4ff66ca2018-02-07 13:53:20 +01001967/* Verify that rtnetlink requests do not pass additional properties
1968 * potentially referring to different network namespaces.
1969 */
1970static int rtnl_ensure_unique_netns(struct nlattr *tb[],
1971 struct netlink_ext_ack *extack,
1972 bool netns_id_only)
1973{
1974
1975 if (netns_id_only) {
1976 if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD])
1977 return 0;
1978
1979 NL_SET_ERR_MSG(extack, "specified netns attribute not supported");
1980 return -EOPNOTSUPP;
1981 }
1982
1983 if (tb[IFLA_IF_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]))
1984 goto invalid_attr;
1985
1986 if (tb[IFLA_NET_NS_PID] && (tb[IFLA_IF_NETNSID] || tb[IFLA_NET_NS_FD]))
1987 goto invalid_attr;
1988
1989 if (tb[IFLA_NET_NS_FD] && (tb[IFLA_IF_NETNSID] || tb[IFLA_NET_NS_PID]))
1990 goto invalid_attr;
1991
1992 return 0;
1993
1994invalid_attr:
1995 NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified");
1996 return -EINVAL;
1997}
1998
Thomas Graf1840bb12008-02-23 19:54:36 -08001999static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
2000{
2001 if (dev) {
2002 if (tb[IFLA_ADDRESS] &&
2003 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
2004 return -EINVAL;
2005
2006 if (tb[IFLA_BROADCAST] &&
2007 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
2008 return -EINVAL;
2009 }
2010
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002011 if (tb[IFLA_AF_SPEC]) {
2012 struct nlattr *af;
2013 int rem, err;
2014
2015 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2016 const struct rtnl_af_ops *af_ops;
2017
Florian Westphal5fa85a02017-10-16 15:44:36 +02002018 rcu_read_lock();
2019 af_ops = rtnl_af_lookup(nla_type(af));
2020 if (!af_ops) {
2021 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002022 return -EAFNOSUPPORT;
Florian Westphal5fa85a02017-10-16 15:44:36 +02002023 }
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002024
Florian Westphal5fa85a02017-10-16 15:44:36 +02002025 if (!af_ops->set_link_af) {
2026 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002027 return -EOPNOTSUPP;
Florian Westphal5fa85a02017-10-16 15:44:36 +02002028 }
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002029
2030 if (af_ops->validate_link_af) {
Kurt Van Dijck6d3a9a62011-01-26 04:55:24 +00002031 err = af_ops->validate_link_af(dev, af);
Florian Westphal5fa85a02017-10-16 15:44:36 +02002032 if (err < 0) {
2033 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002034 return err;
Florian Westphal5fa85a02017-10-16 15:44:36 +02002035 }
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002036 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02002037
2038 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002039 }
2040 }
2041
Thomas Graf1840bb12008-02-23 19:54:36 -08002042 return 0;
2043}
2044
Eli Cohencc8e27c2016-03-11 22:58:34 +02002045static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
2046 int guid_type)
2047{
2048 const struct net_device_ops *ops = dev->netdev_ops;
2049
2050 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
2051}
2052
2053static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
2054{
2055 if (dev->type != ARPHRD_INFINIBAND)
2056 return -EOPNOTSUPP;
2057
2058 return handle_infiniband_guid(dev, ivt, guid_type);
2059}
2060
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002061static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
Chris Wrightc02db8c2010-05-16 01:05:45 -07002062{
Chris Wrightc02db8c2010-05-16 01:05:45 -07002063 const struct net_device_ops *ops = dev->netdev_ops;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002064 int err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002065
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002066 if (tb[IFLA_VF_MAC]) {
2067 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03002068
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002069 err = -EOPNOTSUPP;
2070 if (ops->ndo_set_vf_mac)
2071 err = ops->ndo_set_vf_mac(dev, ivm->vf,
2072 ivm->mac);
2073 if (err < 0)
2074 return err;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002075 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002076
2077 if (tb[IFLA_VF_VLAN]) {
2078 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
2079
2080 err = -EOPNOTSUPP;
2081 if (ops->ndo_set_vf_vlan)
2082 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
Moshe Shemesh79aab092016-09-22 12:11:15 +03002083 ivv->qos,
2084 htons(ETH_P_8021Q));
2085 if (err < 0)
2086 return err;
2087 }
2088
2089 if (tb[IFLA_VF_VLAN_LIST]) {
2090 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
2091 struct nlattr *attr;
2092 int rem, len = 0;
2093
2094 err = -EOPNOTSUPP;
2095 if (!ops->ndo_set_vf_vlan)
2096 return err;
2097
2098 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
2099 if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
2100 nla_len(attr) < NLA_HDRLEN) {
2101 return -EINVAL;
2102 }
2103 if (len >= MAX_VLAN_LIST_LEN)
2104 return -EOPNOTSUPP;
2105 ivvl[len] = nla_data(attr);
2106
2107 len++;
2108 }
Arnd Bergmannfa34cd92016-09-30 18:13:49 +02002109 if (len == 0)
2110 return -EINVAL;
2111
Moshe Shemesh79aab092016-09-22 12:11:15 +03002112 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
2113 ivvl[0]->qos, ivvl[0]->vlan_proto);
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002114 if (err < 0)
2115 return err;
2116 }
2117
2118 if (tb[IFLA_VF_TX_RATE]) {
2119 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
2120 struct ifla_vf_info ivf;
2121
2122 err = -EOPNOTSUPP;
2123 if (ops->ndo_get_vf_config)
2124 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
2125 if (err < 0)
2126 return err;
2127
2128 err = -EOPNOTSUPP;
2129 if (ops->ndo_set_vf_rate)
2130 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2131 ivf.min_tx_rate,
2132 ivt->rate);
2133 if (err < 0)
2134 return err;
2135 }
2136
2137 if (tb[IFLA_VF_RATE]) {
2138 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
2139
2140 err = -EOPNOTSUPP;
2141 if (ops->ndo_set_vf_rate)
2142 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2143 ivt->min_tx_rate,
2144 ivt->max_tx_rate);
2145 if (err < 0)
2146 return err;
2147 }
2148
2149 if (tb[IFLA_VF_SPOOFCHK]) {
2150 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
2151
2152 err = -EOPNOTSUPP;
2153 if (ops->ndo_set_vf_spoofchk)
2154 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
2155 ivs->setting);
2156 if (err < 0)
2157 return err;
2158 }
2159
2160 if (tb[IFLA_VF_LINK_STATE]) {
2161 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
2162
2163 err = -EOPNOTSUPP;
2164 if (ops->ndo_set_vf_link_state)
2165 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
2166 ivl->link_state);
2167 if (err < 0)
2168 return err;
2169 }
2170
2171 if (tb[IFLA_VF_RSS_QUERY_EN]) {
2172 struct ifla_vf_rss_query_en *ivrssq_en;
2173
2174 err = -EOPNOTSUPP;
2175 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
2176 if (ops->ndo_set_vf_rss_query_en)
2177 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
2178 ivrssq_en->setting);
2179 if (err < 0)
2180 return err;
2181 }
2182
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00002183 if (tb[IFLA_VF_TRUST]) {
2184 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
2185
2186 err = -EOPNOTSUPP;
2187 if (ops->ndo_set_vf_trust)
2188 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
2189 if (err < 0)
2190 return err;
2191 }
2192
Eli Cohencc8e27c2016-03-11 22:58:34 +02002193 if (tb[IFLA_VF_IB_NODE_GUID]) {
2194 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
2195
2196 if (!ops->ndo_set_vf_guid)
2197 return -EOPNOTSUPP;
2198
2199 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
2200 }
2201
2202 if (tb[IFLA_VF_IB_PORT_GUID]) {
2203 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
2204
2205 if (!ops->ndo_set_vf_guid)
2206 return -EOPNOTSUPP;
2207
2208 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
2209 }
2210
Chris Wrightc02db8c2010-05-16 01:05:45 -07002211 return err;
2212}
2213
David Ahern33eaf2a2017-10-04 17:48:46 -07002214static int do_set_master(struct net_device *dev, int ifindex,
2215 struct netlink_ext_ack *extack)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002216{
Jiri Pirko898e5062013-01-03 22:48:52 +00002217 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002218 const struct net_device_ops *ops;
2219 int err;
2220
Jiri Pirko898e5062013-01-03 22:48:52 +00002221 if (upper_dev) {
2222 if (upper_dev->ifindex == ifindex)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002223 return 0;
Jiri Pirko898e5062013-01-03 22:48:52 +00002224 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002225 if (ops->ndo_del_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002226 err = ops->ndo_del_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002227 if (err)
2228 return err;
2229 } else {
2230 return -EOPNOTSUPP;
2231 }
2232 }
2233
2234 if (ifindex) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002235 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
2236 if (!upper_dev)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002237 return -EINVAL;
Jiri Pirko898e5062013-01-03 22:48:52 +00002238 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002239 if (ops->ndo_add_slave) {
David Ahern33eaf2a2017-10-04 17:48:46 -07002240 err = ops->ndo_add_slave(upper_dev, dev, extack);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002241 if (err)
2242 return err;
2243 } else {
2244 return -EOPNOTSUPP;
2245 }
2246 }
2247 return 0;
2248}
2249
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002250#define DO_SETLINK_MODIFIED 0x01
Nicolas Dichtelba998902014-09-01 16:07:29 +02002251/* notify flag means notify + modified. */
2252#define DO_SETLINK_NOTIFY 0x03
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002253static int do_setlink(const struct sk_buff *skb,
2254 struct net_device *dev, struct ifinfomsg *ifm,
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002255 struct netlink_ext_ack *extack,
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002256 struct nlattr **tb, char *ifname, int status)
Patrick McHardy0157f602007-06-13 12:03:36 -07002257{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002258 const struct net_device_ops *ops = dev->netdev_ops;
Patrick McHardy0157f602007-06-13 12:03:36 -07002259 int err;
2260
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002261 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_IF_NETNSID]) {
2262 struct net *net = rtnl_link_get_net_capable(skb, dev_net(dev),
2263 tb, CAP_NET_ADMIN);
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002264 if (IS_ERR(net)) {
2265 err = PTR_ERR(net);
2266 goto errout;
2267 }
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002268
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002269 err = dev_change_net_namespace(dev, net, ifname);
2270 put_net(net);
2271 if (err)
2272 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002273 status |= DO_SETLINK_MODIFIED;
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002274 }
2275
Patrick McHardy0157f602007-06-13 12:03:36 -07002276 if (tb[IFLA_MAP]) {
2277 struct rtnl_link_ifmap *u_map;
2278 struct ifmap k_map;
2279
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002280 if (!ops->ndo_set_config) {
Patrick McHardy0157f602007-06-13 12:03:36 -07002281 err = -EOPNOTSUPP;
2282 goto errout;
2283 }
2284
2285 if (!netif_device_present(dev)) {
2286 err = -ENODEV;
2287 goto errout;
2288 }
2289
2290 u_map = nla_data(tb[IFLA_MAP]);
2291 k_map.mem_start = (unsigned long) u_map->mem_start;
2292 k_map.mem_end = (unsigned long) u_map->mem_end;
2293 k_map.base_addr = (unsigned short) u_map->base_addr;
2294 k_map.irq = (unsigned char) u_map->irq;
2295 k_map.dma = (unsigned char) u_map->dma;
2296 k_map.port = (unsigned char) u_map->port;
2297
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002298 err = ops->ndo_set_config(dev, &k_map);
Patrick McHardy0157f602007-06-13 12:03:36 -07002299 if (err < 0)
2300 goto errout;
2301
Nicolas Dichtelba998902014-09-01 16:07:29 +02002302 status |= DO_SETLINK_NOTIFY;
Patrick McHardy0157f602007-06-13 12:03:36 -07002303 }
2304
2305 if (tb[IFLA_ADDRESS]) {
2306 struct sockaddr *sa;
2307 int len;
2308
WANG Cong153711f2017-07-20 11:27:57 -07002309 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2310 sizeof(*sa));
Patrick McHardy0157f602007-06-13 12:03:36 -07002311 sa = kmalloc(len, GFP_KERNEL);
2312 if (!sa) {
2313 err = -ENOMEM;
2314 goto errout;
2315 }
2316 sa->sa_family = dev->type;
2317 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
2318 dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00002319 err = dev_set_mac_address(dev, sa);
Patrick McHardy0157f602007-06-13 12:03:36 -07002320 kfree(sa);
2321 if (err)
2322 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002323 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002324 }
2325
2326 if (tb[IFLA_MTU]) {
2327 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
2328 if (err < 0)
2329 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002330 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002331 }
2332
Vlad Dogarucbda10f2011-01-13 23:38:30 +00002333 if (tb[IFLA_GROUP]) {
2334 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Nicolas Dichtelba998902014-09-01 16:07:29 +02002335 status |= DO_SETLINK_NOTIFY;
Vlad Dogarucbda10f2011-01-13 23:38:30 +00002336 }
2337
Patrick McHardy0157f602007-06-13 12:03:36 -07002338 /*
2339 * Interface selected by interface index but interface
2340 * name provided implies that a name change has been
2341 * requested.
2342 */
2343 if (ifm->ifi_index > 0 && ifname[0]) {
2344 err = dev_change_name(dev, ifname);
2345 if (err < 0)
2346 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002347 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002348 }
2349
Stephen Hemminger0b815a12008-09-22 21:28:11 -07002350 if (tb[IFLA_IFALIAS]) {
2351 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2352 nla_len(tb[IFLA_IFALIAS]));
2353 if (err < 0)
2354 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002355 status |= DO_SETLINK_NOTIFY;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07002356 }
2357
Patrick McHardy0157f602007-06-13 12:03:36 -07002358 if (tb[IFLA_BROADCAST]) {
2359 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00002360 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Patrick McHardy0157f602007-06-13 12:03:36 -07002361 }
2362
2363 if (ifm->ifi_flags || ifm->ifi_change) {
Patrick McHardy3729d502010-02-26 06:34:54 +00002364 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
Johannes Berg5f9021c2008-11-16 23:20:31 -08002365 if (err < 0)
2366 goto errout;
Patrick McHardy0157f602007-06-13 12:03:36 -07002367 }
2368
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002369 if (tb[IFLA_MASTER]) {
David Ahern33eaf2a2017-10-04 17:48:46 -07002370 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002371 if (err)
2372 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002373 status |= DO_SETLINK_MODIFIED;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002374 }
2375
Jiri Pirko9a572472012-12-27 23:49:39 +00002376 if (tb[IFLA_CARRIER]) {
2377 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2378 if (err)
2379 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002380 status |= DO_SETLINK_MODIFIED;
Jiri Pirko9a572472012-12-27 23:49:39 +00002381 }
2382
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002383 if (tb[IFLA_TXQLEN]) {
Alexey Dobriyan0cd29502017-05-17 13:30:44 +03002384 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002385
Cong Wang6a643dd2018-01-25 18:26:22 -08002386 err = dev_change_tx_queue_len(dev, value);
2387 if (err)
2388 goto errout;
2389 status |= DO_SETLINK_MODIFIED;
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002390 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002391
Stephen Hemminger46e6b992017-12-07 15:40:19 -08002392 if (tb[IFLA_GSO_MAX_SIZE]) {
2393 u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]);
2394
2395 if (max_size > GSO_MAX_SIZE) {
2396 err = -EINVAL;
2397 goto errout;
2398 }
2399
2400 if (dev->gso_max_size ^ max_size) {
2401 netif_set_gso_max_size(dev, max_size);
2402 status |= DO_SETLINK_MODIFIED;
2403 }
2404 }
2405
2406 if (tb[IFLA_GSO_MAX_SEGS]) {
2407 u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
2408
2409 if (max_segs > GSO_MAX_SEGS) {
2410 err = -EINVAL;
2411 goto errout;
2412 }
2413
2414 if (dev->gso_max_segs ^ max_segs) {
2415 dev->gso_max_segs = max_segs;
2416 status |= DO_SETLINK_MODIFIED;
2417 }
2418 }
2419
Patrick McHardy0157f602007-06-13 12:03:36 -07002420 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002421 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Patrick McHardy0157f602007-06-13 12:03:36 -07002422
2423 if (tb[IFLA_LINKMODE]) {
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002424 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2425
David S. Miller93b2d4a2008-02-17 18:35:07 -08002426 write_lock_bh(&dev_base_lock);
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002427 if (dev->link_mode ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02002428 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002429 dev->link_mode = value;
David S. Miller93b2d4a2008-02-17 18:35:07 -08002430 write_unlock_bh(&dev_base_lock);
Patrick McHardy0157f602007-06-13 12:03:36 -07002431 }
2432
Chris Wrightc02db8c2010-05-16 01:05:45 -07002433 if (tb[IFLA_VFINFO_LIST]) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002434 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
Chris Wrightc02db8c2010-05-16 01:05:45 -07002435 struct nlattr *attr;
2436 int rem;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002437
Chris Wrightc02db8c2010-05-16 01:05:45 -07002438 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002439 if (nla_type(attr) != IFLA_VF_INFO ||
2440 nla_len(attr) < NLA_HDRLEN) {
David Howells253683b2010-05-21 02:25:27 +00002441 err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002442 goto errout;
David Howells253683b2010-05-21 02:25:27 +00002443 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002444 err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr,
Johannes Bergfceb6432017-04-12 14:34:07 +02002445 ifla_vf_policy, NULL);
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002446 if (err < 0)
2447 goto errout;
2448 err = do_setvfinfo(dev, vfinfo);
Chris Wrightc02db8c2010-05-16 01:05:45 -07002449 if (err < 0)
2450 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002451 status |= DO_SETLINK_NOTIFY;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002452 }
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00002453 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002454 err = 0;
2455
Scott Feldman57b61082010-05-17 22:49:55 -07002456 if (tb[IFLA_VF_PORTS]) {
2457 struct nlattr *port[IFLA_PORT_MAX+1];
2458 struct nlattr *attr;
2459 int vf;
2460 int rem;
2461
2462 err = -EOPNOTSUPP;
2463 if (!ops->ndo_set_vf_port)
2464 goto errout;
2465
2466 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
Daniel Borkmann035d2102015-07-13 00:06:02 +02002467 if (nla_type(attr) != IFLA_VF_PORT ||
2468 nla_len(attr) < NLA_HDRLEN) {
2469 err = -EINVAL;
2470 goto errout;
2471 }
2472 err = nla_parse_nested(port, IFLA_PORT_MAX, attr,
Johannes Bergfceb6432017-04-12 14:34:07 +02002473 ifla_port_policy, NULL);
Scott Feldman57b61082010-05-17 22:49:55 -07002474 if (err < 0)
2475 goto errout;
2476 if (!port[IFLA_PORT_VF]) {
2477 err = -EOPNOTSUPP;
2478 goto errout;
2479 }
2480 vf = nla_get_u32(port[IFLA_PORT_VF]);
2481 err = ops->ndo_set_vf_port(dev, vf, port);
2482 if (err < 0)
2483 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002484 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07002485 }
2486 }
2487 err = 0;
2488
2489 if (tb[IFLA_PORT_SELF]) {
2490 struct nlattr *port[IFLA_PORT_MAX+1];
2491
2492 err = nla_parse_nested(port, IFLA_PORT_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +02002493 tb[IFLA_PORT_SELF], ifla_port_policy,
2494 NULL);
Scott Feldman57b61082010-05-17 22:49:55 -07002495 if (err < 0)
2496 goto errout;
2497
2498 err = -EOPNOTSUPP;
2499 if (ops->ndo_set_vf_port)
2500 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
2501 if (err < 0)
2502 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002503 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07002504 }
Thomas Graff8ff1822010-11-16 04:30:14 +00002505
2506 if (tb[IFLA_AF_SPEC]) {
2507 struct nlattr *af;
2508 int rem;
2509
2510 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2511 const struct rtnl_af_ops *af_ops;
2512
Florian Westphal5fa85a02017-10-16 15:44:36 +02002513 rcu_read_lock();
2514
Gustavo A. R. Silva058c8d52017-10-20 19:43:11 -05002515 BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
Thomas Graff8ff1822010-11-16 04:30:14 +00002516
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002517 err = af_ops->set_link_af(dev, af);
Florian Westphal5fa85a02017-10-16 15:44:36 +02002518 if (err < 0) {
2519 rcu_read_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +00002520 goto errout;
Florian Westphal5fa85a02017-10-16 15:44:36 +02002521 }
Thomas Graff8ff1822010-11-16 04:30:14 +00002522
Florian Westphal5fa85a02017-10-16 15:44:36 +02002523 rcu_read_unlock();
Nicolas Dichtelba998902014-09-01 16:07:29 +02002524 status |= DO_SETLINK_NOTIFY;
Thomas Graff8ff1822010-11-16 04:30:14 +00002525 }
2526 }
Scott Feldman57b61082010-05-17 22:49:55 -07002527 err = 0;
2528
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07002529 if (tb[IFLA_PROTO_DOWN]) {
2530 err = dev_change_proto_down(dev,
2531 nla_get_u8(tb[IFLA_PROTO_DOWN]));
2532 if (err)
2533 goto errout;
2534 status |= DO_SETLINK_NOTIFY;
2535 }
2536
Brenden Blancod1fdd912016-07-19 12:16:49 -07002537 if (tb[IFLA_XDP]) {
2538 struct nlattr *xdp[IFLA_XDP_MAX + 1];
Daniel Borkmann85de8572016-11-28 23:16:54 +01002539 u32 xdp_flags = 0;
Brenden Blancod1fdd912016-07-19 12:16:49 -07002540
2541 err = nla_parse_nested(xdp, IFLA_XDP_MAX, tb[IFLA_XDP],
Johannes Bergfceb6432017-04-12 14:34:07 +02002542 ifla_xdp_policy, NULL);
Brenden Blancod1fdd912016-07-19 12:16:49 -07002543 if (err < 0)
2544 goto errout;
2545
Martin KaFai Lau58038692017-06-15 17:29:09 -07002546 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
Brenden Blanco262d8622016-07-20 17:22:34 -07002547 err = -EINVAL;
2548 goto errout;
2549 }
Daniel Borkmann85de8572016-11-28 23:16:54 +01002550
2551 if (xdp[IFLA_XDP_FLAGS]) {
2552 xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
2553 if (xdp_flags & ~XDP_FLAGS_MASK) {
2554 err = -EINVAL;
2555 goto errout;
2556 }
Jakub Kicinskiee5d0322017-06-21 18:25:04 -07002557 if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
Daniel Borkmann0489df92017-05-12 01:04:45 +02002558 err = -EINVAL;
2559 goto errout;
2560 }
Daniel Borkmann85de8572016-11-28 23:16:54 +01002561 }
2562
Brenden Blancod1fdd912016-07-19 12:16:49 -07002563 if (xdp[IFLA_XDP_FD]) {
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002564 err = dev_change_xdp_fd(dev, extack,
Daniel Borkmann85de8572016-11-28 23:16:54 +01002565 nla_get_s32(xdp[IFLA_XDP_FD]),
2566 xdp_flags);
Brenden Blancod1fdd912016-07-19 12:16:49 -07002567 if (err)
2568 goto errout;
2569 status |= DO_SETLINK_NOTIFY;
2570 }
2571 }
2572
Patrick McHardy0157f602007-06-13 12:03:36 -07002573errout:
Nicolas Dichtelba998902014-09-01 16:07:29 +02002574 if (status & DO_SETLINK_MODIFIED) {
Xin Long64ff90c2017-10-15 18:13:45 +08002575 if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
Nicolas Dichtelba998902014-09-01 16:07:29 +02002576 netdev_state_change(dev);
2577
2578 if (err < 0)
2579 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",
2580 dev->name);
2581 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002582
Patrick McHardy0157f602007-06-13 12:03:36 -07002583 return err;
2584}
2585
David Ahernc21ef3e2017-04-16 09:48:24 -07002586static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2587 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002589 struct net *net = sock_net(skb->sk);
Thomas Grafda5e0492006-08-10 21:17:37 -07002590 struct ifinfomsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 struct net_device *dev;
Patrick McHardy0157f602007-06-13 12:03:36 -07002592 int err;
Thomas Grafda5e0492006-08-10 21:17:37 -07002593 struct nlattr *tb[IFLA_MAX+1];
2594 char ifname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
David Ahernc21ef3e2017-04-16 09:48:24 -07002596 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy,
2597 extack);
Thomas Grafda5e0492006-08-10 21:17:37 -07002598 if (err < 0)
2599 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002601 err = rtnl_ensure_unique_netns(tb, extack, false);
2602 if (err < 0)
2603 goto errout;
2604
Thomas Graf5176f912006-08-26 20:13:18 -07002605 if (tb[IFLA_IFNAME])
2606 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
Patrick McHardy78e5b8912006-09-13 20:35:36 -07002607 else
2608 ifname[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 err = -EINVAL;
Thomas Grafda5e0492006-08-10 21:17:37 -07002611 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -07002612 if (ifm->ifi_index > 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00002613 dev = __dev_get_by_index(net, ifm->ifi_index);
Thomas Grafda5e0492006-08-10 21:17:37 -07002614 else if (tb[IFLA_IFNAME])
Eric Dumazeta3d12892009-10-21 10:59:31 +00002615 dev = __dev_get_by_name(net, ifname);
Thomas Grafda5e0492006-08-10 21:17:37 -07002616 else
2617 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
Thomas Grafda5e0492006-08-10 21:17:37 -07002619 if (dev == NULL) {
2620 err = -ENODEV;
2621 goto errout;
2622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
Eric Dumazete0d087a2009-11-07 01:26:17 -08002624 err = validate_linkmsg(dev, tb);
2625 if (err < 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00002626 goto errout;
Thomas Grafda5e0492006-08-10 21:17:37 -07002627
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002628 err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
Thomas Grafda5e0492006-08-10 21:17:37 -07002629errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 return err;
2631}
2632
WANG Cong66400d52015-03-24 11:53:31 -07002633static int rtnl_group_dellink(const struct net *net, int group)
2634{
2635 struct net_device *dev, *aux;
2636 LIST_HEAD(list_kill);
2637 bool found = false;
2638
2639 if (!group)
2640 return -EPERM;
2641
2642 for_each_netdev(net, dev) {
2643 if (dev->group == group) {
2644 const struct rtnl_link_ops *ops;
2645
2646 found = true;
2647 ops = dev->rtnl_link_ops;
2648 if (!ops || !ops->dellink)
2649 return -EOPNOTSUPP;
2650 }
2651 }
2652
2653 if (!found)
2654 return -ENODEV;
2655
2656 for_each_netdev_safe(net, dev, aux) {
2657 if (dev->group == group) {
2658 const struct rtnl_link_ops *ops;
2659
2660 ops = dev->rtnl_link_ops;
2661 ops->dellink(dev, &list_kill);
2662 }
2663 }
2664 unregister_netdevice_many(&list_kill);
2665
2666 return 0;
2667}
2668
Thomas Graf614732e2015-07-21 10:44:06 +02002669int rtnl_delete_link(struct net_device *dev)
2670{
2671 const struct rtnl_link_ops *ops;
2672 LIST_HEAD(list_kill);
2673
2674 ops = dev->rtnl_link_ops;
2675 if (!ops || !ops->dellink)
2676 return -EOPNOTSUPP;
2677
2678 ops->dellink(dev, &list_kill);
2679 unregister_netdevice_many(&list_kill);
2680
2681 return 0;
2682}
2683EXPORT_SYMBOL_GPL(rtnl_delete_link);
2684
David Ahernc21ef3e2017-04-16 09:48:24 -07002685static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
2686 struct netlink_ext_ack *extack)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002687{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002688 struct net *net = sock_net(skb->sk);
Christian Braunerb61ad682018-01-24 15:26:34 +01002689 struct net *tgt_net = net;
2690 struct net_device *dev = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002691 struct ifinfomsg *ifm;
2692 char ifname[IFNAMSIZ];
2693 struct nlattr *tb[IFLA_MAX+1];
2694 int err;
Christian Braunerb61ad682018-01-24 15:26:34 +01002695 int netnsid = -1;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002696
David Ahernc21ef3e2017-04-16 09:48:24 -07002697 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002698 if (err < 0)
2699 return err;
2700
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002701 err = rtnl_ensure_unique_netns(tb, extack, true);
2702 if (err < 0)
2703 return err;
2704
Patrick McHardy38f7b872007-06-13 12:03:51 -07002705 if (tb[IFLA_IFNAME])
2706 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2707
Christian Braunerb61ad682018-01-24 15:26:34 +01002708 if (tb[IFLA_IF_NETNSID]) {
2709 netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]);
2710 tgt_net = get_target_net(NETLINK_CB(skb).sk, netnsid);
2711 if (IS_ERR(tgt_net))
2712 return PTR_ERR(tgt_net);
2713 }
2714
2715 err = -EINVAL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002716 ifm = nlmsg_data(nlh);
2717 if (ifm->ifi_index > 0)
Christian Braunerb61ad682018-01-24 15:26:34 +01002718 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002719 else if (tb[IFLA_IFNAME])
Christian Braunerb61ad682018-01-24 15:26:34 +01002720 dev = __dev_get_by_name(tgt_net, ifname);
WANG Cong66400d52015-03-24 11:53:31 -07002721 else if (tb[IFLA_GROUP])
Christian Braunerb61ad682018-01-24 15:26:34 +01002722 err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP]));
Patrick McHardy38f7b872007-06-13 12:03:51 -07002723 else
Christian Braunerb61ad682018-01-24 15:26:34 +01002724 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002725
Christian Braunerb61ad682018-01-24 15:26:34 +01002726 if (!dev) {
2727 if (tb[IFLA_IFNAME] || ifm->ifi_index > 0)
2728 err = -ENODEV;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002729
Christian Braunerb61ad682018-01-24 15:26:34 +01002730 goto out;
2731 }
2732
2733 err = rtnl_delete_link(dev);
2734
2735out:
2736 if (netnsid >= 0)
2737 put_net(tgt_net);
2738
2739 return err;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002740}
2741
Patrick McHardy3729d502010-02-26 06:34:54 +00002742int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
2743{
2744 unsigned int old_flags;
2745 int err;
2746
2747 old_flags = dev->flags;
2748 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
2749 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
2750 if (err < 0)
2751 return err;
2752 }
2753
2754 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
Patrick McHardy3729d502010-02-26 06:34:54 +00002755
Nicolas Dichtela528c212013-09-25 12:02:44 +02002756 __dev_notify_flags(dev, old_flags, ~0U);
Patrick McHardy3729d502010-02-26 06:34:54 +00002757 return 0;
2758}
2759EXPORT_SYMBOL(rtnl_configure_link);
2760
Rami Rosenc0713562012-11-30 01:08:47 +00002761struct net_device *rtnl_create_link(struct net *net,
Thomas Graf78ebb0d2015-04-10 01:45:53 +02002762 const char *ifname, unsigned char name_assign_type,
Tom Gundersen55177502014-07-14 16:37:25 +02002763 const struct rtnl_link_ops *ops, struct nlattr *tb[])
Pavel Emelianove7199282007-08-08 22:16:38 -07002764{
Pavel Emelianove7199282007-08-08 22:16:38 -07002765 struct net_device *dev;
Jiri Pirkod40156a2012-07-20 02:28:47 +00002766 unsigned int num_tx_queues = 1;
2767 unsigned int num_rx_queues = 1;
Pavel Emelianove7199282007-08-08 22:16:38 -07002768
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002769 if (tb[IFLA_NUM_TX_QUEUES])
2770 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
2771 else if (ops->get_num_tx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002772 num_tx_queues = ops->get_num_tx_queues();
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002773
2774 if (tb[IFLA_NUM_RX_QUEUES])
2775 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
2776 else if (ops->get_num_rx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002777 num_rx_queues = ops->get_num_rx_queues();
stephen hemmingerefacb302012-04-10 18:34:43 +00002778
Tom Gundersen55177502014-07-14 16:37:25 +02002779 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
Tom Gundersenc835a672014-07-14 16:37:24 +02002780 ops->setup, num_tx_queues, num_rx_queues);
Pavel Emelianove7199282007-08-08 22:16:38 -07002781 if (!dev)
Tobias Klauserd1892e42017-02-20 16:32:06 +01002782 return ERR_PTR(-ENOMEM);
Pavel Emelianove7199282007-08-08 22:16:38 -07002783
Eric W. Biederman81adee42009-11-08 00:53:51 -08002784 dev_net_set(dev, net);
2785 dev->rtnl_link_ops = ops;
Patrick McHardy3729d502010-02-26 06:34:54 +00002786 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
Eric W. Biederman81adee42009-11-08 00:53:51 -08002787
Pavel Emelianove7199282007-08-08 22:16:38 -07002788 if (tb[IFLA_MTU])
2789 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002790 if (tb[IFLA_ADDRESS]) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002791 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
2792 nla_len(tb[IFLA_ADDRESS]));
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002793 dev->addr_assign_type = NET_ADDR_SET;
2794 }
Pavel Emelianove7199282007-08-08 22:16:38 -07002795 if (tb[IFLA_BROADCAST])
2796 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
2797 nla_len(tb[IFLA_BROADCAST]));
2798 if (tb[IFLA_TXQLEN])
2799 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
2800 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002801 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Pavel Emelianove7199282007-08-08 22:16:38 -07002802 if (tb[IFLA_LINKMODE])
2803 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
Patrick McHardyffa934f2011-01-20 03:00:42 +00002804 if (tb[IFLA_GROUP])
2805 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Stephen Hemminger46e6b992017-12-07 15:40:19 -08002806 if (tb[IFLA_GSO_MAX_SIZE])
2807 netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE]));
2808 if (tb[IFLA_GSO_MAX_SEGS])
Stephen Hemmingera0b586f2017-12-08 15:34:13 -08002809 dev->gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
Pavel Emelianove7199282007-08-08 22:16:38 -07002810
2811 return dev;
Pavel Emelianove7199282007-08-08 22:16:38 -07002812}
Eric Dumazete0d087a2009-11-07 01:26:17 -08002813EXPORT_SYMBOL(rtnl_create_link);
Pavel Emelianove7199282007-08-08 22:16:38 -07002814
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002815static int rtnl_group_changelink(const struct sk_buff *skb,
2816 struct net *net, int group,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002817 struct ifinfomsg *ifm,
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002818 struct netlink_ext_ack *extack,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002819 struct nlattr **tb)
2820{
WANG Congd0795352015-03-23 16:31:09 -07002821 struct net_device *dev, *aux;
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002822 int err;
2823
WANG Congd0795352015-03-23 16:31:09 -07002824 for_each_netdev_safe(net, dev, aux) {
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002825 if (dev->group == group) {
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002826 err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002827 if (err < 0)
2828 return err;
2829 }
2830 }
2831
2832 return 0;
2833}
2834
David Ahernc21ef3e2017-04-16 09:48:24 -07002835static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2836 struct netlink_ext_ack *extack)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002837{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002838 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002839 const struct rtnl_link_ops *ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002840 const struct rtnl_link_ops *m_ops = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002841 struct net_device *dev;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002842 struct net_device *master_dev = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002843 struct ifinfomsg *ifm;
2844 char kind[MODULE_NAME_LEN];
2845 char ifname[IFNAMSIZ];
2846 struct nlattr *tb[IFLA_MAX+1];
2847 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
Tom Gundersen55177502014-07-14 16:37:25 +02002848 unsigned char name_assign_type = NET_NAME_USER;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002849 int err;
2850
Johannes Berg95a5afc2008-10-16 15:24:51 -07002851#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002852replay:
Thomas Graf8072f082007-07-31 14:13:50 -07002853#endif
David Ahernc21ef3e2017-04-16 09:48:24 -07002854 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002855 if (err < 0)
2856 return err;
2857
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002858 err = rtnl_ensure_unique_netns(tb, extack, false);
2859 if (err < 0)
2860 return err;
2861
Patrick McHardy38f7b872007-06-13 12:03:51 -07002862 if (tb[IFLA_IFNAME])
2863 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2864 else
2865 ifname[0] = '\0';
2866
2867 ifm = nlmsg_data(nlh);
2868 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07002869 dev = __dev_get_by_index(net, ifm->ifi_index);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002870 else {
2871 if (ifname[0])
2872 dev = __dev_get_by_name(net, ifname);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002873 else
2874 dev = NULL;
2875 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002876
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002877 if (dev) {
2878 master_dev = netdev_master_upper_dev_get(dev);
2879 if (master_dev)
2880 m_ops = master_dev->rtnl_link_ops;
2881 }
2882
Eric Dumazete0d087a2009-11-07 01:26:17 -08002883 err = validate_linkmsg(dev, tb);
2884 if (err < 0)
Thomas Graf1840bb12008-02-23 19:54:36 -08002885 return err;
2886
Patrick McHardy38f7b872007-06-13 12:03:51 -07002887 if (tb[IFLA_LINKINFO]) {
2888 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +02002889 tb[IFLA_LINKINFO], ifla_info_policy,
2890 NULL);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002891 if (err < 0)
2892 return err;
2893 } else
2894 memset(linkinfo, 0, sizeof(linkinfo));
2895
2896 if (linkinfo[IFLA_INFO_KIND]) {
2897 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
2898 ops = rtnl_link_ops_get(kind);
2899 } else {
2900 kind[0] = '\0';
2901 ops = NULL;
2902 }
2903
2904 if (1) {
Sasha Levin4e10fd52015-02-24 14:14:35 -05002905 struct nlattr *attr[ops ? ops->maxtype + 1 : 1];
2906 struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1];
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002907 struct nlattr **data = NULL;
2908 struct nlattr **slave_data = NULL;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002909 struct net *dest_net, *link_net = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002910
2911 if (ops) {
2912 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
2913 err = nla_parse_nested(attr, ops->maxtype,
2914 linkinfo[IFLA_INFO_DATA],
Johannes Bergfceb6432017-04-12 14:34:07 +02002915 ops->policy, NULL);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002916 if (err < 0)
2917 return err;
2918 data = attr;
2919 }
2920 if (ops->validate) {
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02002921 err = ops->validate(tb, data, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002922 if (err < 0)
2923 return err;
2924 }
2925 }
2926
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002927 if (m_ops) {
2928 if (m_ops->slave_maxtype &&
2929 linkinfo[IFLA_INFO_SLAVE_DATA]) {
2930 err = nla_parse_nested(slave_attr,
2931 m_ops->slave_maxtype,
2932 linkinfo[IFLA_INFO_SLAVE_DATA],
Johannes Bergfceb6432017-04-12 14:34:07 +02002933 m_ops->slave_policy,
2934 NULL);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002935 if (err < 0)
2936 return err;
2937 slave_data = slave_attr;
2938 }
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002939 }
2940
Patrick McHardy38f7b872007-06-13 12:03:51 -07002941 if (dev) {
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002942 int status = 0;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002943
2944 if (nlh->nlmsg_flags & NLM_F_EXCL)
2945 return -EEXIST;
2946 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2947 return -EOPNOTSUPP;
2948
2949 if (linkinfo[IFLA_INFO_DATA]) {
2950 if (!ops || ops != dev->rtnl_link_ops ||
2951 !ops->changelink)
2952 return -EOPNOTSUPP;
2953
Matthias Schifferad744b22017-06-25 23:56:00 +02002954 err = ops->changelink(dev, tb, data, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002955 if (err < 0)
2956 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002957 status |= DO_SETLINK_NOTIFY;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002958 }
2959
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002960 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2961 if (!m_ops || !m_ops->slave_changelink)
2962 return -EOPNOTSUPP;
2963
2964 err = m_ops->slave_changelink(master_dev, dev,
Matthias Schiffer17dd0ec2017-06-25 23:56:02 +02002965 tb, slave_data,
2966 extack);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002967 if (err < 0)
2968 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002969 status |= DO_SETLINK_NOTIFY;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002970 }
2971
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002972 return do_setlink(skb, dev, ifm, extack, tb, ifname,
2973 status);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002974 }
2975
Patrick McHardyffa934f2011-01-20 03:00:42 +00002976 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2977 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002978 return rtnl_group_changelink(skb, net,
Patrick McHardyffa934f2011-01-20 03:00:42 +00002979 nla_get_u32(tb[IFLA_GROUP]),
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002980 ifm, extack, tb);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002981 return -ENODEV;
Patrick McHardyffa934f2011-01-20 03:00:42 +00002982 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002983
Theuns Verwoerd160ca012017-01-31 12:23:46 +13002984 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
Patrick McHardy38f7b872007-06-13 12:03:51 -07002985 return -EOPNOTSUPP;
2986
2987 if (!ops) {
Johannes Berg95a5afc2008-10-16 15:24:51 -07002988#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002989 if (kind[0]) {
2990 __rtnl_unlock();
2991 request_module("rtnl-link-%s", kind);
2992 rtnl_lock();
2993 ops = rtnl_link_ops_get(kind);
2994 if (ops)
2995 goto replay;
2996 }
2997#endif
2998 return -EOPNOTSUPP;
2999 }
3000
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02003001 if (!ops->setup)
3002 return -EOPNOTSUPP;
3003
Tom Gundersen55177502014-07-14 16:37:25 +02003004 if (!ifname[0]) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07003005 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
Tom Gundersen55177502014-07-14 16:37:25 +02003006 name_assign_type = NET_NAME_ENUM;
3007 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07003008
Christian Brauner5bb8ed02018-01-29 18:07:20 +01003009 dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN);
Eric W. Biederman13ad1772011-01-29 14:57:22 +00003010 if (IS_ERR(dest_net))
3011 return PTR_ERR(dest_net);
3012
Nicolas Dichtel317f4812015-01-15 15:11:18 +01003013 if (tb[IFLA_LINK_NETNSID]) {
3014 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
3015
3016 link_net = get_net_ns_by_id(dest_net, id);
3017 if (!link_net) {
3018 err = -EINVAL;
3019 goto out;
3020 }
Eric W. Biederman06615be2015-02-26 16:20:07 -06003021 err = -EPERM;
3022 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
3023 goto out;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01003024 }
3025
3026 dev = rtnl_create_link(link_net ? : dest_net, ifname,
3027 name_assign_type, ops, tb);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00003028 if (IS_ERR(dev)) {
Pavel Emelianove7199282007-08-08 22:16:38 -07003029 err = PTR_ERR(dev);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00003030 goto out;
3031 }
3032
3033 dev->ifindex = ifm->ifi_index;
3034
Cong Wang0e0eee22014-02-11 15:51:30 -08003035 if (ops->newlink) {
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02003036 err = ops->newlink(link_net ? : net, dev, tb, data,
3037 extack);
Cong Wang0e0eee22014-02-11 15:51:30 -08003038 /* Drivers should call free_netdev() in ->destructor
Cong Wange51fb152014-06-03 16:40:47 -07003039 * and unregister it on failure after registration
3040 * so that device could be finally freed in rtnl_unlock.
Cong Wang0e0eee22014-02-11 15:51:30 -08003041 */
Cong Wange51fb152014-06-03 16:40:47 -07003042 if (err < 0) {
3043 /* If device is not registered at all, free it now */
3044 if (dev->reg_state == NETREG_UNINITIALIZED)
3045 free_netdev(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08003046 goto out;
Cong Wange51fb152014-06-03 16:40:47 -07003047 }
Cong Wang0e0eee22014-02-11 15:51:30 -08003048 } else {
Patrick McHardy2d85cba2007-07-11 19:42:13 -07003049 err = register_netdevice(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08003050 if (err < 0) {
3051 free_netdev(dev);
3052 goto out;
3053 }
Dan Carpenterfce9b9b2013-08-14 12:35:42 +03003054 }
Patrick McHardy3729d502010-02-26 06:34:54 +00003055 err = rtnl_configure_link(dev, ifm);
David S. Miller43638902015-03-10 21:58:32 -04003056 if (err < 0)
3057 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01003058 if (link_net) {
Nicolas Dichtel317f4812015-01-15 15:11:18 +01003059 err = dev_change_net_namespace(dev, dest_net, ifname);
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01003060 if (err < 0)
David S. Miller43638902015-03-10 21:58:32 -04003061 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01003062 }
Theuns Verwoerd160ca012017-01-31 12:23:46 +13003063 if (tb[IFLA_MASTER]) {
David Ahern33eaf2a2017-10-04 17:48:46 -07003064 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]),
3065 extack);
Theuns Verwoerd160ca012017-01-31 12:23:46 +13003066 if (err)
3067 goto out_unregister;
3068 }
Patrick McHardy3729d502010-02-26 06:34:54 +00003069out:
Nicolas Dichtel317f4812015-01-15 15:11:18 +01003070 if (link_net)
3071 put_net(link_net);
Eric W. Biederman81adee42009-11-08 00:53:51 -08003072 put_net(dest_net);
Patrick McHardy38f7b872007-06-13 12:03:51 -07003073 return err;
David S. Miller43638902015-03-10 21:58:32 -04003074out_unregister:
3075 if (ops->newlink) {
3076 LIST_HEAD(list_kill);
3077
3078 ops->dellink(dev, &list_kill);
3079 unregister_netdevice_many(&list_kill);
3080 } else {
3081 unregister_netdevice(dev);
3082 }
3083 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003084 }
3085}
3086
David Ahernc21ef3e2017-04-16 09:48:24 -07003087static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3088 struct netlink_ext_ack *extack)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003089{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003090 struct net *net = sock_net(skb->sk);
Jiri Benc79e1ad12017-11-02 17:04:38 -02003091 struct net *tgt_net = net;
Thomas Grafb60c5112006-08-04 23:05:34 -07003092 struct ifinfomsg *ifm;
Eric Dumazeta3d12892009-10-21 10:59:31 +00003093 char ifname[IFNAMSIZ];
Thomas Grafb60c5112006-08-04 23:05:34 -07003094 struct nlattr *tb[IFLA_MAX+1];
3095 struct net_device *dev = NULL;
3096 struct sk_buff *nskb;
Jiri Benc79e1ad12017-11-02 17:04:38 -02003097 int netnsid = -1;
Thomas Graf339bf982006-11-10 14:10:15 -08003098 int err;
Greg Rose115c9b82012-02-21 16:54:48 -05003099 u32 ext_filter_mask = 0;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003100
David Ahernc21ef3e2017-04-16 09:48:24 -07003101 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Thomas Grafb60c5112006-08-04 23:05:34 -07003102 if (err < 0)
Eric Sesterhenn9918f232006-09-26 23:26:38 -07003103 return err;
Thomas Grafb60c5112006-08-04 23:05:34 -07003104
Christian Brauner4ff66ca2018-02-07 13:53:20 +01003105 err = rtnl_ensure_unique_netns(tb, extack, true);
3106 if (err < 0)
3107 return err;
3108
Jiri Benc79e1ad12017-11-02 17:04:38 -02003109 if (tb[IFLA_IF_NETNSID]) {
3110 netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]);
Andrei Vaginf428fe42018-01-02 23:27:33 -08003111 tgt_net = get_target_net(NETLINK_CB(skb).sk, netnsid);
Jiri Benc79e1ad12017-11-02 17:04:38 -02003112 if (IS_ERR(tgt_net))
3113 return PTR_ERR(tgt_net);
3114 }
3115
Eric Dumazeta3d12892009-10-21 10:59:31 +00003116 if (tb[IFLA_IFNAME])
3117 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
3118
Greg Rose115c9b82012-02-21 16:54:48 -05003119 if (tb[IFLA_EXT_MASK])
3120 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3121
Jiri Benc79e1ad12017-11-02 17:04:38 -02003122 err = -EINVAL;
Thomas Grafb60c5112006-08-04 23:05:34 -07003123 ifm = nlmsg_data(nlh);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003124 if (ifm->ifi_index > 0)
Jiri Benc79e1ad12017-11-02 17:04:38 -02003125 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003126 else if (tb[IFLA_IFNAME])
Jiri Benc79e1ad12017-11-02 17:04:38 -02003127 dev = __dev_get_by_name(tgt_net, ifname);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003128 else
Jiri Benc79e1ad12017-11-02 17:04:38 -02003129 goto out;
Thomas Grafb60c5112006-08-04 23:05:34 -07003130
Jiri Benc79e1ad12017-11-02 17:04:38 -02003131 err = -ENODEV;
Eric Dumazeta3d12892009-10-21 10:59:31 +00003132 if (dev == NULL)
Jiri Benc79e1ad12017-11-02 17:04:38 -02003133 goto out;
Eric Dumazeta3d12892009-10-21 10:59:31 +00003134
Jiri Benc79e1ad12017-11-02 17:04:38 -02003135 err = -ENOBUFS;
Greg Rose115c9b82012-02-21 16:54:48 -05003136 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003137 if (nskb == NULL)
Jiri Benc79e1ad12017-11-02 17:04:38 -02003138 goto out;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003139
Jiri Benc79e1ad12017-11-02 17:04:38 -02003140 err = rtnl_fill_ifinfo(nskb, dev, net,
3141 RTM_NEWLINK, NETLINK_CB(skb).portid,
3142 nlh->nlmsg_seq, 0, 0, ext_filter_mask,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003143 0, NULL, 0, netnsid);
Patrick McHardy26932562007-01-31 23:16:40 -08003144 if (err < 0) {
3145 /* -EMSGSIZE implies BUG in if_nlmsg_size */
3146 WARN_ON(err == -EMSGSIZE);
3147 kfree_skb(nskb);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003148 } else
Eric W. Biederman15e47302012-09-07 20:12:54 +00003149 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
Jiri Benc79e1ad12017-11-02 17:04:38 -02003150out:
3151 if (netnsid >= 0)
3152 put_net(tgt_net);
Thomas Grafb60c5112006-08-04 23:05:34 -07003153
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003154 return err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003155}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003156
Greg Rose115c9b82012-02-21 16:54:48 -05003157static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
Greg Rosec7ac8672011-06-10 01:27:09 +00003158{
Greg Rose115c9b82012-02-21 16:54:48 -05003159 struct net *net = sock_net(skb->sk);
3160 struct net_device *dev;
3161 struct nlattr *tb[IFLA_MAX+1];
3162 u32 ext_filter_mask = 0;
3163 u16 min_ifinfo_dump_size = 0;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02003164 int hdrlen;
Greg Rose115c9b82012-02-21 16:54:48 -05003165
Michal Schmidte5eca6d2014-05-28 14:15:19 +02003166 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3167 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
3168 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
3169
Johannes Bergfceb6432017-04-12 14:34:07 +02003170 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00003171 if (tb[IFLA_EXT_MASK])
3172 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3173 }
Greg Rose115c9b82012-02-21 16:54:48 -05003174
3175 if (!ext_filter_mask)
3176 return NLMSG_GOODSIZE;
3177 /*
3178 * traverse the list of net devices and compute the minimum
3179 * buffer size based upon the filter mask.
3180 */
Florian Westphal6853dd42017-08-09 20:41:51 +02003181 rcu_read_lock();
3182 for_each_netdev_rcu(net, dev) {
Greg Rose115c9b82012-02-21 16:54:48 -05003183 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
3184 if_nlmsg_size(dev,
3185 ext_filter_mask));
3186 }
Florian Westphal6853dd42017-08-09 20:41:51 +02003187 rcu_read_unlock();
Greg Rose115c9b82012-02-21 16:54:48 -05003188
Zhang Shengju93af2052016-11-22 14:14:28 +08003189 return nlmsg_total_size(min_ifinfo_dump_size);
Greg Rosec7ac8672011-06-10 01:27:09 +00003190}
3191
Adrian Bunk42bad1d2007-04-26 00:57:41 -07003192static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193{
3194 int idx;
3195 int s_idx = cb->family;
3196
3197 if (s_idx == 0)
3198 s_idx = 1;
Florian Westphal6853dd42017-08-09 20:41:51 +02003199
Patrick McHardy25239ce2010-04-26 16:02:05 +02003200 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
Florian Westphaladdf9b92017-12-02 21:44:05 +01003201 struct rtnl_link **tab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 int type = cb->nlh->nlmsg_type-RTM_BASE;
Florian Westphaladdf9b92017-12-02 21:44:05 +01003203 struct rtnl_link *link;
Florian Westphal6853dd42017-08-09 20:41:51 +02003204 rtnl_dumpit_func dumpit;
3205
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 if (idx < s_idx || idx == PF_PACKET)
3207 continue;
Florian Westphal6853dd42017-08-09 20:41:51 +02003208
Florian Westphaladdf9b92017-12-02 21:44:05 +01003209 if (type < 0 || type >= RTM_NR_MSGTYPES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 continue;
Florian Westphal6853dd42017-08-09 20:41:51 +02003211
Florian Westphaladdf9b92017-12-02 21:44:05 +01003212 tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]);
3213 if (!tab)
3214 continue;
3215
3216 link = tab[type];
3217 if (!link)
3218 continue;
3219
3220 dumpit = link->dumpit;
Florian Westphal6853dd42017-08-09 20:41:51 +02003221 if (!dumpit)
3222 continue;
3223
Nicolas Dichtel04652772013-03-22 06:28:42 +00003224 if (idx > s_idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 memset(&cb->args[0], 0, sizeof(cb->args));
Nicolas Dichtel04652772013-03-22 06:28:42 +00003226 cb->prev_seq = 0;
3227 cb->seq = 0;
3228 }
Florian Westphal6853dd42017-08-09 20:41:51 +02003229 if (dumpit(skb, cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 break;
3231 }
3232 cb->family = idx;
3233
3234 return skb->len;
3235}
3236
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003237struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003238 unsigned int change,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003239 u32 event, gfp_t flags, int *new_nsid,
3240 int new_ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003242 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 struct sk_buff *skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003244 int err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00003245 size_t if_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246
Alexei Starovoitov7f294052013-10-23 16:02:42 -07003247 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003248 if (skb == NULL)
3249 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250
Jiri Benc79e1ad12017-11-02 17:04:38 -02003251 err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
3252 type, 0, 0, change, 0, 0, event,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003253 new_nsid, new_ifindex, -1);
Patrick McHardy26932562007-01-31 23:16:40 -08003254 if (err < 0) {
3255 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
3256 WARN_ON(err == -EMSGSIZE);
3257 kfree_skb(skb);
3258 goto errout;
3259 }
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003260 return skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003261errout:
3262 if (err < 0)
Eric W. Biederman4b3da702007-11-19 22:27:40 -08003263 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003264 return NULL;
3265}
3266
3267void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
3268{
3269 struct net *net = dev_net(dev);
3270
3271 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
3272}
3273
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003274static void rtmsg_ifinfo_event(int type, struct net_device *dev,
3275 unsigned int change, u32 event,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003276 gfp_t flags, int *new_nsid, int new_ifindex)
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003277{
3278 struct sk_buff *skb;
3279
Nicolas Dichteled2a80a2015-05-13 14:19:42 +02003280 if (dev->reg_state != NETREG_REGISTERED)
3281 return;
3282
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003283 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid,
3284 new_ifindex);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003285 if (skb)
3286 rtmsg_ifinfo_send(skb, dev, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287}
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003288
3289void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
3290 gfp_t flags)
3291{
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003292 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3293 NULL, 0);
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003294}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003296void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003297 gfp_t flags, int *new_nsid, int new_ifindex)
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003298{
3299 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003300 new_nsid, new_ifindex);
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003301}
3302
John Fastabendd83b0602012-04-15 06:44:08 +00003303static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
3304 struct net_device *dev,
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003305 u8 *addr, u16 vid, u32 pid, u32 seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003306 int type, unsigned int flags,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003307 int nlflags, u16 ndm_state)
John Fastabendd83b0602012-04-15 06:44:08 +00003308{
3309 struct nlmsghdr *nlh;
3310 struct ndmsg *ndm;
3311
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003312 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
John Fastabendd83b0602012-04-15 06:44:08 +00003313 if (!nlh)
3314 return -EMSGSIZE;
3315
3316 ndm = nlmsg_data(nlh);
3317 ndm->ndm_family = AF_BRIDGE;
3318 ndm->ndm_pad1 = 0;
3319 ndm->ndm_pad2 = 0;
3320 ndm->ndm_flags = flags;
3321 ndm->ndm_type = 0;
3322 ndm->ndm_ifindex = dev->ifindex;
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003323 ndm->ndm_state = ndm_state;
John Fastabendd83b0602012-04-15 06:44:08 +00003324
3325 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
3326 goto nla_put_failure;
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003327 if (vid)
3328 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
3329 goto nla_put_failure;
John Fastabendd83b0602012-04-15 06:44:08 +00003330
Johannes Berg053c0952015-01-16 22:09:00 +01003331 nlmsg_end(skb, nlh);
3332 return 0;
John Fastabendd83b0602012-04-15 06:44:08 +00003333
3334nla_put_failure:
3335 nlmsg_cancel(skb, nlh);
3336 return -EMSGSIZE;
3337}
3338
John Fastabend3ff661c2012-04-15 06:44:14 +00003339static inline size_t rtnl_fdb_nlmsg_size(void)
3340{
Sabrina Dubrocaf82ef3e2016-11-18 15:50:39 +01003341 return NLMSG_ALIGN(sizeof(struct ndmsg)) +
3342 nla_total_size(ETH_ALEN) + /* NDA_LLADDR */
3343 nla_total_size(sizeof(u16)) + /* NDA_VLAN */
3344 0;
John Fastabend3ff661c2012-04-15 06:44:14 +00003345}
3346
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003347static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
3348 u16 ndm_state)
John Fastabend3ff661c2012-04-15 06:44:14 +00003349{
3350 struct net *net = dev_net(dev);
3351 struct sk_buff *skb;
3352 int err = -ENOBUFS;
3353
3354 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
3355 if (!skb)
3356 goto errout;
3357
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003358 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003359 0, 0, type, NTF_SELF, 0, ndm_state);
John Fastabend3ff661c2012-04-15 06:44:14 +00003360 if (err < 0) {
3361 kfree_skb(skb);
3362 goto errout;
3363 }
3364
3365 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
3366 return;
3367errout:
3368 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
3369}
3370
Vlad Yasevich090096b2013-03-06 15:39:42 +00003371/**
3372 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3373 */
3374int ndo_dflt_fdb_add(struct ndmsg *ndm,
3375 struct nlattr *tb[],
3376 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003377 const unsigned char *addr, u16 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003378 u16 flags)
3379{
3380 int err = -EINVAL;
3381
3382 /* If aging addresses are supported device will need to
3383 * implement its own handler for this.
3384 */
3385 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
3386 pr_info("%s: FDB only supports static addresses\n", dev->name);
3387 return err;
3388 }
3389
Or Gerlitz65891fe2014-12-14 18:19:05 +02003390 if (vid) {
3391 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
3392 return err;
3393 }
3394
Vlad Yasevich090096b2013-03-06 15:39:42 +00003395 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3396 err = dev_uc_add_excl(dev, addr);
3397 else if (is_multicast_ether_addr(addr))
3398 err = dev_mc_add_excl(dev, addr);
3399
3400 /* Only return duplicate errors if NLM_F_EXCL is set */
3401 if (err == -EEXIST && !(flags & NLM_F_EXCL))
3402 err = 0;
3403
3404 return err;
3405}
3406EXPORT_SYMBOL(ndo_dflt_fdb_add);
3407
Florian Westphalb88d12e2017-10-10 17:10:04 +02003408static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
3409 struct netlink_ext_ack *extack)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003410{
3411 u16 vid = 0;
3412
3413 if (vlan_attr) {
3414 if (nla_len(vlan_attr) != sizeof(u16)) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003415 NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
Jiri Pirkof6f64242014-11-28 14:34:15 +01003416 return -EINVAL;
3417 }
3418
3419 vid = nla_get_u16(vlan_attr);
3420
3421 if (!vid || vid >= VLAN_VID_MASK) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003422 NL_SET_ERR_MSG(extack, "invalid vlan id");
Jiri Pirkof6f64242014-11-28 14:34:15 +01003423 return -EINVAL;
3424 }
3425 }
3426 *p_vid = vid;
3427 return 0;
3428}
3429
David Ahernc21ef3e2017-04-16 09:48:24 -07003430static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
3431 struct netlink_ext_ack *extack)
John Fastabend77162022012-04-15 06:43:56 +00003432{
3433 struct net *net = sock_net(skb->sk);
John Fastabend77162022012-04-15 06:43:56 +00003434 struct ndmsg *ndm;
3435 struct nlattr *tb[NDA_MAX+1];
3436 struct net_device *dev;
3437 u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01003438 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00003439 int err;
3440
David Ahernc21ef3e2017-04-16 09:48:24 -07003441 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
John Fastabend77162022012-04-15 06:43:56 +00003442 if (err < 0)
3443 return err;
3444
3445 ndm = nlmsg_data(nlh);
3446 if (ndm->ndm_ifindex == 0) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003447 NL_SET_ERR_MSG(extack, "invalid ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003448 return -EINVAL;
3449 }
3450
3451 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3452 if (dev == NULL) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003453 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003454 return -ENODEV;
3455 }
3456
3457 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003458 NL_SET_ERR_MSG(extack, "invalid address");
John Fastabend77162022012-04-15 06:43:56 +00003459 return -EINVAL;
3460 }
3461
3462 addr = nla_data(tb[NDA_LLADDR]);
John Fastabend77162022012-04-15 06:43:56 +00003463
Florian Westphalb88d12e2017-10-10 17:10:04 +02003464 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
Jiri Pirkof6f64242014-11-28 14:34:15 +01003465 if (err)
3466 return err;
3467
John Fastabend77162022012-04-15 06:43:56 +00003468 err = -EOPNOTSUPP;
3469
3470 /* Support fdb on master device the net/bridge default case */
3471 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3472 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003473 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3474 const struct net_device_ops *ops = br_dev->netdev_ops;
3475
Jiri Pirkof6f64242014-11-28 14:34:15 +01003476 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
3477 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00003478 if (err)
3479 goto out;
3480 else
3481 ndm->ndm_flags &= ~NTF_MASTER;
3482 }
3483
3484 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00003485 if ((ndm->ndm_flags & NTF_SELF)) {
3486 if (dev->netdev_ops->ndo_fdb_add)
3487 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003488 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003489 nlh->nlmsg_flags);
3490 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01003491 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003492 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00003493
John Fastabend3ff661c2012-04-15 06:44:14 +00003494 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003495 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
3496 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00003497 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00003498 }
John Fastabend77162022012-04-15 06:43:56 +00003499 }
3500out:
3501 return err;
3502}
3503
Vlad Yasevich090096b2013-03-06 15:39:42 +00003504/**
3505 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3506 */
3507int ndo_dflt_fdb_del(struct ndmsg *ndm,
3508 struct nlattr *tb[],
3509 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003510 const unsigned char *addr, u16 vid)
Vlad Yasevich090096b2013-03-06 15:39:42 +00003511{
Alexander Duyckc8a89c42014-07-15 15:15:20 -07003512 int err = -EINVAL;
Vlad Yasevich090096b2013-03-06 15:39:42 +00003513
3514 /* If aging addresses are supported device will need to
3515 * implement its own handler for this.
3516 */
Sridhar Samudrala64535992013-08-08 15:19:48 -07003517 if (!(ndm->ndm_state & NUD_PERMANENT)) {
Vlad Yasevich090096b2013-03-06 15:39:42 +00003518 pr_info("%s: FDB only supports static addresses\n", dev->name);
Alexander Duyckc8a89c42014-07-15 15:15:20 -07003519 return err;
Vlad Yasevich090096b2013-03-06 15:39:42 +00003520 }
3521
3522 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3523 err = dev_uc_del(dev, addr);
3524 else if (is_multicast_ether_addr(addr))
3525 err = dev_mc_del(dev, addr);
Vlad Yasevich090096b2013-03-06 15:39:42 +00003526
3527 return err;
3528}
3529EXPORT_SYMBOL(ndo_dflt_fdb_del);
3530
David Ahernc21ef3e2017-04-16 09:48:24 -07003531static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
3532 struct netlink_ext_ack *extack)
John Fastabend77162022012-04-15 06:43:56 +00003533{
3534 struct net *net = sock_net(skb->sk);
3535 struct ndmsg *ndm;
Vlad Yasevich1690be62013-02-13 12:00:18 +00003536 struct nlattr *tb[NDA_MAX+1];
John Fastabend77162022012-04-15 06:43:56 +00003537 struct net_device *dev;
3538 int err = -EINVAL;
3539 __u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01003540 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00003541
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07003542 if (!netlink_capable(skb, CAP_NET_ADMIN))
Vlad Yasevich1690be62013-02-13 12:00:18 +00003543 return -EPERM;
3544
David Ahernc21ef3e2017-04-16 09:48:24 -07003545 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
Vlad Yasevich1690be62013-02-13 12:00:18 +00003546 if (err < 0)
3547 return err;
John Fastabend77162022012-04-15 06:43:56 +00003548
3549 ndm = nlmsg_data(nlh);
3550 if (ndm->ndm_ifindex == 0) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003551 NL_SET_ERR_MSG(extack, "invalid ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003552 return -EINVAL;
3553 }
3554
3555 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3556 if (dev == NULL) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003557 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003558 return -ENODEV;
3559 }
3560
Vlad Yasevich1690be62013-02-13 12:00:18 +00003561 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003562 NL_SET_ERR_MSG(extack, "invalid address");
John Fastabend77162022012-04-15 06:43:56 +00003563 return -EINVAL;
3564 }
3565
Vlad Yasevich1690be62013-02-13 12:00:18 +00003566 addr = nla_data(tb[NDA_LLADDR]);
Vlad Yasevich1690be62013-02-13 12:00:18 +00003567
Florian Westphalb88d12e2017-10-10 17:10:04 +02003568 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
Jiri Pirkof6f64242014-11-28 14:34:15 +01003569 if (err)
3570 return err;
3571
John Fastabend77162022012-04-15 06:43:56 +00003572 err = -EOPNOTSUPP;
3573
3574 /* Support fdb on master device the net/bridge default case */
3575 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3576 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003577 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3578 const struct net_device_ops *ops = br_dev->netdev_ops;
John Fastabend77162022012-04-15 06:43:56 +00003579
Jiri Pirko898e5062013-01-03 22:48:52 +00003580 if (ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003581 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00003582
3583 if (err)
3584 goto out;
3585 else
3586 ndm->ndm_flags &= ~NTF_MASTER;
3587 }
3588
3589 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00003590 if (ndm->ndm_flags & NTF_SELF) {
3591 if (dev->netdev_ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003592 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
3593 vid);
Vlad Yasevich090096b2013-03-06 15:39:42 +00003594 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01003595 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00003596
John Fastabend3ff661c2012-04-15 06:44:14 +00003597 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003598 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
3599 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00003600 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00003601 }
John Fastabend77162022012-04-15 06:43:56 +00003602 }
3603out:
3604 return err;
3605}
3606
John Fastabendd83b0602012-04-15 06:44:08 +00003607static int nlmsg_populate_fdb(struct sk_buff *skb,
3608 struct netlink_callback *cb,
3609 struct net_device *dev,
3610 int *idx,
3611 struct netdev_hw_addr_list *list)
3612{
3613 struct netdev_hw_addr *ha;
3614 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00003615 u32 portid, seq;
John Fastabendd83b0602012-04-15 06:44:08 +00003616
Eric W. Biederman15e47302012-09-07 20:12:54 +00003617 portid = NETLINK_CB(cb->skb).portid;
John Fastabendd83b0602012-04-15 06:44:08 +00003618 seq = cb->nlh->nlmsg_seq;
3619
3620 list_for_each_entry(ha, &list->list, list) {
Roopa Prabhud2976532016-08-30 21:56:45 -07003621 if (*idx < cb->args[2])
John Fastabendd83b0602012-04-15 06:44:08 +00003622 goto skip;
3623
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003624 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
John Fastabenda7a558f2012-11-01 16:23:10 +00003625 portid, seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003626 RTM_NEWNEIGH, NTF_SELF,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003627 NLM_F_MULTI, NUD_PERMANENT);
John Fastabendd83b0602012-04-15 06:44:08 +00003628 if (err < 0)
3629 return err;
3630skip:
3631 *idx += 1;
3632 }
3633 return 0;
3634}
3635
3636/**
Ben Hutchings2c530402012-07-10 10:55:09 +00003637 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
John Fastabendd83b0602012-04-15 06:44:08 +00003638 * @nlh: netlink message header
3639 * @dev: netdevice
3640 *
3641 * Default netdevice operation to dump the existing unicast address list.
John Fastabend91f3e7b2013-03-29 08:18:37 +00003642 * Returns number of addresses from list put in skb.
John Fastabendd83b0602012-04-15 06:44:08 +00003643 */
3644int ndo_dflt_fdb_dump(struct sk_buff *skb,
3645 struct netlink_callback *cb,
3646 struct net_device *dev,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04003647 struct net_device *filter_dev,
Roopa Prabhud2976532016-08-30 21:56:45 -07003648 int *idx)
John Fastabendd83b0602012-04-15 06:44:08 +00003649{
3650 int err;
3651
3652 netif_addr_lock_bh(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -07003653 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
John Fastabendd83b0602012-04-15 06:44:08 +00003654 if (err)
3655 goto out;
Zhang Shengju2934c9d2016-11-30 16:37:34 +08003656 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
John Fastabendd83b0602012-04-15 06:44:08 +00003657out:
3658 netif_addr_unlock_bh(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -07003659 return err;
John Fastabendd83b0602012-04-15 06:44:08 +00003660}
3661EXPORT_SYMBOL(ndo_dflt_fdb_dump);
3662
John Fastabend77162022012-04-15 06:43:56 +00003663static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
3664{
John Fastabend77162022012-04-15 06:43:56 +00003665 struct net_device *dev;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003666 struct nlattr *tb[IFLA_MAX+1];
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003667 struct net_device *br_dev = NULL;
3668 const struct net_device_ops *ops = NULL;
3669 const struct net_device_ops *cops = NULL;
3670 struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
3671 struct net *net = sock_net(skb->sk);
Roopa Prabhud2976532016-08-30 21:56:45 -07003672 struct hlist_head *head;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003673 int brport_idx = 0;
3674 int br_idx = 0;
Roopa Prabhud2976532016-08-30 21:56:45 -07003675 int h, s_h;
3676 int idx = 0, s_idx;
3677 int err = 0;
3678 int fidx = 0;
John Fastabend77162022012-04-15 06:43:56 +00003679
Alexander Potapenko0ff50e82017-05-23 13:20:28 +02003680 err = nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb,
3681 IFLA_MAX, ifla_policy, NULL);
3682 if (err < 0) {
3683 return -EINVAL;
3684 } else if (err == 0) {
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003685 if (tb[IFLA_MASTER])
3686 br_idx = nla_get_u32(tb[IFLA_MASTER]);
3687 }
John Fastabend77162022012-04-15 06:43:56 +00003688
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003689 brport_idx = ifm->ifi_index;
3690
3691 if (br_idx) {
3692 br_dev = __dev_get_by_index(net, br_idx);
3693 if (!br_dev)
3694 return -ENODEV;
3695
3696 ops = br_dev->netdev_ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003697 }
3698
Roopa Prabhud2976532016-08-30 21:56:45 -07003699 s_h = cb->args[0];
3700 s_idx = cb->args[1];
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003701
Roopa Prabhud2976532016-08-30 21:56:45 -07003702 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
3703 idx = 0;
3704 head = &net->dev_index_head[h];
3705 hlist_for_each_entry(dev, head, index_hlist) {
3706
3707 if (brport_idx && (dev->ifindex != brport_idx))
3708 continue;
3709
3710 if (!br_idx) { /* user did not specify a specific bridge */
3711 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3712 br_dev = netdev_master_upper_dev_get(dev);
3713 cops = br_dev->netdev_ops;
3714 }
3715 } else {
3716 if (dev != br_dev &&
3717 !(dev->priv_flags & IFF_BRIDGE_PORT))
3718 continue;
3719
3720 if (br_dev != netdev_master_upper_dev_get(dev) &&
3721 !(dev->priv_flags & IFF_EBRIDGE))
3722 continue;
3723 cops = ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003724 }
3725
Roopa Prabhud2976532016-08-30 21:56:45 -07003726 if (idx < s_idx)
3727 goto cont;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003728
Roopa Prabhud2976532016-08-30 21:56:45 -07003729 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3730 if (cops && cops->ndo_fdb_dump) {
3731 err = cops->ndo_fdb_dump(skb, cb,
3732 br_dev, dev,
3733 &fidx);
3734 if (err == -EMSGSIZE)
3735 goto out;
3736 }
3737 }
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003738
Roopa Prabhud2976532016-08-30 21:56:45 -07003739 if (dev->netdev_ops->ndo_fdb_dump)
3740 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
3741 dev, NULL,
3742 &fidx);
3743 else
3744 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
3745 &fidx);
3746 if (err == -EMSGSIZE)
3747 goto out;
3748
3749 cops = NULL;
3750
3751 /* reset fdb offset to 0 for rest of the interfaces */
3752 cb->args[2] = 0;
3753 fidx = 0;
3754cont:
3755 idx++;
John Fastabend77162022012-04-15 06:43:56 +00003756 }
John Fastabend77162022012-04-15 06:43:56 +00003757 }
John Fastabend77162022012-04-15 06:43:56 +00003758
Roopa Prabhud2976532016-08-30 21:56:45 -07003759out:
3760 cb->args[0] = h;
3761 cb->args[1] = idx;
3762 cb->args[2] = fidx;
3763
John Fastabend77162022012-04-15 06:43:56 +00003764 return skb->len;
3765}
3766
Scott Feldman2c3c0312014-11-28 14:34:25 +01003767static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
3768 unsigned int attrnum, unsigned int flag)
3769{
3770 if (mask & flag)
3771 return nla_put_u8(skb, attrnum, !!(flags & flag));
3772 return 0;
3773}
3774
John Fastabend815cccb2012-10-24 08:13:09 +00003775int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Scott Feldman2c3c0312014-11-28 14:34:25 +01003776 struct net_device *dev, u16 mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003777 u32 flags, u32 mask, int nlflags,
3778 u32 filter_mask,
3779 int (*vlan_fill)(struct sk_buff *skb,
3780 struct net_device *dev,
3781 u32 filter_mask))
John Fastabend815cccb2012-10-24 08:13:09 +00003782{
3783 struct nlmsghdr *nlh;
3784 struct ifinfomsg *ifm;
3785 struct nlattr *br_afspec;
Scott Feldman2c3c0312014-11-28 14:34:25 +01003786 struct nlattr *protinfo;
John Fastabend815cccb2012-10-24 08:13:09 +00003787 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Jiri Pirko898e5062013-01-03 22:48:52 +00003788 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003789 int err = 0;
John Fastabend815cccb2012-10-24 08:13:09 +00003790
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02003791 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
John Fastabend815cccb2012-10-24 08:13:09 +00003792 if (nlh == NULL)
3793 return -EMSGSIZE;
3794
3795 ifm = nlmsg_data(nlh);
3796 ifm->ifi_family = AF_BRIDGE;
3797 ifm->__ifi_pad = 0;
3798 ifm->ifi_type = dev->type;
3799 ifm->ifi_index = dev->ifindex;
3800 ifm->ifi_flags = dev_get_flags(dev);
3801 ifm->ifi_change = 0;
3802
3803
3804 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
3805 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
3806 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00003807 (br_dev &&
3808 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
John Fastabend815cccb2012-10-24 08:13:09 +00003809 (dev->addr_len &&
3810 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02003811 (dev->ifindex != dev_get_iflink(dev) &&
3812 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
John Fastabend815cccb2012-10-24 08:13:09 +00003813 goto nla_put_failure;
3814
3815 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
3816 if (!br_afspec)
3817 goto nla_put_failure;
3818
Roopa Prabhu1d460b92014-12-08 14:04:20 -08003819 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
John Fastabend815cccb2012-10-24 08:13:09 +00003820 nla_nest_cancel(skb, br_afspec);
3821 goto nla_put_failure;
3822 }
Roopa Prabhu1d460b92014-12-08 14:04:20 -08003823
3824 if (mode != BRIDGE_MODE_UNDEF) {
3825 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
3826 nla_nest_cancel(skb, br_afspec);
3827 goto nla_put_failure;
3828 }
3829 }
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003830 if (vlan_fill) {
3831 err = vlan_fill(skb, dev, filter_mask);
3832 if (err) {
3833 nla_nest_cancel(skb, br_afspec);
3834 goto nla_put_failure;
3835 }
3836 }
John Fastabend815cccb2012-10-24 08:13:09 +00003837 nla_nest_end(skb, br_afspec);
3838
Scott Feldman2c3c0312014-11-28 14:34:25 +01003839 protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
3840 if (!protinfo)
3841 goto nla_put_failure;
3842
3843 if (brport_nla_put_flag(skb, flags, mask,
3844 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
3845 brport_nla_put_flag(skb, flags, mask,
3846 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
3847 brport_nla_put_flag(skb, flags, mask,
3848 IFLA_BRPORT_FAST_LEAVE,
3849 BR_MULTICAST_FAST_LEAVE) ||
3850 brport_nla_put_flag(skb, flags, mask,
3851 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
3852 brport_nla_put_flag(skb, flags, mask,
3853 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
3854 brport_nla_put_flag(skb, flags, mask,
3855 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
3856 brport_nla_put_flag(skb, flags, mask,
3857 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
3858 brport_nla_put_flag(skb, flags, mask,
3859 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
3860 nla_nest_cancel(skb, protinfo);
3861 goto nla_put_failure;
3862 }
3863
3864 nla_nest_end(skb, protinfo);
3865
Johannes Berg053c0952015-01-16 22:09:00 +01003866 nlmsg_end(skb, nlh);
3867 return 0;
John Fastabend815cccb2012-10-24 08:13:09 +00003868nla_put_failure:
3869 nlmsg_cancel(skb, nlh);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003870 return err ? err : -EMSGSIZE;
John Fastabend815cccb2012-10-24 08:13:09 +00003871}
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003872EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
John Fastabend815cccb2012-10-24 08:13:09 +00003873
John Fastabende5a55a82012-10-24 08:12:57 +00003874static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
3875{
3876 struct net *net = sock_net(skb->sk);
3877 struct net_device *dev;
3878 int idx = 0;
3879 u32 portid = NETLINK_CB(cb->skb).portid;
3880 u32 seq = cb->nlh->nlmsg_seq;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00003881 u32 filter_mask = 0;
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003882 int err;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00003883
Thomas Grafaa68c202014-11-26 13:42:20 +01003884 if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
3885 struct nlattr *extfilt;
3886
3887 extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
3888 IFLA_EXT_MASK);
3889 if (extfilt) {
3890 if (nla_len(extfilt) < sizeof(filter_mask))
3891 return -EINVAL;
3892
3893 filter_mask = nla_get_u32(extfilt);
3894 }
3895 }
John Fastabende5a55a82012-10-24 08:12:57 +00003896
3897 rcu_read_lock();
3898 for_each_netdev_rcu(net, dev) {
3899 const struct net_device_ops *ops = dev->netdev_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00003900 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
John Fastabende5a55a82012-10-24 08:12:57 +00003901
Jiri Pirko898e5062013-01-03 22:48:52 +00003902 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003903 if (idx >= cb->args[0]) {
3904 err = br_dev->netdev_ops->ndo_bridge_getlink(
3905 skb, portid, seq, dev,
3906 filter_mask, NLM_F_MULTI);
David Ahernf6c57752017-05-15 23:19:17 -07003907 if (err < 0 && err != -EOPNOTSUPP) {
3908 if (likely(skb->len))
3909 break;
3910
3911 goto out_err;
3912 }
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003913 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00003914 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00003915 }
3916
3917 if (ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003918 if (idx >= cb->args[0]) {
3919 err = ops->ndo_bridge_getlink(skb, portid,
3920 seq, dev,
3921 filter_mask,
3922 NLM_F_MULTI);
David Ahernf6c57752017-05-15 23:19:17 -07003923 if (err < 0 && err != -EOPNOTSUPP) {
3924 if (likely(skb->len))
3925 break;
3926
3927 goto out_err;
3928 }
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003929 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00003930 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00003931 }
3932 }
David Ahernf6c57752017-05-15 23:19:17 -07003933 err = skb->len;
3934out_err:
John Fastabende5a55a82012-10-24 08:12:57 +00003935 rcu_read_unlock();
3936 cb->args[0] = idx;
3937
David Ahernf6c57752017-05-15 23:19:17 -07003938 return err;
John Fastabende5a55a82012-10-24 08:12:57 +00003939}
3940
John Fastabend2469ffd2012-10-24 08:13:03 +00003941static inline size_t bridge_nlmsg_size(void)
3942{
3943 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3944 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3945 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3946 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
3947 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
3948 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
3949 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
3950 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
3951 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
3952 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
3953 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
3954}
3955
Roopa Prabhu02dba432015-01-14 20:02:25 -08003956static int rtnl_bridge_notify(struct net_device *dev)
John Fastabend2469ffd2012-10-24 08:13:03 +00003957{
3958 struct net *net = dev_net(dev);
John Fastabend2469ffd2012-10-24 08:13:03 +00003959 struct sk_buff *skb;
3960 int err = -EOPNOTSUPP;
3961
Roopa Prabhu02dba432015-01-14 20:02:25 -08003962 if (!dev->netdev_ops->ndo_bridge_getlink)
3963 return 0;
3964
John Fastabend2469ffd2012-10-24 08:13:03 +00003965 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
3966 if (!skb) {
3967 err = -ENOMEM;
3968 goto errout;
3969 }
3970
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02003971 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
Roopa Prabhu02dba432015-01-14 20:02:25 -08003972 if (err < 0)
3973 goto errout;
John Fastabend2469ffd2012-10-24 08:13:03 +00003974
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08003975 if (!skb->len)
3976 goto errout;
3977
John Fastabend2469ffd2012-10-24 08:13:03 +00003978 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
3979 return 0;
3980errout:
3981 WARN_ON(err == -EMSGSIZE);
3982 kfree_skb(skb);
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08003983 if (err)
3984 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
John Fastabend2469ffd2012-10-24 08:13:03 +00003985 return err;
3986}
3987
David Ahernc21ef3e2017-04-16 09:48:24 -07003988static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3989 struct netlink_ext_ack *extack)
John Fastabende5a55a82012-10-24 08:12:57 +00003990{
3991 struct net *net = sock_net(skb->sk);
3992 struct ifinfomsg *ifm;
3993 struct net_device *dev;
John Fastabend2469ffd2012-10-24 08:13:03 +00003994 struct nlattr *br_spec, *attr = NULL;
3995 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02003996 u16 flags = 0;
John Fastabendc38e01b2012-11-02 16:32:36 +00003997 bool have_flags = false;
John Fastabende5a55a82012-10-24 08:12:57 +00003998
3999 if (nlmsg_len(nlh) < sizeof(*ifm))
4000 return -EINVAL;
4001
4002 ifm = nlmsg_data(nlh);
4003 if (ifm->ifi_family != AF_BRIDGE)
4004 return -EPFNOSUPPORT;
4005
4006 dev = __dev_get_by_index(net, ifm->ifi_index);
4007 if (!dev) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004008 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabende5a55a82012-10-24 08:12:57 +00004009 return -ENODEV;
4010 }
4011
John Fastabend2469ffd2012-10-24 08:13:03 +00004012 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4013 if (br_spec) {
4014 nla_for_each_nested(attr, br_spec, rem) {
4015 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01004016 if (nla_len(attr) < sizeof(flags))
4017 return -EINVAL;
4018
John Fastabendc38e01b2012-11-02 16:32:36 +00004019 have_flags = true;
John Fastabend2469ffd2012-10-24 08:13:03 +00004020 flags = nla_get_u16(attr);
4021 break;
4022 }
4023 }
4024 }
4025
4026 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00004027 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4028
4029 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
John Fastabend2469ffd2012-10-24 08:13:03 +00004030 err = -EOPNOTSUPP;
4031 goto out;
4032 }
4033
Roopa Prabhuadd511b2015-01-29 22:40:12 -08004034 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
John Fastabende5a55a82012-10-24 08:12:57 +00004035 if (err)
4036 goto out;
John Fastabend2469ffd2012-10-24 08:13:03 +00004037
4038 flags &= ~BRIDGE_FLAGS_MASTER;
John Fastabende5a55a82012-10-24 08:12:57 +00004039 }
4040
John Fastabend2469ffd2012-10-24 08:13:03 +00004041 if ((flags & BRIDGE_FLAGS_SELF)) {
4042 if (!dev->netdev_ops->ndo_bridge_setlink)
4043 err = -EOPNOTSUPP;
4044 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08004045 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
4046 flags);
Roopa Prabhu02dba432015-01-14 20:02:25 -08004047 if (!err) {
John Fastabend2469ffd2012-10-24 08:13:03 +00004048 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08004049
4050 /* Generate event to notify upper layer of bridge
4051 * change
4052 */
4053 err = rtnl_bridge_notify(dev);
4054 }
John Fastabend2469ffd2012-10-24 08:13:03 +00004055 }
4056
John Fastabendc38e01b2012-11-02 16:32:36 +00004057 if (have_flags)
John Fastabend2469ffd2012-10-24 08:13:03 +00004058 memcpy(nla_data(attr), &flags, sizeof(flags));
John Fastabende5a55a82012-10-24 08:12:57 +00004059out:
4060 return err;
4061}
4062
David Ahernc21ef3e2017-04-16 09:48:24 -07004063static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
4064 struct netlink_ext_ack *extack)
Vlad Yasevich407af322013-02-13 12:00:12 +00004065{
4066 struct net *net = sock_net(skb->sk);
4067 struct ifinfomsg *ifm;
4068 struct net_device *dev;
4069 struct nlattr *br_spec, *attr = NULL;
4070 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02004071 u16 flags = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +00004072 bool have_flags = false;
4073
4074 if (nlmsg_len(nlh) < sizeof(*ifm))
4075 return -EINVAL;
4076
4077 ifm = nlmsg_data(nlh);
4078 if (ifm->ifi_family != AF_BRIDGE)
4079 return -EPFNOSUPPORT;
4080
4081 dev = __dev_get_by_index(net, ifm->ifi_index);
4082 if (!dev) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004083 NL_SET_ERR_MSG(extack, "unknown ifindex");
Vlad Yasevich407af322013-02-13 12:00:12 +00004084 return -ENODEV;
4085 }
4086
4087 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4088 if (br_spec) {
4089 nla_for_each_nested(attr, br_spec, rem) {
4090 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01004091 if (nla_len(attr) < sizeof(flags))
4092 return -EINVAL;
4093
Vlad Yasevich407af322013-02-13 12:00:12 +00004094 have_flags = true;
4095 flags = nla_get_u16(attr);
4096 break;
4097 }
4098 }
4099 }
4100
Vlad Yasevich407af322013-02-13 12:00:12 +00004101 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
4102 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4103
4104 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
4105 err = -EOPNOTSUPP;
4106 goto out;
4107 }
4108
Roopa Prabhuadd511b2015-01-29 22:40:12 -08004109 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00004110 if (err)
4111 goto out;
4112
4113 flags &= ~BRIDGE_FLAGS_MASTER;
4114 }
4115
4116 if ((flags & BRIDGE_FLAGS_SELF)) {
4117 if (!dev->netdev_ops->ndo_bridge_dellink)
4118 err = -EOPNOTSUPP;
4119 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08004120 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
4121 flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00004122
Roopa Prabhu02dba432015-01-14 20:02:25 -08004123 if (!err) {
Vlad Yasevich407af322013-02-13 12:00:12 +00004124 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08004125
4126 /* Generate event to notify upper layer of bridge
4127 * change
4128 */
4129 err = rtnl_bridge_notify(dev);
4130 }
Vlad Yasevich407af322013-02-13 12:00:12 +00004131 }
4132
4133 if (have_flags)
4134 memcpy(nla_data(attr), &flags, sizeof(flags));
Vlad Yasevich407af322013-02-13 12:00:12 +00004135out:
4136 return err;
4137}
4138
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004139static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
4140{
4141 return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
4142 (!idxattr || idxattr == attrid);
4143}
4144
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004145#define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
4146static int rtnl_get_offload_stats_attr_size(int attr_id)
4147{
4148 switch (attr_id) {
4149 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
4150 return sizeof(struct rtnl_link_stats64);
4151 }
4152
4153 return 0;
4154}
4155
4156static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
4157 int *prividx)
4158{
4159 struct nlattr *attr = NULL;
4160 int attr_id, size;
4161 void *attr_data;
4162 int err;
4163
4164 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
4165 dev->netdev_ops->ndo_get_offload_stats))
4166 return -ENODATA;
4167
4168 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
4169 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
4170 if (attr_id < *prividx)
4171 continue;
4172
4173 size = rtnl_get_offload_stats_attr_size(attr_id);
4174 if (!size)
4175 continue;
4176
Or Gerlitz3df5b3c2016-11-22 23:09:54 +02004177 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004178 continue;
4179
4180 attr = nla_reserve_64bit(skb, attr_id, size,
4181 IFLA_OFFLOAD_XSTATS_UNSPEC);
4182 if (!attr)
4183 goto nla_put_failure;
4184
4185 attr_data = nla_data(attr);
4186 memset(attr_data, 0, size);
4187 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
4188 attr_data);
4189 if (err)
4190 goto get_offload_stats_failure;
4191 }
4192
4193 if (!attr)
4194 return -ENODATA;
4195
4196 *prividx = 0;
4197 return 0;
4198
4199nla_put_failure:
4200 err = -EMSGSIZE;
4201get_offload_stats_failure:
4202 *prividx = attr_id;
4203 return err;
4204}
4205
4206static int rtnl_get_offload_stats_size(const struct net_device *dev)
4207{
4208 int nla_size = 0;
4209 int attr_id;
4210 int size;
4211
4212 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
4213 dev->netdev_ops->ndo_get_offload_stats))
4214 return 0;
4215
4216 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
4217 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
Or Gerlitz3df5b3c2016-11-22 23:09:54 +02004218 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004219 continue;
4220 size = rtnl_get_offload_stats_attr_size(attr_id);
4221 nla_size += nla_total_size_64bit(size);
4222 }
4223
4224 if (nla_size != 0)
4225 nla_size += nla_total_size(0);
4226
4227 return nla_size;
4228}
4229
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004230static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
4231 int type, u32 pid, u32 seq, u32 change,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004232 unsigned int flags, unsigned int filter_mask,
4233 int *idxattr, int *prividx)
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004234{
4235 struct if_stats_msg *ifsm;
4236 struct nlmsghdr *nlh;
4237 struct nlattr *attr;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004238 int s_prividx = *prividx;
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004239 int err;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004240
4241 ASSERT_RTNL();
4242
4243 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
4244 if (!nlh)
4245 return -EMSGSIZE;
4246
4247 ifsm = nlmsg_data(nlh);
Nikolay Aleksandrovce024f42017-10-03 13:20:48 +03004248 ifsm->family = PF_UNSPEC;
4249 ifsm->pad1 = 0;
4250 ifsm->pad2 = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004251 ifsm->ifindex = dev->ifindex;
4252 ifsm->filter_mask = filter_mask;
4253
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004254 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004255 struct rtnl_link_stats64 *sp;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004256
Nicolas Dichtel58414d32016-04-21 18:58:25 +02004257 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
4258 sizeof(struct rtnl_link_stats64),
4259 IFLA_STATS_UNSPEC);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004260 if (!attr)
4261 goto nla_put_failure;
4262
4263 sp = nla_data(attr);
4264 dev_get_stats(dev, sp);
4265 }
4266
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004267 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
4268 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
4269
4270 if (ops && ops->fill_linkxstats) {
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004271 *idxattr = IFLA_STATS_LINK_XSTATS;
4272 attr = nla_nest_start(skb,
4273 IFLA_STATS_LINK_XSTATS);
4274 if (!attr)
4275 goto nla_put_failure;
4276
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004277 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
4278 nla_nest_end(skb, attr);
4279 if (err)
4280 goto nla_put_failure;
4281 *idxattr = 0;
4282 }
4283 }
4284
4285 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
4286 *idxattr)) {
4287 const struct rtnl_link_ops *ops = NULL;
4288 const struct net_device *master;
4289
4290 master = netdev_master_upper_dev_get(dev);
4291 if (master)
4292 ops = master->rtnl_link_ops;
4293 if (ops && ops->fill_linkxstats) {
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004294 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
4295 attr = nla_nest_start(skb,
4296 IFLA_STATS_LINK_XSTATS_SLAVE);
4297 if (!attr)
4298 goto nla_put_failure;
4299
4300 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004301 nla_nest_end(skb, attr);
4302 if (err)
4303 goto nla_put_failure;
4304 *idxattr = 0;
4305 }
4306 }
4307
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004308 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
4309 *idxattr)) {
4310 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
4311 attr = nla_nest_start(skb, IFLA_STATS_LINK_OFFLOAD_XSTATS);
4312 if (!attr)
4313 goto nla_put_failure;
4314
4315 err = rtnl_get_offload_stats(skb, dev, prividx);
4316 if (err == -ENODATA)
4317 nla_nest_cancel(skb, attr);
4318 else
4319 nla_nest_end(skb, attr);
4320
4321 if (err && err != -ENODATA)
4322 goto nla_put_failure;
4323 *idxattr = 0;
4324 }
4325
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004326 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
4327 struct rtnl_af_ops *af_ops;
4328
4329 *idxattr = IFLA_STATS_AF_SPEC;
4330 attr = nla_nest_start(skb, IFLA_STATS_AF_SPEC);
4331 if (!attr)
4332 goto nla_put_failure;
4333
Florian Westphal5fa85a02017-10-16 15:44:36 +02004334 rcu_read_lock();
4335 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004336 if (af_ops->fill_stats_af) {
4337 struct nlattr *af;
4338 int err;
4339
4340 af = nla_nest_start(skb, af_ops->family);
Florian Westphal5fa85a02017-10-16 15:44:36 +02004341 if (!af) {
4342 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004343 goto nla_put_failure;
Florian Westphal5fa85a02017-10-16 15:44:36 +02004344 }
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004345 err = af_ops->fill_stats_af(skb, dev);
4346
Florian Westphal5fa85a02017-10-16 15:44:36 +02004347 if (err == -ENODATA) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004348 nla_nest_cancel(skb, af);
Florian Westphal5fa85a02017-10-16 15:44:36 +02004349 } else if (err < 0) {
4350 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004351 goto nla_put_failure;
Florian Westphal5fa85a02017-10-16 15:44:36 +02004352 }
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004353
4354 nla_nest_end(skb, af);
4355 }
4356 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02004357 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004358
4359 nla_nest_end(skb, attr);
4360
4361 *idxattr = 0;
4362 }
4363
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004364 nlmsg_end(skb, nlh);
4365
4366 return 0;
4367
4368nla_put_failure:
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004369 /* not a multi message or no progress mean a real error */
4370 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
4371 nlmsg_cancel(skb, nlh);
4372 else
4373 nlmsg_end(skb, nlh);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004374
4375 return -EMSGSIZE;
4376}
4377
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004378static size_t if_nlmsg_stats_size(const struct net_device *dev,
4379 u32 filter_mask)
4380{
4381 size_t size = 0;
4382
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004383 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004384 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
4385
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004386 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
4387 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004388 int attr = IFLA_STATS_LINK_XSTATS;
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004389
4390 if (ops && ops->get_linkxstats_size) {
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004391 size += nla_total_size(ops->get_linkxstats_size(dev,
4392 attr));
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004393 /* for IFLA_STATS_LINK_XSTATS */
4394 size += nla_total_size(0);
4395 }
4396 }
4397
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004398 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
4399 struct net_device *_dev = (struct net_device *)dev;
4400 const struct rtnl_link_ops *ops = NULL;
4401 const struct net_device *master;
4402
4403 /* netdev_master_upper_dev_get can't take const */
4404 master = netdev_master_upper_dev_get(_dev);
4405 if (master)
4406 ops = master->rtnl_link_ops;
4407 if (ops && ops->get_linkxstats_size) {
4408 int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
4409
4410 size += nla_total_size(ops->get_linkxstats_size(dev,
4411 attr));
4412 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
4413 size += nla_total_size(0);
4414 }
4415 }
4416
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004417 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
4418 size += rtnl_get_offload_stats_size(dev);
4419
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004420 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
4421 struct rtnl_af_ops *af_ops;
4422
4423 /* for IFLA_STATS_AF_SPEC */
4424 size += nla_total_size(0);
4425
Florian Westphal5fa85a02017-10-16 15:44:36 +02004426 rcu_read_lock();
4427 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004428 if (af_ops->get_stats_af_size) {
4429 size += nla_total_size(
4430 af_ops->get_stats_af_size(dev));
4431
4432 /* for AF_* */
4433 size += nla_total_size(0);
4434 }
4435 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02004436 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004437 }
4438
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004439 return size;
4440}
4441
David Ahernc21ef3e2017-04-16 09:48:24 -07004442static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
4443 struct netlink_ext_ack *extack)
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004444{
4445 struct net *net = sock_net(skb->sk);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004446 struct net_device *dev = NULL;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004447 int idxattr = 0, prividx = 0;
4448 struct if_stats_msg *ifsm;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004449 struct sk_buff *nskb;
4450 u32 filter_mask;
4451 int err;
4452
Mathias Krause4775cc12016-12-28 17:52:15 +01004453 if (nlmsg_len(nlh) < sizeof(*ifsm))
4454 return -EINVAL;
4455
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004456 ifsm = nlmsg_data(nlh);
4457 if (ifsm->ifindex > 0)
4458 dev = __dev_get_by_index(net, ifsm->ifindex);
4459 else
4460 return -EINVAL;
4461
4462 if (!dev)
4463 return -ENODEV;
4464
4465 filter_mask = ifsm->filter_mask;
4466 if (!filter_mask)
4467 return -EINVAL;
4468
4469 nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
4470 if (!nskb)
4471 return -ENOBUFS;
4472
4473 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
4474 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004475 0, filter_mask, &idxattr, &prividx);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004476 if (err < 0) {
4477 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
4478 WARN_ON(err == -EMSGSIZE);
4479 kfree_skb(nskb);
4480 } else {
4481 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
4482 }
4483
4484 return err;
4485}
4486
4487static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
4488{
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004489 int h, s_h, err, s_idx, s_idxattr, s_prividx;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004490 struct net *net = sock_net(skb->sk);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004491 unsigned int flags = NLM_F_MULTI;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004492 struct if_stats_msg *ifsm;
4493 struct hlist_head *head;
4494 struct net_device *dev;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004495 u32 filter_mask = 0;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004496 int idx = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004497
4498 s_h = cb->args[0];
4499 s_idx = cb->args[1];
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004500 s_idxattr = cb->args[2];
4501 s_prividx = cb->args[3];
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004502
4503 cb->seq = net->dev_base_seq;
4504
Mathias Krause4775cc12016-12-28 17:52:15 +01004505 if (nlmsg_len(cb->nlh) < sizeof(*ifsm))
4506 return -EINVAL;
4507
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004508 ifsm = nlmsg_data(cb->nlh);
4509 filter_mask = ifsm->filter_mask;
4510 if (!filter_mask)
4511 return -EINVAL;
4512
4513 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4514 idx = 0;
4515 head = &net->dev_index_head[h];
4516 hlist_for_each_entry(dev, head, index_hlist) {
4517 if (idx < s_idx)
4518 goto cont;
4519 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
4520 NETLINK_CB(cb->skb).portid,
4521 cb->nlh->nlmsg_seq, 0,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004522 flags, filter_mask,
4523 &s_idxattr, &s_prividx);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004524 /* If we ran out of room on the first message,
4525 * we're in trouble
4526 */
4527 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
4528
4529 if (err < 0)
4530 goto out;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004531 s_prividx = 0;
4532 s_idxattr = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004533 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
4534cont:
4535 idx++;
4536 }
4537 }
4538out:
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004539 cb->args[3] = s_prividx;
4540 cb->args[2] = s_idxattr;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004541 cb->args[1] = idx;
4542 cb->args[0] = h;
4543
4544 return skb->len;
4545}
4546
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547/* Process one rtnetlink message. */
4548
Johannes Berg2d4bc932017-04-12 14:34:04 +02004549static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
4550 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004552 struct net *net = sock_net(skb->sk);
Florian Westphaladdf9b92017-12-02 21:44:05 +01004553 struct rtnl_link *link;
Florian Westphale4202512017-12-02 21:44:06 +01004554 struct module *owner;
Florian Westphal6853dd42017-08-09 20:41:51 +02004555 int err = -EOPNOTSUPP;
Thomas Grafe2849862007-03-22 11:48:11 -07004556 rtnl_doit_func doit;
Florian Westphal62256f92017-08-09 20:41:52 +02004557 unsigned int flags;
Alexander Kuleshov617cfc72016-01-10 21:26:57 +06004558 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 int family;
4560 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 if (type > RTM_MAX)
Thomas Graf038890f2007-04-05 14:35:52 -07004564 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565
4566 type -= RTM_BASE;
4567
4568 /* All the messages must have at least 1 byte length */
Hong zhi guo573ce262013-03-27 06:47:04 +00004569 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 return 0;
4571
Hong zhi guo573ce262013-03-27 06:47:04 +00004572 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573 kind = type&3;
4574
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07004575 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07004576 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577
Florian Westphal6853dd42017-08-09 20:41:51 +02004578 rcu_read_lock();
David S. Millerb8f3ab42011-01-18 12:40:38 -08004579 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004580 struct sock *rtnl;
Thomas Grafe2849862007-03-22 11:48:11 -07004581 rtnl_dumpit_func dumpit;
Greg Rosec7ac8672011-06-10 01:27:09 +00004582 u16 min_dump_alloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583
Florian Westphaladdf9b92017-12-02 21:44:05 +01004584 link = rtnl_get_link(family, type);
4585 if (!link || !link->dumpit) {
Florian Westphal6853dd42017-08-09 20:41:51 +02004586 family = PF_UNSPEC;
Florian Westphaladdf9b92017-12-02 21:44:05 +01004587 link = rtnl_get_link(family, type);
4588 if (!link || !link->dumpit)
Florian Westphal6853dd42017-08-09 20:41:51 +02004589 goto err_unlock;
4590 }
Florian Westphale4202512017-12-02 21:44:06 +01004591 owner = link->owner;
Florian Westphaladdf9b92017-12-02 21:44:05 +01004592 dumpit = link->dumpit;
Florian Westphale1fa6d22017-08-09 20:41:47 +02004593
Florian Westphal5c2bb9b2017-08-10 16:52:58 +02004594 if (type == RTM_GETLINK - RTM_BASE)
Florian Westphale1fa6d22017-08-09 20:41:47 +02004595 min_dump_alloc = rtnl_calcit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596
Florian Westphale4202512017-12-02 21:44:06 +01004597 err = 0;
4598 /* need to do this before rcu_read_unlock() */
4599 if (!try_module_get(owner))
4600 err = -EPROTONOSUPPORT;
4601
Florian Westphal6853dd42017-08-09 20:41:51 +02004602 rcu_read_unlock();
4603
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004604 rtnl = net->rtnl;
Florian Westphale4202512017-12-02 21:44:06 +01004605 if (err == 0) {
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00004606 struct netlink_dump_control c = {
4607 .dump = dumpit,
4608 .min_dump_alloc = min_dump_alloc,
Florian Westphale4202512017-12-02 21:44:06 +01004609 .module = owner,
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00004610 };
4611 err = netlink_dump_start(rtnl, skb, nlh, &c);
Florian Westphale4202512017-12-02 21:44:06 +01004612 /* netlink_dump_start() will keep a reference on
4613 * module if dump is still in progress.
4614 */
4615 module_put(owner);
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00004616 }
Eric Dumazet2907c352011-05-25 07:34:04 +00004617 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 }
4619
Florian Westphaladdf9b92017-12-02 21:44:05 +01004620 link = rtnl_get_link(family, type);
4621 if (!link || !link->doit) {
Florian Westphal8caa38b2017-08-10 16:53:01 +02004622 family = PF_UNSPEC;
Florian Westphaladdf9b92017-12-02 21:44:05 +01004623 link = rtnl_get_link(PF_UNSPEC, type);
4624 if (!link || !link->doit)
4625 goto out_unlock;
Florian Westphal8caa38b2017-08-10 16:53:01 +02004626 }
4627
Florian Westphale4202512017-12-02 21:44:06 +01004628 owner = link->owner;
4629 if (!try_module_get(owner)) {
4630 err = -EPROTONOSUPPORT;
4631 goto out_unlock;
4632 }
4633
Florian Westphaladdf9b92017-12-02 21:44:05 +01004634 flags = link->flags;
Florian Westphal62256f92017-08-09 20:41:52 +02004635 if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
Florian Westphaladdf9b92017-12-02 21:44:05 +01004636 doit = link->doit;
Florian Westphal62256f92017-08-09 20:41:52 +02004637 rcu_read_unlock();
4638 if (doit)
4639 err = doit(skb, nlh, extack);
Florian Westphale4202512017-12-02 21:44:06 +01004640 module_put(owner);
Florian Westphal62256f92017-08-09 20:41:52 +02004641 return err;
4642 }
Florian Westphal6853dd42017-08-09 20:41:51 +02004643 rcu_read_unlock();
4644
Florian Westphal0cc09022017-08-09 20:41:50 +02004645 rtnl_lock();
Florian Westphaladdf9b92017-12-02 21:44:05 +01004646 link = rtnl_get_link(family, type);
4647 if (link && link->doit)
4648 err = link->doit(skb, nlh, extack);
Florian Westphal0cc09022017-08-09 20:41:50 +02004649 rtnl_unlock();
Florian Westphaladdf9b92017-12-02 21:44:05 +01004650
Florian Westphale4202512017-12-02 21:44:06 +01004651 module_put(owner);
4652
Florian Westphaladdf9b92017-12-02 21:44:05 +01004653 return err;
4654
4655out_unlock:
4656 rcu_read_unlock();
Florian Westphal0cc09022017-08-09 20:41:50 +02004657 return err;
4658
4659err_unlock:
Florian Westphal6853dd42017-08-09 20:41:51 +02004660 rcu_read_unlock();
Florian Westphal0cc09022017-08-09 20:41:50 +02004661 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662}
4663
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07004664static void rtnetlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07004666 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667}
4668
Julien Gomes5f729ea2017-06-20 13:54:16 -07004669static int rtnetlink_bind(struct net *net, int group)
4670{
4671 switch (group) {
4672 case RTNLGRP_IPV4_MROUTE_R:
4673 case RTNLGRP_IPV6_MROUTE_R:
4674 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
4675 return -EPERM;
4676 break;
4677 }
4678 return 0;
4679}
4680
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
4682{
Jiri Pirko351638e2013-05-28 01:30:21 +00004683 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02004684
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 switch (event) {
Vlad Yasevich5138e862017-04-04 09:23:41 -04004686 case NETDEV_REBOOT:
Xin Long8a212582017-10-15 18:13:41 +08004687 case NETDEV_CHANGEMTU:
David Ahern37536542017-07-19 10:22:40 -07004688 case NETDEV_CHANGEADDR:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004689 case NETDEV_CHANGENAME:
4690 case NETDEV_FEAT_CHANGE:
4691 case NETDEV_BONDING_FAILOVER:
Xin Longe6e66592017-10-15 18:13:43 +08004692 case NETDEV_POST_TYPE_CHANGE:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004693 case NETDEV_NOTIFY_PEERS:
Xin Longdc709f32017-10-15 18:13:44 +08004694 case NETDEV_CHANGEUPPER:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004695 case NETDEV_RESEND_IGMP:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004696 case NETDEV_CHANGEINFODATA:
Xin Longeeda3fb2017-10-24 13:54:19 +08004697 case NETDEV_CHANGELOWERSTATE:
Xin Longebdcf042017-10-15 18:13:42 +08004698 case NETDEV_CHANGE_TX_QUEUE_LEN:
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04004699 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01004700 GFP_KERNEL, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 break;
4702 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 break;
4704 }
4705 return NOTIFY_DONE;
4706}
4707
4708static struct notifier_block rtnetlink_dev_notifier = {
4709 .notifier_call = rtnetlink_event,
4710};
4711
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004712
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004713static int __net_init rtnetlink_net_init(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004714{
4715 struct sock *sk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00004716 struct netlink_kernel_cfg cfg = {
4717 .groups = RTNLGRP_MAX,
4718 .input = rtnetlink_rcv,
4719 .cb_mutex = &rtnl_mutex,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00004720 .flags = NL_CFG_F_NONROOT_RECV,
Julien Gomes5f729ea2017-06-20 13:54:16 -07004721 .bind = rtnetlink_bind,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00004722 };
4723
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00004724 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004725 if (!sk)
4726 return -ENOMEM;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004727 net->rtnl = sk;
4728 return 0;
4729}
4730
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004731static void __net_exit rtnetlink_net_exit(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004732{
Denis V. Lunev775516b2008-01-18 23:55:19 -08004733 netlink_kernel_release(net->rtnl);
4734 net->rtnl = NULL;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004735}
4736
4737static struct pernet_operations rtnetlink_net_ops = {
4738 .init = rtnetlink_net_init,
4739 .exit = rtnetlink_net_exit,
4740};
4741
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742void __init rtnetlink_init(void)
4743{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004744 if (register_pernet_subsys(&rtnetlink_net_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 panic("rtnetlink_init: cannot initialize rtnetlink\n");
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004746
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747 register_netdevice_notifier(&rtnetlink_dev_notifier);
Thomas Graf340d17f2007-03-22 11:49:22 -07004748
Greg Rosec7ac8672011-06-10 01:27:09 +00004749 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
Florian Westphalb97bac62017-08-09 20:41:48 +02004750 rtnl_dump_ifinfo, 0);
4751 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
4752 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
4753 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
Thomas Graf687ad8c2007-03-22 11:59:42 -07004754
Florian Westphalb97bac62017-08-09 20:41:48 +02004755 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
4756 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
4757 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
John Fastabend77162022012-04-15 06:43:56 +00004758
Florian Westphalb97bac62017-08-09 20:41:48 +02004759 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
4760 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
4761 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, 0);
John Fastabende5a55a82012-10-24 08:12:57 +00004762
Florian Westphalb97bac62017-08-09 20:41:48 +02004763 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
4764 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
4765 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004766
4767 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
Florian Westphalb97bac62017-08-09 20:41:48 +02004768 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769}