blob: a78fd61da0ec50a0cf2f8f6216a15944086d0e89 [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 Westphal62256f92017-08-09 20:41:52 +020065 unsigned int flags;
Thomas Grafe2849862007-03-22 11:48:11 -070066};
67
Stephen Hemminger6756ae42006-03-20 22:23:58 -080068static DEFINE_MUTEX(rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70void rtnl_lock(void)
71{
Stephen Hemminger6756ae42006-03-20 22:23:58 -080072 mutex_lock(&rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
Eric Dumazete0d087a2009-11-07 01:26:17 -080074EXPORT_SYMBOL(rtnl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Eric Dumazet1b5c5492016-06-13 20:21:50 -070076static struct sk_buff *defer_kfree_skb_list;
77void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail)
78{
79 if (head && tail) {
80 tail->next = defer_kfree_skb_list;
81 defer_kfree_skb_list = head;
82 }
83}
84EXPORT_SYMBOL(rtnl_kfree_skbs);
85
Stephen Hemminger6756ae42006-03-20 22:23:58 -080086void __rtnl_unlock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Eric Dumazet1b5c5492016-06-13 20:21:50 -070088 struct sk_buff *head = defer_kfree_skb_list;
89
90 defer_kfree_skb_list = NULL;
91
Stephen Hemminger6756ae42006-03-20 22:23:58 -080092 mutex_unlock(&rtnl_mutex);
Eric Dumazet1b5c5492016-06-13 20:21:50 -070093
94 while (head) {
95 struct sk_buff *next = head->next;
96
97 kfree_skb(head);
98 cond_resched();
99 head = next;
100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103void rtnl_unlock(void)
104{
Herbert Xu58ec3b42008-10-07 15:50:03 -0700105 /* This fellow will unlock it for us. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 netdev_run_todo();
107}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800108EXPORT_SYMBOL(rtnl_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800110int rtnl_trylock(void)
111{
112 return mutex_trylock(&rtnl_mutex);
113}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800114EXPORT_SYMBOL(rtnl_trylock);
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800115
Patrick McHardyc9c10142008-04-23 22:10:48 -0700116int rtnl_is_locked(void)
117{
118 return mutex_is_locked(&rtnl_mutex);
119}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800120EXPORT_SYMBOL(rtnl_is_locked);
Patrick McHardyc9c10142008-04-23 22:10:48 -0700121
Paul E. McKenneya898def2010-02-22 17:04:49 -0800122#ifdef CONFIG_PROVE_LOCKING
Yaowei Bai0cbf3342015-10-08 21:29:02 +0800123bool lockdep_rtnl_is_held(void)
Paul E. McKenneya898def2010-02-22 17:04:49 -0800124{
125 return lockdep_is_held(&rtnl_mutex);
126}
127EXPORT_SYMBOL(lockdep_rtnl_is_held);
128#endif /* #ifdef CONFIG_PROVE_LOCKING */
129
Florian Westphal6853dd42017-08-09 20:41:51 +0200130static struct rtnl_link __rcu *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
Florian Westphal019a3162017-08-09 20:41:49 +0200131static refcount_t rtnl_msg_handlers_ref[RTNL_FAMILY_MAX + 1];
Thomas Grafe2849862007-03-22 11:48:11 -0700132
133static inline int rtm_msgindex(int msgtype)
134{
135 int msgindex = msgtype - RTM_BASE;
136
137 /*
138 * msgindex < 0 implies someone tried to register a netlink
139 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
140 * the message type has not been added to linux/rtnetlink.h
141 */
142 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
143
144 return msgindex;
145}
146
Thomas Grafe2849862007-03-22 11:48:11 -0700147/**
148 * __rtnl_register - Register a rtnetlink message type
149 * @protocol: Protocol family or PF_UNSPEC
150 * @msgtype: rtnetlink message type
151 * @doit: Function pointer called for each request message
152 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
Florian Westphalb97bac62017-08-09 20:41:48 +0200153 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
Thomas Grafe2849862007-03-22 11:48:11 -0700154 *
155 * Registers the specified function pointers (at least one of them has
156 * to be non-NULL) to be called whenever a request message for the
157 * specified protocol family and message type is received.
158 *
159 * The special protocol family PF_UNSPEC may be used to define fallback
160 * function pointers for the case when no entry for the specific protocol
161 * family exists.
162 *
163 * Returns 0 on success or a negative error code.
164 */
165int __rtnl_register(int protocol, int msgtype,
Greg Rosec7ac8672011-06-10 01:27:09 +0000166 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
Florian Westphalb97bac62017-08-09 20:41:48 +0200167 unsigned int flags)
Thomas Grafe2849862007-03-22 11:48:11 -0700168{
169 struct rtnl_link *tab;
170 int msgindex;
171
Patrick McHardy25239ce2010-04-26 16:02:05 +0200172 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700173 msgindex = rtm_msgindex(msgtype);
174
Florian Westphal377cb242017-08-10 16:52:57 +0200175 tab = rcu_dereference_raw(rtnl_msg_handlers[protocol]);
Thomas Grafe2849862007-03-22 11:48:11 -0700176 if (tab == NULL) {
177 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
178 if (tab == NULL)
179 return -ENOBUFS;
180
Florian Westphal6853dd42017-08-09 20:41:51 +0200181 rcu_assign_pointer(rtnl_msg_handlers[protocol], tab);
Thomas Grafe2849862007-03-22 11:48:11 -0700182 }
183
184 if (doit)
185 tab[msgindex].doit = doit;
Thomas Grafe2849862007-03-22 11:48:11 -0700186 if (dumpit)
187 tab[msgindex].dumpit = dumpit;
Florian Westphal62256f92017-08-09 20:41:52 +0200188 tab[msgindex].flags |= flags;
Thomas Grafe2849862007-03-22 11:48:11 -0700189
190 return 0;
191}
Thomas Grafe2849862007-03-22 11:48:11 -0700192EXPORT_SYMBOL_GPL(__rtnl_register);
193
194/**
195 * rtnl_register - Register a rtnetlink message type
196 *
197 * Identical to __rtnl_register() but panics on failure. This is useful
198 * as failure of this function is very unlikely, it can only happen due
199 * to lack of memory when allocating the chain to store all message
200 * handlers for a protocol. Meant for use in init functions where lack
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300201 * of memory implies no sense in continuing.
Thomas Grafe2849862007-03-22 11:48:11 -0700202 */
203void rtnl_register(int protocol, int msgtype,
Greg Rosec7ac8672011-06-10 01:27:09 +0000204 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
Florian Westphalb97bac62017-08-09 20:41:48 +0200205 unsigned int flags)
Thomas Grafe2849862007-03-22 11:48:11 -0700206{
Florian Westphalb97bac62017-08-09 20:41:48 +0200207 if (__rtnl_register(protocol, msgtype, doit, dumpit, flags) < 0)
Thomas Grafe2849862007-03-22 11:48:11 -0700208 panic("Unable to register rtnetlink message handler, "
209 "protocol = %d, message type = %d\n",
210 protocol, msgtype);
211}
Thomas Grafe2849862007-03-22 11:48:11 -0700212EXPORT_SYMBOL_GPL(rtnl_register);
213
214/**
215 * rtnl_unregister - Unregister a rtnetlink message type
216 * @protocol: Protocol family or PF_UNSPEC
217 * @msgtype: rtnetlink message type
218 *
219 * Returns 0 on success or a negative error code.
220 */
221int rtnl_unregister(int protocol, int msgtype)
222{
Florian Westphal6853dd42017-08-09 20:41:51 +0200223 struct rtnl_link *handlers;
Thomas Grafe2849862007-03-22 11:48:11 -0700224 int msgindex;
225
Patrick McHardy25239ce2010-04-26 16:02:05 +0200226 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700227 msgindex = rtm_msgindex(msgtype);
228
Florian Westphal6853dd42017-08-09 20:41:51 +0200229 rtnl_lock();
230 handlers = rtnl_dereference(rtnl_msg_handlers[protocol]);
231 if (!handlers) {
232 rtnl_unlock();
Thomas Grafe2849862007-03-22 11:48:11 -0700233 return -ENOENT;
Florian Westphal6853dd42017-08-09 20:41:51 +0200234 }
Thomas Grafe2849862007-03-22 11:48:11 -0700235
Florian Westphal6853dd42017-08-09 20:41:51 +0200236 handlers[msgindex].doit = NULL;
237 handlers[msgindex].dumpit = NULL;
Florian Westphal62256f92017-08-09 20:41:52 +0200238 handlers[msgindex].flags = 0;
Florian Westphal6853dd42017-08-09 20:41:51 +0200239 rtnl_unlock();
Thomas Grafe2849862007-03-22 11:48:11 -0700240
241 return 0;
242}
Thomas Grafe2849862007-03-22 11:48:11 -0700243EXPORT_SYMBOL_GPL(rtnl_unregister);
244
245/**
246 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
247 * @protocol : Protocol family or PF_UNSPEC
248 *
249 * Identical to calling rtnl_unregster() for all registered message types
250 * of a certain protocol family.
251 */
252void rtnl_unregister_all(int protocol)
253{
Florian Westphal019a3162017-08-09 20:41:49 +0200254 struct rtnl_link *handlers;
255
Patrick McHardy25239ce2010-04-26 16:02:05 +0200256 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700257
Florian Westphal019a3162017-08-09 20:41:49 +0200258 rtnl_lock();
Florian Westphal6853dd42017-08-09 20:41:51 +0200259 handlers = rtnl_dereference(rtnl_msg_handlers[protocol]);
260 RCU_INIT_POINTER(rtnl_msg_handlers[protocol], NULL);
Florian Westphal019a3162017-08-09 20:41:49 +0200261 rtnl_unlock();
262
Florian Westphal6853dd42017-08-09 20:41:51 +0200263 synchronize_net();
264
Florian Westphald38a6512017-08-10 16:53:00 +0200265 while (refcount_read(&rtnl_msg_handlers_ref[protocol]) > 1)
Florian Westphal019a3162017-08-09 20:41:49 +0200266 schedule();
267 kfree(handlers);
Thomas Grafe2849862007-03-22 11:48:11 -0700268}
Thomas Grafe2849862007-03-22 11:48:11 -0700269EXPORT_SYMBOL_GPL(rtnl_unregister_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Patrick McHardy38f7b872007-06-13 12:03:51 -0700271static LIST_HEAD(link_ops);
272
Eric Dumazetc63044f2011-12-13 11:38:00 +0000273static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
274{
275 const struct rtnl_link_ops *ops;
276
277 list_for_each_entry(ops, &link_ops, list) {
278 if (!strcmp(ops->kind, kind))
279 return ops;
280 }
281 return NULL;
282}
283
Patrick McHardy38f7b872007-06-13 12:03:51 -0700284/**
285 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
286 * @ops: struct rtnl_link_ops * to register
287 *
288 * The caller must hold the rtnl_mutex. This function should be used
289 * by drivers that create devices during module initialization. It
290 * must be called before registering the devices.
291 *
292 * Returns 0 on success or a negative error code.
293 */
294int __rtnl_link_register(struct rtnl_link_ops *ops)
295{
Eric Dumazetc63044f2011-12-13 11:38:00 +0000296 if (rtnl_link_ops_get(ops->kind))
297 return -EEXIST;
298
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +0200299 /* The check for setup is here because if ops
300 * does not have that filled up, it is not possible
301 * to use the ops for creating device. So do not
302 * fill up dellink as well. That disables rtnl_dellink.
303 */
304 if (ops->setup && !ops->dellink)
Eric Dumazet23289a32009-10-27 07:06:36 +0000305 ops->dellink = unregister_netdevice_queue;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700306
Patrick McHardy38f7b872007-06-13 12:03:51 -0700307 list_add_tail(&ops->list, &link_ops);
308 return 0;
309}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700310EXPORT_SYMBOL_GPL(__rtnl_link_register);
311
312/**
313 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
314 * @ops: struct rtnl_link_ops * to register
315 *
316 * Returns 0 on success or a negative error code.
317 */
318int rtnl_link_register(struct rtnl_link_ops *ops)
319{
320 int err;
321
322 rtnl_lock();
323 err = __rtnl_link_register(ops);
324 rtnl_unlock();
325 return err;
326}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700327EXPORT_SYMBOL_GPL(rtnl_link_register);
328
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700329static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
330{
331 struct net_device *dev;
Eric Dumazet23289a32009-10-27 07:06:36 +0000332 LIST_HEAD(list_kill);
333
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700334 for_each_netdev(net, dev) {
Eric Dumazet23289a32009-10-27 07:06:36 +0000335 if (dev->rtnl_link_ops == ops)
336 ops->dellink(dev, &list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700337 }
Eric Dumazet23289a32009-10-27 07:06:36 +0000338 unregister_netdevice_many(&list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700339}
340
Patrick McHardy38f7b872007-06-13 12:03:51 -0700341/**
342 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
343 * @ops: struct rtnl_link_ops * to unregister
344 *
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700345 * The caller must hold the rtnl_mutex.
Patrick McHardy38f7b872007-06-13 12:03:51 -0700346 */
347void __rtnl_link_unregister(struct rtnl_link_ops *ops)
348{
Eric W. Biederman881d9662007-09-17 11:56:21 -0700349 struct net *net;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700350
Eric W. Biederman881d9662007-09-17 11:56:21 -0700351 for_each_net(net) {
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700352 __rtnl_kill_links(net, ops);
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700353 }
Patrick McHardy38f7b872007-06-13 12:03:51 -0700354 list_del(&ops->list);
355}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700356EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
357
Cong Wang200b9162014-05-12 15:11:20 -0700358/* Return with the rtnl_lock held when there are no network
359 * devices unregistering in any network namespace.
360 */
361static void rtnl_lock_unregistering_all(void)
362{
363 struct net *net;
364 bool unregistering;
Peter Zijlstraff960a72014-10-29 17:04:56 +0100365 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Cong Wang200b9162014-05-12 15:11:20 -0700366
Peter Zijlstraff960a72014-10-29 17:04:56 +0100367 add_wait_queue(&netdev_unregistering_wq, &wait);
Cong Wang200b9162014-05-12 15:11:20 -0700368 for (;;) {
Cong Wang200b9162014-05-12 15:11:20 -0700369 unregistering = false;
370 rtnl_lock();
371 for_each_net(net) {
372 if (net->dev_unreg_count > 0) {
373 unregistering = true;
374 break;
375 }
376 }
377 if (!unregistering)
378 break;
379 __rtnl_unlock();
Peter Zijlstraff960a72014-10-29 17:04:56 +0100380
381 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Cong Wang200b9162014-05-12 15:11:20 -0700382 }
Peter Zijlstraff960a72014-10-29 17:04:56 +0100383 remove_wait_queue(&netdev_unregistering_wq, &wait);
Cong Wang200b9162014-05-12 15:11:20 -0700384}
385
Patrick McHardy38f7b872007-06-13 12:03:51 -0700386/**
387 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
388 * @ops: struct rtnl_link_ops * to unregister
389 */
390void rtnl_link_unregister(struct rtnl_link_ops *ops)
391{
Cong Wang200b9162014-05-12 15:11:20 -0700392 /* Close the race with cleanup_net() */
393 mutex_lock(&net_mutex);
394 rtnl_lock_unregistering_all();
Patrick McHardy38f7b872007-06-13 12:03:51 -0700395 __rtnl_link_unregister(ops);
396 rtnl_unlock();
Cong Wang200b9162014-05-12 15:11:20 -0700397 mutex_unlock(&net_mutex);
Patrick McHardy38f7b872007-06-13 12:03:51 -0700398}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700399EXPORT_SYMBOL_GPL(rtnl_link_unregister);
400
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100401static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
402{
403 struct net_device *master_dev;
404 const struct rtnl_link_ops *ops;
Florian Westphal8515ae32017-08-10 16:52:59 +0200405 size_t size = 0;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100406
Florian Westphal8515ae32017-08-10 16:52:59 +0200407 rcu_read_lock();
408
409 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100410 if (!master_dev)
Florian Westphal8515ae32017-08-10 16:52:59 +0200411 goto out;
412
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100413 ops = master_dev->rtnl_link_ops;
Fernando Luis Vazquez Cao6049f252014-02-04 19:35:02 +0900414 if (!ops || !ops->get_slave_size)
Florian Westphal8515ae32017-08-10 16:52:59 +0200415 goto out;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100416 /* IFLA_INFO_SLAVE_DATA + nested data */
Florian Westphal8515ae32017-08-10 16:52:59 +0200417 size = nla_total_size(sizeof(struct nlattr)) +
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100418 ops->get_slave_size(master_dev, dev);
Florian Westphal8515ae32017-08-10 16:52:59 +0200419
420out:
421 rcu_read_unlock();
422 return size;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100423}
424
Patrick McHardy38f7b872007-06-13 12:03:51 -0700425static size_t rtnl_link_get_size(const struct net_device *dev)
426{
427 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
428 size_t size;
429
430 if (!ops)
431 return 0;
432
Thomas Graf369cf772010-11-11 15:47:59 +0000433 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
434 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700435
436 if (ops->get_size)
437 /* IFLA_INFO_DATA + nested data */
Thomas Graf369cf772010-11-11 15:47:59 +0000438 size += nla_total_size(sizeof(struct nlattr)) +
Patrick McHardy38f7b872007-06-13 12:03:51 -0700439 ops->get_size(dev);
440
441 if (ops->get_xstats_size)
Thomas Graf369cf772010-11-11 15:47:59 +0000442 /* IFLA_INFO_XSTATS */
443 size += nla_total_size(ops->get_xstats_size(dev));
Patrick McHardy38f7b872007-06-13 12:03:51 -0700444
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100445 size += rtnl_link_get_slave_info_data_size(dev);
446
Patrick McHardy38f7b872007-06-13 12:03:51 -0700447 return size;
448}
449
Thomas Graff8ff1822010-11-16 04:30:14 +0000450static LIST_HEAD(rtnl_af_ops);
451
452static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
453{
454 const struct rtnl_af_ops *ops;
455
456 list_for_each_entry(ops, &rtnl_af_ops, list) {
457 if (ops->family == family)
458 return ops;
459 }
460
461 return NULL;
462}
463
464/**
Thomas Graff8ff1822010-11-16 04:30:14 +0000465 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
466 * @ops: struct rtnl_af_ops * to register
467 *
468 * Returns 0 on success or a negative error code.
469 */
stephen hemminger3678a9d2013-12-30 10:41:32 -0800470void rtnl_af_register(struct rtnl_af_ops *ops)
Thomas Graff8ff1822010-11-16 04:30:14 +0000471{
Thomas Graff8ff1822010-11-16 04:30:14 +0000472 rtnl_lock();
stephen hemminger3678a9d2013-12-30 10:41:32 -0800473 list_add_tail(&ops->list, &rtnl_af_ops);
Thomas Graff8ff1822010-11-16 04:30:14 +0000474 rtnl_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +0000475}
476EXPORT_SYMBOL_GPL(rtnl_af_register);
477
478/**
479 * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
480 * @ops: struct rtnl_af_ops * to unregister
481 *
482 * The caller must hold the rtnl_mutex.
483 */
484void __rtnl_af_unregister(struct rtnl_af_ops *ops)
485{
486 list_del(&ops->list);
487}
488EXPORT_SYMBOL_GPL(__rtnl_af_unregister);
489
490/**
491 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
492 * @ops: struct rtnl_af_ops * to unregister
493 */
494void rtnl_af_unregister(struct rtnl_af_ops *ops)
495{
496 rtnl_lock();
497 __rtnl_af_unregister(ops);
498 rtnl_unlock();
499}
500EXPORT_SYMBOL_GPL(rtnl_af_unregister);
501
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700502static size_t rtnl_link_get_af_size(const struct net_device *dev,
503 u32 ext_filter_mask)
Thomas Graff8ff1822010-11-16 04:30:14 +0000504{
505 struct rtnl_af_ops *af_ops;
506 size_t size;
507
508 /* IFLA_AF_SPEC */
509 size = nla_total_size(sizeof(struct nlattr));
510
511 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
512 if (af_ops->get_link_af_size) {
513 /* AF_* + nested data */
514 size += nla_total_size(sizeof(struct nlattr)) +
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700515 af_ops->get_link_af_size(dev, ext_filter_mask);
Thomas Graff8ff1822010-11-16 04:30:14 +0000516 }
517 }
518
519 return size;
520}
521
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100522static bool rtnl_have_link_slave_info(const struct net_device *dev)
523{
524 struct net_device *master_dev;
525
526 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
Jiri Pirko813f0202014-01-23 19:19:21 +0100527 if (master_dev && master_dev->rtnl_link_ops)
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100528 return true;
529 return false;
530}
531
532static int rtnl_link_slave_info_fill(struct sk_buff *skb,
533 const struct net_device *dev)
534{
535 struct net_device *master_dev;
536 const struct rtnl_link_ops *ops;
537 struct nlattr *slave_data;
538 int err;
539
540 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
541 if (!master_dev)
542 return 0;
543 ops = master_dev->rtnl_link_ops;
544 if (!ops)
545 return 0;
546 if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
547 return -EMSGSIZE;
548 if (ops->fill_slave_info) {
549 slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA);
550 if (!slave_data)
551 return -EMSGSIZE;
552 err = ops->fill_slave_info(skb, master_dev, dev);
553 if (err < 0)
554 goto err_cancel_slave_data;
555 nla_nest_end(skb, slave_data);
556 }
557 return 0;
558
559err_cancel_slave_data:
560 nla_nest_cancel(skb, slave_data);
561 return err;
562}
563
564static int rtnl_link_info_fill(struct sk_buff *skb,
565 const struct net_device *dev)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700566{
567 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100568 struct nlattr *data;
569 int err;
570
571 if (!ops)
572 return 0;
573 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
574 return -EMSGSIZE;
575 if (ops->fill_xstats) {
576 err = ops->fill_xstats(skb, dev);
577 if (err < 0)
578 return err;
579 }
580 if (ops->fill_info) {
581 data = nla_nest_start(skb, IFLA_INFO_DATA);
582 if (data == NULL)
583 return -EMSGSIZE;
584 err = ops->fill_info(skb, dev);
585 if (err < 0)
586 goto err_cancel_data;
587 nla_nest_end(skb, data);
588 }
589 return 0;
590
591err_cancel_data:
592 nla_nest_cancel(skb, data);
593 return err;
594}
595
596static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
597{
598 struct nlattr *linkinfo;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700599 int err = -EMSGSIZE;
600
601 linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
602 if (linkinfo == NULL)
603 goto out;
604
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100605 err = rtnl_link_info_fill(skb, dev);
606 if (err < 0)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700607 goto err_cancel_link;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100608
609 err = rtnl_link_slave_info_fill(skb, dev);
610 if (err < 0)
611 goto err_cancel_link;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700612
613 nla_nest_end(skb, linkinfo);
614 return 0;
615
Patrick McHardy38f7b872007-06-13 12:03:51 -0700616err_cancel_link:
617 nla_nest_cancel(skb, linkinfo);
618out:
619 return err;
620}
621
Eric Dumazet95c96172012-04-15 05:58:06 +0000622int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800624 struct sock *rtnl = net->rtnl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 int err = 0;
626
Patrick McHardyac6d4392005-08-14 19:29:52 -0700627 NETLINK_CB(skb).dst_group = group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if (echo)
Reshetova, Elena63354792017-06-30 13:07:58 +0300629 refcount_inc(&skb->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
631 if (echo)
632 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
633 return err;
634}
635
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800636int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
Thomas Graf2942e902006-08-15 00:30:25 -0700637{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800638 struct sock *rtnl = net->rtnl;
639
Thomas Graf2942e902006-08-15 00:30:25 -0700640 return nlmsg_unicast(rtnl, skb, pid);
641}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800642EXPORT_SYMBOL(rtnl_unicast);
Thomas Graf2942e902006-08-15 00:30:25 -0700643
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800644void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
645 struct nlmsghdr *nlh, gfp_t flags)
Thomas Graf97676b62006-08-15 00:31:41 -0700646{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800647 struct sock *rtnl = net->rtnl;
Thomas Graf97676b62006-08-15 00:31:41 -0700648 int report = 0;
649
650 if (nlh)
651 report = nlmsg_report(nlh);
652
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800653 nlmsg_notify(rtnl, skb, pid, group, report, flags);
Thomas Graf97676b62006-08-15 00:31:41 -0700654}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800655EXPORT_SYMBOL(rtnl_notify);
Thomas Graf97676b62006-08-15 00:31:41 -0700656
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800657void rtnl_set_sk_err(struct net *net, u32 group, int error)
Thomas Graf97676b62006-08-15 00:31:41 -0700658{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800659 struct sock *rtnl = net->rtnl;
660
Thomas Graf97676b62006-08-15 00:31:41 -0700661 netlink_set_err(rtnl, 0, group, error);
662}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800663EXPORT_SYMBOL(rtnl_set_sk_err);
Thomas Graf97676b62006-08-15 00:31:41 -0700664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
666{
Thomas Graf2d7202b2006-08-22 00:01:27 -0700667 struct nlattr *mx;
668 int i, valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Thomas Graf2d7202b2006-08-22 00:01:27 -0700670 mx = nla_nest_start(skb, RTA_METRICS);
671 if (mx == NULL)
672 return -ENOBUFS;
673
674 for (i = 0; i < RTAX_MAX; i++) {
675 if (metrics[i]) {
Daniel Borkmannea697632015-01-05 23:57:47 +0100676 if (i == RTAX_CC_ALGO - 1) {
677 char tmp[TCP_CA_NAME_MAX], *name;
678
679 name = tcp_ca_get_name_by_key(metrics[i], tmp);
680 if (!name)
681 continue;
682 if (nla_put_string(skb, i + 1, name))
683 goto nla_put_failure;
Daniel Borkmannc3a8d942015-08-31 15:58:47 +0200684 } else if (i == RTAX_FEATURES - 1) {
685 u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
686
Phil Sutterf8edcd12016-08-23 13:14:31 +0200687 if (!user_features)
688 continue;
Daniel Borkmannc3a8d942015-08-31 15:58:47 +0200689 BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
690 if (nla_put_u32(skb, i + 1, user_features))
691 goto nla_put_failure;
Daniel Borkmannea697632015-01-05 23:57:47 +0100692 } else {
693 if (nla_put_u32(skb, i + 1, metrics[i]))
694 goto nla_put_failure;
695 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700696 valid++;
Thomas Graf2d7202b2006-08-22 00:01:27 -0700697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
David S. Millera57d27f2006-08-22 22:20:14 -0700700 if (!valid) {
701 nla_nest_cancel(skb, mx);
702 return 0;
703 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700704
705 return nla_nest_end(skb, mx);
706
707nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700708 nla_nest_cancel(skb, mx);
709 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800711EXPORT_SYMBOL(rtnetlink_put_metrics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Thomas Grafe3703b32006-11-27 09:27:07 -0800713int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
David S. Miller87a50692012-07-10 05:06:14 -0700714 long expires, u32 error)
Thomas Grafe3703b32006-11-27 09:27:07 -0800715{
716 struct rta_cacheinfo ci = {
Eric Dumazeta399a802012-08-08 21:13:53 +0000717 .rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse),
Thomas Grafe3703b32006-11-27 09:27:07 -0800718 .rta_used = dst->__use,
719 .rta_clntref = atomic_read(&(dst->__refcnt)),
720 .rta_error = error,
721 .rta_id = id,
Thomas Grafe3703b32006-11-27 09:27:07 -0800722 };
723
Li Wei82539472012-07-29 16:01:30 +0000724 if (expires) {
725 unsigned long clock;
Thomas Grafe3703b32006-11-27 09:27:07 -0800726
Li Wei82539472012-07-29 16:01:30 +0000727 clock = jiffies_to_clock_t(abs(expires));
728 clock = min_t(unsigned long, clock, INT_MAX);
729 ci.rta_expires = (expires > 0) ? clock : -clock;
730 }
Thomas Grafe3703b32006-11-27 09:27:07 -0800731 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
732}
Thomas Grafe3703b32006-11-27 09:27:07 -0800733EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
David S. Miller93b2d4a2008-02-17 18:35:07 -0800735static void set_operstate(struct net_device *dev, unsigned char transition)
Stefan Rompfb00055a2006-03-20 17:09:11 -0800736{
737 unsigned char operstate = dev->operstate;
738
Eric Dumazete0d087a2009-11-07 01:26:17 -0800739 switch (transition) {
Stefan Rompfb00055a2006-03-20 17:09:11 -0800740 case IF_OPER_UP:
741 if ((operstate == IF_OPER_DORMANT ||
742 operstate == IF_OPER_UNKNOWN) &&
743 !netif_dormant(dev))
744 operstate = IF_OPER_UP;
745 break;
746
747 case IF_OPER_DORMANT:
748 if (operstate == IF_OPER_UP ||
749 operstate == IF_OPER_UNKNOWN)
750 operstate = IF_OPER_DORMANT;
751 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700752 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800753
754 if (dev->operstate != operstate) {
755 write_lock_bh(&dev_base_lock);
756 dev->operstate = operstate;
757 write_unlock_bh(&dev_base_lock);
David S. Miller93b2d4a2008-02-17 18:35:07 -0800758 netdev_state_change(dev);
759 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800760}
761
Jiri Bencb1beb682012-07-27 02:58:22 +0000762static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
763{
764 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
765 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
766}
767
Patrick McHardy3729d502010-02-26 06:34:54 +0000768static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
769 const struct ifinfomsg *ifm)
770{
771 unsigned int flags = ifm->ifi_flags;
772
773 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
774 if (ifm->ifi_change)
775 flags = (flags & ifm->ifi_change) |
Jiri Bencb1beb682012-07-27 02:58:22 +0000776 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
Patrick McHardy3729d502010-02-26 06:34:54 +0000777
778 return flags;
779}
780
Thomas Grafb60c5112006-08-04 23:05:34 -0700781static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000782 const struct rtnl_link_stats64 *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
Thomas Grafb60c5112006-08-04 23:05:34 -0700784 a->rx_packets = b->rx_packets;
785 a->tx_packets = b->tx_packets;
786 a->rx_bytes = b->rx_bytes;
787 a->tx_bytes = b->tx_bytes;
788 a->rx_errors = b->rx_errors;
789 a->tx_errors = b->tx_errors;
790 a->rx_dropped = b->rx_dropped;
791 a->tx_dropped = b->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Thomas Grafb60c5112006-08-04 23:05:34 -0700793 a->multicast = b->multicast;
794 a->collisions = b->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Thomas Grafb60c5112006-08-04 23:05:34 -0700796 a->rx_length_errors = b->rx_length_errors;
797 a->rx_over_errors = b->rx_over_errors;
798 a->rx_crc_errors = b->rx_crc_errors;
799 a->rx_frame_errors = b->rx_frame_errors;
800 a->rx_fifo_errors = b->rx_fifo_errors;
801 a->rx_missed_errors = b->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Thomas Grafb60c5112006-08-04 23:05:34 -0700803 a->tx_aborted_errors = b->tx_aborted_errors;
804 a->tx_carrier_errors = b->tx_carrier_errors;
805 a->tx_fifo_errors = b->tx_fifo_errors;
806 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
807 a->tx_window_errors = b->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Thomas Grafb60c5112006-08-04 23:05:34 -0700809 a->rx_compressed = b->rx_compressed;
810 a->tx_compressed = b->tx_compressed;
Jarod Wilson6e7333d2016-02-01 18:51:05 -0500811
812 a->rx_nohandler = b->rx_nohandler;
Jan Engelhardt10708f32010-03-11 09:57:29 +0000813}
814
Chris Wrightc02db8c2010-05-16 01:05:45 -0700815/* All VF info */
Greg Rose115c9b82012-02-21 16:54:48 -0500816static inline int rtnl_vfinfo_size(const struct net_device *dev,
817 u32 ext_filter_mask)
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000818{
Phil Sutter9af15c32017-01-18 14:04:39 +0100819 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
Chris Wrightc02db8c2010-05-16 01:05:45 -0700820 int num_vfs = dev_num_vf(dev->dev.parent);
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100821 size_t size = nla_total_size(0);
Scott Feldman045de012010-05-28 03:42:43 -0700822 size += num_vfs *
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100823 (nla_total_size(0) +
824 nla_total_size(sizeof(struct ifla_vf_mac)) +
825 nla_total_size(sizeof(struct ifla_vf_vlan)) +
826 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
Moshe Shemesh79aab092016-09-22 12:11:15 +0300827 nla_total_size(MAX_VLAN_LIST_LEN *
828 sizeof(struct ifla_vf_vlan_info)) +
Sucheta Chakrabortyed616682014-05-22 09:59:05 -0400829 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100830 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
Jiri Benc945a3672014-08-08 16:44:32 +0200831 nla_total_size(sizeof(struct ifla_vf_rate)) +
Vlad Zolotarov01a3d792015-03-30 21:35:23 +0300832 nla_total_size(sizeof(struct ifla_vf_link_state)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300833 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100834 nla_total_size(0) + /* nest IFLA_VF_STATS */
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300835 /* IFLA_VF_STATS_RX_PACKETS */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200836 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300837 /* IFLA_VF_STATS_TX_PACKETS */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200838 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300839 /* IFLA_VF_STATS_RX_BYTES */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200840 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300841 /* IFLA_VF_STATS_TX_BYTES */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200842 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300843 /* IFLA_VF_STATS_BROADCAST */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200844 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300845 /* IFLA_VF_STATS_MULTICAST */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200846 nla_total_size_64bit(sizeof(__u64)) +
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +0000847 nla_total_size(sizeof(struct ifla_vf_trust)));
Chris Wrightc02db8c2010-05-16 01:05:45 -0700848 return size;
849 } else
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000850 return 0;
851}
852
David Gibsonc53864f2014-04-24 10:22:36 +1000853static size_t rtnl_port_size(const struct net_device *dev,
854 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -0700855{
856 size_t port_size = nla_total_size(4) /* PORT_VF */
857 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
Scott Feldman57b61082010-05-17 22:49:55 -0700858 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
859 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
860 + nla_total_size(1) /* PROT_VDP_REQUEST */
861 + nla_total_size(2); /* PORT_VDP_RESPONSE */
862 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
863 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
864 + port_size;
865 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
866 + port_size;
867
David Gibsonc53864f2014-04-24 10:22:36 +1000868 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
869 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -0700870 return 0;
871 if (dev_num_vf(dev->dev.parent))
872 return port_self_size + vf_ports_size +
873 vf_port_size * dev_num_vf(dev->dev.parent);
874 else
875 return port_self_size;
876}
877
David S. Millerb5cdae32017-04-18 15:36:58 -0400878static size_t rtnl_xdp_size(void)
Brenden Blancod1fdd912016-07-19 12:16:49 -0700879{
Sabrina Dubrocab3cfaa32016-11-15 11:16:35 +0100880 size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */
Martin KaFai Lau58038692017-06-15 17:29:09 -0700881 nla_total_size(1) + /* XDP_ATTACHED */
882 nla_total_size(4); /* XDP_PROG_ID */
Brenden Blancod1fdd912016-07-19 12:16:49 -0700883
David S. Millerb5cdae32017-04-18 15:36:58 -0400884 return xdp_size;
Brenden Blancod1fdd912016-07-19 12:16:49 -0700885}
886
Greg Rose115c9b82012-02-21 16:54:48 -0500887static noinline size_t if_nlmsg_size(const struct net_device *dev,
888 u32 ext_filter_mask)
Thomas Graf339bf982006-11-10 14:10:15 -0800889{
890 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
891 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700892 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
Thomas Graf339bf982006-11-10 14:10:15 -0800893 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
Nicolas Dichtel270cb4d2016-04-26 10:06:16 +0200894 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
Thomas Graf339bf982006-11-10 14:10:15 -0800895 + nla_total_size(sizeof(struct rtnl_link_stats))
David S. Miller35c58452016-04-19 19:49:29 -0400896 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
Thomas Graf339bf982006-11-10 14:10:15 -0800897 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
898 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
899 + nla_total_size(4) /* IFLA_TXQLEN */
900 + nla_total_size(4) /* IFLA_WEIGHT */
901 + nla_total_size(4) /* IFLA_MTU */
902 + nla_total_size(4) /* IFLA_LINK */
903 + nla_total_size(4) /* IFLA_MASTER */
Jiri Pirko9a572472012-12-27 23:49:39 +0000904 + nla_total_size(1) /* IFLA_CARRIER */
Ben Greearedbc0bb2012-03-29 12:51:30 +0000905 + nla_total_size(4) /* IFLA_PROMISCUITY */
Jiri Pirko76ff5cc2012-07-20 02:28:48 +0000906 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
907 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
Tobias Klauser69197562016-11-30 14:30:37 +0100908 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
909 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
Thomas Graf339bf982006-11-10 14:10:15 -0800910 + nla_total_size(1) /* IFLA_OPERSTATE */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700911 + nla_total_size(1) /* IFLA_LINKMODE */
david decotigny2d3b4792014-03-29 09:48:35 -0700912 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
Nicolas Dichteld37512a2015-01-15 15:11:16 +0100913 + nla_total_size(4) /* IFLA_LINK_NETNSID */
Serhey Popovychdb833d42017-06-20 14:35:23 +0300914 + nla_total_size(4) /* IFLA_GROUP */
Greg Rose115c9b82012-02-21 16:54:48 -0500915 + nla_total_size(ext_filter_mask
916 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
917 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
David Gibsonc53864f2014-04-24 10:22:36 +1000918 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
Thomas Graff8ff1822010-11-16 04:30:14 +0000919 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700920 + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
Jiri Pirko82f28412014-11-28 14:34:18 +0100921 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
Anuradha Karuppiah88d63782015-07-14 13:43:20 -0700922 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
Nicolas Dichtelc57c7a92016-03-31 18:10:31 +0200923 + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
David S. Millerb5cdae32017-04-18 15:36:58 -0400924 + rtnl_xdp_size() /* IFLA_XDP */
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -0400925 + nla_total_size(4) /* IFLA_EVENT */
Anuradha Karuppiah88d63782015-07-14 13:43:20 -0700926 + nla_total_size(1); /* IFLA_PROTO_DOWN */
927
Thomas Graf339bf982006-11-10 14:10:15 -0800928}
929
Scott Feldman57b61082010-05-17 22:49:55 -0700930static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
931{
932 struct nlattr *vf_ports;
933 struct nlattr *vf_port;
934 int vf;
935 int err;
936
937 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
938 if (!vf_ports)
939 return -EMSGSIZE;
940
941 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
942 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
Scott Feldman8ca94182010-05-28 03:42:18 -0700943 if (!vf_port)
944 goto nla_put_failure;
David S. Millera6574342012-04-01 20:12:00 -0400945 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
946 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -0700947 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
Scott Feldman8ca94182010-05-28 03:42:18 -0700948 if (err == -EMSGSIZE)
949 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -0700950 if (err) {
Scott Feldman57b61082010-05-17 22:49:55 -0700951 nla_nest_cancel(skb, vf_port);
952 continue;
953 }
954 nla_nest_end(skb, vf_port);
955 }
956
957 nla_nest_end(skb, vf_ports);
958
959 return 0;
Scott Feldman8ca94182010-05-28 03:42:18 -0700960
961nla_put_failure:
962 nla_nest_cancel(skb, vf_ports);
963 return -EMSGSIZE;
Scott Feldman57b61082010-05-17 22:49:55 -0700964}
965
966static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
967{
968 struct nlattr *port_self;
969 int err;
970
971 port_self = nla_nest_start(skb, IFLA_PORT_SELF);
972 if (!port_self)
973 return -EMSGSIZE;
974
975 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
976 if (err) {
977 nla_nest_cancel(skb, port_self);
Scott Feldman8ca94182010-05-28 03:42:18 -0700978 return (err == -EMSGSIZE) ? err : 0;
Scott Feldman57b61082010-05-17 22:49:55 -0700979 }
980
981 nla_nest_end(skb, port_self);
982
983 return 0;
984}
985
David Gibsonc53864f2014-04-24 10:22:36 +1000986static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
987 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -0700988{
989 int err;
990
David Gibsonc53864f2014-04-24 10:22:36 +1000991 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
992 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -0700993 return 0;
994
995 err = rtnl_port_self_fill(skb, dev);
996 if (err)
997 return err;
998
999 if (dev_num_vf(dev->dev.parent)) {
1000 err = rtnl_vf_ports_fill(skb, dev);
1001 if (err)
1002 return err;
1003 }
1004
1005 return 0;
1006}
1007
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001008static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
1009{
1010 int err;
Jiri Pirko02637fc2014-11-28 14:34:16 +01001011 struct netdev_phys_item_id ppid;
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001012
1013 err = dev_get_phys_port_id(dev, &ppid);
1014 if (err) {
1015 if (err == -EOPNOTSUPP)
1016 return 0;
1017 return err;
1018 }
1019
1020 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1021 return -EMSGSIZE;
1022
1023 return 0;
1024}
1025
David Aherndb24a902015-03-17 20:23:15 -06001026static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1027{
1028 char name[IFNAMSIZ];
1029 int err;
1030
1031 err = dev_get_phys_port_name(dev, name, sizeof(name));
1032 if (err) {
1033 if (err == -EOPNOTSUPP)
1034 return 0;
1035 return err;
1036 }
1037
Michal Schmidt77ef0332017-05-04 16:48:58 +02001038 if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
David Aherndb24a902015-03-17 20:23:15 -06001039 return -EMSGSIZE;
1040
1041 return 0;
1042}
1043
Jiri Pirko82f28412014-11-28 14:34:18 +01001044static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1045{
1046 int err;
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001047 struct switchdev_attr attr = {
Ido Schimmel6ff64f62015-12-15 16:03:35 +01001048 .orig_dev = dev,
Jiri Pirko1f868392015-10-01 11:03:42 +02001049 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001050 .flags = SWITCHDEV_F_NO_RECURSE,
1051 };
Jiri Pirko82f28412014-11-28 14:34:18 +01001052
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001053 err = switchdev_port_attr_get(dev, &attr);
Jiri Pirko82f28412014-11-28 14:34:18 +01001054 if (err) {
1055 if (err == -EOPNOTSUPP)
1056 return 0;
1057 return err;
1058 }
1059
Scott Feldman42275bd2015-05-13 11:16:50 -07001060 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len,
1061 attr.u.ppid.id))
Jiri Pirko82f28412014-11-28 14:34:18 +01001062 return -EMSGSIZE;
1063
1064 return 0;
1065}
1066
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001067static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1068 struct net_device *dev)
1069{
Roopa Prabhu550bce52016-04-15 20:36:25 -07001070 struct rtnl_link_stats64 *sp;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001071 struct nlattr *attr;
1072
Nicolas Dichtel58414d32016-04-21 18:58:25 +02001073 attr = nla_reserve_64bit(skb, IFLA_STATS64,
1074 sizeof(struct rtnl_link_stats64), IFLA_PAD);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001075 if (!attr)
1076 return -EMSGSIZE;
1077
Roopa Prabhu550bce52016-04-15 20:36:25 -07001078 sp = nla_data(attr);
1079 dev_get_stats(dev, sp);
1080
1081 attr = nla_reserve(skb, IFLA_STATS,
1082 sizeof(struct rtnl_link_stats));
1083 if (!attr)
1084 return -EMSGSIZE;
1085
1086 copy_rtnl_link_stats(nla_data(attr), sp);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001087
1088 return 0;
1089}
1090
1091static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1092 struct net_device *dev,
1093 int vfs_num,
1094 struct nlattr *vfinfo)
1095{
1096 struct ifla_vf_rss_query_en vf_rss_query_en;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001097 struct nlattr *vf, *vfstats, *vfvlanlist;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001098 struct ifla_vf_link_state vf_linkstate;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001099 struct ifla_vf_vlan_info vf_vlan_info;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001100 struct ifla_vf_spoofchk vf_spoofchk;
1101 struct ifla_vf_tx_rate vf_tx_rate;
1102 struct ifla_vf_stats vf_stats;
1103 struct ifla_vf_trust vf_trust;
1104 struct ifla_vf_vlan vf_vlan;
1105 struct ifla_vf_rate vf_rate;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001106 struct ifla_vf_mac vf_mac;
1107 struct ifla_vf_info ivi;
1108
Mintz, Yuval0eed9cf2017-06-07 21:00:33 +03001109 memset(&ivi, 0, sizeof(ivi));
1110
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001111 /* Not all SR-IOV capable drivers support the
1112 * spoofcheck and "RSS query enable" query. Preset to
1113 * -1 so the user space tool can detect that the driver
1114 * didn't report anything.
1115 */
1116 ivi.spoofchk = -1;
1117 ivi.rss_query_en = -1;
1118 ivi.trusted = -1;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001119 /* The default value for VF link state is "auto"
1120 * IFLA_VF_LINK_STATE_AUTO which equals zero
1121 */
1122 ivi.linkstate = 0;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001123 /* VLAN Protocol by default is 802.1Q */
1124 ivi.vlan_proto = htons(ETH_P_8021Q);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001125 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1126 return 0;
1127
Dan Carpenter775f4f02016-10-13 11:45:28 +03001128 memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
1129
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001130 vf_mac.vf =
1131 vf_vlan.vf =
Moshe Shemesh79aab092016-09-22 12:11:15 +03001132 vf_vlan_info.vf =
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001133 vf_rate.vf =
1134 vf_tx_rate.vf =
1135 vf_spoofchk.vf =
1136 vf_linkstate.vf =
1137 vf_rss_query_en.vf =
1138 vf_trust.vf = ivi.vf;
1139
1140 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1141 vf_vlan.vlan = ivi.vlan;
1142 vf_vlan.qos = ivi.qos;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001143 vf_vlan_info.vlan = ivi.vlan;
1144 vf_vlan_info.qos = ivi.qos;
1145 vf_vlan_info.vlan_proto = ivi.vlan_proto;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001146 vf_tx_rate.rate = ivi.max_tx_rate;
1147 vf_rate.min_tx_rate = ivi.min_tx_rate;
1148 vf_rate.max_tx_rate = ivi.max_tx_rate;
1149 vf_spoofchk.setting = ivi.spoofchk;
1150 vf_linkstate.link_state = ivi.linkstate;
1151 vf_rss_query_en.setting = ivi.rss_query_en;
1152 vf_trust.setting = ivi.trusted;
1153 vf = nla_nest_start(skb, IFLA_VF_INFO);
Moshe Shemesh79aab092016-09-22 12:11:15 +03001154 if (!vf)
1155 goto nla_put_vfinfo_failure;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001156 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1157 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1158 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1159 &vf_rate) ||
1160 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1161 &vf_tx_rate) ||
1162 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1163 &vf_spoofchk) ||
1164 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1165 &vf_linkstate) ||
1166 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1167 sizeof(vf_rss_query_en),
1168 &vf_rss_query_en) ||
1169 nla_put(skb, IFLA_VF_TRUST,
1170 sizeof(vf_trust), &vf_trust))
Moshe Shemesh79aab092016-09-22 12:11:15 +03001171 goto nla_put_vf_failure;
1172 vfvlanlist = nla_nest_start(skb, IFLA_VF_VLAN_LIST);
1173 if (!vfvlanlist)
1174 goto nla_put_vf_failure;
1175 if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
1176 &vf_vlan_info)) {
1177 nla_nest_cancel(skb, vfvlanlist);
1178 goto nla_put_vf_failure;
1179 }
1180 nla_nest_end(skb, vfvlanlist);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001181 memset(&vf_stats, 0, sizeof(vf_stats));
1182 if (dev->netdev_ops->ndo_get_vf_stats)
1183 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1184 &vf_stats);
1185 vfstats = nla_nest_start(skb, IFLA_VF_STATS);
Moshe Shemesh79aab092016-09-22 12:11:15 +03001186 if (!vfstats)
1187 goto nla_put_vf_failure;
Nicolas Dichtel343a6d82016-04-25 10:25:14 +02001188 if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1189 vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1190 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1191 vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1192 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1193 vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1194 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1195 vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1196 nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1197 vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1198 nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
Moshe Shemesh79aab092016-09-22 12:11:15 +03001199 vf_stats.multicast, IFLA_VF_STATS_PAD)) {
1200 nla_nest_cancel(skb, vfstats);
1201 goto nla_put_vf_failure;
1202 }
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001203 nla_nest_end(skb, vfstats);
1204 nla_nest_end(skb, vf);
1205 return 0;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001206
1207nla_put_vf_failure:
1208 nla_nest_cancel(skb, vf);
1209nla_put_vfinfo_failure:
1210 nla_nest_cancel(skb, vfinfo);
1211 return -EMSGSIZE;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001212}
1213
1214static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1215{
Kangjie Lu5f8e4472016-05-03 16:46:24 -04001216 struct rtnl_link_ifmap map;
1217
1218 memset(&map, 0, sizeof(map));
1219 map.mem_start = dev->mem_start;
1220 map.mem_end = dev->mem_end;
1221 map.base_addr = dev->base_addr;
1222 map.irq = dev->irq;
1223 map.dma = dev->dma;
1224 map.port = dev->if_port;
1225
Nicolas Dichtel270cb4d2016-04-26 10:06:16 +02001226 if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001227 return -EMSGSIZE;
1228
1229 return 0;
1230}
1231
Martin KaFai Lau58038692017-06-15 17:29:09 -07001232static u8 rtnl_xdp_attached_mode(struct net_device *dev, u32 *prog_id)
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001233{
1234 const struct net_device_ops *ops = dev->netdev_ops;
Martin KaFai Lau58038692017-06-15 17:29:09 -07001235 const struct bpf_prog *generic_xdp_prog;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001236
1237 ASSERT_RTNL();
1238
Martin KaFai Lau58038692017-06-15 17:29:09 -07001239 *prog_id = 0;
1240 generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
1241 if (generic_xdp_prog) {
1242 *prog_id = generic_xdp_prog->aux->id;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001243 return XDP_ATTACHED_SKB;
Martin KaFai Lau58038692017-06-15 17:29:09 -07001244 }
Jakub Kicinskice158e52017-06-21 18:25:09 -07001245 if (!ops->ndo_xdp)
1246 return XDP_ATTACHED_NONE;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001247
Jakub Kicinskice158e52017-06-21 18:25:09 -07001248 return __dev_xdp_attached(dev, ops->ndo_xdp, prog_id);
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001249}
1250
Brenden Blancod1fdd912016-07-19 12:16:49 -07001251static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1252{
Brenden Blancod1fdd912016-07-19 12:16:49 -07001253 struct nlattr *xdp;
Martin KaFai Lau58038692017-06-15 17:29:09 -07001254 u32 prog_id;
Brenden Blancod1fdd912016-07-19 12:16:49 -07001255 int err;
1256
Brenden Blancod1fdd912016-07-19 12:16:49 -07001257 xdp = nla_nest_start(skb, IFLA_XDP);
1258 if (!xdp)
1259 return -EMSGSIZE;
David S. Millerb5cdae32017-04-18 15:36:58 -04001260
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001261 err = nla_put_u8(skb, IFLA_XDP_ATTACHED,
Martin KaFai Lau58038692017-06-15 17:29:09 -07001262 rtnl_xdp_attached_mode(dev, &prog_id));
Brenden Blancod1fdd912016-07-19 12:16:49 -07001263 if (err)
1264 goto err_cancel;
1265
Martin KaFai Lau58038692017-06-15 17:29:09 -07001266 if (prog_id) {
1267 err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
1268 if (err)
1269 goto err_cancel;
1270 }
1271
Brenden Blancod1fdd912016-07-19 12:16:49 -07001272 nla_nest_end(skb, xdp);
1273 return 0;
1274
1275err_cancel:
1276 nla_nest_cancel(skb, xdp);
1277 return err;
1278}
1279
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001280static u32 rtnl_get_event(unsigned long event)
1281{
1282 u32 rtnl_event_type = IFLA_EVENT_NONE;
1283
1284 switch (event) {
1285 case NETDEV_REBOOT:
1286 rtnl_event_type = IFLA_EVENT_REBOOT;
1287 break;
1288 case NETDEV_FEAT_CHANGE:
1289 rtnl_event_type = IFLA_EVENT_FEATURES;
1290 break;
1291 case NETDEV_BONDING_FAILOVER:
1292 rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
1293 break;
1294 case NETDEV_NOTIFY_PEERS:
1295 rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
1296 break;
1297 case NETDEV_RESEND_IGMP:
1298 rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
1299 break;
1300 case NETDEV_CHANGEINFODATA:
1301 rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
1302 break;
1303 default:
1304 break;
1305 }
1306
1307 return rtnl_event_type;
1308}
1309
Thomas Grafb60c5112006-08-04 23:05:34 -07001310static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
Patrick McHardy575c3e22007-05-22 17:00:49 -07001311 int type, u32 pid, u32 seq, u32 change,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001312 unsigned int flags, u32 ext_filter_mask,
1313 u32 event)
Thomas Grafb60c5112006-08-04 23:05:34 -07001314{
1315 struct ifinfomsg *ifm;
1316 struct nlmsghdr *nlh;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001317 struct nlattr *af_spec;
Thomas Graff8ff1822010-11-16 04:30:14 +00001318 struct rtnl_af_ops *af_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00001319 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Thomas Grafb60c5112006-08-04 23:05:34 -07001320
Eric Dumazet2907c352011-05-25 07:34:04 +00001321 ASSERT_RTNL();
Thomas Grafb60c5112006-08-04 23:05:34 -07001322 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1323 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08001324 return -EMSGSIZE;
Thomas Grafb60c5112006-08-04 23:05:34 -07001325
1326 ifm = nlmsg_data(nlh);
1327 ifm->ifi_family = AF_UNSPEC;
1328 ifm->__ifi_pad = 0;
1329 ifm->ifi_type = dev->type;
1330 ifm->ifi_index = dev->ifindex;
1331 ifm->ifi_flags = dev_get_flags(dev);
1332 ifm->ifi_change = change;
1333
David S. Millera6574342012-04-01 20:12:00 -04001334 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1335 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1336 nla_put_u8(skb, IFLA_OPERSTATE,
1337 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1338 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1339 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1340 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
Ben Greearedbc0bb2012-03-29 12:51:30 +00001341 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001342 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
Eric Dumazetc70ce022016-03-21 09:55:10 -07001343 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1344 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001345#ifdef CONFIG_RPS
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001346 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001347#endif
Nicolas Dichtela54acb32015-04-02 17:07:00 +02001348 (dev->ifindex != dev_get_iflink(dev) &&
1349 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00001350 (upper_dev &&
1351 nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
Jiri Pirko9a572472012-12-27 23:49:39 +00001352 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
David S. Millera6574342012-04-01 20:12:00 -04001353 (dev->qdisc &&
1354 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1355 (dev->ifalias &&
david decotigny2d3b4792014-03-29 09:48:35 -07001356 nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) ||
1357 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001358 atomic_read(&dev->carrier_changes)) ||
1359 nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
David S. Millera6574342012-04-01 20:12:00 -04001360 goto nla_put_failure;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001361
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001362 if (event != IFLA_EVENT_NONE) {
1363 if (nla_put_u32(skb, IFLA_EVENT, event))
1364 goto nla_put_failure;
1365 }
1366
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001367 if (rtnl_fill_link_ifmap(skb, dev))
1368 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370 if (dev->addr_len) {
David S. Millera6574342012-04-01 20:12:00 -04001371 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1372 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1373 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 }
1375
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001376 if (rtnl_phys_port_id_fill(skb, dev))
1377 goto nla_put_failure;
1378
David Aherndb24a902015-03-17 20:23:15 -06001379 if (rtnl_phys_port_name_fill(skb, dev))
1380 goto nla_put_failure;
1381
Jiri Pirko82f28412014-11-28 14:34:18 +01001382 if (rtnl_phys_switch_id_fill(skb, dev))
1383 goto nla_put_failure;
1384
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001385 if (rtnl_fill_stats(skb, dev))
Pavel Emelyanov96e74082008-05-21 14:12:46 -07001386 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
David S. Millera6574342012-04-01 20:12:00 -04001388 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
1389 nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
1390 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001391
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001392 if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent &&
1393 ext_filter_mask & RTEXT_FILTER_VF) {
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001394 int i;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001395 struct nlattr *vfinfo;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001396 int num_vfs = dev_num_vf(dev->dev.parent);
1397
Chris Wrightc02db8c2010-05-16 01:05:45 -07001398 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1399 if (!vfinfo)
1400 goto nla_put_failure;
1401 for (i = 0; i < num_vfs; i++) {
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001402 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
Chris Wrightc02db8c2010-05-16 01:05:45 -07001403 goto nla_put_failure;
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001404 }
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001405
Chris Wrightc02db8c2010-05-16 01:05:45 -07001406 nla_nest_end(skb, vfinfo);
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001407 }
Scott Feldman57b61082010-05-17 22:49:55 -07001408
David Gibsonc53864f2014-04-24 10:22:36 +10001409 if (rtnl_port_fill(skb, dev, ext_filter_mask))
Scott Feldman57b61082010-05-17 22:49:55 -07001410 goto nla_put_failure;
1411
Brenden Blancod1fdd912016-07-19 12:16:49 -07001412 if (rtnl_xdp_fill(skb, dev))
1413 goto nla_put_failure;
1414
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001415 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07001416 if (rtnl_link_fill(skb, dev) < 0)
1417 goto nla_put_failure;
1418 }
1419
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001420 if (dev->rtnl_link_ops &&
1421 dev->rtnl_link_ops->get_link_net) {
1422 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1423
1424 if (!net_eq(dev_net(dev), link_net)) {
Nicolas Dichtel7a0877d2015-05-07 11:02:49 +02001425 int id = peernet2id_alloc(dev_net(dev), link_net);
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001426
1427 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1428 goto nla_put_failure;
1429 }
1430 }
1431
Thomas Graff8ff1822010-11-16 04:30:14 +00001432 if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1433 goto nla_put_failure;
1434
1435 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1436 if (af_ops->fill_link_af) {
1437 struct nlattr *af;
1438 int err;
1439
1440 if (!(af = nla_nest_start(skb, af_ops->family)))
1441 goto nla_put_failure;
1442
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04001443 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
Thomas Graff8ff1822010-11-16 04:30:14 +00001444
1445 /*
1446 * Caller may return ENODATA to indicate that there
1447 * was no data to be dumped. This is not an error, it
1448 * means we should trim the attribute header and
1449 * continue.
1450 */
1451 if (err == -ENODATA)
1452 nla_nest_cancel(skb, af);
1453 else if (err < 0)
1454 goto nla_put_failure;
1455
1456 nla_nest_end(skb, af);
1457 }
1458 }
1459
1460 nla_nest_end(skb, af_spec);
1461
Johannes Berg053c0952015-01-16 22:09:00 +01001462 nlmsg_end(skb, nlh);
1463 return 0;
Thomas Grafb60c5112006-08-04 23:05:34 -07001464
1465nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001466 nlmsg_cancel(skb, nlh);
1467 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468}
1469
Jiri Pirkof7b12602014-02-18 20:53:18 +01001470static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1471 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1472 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1473 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1474 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1475 [IFLA_MTU] = { .type = NLA_U32 },
1476 [IFLA_LINK] = { .type = NLA_U32 },
1477 [IFLA_MASTER] = { .type = NLA_U32 },
1478 [IFLA_CARRIER] = { .type = NLA_U8 },
1479 [IFLA_TXQLEN] = { .type = NLA_U32 },
1480 [IFLA_WEIGHT] = { .type = NLA_U32 },
1481 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1482 [IFLA_LINKMODE] = { .type = NLA_U8 },
1483 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1484 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1485 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
1486 [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 },
1487 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1488 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1489 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1490 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1491 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1492 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1493 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1494 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
Jiri Pirko02637fc2014-11-28 14:34:16 +01001495 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
david decotigny2d3b4792014-03-29 09:48:35 -07001496 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
Jiri Pirko82f28412014-11-28 14:34:18 +01001497 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
Nicolas Dichtel317f4812015-01-15 15:11:18 +01001498 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001499 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001500 [IFLA_XDP] = { .type = NLA_NESTED },
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001501 [IFLA_EVENT] = { .type = NLA_U32 },
Serhey Popovychdb833d42017-06-20 14:35:23 +03001502 [IFLA_GROUP] = { .type = NLA_U32 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001503};
1504
1505static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1506 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1507 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1508 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1509 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1510};
1511
Jiri Pirkof7b12602014-02-18 20:53:18 +01001512static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
Daniel Borkmann364d5712015-02-05 18:44:04 +01001513 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1514 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
Moshe Shemesh79aab092016-09-22 12:11:15 +03001515 [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED },
Daniel Borkmann364d5712015-02-05 18:44:04 +01001516 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1517 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1518 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1519 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001520 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001521 [IFLA_VF_STATS] = { .type = NLA_NESTED },
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00001522 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
Eli Cohencc8e27c2016-03-11 22:58:34 +02001523 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1524 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001525};
1526
Jiri Pirkof7b12602014-02-18 20:53:18 +01001527static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1528 [IFLA_PORT_VF] = { .type = NLA_U32 },
1529 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1530 .len = PORT_PROFILE_MAX },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001531 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1532 .len = PORT_UUID_MAX },
1533 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1534 .len = PORT_UUID_MAX },
1535 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1536 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
Daniel Borkmann025331d2017-02-17 01:56:11 +01001537
1538 /* Unused, but we need to keep it here since user space could
1539 * fill it. It's also broken with regard to NLA_BINARY use in
1540 * combination with structs.
1541 */
1542 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1543 .len = sizeof(struct ifla_port_vsi) },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001544};
1545
Brenden Blancod1fdd912016-07-19 12:16:49 -07001546static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
1547 [IFLA_XDP_FD] = { .type = NLA_S32 },
1548 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 },
Daniel Borkmann85de8572016-11-28 23:16:54 +01001549 [IFLA_XDP_FLAGS] = { .type = NLA_U32 },
Martin KaFai Lau58038692017-06-15 17:29:09 -07001550 [IFLA_XDP_PROG_ID] = { .type = NLA_U32 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001551};
1552
David Aherndc599f72016-02-02 08:17:07 -08001553static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1554{
1555 const struct rtnl_link_ops *ops = NULL;
1556 struct nlattr *linfo[IFLA_INFO_MAX + 1];
1557
Johannes Bergfceb6432017-04-12 14:34:07 +02001558 if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla,
1559 ifla_info_policy, NULL) < 0)
David Aherndc599f72016-02-02 08:17:07 -08001560 return NULL;
1561
1562 if (linfo[IFLA_INFO_KIND]) {
1563 char kind[MODULE_NAME_LEN];
1564
1565 nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1566 ops = rtnl_link_ops_get(kind);
1567 }
1568
1569 return ops;
1570}
1571
1572static bool link_master_filtered(struct net_device *dev, int master_idx)
1573{
1574 struct net_device *master;
1575
1576 if (!master_idx)
1577 return false;
1578
1579 master = netdev_master_upper_dev_get(dev);
1580 if (!master || master->ifindex != master_idx)
1581 return true;
1582
1583 return false;
1584}
1585
1586static bool link_kind_filtered(const struct net_device *dev,
1587 const struct rtnl_link_ops *kind_ops)
1588{
1589 if (kind_ops && dev->rtnl_link_ops != kind_ops)
1590 return true;
1591
1592 return false;
1593}
1594
1595static bool link_dump_filtered(struct net_device *dev,
1596 int master_idx,
1597 const struct rtnl_link_ops *kind_ops)
1598{
1599 if (link_master_filtered(dev, master_idx) ||
1600 link_kind_filtered(dev, kind_ops))
1601 return true;
1602
1603 return false;
1604}
1605
Thomas Grafb60c5112006-08-04 23:05:34 -07001606static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001608 struct net *net = sock_net(skb->sk);
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001609 int h, s_h;
1610 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 struct net_device *dev;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001612 struct hlist_head *head;
Greg Rose115c9b82012-02-21 16:54:48 -05001613 struct nlattr *tb[IFLA_MAX+1];
1614 u32 ext_filter_mask = 0;
David Aherndc599f72016-02-02 08:17:07 -08001615 const struct rtnl_link_ops *kind_ops = NULL;
1616 unsigned int flags = NLM_F_MULTI;
1617 int master_idx = 0;
David Gibson973462b2014-04-24 10:22:35 +10001618 int err;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001619 int hdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001621 s_h = cb->args[0];
1622 s_idx = cb->args[1];
1623
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001624 /* A hack to preserve kernel<->userspace interface.
1625 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1626 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1627 * what iproute2 < v3.9.0 used.
1628 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1629 * attribute, its netlink message is shorter than struct ifinfomsg.
1630 */
1631 hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1632 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1633
Johannes Bergfceb6432017-04-12 14:34:07 +02001634 if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX,
1635 ifla_policy, NULL) >= 0) {
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001636 if (tb[IFLA_EXT_MASK])
1637 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
David Aherndc599f72016-02-02 08:17:07 -08001638
1639 if (tb[IFLA_MASTER])
1640 master_idx = nla_get_u32(tb[IFLA_MASTER]);
1641
1642 if (tb[IFLA_LINKINFO])
1643 kind_ops = linkinfo_to_kind_ops(tb[IFLA_LINKINFO]);
1644
1645 if (master_idx || kind_ops)
1646 flags |= NLM_F_DUMP_FILTERED;
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001647 }
Greg Rose115c9b82012-02-21 16:54:48 -05001648
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001649 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1650 idx = 0;
1651 head = &net->dev_index_head[h];
Eric Dumazetcac5e652015-02-27 09:42:50 -08001652 hlist_for_each_entry(dev, head, index_hlist) {
David Aherndc599f72016-02-02 08:17:07 -08001653 if (link_dump_filtered(dev, master_idx, kind_ops))
Zhang Shengju3f0ae052016-11-19 23:28:32 +08001654 goto cont;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001655 if (idx < s_idx)
1656 goto cont;
David Gibson973462b2014-04-24 10:22:35 +10001657 err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
1658 NETLINK_CB(cb->skb).portid,
1659 cb->nlh->nlmsg_seq, 0,
David Aherndc599f72016-02-02 08:17:07 -08001660 flags,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001661 ext_filter_mask, 0);
David Gibson973462b2014-04-24 10:22:35 +10001662
David Ahernf6c57752017-05-15 23:19:17 -07001663 if (err < 0) {
1664 if (likely(skb->len))
1665 goto out;
1666
1667 goto out_err;
1668 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07001669cont:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001670 idx++;
1671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 }
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001673out:
David Ahernf6c57752017-05-15 23:19:17 -07001674 err = skb->len;
1675out_err:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001676 cb->args[1] = idx;
1677 cb->args[0] = h;
Jakub Sitnickid0225782017-08-09 17:39:12 +02001678 cb->seq = net->dev_base_seq;
1679 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
David Ahernf6c57752017-05-15 23:19:17 -07001681 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682}
1683
Johannes Bergfceb6432017-04-12 14:34:07 +02001684int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
1685 struct netlink_ext_ack *exterr)
Jiri Pirkof7b12602014-02-18 20:53:18 +01001686{
Johannes Bergfceb6432017-04-12 14:34:07 +02001687 return nla_parse(tb, IFLA_MAX, head, len, ifla_policy, exterr);
Jiri Pirkof7b12602014-02-18 20:53:18 +01001688}
1689EXPORT_SYMBOL(rtnl_nla_parse_ifla);
Scott Feldman57b61082010-05-17 22:49:55 -07001690
Eric W. Biederman81adee42009-11-08 00:53:51 -08001691struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1692{
1693 struct net *net;
1694 /* Examine the link attributes and figure out which
1695 * network namespace we are talking about.
1696 */
1697 if (tb[IFLA_NET_NS_PID])
1698 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001699 else if (tb[IFLA_NET_NS_FD])
1700 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
Eric W. Biederman81adee42009-11-08 00:53:51 -08001701 else
1702 net = get_net(src_net);
1703 return net;
1704}
1705EXPORT_SYMBOL(rtnl_link_get_net);
1706
Thomas Graf1840bb12008-02-23 19:54:36 -08001707static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1708{
1709 if (dev) {
1710 if (tb[IFLA_ADDRESS] &&
1711 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1712 return -EINVAL;
1713
1714 if (tb[IFLA_BROADCAST] &&
1715 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1716 return -EINVAL;
1717 }
1718
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001719 if (tb[IFLA_AF_SPEC]) {
1720 struct nlattr *af;
1721 int rem, err;
1722
1723 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1724 const struct rtnl_af_ops *af_ops;
1725
1726 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1727 return -EAFNOSUPPORT;
1728
1729 if (!af_ops->set_link_af)
1730 return -EOPNOTSUPP;
1731
1732 if (af_ops->validate_link_af) {
Kurt Van Dijck6d3a9a62011-01-26 04:55:24 +00001733 err = af_ops->validate_link_af(dev, af);
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001734 if (err < 0)
1735 return err;
1736 }
1737 }
1738 }
1739
Thomas Graf1840bb12008-02-23 19:54:36 -08001740 return 0;
1741}
1742
Eli Cohencc8e27c2016-03-11 22:58:34 +02001743static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
1744 int guid_type)
1745{
1746 const struct net_device_ops *ops = dev->netdev_ops;
1747
1748 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
1749}
1750
1751static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
1752{
1753 if (dev->type != ARPHRD_INFINIBAND)
1754 return -EOPNOTSUPP;
1755
1756 return handle_infiniband_guid(dev, ivt, guid_type);
1757}
1758
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001759static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
Chris Wrightc02db8c2010-05-16 01:05:45 -07001760{
Chris Wrightc02db8c2010-05-16 01:05:45 -07001761 const struct net_device_ops *ops = dev->netdev_ops;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001762 int err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001763
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001764 if (tb[IFLA_VF_MAC]) {
1765 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001766
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001767 err = -EOPNOTSUPP;
1768 if (ops->ndo_set_vf_mac)
1769 err = ops->ndo_set_vf_mac(dev, ivm->vf,
1770 ivm->mac);
1771 if (err < 0)
1772 return err;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001773 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001774
1775 if (tb[IFLA_VF_VLAN]) {
1776 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
1777
1778 err = -EOPNOTSUPP;
1779 if (ops->ndo_set_vf_vlan)
1780 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
Moshe Shemesh79aab092016-09-22 12:11:15 +03001781 ivv->qos,
1782 htons(ETH_P_8021Q));
1783 if (err < 0)
1784 return err;
1785 }
1786
1787 if (tb[IFLA_VF_VLAN_LIST]) {
1788 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
1789 struct nlattr *attr;
1790 int rem, len = 0;
1791
1792 err = -EOPNOTSUPP;
1793 if (!ops->ndo_set_vf_vlan)
1794 return err;
1795
1796 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
1797 if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
1798 nla_len(attr) < NLA_HDRLEN) {
1799 return -EINVAL;
1800 }
1801 if (len >= MAX_VLAN_LIST_LEN)
1802 return -EOPNOTSUPP;
1803 ivvl[len] = nla_data(attr);
1804
1805 len++;
1806 }
Arnd Bergmannfa34cd92016-09-30 18:13:49 +02001807 if (len == 0)
1808 return -EINVAL;
1809
Moshe Shemesh79aab092016-09-22 12:11:15 +03001810 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
1811 ivvl[0]->qos, ivvl[0]->vlan_proto);
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001812 if (err < 0)
1813 return err;
1814 }
1815
1816 if (tb[IFLA_VF_TX_RATE]) {
1817 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
1818 struct ifla_vf_info ivf;
1819
1820 err = -EOPNOTSUPP;
1821 if (ops->ndo_get_vf_config)
1822 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
1823 if (err < 0)
1824 return err;
1825
1826 err = -EOPNOTSUPP;
1827 if (ops->ndo_set_vf_rate)
1828 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1829 ivf.min_tx_rate,
1830 ivt->rate);
1831 if (err < 0)
1832 return err;
1833 }
1834
1835 if (tb[IFLA_VF_RATE]) {
1836 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
1837
1838 err = -EOPNOTSUPP;
1839 if (ops->ndo_set_vf_rate)
1840 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1841 ivt->min_tx_rate,
1842 ivt->max_tx_rate);
1843 if (err < 0)
1844 return err;
1845 }
1846
1847 if (tb[IFLA_VF_SPOOFCHK]) {
1848 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
1849
1850 err = -EOPNOTSUPP;
1851 if (ops->ndo_set_vf_spoofchk)
1852 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
1853 ivs->setting);
1854 if (err < 0)
1855 return err;
1856 }
1857
1858 if (tb[IFLA_VF_LINK_STATE]) {
1859 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
1860
1861 err = -EOPNOTSUPP;
1862 if (ops->ndo_set_vf_link_state)
1863 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
1864 ivl->link_state);
1865 if (err < 0)
1866 return err;
1867 }
1868
1869 if (tb[IFLA_VF_RSS_QUERY_EN]) {
1870 struct ifla_vf_rss_query_en *ivrssq_en;
1871
1872 err = -EOPNOTSUPP;
1873 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
1874 if (ops->ndo_set_vf_rss_query_en)
1875 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
1876 ivrssq_en->setting);
1877 if (err < 0)
1878 return err;
1879 }
1880
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00001881 if (tb[IFLA_VF_TRUST]) {
1882 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
1883
1884 err = -EOPNOTSUPP;
1885 if (ops->ndo_set_vf_trust)
1886 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
1887 if (err < 0)
1888 return err;
1889 }
1890
Eli Cohencc8e27c2016-03-11 22:58:34 +02001891 if (tb[IFLA_VF_IB_NODE_GUID]) {
1892 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
1893
1894 if (!ops->ndo_set_vf_guid)
1895 return -EOPNOTSUPP;
1896
1897 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
1898 }
1899
1900 if (tb[IFLA_VF_IB_PORT_GUID]) {
1901 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
1902
1903 if (!ops->ndo_set_vf_guid)
1904 return -EOPNOTSUPP;
1905
1906 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
1907 }
1908
Chris Wrightc02db8c2010-05-16 01:05:45 -07001909 return err;
1910}
1911
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001912static int do_set_master(struct net_device *dev, int ifindex)
1913{
Jiri Pirko898e5062013-01-03 22:48:52 +00001914 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001915 const struct net_device_ops *ops;
1916 int err;
1917
Jiri Pirko898e5062013-01-03 22:48:52 +00001918 if (upper_dev) {
1919 if (upper_dev->ifindex == ifindex)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001920 return 0;
Jiri Pirko898e5062013-01-03 22:48:52 +00001921 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001922 if (ops->ndo_del_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001923 err = ops->ndo_del_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001924 if (err)
1925 return err;
1926 } else {
1927 return -EOPNOTSUPP;
1928 }
1929 }
1930
1931 if (ifindex) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001932 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
1933 if (!upper_dev)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001934 return -EINVAL;
Jiri Pirko898e5062013-01-03 22:48:52 +00001935 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001936 if (ops->ndo_add_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001937 err = ops->ndo_add_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001938 if (err)
1939 return err;
1940 } else {
1941 return -EOPNOTSUPP;
1942 }
1943 }
1944 return 0;
1945}
1946
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001947#define DO_SETLINK_MODIFIED 0x01
Nicolas Dichtelba998902014-09-01 16:07:29 +02001948/* notify flag means notify + modified. */
1949#define DO_SETLINK_NOTIFY 0x03
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001950static int do_setlink(const struct sk_buff *skb,
1951 struct net_device *dev, struct ifinfomsg *ifm,
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07001952 struct netlink_ext_ack *extack,
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001953 struct nlattr **tb, char *ifname, int status)
Patrick McHardy0157f602007-06-13 12:03:36 -07001954{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001955 const struct net_device_ops *ops = dev->netdev_ops;
Patrick McHardy0157f602007-06-13 12:03:36 -07001956 int err;
1957
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001958 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
Eric W. Biederman81adee42009-11-08 00:53:51 -08001959 struct net *net = rtnl_link_get_net(dev_net(dev), tb);
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001960 if (IS_ERR(net)) {
1961 err = PTR_ERR(net);
1962 goto errout;
1963 }
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001964 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
Nicolas Dichtele0ebde02014-11-27 10:16:15 +01001965 put_net(net);
Eric W. Biedermanb51642f2012-11-16 03:03:11 +00001966 err = -EPERM;
1967 goto errout;
1968 }
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001969 err = dev_change_net_namespace(dev, net, ifname);
1970 put_net(net);
1971 if (err)
1972 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001973 status |= DO_SETLINK_MODIFIED;
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001974 }
1975
Patrick McHardy0157f602007-06-13 12:03:36 -07001976 if (tb[IFLA_MAP]) {
1977 struct rtnl_link_ifmap *u_map;
1978 struct ifmap k_map;
1979
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001980 if (!ops->ndo_set_config) {
Patrick McHardy0157f602007-06-13 12:03:36 -07001981 err = -EOPNOTSUPP;
1982 goto errout;
1983 }
1984
1985 if (!netif_device_present(dev)) {
1986 err = -ENODEV;
1987 goto errout;
1988 }
1989
1990 u_map = nla_data(tb[IFLA_MAP]);
1991 k_map.mem_start = (unsigned long) u_map->mem_start;
1992 k_map.mem_end = (unsigned long) u_map->mem_end;
1993 k_map.base_addr = (unsigned short) u_map->base_addr;
1994 k_map.irq = (unsigned char) u_map->irq;
1995 k_map.dma = (unsigned char) u_map->dma;
1996 k_map.port = (unsigned char) u_map->port;
1997
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001998 err = ops->ndo_set_config(dev, &k_map);
Patrick McHardy0157f602007-06-13 12:03:36 -07001999 if (err < 0)
2000 goto errout;
2001
Nicolas Dichtelba998902014-09-01 16:07:29 +02002002 status |= DO_SETLINK_NOTIFY;
Patrick McHardy0157f602007-06-13 12:03:36 -07002003 }
2004
2005 if (tb[IFLA_ADDRESS]) {
2006 struct sockaddr *sa;
2007 int len;
2008
WANG Cong153711f2017-07-20 11:27:57 -07002009 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2010 sizeof(*sa));
Patrick McHardy0157f602007-06-13 12:03:36 -07002011 sa = kmalloc(len, GFP_KERNEL);
2012 if (!sa) {
2013 err = -ENOMEM;
2014 goto errout;
2015 }
2016 sa->sa_family = dev->type;
2017 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
2018 dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00002019 err = dev_set_mac_address(dev, sa);
Patrick McHardy0157f602007-06-13 12:03:36 -07002020 kfree(sa);
2021 if (err)
2022 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002023 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002024 }
2025
2026 if (tb[IFLA_MTU]) {
2027 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
2028 if (err < 0)
2029 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002030 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002031 }
2032
Vlad Dogarucbda10f2011-01-13 23:38:30 +00002033 if (tb[IFLA_GROUP]) {
2034 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Nicolas Dichtelba998902014-09-01 16:07:29 +02002035 status |= DO_SETLINK_NOTIFY;
Vlad Dogarucbda10f2011-01-13 23:38:30 +00002036 }
2037
Patrick McHardy0157f602007-06-13 12:03:36 -07002038 /*
2039 * Interface selected by interface index but interface
2040 * name provided implies that a name change has been
2041 * requested.
2042 */
2043 if (ifm->ifi_index > 0 && ifname[0]) {
2044 err = dev_change_name(dev, ifname);
2045 if (err < 0)
2046 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002047 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002048 }
2049
Stephen Hemminger0b815a12008-09-22 21:28:11 -07002050 if (tb[IFLA_IFALIAS]) {
2051 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2052 nla_len(tb[IFLA_IFALIAS]));
2053 if (err < 0)
2054 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002055 status |= DO_SETLINK_NOTIFY;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07002056 }
2057
Patrick McHardy0157f602007-06-13 12:03:36 -07002058 if (tb[IFLA_BROADCAST]) {
2059 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00002060 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Patrick McHardy0157f602007-06-13 12:03:36 -07002061 }
2062
2063 if (ifm->ifi_flags || ifm->ifi_change) {
Patrick McHardy3729d502010-02-26 06:34:54 +00002064 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
Johannes Berg5f9021c2008-11-16 23:20:31 -08002065 if (err < 0)
2066 goto errout;
Patrick McHardy0157f602007-06-13 12:03:36 -07002067 }
2068
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002069 if (tb[IFLA_MASTER]) {
2070 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
2071 if (err)
2072 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002073 status |= DO_SETLINK_MODIFIED;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002074 }
2075
Jiri Pirko9a572472012-12-27 23:49:39 +00002076 if (tb[IFLA_CARRIER]) {
2077 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2078 if (err)
2079 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002080 status |= DO_SETLINK_MODIFIED;
Jiri Pirko9a572472012-12-27 23:49:39 +00002081 }
2082
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002083 if (tb[IFLA_TXQLEN]) {
Alexey Dobriyan0cd29502017-05-17 13:30:44 +03002084 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
2085 unsigned int orig_len = dev->tx_queue_len;
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002086
Jason Wang08294a22016-06-30 14:45:35 +08002087 if (dev->tx_queue_len ^ value) {
2088 dev->tx_queue_len = value;
2089 err = call_netdevice_notifiers(
2090 NETDEV_CHANGE_TX_QUEUE_LEN, dev);
2091 err = notifier_to_errno(err);
2092 if (err) {
2093 dev->tx_queue_len = orig_len;
2094 goto errout;
2095 }
Nicolas Dichtelba998902014-09-01 16:07:29 +02002096 status |= DO_SETLINK_NOTIFY;
Jason Wang08294a22016-06-30 14:45:35 +08002097 }
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002098 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002099
Patrick McHardy0157f602007-06-13 12:03:36 -07002100 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002101 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Patrick McHardy0157f602007-06-13 12:03:36 -07002102
2103 if (tb[IFLA_LINKMODE]) {
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002104 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2105
David S. Miller93b2d4a2008-02-17 18:35:07 -08002106 write_lock_bh(&dev_base_lock);
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002107 if (dev->link_mode ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02002108 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002109 dev->link_mode = value;
David S. Miller93b2d4a2008-02-17 18:35:07 -08002110 write_unlock_bh(&dev_base_lock);
Patrick McHardy0157f602007-06-13 12:03:36 -07002111 }
2112
Chris Wrightc02db8c2010-05-16 01:05:45 -07002113 if (tb[IFLA_VFINFO_LIST]) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002114 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
Chris Wrightc02db8c2010-05-16 01:05:45 -07002115 struct nlattr *attr;
2116 int rem;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002117
Chris Wrightc02db8c2010-05-16 01:05:45 -07002118 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002119 if (nla_type(attr) != IFLA_VF_INFO ||
2120 nla_len(attr) < NLA_HDRLEN) {
David Howells253683b2010-05-21 02:25:27 +00002121 err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002122 goto errout;
David Howells253683b2010-05-21 02:25:27 +00002123 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002124 err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr,
Johannes Bergfceb6432017-04-12 14:34:07 +02002125 ifla_vf_policy, NULL);
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002126 if (err < 0)
2127 goto errout;
2128 err = do_setvfinfo(dev, vfinfo);
Chris Wrightc02db8c2010-05-16 01:05:45 -07002129 if (err < 0)
2130 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002131 status |= DO_SETLINK_NOTIFY;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002132 }
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00002133 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002134 err = 0;
2135
Scott Feldman57b61082010-05-17 22:49:55 -07002136 if (tb[IFLA_VF_PORTS]) {
2137 struct nlattr *port[IFLA_PORT_MAX+1];
2138 struct nlattr *attr;
2139 int vf;
2140 int rem;
2141
2142 err = -EOPNOTSUPP;
2143 if (!ops->ndo_set_vf_port)
2144 goto errout;
2145
2146 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
Daniel Borkmann035d2102015-07-13 00:06:02 +02002147 if (nla_type(attr) != IFLA_VF_PORT ||
2148 nla_len(attr) < NLA_HDRLEN) {
2149 err = -EINVAL;
2150 goto errout;
2151 }
2152 err = nla_parse_nested(port, IFLA_PORT_MAX, attr,
Johannes Bergfceb6432017-04-12 14:34:07 +02002153 ifla_port_policy, NULL);
Scott Feldman57b61082010-05-17 22:49:55 -07002154 if (err < 0)
2155 goto errout;
2156 if (!port[IFLA_PORT_VF]) {
2157 err = -EOPNOTSUPP;
2158 goto errout;
2159 }
2160 vf = nla_get_u32(port[IFLA_PORT_VF]);
2161 err = ops->ndo_set_vf_port(dev, vf, port);
2162 if (err < 0)
2163 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002164 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07002165 }
2166 }
2167 err = 0;
2168
2169 if (tb[IFLA_PORT_SELF]) {
2170 struct nlattr *port[IFLA_PORT_MAX+1];
2171
2172 err = nla_parse_nested(port, IFLA_PORT_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +02002173 tb[IFLA_PORT_SELF], ifla_port_policy,
2174 NULL);
Scott Feldman57b61082010-05-17 22:49:55 -07002175 if (err < 0)
2176 goto errout;
2177
2178 err = -EOPNOTSUPP;
2179 if (ops->ndo_set_vf_port)
2180 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
2181 if (err < 0)
2182 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002183 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07002184 }
Thomas Graff8ff1822010-11-16 04:30:14 +00002185
2186 if (tb[IFLA_AF_SPEC]) {
2187 struct nlattr *af;
2188 int rem;
2189
2190 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2191 const struct rtnl_af_ops *af_ops;
2192
2193 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002194 BUG();
Thomas Graff8ff1822010-11-16 04:30:14 +00002195
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002196 err = af_ops->set_link_af(dev, af);
Thomas Graff8ff1822010-11-16 04:30:14 +00002197 if (err < 0)
2198 goto errout;
2199
Nicolas Dichtelba998902014-09-01 16:07:29 +02002200 status |= DO_SETLINK_NOTIFY;
Thomas Graff8ff1822010-11-16 04:30:14 +00002201 }
2202 }
Scott Feldman57b61082010-05-17 22:49:55 -07002203 err = 0;
2204
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07002205 if (tb[IFLA_PROTO_DOWN]) {
2206 err = dev_change_proto_down(dev,
2207 nla_get_u8(tb[IFLA_PROTO_DOWN]));
2208 if (err)
2209 goto errout;
2210 status |= DO_SETLINK_NOTIFY;
2211 }
2212
Brenden Blancod1fdd912016-07-19 12:16:49 -07002213 if (tb[IFLA_XDP]) {
2214 struct nlattr *xdp[IFLA_XDP_MAX + 1];
Daniel Borkmann85de8572016-11-28 23:16:54 +01002215 u32 xdp_flags = 0;
Brenden Blancod1fdd912016-07-19 12:16:49 -07002216
2217 err = nla_parse_nested(xdp, IFLA_XDP_MAX, tb[IFLA_XDP],
Johannes Bergfceb6432017-04-12 14:34:07 +02002218 ifla_xdp_policy, NULL);
Brenden Blancod1fdd912016-07-19 12:16:49 -07002219 if (err < 0)
2220 goto errout;
2221
Martin KaFai Lau58038692017-06-15 17:29:09 -07002222 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
Brenden Blanco262d8622016-07-20 17:22:34 -07002223 err = -EINVAL;
2224 goto errout;
2225 }
Daniel Borkmann85de8572016-11-28 23:16:54 +01002226
2227 if (xdp[IFLA_XDP_FLAGS]) {
2228 xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
2229 if (xdp_flags & ~XDP_FLAGS_MASK) {
2230 err = -EINVAL;
2231 goto errout;
2232 }
Jakub Kicinskiee5d0322017-06-21 18:25:04 -07002233 if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
Daniel Borkmann0489df92017-05-12 01:04:45 +02002234 err = -EINVAL;
2235 goto errout;
2236 }
Daniel Borkmann85de8572016-11-28 23:16:54 +01002237 }
2238
Brenden Blancod1fdd912016-07-19 12:16:49 -07002239 if (xdp[IFLA_XDP_FD]) {
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002240 err = dev_change_xdp_fd(dev, extack,
Daniel Borkmann85de8572016-11-28 23:16:54 +01002241 nla_get_s32(xdp[IFLA_XDP_FD]),
2242 xdp_flags);
Brenden Blancod1fdd912016-07-19 12:16:49 -07002243 if (err)
2244 goto errout;
2245 status |= DO_SETLINK_NOTIFY;
2246 }
2247 }
2248
Patrick McHardy0157f602007-06-13 12:03:36 -07002249errout:
Nicolas Dichtelba998902014-09-01 16:07:29 +02002250 if (status & DO_SETLINK_MODIFIED) {
2251 if (status & DO_SETLINK_NOTIFY)
2252 netdev_state_change(dev);
2253
2254 if (err < 0)
2255 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",
2256 dev->name);
2257 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002258
Patrick McHardy0157f602007-06-13 12:03:36 -07002259 return err;
2260}
2261
David Ahernc21ef3e2017-04-16 09:48:24 -07002262static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2263 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002265 struct net *net = sock_net(skb->sk);
Thomas Grafda5e0492006-08-10 21:17:37 -07002266 struct ifinfomsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 struct net_device *dev;
Patrick McHardy0157f602007-06-13 12:03:36 -07002268 int err;
Thomas Grafda5e0492006-08-10 21:17:37 -07002269 struct nlattr *tb[IFLA_MAX+1];
2270 char ifname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
David Ahernc21ef3e2017-04-16 09:48:24 -07002272 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy,
2273 extack);
Thomas Grafda5e0492006-08-10 21:17:37 -07002274 if (err < 0)
2275 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
Thomas Graf5176f912006-08-26 20:13:18 -07002277 if (tb[IFLA_IFNAME])
2278 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
Patrick McHardy78e5b8912006-09-13 20:35:36 -07002279 else
2280 ifname[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 err = -EINVAL;
Thomas Grafda5e0492006-08-10 21:17:37 -07002283 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -07002284 if (ifm->ifi_index > 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00002285 dev = __dev_get_by_index(net, ifm->ifi_index);
Thomas Grafda5e0492006-08-10 21:17:37 -07002286 else if (tb[IFLA_IFNAME])
Eric Dumazeta3d12892009-10-21 10:59:31 +00002287 dev = __dev_get_by_name(net, ifname);
Thomas Grafda5e0492006-08-10 21:17:37 -07002288 else
2289 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
Thomas Grafda5e0492006-08-10 21:17:37 -07002291 if (dev == NULL) {
2292 err = -ENODEV;
2293 goto errout;
2294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
Eric Dumazete0d087a2009-11-07 01:26:17 -08002296 err = validate_linkmsg(dev, tb);
2297 if (err < 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00002298 goto errout;
Thomas Grafda5e0492006-08-10 21:17:37 -07002299
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002300 err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
Thomas Grafda5e0492006-08-10 21:17:37 -07002301errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 return err;
2303}
2304
WANG Cong66400d52015-03-24 11:53:31 -07002305static int rtnl_group_dellink(const struct net *net, int group)
2306{
2307 struct net_device *dev, *aux;
2308 LIST_HEAD(list_kill);
2309 bool found = false;
2310
2311 if (!group)
2312 return -EPERM;
2313
2314 for_each_netdev(net, dev) {
2315 if (dev->group == group) {
2316 const struct rtnl_link_ops *ops;
2317
2318 found = true;
2319 ops = dev->rtnl_link_ops;
2320 if (!ops || !ops->dellink)
2321 return -EOPNOTSUPP;
2322 }
2323 }
2324
2325 if (!found)
2326 return -ENODEV;
2327
2328 for_each_netdev_safe(net, dev, aux) {
2329 if (dev->group == group) {
2330 const struct rtnl_link_ops *ops;
2331
2332 ops = dev->rtnl_link_ops;
2333 ops->dellink(dev, &list_kill);
2334 }
2335 }
2336 unregister_netdevice_many(&list_kill);
2337
2338 return 0;
2339}
2340
Thomas Graf614732e2015-07-21 10:44:06 +02002341int rtnl_delete_link(struct net_device *dev)
2342{
2343 const struct rtnl_link_ops *ops;
2344 LIST_HEAD(list_kill);
2345
2346 ops = dev->rtnl_link_ops;
2347 if (!ops || !ops->dellink)
2348 return -EOPNOTSUPP;
2349
2350 ops->dellink(dev, &list_kill);
2351 unregister_netdevice_many(&list_kill);
2352
2353 return 0;
2354}
2355EXPORT_SYMBOL_GPL(rtnl_delete_link);
2356
David Ahernc21ef3e2017-04-16 09:48:24 -07002357static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
2358 struct netlink_ext_ack *extack)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002359{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002360 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002361 struct net_device *dev;
2362 struct ifinfomsg *ifm;
2363 char ifname[IFNAMSIZ];
2364 struct nlattr *tb[IFLA_MAX+1];
2365 int err;
2366
David Ahernc21ef3e2017-04-16 09:48:24 -07002367 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002368 if (err < 0)
2369 return err;
2370
2371 if (tb[IFLA_IFNAME])
2372 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2373
2374 ifm = nlmsg_data(nlh);
2375 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07002376 dev = __dev_get_by_index(net, ifm->ifi_index);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002377 else if (tb[IFLA_IFNAME])
Eric W. Biederman881d9662007-09-17 11:56:21 -07002378 dev = __dev_get_by_name(net, ifname);
WANG Cong66400d52015-03-24 11:53:31 -07002379 else if (tb[IFLA_GROUP])
2380 return rtnl_group_dellink(net, nla_get_u32(tb[IFLA_GROUP]));
Patrick McHardy38f7b872007-06-13 12:03:51 -07002381 else
2382 return -EINVAL;
2383
2384 if (!dev)
2385 return -ENODEV;
2386
Thomas Graf614732e2015-07-21 10:44:06 +02002387 return rtnl_delete_link(dev);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002388}
2389
Patrick McHardy3729d502010-02-26 06:34:54 +00002390int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
2391{
2392 unsigned int old_flags;
2393 int err;
2394
2395 old_flags = dev->flags;
2396 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
2397 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
2398 if (err < 0)
2399 return err;
2400 }
2401
2402 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
Patrick McHardy3729d502010-02-26 06:34:54 +00002403
Nicolas Dichtela528c212013-09-25 12:02:44 +02002404 __dev_notify_flags(dev, old_flags, ~0U);
Patrick McHardy3729d502010-02-26 06:34:54 +00002405 return 0;
2406}
2407EXPORT_SYMBOL(rtnl_configure_link);
2408
Rami Rosenc0713562012-11-30 01:08:47 +00002409struct net_device *rtnl_create_link(struct net *net,
Thomas Graf78ebb0d2015-04-10 01:45:53 +02002410 const char *ifname, unsigned char name_assign_type,
Tom Gundersen55177502014-07-14 16:37:25 +02002411 const struct rtnl_link_ops *ops, struct nlattr *tb[])
Pavel Emelianove7199282007-08-08 22:16:38 -07002412{
Pavel Emelianove7199282007-08-08 22:16:38 -07002413 struct net_device *dev;
Jiri Pirkod40156a2012-07-20 02:28:47 +00002414 unsigned int num_tx_queues = 1;
2415 unsigned int num_rx_queues = 1;
Pavel Emelianove7199282007-08-08 22:16:38 -07002416
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002417 if (tb[IFLA_NUM_TX_QUEUES])
2418 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
2419 else if (ops->get_num_tx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002420 num_tx_queues = ops->get_num_tx_queues();
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002421
2422 if (tb[IFLA_NUM_RX_QUEUES])
2423 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
2424 else if (ops->get_num_rx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002425 num_rx_queues = ops->get_num_rx_queues();
stephen hemmingerefacb302012-04-10 18:34:43 +00002426
Tom Gundersen55177502014-07-14 16:37:25 +02002427 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
Tom Gundersenc835a672014-07-14 16:37:24 +02002428 ops->setup, num_tx_queues, num_rx_queues);
Pavel Emelianove7199282007-08-08 22:16:38 -07002429 if (!dev)
Tobias Klauserd1892e42017-02-20 16:32:06 +01002430 return ERR_PTR(-ENOMEM);
Pavel Emelianove7199282007-08-08 22:16:38 -07002431
Eric W. Biederman81adee42009-11-08 00:53:51 -08002432 dev_net_set(dev, net);
2433 dev->rtnl_link_ops = ops;
Patrick McHardy3729d502010-02-26 06:34:54 +00002434 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
Eric W. Biederman81adee42009-11-08 00:53:51 -08002435
Pavel Emelianove7199282007-08-08 22:16:38 -07002436 if (tb[IFLA_MTU])
2437 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002438 if (tb[IFLA_ADDRESS]) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002439 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
2440 nla_len(tb[IFLA_ADDRESS]));
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002441 dev->addr_assign_type = NET_ADDR_SET;
2442 }
Pavel Emelianove7199282007-08-08 22:16:38 -07002443 if (tb[IFLA_BROADCAST])
2444 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
2445 nla_len(tb[IFLA_BROADCAST]));
2446 if (tb[IFLA_TXQLEN])
2447 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
2448 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002449 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Pavel Emelianove7199282007-08-08 22:16:38 -07002450 if (tb[IFLA_LINKMODE])
2451 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
Patrick McHardyffa934f2011-01-20 03:00:42 +00002452 if (tb[IFLA_GROUP])
2453 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Pavel Emelianove7199282007-08-08 22:16:38 -07002454
2455 return dev;
Pavel Emelianove7199282007-08-08 22:16:38 -07002456}
Eric Dumazete0d087a2009-11-07 01:26:17 -08002457EXPORT_SYMBOL(rtnl_create_link);
Pavel Emelianove7199282007-08-08 22:16:38 -07002458
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002459static int rtnl_group_changelink(const struct sk_buff *skb,
2460 struct net *net, int group,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002461 struct ifinfomsg *ifm,
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002462 struct netlink_ext_ack *extack,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002463 struct nlattr **tb)
2464{
WANG Congd0795352015-03-23 16:31:09 -07002465 struct net_device *dev, *aux;
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002466 int err;
2467
WANG Congd0795352015-03-23 16:31:09 -07002468 for_each_netdev_safe(net, dev, aux) {
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002469 if (dev->group == group) {
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002470 err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002471 if (err < 0)
2472 return err;
2473 }
2474 }
2475
2476 return 0;
2477}
2478
David Ahernc21ef3e2017-04-16 09:48:24 -07002479static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2480 struct netlink_ext_ack *extack)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002481{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002482 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002483 const struct rtnl_link_ops *ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002484 const struct rtnl_link_ops *m_ops = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002485 struct net_device *dev;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002486 struct net_device *master_dev = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002487 struct ifinfomsg *ifm;
2488 char kind[MODULE_NAME_LEN];
2489 char ifname[IFNAMSIZ];
2490 struct nlattr *tb[IFLA_MAX+1];
2491 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
Tom Gundersen55177502014-07-14 16:37:25 +02002492 unsigned char name_assign_type = NET_NAME_USER;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002493 int err;
2494
Johannes Berg95a5afc2008-10-16 15:24:51 -07002495#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002496replay:
Thomas Graf8072f082007-07-31 14:13:50 -07002497#endif
David Ahernc21ef3e2017-04-16 09:48:24 -07002498 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002499 if (err < 0)
2500 return err;
2501
2502 if (tb[IFLA_IFNAME])
2503 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2504 else
2505 ifname[0] = '\0';
2506
2507 ifm = nlmsg_data(nlh);
2508 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07002509 dev = __dev_get_by_index(net, ifm->ifi_index);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002510 else {
2511 if (ifname[0])
2512 dev = __dev_get_by_name(net, ifname);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002513 else
2514 dev = NULL;
2515 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002516
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002517 if (dev) {
2518 master_dev = netdev_master_upper_dev_get(dev);
2519 if (master_dev)
2520 m_ops = master_dev->rtnl_link_ops;
2521 }
2522
Eric Dumazete0d087a2009-11-07 01:26:17 -08002523 err = validate_linkmsg(dev, tb);
2524 if (err < 0)
Thomas Graf1840bb12008-02-23 19:54:36 -08002525 return err;
2526
Patrick McHardy38f7b872007-06-13 12:03:51 -07002527 if (tb[IFLA_LINKINFO]) {
2528 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +02002529 tb[IFLA_LINKINFO], ifla_info_policy,
2530 NULL);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002531 if (err < 0)
2532 return err;
2533 } else
2534 memset(linkinfo, 0, sizeof(linkinfo));
2535
2536 if (linkinfo[IFLA_INFO_KIND]) {
2537 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
2538 ops = rtnl_link_ops_get(kind);
2539 } else {
2540 kind[0] = '\0';
2541 ops = NULL;
2542 }
2543
2544 if (1) {
Sasha Levin4e10fd52015-02-24 14:14:35 -05002545 struct nlattr *attr[ops ? ops->maxtype + 1 : 1];
2546 struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1];
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002547 struct nlattr **data = NULL;
2548 struct nlattr **slave_data = NULL;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002549 struct net *dest_net, *link_net = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002550
2551 if (ops) {
2552 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
2553 err = nla_parse_nested(attr, ops->maxtype,
2554 linkinfo[IFLA_INFO_DATA],
Johannes Bergfceb6432017-04-12 14:34:07 +02002555 ops->policy, NULL);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002556 if (err < 0)
2557 return err;
2558 data = attr;
2559 }
2560 if (ops->validate) {
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02002561 err = ops->validate(tb, data, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002562 if (err < 0)
2563 return err;
2564 }
2565 }
2566
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002567 if (m_ops) {
2568 if (m_ops->slave_maxtype &&
2569 linkinfo[IFLA_INFO_SLAVE_DATA]) {
2570 err = nla_parse_nested(slave_attr,
2571 m_ops->slave_maxtype,
2572 linkinfo[IFLA_INFO_SLAVE_DATA],
Johannes Bergfceb6432017-04-12 14:34:07 +02002573 m_ops->slave_policy,
2574 NULL);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002575 if (err < 0)
2576 return err;
2577 slave_data = slave_attr;
2578 }
2579 if (m_ops->slave_validate) {
Matthias Schifferd116ffc2017-06-25 23:56:03 +02002580 err = m_ops->slave_validate(tb, slave_data,
2581 extack);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002582 if (err < 0)
2583 return err;
2584 }
2585 }
2586
Patrick McHardy38f7b872007-06-13 12:03:51 -07002587 if (dev) {
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002588 int status = 0;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002589
2590 if (nlh->nlmsg_flags & NLM_F_EXCL)
2591 return -EEXIST;
2592 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2593 return -EOPNOTSUPP;
2594
2595 if (linkinfo[IFLA_INFO_DATA]) {
2596 if (!ops || ops != dev->rtnl_link_ops ||
2597 !ops->changelink)
2598 return -EOPNOTSUPP;
2599
Matthias Schifferad744b22017-06-25 23:56:00 +02002600 err = ops->changelink(dev, tb, data, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002601 if (err < 0)
2602 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002603 status |= DO_SETLINK_NOTIFY;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002604 }
2605
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002606 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2607 if (!m_ops || !m_ops->slave_changelink)
2608 return -EOPNOTSUPP;
2609
2610 err = m_ops->slave_changelink(master_dev, dev,
Matthias Schiffer17dd0ec2017-06-25 23:56:02 +02002611 tb, slave_data,
2612 extack);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002613 if (err < 0)
2614 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002615 status |= DO_SETLINK_NOTIFY;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002616 }
2617
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002618 return do_setlink(skb, dev, ifm, extack, tb, ifname,
2619 status);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002620 }
2621
Patrick McHardyffa934f2011-01-20 03:00:42 +00002622 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2623 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002624 return rtnl_group_changelink(skb, net,
Patrick McHardyffa934f2011-01-20 03:00:42 +00002625 nla_get_u32(tb[IFLA_GROUP]),
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002626 ifm, extack, tb);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002627 return -ENODEV;
Patrick McHardyffa934f2011-01-20 03:00:42 +00002628 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002629
Theuns Verwoerd160ca012017-01-31 12:23:46 +13002630 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
Patrick McHardy38f7b872007-06-13 12:03:51 -07002631 return -EOPNOTSUPP;
2632
2633 if (!ops) {
Johannes Berg95a5afc2008-10-16 15:24:51 -07002634#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002635 if (kind[0]) {
2636 __rtnl_unlock();
2637 request_module("rtnl-link-%s", kind);
2638 rtnl_lock();
2639 ops = rtnl_link_ops_get(kind);
2640 if (ops)
2641 goto replay;
2642 }
2643#endif
2644 return -EOPNOTSUPP;
2645 }
2646
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02002647 if (!ops->setup)
2648 return -EOPNOTSUPP;
2649
Tom Gundersen55177502014-07-14 16:37:25 +02002650 if (!ifname[0]) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07002651 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
Tom Gundersen55177502014-07-14 16:37:25 +02002652 name_assign_type = NET_NAME_ENUM;
2653 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002654
Eric W. Biederman81adee42009-11-08 00:53:51 -08002655 dest_net = rtnl_link_get_net(net, tb);
Eric W. Biederman13ad1772011-01-29 14:57:22 +00002656 if (IS_ERR(dest_net))
2657 return PTR_ERR(dest_net);
2658
Eric W. Biederman505ce412015-02-26 16:19:00 -06002659 err = -EPERM;
2660 if (!netlink_ns_capable(skb, dest_net->user_ns, CAP_NET_ADMIN))
2661 goto out;
2662
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002663 if (tb[IFLA_LINK_NETNSID]) {
2664 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
2665
2666 link_net = get_net_ns_by_id(dest_net, id);
2667 if (!link_net) {
2668 err = -EINVAL;
2669 goto out;
2670 }
Eric W. Biederman06615be2015-02-26 16:20:07 -06002671 err = -EPERM;
2672 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
2673 goto out;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002674 }
2675
2676 dev = rtnl_create_link(link_net ? : dest_net, ifname,
2677 name_assign_type, ops, tb);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002678 if (IS_ERR(dev)) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002679 err = PTR_ERR(dev);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002680 goto out;
2681 }
2682
2683 dev->ifindex = ifm->ifi_index;
2684
Cong Wang0e0eee22014-02-11 15:51:30 -08002685 if (ops->newlink) {
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02002686 err = ops->newlink(link_net ? : net, dev, tb, data,
2687 extack);
Cong Wang0e0eee22014-02-11 15:51:30 -08002688 /* Drivers should call free_netdev() in ->destructor
Cong Wange51fb152014-06-03 16:40:47 -07002689 * and unregister it on failure after registration
2690 * so that device could be finally freed in rtnl_unlock.
Cong Wang0e0eee22014-02-11 15:51:30 -08002691 */
Cong Wange51fb152014-06-03 16:40:47 -07002692 if (err < 0) {
2693 /* If device is not registered at all, free it now */
2694 if (dev->reg_state == NETREG_UNINITIALIZED)
2695 free_netdev(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002696 goto out;
Cong Wange51fb152014-06-03 16:40:47 -07002697 }
Cong Wang0e0eee22014-02-11 15:51:30 -08002698 } else {
Patrick McHardy2d85cba2007-07-11 19:42:13 -07002699 err = register_netdevice(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002700 if (err < 0) {
2701 free_netdev(dev);
2702 goto out;
2703 }
Dan Carpenterfce9b9b2013-08-14 12:35:42 +03002704 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002705 err = rtnl_configure_link(dev, ifm);
David S. Miller43638902015-03-10 21:58:32 -04002706 if (err < 0)
2707 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002708 if (link_net) {
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002709 err = dev_change_net_namespace(dev, dest_net, ifname);
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002710 if (err < 0)
David S. Miller43638902015-03-10 21:58:32 -04002711 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002712 }
Theuns Verwoerd160ca012017-01-31 12:23:46 +13002713 if (tb[IFLA_MASTER]) {
2714 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
2715 if (err)
2716 goto out_unregister;
2717 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002718out:
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002719 if (link_net)
2720 put_net(link_net);
Eric W. Biederman81adee42009-11-08 00:53:51 -08002721 put_net(dest_net);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002722 return err;
David S. Miller43638902015-03-10 21:58:32 -04002723out_unregister:
2724 if (ops->newlink) {
2725 LIST_HEAD(list_kill);
2726
2727 ops->dellink(dev, &list_kill);
2728 unregister_netdevice_many(&list_kill);
2729 } else {
2730 unregister_netdevice(dev);
2731 }
2732 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002733 }
2734}
2735
David Ahernc21ef3e2017-04-16 09:48:24 -07002736static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2737 struct netlink_ext_ack *extack)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002738{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002739 struct net *net = sock_net(skb->sk);
Thomas Grafb60c5112006-08-04 23:05:34 -07002740 struct ifinfomsg *ifm;
Eric Dumazeta3d12892009-10-21 10:59:31 +00002741 char ifname[IFNAMSIZ];
Thomas Grafb60c5112006-08-04 23:05:34 -07002742 struct nlattr *tb[IFLA_MAX+1];
2743 struct net_device *dev = NULL;
2744 struct sk_buff *nskb;
Thomas Graf339bf982006-11-10 14:10:15 -08002745 int err;
Greg Rose115c9b82012-02-21 16:54:48 -05002746 u32 ext_filter_mask = 0;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002747
David Ahernc21ef3e2017-04-16 09:48:24 -07002748 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Thomas Grafb60c5112006-08-04 23:05:34 -07002749 if (err < 0)
Eric Sesterhenn9918f232006-09-26 23:26:38 -07002750 return err;
Thomas Grafb60c5112006-08-04 23:05:34 -07002751
Eric Dumazeta3d12892009-10-21 10:59:31 +00002752 if (tb[IFLA_IFNAME])
2753 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2754
Greg Rose115c9b82012-02-21 16:54:48 -05002755 if (tb[IFLA_EXT_MASK])
2756 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2757
Thomas Grafb60c5112006-08-04 23:05:34 -07002758 ifm = nlmsg_data(nlh);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002759 if (ifm->ifi_index > 0)
2760 dev = __dev_get_by_index(net, ifm->ifi_index);
2761 else if (tb[IFLA_IFNAME])
2762 dev = __dev_get_by_name(net, ifname);
2763 else
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002764 return -EINVAL;
Thomas Grafb60c5112006-08-04 23:05:34 -07002765
Eric Dumazeta3d12892009-10-21 10:59:31 +00002766 if (dev == NULL)
2767 return -ENODEV;
2768
Greg Rose115c9b82012-02-21 16:54:48 -05002769 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002770 if (nskb == NULL)
2771 return -ENOBUFS;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002772
Eric W. Biederman15e47302012-09-07 20:12:54 +00002773 err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04002774 nlh->nlmsg_seq, 0, 0, ext_filter_mask, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08002775 if (err < 0) {
2776 /* -EMSGSIZE implies BUG in if_nlmsg_size */
2777 WARN_ON(err == -EMSGSIZE);
2778 kfree_skb(nskb);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002779 } else
Eric W. Biederman15e47302012-09-07 20:12:54 +00002780 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
Thomas Grafb60c5112006-08-04 23:05:34 -07002781
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002782 return err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002783}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002784
Greg Rose115c9b82012-02-21 16:54:48 -05002785static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
Greg Rosec7ac8672011-06-10 01:27:09 +00002786{
Greg Rose115c9b82012-02-21 16:54:48 -05002787 struct net *net = sock_net(skb->sk);
2788 struct net_device *dev;
2789 struct nlattr *tb[IFLA_MAX+1];
2790 u32 ext_filter_mask = 0;
2791 u16 min_ifinfo_dump_size = 0;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02002792 int hdrlen;
Greg Rose115c9b82012-02-21 16:54:48 -05002793
Michal Schmidte5eca6d2014-05-28 14:15:19 +02002794 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2795 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2796 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2797
Johannes Bergfceb6432017-04-12 14:34:07 +02002798 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00002799 if (tb[IFLA_EXT_MASK])
2800 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2801 }
Greg Rose115c9b82012-02-21 16:54:48 -05002802
2803 if (!ext_filter_mask)
2804 return NLMSG_GOODSIZE;
2805 /*
2806 * traverse the list of net devices and compute the minimum
2807 * buffer size based upon the filter mask.
2808 */
Florian Westphal6853dd42017-08-09 20:41:51 +02002809 rcu_read_lock();
2810 for_each_netdev_rcu(net, dev) {
Greg Rose115c9b82012-02-21 16:54:48 -05002811 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
2812 if_nlmsg_size(dev,
2813 ext_filter_mask));
2814 }
Florian Westphal6853dd42017-08-09 20:41:51 +02002815 rcu_read_unlock();
Greg Rose115c9b82012-02-21 16:54:48 -05002816
Zhang Shengju93af2052016-11-22 14:14:28 +08002817 return nlmsg_total_size(min_ifinfo_dump_size);
Greg Rosec7ac8672011-06-10 01:27:09 +00002818}
2819
Adrian Bunk42bad1d2007-04-26 00:57:41 -07002820static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821{
2822 int idx;
2823 int s_idx = cb->family;
2824
2825 if (s_idx == 0)
2826 s_idx = 1;
Florian Westphal6853dd42017-08-09 20:41:51 +02002827
Patrick McHardy25239ce2010-04-26 16:02:05 +02002828 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 int type = cb->nlh->nlmsg_type-RTM_BASE;
Florian Westphal6853dd42017-08-09 20:41:51 +02002830 struct rtnl_link *handlers;
2831 rtnl_dumpit_func dumpit;
2832
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 if (idx < s_idx || idx == PF_PACKET)
2834 continue;
Florian Westphal6853dd42017-08-09 20:41:51 +02002835
2836 handlers = rtnl_dereference(rtnl_msg_handlers[idx]);
2837 if (!handlers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 continue;
Florian Westphal6853dd42017-08-09 20:41:51 +02002839
2840 dumpit = READ_ONCE(handlers[type].dumpit);
2841 if (!dumpit)
2842 continue;
2843
Nicolas Dichtel04652772013-03-22 06:28:42 +00002844 if (idx > s_idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 memset(&cb->args[0], 0, sizeof(cb->args));
Nicolas Dichtel04652772013-03-22 06:28:42 +00002846 cb->prev_seq = 0;
2847 cb->seq = 0;
2848 }
Florian Westphal6853dd42017-08-09 20:41:51 +02002849 if (dumpit(skb, cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 break;
2851 }
2852 cb->family = idx;
2853
2854 return skb->len;
2855}
2856
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002857struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04002858 unsigned int change,
2859 u32 event, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002861 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 struct sk_buff *skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002863 int err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00002864 size_t if_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
Alexei Starovoitov7f294052013-10-23 16:02:42 -07002866 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002867 if (skb == NULL)
2868 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04002870 err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0, event);
Patrick McHardy26932562007-01-31 23:16:40 -08002871 if (err < 0) {
2872 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
2873 WARN_ON(err == -EMSGSIZE);
2874 kfree_skb(skb);
2875 goto errout;
2876 }
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002877 return skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002878errout:
2879 if (err < 0)
Eric W. Biederman4b3da702007-11-19 22:27:40 -08002880 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002881 return NULL;
2882}
2883
2884void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
2885{
2886 struct net *net = dev_net(dev);
2887
2888 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
2889}
2890
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04002891static void rtmsg_ifinfo_event(int type, struct net_device *dev,
2892 unsigned int change, u32 event,
2893 gfp_t flags)
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002894{
2895 struct sk_buff *skb;
2896
Nicolas Dichteled2a80a2015-05-13 14:19:42 +02002897 if (dev->reg_state != NETREG_REGISTERED)
2898 return;
2899
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04002900 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002901 if (skb)
2902 rtmsg_ifinfo_send(skb, dev, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903}
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04002904
2905void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
2906 gfp_t flags)
2907{
Vlad Yasevich8c6c9182017-05-30 10:07:02 -04002908 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags);
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04002909}
Jiri Pirko471cb5a2013-01-03 22:49:01 +00002910EXPORT_SYMBOL(rtmsg_ifinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911
John Fastabendd83b0602012-04-15 06:44:08 +00002912static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
2913 struct net_device *dev,
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002914 u8 *addr, u16 vid, u32 pid, u32 seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002915 int type, unsigned int flags,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002916 int nlflags, u16 ndm_state)
John Fastabendd83b0602012-04-15 06:44:08 +00002917{
2918 struct nlmsghdr *nlh;
2919 struct ndmsg *ndm;
2920
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002921 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
John Fastabendd83b0602012-04-15 06:44:08 +00002922 if (!nlh)
2923 return -EMSGSIZE;
2924
2925 ndm = nlmsg_data(nlh);
2926 ndm->ndm_family = AF_BRIDGE;
2927 ndm->ndm_pad1 = 0;
2928 ndm->ndm_pad2 = 0;
2929 ndm->ndm_flags = flags;
2930 ndm->ndm_type = 0;
2931 ndm->ndm_ifindex = dev->ifindex;
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002932 ndm->ndm_state = ndm_state;
John Fastabendd83b0602012-04-15 06:44:08 +00002933
2934 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
2935 goto nla_put_failure;
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002936 if (vid)
2937 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
2938 goto nla_put_failure;
John Fastabendd83b0602012-04-15 06:44:08 +00002939
Johannes Berg053c0952015-01-16 22:09:00 +01002940 nlmsg_end(skb, nlh);
2941 return 0;
John Fastabendd83b0602012-04-15 06:44:08 +00002942
2943nla_put_failure:
2944 nlmsg_cancel(skb, nlh);
2945 return -EMSGSIZE;
2946}
2947
John Fastabend3ff661c2012-04-15 06:44:14 +00002948static inline size_t rtnl_fdb_nlmsg_size(void)
2949{
Sabrina Dubrocaf82ef3e2016-11-18 15:50:39 +01002950 return NLMSG_ALIGN(sizeof(struct ndmsg)) +
2951 nla_total_size(ETH_ALEN) + /* NDA_LLADDR */
2952 nla_total_size(sizeof(u16)) + /* NDA_VLAN */
2953 0;
John Fastabend3ff661c2012-04-15 06:44:14 +00002954}
2955
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002956static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
2957 u16 ndm_state)
John Fastabend3ff661c2012-04-15 06:44:14 +00002958{
2959 struct net *net = dev_net(dev);
2960 struct sk_buff *skb;
2961 int err = -ENOBUFS;
2962
2963 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
2964 if (!skb)
2965 goto errout;
2966
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002967 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002968 0, 0, type, NTF_SELF, 0, ndm_state);
John Fastabend3ff661c2012-04-15 06:44:14 +00002969 if (err < 0) {
2970 kfree_skb(skb);
2971 goto errout;
2972 }
2973
2974 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
2975 return;
2976errout:
2977 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
2978}
2979
Vlad Yasevich090096b2013-03-06 15:39:42 +00002980/**
2981 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2982 */
2983int ndo_dflt_fdb_add(struct ndmsg *ndm,
2984 struct nlattr *tb[],
2985 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01002986 const unsigned char *addr, u16 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00002987 u16 flags)
2988{
2989 int err = -EINVAL;
2990
2991 /* If aging addresses are supported device will need to
2992 * implement its own handler for this.
2993 */
2994 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2995 pr_info("%s: FDB only supports static addresses\n", dev->name);
2996 return err;
2997 }
2998
Or Gerlitz65891fe2014-12-14 18:19:05 +02002999 if (vid) {
3000 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
3001 return err;
3002 }
3003
Vlad Yasevich090096b2013-03-06 15:39:42 +00003004 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3005 err = dev_uc_add_excl(dev, addr);
3006 else if (is_multicast_ether_addr(addr))
3007 err = dev_mc_add_excl(dev, addr);
3008
3009 /* Only return duplicate errors if NLM_F_EXCL is set */
3010 if (err == -EEXIST && !(flags & NLM_F_EXCL))
3011 err = 0;
3012
3013 return err;
3014}
3015EXPORT_SYMBOL(ndo_dflt_fdb_add);
3016
Jiri Pirkof6f64242014-11-28 14:34:15 +01003017static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid)
3018{
3019 u16 vid = 0;
3020
3021 if (vlan_attr) {
3022 if (nla_len(vlan_attr) != sizeof(u16)) {
3023 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n");
3024 return -EINVAL;
3025 }
3026
3027 vid = nla_get_u16(vlan_attr);
3028
3029 if (!vid || vid >= VLAN_VID_MASK) {
3030 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n",
3031 vid);
3032 return -EINVAL;
3033 }
3034 }
3035 *p_vid = vid;
3036 return 0;
3037}
3038
David Ahernc21ef3e2017-04-16 09:48:24 -07003039static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
3040 struct netlink_ext_ack *extack)
John Fastabend77162022012-04-15 06:43:56 +00003041{
3042 struct net *net = sock_net(skb->sk);
John Fastabend77162022012-04-15 06:43:56 +00003043 struct ndmsg *ndm;
3044 struct nlattr *tb[NDA_MAX+1];
3045 struct net_device *dev;
3046 u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01003047 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00003048 int err;
3049
David Ahernc21ef3e2017-04-16 09:48:24 -07003050 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
John Fastabend77162022012-04-15 06:43:56 +00003051 if (err < 0)
3052 return err;
3053
3054 ndm = nlmsg_data(nlh);
3055 if (ndm->ndm_ifindex == 0) {
3056 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
3057 return -EINVAL;
3058 }
3059
3060 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3061 if (dev == NULL) {
3062 pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
3063 return -ENODEV;
3064 }
3065
3066 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3067 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
3068 return -EINVAL;
3069 }
3070
3071 addr = nla_data(tb[NDA_LLADDR]);
John Fastabend77162022012-04-15 06:43:56 +00003072
Jiri Pirkof6f64242014-11-28 14:34:15 +01003073 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
3074 if (err)
3075 return err;
3076
John Fastabend77162022012-04-15 06:43:56 +00003077 err = -EOPNOTSUPP;
3078
3079 /* Support fdb on master device the net/bridge default case */
3080 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3081 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003082 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3083 const struct net_device_ops *ops = br_dev->netdev_ops;
3084
Jiri Pirkof6f64242014-11-28 14:34:15 +01003085 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
3086 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00003087 if (err)
3088 goto out;
3089 else
3090 ndm->ndm_flags &= ~NTF_MASTER;
3091 }
3092
3093 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00003094 if ((ndm->ndm_flags & NTF_SELF)) {
3095 if (dev->netdev_ops->ndo_fdb_add)
3096 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003097 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003098 nlh->nlmsg_flags);
3099 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01003100 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003101 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00003102
John Fastabend3ff661c2012-04-15 06:44:14 +00003103 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003104 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
3105 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00003106 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00003107 }
John Fastabend77162022012-04-15 06:43:56 +00003108 }
3109out:
3110 return err;
3111}
3112
Vlad Yasevich090096b2013-03-06 15:39:42 +00003113/**
3114 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3115 */
3116int ndo_dflt_fdb_del(struct ndmsg *ndm,
3117 struct nlattr *tb[],
3118 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003119 const unsigned char *addr, u16 vid)
Vlad Yasevich090096b2013-03-06 15:39:42 +00003120{
Alexander Duyckc8a89c42014-07-15 15:15:20 -07003121 int err = -EINVAL;
Vlad Yasevich090096b2013-03-06 15:39:42 +00003122
3123 /* If aging addresses are supported device will need to
3124 * implement its own handler for this.
3125 */
Sridhar Samudrala64535992013-08-08 15:19:48 -07003126 if (!(ndm->ndm_state & NUD_PERMANENT)) {
Vlad Yasevich090096b2013-03-06 15:39:42 +00003127 pr_info("%s: FDB only supports static addresses\n", dev->name);
Alexander Duyckc8a89c42014-07-15 15:15:20 -07003128 return err;
Vlad Yasevich090096b2013-03-06 15:39:42 +00003129 }
3130
3131 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3132 err = dev_uc_del(dev, addr);
3133 else if (is_multicast_ether_addr(addr))
3134 err = dev_mc_del(dev, addr);
Vlad Yasevich090096b2013-03-06 15:39:42 +00003135
3136 return err;
3137}
3138EXPORT_SYMBOL(ndo_dflt_fdb_del);
3139
David Ahernc21ef3e2017-04-16 09:48:24 -07003140static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
3141 struct netlink_ext_ack *extack)
John Fastabend77162022012-04-15 06:43:56 +00003142{
3143 struct net *net = sock_net(skb->sk);
3144 struct ndmsg *ndm;
Vlad Yasevich1690be62013-02-13 12:00:18 +00003145 struct nlattr *tb[NDA_MAX+1];
John Fastabend77162022012-04-15 06:43:56 +00003146 struct net_device *dev;
3147 int err = -EINVAL;
3148 __u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01003149 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00003150
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07003151 if (!netlink_capable(skb, CAP_NET_ADMIN))
Vlad Yasevich1690be62013-02-13 12:00:18 +00003152 return -EPERM;
3153
David Ahernc21ef3e2017-04-16 09:48:24 -07003154 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
Vlad Yasevich1690be62013-02-13 12:00:18 +00003155 if (err < 0)
3156 return err;
John Fastabend77162022012-04-15 06:43:56 +00003157
3158 ndm = nlmsg_data(nlh);
3159 if (ndm->ndm_ifindex == 0) {
3160 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
3161 return -EINVAL;
3162 }
3163
3164 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3165 if (dev == NULL) {
3166 pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
3167 return -ENODEV;
3168 }
3169
Vlad Yasevich1690be62013-02-13 12:00:18 +00003170 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3171 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
John Fastabend77162022012-04-15 06:43:56 +00003172 return -EINVAL;
3173 }
3174
Vlad Yasevich1690be62013-02-13 12:00:18 +00003175 addr = nla_data(tb[NDA_LLADDR]);
Vlad Yasevich1690be62013-02-13 12:00:18 +00003176
Jiri Pirkof6f64242014-11-28 14:34:15 +01003177 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
3178 if (err)
3179 return err;
3180
John Fastabend77162022012-04-15 06:43:56 +00003181 err = -EOPNOTSUPP;
3182
3183 /* Support fdb on master device the net/bridge default case */
3184 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3185 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003186 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3187 const struct net_device_ops *ops = br_dev->netdev_ops;
John Fastabend77162022012-04-15 06:43:56 +00003188
Jiri Pirko898e5062013-01-03 22:48:52 +00003189 if (ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003190 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00003191
3192 if (err)
3193 goto out;
3194 else
3195 ndm->ndm_flags &= ~NTF_MASTER;
3196 }
3197
3198 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00003199 if (ndm->ndm_flags & NTF_SELF) {
3200 if (dev->netdev_ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003201 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
3202 vid);
Vlad Yasevich090096b2013-03-06 15:39:42 +00003203 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01003204 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00003205
John Fastabend3ff661c2012-04-15 06:44:14 +00003206 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003207 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
3208 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00003209 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00003210 }
John Fastabend77162022012-04-15 06:43:56 +00003211 }
3212out:
3213 return err;
3214}
3215
John Fastabendd83b0602012-04-15 06:44:08 +00003216static int nlmsg_populate_fdb(struct sk_buff *skb,
3217 struct netlink_callback *cb,
3218 struct net_device *dev,
3219 int *idx,
3220 struct netdev_hw_addr_list *list)
3221{
3222 struct netdev_hw_addr *ha;
3223 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00003224 u32 portid, seq;
John Fastabendd83b0602012-04-15 06:44:08 +00003225
Eric W. Biederman15e47302012-09-07 20:12:54 +00003226 portid = NETLINK_CB(cb->skb).portid;
John Fastabendd83b0602012-04-15 06:44:08 +00003227 seq = cb->nlh->nlmsg_seq;
3228
3229 list_for_each_entry(ha, &list->list, list) {
Roopa Prabhud2976532016-08-30 21:56:45 -07003230 if (*idx < cb->args[2])
John Fastabendd83b0602012-04-15 06:44:08 +00003231 goto skip;
3232
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003233 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
John Fastabenda7a558f2012-11-01 16:23:10 +00003234 portid, seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003235 RTM_NEWNEIGH, NTF_SELF,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003236 NLM_F_MULTI, NUD_PERMANENT);
John Fastabendd83b0602012-04-15 06:44:08 +00003237 if (err < 0)
3238 return err;
3239skip:
3240 *idx += 1;
3241 }
3242 return 0;
3243}
3244
3245/**
Ben Hutchings2c530402012-07-10 10:55:09 +00003246 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
John Fastabendd83b0602012-04-15 06:44:08 +00003247 * @nlh: netlink message header
3248 * @dev: netdevice
3249 *
3250 * Default netdevice operation to dump the existing unicast address list.
John Fastabend91f3e7b2013-03-29 08:18:37 +00003251 * Returns number of addresses from list put in skb.
John Fastabendd83b0602012-04-15 06:44:08 +00003252 */
3253int ndo_dflt_fdb_dump(struct sk_buff *skb,
3254 struct netlink_callback *cb,
3255 struct net_device *dev,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04003256 struct net_device *filter_dev,
Roopa Prabhud2976532016-08-30 21:56:45 -07003257 int *idx)
John Fastabendd83b0602012-04-15 06:44:08 +00003258{
3259 int err;
3260
3261 netif_addr_lock_bh(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -07003262 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
John Fastabendd83b0602012-04-15 06:44:08 +00003263 if (err)
3264 goto out;
Zhang Shengju2934c9d2016-11-30 16:37:34 +08003265 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
John Fastabendd83b0602012-04-15 06:44:08 +00003266out:
3267 netif_addr_unlock_bh(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -07003268 return err;
John Fastabendd83b0602012-04-15 06:44:08 +00003269}
3270EXPORT_SYMBOL(ndo_dflt_fdb_dump);
3271
John Fastabend77162022012-04-15 06:43:56 +00003272static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
3273{
John Fastabend77162022012-04-15 06:43:56 +00003274 struct net_device *dev;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003275 struct nlattr *tb[IFLA_MAX+1];
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003276 struct net_device *br_dev = NULL;
3277 const struct net_device_ops *ops = NULL;
3278 const struct net_device_ops *cops = NULL;
3279 struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
3280 struct net *net = sock_net(skb->sk);
Roopa Prabhud2976532016-08-30 21:56:45 -07003281 struct hlist_head *head;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003282 int brport_idx = 0;
3283 int br_idx = 0;
Roopa Prabhud2976532016-08-30 21:56:45 -07003284 int h, s_h;
3285 int idx = 0, s_idx;
3286 int err = 0;
3287 int fidx = 0;
John Fastabend77162022012-04-15 06:43:56 +00003288
Alexander Potapenko0ff50e82017-05-23 13:20:28 +02003289 err = nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb,
3290 IFLA_MAX, ifla_policy, NULL);
3291 if (err < 0) {
3292 return -EINVAL;
3293 } else if (err == 0) {
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003294 if (tb[IFLA_MASTER])
3295 br_idx = nla_get_u32(tb[IFLA_MASTER]);
3296 }
John Fastabend77162022012-04-15 06:43:56 +00003297
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003298 brport_idx = ifm->ifi_index;
3299
3300 if (br_idx) {
3301 br_dev = __dev_get_by_index(net, br_idx);
3302 if (!br_dev)
3303 return -ENODEV;
3304
3305 ops = br_dev->netdev_ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003306 }
3307
Roopa Prabhud2976532016-08-30 21:56:45 -07003308 s_h = cb->args[0];
3309 s_idx = cb->args[1];
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003310
Roopa Prabhud2976532016-08-30 21:56:45 -07003311 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
3312 idx = 0;
3313 head = &net->dev_index_head[h];
3314 hlist_for_each_entry(dev, head, index_hlist) {
3315
3316 if (brport_idx && (dev->ifindex != brport_idx))
3317 continue;
3318
3319 if (!br_idx) { /* user did not specify a specific bridge */
3320 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3321 br_dev = netdev_master_upper_dev_get(dev);
3322 cops = br_dev->netdev_ops;
3323 }
3324 } else {
3325 if (dev != br_dev &&
3326 !(dev->priv_flags & IFF_BRIDGE_PORT))
3327 continue;
3328
3329 if (br_dev != netdev_master_upper_dev_get(dev) &&
3330 !(dev->priv_flags & IFF_EBRIDGE))
3331 continue;
3332 cops = ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003333 }
3334
Roopa Prabhud2976532016-08-30 21:56:45 -07003335 if (idx < s_idx)
3336 goto cont;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003337
Roopa Prabhud2976532016-08-30 21:56:45 -07003338 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3339 if (cops && cops->ndo_fdb_dump) {
3340 err = cops->ndo_fdb_dump(skb, cb,
3341 br_dev, dev,
3342 &fidx);
3343 if (err == -EMSGSIZE)
3344 goto out;
3345 }
3346 }
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003347
Roopa Prabhud2976532016-08-30 21:56:45 -07003348 if (dev->netdev_ops->ndo_fdb_dump)
3349 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
3350 dev, NULL,
3351 &fidx);
3352 else
3353 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
3354 &fidx);
3355 if (err == -EMSGSIZE)
3356 goto out;
3357
3358 cops = NULL;
3359
3360 /* reset fdb offset to 0 for rest of the interfaces */
3361 cb->args[2] = 0;
3362 fidx = 0;
3363cont:
3364 idx++;
John Fastabend77162022012-04-15 06:43:56 +00003365 }
John Fastabend77162022012-04-15 06:43:56 +00003366 }
John Fastabend77162022012-04-15 06:43:56 +00003367
Roopa Prabhud2976532016-08-30 21:56:45 -07003368out:
3369 cb->args[0] = h;
3370 cb->args[1] = idx;
3371 cb->args[2] = fidx;
3372
John Fastabend77162022012-04-15 06:43:56 +00003373 return skb->len;
3374}
3375
Scott Feldman2c3c0312014-11-28 14:34:25 +01003376static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
3377 unsigned int attrnum, unsigned int flag)
3378{
3379 if (mask & flag)
3380 return nla_put_u8(skb, attrnum, !!(flags & flag));
3381 return 0;
3382}
3383
John Fastabend815cccb2012-10-24 08:13:09 +00003384int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Scott Feldman2c3c0312014-11-28 14:34:25 +01003385 struct net_device *dev, u16 mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003386 u32 flags, u32 mask, int nlflags,
3387 u32 filter_mask,
3388 int (*vlan_fill)(struct sk_buff *skb,
3389 struct net_device *dev,
3390 u32 filter_mask))
John Fastabend815cccb2012-10-24 08:13:09 +00003391{
3392 struct nlmsghdr *nlh;
3393 struct ifinfomsg *ifm;
3394 struct nlattr *br_afspec;
Scott Feldman2c3c0312014-11-28 14:34:25 +01003395 struct nlattr *protinfo;
John Fastabend815cccb2012-10-24 08:13:09 +00003396 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Jiri Pirko898e5062013-01-03 22:48:52 +00003397 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003398 int err = 0;
John Fastabend815cccb2012-10-24 08:13:09 +00003399
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02003400 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
John Fastabend815cccb2012-10-24 08:13:09 +00003401 if (nlh == NULL)
3402 return -EMSGSIZE;
3403
3404 ifm = nlmsg_data(nlh);
3405 ifm->ifi_family = AF_BRIDGE;
3406 ifm->__ifi_pad = 0;
3407 ifm->ifi_type = dev->type;
3408 ifm->ifi_index = dev->ifindex;
3409 ifm->ifi_flags = dev_get_flags(dev);
3410 ifm->ifi_change = 0;
3411
3412
3413 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
3414 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
3415 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00003416 (br_dev &&
3417 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
John Fastabend815cccb2012-10-24 08:13:09 +00003418 (dev->addr_len &&
3419 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02003420 (dev->ifindex != dev_get_iflink(dev) &&
3421 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
John Fastabend815cccb2012-10-24 08:13:09 +00003422 goto nla_put_failure;
3423
3424 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
3425 if (!br_afspec)
3426 goto nla_put_failure;
3427
Roopa Prabhu1d460b92014-12-08 14:04:20 -08003428 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
John Fastabend815cccb2012-10-24 08:13:09 +00003429 nla_nest_cancel(skb, br_afspec);
3430 goto nla_put_failure;
3431 }
Roopa Prabhu1d460b92014-12-08 14:04:20 -08003432
3433 if (mode != BRIDGE_MODE_UNDEF) {
3434 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
3435 nla_nest_cancel(skb, br_afspec);
3436 goto nla_put_failure;
3437 }
3438 }
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003439 if (vlan_fill) {
3440 err = vlan_fill(skb, dev, filter_mask);
3441 if (err) {
3442 nla_nest_cancel(skb, br_afspec);
3443 goto nla_put_failure;
3444 }
3445 }
John Fastabend815cccb2012-10-24 08:13:09 +00003446 nla_nest_end(skb, br_afspec);
3447
Scott Feldman2c3c0312014-11-28 14:34:25 +01003448 protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
3449 if (!protinfo)
3450 goto nla_put_failure;
3451
3452 if (brport_nla_put_flag(skb, flags, mask,
3453 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
3454 brport_nla_put_flag(skb, flags, mask,
3455 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
3456 brport_nla_put_flag(skb, flags, mask,
3457 IFLA_BRPORT_FAST_LEAVE,
3458 BR_MULTICAST_FAST_LEAVE) ||
3459 brport_nla_put_flag(skb, flags, mask,
3460 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
3461 brport_nla_put_flag(skb, flags, mask,
3462 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
3463 brport_nla_put_flag(skb, flags, mask,
3464 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
3465 brport_nla_put_flag(skb, flags, mask,
3466 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
3467 brport_nla_put_flag(skb, flags, mask,
3468 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
3469 nla_nest_cancel(skb, protinfo);
3470 goto nla_put_failure;
3471 }
3472
3473 nla_nest_end(skb, protinfo);
3474
Johannes Berg053c0952015-01-16 22:09:00 +01003475 nlmsg_end(skb, nlh);
3476 return 0;
John Fastabend815cccb2012-10-24 08:13:09 +00003477nla_put_failure:
3478 nlmsg_cancel(skb, nlh);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003479 return err ? err : -EMSGSIZE;
John Fastabend815cccb2012-10-24 08:13:09 +00003480}
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003481EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
John Fastabend815cccb2012-10-24 08:13:09 +00003482
John Fastabende5a55a82012-10-24 08:12:57 +00003483static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
3484{
3485 struct net *net = sock_net(skb->sk);
3486 struct net_device *dev;
3487 int idx = 0;
3488 u32 portid = NETLINK_CB(cb->skb).portid;
3489 u32 seq = cb->nlh->nlmsg_seq;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00003490 u32 filter_mask = 0;
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003491 int err;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00003492
Thomas Grafaa68c202014-11-26 13:42:20 +01003493 if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
3494 struct nlattr *extfilt;
3495
3496 extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
3497 IFLA_EXT_MASK);
3498 if (extfilt) {
3499 if (nla_len(extfilt) < sizeof(filter_mask))
3500 return -EINVAL;
3501
3502 filter_mask = nla_get_u32(extfilt);
3503 }
3504 }
John Fastabende5a55a82012-10-24 08:12:57 +00003505
3506 rcu_read_lock();
3507 for_each_netdev_rcu(net, dev) {
3508 const struct net_device_ops *ops = dev->netdev_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00003509 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
John Fastabende5a55a82012-10-24 08:12:57 +00003510
Jiri Pirko898e5062013-01-03 22:48:52 +00003511 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003512 if (idx >= cb->args[0]) {
3513 err = br_dev->netdev_ops->ndo_bridge_getlink(
3514 skb, portid, seq, dev,
3515 filter_mask, NLM_F_MULTI);
David Ahernf6c57752017-05-15 23:19:17 -07003516 if (err < 0 && err != -EOPNOTSUPP) {
3517 if (likely(skb->len))
3518 break;
3519
3520 goto out_err;
3521 }
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003522 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00003523 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00003524 }
3525
3526 if (ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003527 if (idx >= cb->args[0]) {
3528 err = ops->ndo_bridge_getlink(skb, portid,
3529 seq, dev,
3530 filter_mask,
3531 NLM_F_MULTI);
David Ahernf6c57752017-05-15 23:19:17 -07003532 if (err < 0 && err != -EOPNOTSUPP) {
3533 if (likely(skb->len))
3534 break;
3535
3536 goto out_err;
3537 }
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003538 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00003539 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00003540 }
3541 }
David Ahernf6c57752017-05-15 23:19:17 -07003542 err = skb->len;
3543out_err:
John Fastabende5a55a82012-10-24 08:12:57 +00003544 rcu_read_unlock();
3545 cb->args[0] = idx;
3546
David Ahernf6c57752017-05-15 23:19:17 -07003547 return err;
John Fastabende5a55a82012-10-24 08:12:57 +00003548}
3549
John Fastabend2469ffd2012-10-24 08:13:03 +00003550static inline size_t bridge_nlmsg_size(void)
3551{
3552 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3553 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3554 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3555 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
3556 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
3557 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
3558 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
3559 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
3560 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
3561 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
3562 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
3563}
3564
Roopa Prabhu02dba432015-01-14 20:02:25 -08003565static int rtnl_bridge_notify(struct net_device *dev)
John Fastabend2469ffd2012-10-24 08:13:03 +00003566{
3567 struct net *net = dev_net(dev);
John Fastabend2469ffd2012-10-24 08:13:03 +00003568 struct sk_buff *skb;
3569 int err = -EOPNOTSUPP;
3570
Roopa Prabhu02dba432015-01-14 20:02:25 -08003571 if (!dev->netdev_ops->ndo_bridge_getlink)
3572 return 0;
3573
John Fastabend2469ffd2012-10-24 08:13:03 +00003574 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
3575 if (!skb) {
3576 err = -ENOMEM;
3577 goto errout;
3578 }
3579
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02003580 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
Roopa Prabhu02dba432015-01-14 20:02:25 -08003581 if (err < 0)
3582 goto errout;
John Fastabend2469ffd2012-10-24 08:13:03 +00003583
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08003584 if (!skb->len)
3585 goto errout;
3586
John Fastabend2469ffd2012-10-24 08:13:03 +00003587 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
3588 return 0;
3589errout:
3590 WARN_ON(err == -EMSGSIZE);
3591 kfree_skb(skb);
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08003592 if (err)
3593 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
John Fastabend2469ffd2012-10-24 08:13:03 +00003594 return err;
3595}
3596
David Ahernc21ef3e2017-04-16 09:48:24 -07003597static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3598 struct netlink_ext_ack *extack)
John Fastabende5a55a82012-10-24 08:12:57 +00003599{
3600 struct net *net = sock_net(skb->sk);
3601 struct ifinfomsg *ifm;
3602 struct net_device *dev;
John Fastabend2469ffd2012-10-24 08:13:03 +00003603 struct nlattr *br_spec, *attr = NULL;
3604 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02003605 u16 flags = 0;
John Fastabendc38e01b2012-11-02 16:32:36 +00003606 bool have_flags = false;
John Fastabende5a55a82012-10-24 08:12:57 +00003607
3608 if (nlmsg_len(nlh) < sizeof(*ifm))
3609 return -EINVAL;
3610
3611 ifm = nlmsg_data(nlh);
3612 if (ifm->ifi_family != AF_BRIDGE)
3613 return -EPFNOSUPPORT;
3614
3615 dev = __dev_get_by_index(net, ifm->ifi_index);
3616 if (!dev) {
3617 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3618 return -ENODEV;
3619 }
3620
John Fastabend2469ffd2012-10-24 08:13:03 +00003621 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3622 if (br_spec) {
3623 nla_for_each_nested(attr, br_spec, rem) {
3624 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01003625 if (nla_len(attr) < sizeof(flags))
3626 return -EINVAL;
3627
John Fastabendc38e01b2012-11-02 16:32:36 +00003628 have_flags = true;
John Fastabend2469ffd2012-10-24 08:13:03 +00003629 flags = nla_get_u16(attr);
3630 break;
3631 }
3632 }
3633 }
3634
3635 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003636 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3637
3638 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
John Fastabend2469ffd2012-10-24 08:13:03 +00003639 err = -EOPNOTSUPP;
3640 goto out;
3641 }
3642
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003643 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
John Fastabende5a55a82012-10-24 08:12:57 +00003644 if (err)
3645 goto out;
John Fastabend2469ffd2012-10-24 08:13:03 +00003646
3647 flags &= ~BRIDGE_FLAGS_MASTER;
John Fastabende5a55a82012-10-24 08:12:57 +00003648 }
3649
John Fastabend2469ffd2012-10-24 08:13:03 +00003650 if ((flags & BRIDGE_FLAGS_SELF)) {
3651 if (!dev->netdev_ops->ndo_bridge_setlink)
3652 err = -EOPNOTSUPP;
3653 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003654 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
3655 flags);
Roopa Prabhu02dba432015-01-14 20:02:25 -08003656 if (!err) {
John Fastabend2469ffd2012-10-24 08:13:03 +00003657 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08003658
3659 /* Generate event to notify upper layer of bridge
3660 * change
3661 */
3662 err = rtnl_bridge_notify(dev);
3663 }
John Fastabend2469ffd2012-10-24 08:13:03 +00003664 }
3665
John Fastabendc38e01b2012-11-02 16:32:36 +00003666 if (have_flags)
John Fastabend2469ffd2012-10-24 08:13:03 +00003667 memcpy(nla_data(attr), &flags, sizeof(flags));
John Fastabende5a55a82012-10-24 08:12:57 +00003668out:
3669 return err;
3670}
3671
David Ahernc21ef3e2017-04-16 09:48:24 -07003672static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
3673 struct netlink_ext_ack *extack)
Vlad Yasevich407af322013-02-13 12:00:12 +00003674{
3675 struct net *net = sock_net(skb->sk);
3676 struct ifinfomsg *ifm;
3677 struct net_device *dev;
3678 struct nlattr *br_spec, *attr = NULL;
3679 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02003680 u16 flags = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +00003681 bool have_flags = false;
3682
3683 if (nlmsg_len(nlh) < sizeof(*ifm))
3684 return -EINVAL;
3685
3686 ifm = nlmsg_data(nlh);
3687 if (ifm->ifi_family != AF_BRIDGE)
3688 return -EPFNOSUPPORT;
3689
3690 dev = __dev_get_by_index(net, ifm->ifi_index);
3691 if (!dev) {
3692 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3693 return -ENODEV;
3694 }
3695
3696 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3697 if (br_spec) {
3698 nla_for_each_nested(attr, br_spec, rem) {
3699 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01003700 if (nla_len(attr) < sizeof(flags))
3701 return -EINVAL;
3702
Vlad Yasevich407af322013-02-13 12:00:12 +00003703 have_flags = true;
3704 flags = nla_get_u16(attr);
3705 break;
3706 }
3707 }
3708 }
3709
Vlad Yasevich407af322013-02-13 12:00:12 +00003710 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3711 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3712
3713 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
3714 err = -EOPNOTSUPP;
3715 goto out;
3716 }
3717
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003718 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00003719 if (err)
3720 goto out;
3721
3722 flags &= ~BRIDGE_FLAGS_MASTER;
3723 }
3724
3725 if ((flags & BRIDGE_FLAGS_SELF)) {
3726 if (!dev->netdev_ops->ndo_bridge_dellink)
3727 err = -EOPNOTSUPP;
3728 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003729 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
3730 flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00003731
Roopa Prabhu02dba432015-01-14 20:02:25 -08003732 if (!err) {
Vlad Yasevich407af322013-02-13 12:00:12 +00003733 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08003734
3735 /* Generate event to notify upper layer of bridge
3736 * change
3737 */
3738 err = rtnl_bridge_notify(dev);
3739 }
Vlad Yasevich407af322013-02-13 12:00:12 +00003740 }
3741
3742 if (have_flags)
3743 memcpy(nla_data(attr), &flags, sizeof(flags));
Vlad Yasevich407af322013-02-13 12:00:12 +00003744out:
3745 return err;
3746}
3747
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003748static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
3749{
3750 return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
3751 (!idxattr || idxattr == attrid);
3752}
3753
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02003754#define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
3755static int rtnl_get_offload_stats_attr_size(int attr_id)
3756{
3757 switch (attr_id) {
3758 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
3759 return sizeof(struct rtnl_link_stats64);
3760 }
3761
3762 return 0;
3763}
3764
3765static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
3766 int *prividx)
3767{
3768 struct nlattr *attr = NULL;
3769 int attr_id, size;
3770 void *attr_data;
3771 int err;
3772
3773 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
3774 dev->netdev_ops->ndo_get_offload_stats))
3775 return -ENODATA;
3776
3777 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
3778 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
3779 if (attr_id < *prividx)
3780 continue;
3781
3782 size = rtnl_get_offload_stats_attr_size(attr_id);
3783 if (!size)
3784 continue;
3785
Or Gerlitz3df5b3c2016-11-22 23:09:54 +02003786 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02003787 continue;
3788
3789 attr = nla_reserve_64bit(skb, attr_id, size,
3790 IFLA_OFFLOAD_XSTATS_UNSPEC);
3791 if (!attr)
3792 goto nla_put_failure;
3793
3794 attr_data = nla_data(attr);
3795 memset(attr_data, 0, size);
3796 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
3797 attr_data);
3798 if (err)
3799 goto get_offload_stats_failure;
3800 }
3801
3802 if (!attr)
3803 return -ENODATA;
3804
3805 *prividx = 0;
3806 return 0;
3807
3808nla_put_failure:
3809 err = -EMSGSIZE;
3810get_offload_stats_failure:
3811 *prividx = attr_id;
3812 return err;
3813}
3814
3815static int rtnl_get_offload_stats_size(const struct net_device *dev)
3816{
3817 int nla_size = 0;
3818 int attr_id;
3819 int size;
3820
3821 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
3822 dev->netdev_ops->ndo_get_offload_stats))
3823 return 0;
3824
3825 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
3826 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
Or Gerlitz3df5b3c2016-11-22 23:09:54 +02003827 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02003828 continue;
3829 size = rtnl_get_offload_stats_attr_size(attr_id);
3830 nla_size += nla_total_size_64bit(size);
3831 }
3832
3833 if (nla_size != 0)
3834 nla_size += nla_total_size(0);
3835
3836 return nla_size;
3837}
3838
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003839static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
3840 int type, u32 pid, u32 seq, u32 change,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003841 unsigned int flags, unsigned int filter_mask,
3842 int *idxattr, int *prividx)
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003843{
3844 struct if_stats_msg *ifsm;
3845 struct nlmsghdr *nlh;
3846 struct nlattr *attr;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003847 int s_prividx = *prividx;
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02003848 int err;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003849
3850 ASSERT_RTNL();
3851
3852 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
3853 if (!nlh)
3854 return -EMSGSIZE;
3855
3856 ifsm = nlmsg_data(nlh);
3857 ifsm->ifindex = dev->ifindex;
3858 ifsm->filter_mask = filter_mask;
3859
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003860 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003861 struct rtnl_link_stats64 *sp;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003862
Nicolas Dichtel58414d32016-04-21 18:58:25 +02003863 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
3864 sizeof(struct rtnl_link_stats64),
3865 IFLA_STATS_UNSPEC);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003866 if (!attr)
3867 goto nla_put_failure;
3868
3869 sp = nla_data(attr);
3870 dev_get_stats(dev, sp);
3871 }
3872
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02003873 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
3874 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
3875
3876 if (ops && ops->fill_linkxstats) {
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02003877 *idxattr = IFLA_STATS_LINK_XSTATS;
3878 attr = nla_nest_start(skb,
3879 IFLA_STATS_LINK_XSTATS);
3880 if (!attr)
3881 goto nla_put_failure;
3882
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02003883 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
3884 nla_nest_end(skb, attr);
3885 if (err)
3886 goto nla_put_failure;
3887 *idxattr = 0;
3888 }
3889 }
3890
3891 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
3892 *idxattr)) {
3893 const struct rtnl_link_ops *ops = NULL;
3894 const struct net_device *master;
3895
3896 master = netdev_master_upper_dev_get(dev);
3897 if (master)
3898 ops = master->rtnl_link_ops;
3899 if (ops && ops->fill_linkxstats) {
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02003900 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
3901 attr = nla_nest_start(skb,
3902 IFLA_STATS_LINK_XSTATS_SLAVE);
3903 if (!attr)
3904 goto nla_put_failure;
3905
3906 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02003907 nla_nest_end(skb, attr);
3908 if (err)
3909 goto nla_put_failure;
3910 *idxattr = 0;
3911 }
3912 }
3913
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02003914 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
3915 *idxattr)) {
3916 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
3917 attr = nla_nest_start(skb, IFLA_STATS_LINK_OFFLOAD_XSTATS);
3918 if (!attr)
3919 goto nla_put_failure;
3920
3921 err = rtnl_get_offload_stats(skb, dev, prividx);
3922 if (err == -ENODATA)
3923 nla_nest_cancel(skb, attr);
3924 else
3925 nla_nest_end(skb, attr);
3926
3927 if (err && err != -ENODATA)
3928 goto nla_put_failure;
3929 *idxattr = 0;
3930 }
3931
Robert Shearmanaefb4d42017-01-16 14:16:36 +00003932 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
3933 struct rtnl_af_ops *af_ops;
3934
3935 *idxattr = IFLA_STATS_AF_SPEC;
3936 attr = nla_nest_start(skb, IFLA_STATS_AF_SPEC);
3937 if (!attr)
3938 goto nla_put_failure;
3939
3940 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
3941 if (af_ops->fill_stats_af) {
3942 struct nlattr *af;
3943 int err;
3944
3945 af = nla_nest_start(skb, af_ops->family);
3946 if (!af)
3947 goto nla_put_failure;
3948
3949 err = af_ops->fill_stats_af(skb, dev);
3950
3951 if (err == -ENODATA)
3952 nla_nest_cancel(skb, af);
3953 else if (err < 0)
3954 goto nla_put_failure;
3955
3956 nla_nest_end(skb, af);
3957 }
3958 }
3959
3960 nla_nest_end(skb, attr);
3961
3962 *idxattr = 0;
3963 }
3964
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003965 nlmsg_end(skb, nlh);
3966
3967 return 0;
3968
3969nla_put_failure:
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003970 /* not a multi message or no progress mean a real error */
3971 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
3972 nlmsg_cancel(skb, nlh);
3973 else
3974 nlmsg_end(skb, nlh);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003975
3976 return -EMSGSIZE;
3977}
3978
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003979static size_t if_nlmsg_stats_size(const struct net_device *dev,
3980 u32 filter_mask)
3981{
3982 size_t size = 0;
3983
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003984 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003985 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
3986
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02003987 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
3988 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02003989 int attr = IFLA_STATS_LINK_XSTATS;
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02003990
3991 if (ops && ops->get_linkxstats_size) {
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02003992 size += nla_total_size(ops->get_linkxstats_size(dev,
3993 attr));
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02003994 /* for IFLA_STATS_LINK_XSTATS */
3995 size += nla_total_size(0);
3996 }
3997 }
3998
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02003999 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
4000 struct net_device *_dev = (struct net_device *)dev;
4001 const struct rtnl_link_ops *ops = NULL;
4002 const struct net_device *master;
4003
4004 /* netdev_master_upper_dev_get can't take const */
4005 master = netdev_master_upper_dev_get(_dev);
4006 if (master)
4007 ops = master->rtnl_link_ops;
4008 if (ops && ops->get_linkxstats_size) {
4009 int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
4010
4011 size += nla_total_size(ops->get_linkxstats_size(dev,
4012 attr));
4013 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
4014 size += nla_total_size(0);
4015 }
4016 }
4017
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004018 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
4019 size += rtnl_get_offload_stats_size(dev);
4020
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004021 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
4022 struct rtnl_af_ops *af_ops;
4023
4024 /* for IFLA_STATS_AF_SPEC */
4025 size += nla_total_size(0);
4026
4027 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
4028 if (af_ops->get_stats_af_size) {
4029 size += nla_total_size(
4030 af_ops->get_stats_af_size(dev));
4031
4032 /* for AF_* */
4033 size += nla_total_size(0);
4034 }
4035 }
4036 }
4037
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004038 return size;
4039}
4040
David Ahernc21ef3e2017-04-16 09:48:24 -07004041static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
4042 struct netlink_ext_ack *extack)
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004043{
4044 struct net *net = sock_net(skb->sk);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004045 struct net_device *dev = NULL;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004046 int idxattr = 0, prividx = 0;
4047 struct if_stats_msg *ifsm;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004048 struct sk_buff *nskb;
4049 u32 filter_mask;
4050 int err;
4051
Mathias Krause4775cc12016-12-28 17:52:15 +01004052 if (nlmsg_len(nlh) < sizeof(*ifsm))
4053 return -EINVAL;
4054
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004055 ifsm = nlmsg_data(nlh);
4056 if (ifsm->ifindex > 0)
4057 dev = __dev_get_by_index(net, ifsm->ifindex);
4058 else
4059 return -EINVAL;
4060
4061 if (!dev)
4062 return -ENODEV;
4063
4064 filter_mask = ifsm->filter_mask;
4065 if (!filter_mask)
4066 return -EINVAL;
4067
4068 nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
4069 if (!nskb)
4070 return -ENOBUFS;
4071
4072 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
4073 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004074 0, filter_mask, &idxattr, &prividx);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004075 if (err < 0) {
4076 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
4077 WARN_ON(err == -EMSGSIZE);
4078 kfree_skb(nskb);
4079 } else {
4080 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
4081 }
4082
4083 return err;
4084}
4085
4086static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
4087{
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004088 int h, s_h, err, s_idx, s_idxattr, s_prividx;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004089 struct net *net = sock_net(skb->sk);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004090 unsigned int flags = NLM_F_MULTI;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004091 struct if_stats_msg *ifsm;
4092 struct hlist_head *head;
4093 struct net_device *dev;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004094 u32 filter_mask = 0;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004095 int idx = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004096
4097 s_h = cb->args[0];
4098 s_idx = cb->args[1];
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004099 s_idxattr = cb->args[2];
4100 s_prividx = cb->args[3];
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004101
4102 cb->seq = net->dev_base_seq;
4103
Mathias Krause4775cc12016-12-28 17:52:15 +01004104 if (nlmsg_len(cb->nlh) < sizeof(*ifsm))
4105 return -EINVAL;
4106
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004107 ifsm = nlmsg_data(cb->nlh);
4108 filter_mask = ifsm->filter_mask;
4109 if (!filter_mask)
4110 return -EINVAL;
4111
4112 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4113 idx = 0;
4114 head = &net->dev_index_head[h];
4115 hlist_for_each_entry(dev, head, index_hlist) {
4116 if (idx < s_idx)
4117 goto cont;
4118 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
4119 NETLINK_CB(cb->skb).portid,
4120 cb->nlh->nlmsg_seq, 0,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004121 flags, filter_mask,
4122 &s_idxattr, &s_prividx);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004123 /* If we ran out of room on the first message,
4124 * we're in trouble
4125 */
4126 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
4127
4128 if (err < 0)
4129 goto out;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004130 s_prividx = 0;
4131 s_idxattr = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004132 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
4133cont:
4134 idx++;
4135 }
4136 }
4137out:
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004138 cb->args[3] = s_prividx;
4139 cb->args[2] = s_idxattr;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004140 cb->args[1] = idx;
4141 cb->args[0] = h;
4142
4143 return skb->len;
4144}
4145
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146/* Process one rtnetlink message. */
4147
Johannes Berg2d4bc932017-04-12 14:34:04 +02004148static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
4149 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004151 struct net *net = sock_net(skb->sk);
Florian Westphal6853dd42017-08-09 20:41:51 +02004152 struct rtnl_link *handlers;
4153 int err = -EOPNOTSUPP;
Thomas Grafe2849862007-03-22 11:48:11 -07004154 rtnl_doit_func doit;
Florian Westphal62256f92017-08-09 20:41:52 +02004155 unsigned int flags;
Alexander Kuleshov617cfc72016-01-10 21:26:57 +06004156 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 int family;
4158 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 if (type > RTM_MAX)
Thomas Graf038890f2007-04-05 14:35:52 -07004162 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163
4164 type -= RTM_BASE;
4165
4166 /* All the messages must have at least 1 byte length */
Hong zhi guo573ce262013-03-27 06:47:04 +00004167 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 return 0;
4169
Hong zhi guo573ce262013-03-27 06:47:04 +00004170 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 kind = type&3;
4172
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07004173 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07004174 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175
Florian Westphal5c2bb9b2017-08-10 16:52:58 +02004176 if (family >= ARRAY_SIZE(rtnl_msg_handlers))
Florian Westphal6853dd42017-08-09 20:41:51 +02004177 family = PF_UNSPEC;
4178
4179 rcu_read_lock();
4180 handlers = rcu_dereference(rtnl_msg_handlers[family]);
4181 if (!handlers) {
4182 family = PF_UNSPEC;
4183 handlers = rcu_dereference(rtnl_msg_handlers[family]);
4184 }
4185
David S. Millerb8f3ab42011-01-18 12:40:38 -08004186 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004187 struct sock *rtnl;
Thomas Grafe2849862007-03-22 11:48:11 -07004188 rtnl_dumpit_func dumpit;
Greg Rosec7ac8672011-06-10 01:27:09 +00004189 u16 min_dump_alloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190
Florian Westphal6853dd42017-08-09 20:41:51 +02004191 dumpit = READ_ONCE(handlers[type].dumpit);
4192 if (!dumpit) {
4193 family = PF_UNSPEC;
4194 handlers = rcu_dereference(rtnl_msg_handlers[PF_UNSPEC]);
4195 if (!handlers)
4196 goto err_unlock;
Florian Westphal0cc09022017-08-09 20:41:50 +02004197
Florian Westphal6853dd42017-08-09 20:41:51 +02004198 dumpit = READ_ONCE(handlers[type].dumpit);
4199 if (!dumpit)
4200 goto err_unlock;
4201 }
Florian Westphale1fa6d22017-08-09 20:41:47 +02004202
Florian Westphal019a3162017-08-09 20:41:49 +02004203 refcount_inc(&rtnl_msg_handlers_ref[family]);
4204
Florian Westphal5c2bb9b2017-08-10 16:52:58 +02004205 if (type == RTM_GETLINK - RTM_BASE)
Florian Westphale1fa6d22017-08-09 20:41:47 +02004206 min_dump_alloc = rtnl_calcit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207
Florian Westphal6853dd42017-08-09 20:41:51 +02004208 rcu_read_unlock();
4209
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004210 rtnl = net->rtnl;
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00004211 {
4212 struct netlink_dump_control c = {
4213 .dump = dumpit,
4214 .min_dump_alloc = min_dump_alloc,
4215 };
4216 err = netlink_dump_start(rtnl, skb, nlh, &c);
4217 }
Florian Westphal019a3162017-08-09 20:41:49 +02004218 refcount_dec(&rtnl_msg_handlers_ref[family]);
Eric Dumazet2907c352011-05-25 07:34:04 +00004219 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 }
4221
Florian Westphal8caa38b2017-08-10 16:53:01 +02004222 doit = READ_ONCE(handlers[type].doit);
4223 if (!doit) {
4224 family = PF_UNSPEC;
4225 handlers = rcu_dereference(rtnl_msg_handlers[family]);
4226 }
4227
Florian Westphal62256f92017-08-09 20:41:52 +02004228 flags = READ_ONCE(handlers[type].flags);
4229 if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
4230 refcount_inc(&rtnl_msg_handlers_ref[family]);
4231 doit = READ_ONCE(handlers[type].doit);
4232 rcu_read_unlock();
4233 if (doit)
4234 err = doit(skb, nlh, extack);
4235 refcount_dec(&rtnl_msg_handlers_ref[family]);
4236 return err;
4237 }
4238
Florian Westphal6853dd42017-08-09 20:41:51 +02004239 rcu_read_unlock();
4240
Florian Westphal0cc09022017-08-09 20:41:50 +02004241 rtnl_lock();
Florian Westphal6853dd42017-08-09 20:41:51 +02004242 handlers = rtnl_dereference(rtnl_msg_handlers[family]);
4243 if (handlers) {
4244 doit = READ_ONCE(handlers[type].doit);
4245 if (doit)
4246 err = doit(skb, nlh, extack);
4247 }
Florian Westphal0cc09022017-08-09 20:41:50 +02004248 rtnl_unlock();
Florian Westphal0cc09022017-08-09 20:41:50 +02004249 return err;
4250
4251err_unlock:
Florian Westphal6853dd42017-08-09 20:41:51 +02004252 rcu_read_unlock();
Florian Westphal0cc09022017-08-09 20:41:50 +02004253 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254}
4255
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07004256static void rtnetlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07004258 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259}
4260
Julien Gomes5f729ea2017-06-20 13:54:16 -07004261static int rtnetlink_bind(struct net *net, int group)
4262{
4263 switch (group) {
4264 case RTNLGRP_IPV4_MROUTE_R:
4265 case RTNLGRP_IPV6_MROUTE_R:
4266 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
4267 return -EPERM;
4268 break;
4269 }
4270 return 0;
4271}
4272
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
4274{
Jiri Pirko351638e2013-05-28 01:30:21 +00004275 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02004276
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 switch (event) {
Vlad Yasevich5138e862017-04-04 09:23:41 -04004278 case NETDEV_REBOOT:
David Ahern37536542017-07-19 10:22:40 -07004279 case NETDEV_CHANGEADDR:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004280 case NETDEV_CHANGENAME:
4281 case NETDEV_FEAT_CHANGE:
4282 case NETDEV_BONDING_FAILOVER:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004283 case NETDEV_NOTIFY_PEERS:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004284 case NETDEV_RESEND_IGMP:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004285 case NETDEV_CHANGEINFODATA:
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04004286 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
4287 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 break;
4289 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 break;
4291 }
4292 return NOTIFY_DONE;
4293}
4294
4295static struct notifier_block rtnetlink_dev_notifier = {
4296 .notifier_call = rtnetlink_event,
4297};
4298
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004299
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004300static int __net_init rtnetlink_net_init(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004301{
4302 struct sock *sk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00004303 struct netlink_kernel_cfg cfg = {
4304 .groups = RTNLGRP_MAX,
4305 .input = rtnetlink_rcv,
4306 .cb_mutex = &rtnl_mutex,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00004307 .flags = NL_CFG_F_NONROOT_RECV,
Julien Gomes5f729ea2017-06-20 13:54:16 -07004308 .bind = rtnetlink_bind,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00004309 };
4310
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00004311 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004312 if (!sk)
4313 return -ENOMEM;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004314 net->rtnl = sk;
4315 return 0;
4316}
4317
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004318static void __net_exit rtnetlink_net_exit(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004319{
Denis V. Lunev775516b2008-01-18 23:55:19 -08004320 netlink_kernel_release(net->rtnl);
4321 net->rtnl = NULL;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004322}
4323
4324static struct pernet_operations rtnetlink_net_ops = {
4325 .init = rtnetlink_net_init,
4326 .exit = rtnetlink_net_exit,
4327};
4328
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329void __init rtnetlink_init(void)
4330{
Florian Westphald38a6512017-08-10 16:53:00 +02004331 int i;
4332
4333 for (i = 0; i < ARRAY_SIZE(rtnl_msg_handlers_ref); i++)
4334 refcount_set(&rtnl_msg_handlers_ref[i], 1);
4335
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004336 if (register_pernet_subsys(&rtnetlink_net_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 panic("rtnetlink_init: cannot initialize rtnetlink\n");
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004338
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 register_netdevice_notifier(&rtnetlink_dev_notifier);
Thomas Graf340d17f2007-03-22 11:49:22 -07004340
Greg Rosec7ac8672011-06-10 01:27:09 +00004341 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
Florian Westphalb97bac62017-08-09 20:41:48 +02004342 rtnl_dump_ifinfo, 0);
4343 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
4344 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
4345 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
Thomas Graf687ad8c2007-03-22 11:59:42 -07004346
Florian Westphalb97bac62017-08-09 20:41:48 +02004347 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
4348 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
4349 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
John Fastabend77162022012-04-15 06:43:56 +00004350
Florian Westphalb97bac62017-08-09 20:41:48 +02004351 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
4352 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
4353 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, 0);
John Fastabende5a55a82012-10-24 08:12:57 +00004354
Florian Westphalb97bac62017-08-09 20:41:48 +02004355 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
4356 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
4357 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004358
4359 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
Florian Westphalb97bac62017-08-09 20:41:48 +02004360 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361}