blob: 8a8c51937edff6bf59a6813dd8de88fef9962bb7 [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
Florian Westphal5fa85a02017-10-16 15:44:36 +0200456 list_for_each_entry_rcu(ops, &rtnl_af_ops, list) {
Thomas Graff8ff1822010-11-16 04:30:14 +0000457 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();
Florian Westphal5fa85a02017-10-16 15:44:36 +0200473 list_add_tail_rcu(&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/**
Thomas Graff8ff1822010-11-16 04:30:14 +0000479 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
480 * @ops: struct rtnl_af_ops * to unregister
481 */
482void rtnl_af_unregister(struct rtnl_af_ops *ops)
483{
484 rtnl_lock();
Florian Westphal5fa85a02017-10-16 15:44:36 +0200485 list_del_rcu(&ops->list);
Thomas Graff8ff1822010-11-16 04:30:14 +0000486 rtnl_unlock();
Florian Westphal5fa85a02017-10-16 15:44:36 +0200487
488 synchronize_rcu();
Thomas Graff8ff1822010-11-16 04:30:14 +0000489}
490EXPORT_SYMBOL_GPL(rtnl_af_unregister);
491
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700492static size_t rtnl_link_get_af_size(const struct net_device *dev,
493 u32 ext_filter_mask)
Thomas Graff8ff1822010-11-16 04:30:14 +0000494{
495 struct rtnl_af_ops *af_ops;
496 size_t size;
497
498 /* IFLA_AF_SPEC */
499 size = nla_total_size(sizeof(struct nlattr));
500
Florian Westphal5fa85a02017-10-16 15:44:36 +0200501 rcu_read_lock();
502 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Thomas Graff8ff1822010-11-16 04:30:14 +0000503 if (af_ops->get_link_af_size) {
504 /* AF_* + nested data */
505 size += nla_total_size(sizeof(struct nlattr)) +
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700506 af_ops->get_link_af_size(dev, ext_filter_mask);
Thomas Graff8ff1822010-11-16 04:30:14 +0000507 }
508 }
Florian Westphal5fa85a02017-10-16 15:44:36 +0200509 rcu_read_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +0000510
511 return size;
512}
513
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100514static bool rtnl_have_link_slave_info(const struct net_device *dev)
515{
516 struct net_device *master_dev;
Florian Westphal4c82a952017-09-26 13:58:43 +0200517 bool ret = false;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100518
Florian Westphal4c82a952017-09-26 13:58:43 +0200519 rcu_read_lock();
520
521 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
Jiri Pirko813f0202014-01-23 19:19:21 +0100522 if (master_dev && master_dev->rtnl_link_ops)
Florian Westphal4c82a952017-09-26 13:58:43 +0200523 ret = true;
524 rcu_read_unlock();
525 return ret;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100526}
527
528static int rtnl_link_slave_info_fill(struct sk_buff *skb,
529 const struct net_device *dev)
530{
531 struct net_device *master_dev;
532 const struct rtnl_link_ops *ops;
533 struct nlattr *slave_data;
534 int err;
535
536 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
537 if (!master_dev)
538 return 0;
539 ops = master_dev->rtnl_link_ops;
540 if (!ops)
541 return 0;
542 if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
543 return -EMSGSIZE;
544 if (ops->fill_slave_info) {
545 slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA);
546 if (!slave_data)
547 return -EMSGSIZE;
548 err = ops->fill_slave_info(skb, master_dev, dev);
549 if (err < 0)
550 goto err_cancel_slave_data;
551 nla_nest_end(skb, slave_data);
552 }
553 return 0;
554
555err_cancel_slave_data:
556 nla_nest_cancel(skb, slave_data);
557 return err;
558}
559
560static int rtnl_link_info_fill(struct sk_buff *skb,
561 const struct net_device *dev)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700562{
563 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100564 struct nlattr *data;
565 int err;
566
567 if (!ops)
568 return 0;
569 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
570 return -EMSGSIZE;
571 if (ops->fill_xstats) {
572 err = ops->fill_xstats(skb, dev);
573 if (err < 0)
574 return err;
575 }
576 if (ops->fill_info) {
577 data = nla_nest_start(skb, IFLA_INFO_DATA);
578 if (data == NULL)
579 return -EMSGSIZE;
580 err = ops->fill_info(skb, dev);
581 if (err < 0)
582 goto err_cancel_data;
583 nla_nest_end(skb, data);
584 }
585 return 0;
586
587err_cancel_data:
588 nla_nest_cancel(skb, data);
589 return err;
590}
591
592static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
593{
594 struct nlattr *linkinfo;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700595 int err = -EMSGSIZE;
596
597 linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
598 if (linkinfo == NULL)
599 goto out;
600
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100601 err = rtnl_link_info_fill(skb, dev);
602 if (err < 0)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700603 goto err_cancel_link;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100604
605 err = rtnl_link_slave_info_fill(skb, dev);
606 if (err < 0)
607 goto err_cancel_link;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700608
609 nla_nest_end(skb, linkinfo);
610 return 0;
611
Patrick McHardy38f7b872007-06-13 12:03:51 -0700612err_cancel_link:
613 nla_nest_cancel(skb, linkinfo);
614out:
615 return err;
616}
617
Eric Dumazet95c96172012-04-15 05:58:06 +0000618int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800620 struct sock *rtnl = net->rtnl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 int err = 0;
622
Patrick McHardyac6d4392005-08-14 19:29:52 -0700623 NETLINK_CB(skb).dst_group = group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (echo)
Reshetova, Elena63354792017-06-30 13:07:58 +0300625 refcount_inc(&skb->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
627 if (echo)
628 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
629 return err;
630}
631
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800632int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
Thomas Graf2942e902006-08-15 00:30:25 -0700633{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800634 struct sock *rtnl = net->rtnl;
635
Thomas Graf2942e902006-08-15 00:30:25 -0700636 return nlmsg_unicast(rtnl, skb, pid);
637}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800638EXPORT_SYMBOL(rtnl_unicast);
Thomas Graf2942e902006-08-15 00:30:25 -0700639
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800640void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
641 struct nlmsghdr *nlh, gfp_t flags)
Thomas Graf97676b62006-08-15 00:31:41 -0700642{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800643 struct sock *rtnl = net->rtnl;
Thomas Graf97676b62006-08-15 00:31:41 -0700644 int report = 0;
645
646 if (nlh)
647 report = nlmsg_report(nlh);
648
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800649 nlmsg_notify(rtnl, skb, pid, group, report, flags);
Thomas Graf97676b62006-08-15 00:31:41 -0700650}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800651EXPORT_SYMBOL(rtnl_notify);
Thomas Graf97676b62006-08-15 00:31:41 -0700652
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800653void rtnl_set_sk_err(struct net *net, u32 group, int error)
Thomas Graf97676b62006-08-15 00:31:41 -0700654{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800655 struct sock *rtnl = net->rtnl;
656
Thomas Graf97676b62006-08-15 00:31:41 -0700657 netlink_set_err(rtnl, 0, group, error);
658}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800659EXPORT_SYMBOL(rtnl_set_sk_err);
Thomas Graf97676b62006-08-15 00:31:41 -0700660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
662{
Thomas Graf2d7202b2006-08-22 00:01:27 -0700663 struct nlattr *mx;
664 int i, valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Thomas Graf2d7202b2006-08-22 00:01:27 -0700666 mx = nla_nest_start(skb, RTA_METRICS);
667 if (mx == NULL)
668 return -ENOBUFS;
669
670 for (i = 0; i < RTAX_MAX; i++) {
671 if (metrics[i]) {
Daniel Borkmannea697632015-01-05 23:57:47 +0100672 if (i == RTAX_CC_ALGO - 1) {
673 char tmp[TCP_CA_NAME_MAX], *name;
674
675 name = tcp_ca_get_name_by_key(metrics[i], tmp);
676 if (!name)
677 continue;
678 if (nla_put_string(skb, i + 1, name))
679 goto nla_put_failure;
Daniel Borkmannc3a8d942015-08-31 15:58:47 +0200680 } else if (i == RTAX_FEATURES - 1) {
681 u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
682
Phil Sutterf8edcd12016-08-23 13:14:31 +0200683 if (!user_features)
684 continue;
Daniel Borkmannc3a8d942015-08-31 15:58:47 +0200685 BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
686 if (nla_put_u32(skb, i + 1, user_features))
687 goto nla_put_failure;
Daniel Borkmannea697632015-01-05 23:57:47 +0100688 } else {
689 if (nla_put_u32(skb, i + 1, metrics[i]))
690 goto nla_put_failure;
691 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700692 valid++;
Thomas Graf2d7202b2006-08-22 00:01:27 -0700693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
David S. Millera57d27f2006-08-22 22:20:14 -0700696 if (!valid) {
697 nla_nest_cancel(skb, mx);
698 return 0;
699 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700700
701 return nla_nest_end(skb, mx);
702
703nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700704 nla_nest_cancel(skb, mx);
705 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800707EXPORT_SYMBOL(rtnetlink_put_metrics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Thomas Grafe3703b32006-11-27 09:27:07 -0800709int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
David S. Miller87a50692012-07-10 05:06:14 -0700710 long expires, u32 error)
Thomas Grafe3703b32006-11-27 09:27:07 -0800711{
712 struct rta_cacheinfo ci = {
Eric Dumazeta399a802012-08-08 21:13:53 +0000713 .rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse),
Thomas Grafe3703b32006-11-27 09:27:07 -0800714 .rta_used = dst->__use,
715 .rta_clntref = atomic_read(&(dst->__refcnt)),
716 .rta_error = error,
717 .rta_id = id,
Thomas Grafe3703b32006-11-27 09:27:07 -0800718 };
719
Li Wei82539472012-07-29 16:01:30 +0000720 if (expires) {
721 unsigned long clock;
Thomas Grafe3703b32006-11-27 09:27:07 -0800722
Li Wei82539472012-07-29 16:01:30 +0000723 clock = jiffies_to_clock_t(abs(expires));
724 clock = min_t(unsigned long, clock, INT_MAX);
725 ci.rta_expires = (expires > 0) ? clock : -clock;
726 }
Thomas Grafe3703b32006-11-27 09:27:07 -0800727 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
728}
Thomas Grafe3703b32006-11-27 09:27:07 -0800729EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
David S. Miller93b2d4a2008-02-17 18:35:07 -0800731static void set_operstate(struct net_device *dev, unsigned char transition)
Stefan Rompfb00055a2006-03-20 17:09:11 -0800732{
733 unsigned char operstate = dev->operstate;
734
Eric Dumazete0d087a2009-11-07 01:26:17 -0800735 switch (transition) {
Stefan Rompfb00055a2006-03-20 17:09:11 -0800736 case IF_OPER_UP:
737 if ((operstate == IF_OPER_DORMANT ||
738 operstate == IF_OPER_UNKNOWN) &&
739 !netif_dormant(dev))
740 operstate = IF_OPER_UP;
741 break;
742
743 case IF_OPER_DORMANT:
744 if (operstate == IF_OPER_UP ||
745 operstate == IF_OPER_UNKNOWN)
746 operstate = IF_OPER_DORMANT;
747 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700748 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800749
750 if (dev->operstate != operstate) {
751 write_lock_bh(&dev_base_lock);
752 dev->operstate = operstate;
753 write_unlock_bh(&dev_base_lock);
David S. Miller93b2d4a2008-02-17 18:35:07 -0800754 netdev_state_change(dev);
755 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800756}
757
Jiri Bencb1beb682012-07-27 02:58:22 +0000758static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
759{
760 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
761 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
762}
763
Patrick McHardy3729d502010-02-26 06:34:54 +0000764static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
765 const struct ifinfomsg *ifm)
766{
767 unsigned int flags = ifm->ifi_flags;
768
769 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
770 if (ifm->ifi_change)
771 flags = (flags & ifm->ifi_change) |
Jiri Bencb1beb682012-07-27 02:58:22 +0000772 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
Patrick McHardy3729d502010-02-26 06:34:54 +0000773
774 return flags;
775}
776
Thomas Grafb60c5112006-08-04 23:05:34 -0700777static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000778 const struct rtnl_link_stats64 *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
Thomas Grafb60c5112006-08-04 23:05:34 -0700780 a->rx_packets = b->rx_packets;
781 a->tx_packets = b->tx_packets;
782 a->rx_bytes = b->rx_bytes;
783 a->tx_bytes = b->tx_bytes;
784 a->rx_errors = b->rx_errors;
785 a->tx_errors = b->tx_errors;
786 a->rx_dropped = b->rx_dropped;
787 a->tx_dropped = b->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Thomas Grafb60c5112006-08-04 23:05:34 -0700789 a->multicast = b->multicast;
790 a->collisions = b->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Thomas Grafb60c5112006-08-04 23:05:34 -0700792 a->rx_length_errors = b->rx_length_errors;
793 a->rx_over_errors = b->rx_over_errors;
794 a->rx_crc_errors = b->rx_crc_errors;
795 a->rx_frame_errors = b->rx_frame_errors;
796 a->rx_fifo_errors = b->rx_fifo_errors;
797 a->rx_missed_errors = b->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Thomas Grafb60c5112006-08-04 23:05:34 -0700799 a->tx_aborted_errors = b->tx_aborted_errors;
800 a->tx_carrier_errors = b->tx_carrier_errors;
801 a->tx_fifo_errors = b->tx_fifo_errors;
802 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
803 a->tx_window_errors = b->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Thomas Grafb60c5112006-08-04 23:05:34 -0700805 a->rx_compressed = b->rx_compressed;
806 a->tx_compressed = b->tx_compressed;
Jarod Wilson6e7333d2016-02-01 18:51:05 -0500807
808 a->rx_nohandler = b->rx_nohandler;
Jan Engelhardt10708f32010-03-11 09:57:29 +0000809}
810
Chris Wrightc02db8c2010-05-16 01:05:45 -0700811/* All VF info */
Greg Rose115c9b82012-02-21 16:54:48 -0500812static inline int rtnl_vfinfo_size(const struct net_device *dev,
813 u32 ext_filter_mask)
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000814{
Phil Sutter9af15c32017-01-18 14:04:39 +0100815 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
Chris Wrightc02db8c2010-05-16 01:05:45 -0700816 int num_vfs = dev_num_vf(dev->dev.parent);
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100817 size_t size = nla_total_size(0);
Scott Feldman045de012010-05-28 03:42:43 -0700818 size += num_vfs *
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100819 (nla_total_size(0) +
820 nla_total_size(sizeof(struct ifla_vf_mac)) +
821 nla_total_size(sizeof(struct ifla_vf_vlan)) +
822 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
Moshe Shemesh79aab092016-09-22 12:11:15 +0300823 nla_total_size(MAX_VLAN_LIST_LEN *
824 sizeof(struct ifla_vf_vlan_info)) +
Sucheta Chakrabortyed616682014-05-22 09:59:05 -0400825 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100826 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
Jiri Benc945a3672014-08-08 16:44:32 +0200827 nla_total_size(sizeof(struct ifla_vf_rate)) +
Vlad Zolotarov01a3d792015-03-30 21:35:23 +0300828 nla_total_size(sizeof(struct ifla_vf_link_state)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300829 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100830 nla_total_size(0) + /* nest IFLA_VF_STATS */
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300831 /* IFLA_VF_STATS_RX_PACKETS */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200832 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300833 /* IFLA_VF_STATS_TX_PACKETS */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200834 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300835 /* IFLA_VF_STATS_RX_BYTES */
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_BYTES */
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_BROADCAST */
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_MULTICAST */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200842 nla_total_size_64bit(sizeof(__u64)) +
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +0000843 nla_total_size(sizeof(struct ifla_vf_trust)));
Chris Wrightc02db8c2010-05-16 01:05:45 -0700844 return size;
845 } else
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000846 return 0;
847}
848
David Gibsonc53864f2014-04-24 10:22:36 +1000849static size_t rtnl_port_size(const struct net_device *dev,
850 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -0700851{
852 size_t port_size = nla_total_size(4) /* PORT_VF */
853 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
Scott Feldman57b61082010-05-17 22:49:55 -0700854 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
855 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
856 + nla_total_size(1) /* PROT_VDP_REQUEST */
857 + nla_total_size(2); /* PORT_VDP_RESPONSE */
858 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
859 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
860 + port_size;
861 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
862 + port_size;
863
David Gibsonc53864f2014-04-24 10:22:36 +1000864 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
865 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -0700866 return 0;
867 if (dev_num_vf(dev->dev.parent))
868 return port_self_size + vf_ports_size +
869 vf_port_size * dev_num_vf(dev->dev.parent);
870 else
871 return port_self_size;
872}
873
David S. Millerb5cdae32017-04-18 15:36:58 -0400874static size_t rtnl_xdp_size(void)
Brenden Blancod1fdd912016-07-19 12:16:49 -0700875{
Sabrina Dubrocab3cfaa32016-11-15 11:16:35 +0100876 size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */
Martin KaFai Lau58038692017-06-15 17:29:09 -0700877 nla_total_size(1) + /* XDP_ATTACHED */
878 nla_total_size(4); /* XDP_PROG_ID */
Brenden Blancod1fdd912016-07-19 12:16:49 -0700879
David S. Millerb5cdae32017-04-18 15:36:58 -0400880 return xdp_size;
Brenden Blancod1fdd912016-07-19 12:16:49 -0700881}
882
Greg Rose115c9b82012-02-21 16:54:48 -0500883static noinline size_t if_nlmsg_size(const struct net_device *dev,
884 u32 ext_filter_mask)
Thomas Graf339bf982006-11-10 14:10:15 -0800885{
886 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
887 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700888 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
Thomas Graf339bf982006-11-10 14:10:15 -0800889 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
Nicolas Dichtel270cb4d2016-04-26 10:06:16 +0200890 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
Thomas Graf339bf982006-11-10 14:10:15 -0800891 + nla_total_size(sizeof(struct rtnl_link_stats))
David S. Miller35c58452016-04-19 19:49:29 -0400892 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
Thomas Graf339bf982006-11-10 14:10:15 -0800893 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
894 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
895 + nla_total_size(4) /* IFLA_TXQLEN */
896 + nla_total_size(4) /* IFLA_WEIGHT */
897 + nla_total_size(4) /* IFLA_MTU */
898 + nla_total_size(4) /* IFLA_LINK */
899 + nla_total_size(4) /* IFLA_MASTER */
Jiri Pirko9a572472012-12-27 23:49:39 +0000900 + nla_total_size(1) /* IFLA_CARRIER */
Ben Greearedbc0bb2012-03-29 12:51:30 +0000901 + nla_total_size(4) /* IFLA_PROMISCUITY */
Jiri Pirko76ff5cc2012-07-20 02:28:48 +0000902 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
903 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
Tobias Klauser69197562016-11-30 14:30:37 +0100904 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
905 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
Thomas Graf339bf982006-11-10 14:10:15 -0800906 + nla_total_size(1) /* IFLA_OPERSTATE */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700907 + nla_total_size(1) /* IFLA_LINKMODE */
david decotigny2d3b4792014-03-29 09:48:35 -0700908 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
Nicolas Dichteld37512a2015-01-15 15:11:16 +0100909 + nla_total_size(4) /* IFLA_LINK_NETNSID */
Serhey Popovychdb833d42017-06-20 14:35:23 +0300910 + nla_total_size(4) /* IFLA_GROUP */
Greg Rose115c9b82012-02-21 16:54:48 -0500911 + nla_total_size(ext_filter_mask
912 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
913 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
David Gibsonc53864f2014-04-24 10:22:36 +1000914 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
Thomas Graff8ff1822010-11-16 04:30:14 +0000915 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700916 + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
Jiri Pirko82f28412014-11-28 14:34:18 +0100917 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
Anuradha Karuppiah88d63782015-07-14 13:43:20 -0700918 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
Nicolas Dichtelc57c7a92016-03-31 18:10:31 +0200919 + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
David S. Millerb5cdae32017-04-18 15:36:58 -0400920 + rtnl_xdp_size() /* IFLA_XDP */
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -0400921 + nla_total_size(4) /* IFLA_EVENT */
Nicolas Dichtel6621dd22017-10-03 13:53:23 +0200922 + nla_total_size(4) /* IFLA_NEW_NETNSID */
Anuradha Karuppiah88d63782015-07-14 13:43:20 -0700923 + nla_total_size(1); /* IFLA_PROTO_DOWN */
Jiri Benc79e1ad12017-11-02 17:04:38 -0200924 + nla_total_size(4) /* IFLA_IF_NETNSID */
925 + 0;
Thomas Graf339bf982006-11-10 14:10:15 -0800926}
927
Scott Feldman57b61082010-05-17 22:49:55 -0700928static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
929{
930 struct nlattr *vf_ports;
931 struct nlattr *vf_port;
932 int vf;
933 int err;
934
935 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
936 if (!vf_ports)
937 return -EMSGSIZE;
938
939 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
940 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
Scott Feldman8ca94182010-05-28 03:42:18 -0700941 if (!vf_port)
942 goto nla_put_failure;
David S. Millera6574342012-04-01 20:12:00 -0400943 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
944 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -0700945 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
Scott Feldman8ca94182010-05-28 03:42:18 -0700946 if (err == -EMSGSIZE)
947 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -0700948 if (err) {
Scott Feldman57b61082010-05-17 22:49:55 -0700949 nla_nest_cancel(skb, vf_port);
950 continue;
951 }
952 nla_nest_end(skb, vf_port);
953 }
954
955 nla_nest_end(skb, vf_ports);
956
957 return 0;
Scott Feldman8ca94182010-05-28 03:42:18 -0700958
959nla_put_failure:
960 nla_nest_cancel(skb, vf_ports);
961 return -EMSGSIZE;
Scott Feldman57b61082010-05-17 22:49:55 -0700962}
963
964static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
965{
966 struct nlattr *port_self;
967 int err;
968
969 port_self = nla_nest_start(skb, IFLA_PORT_SELF);
970 if (!port_self)
971 return -EMSGSIZE;
972
973 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
974 if (err) {
975 nla_nest_cancel(skb, port_self);
Scott Feldman8ca94182010-05-28 03:42:18 -0700976 return (err == -EMSGSIZE) ? err : 0;
Scott Feldman57b61082010-05-17 22:49:55 -0700977 }
978
979 nla_nest_end(skb, port_self);
980
981 return 0;
982}
983
David Gibsonc53864f2014-04-24 10:22:36 +1000984static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
985 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -0700986{
987 int err;
988
David Gibsonc53864f2014-04-24 10:22:36 +1000989 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
990 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -0700991 return 0;
992
993 err = rtnl_port_self_fill(skb, dev);
994 if (err)
995 return err;
996
997 if (dev_num_vf(dev->dev.parent)) {
998 err = rtnl_vf_ports_fill(skb, dev);
999 if (err)
1000 return err;
1001 }
1002
1003 return 0;
1004}
1005
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001006static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
1007{
1008 int err;
Jiri Pirko02637fc2014-11-28 14:34:16 +01001009 struct netdev_phys_item_id ppid;
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001010
1011 err = dev_get_phys_port_id(dev, &ppid);
1012 if (err) {
1013 if (err == -EOPNOTSUPP)
1014 return 0;
1015 return err;
1016 }
1017
1018 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1019 return -EMSGSIZE;
1020
1021 return 0;
1022}
1023
David Aherndb24a902015-03-17 20:23:15 -06001024static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1025{
1026 char name[IFNAMSIZ];
1027 int err;
1028
1029 err = dev_get_phys_port_name(dev, name, sizeof(name));
1030 if (err) {
1031 if (err == -EOPNOTSUPP)
1032 return 0;
1033 return err;
1034 }
1035
Michal Schmidt77ef0332017-05-04 16:48:58 +02001036 if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
David Aherndb24a902015-03-17 20:23:15 -06001037 return -EMSGSIZE;
1038
1039 return 0;
1040}
1041
Jiri Pirko82f28412014-11-28 14:34:18 +01001042static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1043{
1044 int err;
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001045 struct switchdev_attr attr = {
Ido Schimmel6ff64f62015-12-15 16:03:35 +01001046 .orig_dev = dev,
Jiri Pirko1f868392015-10-01 11:03:42 +02001047 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001048 .flags = SWITCHDEV_F_NO_RECURSE,
1049 };
Jiri Pirko82f28412014-11-28 14:34:18 +01001050
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001051 err = switchdev_port_attr_get(dev, &attr);
Jiri Pirko82f28412014-11-28 14:34:18 +01001052 if (err) {
1053 if (err == -EOPNOTSUPP)
1054 return 0;
1055 return err;
1056 }
1057
Scott Feldman42275bd2015-05-13 11:16:50 -07001058 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len,
1059 attr.u.ppid.id))
Jiri Pirko82f28412014-11-28 14:34:18 +01001060 return -EMSGSIZE;
1061
1062 return 0;
1063}
1064
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001065static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1066 struct net_device *dev)
1067{
Roopa Prabhu550bce52016-04-15 20:36:25 -07001068 struct rtnl_link_stats64 *sp;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001069 struct nlattr *attr;
1070
Nicolas Dichtel58414d32016-04-21 18:58:25 +02001071 attr = nla_reserve_64bit(skb, IFLA_STATS64,
1072 sizeof(struct rtnl_link_stats64), IFLA_PAD);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001073 if (!attr)
1074 return -EMSGSIZE;
1075
Roopa Prabhu550bce52016-04-15 20:36:25 -07001076 sp = nla_data(attr);
1077 dev_get_stats(dev, sp);
1078
1079 attr = nla_reserve(skb, IFLA_STATS,
1080 sizeof(struct rtnl_link_stats));
1081 if (!attr)
1082 return -EMSGSIZE;
1083
1084 copy_rtnl_link_stats(nla_data(attr), sp);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001085
1086 return 0;
1087}
1088
1089static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1090 struct net_device *dev,
1091 int vfs_num,
1092 struct nlattr *vfinfo)
1093{
1094 struct ifla_vf_rss_query_en vf_rss_query_en;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001095 struct nlattr *vf, *vfstats, *vfvlanlist;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001096 struct ifla_vf_link_state vf_linkstate;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001097 struct ifla_vf_vlan_info vf_vlan_info;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001098 struct ifla_vf_spoofchk vf_spoofchk;
1099 struct ifla_vf_tx_rate vf_tx_rate;
1100 struct ifla_vf_stats vf_stats;
1101 struct ifla_vf_trust vf_trust;
1102 struct ifla_vf_vlan vf_vlan;
1103 struct ifla_vf_rate vf_rate;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001104 struct ifla_vf_mac vf_mac;
1105 struct ifla_vf_info ivi;
1106
Mintz, Yuval0eed9cf2017-06-07 21:00:33 +03001107 memset(&ivi, 0, sizeof(ivi));
1108
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001109 /* Not all SR-IOV capable drivers support the
1110 * spoofcheck and "RSS query enable" query. Preset to
1111 * -1 so the user space tool can detect that the driver
1112 * didn't report anything.
1113 */
1114 ivi.spoofchk = -1;
1115 ivi.rss_query_en = -1;
1116 ivi.trusted = -1;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001117 /* The default value for VF link state is "auto"
1118 * IFLA_VF_LINK_STATE_AUTO which equals zero
1119 */
1120 ivi.linkstate = 0;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001121 /* VLAN Protocol by default is 802.1Q */
1122 ivi.vlan_proto = htons(ETH_P_8021Q);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001123 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1124 return 0;
1125
Dan Carpenter775f4f02016-10-13 11:45:28 +03001126 memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
1127
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001128 vf_mac.vf =
1129 vf_vlan.vf =
Moshe Shemesh79aab092016-09-22 12:11:15 +03001130 vf_vlan_info.vf =
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001131 vf_rate.vf =
1132 vf_tx_rate.vf =
1133 vf_spoofchk.vf =
1134 vf_linkstate.vf =
1135 vf_rss_query_en.vf =
1136 vf_trust.vf = ivi.vf;
1137
1138 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1139 vf_vlan.vlan = ivi.vlan;
1140 vf_vlan.qos = ivi.qos;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001141 vf_vlan_info.vlan = ivi.vlan;
1142 vf_vlan_info.qos = ivi.qos;
1143 vf_vlan_info.vlan_proto = ivi.vlan_proto;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001144 vf_tx_rate.rate = ivi.max_tx_rate;
1145 vf_rate.min_tx_rate = ivi.min_tx_rate;
1146 vf_rate.max_tx_rate = ivi.max_tx_rate;
1147 vf_spoofchk.setting = ivi.spoofchk;
1148 vf_linkstate.link_state = ivi.linkstate;
1149 vf_rss_query_en.setting = ivi.rss_query_en;
1150 vf_trust.setting = ivi.trusted;
1151 vf = nla_nest_start(skb, IFLA_VF_INFO);
Moshe Shemesh79aab092016-09-22 12:11:15 +03001152 if (!vf)
1153 goto nla_put_vfinfo_failure;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001154 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1155 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1156 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1157 &vf_rate) ||
1158 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1159 &vf_tx_rate) ||
1160 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1161 &vf_spoofchk) ||
1162 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1163 &vf_linkstate) ||
1164 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1165 sizeof(vf_rss_query_en),
1166 &vf_rss_query_en) ||
1167 nla_put(skb, IFLA_VF_TRUST,
1168 sizeof(vf_trust), &vf_trust))
Moshe Shemesh79aab092016-09-22 12:11:15 +03001169 goto nla_put_vf_failure;
1170 vfvlanlist = nla_nest_start(skb, IFLA_VF_VLAN_LIST);
1171 if (!vfvlanlist)
1172 goto nla_put_vf_failure;
1173 if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
1174 &vf_vlan_info)) {
1175 nla_nest_cancel(skb, vfvlanlist);
1176 goto nla_put_vf_failure;
1177 }
1178 nla_nest_end(skb, vfvlanlist);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001179 memset(&vf_stats, 0, sizeof(vf_stats));
1180 if (dev->netdev_ops->ndo_get_vf_stats)
1181 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1182 &vf_stats);
1183 vfstats = nla_nest_start(skb, IFLA_VF_STATS);
Moshe Shemesh79aab092016-09-22 12:11:15 +03001184 if (!vfstats)
1185 goto nla_put_vf_failure;
Nicolas Dichtel343a6d82016-04-25 10:25:14 +02001186 if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1187 vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1188 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1189 vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1190 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1191 vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1192 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1193 vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1194 nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1195 vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1196 nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
Moshe Shemesh79aab092016-09-22 12:11:15 +03001197 vf_stats.multicast, IFLA_VF_STATS_PAD)) {
1198 nla_nest_cancel(skb, vfstats);
1199 goto nla_put_vf_failure;
1200 }
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001201 nla_nest_end(skb, vfstats);
1202 nla_nest_end(skb, vf);
1203 return 0;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001204
1205nla_put_vf_failure:
1206 nla_nest_cancel(skb, vf);
1207nla_put_vfinfo_failure:
1208 nla_nest_cancel(skb, vfinfo);
1209 return -EMSGSIZE;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001210}
1211
Florian Westphal250fc3d2017-09-26 13:58:41 +02001212static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb,
1213 struct net_device *dev,
1214 u32 ext_filter_mask)
1215{
1216 struct nlattr *vfinfo;
1217 int i, num_vfs;
1218
1219 if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0))
1220 return 0;
1221
1222 num_vfs = dev_num_vf(dev->dev.parent);
1223 if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs))
1224 return -EMSGSIZE;
1225
1226 if (!dev->netdev_ops->ndo_get_vf_config)
1227 return 0;
1228
1229 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1230 if (!vfinfo)
1231 return -EMSGSIZE;
1232
1233 for (i = 0; i < num_vfs; i++) {
1234 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1235 return -EMSGSIZE;
1236 }
1237
1238 nla_nest_end(skb, vfinfo);
1239 return 0;
1240}
1241
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001242static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1243{
Kangjie Lu5f8e4472016-05-03 16:46:24 -04001244 struct rtnl_link_ifmap map;
1245
1246 memset(&map, 0, sizeof(map));
1247 map.mem_start = dev->mem_start;
1248 map.mem_end = dev->mem_end;
1249 map.base_addr = dev->base_addr;
1250 map.irq = dev->irq;
1251 map.dma = dev->dma;
1252 map.port = dev->if_port;
1253
Nicolas Dichtel270cb4d2016-04-26 10:06:16 +02001254 if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001255 return -EMSGSIZE;
1256
1257 return 0;
1258}
1259
Martin KaFai Lau58038692017-06-15 17:29:09 -07001260static u8 rtnl_xdp_attached_mode(struct net_device *dev, u32 *prog_id)
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001261{
1262 const struct net_device_ops *ops = dev->netdev_ops;
Martin KaFai Lau58038692017-06-15 17:29:09 -07001263 const struct bpf_prog *generic_xdp_prog;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001264
1265 ASSERT_RTNL();
1266
Martin KaFai Lau58038692017-06-15 17:29:09 -07001267 *prog_id = 0;
1268 generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
1269 if (generic_xdp_prog) {
1270 *prog_id = generic_xdp_prog->aux->id;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001271 return XDP_ATTACHED_SKB;
Martin KaFai Lau58038692017-06-15 17:29:09 -07001272 }
Jakub Kicinskice158e52017-06-21 18:25:09 -07001273 if (!ops->ndo_xdp)
1274 return XDP_ATTACHED_NONE;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001275
Jakub Kicinskice158e52017-06-21 18:25:09 -07001276 return __dev_xdp_attached(dev, ops->ndo_xdp, prog_id);
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001277}
1278
Brenden Blancod1fdd912016-07-19 12:16:49 -07001279static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1280{
Brenden Blancod1fdd912016-07-19 12:16:49 -07001281 struct nlattr *xdp;
Martin KaFai Lau58038692017-06-15 17:29:09 -07001282 u32 prog_id;
Brenden Blancod1fdd912016-07-19 12:16:49 -07001283 int err;
1284
Brenden Blancod1fdd912016-07-19 12:16:49 -07001285 xdp = nla_nest_start(skb, IFLA_XDP);
1286 if (!xdp)
1287 return -EMSGSIZE;
David S. Millerb5cdae32017-04-18 15:36:58 -04001288
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001289 err = nla_put_u8(skb, IFLA_XDP_ATTACHED,
Martin KaFai Lau58038692017-06-15 17:29:09 -07001290 rtnl_xdp_attached_mode(dev, &prog_id));
Brenden Blancod1fdd912016-07-19 12:16:49 -07001291 if (err)
1292 goto err_cancel;
1293
Martin KaFai Lau58038692017-06-15 17:29:09 -07001294 if (prog_id) {
1295 err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
1296 if (err)
1297 goto err_cancel;
1298 }
1299
Brenden Blancod1fdd912016-07-19 12:16:49 -07001300 nla_nest_end(skb, xdp);
1301 return 0;
1302
1303err_cancel:
1304 nla_nest_cancel(skb, xdp);
1305 return err;
1306}
1307
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001308static u32 rtnl_get_event(unsigned long event)
1309{
1310 u32 rtnl_event_type = IFLA_EVENT_NONE;
1311
1312 switch (event) {
1313 case NETDEV_REBOOT:
1314 rtnl_event_type = IFLA_EVENT_REBOOT;
1315 break;
1316 case NETDEV_FEAT_CHANGE:
1317 rtnl_event_type = IFLA_EVENT_FEATURES;
1318 break;
1319 case NETDEV_BONDING_FAILOVER:
1320 rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
1321 break;
1322 case NETDEV_NOTIFY_PEERS:
1323 rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
1324 break;
1325 case NETDEV_RESEND_IGMP:
1326 rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
1327 break;
1328 case NETDEV_CHANGEINFODATA:
1329 rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
1330 break;
1331 default:
1332 break;
1333 }
1334
1335 return rtnl_event_type;
1336}
1337
Florian Westphal79110a02017-09-26 13:58:40 +02001338static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
1339{
1340 const struct net_device *upper_dev;
1341 int ret = 0;
1342
1343 rcu_read_lock();
1344
1345 upper_dev = netdev_master_upper_dev_get_rcu(dev);
1346 if (upper_dev)
1347 ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);
1348
1349 rcu_read_unlock();
1350 return ret;
1351}
1352
1353static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev)
1354{
1355 int ifindex = dev_get_iflink(dev);
1356
1357 if (dev->ifindex == ifindex)
1358 return 0;
1359
1360 return nla_put_u32(skb, IFLA_LINK, ifindex);
1361}
1362
Florian Westphal6c557002017-10-02 23:50:05 +02001363static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
1364 struct net_device *dev)
1365{
1366 char buf[IFALIASZ];
1367 int ret;
1368
1369 ret = dev_get_alias(dev, buf, sizeof(buf));
1370 return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
1371}
1372
Florian Westphalb1e66b92017-09-26 13:58:42 +02001373static int rtnl_fill_link_netnsid(struct sk_buff *skb,
Jiri Benc79e1ad12017-11-02 17:04:38 -02001374 const struct net_device *dev,
1375 struct net *src_net)
Florian Westphalb1e66b92017-09-26 13:58:42 +02001376{
1377 if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
1378 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1379
1380 if (!net_eq(dev_net(dev), link_net)) {
Jiri Benc79e1ad12017-11-02 17:04:38 -02001381 int id = peernet2id_alloc(src_net, link_net);
Florian Westphalb1e66b92017-09-26 13:58:42 +02001382
1383 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1384 return -EMSGSIZE;
1385 }
1386 }
1387
1388 return 0;
1389}
1390
Florian Westphal070cbf5b2017-10-16 15:44:35 +02001391static int rtnl_fill_link_af(struct sk_buff *skb,
1392 const struct net_device *dev,
1393 u32 ext_filter_mask)
1394{
1395 const struct rtnl_af_ops *af_ops;
1396 struct nlattr *af_spec;
1397
1398 af_spec = nla_nest_start(skb, IFLA_AF_SPEC);
1399 if (!af_spec)
1400 return -EMSGSIZE;
1401
Florian Westphal5fa85a02017-10-16 15:44:36 +02001402 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Florian Westphal070cbf5b2017-10-16 15:44:35 +02001403 struct nlattr *af;
1404 int err;
1405
1406 if (!af_ops->fill_link_af)
1407 continue;
1408
1409 af = nla_nest_start(skb, af_ops->family);
1410 if (!af)
1411 return -EMSGSIZE;
1412
1413 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1414 /*
1415 * Caller may return ENODATA to indicate that there
1416 * was no data to be dumped. This is not an error, it
1417 * means we should trim the attribute header and
1418 * continue.
1419 */
1420 if (err == -ENODATA)
1421 nla_nest_cancel(skb, af);
1422 else if (err < 0)
1423 return -EMSGSIZE;
1424
1425 nla_nest_end(skb, af);
1426 }
1427
1428 nla_nest_end(skb, af_spec);
1429 return 0;
1430}
1431
Jiri Benc79e1ad12017-11-02 17:04:38 -02001432static int rtnl_fill_ifinfo(struct sk_buff *skb,
1433 struct net_device *dev, struct net *src_net,
Patrick McHardy575c3e22007-05-22 17:00:49 -07001434 int type, u32 pid, u32 seq, u32 change,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001435 unsigned int flags, u32 ext_filter_mask,
Jiri Benc79e1ad12017-11-02 17:04:38 -02001436 u32 event, int *new_nsid, int tgt_netnsid)
Thomas Grafb60c5112006-08-04 23:05:34 -07001437{
1438 struct ifinfomsg *ifm;
1439 struct nlmsghdr *nlh;
1440
Eric Dumazet2907c352011-05-25 07:34:04 +00001441 ASSERT_RTNL();
Thomas Grafb60c5112006-08-04 23:05:34 -07001442 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1443 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08001444 return -EMSGSIZE;
Thomas Grafb60c5112006-08-04 23:05:34 -07001445
1446 ifm = nlmsg_data(nlh);
1447 ifm->ifi_family = AF_UNSPEC;
1448 ifm->__ifi_pad = 0;
1449 ifm->ifi_type = dev->type;
1450 ifm->ifi_index = dev->ifindex;
1451 ifm->ifi_flags = dev_get_flags(dev);
1452 ifm->ifi_change = change;
1453
Jiri Benc79e1ad12017-11-02 17:04:38 -02001454 if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_IF_NETNSID, tgt_netnsid))
1455 goto nla_put_failure;
1456
David S. Millera6574342012-04-01 20:12:00 -04001457 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1458 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1459 nla_put_u8(skb, IFLA_OPERSTATE,
1460 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1461 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1462 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1463 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
Ben Greearedbc0bb2012-03-29 12:51:30 +00001464 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001465 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
Eric Dumazetc70ce022016-03-21 09:55:10 -07001466 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1467 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001468#ifdef CONFIG_RPS
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001469 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001470#endif
Florian Westphal79110a02017-09-26 13:58:40 +02001471 nla_put_iflink(skb, dev) ||
1472 put_master_ifindex(skb, dev) ||
Jiri Pirko9a572472012-12-27 23:49:39 +00001473 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
David S. Millera6574342012-04-01 20:12:00 -04001474 (dev->qdisc &&
1475 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
Florian Westphal6c557002017-10-02 23:50:05 +02001476 nla_put_ifalias(skb, dev) ||
david decotigny2d3b4792014-03-29 09:48:35 -07001477 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001478 atomic_read(&dev->carrier_changes)) ||
1479 nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
David S. Millera6574342012-04-01 20:12:00 -04001480 goto nla_put_failure;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001481
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001482 if (event != IFLA_EVENT_NONE) {
1483 if (nla_put_u32(skb, IFLA_EVENT, event))
1484 goto nla_put_failure;
1485 }
1486
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001487 if (rtnl_fill_link_ifmap(skb, dev))
1488 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
1490 if (dev->addr_len) {
David S. Millera6574342012-04-01 20:12:00 -04001491 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1492 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1493 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 }
1495
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001496 if (rtnl_phys_port_id_fill(skb, dev))
1497 goto nla_put_failure;
1498
David Aherndb24a902015-03-17 20:23:15 -06001499 if (rtnl_phys_port_name_fill(skb, dev))
1500 goto nla_put_failure;
1501
Jiri Pirko82f28412014-11-28 14:34:18 +01001502 if (rtnl_phys_switch_id_fill(skb, dev))
1503 goto nla_put_failure;
1504
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001505 if (rtnl_fill_stats(skb, dev))
Pavel Emelyanov96e74082008-05-21 14:12:46 -07001506 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Florian Westphal250fc3d2017-09-26 13:58:41 +02001508 if (rtnl_fill_vf(skb, dev, ext_filter_mask))
David S. Millera6574342012-04-01 20:12:00 -04001509 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001510
David Gibsonc53864f2014-04-24 10:22:36 +10001511 if (rtnl_port_fill(skb, dev, ext_filter_mask))
Scott Feldman57b61082010-05-17 22:49:55 -07001512 goto nla_put_failure;
1513
Brenden Blancod1fdd912016-07-19 12:16:49 -07001514 if (rtnl_xdp_fill(skb, dev))
1515 goto nla_put_failure;
1516
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001517 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07001518 if (rtnl_link_fill(skb, dev) < 0)
1519 goto nla_put_failure;
1520 }
1521
Jiri Benc79e1ad12017-11-02 17:04:38 -02001522 if (rtnl_fill_link_netnsid(skb, dev, src_net))
Florian Westphalb1e66b92017-09-26 13:58:42 +02001523 goto nla_put_failure;
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001524
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02001525 if (new_nsid &&
1526 nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0)
1527 goto nla_put_failure;
1528
Florian Westphal5fa85a02017-10-16 15:44:36 +02001529 rcu_read_lock();
Florian Westphal070cbf5b2017-10-16 15:44:35 +02001530 if (rtnl_fill_link_af(skb, dev, ext_filter_mask))
Florian Westphal5fa85a02017-10-16 15:44:36 +02001531 goto nla_put_failure_rcu;
1532 rcu_read_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +00001533
Johannes Berg053c0952015-01-16 22:09:00 +01001534 nlmsg_end(skb, nlh);
1535 return 0;
Thomas Grafb60c5112006-08-04 23:05:34 -07001536
Florian Westphal5fa85a02017-10-16 15:44:36 +02001537nla_put_failure_rcu:
1538 rcu_read_unlock();
Thomas Grafb60c5112006-08-04 23:05:34 -07001539nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001540 nlmsg_cancel(skb, nlh);
1541 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542}
1543
Jiri Pirkof7b12602014-02-18 20:53:18 +01001544static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1545 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1546 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1547 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1548 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1549 [IFLA_MTU] = { .type = NLA_U32 },
1550 [IFLA_LINK] = { .type = NLA_U32 },
1551 [IFLA_MASTER] = { .type = NLA_U32 },
1552 [IFLA_CARRIER] = { .type = NLA_U8 },
1553 [IFLA_TXQLEN] = { .type = NLA_U32 },
1554 [IFLA_WEIGHT] = { .type = NLA_U32 },
1555 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1556 [IFLA_LINKMODE] = { .type = NLA_U8 },
1557 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1558 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1559 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
Nicolas Dichtel2459b4c2017-10-11 16:24:48 +02001560 /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
1561 * allow 0-length string (needed to remove an alias).
1562 */
1563 [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001564 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1565 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1566 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1567 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1568 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1569 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1570 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1571 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
Jiri Pirko02637fc2014-11-28 14:34:16 +01001572 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
david decotigny2d3b4792014-03-29 09:48:35 -07001573 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
Jiri Pirko82f28412014-11-28 14:34:18 +01001574 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
Nicolas Dichtel317f4812015-01-15 15:11:18 +01001575 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001576 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001577 [IFLA_XDP] = { .type = NLA_NESTED },
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001578 [IFLA_EVENT] = { .type = NLA_U32 },
Serhey Popovychdb833d42017-06-20 14:35:23 +03001579 [IFLA_GROUP] = { .type = NLA_U32 },
Jiri Benc79e1ad12017-11-02 17:04:38 -02001580 [IFLA_IF_NETNSID] = { .type = NLA_S32 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001581};
1582
1583static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1584 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1585 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1586 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1587 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1588};
1589
Jiri Pirkof7b12602014-02-18 20:53:18 +01001590static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
Daniel Borkmann364d5712015-02-05 18:44:04 +01001591 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1592 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
Moshe Shemesh79aab092016-09-22 12:11:15 +03001593 [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED },
Daniel Borkmann364d5712015-02-05 18:44:04 +01001594 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1595 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1596 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1597 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001598 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001599 [IFLA_VF_STATS] = { .type = NLA_NESTED },
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00001600 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
Eli Cohencc8e27c2016-03-11 22:58:34 +02001601 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1602 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001603};
1604
Jiri Pirkof7b12602014-02-18 20:53:18 +01001605static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1606 [IFLA_PORT_VF] = { .type = NLA_U32 },
1607 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1608 .len = PORT_PROFILE_MAX },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001609 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1610 .len = PORT_UUID_MAX },
1611 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1612 .len = PORT_UUID_MAX },
1613 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1614 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
Daniel Borkmann025331d2017-02-17 01:56:11 +01001615
1616 /* Unused, but we need to keep it here since user space could
1617 * fill it. It's also broken with regard to NLA_BINARY use in
1618 * combination with structs.
1619 */
1620 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1621 .len = sizeof(struct ifla_port_vsi) },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001622};
1623
Brenden Blancod1fdd912016-07-19 12:16:49 -07001624static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
1625 [IFLA_XDP_FD] = { .type = NLA_S32 },
1626 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 },
Daniel Borkmann85de8572016-11-28 23:16:54 +01001627 [IFLA_XDP_FLAGS] = { .type = NLA_U32 },
Martin KaFai Lau58038692017-06-15 17:29:09 -07001628 [IFLA_XDP_PROG_ID] = { .type = NLA_U32 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001629};
1630
David Aherndc599f72016-02-02 08:17:07 -08001631static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1632{
1633 const struct rtnl_link_ops *ops = NULL;
1634 struct nlattr *linfo[IFLA_INFO_MAX + 1];
1635
Johannes Bergfceb6432017-04-12 14:34:07 +02001636 if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla,
1637 ifla_info_policy, NULL) < 0)
David Aherndc599f72016-02-02 08:17:07 -08001638 return NULL;
1639
1640 if (linfo[IFLA_INFO_KIND]) {
1641 char kind[MODULE_NAME_LEN];
1642
1643 nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1644 ops = rtnl_link_ops_get(kind);
1645 }
1646
1647 return ops;
1648}
1649
1650static bool link_master_filtered(struct net_device *dev, int master_idx)
1651{
1652 struct net_device *master;
1653
1654 if (!master_idx)
1655 return false;
1656
1657 master = netdev_master_upper_dev_get(dev);
1658 if (!master || master->ifindex != master_idx)
1659 return true;
1660
1661 return false;
1662}
1663
1664static bool link_kind_filtered(const struct net_device *dev,
1665 const struct rtnl_link_ops *kind_ops)
1666{
1667 if (kind_ops && dev->rtnl_link_ops != kind_ops)
1668 return true;
1669
1670 return false;
1671}
1672
1673static bool link_dump_filtered(struct net_device *dev,
1674 int master_idx,
1675 const struct rtnl_link_ops *kind_ops)
1676{
1677 if (link_master_filtered(dev, master_idx) ||
1678 link_kind_filtered(dev, kind_ops))
1679 return true;
1680
1681 return false;
1682}
1683
Jiri Benc79e1ad12017-11-02 17:04:38 -02001684static struct net *get_target_net(struct sk_buff *skb, int netnsid)
1685{
1686 struct net *net;
1687
1688 net = get_net_ns_by_id(sock_net(skb->sk), netnsid);
1689 if (!net)
1690 return ERR_PTR(-EINVAL);
1691
1692 /* For now, the caller is required to have CAP_NET_ADMIN in
1693 * the user namespace owning the target net ns.
1694 */
1695 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
1696 put_net(net);
1697 return ERR_PTR(-EACCES);
1698 }
1699 return net;
1700}
1701
Thomas Grafb60c5112006-08-04 23:05:34 -07001702static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001704 struct net *net = sock_net(skb->sk);
Jiri Benc79e1ad12017-11-02 17:04:38 -02001705 struct net *tgt_net = net;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001706 int h, s_h;
1707 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 struct net_device *dev;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001709 struct hlist_head *head;
Greg Rose115c9b82012-02-21 16:54:48 -05001710 struct nlattr *tb[IFLA_MAX+1];
1711 u32 ext_filter_mask = 0;
David Aherndc599f72016-02-02 08:17:07 -08001712 const struct rtnl_link_ops *kind_ops = NULL;
1713 unsigned int flags = NLM_F_MULTI;
1714 int master_idx = 0;
Jiri Benc79e1ad12017-11-02 17:04:38 -02001715 int netnsid = -1;
David Gibson973462b2014-04-24 10:22:35 +10001716 int err;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001717 int hdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001719 s_h = cb->args[0];
1720 s_idx = cb->args[1];
1721
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001722 /* A hack to preserve kernel<->userspace interface.
1723 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1724 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1725 * what iproute2 < v3.9.0 used.
1726 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1727 * attribute, its netlink message is shorter than struct ifinfomsg.
1728 */
1729 hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1730 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1731
Johannes Bergfceb6432017-04-12 14:34:07 +02001732 if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX,
1733 ifla_policy, NULL) >= 0) {
Jiri Benc79e1ad12017-11-02 17:04:38 -02001734 if (tb[IFLA_IF_NETNSID]) {
1735 netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]);
1736 tgt_net = get_target_net(skb, netnsid);
1737 if (IS_ERR(tgt_net)) {
1738 tgt_net = net;
1739 netnsid = -1;
1740 }
1741 }
1742
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001743 if (tb[IFLA_EXT_MASK])
1744 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
David Aherndc599f72016-02-02 08:17:07 -08001745
1746 if (tb[IFLA_MASTER])
1747 master_idx = nla_get_u32(tb[IFLA_MASTER]);
1748
1749 if (tb[IFLA_LINKINFO])
1750 kind_ops = linkinfo_to_kind_ops(tb[IFLA_LINKINFO]);
1751
1752 if (master_idx || kind_ops)
1753 flags |= NLM_F_DUMP_FILTERED;
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001754 }
Greg Rose115c9b82012-02-21 16:54:48 -05001755
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001756 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1757 idx = 0;
Jiri Benc79e1ad12017-11-02 17:04:38 -02001758 head = &tgt_net->dev_index_head[h];
Eric Dumazetcac5e652015-02-27 09:42:50 -08001759 hlist_for_each_entry(dev, head, index_hlist) {
David Aherndc599f72016-02-02 08:17:07 -08001760 if (link_dump_filtered(dev, master_idx, kind_ops))
Zhang Shengju3f0ae052016-11-19 23:28:32 +08001761 goto cont;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001762 if (idx < s_idx)
1763 goto cont;
Jiri Benc79e1ad12017-11-02 17:04:38 -02001764 err = rtnl_fill_ifinfo(skb, dev, net,
1765 RTM_NEWLINK,
David Gibson973462b2014-04-24 10:22:35 +10001766 NETLINK_CB(cb->skb).portid,
1767 cb->nlh->nlmsg_seq, 0,
David Aherndc599f72016-02-02 08:17:07 -08001768 flags,
Jiri Benc79e1ad12017-11-02 17:04:38 -02001769 ext_filter_mask, 0, NULL,
1770 netnsid);
David Gibson973462b2014-04-24 10:22:35 +10001771
David Ahernf6c57752017-05-15 23:19:17 -07001772 if (err < 0) {
1773 if (likely(skb->len))
1774 goto out;
1775
1776 goto out_err;
1777 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07001778cont:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001779 idx++;
1780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 }
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001782out:
David Ahernf6c57752017-05-15 23:19:17 -07001783 err = skb->len;
1784out_err:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001785 cb->args[1] = idx;
1786 cb->args[0] = h;
Jakub Sitnickid0225782017-08-09 17:39:12 +02001787 cb->seq = net->dev_base_seq;
1788 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Jiri Benc79e1ad12017-11-02 17:04:38 -02001789 if (netnsid >= 0)
1790 put_net(tgt_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
David Ahernf6c57752017-05-15 23:19:17 -07001792 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793}
1794
Johannes Bergfceb6432017-04-12 14:34:07 +02001795int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
1796 struct netlink_ext_ack *exterr)
Jiri Pirkof7b12602014-02-18 20:53:18 +01001797{
Johannes Bergfceb6432017-04-12 14:34:07 +02001798 return nla_parse(tb, IFLA_MAX, head, len, ifla_policy, exterr);
Jiri Pirkof7b12602014-02-18 20:53:18 +01001799}
1800EXPORT_SYMBOL(rtnl_nla_parse_ifla);
Scott Feldman57b61082010-05-17 22:49:55 -07001801
Eric W. Biederman81adee42009-11-08 00:53:51 -08001802struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1803{
1804 struct net *net;
1805 /* Examine the link attributes and figure out which
1806 * network namespace we are talking about.
1807 */
1808 if (tb[IFLA_NET_NS_PID])
1809 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001810 else if (tb[IFLA_NET_NS_FD])
1811 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
Eric W. Biederman81adee42009-11-08 00:53:51 -08001812 else
1813 net = get_net(src_net);
1814 return net;
1815}
1816EXPORT_SYMBOL(rtnl_link_get_net);
1817
Thomas Graf1840bb12008-02-23 19:54:36 -08001818static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1819{
1820 if (dev) {
1821 if (tb[IFLA_ADDRESS] &&
1822 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1823 return -EINVAL;
1824
1825 if (tb[IFLA_BROADCAST] &&
1826 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1827 return -EINVAL;
1828 }
1829
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001830 if (tb[IFLA_AF_SPEC]) {
1831 struct nlattr *af;
1832 int rem, err;
1833
1834 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1835 const struct rtnl_af_ops *af_ops;
1836
Florian Westphal5fa85a02017-10-16 15:44:36 +02001837 rcu_read_lock();
1838 af_ops = rtnl_af_lookup(nla_type(af));
1839 if (!af_ops) {
1840 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001841 return -EAFNOSUPPORT;
Florian Westphal5fa85a02017-10-16 15:44:36 +02001842 }
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001843
Florian Westphal5fa85a02017-10-16 15:44:36 +02001844 if (!af_ops->set_link_af) {
1845 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001846 return -EOPNOTSUPP;
Florian Westphal5fa85a02017-10-16 15:44:36 +02001847 }
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001848
1849 if (af_ops->validate_link_af) {
Kurt Van Dijck6d3a9a62011-01-26 04:55:24 +00001850 err = af_ops->validate_link_af(dev, af);
Florian Westphal5fa85a02017-10-16 15:44:36 +02001851 if (err < 0) {
1852 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001853 return err;
Florian Westphal5fa85a02017-10-16 15:44:36 +02001854 }
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001855 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02001856
1857 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001858 }
1859 }
1860
Thomas Graf1840bb12008-02-23 19:54:36 -08001861 return 0;
1862}
1863
Eli Cohencc8e27c2016-03-11 22:58:34 +02001864static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
1865 int guid_type)
1866{
1867 const struct net_device_ops *ops = dev->netdev_ops;
1868
1869 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
1870}
1871
1872static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
1873{
1874 if (dev->type != ARPHRD_INFINIBAND)
1875 return -EOPNOTSUPP;
1876
1877 return handle_infiniband_guid(dev, ivt, guid_type);
1878}
1879
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001880static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
Chris Wrightc02db8c2010-05-16 01:05:45 -07001881{
Chris Wrightc02db8c2010-05-16 01:05:45 -07001882 const struct net_device_ops *ops = dev->netdev_ops;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001883 int err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001884
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001885 if (tb[IFLA_VF_MAC]) {
1886 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001887
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001888 err = -EOPNOTSUPP;
1889 if (ops->ndo_set_vf_mac)
1890 err = ops->ndo_set_vf_mac(dev, ivm->vf,
1891 ivm->mac);
1892 if (err < 0)
1893 return err;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001894 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001895
1896 if (tb[IFLA_VF_VLAN]) {
1897 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
1898
1899 err = -EOPNOTSUPP;
1900 if (ops->ndo_set_vf_vlan)
1901 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
Moshe Shemesh79aab092016-09-22 12:11:15 +03001902 ivv->qos,
1903 htons(ETH_P_8021Q));
1904 if (err < 0)
1905 return err;
1906 }
1907
1908 if (tb[IFLA_VF_VLAN_LIST]) {
1909 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
1910 struct nlattr *attr;
1911 int rem, len = 0;
1912
1913 err = -EOPNOTSUPP;
1914 if (!ops->ndo_set_vf_vlan)
1915 return err;
1916
1917 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
1918 if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
1919 nla_len(attr) < NLA_HDRLEN) {
1920 return -EINVAL;
1921 }
1922 if (len >= MAX_VLAN_LIST_LEN)
1923 return -EOPNOTSUPP;
1924 ivvl[len] = nla_data(attr);
1925
1926 len++;
1927 }
Arnd Bergmannfa34cd92016-09-30 18:13:49 +02001928 if (len == 0)
1929 return -EINVAL;
1930
Moshe Shemesh79aab092016-09-22 12:11:15 +03001931 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
1932 ivvl[0]->qos, ivvl[0]->vlan_proto);
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001933 if (err < 0)
1934 return err;
1935 }
1936
1937 if (tb[IFLA_VF_TX_RATE]) {
1938 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
1939 struct ifla_vf_info ivf;
1940
1941 err = -EOPNOTSUPP;
1942 if (ops->ndo_get_vf_config)
1943 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
1944 if (err < 0)
1945 return err;
1946
1947 err = -EOPNOTSUPP;
1948 if (ops->ndo_set_vf_rate)
1949 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1950 ivf.min_tx_rate,
1951 ivt->rate);
1952 if (err < 0)
1953 return err;
1954 }
1955
1956 if (tb[IFLA_VF_RATE]) {
1957 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
1958
1959 err = -EOPNOTSUPP;
1960 if (ops->ndo_set_vf_rate)
1961 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1962 ivt->min_tx_rate,
1963 ivt->max_tx_rate);
1964 if (err < 0)
1965 return err;
1966 }
1967
1968 if (tb[IFLA_VF_SPOOFCHK]) {
1969 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
1970
1971 err = -EOPNOTSUPP;
1972 if (ops->ndo_set_vf_spoofchk)
1973 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
1974 ivs->setting);
1975 if (err < 0)
1976 return err;
1977 }
1978
1979 if (tb[IFLA_VF_LINK_STATE]) {
1980 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
1981
1982 err = -EOPNOTSUPP;
1983 if (ops->ndo_set_vf_link_state)
1984 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
1985 ivl->link_state);
1986 if (err < 0)
1987 return err;
1988 }
1989
1990 if (tb[IFLA_VF_RSS_QUERY_EN]) {
1991 struct ifla_vf_rss_query_en *ivrssq_en;
1992
1993 err = -EOPNOTSUPP;
1994 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
1995 if (ops->ndo_set_vf_rss_query_en)
1996 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
1997 ivrssq_en->setting);
1998 if (err < 0)
1999 return err;
2000 }
2001
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00002002 if (tb[IFLA_VF_TRUST]) {
2003 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
2004
2005 err = -EOPNOTSUPP;
2006 if (ops->ndo_set_vf_trust)
2007 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
2008 if (err < 0)
2009 return err;
2010 }
2011
Eli Cohencc8e27c2016-03-11 22:58:34 +02002012 if (tb[IFLA_VF_IB_NODE_GUID]) {
2013 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
2014
2015 if (!ops->ndo_set_vf_guid)
2016 return -EOPNOTSUPP;
2017
2018 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
2019 }
2020
2021 if (tb[IFLA_VF_IB_PORT_GUID]) {
2022 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
2023
2024 if (!ops->ndo_set_vf_guid)
2025 return -EOPNOTSUPP;
2026
2027 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
2028 }
2029
Chris Wrightc02db8c2010-05-16 01:05:45 -07002030 return err;
2031}
2032
David Ahern33eaf2a2017-10-04 17:48:46 -07002033static int do_set_master(struct net_device *dev, int ifindex,
2034 struct netlink_ext_ack *extack)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002035{
Jiri Pirko898e5062013-01-03 22:48:52 +00002036 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002037 const struct net_device_ops *ops;
2038 int err;
2039
Jiri Pirko898e5062013-01-03 22:48:52 +00002040 if (upper_dev) {
2041 if (upper_dev->ifindex == ifindex)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002042 return 0;
Jiri Pirko898e5062013-01-03 22:48:52 +00002043 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002044 if (ops->ndo_del_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002045 err = ops->ndo_del_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002046 if (err)
2047 return err;
2048 } else {
2049 return -EOPNOTSUPP;
2050 }
2051 }
2052
2053 if (ifindex) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002054 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
2055 if (!upper_dev)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002056 return -EINVAL;
Jiri Pirko898e5062013-01-03 22:48:52 +00002057 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002058 if (ops->ndo_add_slave) {
David Ahern33eaf2a2017-10-04 17:48:46 -07002059 err = ops->ndo_add_slave(upper_dev, dev, extack);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002060 if (err)
2061 return err;
2062 } else {
2063 return -EOPNOTSUPP;
2064 }
2065 }
2066 return 0;
2067}
2068
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002069#define DO_SETLINK_MODIFIED 0x01
Nicolas Dichtelba998902014-09-01 16:07:29 +02002070/* notify flag means notify + modified. */
2071#define DO_SETLINK_NOTIFY 0x03
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002072static int do_setlink(const struct sk_buff *skb,
2073 struct net_device *dev, struct ifinfomsg *ifm,
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002074 struct netlink_ext_ack *extack,
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002075 struct nlattr **tb, char *ifname, int status)
Patrick McHardy0157f602007-06-13 12:03:36 -07002076{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002077 const struct net_device_ops *ops = dev->netdev_ops;
Patrick McHardy0157f602007-06-13 12:03:36 -07002078 int err;
2079
Eric W. Biedermanf0630522011-05-04 17:51:50 -07002080 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
Eric W. Biederman81adee42009-11-08 00:53:51 -08002081 struct net *net = rtnl_link_get_net(dev_net(dev), tb);
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002082 if (IS_ERR(net)) {
2083 err = PTR_ERR(net);
2084 goto errout;
2085 }
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002086 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
Nicolas Dichtele0ebde02014-11-27 10:16:15 +01002087 put_net(net);
Eric W. Biedermanb51642f2012-11-16 03:03:11 +00002088 err = -EPERM;
2089 goto errout;
2090 }
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002091 err = dev_change_net_namespace(dev, net, ifname);
2092 put_net(net);
2093 if (err)
2094 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002095 status |= DO_SETLINK_MODIFIED;
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002096 }
2097
Patrick McHardy0157f602007-06-13 12:03:36 -07002098 if (tb[IFLA_MAP]) {
2099 struct rtnl_link_ifmap *u_map;
2100 struct ifmap k_map;
2101
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002102 if (!ops->ndo_set_config) {
Patrick McHardy0157f602007-06-13 12:03:36 -07002103 err = -EOPNOTSUPP;
2104 goto errout;
2105 }
2106
2107 if (!netif_device_present(dev)) {
2108 err = -ENODEV;
2109 goto errout;
2110 }
2111
2112 u_map = nla_data(tb[IFLA_MAP]);
2113 k_map.mem_start = (unsigned long) u_map->mem_start;
2114 k_map.mem_end = (unsigned long) u_map->mem_end;
2115 k_map.base_addr = (unsigned short) u_map->base_addr;
2116 k_map.irq = (unsigned char) u_map->irq;
2117 k_map.dma = (unsigned char) u_map->dma;
2118 k_map.port = (unsigned char) u_map->port;
2119
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002120 err = ops->ndo_set_config(dev, &k_map);
Patrick McHardy0157f602007-06-13 12:03:36 -07002121 if (err < 0)
2122 goto errout;
2123
Nicolas Dichtelba998902014-09-01 16:07:29 +02002124 status |= DO_SETLINK_NOTIFY;
Patrick McHardy0157f602007-06-13 12:03:36 -07002125 }
2126
2127 if (tb[IFLA_ADDRESS]) {
2128 struct sockaddr *sa;
2129 int len;
2130
WANG Cong153711f2017-07-20 11:27:57 -07002131 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2132 sizeof(*sa));
Patrick McHardy0157f602007-06-13 12:03:36 -07002133 sa = kmalloc(len, GFP_KERNEL);
2134 if (!sa) {
2135 err = -ENOMEM;
2136 goto errout;
2137 }
2138 sa->sa_family = dev->type;
2139 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
2140 dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00002141 err = dev_set_mac_address(dev, sa);
Patrick McHardy0157f602007-06-13 12:03:36 -07002142 kfree(sa);
2143 if (err)
2144 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002145 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002146 }
2147
2148 if (tb[IFLA_MTU]) {
2149 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
2150 if (err < 0)
2151 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002152 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002153 }
2154
Vlad Dogarucbda10f2011-01-13 23:38:30 +00002155 if (tb[IFLA_GROUP]) {
2156 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Nicolas Dichtelba998902014-09-01 16:07:29 +02002157 status |= DO_SETLINK_NOTIFY;
Vlad Dogarucbda10f2011-01-13 23:38:30 +00002158 }
2159
Patrick McHardy0157f602007-06-13 12:03:36 -07002160 /*
2161 * Interface selected by interface index but interface
2162 * name provided implies that a name change has been
2163 * requested.
2164 */
2165 if (ifm->ifi_index > 0 && ifname[0]) {
2166 err = dev_change_name(dev, ifname);
2167 if (err < 0)
2168 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002169 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002170 }
2171
Stephen Hemminger0b815a12008-09-22 21:28:11 -07002172 if (tb[IFLA_IFALIAS]) {
2173 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2174 nla_len(tb[IFLA_IFALIAS]));
2175 if (err < 0)
2176 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002177 status |= DO_SETLINK_NOTIFY;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07002178 }
2179
Patrick McHardy0157f602007-06-13 12:03:36 -07002180 if (tb[IFLA_BROADCAST]) {
2181 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00002182 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Patrick McHardy0157f602007-06-13 12:03:36 -07002183 }
2184
2185 if (ifm->ifi_flags || ifm->ifi_change) {
Patrick McHardy3729d502010-02-26 06:34:54 +00002186 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
Johannes Berg5f9021c2008-11-16 23:20:31 -08002187 if (err < 0)
2188 goto errout;
Patrick McHardy0157f602007-06-13 12:03:36 -07002189 }
2190
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002191 if (tb[IFLA_MASTER]) {
David Ahern33eaf2a2017-10-04 17:48:46 -07002192 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002193 if (err)
2194 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002195 status |= DO_SETLINK_MODIFIED;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002196 }
2197
Jiri Pirko9a572472012-12-27 23:49:39 +00002198 if (tb[IFLA_CARRIER]) {
2199 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2200 if (err)
2201 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002202 status |= DO_SETLINK_MODIFIED;
Jiri Pirko9a572472012-12-27 23:49:39 +00002203 }
2204
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002205 if (tb[IFLA_TXQLEN]) {
Alexey Dobriyan0cd29502017-05-17 13:30:44 +03002206 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
2207 unsigned int orig_len = dev->tx_queue_len;
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002208
Jason Wang08294a22016-06-30 14:45:35 +08002209 if (dev->tx_queue_len ^ value) {
2210 dev->tx_queue_len = value;
2211 err = call_netdevice_notifiers(
2212 NETDEV_CHANGE_TX_QUEUE_LEN, dev);
2213 err = notifier_to_errno(err);
2214 if (err) {
2215 dev->tx_queue_len = orig_len;
2216 goto errout;
2217 }
Xin Long2d7f6692017-10-15 18:13:46 +08002218 status |= DO_SETLINK_MODIFIED;
Jason Wang08294a22016-06-30 14:45:35 +08002219 }
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002220 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002221
Patrick McHardy0157f602007-06-13 12:03:36 -07002222 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002223 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Patrick McHardy0157f602007-06-13 12:03:36 -07002224
2225 if (tb[IFLA_LINKMODE]) {
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002226 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2227
David S. Miller93b2d4a2008-02-17 18:35:07 -08002228 write_lock_bh(&dev_base_lock);
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002229 if (dev->link_mode ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02002230 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002231 dev->link_mode = value;
David S. Miller93b2d4a2008-02-17 18:35:07 -08002232 write_unlock_bh(&dev_base_lock);
Patrick McHardy0157f602007-06-13 12:03:36 -07002233 }
2234
Chris Wrightc02db8c2010-05-16 01:05:45 -07002235 if (tb[IFLA_VFINFO_LIST]) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002236 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
Chris Wrightc02db8c2010-05-16 01:05:45 -07002237 struct nlattr *attr;
2238 int rem;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002239
Chris Wrightc02db8c2010-05-16 01:05:45 -07002240 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002241 if (nla_type(attr) != IFLA_VF_INFO ||
2242 nla_len(attr) < NLA_HDRLEN) {
David Howells253683b2010-05-21 02:25:27 +00002243 err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002244 goto errout;
David Howells253683b2010-05-21 02:25:27 +00002245 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002246 err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr,
Johannes Bergfceb6432017-04-12 14:34:07 +02002247 ifla_vf_policy, NULL);
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002248 if (err < 0)
2249 goto errout;
2250 err = do_setvfinfo(dev, vfinfo);
Chris Wrightc02db8c2010-05-16 01:05:45 -07002251 if (err < 0)
2252 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002253 status |= DO_SETLINK_NOTIFY;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002254 }
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00002255 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002256 err = 0;
2257
Scott Feldman57b61082010-05-17 22:49:55 -07002258 if (tb[IFLA_VF_PORTS]) {
2259 struct nlattr *port[IFLA_PORT_MAX+1];
2260 struct nlattr *attr;
2261 int vf;
2262 int rem;
2263
2264 err = -EOPNOTSUPP;
2265 if (!ops->ndo_set_vf_port)
2266 goto errout;
2267
2268 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
Daniel Borkmann035d2102015-07-13 00:06:02 +02002269 if (nla_type(attr) != IFLA_VF_PORT ||
2270 nla_len(attr) < NLA_HDRLEN) {
2271 err = -EINVAL;
2272 goto errout;
2273 }
2274 err = nla_parse_nested(port, IFLA_PORT_MAX, attr,
Johannes Bergfceb6432017-04-12 14:34:07 +02002275 ifla_port_policy, NULL);
Scott Feldman57b61082010-05-17 22:49:55 -07002276 if (err < 0)
2277 goto errout;
2278 if (!port[IFLA_PORT_VF]) {
2279 err = -EOPNOTSUPP;
2280 goto errout;
2281 }
2282 vf = nla_get_u32(port[IFLA_PORT_VF]);
2283 err = ops->ndo_set_vf_port(dev, vf, port);
2284 if (err < 0)
2285 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002286 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07002287 }
2288 }
2289 err = 0;
2290
2291 if (tb[IFLA_PORT_SELF]) {
2292 struct nlattr *port[IFLA_PORT_MAX+1];
2293
2294 err = nla_parse_nested(port, IFLA_PORT_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +02002295 tb[IFLA_PORT_SELF], ifla_port_policy,
2296 NULL);
Scott Feldman57b61082010-05-17 22:49:55 -07002297 if (err < 0)
2298 goto errout;
2299
2300 err = -EOPNOTSUPP;
2301 if (ops->ndo_set_vf_port)
2302 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
2303 if (err < 0)
2304 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002305 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07002306 }
Thomas Graff8ff1822010-11-16 04:30:14 +00002307
2308 if (tb[IFLA_AF_SPEC]) {
2309 struct nlattr *af;
2310 int rem;
2311
2312 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2313 const struct rtnl_af_ops *af_ops;
2314
Florian Westphal5fa85a02017-10-16 15:44:36 +02002315 rcu_read_lock();
2316
Gustavo A. R. Silva058c8d52017-10-20 19:43:11 -05002317 BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
Thomas Graff8ff1822010-11-16 04:30:14 +00002318
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002319 err = af_ops->set_link_af(dev, af);
Florian Westphal5fa85a02017-10-16 15:44:36 +02002320 if (err < 0) {
2321 rcu_read_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +00002322 goto errout;
Florian Westphal5fa85a02017-10-16 15:44:36 +02002323 }
Thomas Graff8ff1822010-11-16 04:30:14 +00002324
Florian Westphal5fa85a02017-10-16 15:44:36 +02002325 rcu_read_unlock();
Nicolas Dichtelba998902014-09-01 16:07:29 +02002326 status |= DO_SETLINK_NOTIFY;
Thomas Graff8ff1822010-11-16 04:30:14 +00002327 }
2328 }
Scott Feldman57b61082010-05-17 22:49:55 -07002329 err = 0;
2330
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07002331 if (tb[IFLA_PROTO_DOWN]) {
2332 err = dev_change_proto_down(dev,
2333 nla_get_u8(tb[IFLA_PROTO_DOWN]));
2334 if (err)
2335 goto errout;
2336 status |= DO_SETLINK_NOTIFY;
2337 }
2338
Brenden Blancod1fdd912016-07-19 12:16:49 -07002339 if (tb[IFLA_XDP]) {
2340 struct nlattr *xdp[IFLA_XDP_MAX + 1];
Daniel Borkmann85de8572016-11-28 23:16:54 +01002341 u32 xdp_flags = 0;
Brenden Blancod1fdd912016-07-19 12:16:49 -07002342
2343 err = nla_parse_nested(xdp, IFLA_XDP_MAX, tb[IFLA_XDP],
Johannes Bergfceb6432017-04-12 14:34:07 +02002344 ifla_xdp_policy, NULL);
Brenden Blancod1fdd912016-07-19 12:16:49 -07002345 if (err < 0)
2346 goto errout;
2347
Martin KaFai Lau58038692017-06-15 17:29:09 -07002348 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
Brenden Blanco262d8622016-07-20 17:22:34 -07002349 err = -EINVAL;
2350 goto errout;
2351 }
Daniel Borkmann85de8572016-11-28 23:16:54 +01002352
2353 if (xdp[IFLA_XDP_FLAGS]) {
2354 xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
2355 if (xdp_flags & ~XDP_FLAGS_MASK) {
2356 err = -EINVAL;
2357 goto errout;
2358 }
Jakub Kicinskiee5d0322017-06-21 18:25:04 -07002359 if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
Daniel Borkmann0489df92017-05-12 01:04:45 +02002360 err = -EINVAL;
2361 goto errout;
2362 }
Daniel Borkmann85de8572016-11-28 23:16:54 +01002363 }
2364
Brenden Blancod1fdd912016-07-19 12:16:49 -07002365 if (xdp[IFLA_XDP_FD]) {
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002366 err = dev_change_xdp_fd(dev, extack,
Daniel Borkmann85de8572016-11-28 23:16:54 +01002367 nla_get_s32(xdp[IFLA_XDP_FD]),
2368 xdp_flags);
Brenden Blancod1fdd912016-07-19 12:16:49 -07002369 if (err)
2370 goto errout;
2371 status |= DO_SETLINK_NOTIFY;
2372 }
2373 }
2374
Patrick McHardy0157f602007-06-13 12:03:36 -07002375errout:
Nicolas Dichtelba998902014-09-01 16:07:29 +02002376 if (status & DO_SETLINK_MODIFIED) {
Xin Long64ff90c2017-10-15 18:13:45 +08002377 if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
Nicolas Dichtelba998902014-09-01 16:07:29 +02002378 netdev_state_change(dev);
2379
2380 if (err < 0)
2381 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",
2382 dev->name);
2383 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002384
Patrick McHardy0157f602007-06-13 12:03:36 -07002385 return err;
2386}
2387
David Ahernc21ef3e2017-04-16 09:48:24 -07002388static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2389 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002391 struct net *net = sock_net(skb->sk);
Thomas Grafda5e0492006-08-10 21:17:37 -07002392 struct ifinfomsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 struct net_device *dev;
Patrick McHardy0157f602007-06-13 12:03:36 -07002394 int err;
Thomas Grafda5e0492006-08-10 21:17:37 -07002395 struct nlattr *tb[IFLA_MAX+1];
2396 char ifname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
David Ahernc21ef3e2017-04-16 09:48:24 -07002398 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy,
2399 extack);
Thomas Grafda5e0492006-08-10 21:17:37 -07002400 if (err < 0)
2401 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
Jiri Benc79e1ad12017-11-02 17:04:38 -02002403 if (tb[IFLA_IF_NETNSID])
2404 return -EOPNOTSUPP;
2405
Thomas Graf5176f912006-08-26 20:13:18 -07002406 if (tb[IFLA_IFNAME])
2407 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
Patrick McHardy78e5b8912006-09-13 20:35:36 -07002408 else
2409 ifname[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 err = -EINVAL;
Thomas Grafda5e0492006-08-10 21:17:37 -07002412 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -07002413 if (ifm->ifi_index > 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00002414 dev = __dev_get_by_index(net, ifm->ifi_index);
Thomas Grafda5e0492006-08-10 21:17:37 -07002415 else if (tb[IFLA_IFNAME])
Eric Dumazeta3d12892009-10-21 10:59:31 +00002416 dev = __dev_get_by_name(net, ifname);
Thomas Grafda5e0492006-08-10 21:17:37 -07002417 else
2418 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
Thomas Grafda5e0492006-08-10 21:17:37 -07002420 if (dev == NULL) {
2421 err = -ENODEV;
2422 goto errout;
2423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
Eric Dumazete0d087a2009-11-07 01:26:17 -08002425 err = validate_linkmsg(dev, tb);
2426 if (err < 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00002427 goto errout;
Thomas Grafda5e0492006-08-10 21:17:37 -07002428
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002429 err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
Thomas Grafda5e0492006-08-10 21:17:37 -07002430errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 return err;
2432}
2433
WANG Cong66400d52015-03-24 11:53:31 -07002434static int rtnl_group_dellink(const struct net *net, int group)
2435{
2436 struct net_device *dev, *aux;
2437 LIST_HEAD(list_kill);
2438 bool found = false;
2439
2440 if (!group)
2441 return -EPERM;
2442
2443 for_each_netdev(net, dev) {
2444 if (dev->group == group) {
2445 const struct rtnl_link_ops *ops;
2446
2447 found = true;
2448 ops = dev->rtnl_link_ops;
2449 if (!ops || !ops->dellink)
2450 return -EOPNOTSUPP;
2451 }
2452 }
2453
2454 if (!found)
2455 return -ENODEV;
2456
2457 for_each_netdev_safe(net, dev, aux) {
2458 if (dev->group == group) {
2459 const struct rtnl_link_ops *ops;
2460
2461 ops = dev->rtnl_link_ops;
2462 ops->dellink(dev, &list_kill);
2463 }
2464 }
2465 unregister_netdevice_many(&list_kill);
2466
2467 return 0;
2468}
2469
Thomas Graf614732e2015-07-21 10:44:06 +02002470int rtnl_delete_link(struct net_device *dev)
2471{
2472 const struct rtnl_link_ops *ops;
2473 LIST_HEAD(list_kill);
2474
2475 ops = dev->rtnl_link_ops;
2476 if (!ops || !ops->dellink)
2477 return -EOPNOTSUPP;
2478
2479 ops->dellink(dev, &list_kill);
2480 unregister_netdevice_many(&list_kill);
2481
2482 return 0;
2483}
2484EXPORT_SYMBOL_GPL(rtnl_delete_link);
2485
David Ahernc21ef3e2017-04-16 09:48:24 -07002486static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
2487 struct netlink_ext_ack *extack)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002488{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002489 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002490 struct net_device *dev;
2491 struct ifinfomsg *ifm;
2492 char ifname[IFNAMSIZ];
2493 struct nlattr *tb[IFLA_MAX+1];
2494 int err;
2495
David Ahernc21ef3e2017-04-16 09:48:24 -07002496 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002497 if (err < 0)
2498 return err;
2499
Jiri Benc79e1ad12017-11-02 17:04:38 -02002500 if (tb[IFLA_IF_NETNSID])
2501 return -EOPNOTSUPP;
2502
Patrick McHardy38f7b872007-06-13 12:03:51 -07002503 if (tb[IFLA_IFNAME])
2504 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2505
2506 ifm = nlmsg_data(nlh);
2507 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07002508 dev = __dev_get_by_index(net, ifm->ifi_index);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002509 else if (tb[IFLA_IFNAME])
Eric W. Biederman881d9662007-09-17 11:56:21 -07002510 dev = __dev_get_by_name(net, ifname);
WANG Cong66400d52015-03-24 11:53:31 -07002511 else if (tb[IFLA_GROUP])
2512 return rtnl_group_dellink(net, nla_get_u32(tb[IFLA_GROUP]));
Patrick McHardy38f7b872007-06-13 12:03:51 -07002513 else
2514 return -EINVAL;
2515
2516 if (!dev)
2517 return -ENODEV;
2518
Thomas Graf614732e2015-07-21 10:44:06 +02002519 return rtnl_delete_link(dev);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002520}
2521
Patrick McHardy3729d502010-02-26 06:34:54 +00002522int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
2523{
2524 unsigned int old_flags;
2525 int err;
2526
2527 old_flags = dev->flags;
2528 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
2529 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
2530 if (err < 0)
2531 return err;
2532 }
2533
2534 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
Patrick McHardy3729d502010-02-26 06:34:54 +00002535
Nicolas Dichtela528c212013-09-25 12:02:44 +02002536 __dev_notify_flags(dev, old_flags, ~0U);
Patrick McHardy3729d502010-02-26 06:34:54 +00002537 return 0;
2538}
2539EXPORT_SYMBOL(rtnl_configure_link);
2540
Rami Rosenc0713562012-11-30 01:08:47 +00002541struct net_device *rtnl_create_link(struct net *net,
Thomas Graf78ebb0d2015-04-10 01:45:53 +02002542 const char *ifname, unsigned char name_assign_type,
Tom Gundersen55177502014-07-14 16:37:25 +02002543 const struct rtnl_link_ops *ops, struct nlattr *tb[])
Pavel Emelianove7199282007-08-08 22:16:38 -07002544{
Pavel Emelianove7199282007-08-08 22:16:38 -07002545 struct net_device *dev;
Jiri Pirkod40156a2012-07-20 02:28:47 +00002546 unsigned int num_tx_queues = 1;
2547 unsigned int num_rx_queues = 1;
Pavel Emelianove7199282007-08-08 22:16:38 -07002548
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002549 if (tb[IFLA_NUM_TX_QUEUES])
2550 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
2551 else if (ops->get_num_tx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002552 num_tx_queues = ops->get_num_tx_queues();
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002553
2554 if (tb[IFLA_NUM_RX_QUEUES])
2555 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
2556 else if (ops->get_num_rx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002557 num_rx_queues = ops->get_num_rx_queues();
stephen hemmingerefacb302012-04-10 18:34:43 +00002558
Tom Gundersen55177502014-07-14 16:37:25 +02002559 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
Tom Gundersenc835a672014-07-14 16:37:24 +02002560 ops->setup, num_tx_queues, num_rx_queues);
Pavel Emelianove7199282007-08-08 22:16:38 -07002561 if (!dev)
Tobias Klauserd1892e42017-02-20 16:32:06 +01002562 return ERR_PTR(-ENOMEM);
Pavel Emelianove7199282007-08-08 22:16:38 -07002563
Eric W. Biederman81adee42009-11-08 00:53:51 -08002564 dev_net_set(dev, net);
2565 dev->rtnl_link_ops = ops;
Patrick McHardy3729d502010-02-26 06:34:54 +00002566 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
Eric W. Biederman81adee42009-11-08 00:53:51 -08002567
Pavel Emelianove7199282007-08-08 22:16:38 -07002568 if (tb[IFLA_MTU])
2569 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002570 if (tb[IFLA_ADDRESS]) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002571 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
2572 nla_len(tb[IFLA_ADDRESS]));
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002573 dev->addr_assign_type = NET_ADDR_SET;
2574 }
Pavel Emelianove7199282007-08-08 22:16:38 -07002575 if (tb[IFLA_BROADCAST])
2576 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
2577 nla_len(tb[IFLA_BROADCAST]));
2578 if (tb[IFLA_TXQLEN])
2579 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
2580 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002581 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Pavel Emelianove7199282007-08-08 22:16:38 -07002582 if (tb[IFLA_LINKMODE])
2583 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
Patrick McHardyffa934f2011-01-20 03:00:42 +00002584 if (tb[IFLA_GROUP])
2585 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Pavel Emelianove7199282007-08-08 22:16:38 -07002586
2587 return dev;
Pavel Emelianove7199282007-08-08 22:16:38 -07002588}
Eric Dumazete0d087a2009-11-07 01:26:17 -08002589EXPORT_SYMBOL(rtnl_create_link);
Pavel Emelianove7199282007-08-08 22:16:38 -07002590
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002591static int rtnl_group_changelink(const struct sk_buff *skb,
2592 struct net *net, int group,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002593 struct ifinfomsg *ifm,
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002594 struct netlink_ext_ack *extack,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002595 struct nlattr **tb)
2596{
WANG Congd0795352015-03-23 16:31:09 -07002597 struct net_device *dev, *aux;
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002598 int err;
2599
WANG Congd0795352015-03-23 16:31:09 -07002600 for_each_netdev_safe(net, dev, aux) {
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002601 if (dev->group == group) {
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002602 err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002603 if (err < 0)
2604 return err;
2605 }
2606 }
2607
2608 return 0;
2609}
2610
David Ahernc21ef3e2017-04-16 09:48:24 -07002611static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2612 struct netlink_ext_ack *extack)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002613{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002614 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002615 const struct rtnl_link_ops *ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002616 const struct rtnl_link_ops *m_ops = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002617 struct net_device *dev;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002618 struct net_device *master_dev = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002619 struct ifinfomsg *ifm;
2620 char kind[MODULE_NAME_LEN];
2621 char ifname[IFNAMSIZ];
2622 struct nlattr *tb[IFLA_MAX+1];
2623 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
Tom Gundersen55177502014-07-14 16:37:25 +02002624 unsigned char name_assign_type = NET_NAME_USER;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002625 int err;
2626
Johannes Berg95a5afc2008-10-16 15:24:51 -07002627#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002628replay:
Thomas Graf8072f082007-07-31 14:13:50 -07002629#endif
David Ahernc21ef3e2017-04-16 09:48:24 -07002630 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002631 if (err < 0)
2632 return err;
2633
Jiri Benc79e1ad12017-11-02 17:04:38 -02002634 if (tb[IFLA_IF_NETNSID])
2635 return -EOPNOTSUPP;
2636
Patrick McHardy38f7b872007-06-13 12:03:51 -07002637 if (tb[IFLA_IFNAME])
2638 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2639 else
2640 ifname[0] = '\0';
2641
2642 ifm = nlmsg_data(nlh);
2643 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07002644 dev = __dev_get_by_index(net, ifm->ifi_index);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002645 else {
2646 if (ifname[0])
2647 dev = __dev_get_by_name(net, ifname);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002648 else
2649 dev = NULL;
2650 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002651
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002652 if (dev) {
2653 master_dev = netdev_master_upper_dev_get(dev);
2654 if (master_dev)
2655 m_ops = master_dev->rtnl_link_ops;
2656 }
2657
Eric Dumazete0d087a2009-11-07 01:26:17 -08002658 err = validate_linkmsg(dev, tb);
2659 if (err < 0)
Thomas Graf1840bb12008-02-23 19:54:36 -08002660 return err;
2661
Patrick McHardy38f7b872007-06-13 12:03:51 -07002662 if (tb[IFLA_LINKINFO]) {
2663 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +02002664 tb[IFLA_LINKINFO], ifla_info_policy,
2665 NULL);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002666 if (err < 0)
2667 return err;
2668 } else
2669 memset(linkinfo, 0, sizeof(linkinfo));
2670
2671 if (linkinfo[IFLA_INFO_KIND]) {
2672 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
2673 ops = rtnl_link_ops_get(kind);
2674 } else {
2675 kind[0] = '\0';
2676 ops = NULL;
2677 }
2678
2679 if (1) {
Sasha Levin4e10fd52015-02-24 14:14:35 -05002680 struct nlattr *attr[ops ? ops->maxtype + 1 : 1];
2681 struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1];
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002682 struct nlattr **data = NULL;
2683 struct nlattr **slave_data = NULL;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002684 struct net *dest_net, *link_net = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002685
2686 if (ops) {
2687 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
2688 err = nla_parse_nested(attr, ops->maxtype,
2689 linkinfo[IFLA_INFO_DATA],
Johannes Bergfceb6432017-04-12 14:34:07 +02002690 ops->policy, NULL);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002691 if (err < 0)
2692 return err;
2693 data = attr;
2694 }
2695 if (ops->validate) {
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02002696 err = ops->validate(tb, data, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002697 if (err < 0)
2698 return err;
2699 }
2700 }
2701
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002702 if (m_ops) {
2703 if (m_ops->slave_maxtype &&
2704 linkinfo[IFLA_INFO_SLAVE_DATA]) {
2705 err = nla_parse_nested(slave_attr,
2706 m_ops->slave_maxtype,
2707 linkinfo[IFLA_INFO_SLAVE_DATA],
Johannes Bergfceb6432017-04-12 14:34:07 +02002708 m_ops->slave_policy,
2709 NULL);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002710 if (err < 0)
2711 return err;
2712 slave_data = slave_attr;
2713 }
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002714 }
2715
Patrick McHardy38f7b872007-06-13 12:03:51 -07002716 if (dev) {
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002717 int status = 0;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002718
2719 if (nlh->nlmsg_flags & NLM_F_EXCL)
2720 return -EEXIST;
2721 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2722 return -EOPNOTSUPP;
2723
2724 if (linkinfo[IFLA_INFO_DATA]) {
2725 if (!ops || ops != dev->rtnl_link_ops ||
2726 !ops->changelink)
2727 return -EOPNOTSUPP;
2728
Matthias Schifferad744b22017-06-25 23:56:00 +02002729 err = ops->changelink(dev, tb, data, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002730 if (err < 0)
2731 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002732 status |= DO_SETLINK_NOTIFY;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002733 }
2734
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002735 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2736 if (!m_ops || !m_ops->slave_changelink)
2737 return -EOPNOTSUPP;
2738
2739 err = m_ops->slave_changelink(master_dev, dev,
Matthias Schiffer17dd0ec2017-06-25 23:56:02 +02002740 tb, slave_data,
2741 extack);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002742 if (err < 0)
2743 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002744 status |= DO_SETLINK_NOTIFY;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002745 }
2746
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002747 return do_setlink(skb, dev, ifm, extack, tb, ifname,
2748 status);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002749 }
2750
Patrick McHardyffa934f2011-01-20 03:00:42 +00002751 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2752 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002753 return rtnl_group_changelink(skb, net,
Patrick McHardyffa934f2011-01-20 03:00:42 +00002754 nla_get_u32(tb[IFLA_GROUP]),
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002755 ifm, extack, tb);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002756 return -ENODEV;
Patrick McHardyffa934f2011-01-20 03:00:42 +00002757 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002758
Theuns Verwoerd160ca012017-01-31 12:23:46 +13002759 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
Patrick McHardy38f7b872007-06-13 12:03:51 -07002760 return -EOPNOTSUPP;
2761
2762 if (!ops) {
Johannes Berg95a5afc2008-10-16 15:24:51 -07002763#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002764 if (kind[0]) {
2765 __rtnl_unlock();
2766 request_module("rtnl-link-%s", kind);
2767 rtnl_lock();
2768 ops = rtnl_link_ops_get(kind);
2769 if (ops)
2770 goto replay;
2771 }
2772#endif
2773 return -EOPNOTSUPP;
2774 }
2775
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02002776 if (!ops->setup)
2777 return -EOPNOTSUPP;
2778
Tom Gundersen55177502014-07-14 16:37:25 +02002779 if (!ifname[0]) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07002780 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
Tom Gundersen55177502014-07-14 16:37:25 +02002781 name_assign_type = NET_NAME_ENUM;
2782 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002783
Eric W. Biederman81adee42009-11-08 00:53:51 -08002784 dest_net = rtnl_link_get_net(net, tb);
Eric W. Biederman13ad1772011-01-29 14:57:22 +00002785 if (IS_ERR(dest_net))
2786 return PTR_ERR(dest_net);
2787
Eric W. Biederman505ce412015-02-26 16:19:00 -06002788 err = -EPERM;
2789 if (!netlink_ns_capable(skb, dest_net->user_ns, CAP_NET_ADMIN))
2790 goto out;
2791
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002792 if (tb[IFLA_LINK_NETNSID]) {
2793 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
2794
2795 link_net = get_net_ns_by_id(dest_net, id);
2796 if (!link_net) {
2797 err = -EINVAL;
2798 goto out;
2799 }
Eric W. Biederman06615be2015-02-26 16:20:07 -06002800 err = -EPERM;
2801 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
2802 goto out;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002803 }
2804
2805 dev = rtnl_create_link(link_net ? : dest_net, ifname,
2806 name_assign_type, ops, tb);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002807 if (IS_ERR(dev)) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002808 err = PTR_ERR(dev);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002809 goto out;
2810 }
2811
2812 dev->ifindex = ifm->ifi_index;
2813
Cong Wang0e0eee22014-02-11 15:51:30 -08002814 if (ops->newlink) {
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02002815 err = ops->newlink(link_net ? : net, dev, tb, data,
2816 extack);
Cong Wang0e0eee22014-02-11 15:51:30 -08002817 /* Drivers should call free_netdev() in ->destructor
Cong Wange51fb152014-06-03 16:40:47 -07002818 * and unregister it on failure after registration
2819 * so that device could be finally freed in rtnl_unlock.
Cong Wang0e0eee22014-02-11 15:51:30 -08002820 */
Cong Wange51fb152014-06-03 16:40:47 -07002821 if (err < 0) {
2822 /* If device is not registered at all, free it now */
2823 if (dev->reg_state == NETREG_UNINITIALIZED)
2824 free_netdev(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002825 goto out;
Cong Wange51fb152014-06-03 16:40:47 -07002826 }
Cong Wang0e0eee22014-02-11 15:51:30 -08002827 } else {
Patrick McHardy2d85cba2007-07-11 19:42:13 -07002828 err = register_netdevice(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002829 if (err < 0) {
2830 free_netdev(dev);
2831 goto out;
2832 }
Dan Carpenterfce9b9b2013-08-14 12:35:42 +03002833 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002834 err = rtnl_configure_link(dev, ifm);
David S. Miller43638902015-03-10 21:58:32 -04002835 if (err < 0)
2836 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002837 if (link_net) {
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002838 err = dev_change_net_namespace(dev, dest_net, ifname);
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002839 if (err < 0)
David S. Miller43638902015-03-10 21:58:32 -04002840 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002841 }
Theuns Verwoerd160ca012017-01-31 12:23:46 +13002842 if (tb[IFLA_MASTER]) {
David Ahern33eaf2a2017-10-04 17:48:46 -07002843 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]),
2844 extack);
Theuns Verwoerd160ca012017-01-31 12:23:46 +13002845 if (err)
2846 goto out_unregister;
2847 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002848out:
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002849 if (link_net)
2850 put_net(link_net);
Eric W. Biederman81adee42009-11-08 00:53:51 -08002851 put_net(dest_net);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002852 return err;
David S. Miller43638902015-03-10 21:58:32 -04002853out_unregister:
2854 if (ops->newlink) {
2855 LIST_HEAD(list_kill);
2856
2857 ops->dellink(dev, &list_kill);
2858 unregister_netdevice_many(&list_kill);
2859 } else {
2860 unregister_netdevice(dev);
2861 }
2862 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002863 }
2864}
2865
David Ahernc21ef3e2017-04-16 09:48:24 -07002866static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2867 struct netlink_ext_ack *extack)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002868{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002869 struct net *net = sock_net(skb->sk);
Jiri Benc79e1ad12017-11-02 17:04:38 -02002870 struct net *tgt_net = net;
Thomas Grafb60c5112006-08-04 23:05:34 -07002871 struct ifinfomsg *ifm;
Eric Dumazeta3d12892009-10-21 10:59:31 +00002872 char ifname[IFNAMSIZ];
Thomas Grafb60c5112006-08-04 23:05:34 -07002873 struct nlattr *tb[IFLA_MAX+1];
2874 struct net_device *dev = NULL;
2875 struct sk_buff *nskb;
Jiri Benc79e1ad12017-11-02 17:04:38 -02002876 int netnsid = -1;
Thomas Graf339bf982006-11-10 14:10:15 -08002877 int err;
Greg Rose115c9b82012-02-21 16:54:48 -05002878 u32 ext_filter_mask = 0;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002879
David Ahernc21ef3e2017-04-16 09:48:24 -07002880 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Thomas Grafb60c5112006-08-04 23:05:34 -07002881 if (err < 0)
Eric Sesterhenn9918f232006-09-26 23:26:38 -07002882 return err;
Thomas Grafb60c5112006-08-04 23:05:34 -07002883
Jiri Benc79e1ad12017-11-02 17:04:38 -02002884 if (tb[IFLA_IF_NETNSID]) {
2885 netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]);
2886 tgt_net = get_target_net(skb, netnsid);
2887 if (IS_ERR(tgt_net))
2888 return PTR_ERR(tgt_net);
2889 }
2890
Eric Dumazeta3d12892009-10-21 10:59:31 +00002891 if (tb[IFLA_IFNAME])
2892 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2893
Greg Rose115c9b82012-02-21 16:54:48 -05002894 if (tb[IFLA_EXT_MASK])
2895 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2896
Jiri Benc79e1ad12017-11-02 17:04:38 -02002897 err = -EINVAL;
Thomas Grafb60c5112006-08-04 23:05:34 -07002898 ifm = nlmsg_data(nlh);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002899 if (ifm->ifi_index > 0)
Jiri Benc79e1ad12017-11-02 17:04:38 -02002900 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002901 else if (tb[IFLA_IFNAME])
Jiri Benc79e1ad12017-11-02 17:04:38 -02002902 dev = __dev_get_by_name(tgt_net, ifname);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002903 else
Jiri Benc79e1ad12017-11-02 17:04:38 -02002904 goto out;
Thomas Grafb60c5112006-08-04 23:05:34 -07002905
Jiri Benc79e1ad12017-11-02 17:04:38 -02002906 err = -ENODEV;
Eric Dumazeta3d12892009-10-21 10:59:31 +00002907 if (dev == NULL)
Jiri Benc79e1ad12017-11-02 17:04:38 -02002908 goto out;
Eric Dumazeta3d12892009-10-21 10:59:31 +00002909
Jiri Benc79e1ad12017-11-02 17:04:38 -02002910 err = -ENOBUFS;
Greg Rose115c9b82012-02-21 16:54:48 -05002911 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002912 if (nskb == NULL)
Jiri Benc79e1ad12017-11-02 17:04:38 -02002913 goto out;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002914
Jiri Benc79e1ad12017-11-02 17:04:38 -02002915 err = rtnl_fill_ifinfo(nskb, dev, net,
2916 RTM_NEWLINK, NETLINK_CB(skb).portid,
2917 nlh->nlmsg_seq, 0, 0, ext_filter_mask,
2918 0, NULL, netnsid);
Patrick McHardy26932562007-01-31 23:16:40 -08002919 if (err < 0) {
2920 /* -EMSGSIZE implies BUG in if_nlmsg_size */
2921 WARN_ON(err == -EMSGSIZE);
2922 kfree_skb(nskb);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002923 } else
Eric W. Biederman15e47302012-09-07 20:12:54 +00002924 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
Jiri Benc79e1ad12017-11-02 17:04:38 -02002925out:
2926 if (netnsid >= 0)
2927 put_net(tgt_net);
Thomas Grafb60c5112006-08-04 23:05:34 -07002928
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002929 return err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002930}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002931
Greg Rose115c9b82012-02-21 16:54:48 -05002932static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
Greg Rosec7ac8672011-06-10 01:27:09 +00002933{
Greg Rose115c9b82012-02-21 16:54:48 -05002934 struct net *net = sock_net(skb->sk);
2935 struct net_device *dev;
2936 struct nlattr *tb[IFLA_MAX+1];
2937 u32 ext_filter_mask = 0;
2938 u16 min_ifinfo_dump_size = 0;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02002939 int hdrlen;
Greg Rose115c9b82012-02-21 16:54:48 -05002940
Michal Schmidte5eca6d2014-05-28 14:15:19 +02002941 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2942 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2943 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2944
Johannes Bergfceb6432017-04-12 14:34:07 +02002945 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00002946 if (tb[IFLA_EXT_MASK])
2947 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2948 }
Greg Rose115c9b82012-02-21 16:54:48 -05002949
2950 if (!ext_filter_mask)
2951 return NLMSG_GOODSIZE;
2952 /*
2953 * traverse the list of net devices and compute the minimum
2954 * buffer size based upon the filter mask.
2955 */
Florian Westphal6853dd42017-08-09 20:41:51 +02002956 rcu_read_lock();
2957 for_each_netdev_rcu(net, dev) {
Greg Rose115c9b82012-02-21 16:54:48 -05002958 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
2959 if_nlmsg_size(dev,
2960 ext_filter_mask));
2961 }
Florian Westphal6853dd42017-08-09 20:41:51 +02002962 rcu_read_unlock();
Greg Rose115c9b82012-02-21 16:54:48 -05002963
Zhang Shengju93af2052016-11-22 14:14:28 +08002964 return nlmsg_total_size(min_ifinfo_dump_size);
Greg Rosec7ac8672011-06-10 01:27:09 +00002965}
2966
Adrian Bunk42bad1d2007-04-26 00:57:41 -07002967static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968{
2969 int idx;
2970 int s_idx = cb->family;
2971
2972 if (s_idx == 0)
2973 s_idx = 1;
Florian Westphal6853dd42017-08-09 20:41:51 +02002974
Patrick McHardy25239ce2010-04-26 16:02:05 +02002975 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 int type = cb->nlh->nlmsg_type-RTM_BASE;
Florian Westphal6853dd42017-08-09 20:41:51 +02002977 struct rtnl_link *handlers;
2978 rtnl_dumpit_func dumpit;
2979
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 if (idx < s_idx || idx == PF_PACKET)
2981 continue;
Florian Westphal6853dd42017-08-09 20:41:51 +02002982
2983 handlers = rtnl_dereference(rtnl_msg_handlers[idx]);
2984 if (!handlers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 continue;
Florian Westphal6853dd42017-08-09 20:41:51 +02002986
2987 dumpit = READ_ONCE(handlers[type].dumpit);
2988 if (!dumpit)
2989 continue;
2990
Nicolas Dichtel04652772013-03-22 06:28:42 +00002991 if (idx > s_idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 memset(&cb->args[0], 0, sizeof(cb->args));
Nicolas Dichtel04652772013-03-22 06:28:42 +00002993 cb->prev_seq = 0;
2994 cb->seq = 0;
2995 }
Florian Westphal6853dd42017-08-09 20:41:51 +02002996 if (dumpit(skb, cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 break;
2998 }
2999 cb->family = idx;
3000
3001 return skb->len;
3002}
3003
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003004struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003005 unsigned int change,
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003006 u32 event, gfp_t flags, int *new_nsid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003008 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 struct sk_buff *skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003010 int err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00003011 size_t if_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012
Alexei Starovoitov7f294052013-10-23 16:02:42 -07003013 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003014 if (skb == NULL)
3015 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
Jiri Benc79e1ad12017-11-02 17:04:38 -02003017 err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
3018 type, 0, 0, change, 0, 0, event,
3019 new_nsid, -1);
Patrick McHardy26932562007-01-31 23:16:40 -08003020 if (err < 0) {
3021 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
3022 WARN_ON(err == -EMSGSIZE);
3023 kfree_skb(skb);
3024 goto errout;
3025 }
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003026 return skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003027errout:
3028 if (err < 0)
Eric W. Biederman4b3da702007-11-19 22:27:40 -08003029 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003030 return NULL;
3031}
3032
3033void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
3034{
3035 struct net *net = dev_net(dev);
3036
3037 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
3038}
3039
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003040static void rtmsg_ifinfo_event(int type, struct net_device *dev,
3041 unsigned int change, u32 event,
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003042 gfp_t flags, int *new_nsid)
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003043{
3044 struct sk_buff *skb;
3045
Nicolas Dichteled2a80a2015-05-13 14:19:42 +02003046 if (dev->reg_state != NETREG_REGISTERED)
3047 return;
3048
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003049 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003050 if (skb)
3051 rtmsg_ifinfo_send(skb, dev, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052}
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003053
3054void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
3055 gfp_t flags)
3056{
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003057 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, NULL);
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003058}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003060void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
3061 gfp_t flags, int *new_nsid)
3062{
3063 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3064 new_nsid);
3065}
3066
John Fastabendd83b0602012-04-15 06:44:08 +00003067static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
3068 struct net_device *dev,
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003069 u8 *addr, u16 vid, u32 pid, u32 seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003070 int type, unsigned int flags,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003071 int nlflags, u16 ndm_state)
John Fastabendd83b0602012-04-15 06:44:08 +00003072{
3073 struct nlmsghdr *nlh;
3074 struct ndmsg *ndm;
3075
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003076 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
John Fastabendd83b0602012-04-15 06:44:08 +00003077 if (!nlh)
3078 return -EMSGSIZE;
3079
3080 ndm = nlmsg_data(nlh);
3081 ndm->ndm_family = AF_BRIDGE;
3082 ndm->ndm_pad1 = 0;
3083 ndm->ndm_pad2 = 0;
3084 ndm->ndm_flags = flags;
3085 ndm->ndm_type = 0;
3086 ndm->ndm_ifindex = dev->ifindex;
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003087 ndm->ndm_state = ndm_state;
John Fastabendd83b0602012-04-15 06:44:08 +00003088
3089 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
3090 goto nla_put_failure;
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003091 if (vid)
3092 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
3093 goto nla_put_failure;
John Fastabendd83b0602012-04-15 06:44:08 +00003094
Johannes Berg053c0952015-01-16 22:09:00 +01003095 nlmsg_end(skb, nlh);
3096 return 0;
John Fastabendd83b0602012-04-15 06:44:08 +00003097
3098nla_put_failure:
3099 nlmsg_cancel(skb, nlh);
3100 return -EMSGSIZE;
3101}
3102
John Fastabend3ff661c2012-04-15 06:44:14 +00003103static inline size_t rtnl_fdb_nlmsg_size(void)
3104{
Sabrina Dubrocaf82ef3e2016-11-18 15:50:39 +01003105 return NLMSG_ALIGN(sizeof(struct ndmsg)) +
3106 nla_total_size(ETH_ALEN) + /* NDA_LLADDR */
3107 nla_total_size(sizeof(u16)) + /* NDA_VLAN */
3108 0;
John Fastabend3ff661c2012-04-15 06:44:14 +00003109}
3110
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003111static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
3112 u16 ndm_state)
John Fastabend3ff661c2012-04-15 06:44:14 +00003113{
3114 struct net *net = dev_net(dev);
3115 struct sk_buff *skb;
3116 int err = -ENOBUFS;
3117
3118 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
3119 if (!skb)
3120 goto errout;
3121
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003122 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003123 0, 0, type, NTF_SELF, 0, ndm_state);
John Fastabend3ff661c2012-04-15 06:44:14 +00003124 if (err < 0) {
3125 kfree_skb(skb);
3126 goto errout;
3127 }
3128
3129 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
3130 return;
3131errout:
3132 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
3133}
3134
Vlad Yasevich090096b2013-03-06 15:39:42 +00003135/**
3136 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3137 */
3138int ndo_dflt_fdb_add(struct ndmsg *ndm,
3139 struct nlattr *tb[],
3140 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003141 const unsigned char *addr, u16 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003142 u16 flags)
3143{
3144 int err = -EINVAL;
3145
3146 /* If aging addresses are supported device will need to
3147 * implement its own handler for this.
3148 */
3149 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
3150 pr_info("%s: FDB only supports static addresses\n", dev->name);
3151 return err;
3152 }
3153
Or Gerlitz65891fe2014-12-14 18:19:05 +02003154 if (vid) {
3155 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
3156 return err;
3157 }
3158
Vlad Yasevich090096b2013-03-06 15:39:42 +00003159 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3160 err = dev_uc_add_excl(dev, addr);
3161 else if (is_multicast_ether_addr(addr))
3162 err = dev_mc_add_excl(dev, addr);
3163
3164 /* Only return duplicate errors if NLM_F_EXCL is set */
3165 if (err == -EEXIST && !(flags & NLM_F_EXCL))
3166 err = 0;
3167
3168 return err;
3169}
3170EXPORT_SYMBOL(ndo_dflt_fdb_add);
3171
Florian Westphalb88d12e2017-10-10 17:10:04 +02003172static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
3173 struct netlink_ext_ack *extack)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003174{
3175 u16 vid = 0;
3176
3177 if (vlan_attr) {
3178 if (nla_len(vlan_attr) != sizeof(u16)) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003179 NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
Jiri Pirkof6f64242014-11-28 14:34:15 +01003180 return -EINVAL;
3181 }
3182
3183 vid = nla_get_u16(vlan_attr);
3184
3185 if (!vid || vid >= VLAN_VID_MASK) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003186 NL_SET_ERR_MSG(extack, "invalid vlan id");
Jiri Pirkof6f64242014-11-28 14:34:15 +01003187 return -EINVAL;
3188 }
3189 }
3190 *p_vid = vid;
3191 return 0;
3192}
3193
David Ahernc21ef3e2017-04-16 09:48:24 -07003194static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
3195 struct netlink_ext_ack *extack)
John Fastabend77162022012-04-15 06:43:56 +00003196{
3197 struct net *net = sock_net(skb->sk);
John Fastabend77162022012-04-15 06:43:56 +00003198 struct ndmsg *ndm;
3199 struct nlattr *tb[NDA_MAX+1];
3200 struct net_device *dev;
3201 u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01003202 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00003203 int err;
3204
David Ahernc21ef3e2017-04-16 09:48:24 -07003205 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
John Fastabend77162022012-04-15 06:43:56 +00003206 if (err < 0)
3207 return err;
3208
3209 ndm = nlmsg_data(nlh);
3210 if (ndm->ndm_ifindex == 0) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003211 NL_SET_ERR_MSG(extack, "invalid ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003212 return -EINVAL;
3213 }
3214
3215 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3216 if (dev == NULL) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003217 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003218 return -ENODEV;
3219 }
3220
3221 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003222 NL_SET_ERR_MSG(extack, "invalid address");
John Fastabend77162022012-04-15 06:43:56 +00003223 return -EINVAL;
3224 }
3225
3226 addr = nla_data(tb[NDA_LLADDR]);
John Fastabend77162022012-04-15 06:43:56 +00003227
Florian Westphalb88d12e2017-10-10 17:10:04 +02003228 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
Jiri Pirkof6f64242014-11-28 14:34:15 +01003229 if (err)
3230 return err;
3231
John Fastabend77162022012-04-15 06:43:56 +00003232 err = -EOPNOTSUPP;
3233
3234 /* Support fdb on master device the net/bridge default case */
3235 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3236 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003237 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3238 const struct net_device_ops *ops = br_dev->netdev_ops;
3239
Jiri Pirkof6f64242014-11-28 14:34:15 +01003240 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
3241 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00003242 if (err)
3243 goto out;
3244 else
3245 ndm->ndm_flags &= ~NTF_MASTER;
3246 }
3247
3248 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00003249 if ((ndm->ndm_flags & NTF_SELF)) {
3250 if (dev->netdev_ops->ndo_fdb_add)
3251 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003252 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003253 nlh->nlmsg_flags);
3254 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01003255 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003256 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00003257
John Fastabend3ff661c2012-04-15 06:44:14 +00003258 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003259 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
3260 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00003261 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00003262 }
John Fastabend77162022012-04-15 06:43:56 +00003263 }
3264out:
3265 return err;
3266}
3267
Vlad Yasevich090096b2013-03-06 15:39:42 +00003268/**
3269 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3270 */
3271int ndo_dflt_fdb_del(struct ndmsg *ndm,
3272 struct nlattr *tb[],
3273 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003274 const unsigned char *addr, u16 vid)
Vlad Yasevich090096b2013-03-06 15:39:42 +00003275{
Alexander Duyckc8a89c42014-07-15 15:15:20 -07003276 int err = -EINVAL;
Vlad Yasevich090096b2013-03-06 15:39:42 +00003277
3278 /* If aging addresses are supported device will need to
3279 * implement its own handler for this.
3280 */
Sridhar Samudrala64535992013-08-08 15:19:48 -07003281 if (!(ndm->ndm_state & NUD_PERMANENT)) {
Vlad Yasevich090096b2013-03-06 15:39:42 +00003282 pr_info("%s: FDB only supports static addresses\n", dev->name);
Alexander Duyckc8a89c42014-07-15 15:15:20 -07003283 return err;
Vlad Yasevich090096b2013-03-06 15:39:42 +00003284 }
3285
3286 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3287 err = dev_uc_del(dev, addr);
3288 else if (is_multicast_ether_addr(addr))
3289 err = dev_mc_del(dev, addr);
Vlad Yasevich090096b2013-03-06 15:39:42 +00003290
3291 return err;
3292}
3293EXPORT_SYMBOL(ndo_dflt_fdb_del);
3294
David Ahernc21ef3e2017-04-16 09:48:24 -07003295static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
3296 struct netlink_ext_ack *extack)
John Fastabend77162022012-04-15 06:43:56 +00003297{
3298 struct net *net = sock_net(skb->sk);
3299 struct ndmsg *ndm;
Vlad Yasevich1690be62013-02-13 12:00:18 +00003300 struct nlattr *tb[NDA_MAX+1];
John Fastabend77162022012-04-15 06:43:56 +00003301 struct net_device *dev;
3302 int err = -EINVAL;
3303 __u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01003304 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00003305
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07003306 if (!netlink_capable(skb, CAP_NET_ADMIN))
Vlad Yasevich1690be62013-02-13 12:00:18 +00003307 return -EPERM;
3308
David Ahernc21ef3e2017-04-16 09:48:24 -07003309 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
Vlad Yasevich1690be62013-02-13 12:00:18 +00003310 if (err < 0)
3311 return err;
John Fastabend77162022012-04-15 06:43:56 +00003312
3313 ndm = nlmsg_data(nlh);
3314 if (ndm->ndm_ifindex == 0) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003315 NL_SET_ERR_MSG(extack, "invalid ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003316 return -EINVAL;
3317 }
3318
3319 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3320 if (dev == NULL) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003321 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003322 return -ENODEV;
3323 }
3324
Vlad Yasevich1690be62013-02-13 12:00:18 +00003325 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003326 NL_SET_ERR_MSG(extack, "invalid address");
John Fastabend77162022012-04-15 06:43:56 +00003327 return -EINVAL;
3328 }
3329
Vlad Yasevich1690be62013-02-13 12:00:18 +00003330 addr = nla_data(tb[NDA_LLADDR]);
Vlad Yasevich1690be62013-02-13 12:00:18 +00003331
Florian Westphalb88d12e2017-10-10 17:10:04 +02003332 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
Jiri Pirkof6f64242014-11-28 14:34:15 +01003333 if (err)
3334 return err;
3335
John Fastabend77162022012-04-15 06:43:56 +00003336 err = -EOPNOTSUPP;
3337
3338 /* Support fdb on master device the net/bridge default case */
3339 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3340 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003341 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3342 const struct net_device_ops *ops = br_dev->netdev_ops;
John Fastabend77162022012-04-15 06:43:56 +00003343
Jiri Pirko898e5062013-01-03 22:48:52 +00003344 if (ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003345 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00003346
3347 if (err)
3348 goto out;
3349 else
3350 ndm->ndm_flags &= ~NTF_MASTER;
3351 }
3352
3353 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00003354 if (ndm->ndm_flags & NTF_SELF) {
3355 if (dev->netdev_ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003356 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
3357 vid);
Vlad Yasevich090096b2013-03-06 15:39:42 +00003358 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01003359 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00003360
John Fastabend3ff661c2012-04-15 06:44:14 +00003361 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003362 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
3363 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00003364 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00003365 }
John Fastabend77162022012-04-15 06:43:56 +00003366 }
3367out:
3368 return err;
3369}
3370
John Fastabendd83b0602012-04-15 06:44:08 +00003371static int nlmsg_populate_fdb(struct sk_buff *skb,
3372 struct netlink_callback *cb,
3373 struct net_device *dev,
3374 int *idx,
3375 struct netdev_hw_addr_list *list)
3376{
3377 struct netdev_hw_addr *ha;
3378 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00003379 u32 portid, seq;
John Fastabendd83b0602012-04-15 06:44:08 +00003380
Eric W. Biederman15e47302012-09-07 20:12:54 +00003381 portid = NETLINK_CB(cb->skb).portid;
John Fastabendd83b0602012-04-15 06:44:08 +00003382 seq = cb->nlh->nlmsg_seq;
3383
3384 list_for_each_entry(ha, &list->list, list) {
Roopa Prabhud2976532016-08-30 21:56:45 -07003385 if (*idx < cb->args[2])
John Fastabendd83b0602012-04-15 06:44:08 +00003386 goto skip;
3387
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003388 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
John Fastabenda7a558f2012-11-01 16:23:10 +00003389 portid, seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003390 RTM_NEWNEIGH, NTF_SELF,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003391 NLM_F_MULTI, NUD_PERMANENT);
John Fastabendd83b0602012-04-15 06:44:08 +00003392 if (err < 0)
3393 return err;
3394skip:
3395 *idx += 1;
3396 }
3397 return 0;
3398}
3399
3400/**
Ben Hutchings2c530402012-07-10 10:55:09 +00003401 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
John Fastabendd83b0602012-04-15 06:44:08 +00003402 * @nlh: netlink message header
3403 * @dev: netdevice
3404 *
3405 * Default netdevice operation to dump the existing unicast address list.
John Fastabend91f3e7b2013-03-29 08:18:37 +00003406 * Returns number of addresses from list put in skb.
John Fastabendd83b0602012-04-15 06:44:08 +00003407 */
3408int ndo_dflt_fdb_dump(struct sk_buff *skb,
3409 struct netlink_callback *cb,
3410 struct net_device *dev,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04003411 struct net_device *filter_dev,
Roopa Prabhud2976532016-08-30 21:56:45 -07003412 int *idx)
John Fastabendd83b0602012-04-15 06:44:08 +00003413{
3414 int err;
3415
3416 netif_addr_lock_bh(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -07003417 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
John Fastabendd83b0602012-04-15 06:44:08 +00003418 if (err)
3419 goto out;
Zhang Shengju2934c9d2016-11-30 16:37:34 +08003420 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
John Fastabendd83b0602012-04-15 06:44:08 +00003421out:
3422 netif_addr_unlock_bh(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -07003423 return err;
John Fastabendd83b0602012-04-15 06:44:08 +00003424}
3425EXPORT_SYMBOL(ndo_dflt_fdb_dump);
3426
John Fastabend77162022012-04-15 06:43:56 +00003427static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
3428{
John Fastabend77162022012-04-15 06:43:56 +00003429 struct net_device *dev;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003430 struct nlattr *tb[IFLA_MAX+1];
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003431 struct net_device *br_dev = NULL;
3432 const struct net_device_ops *ops = NULL;
3433 const struct net_device_ops *cops = NULL;
3434 struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
3435 struct net *net = sock_net(skb->sk);
Roopa Prabhud2976532016-08-30 21:56:45 -07003436 struct hlist_head *head;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003437 int brport_idx = 0;
3438 int br_idx = 0;
Roopa Prabhud2976532016-08-30 21:56:45 -07003439 int h, s_h;
3440 int idx = 0, s_idx;
3441 int err = 0;
3442 int fidx = 0;
John Fastabend77162022012-04-15 06:43:56 +00003443
Alexander Potapenko0ff50e82017-05-23 13:20:28 +02003444 err = nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb,
3445 IFLA_MAX, ifla_policy, NULL);
3446 if (err < 0) {
3447 return -EINVAL;
3448 } else if (err == 0) {
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003449 if (tb[IFLA_MASTER])
3450 br_idx = nla_get_u32(tb[IFLA_MASTER]);
3451 }
John Fastabend77162022012-04-15 06:43:56 +00003452
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003453 brport_idx = ifm->ifi_index;
3454
3455 if (br_idx) {
3456 br_dev = __dev_get_by_index(net, br_idx);
3457 if (!br_dev)
3458 return -ENODEV;
3459
3460 ops = br_dev->netdev_ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003461 }
3462
Roopa Prabhud2976532016-08-30 21:56:45 -07003463 s_h = cb->args[0];
3464 s_idx = cb->args[1];
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003465
Roopa Prabhud2976532016-08-30 21:56:45 -07003466 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
3467 idx = 0;
3468 head = &net->dev_index_head[h];
3469 hlist_for_each_entry(dev, head, index_hlist) {
3470
3471 if (brport_idx && (dev->ifindex != brport_idx))
3472 continue;
3473
3474 if (!br_idx) { /* user did not specify a specific bridge */
3475 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3476 br_dev = netdev_master_upper_dev_get(dev);
3477 cops = br_dev->netdev_ops;
3478 }
3479 } else {
3480 if (dev != br_dev &&
3481 !(dev->priv_flags & IFF_BRIDGE_PORT))
3482 continue;
3483
3484 if (br_dev != netdev_master_upper_dev_get(dev) &&
3485 !(dev->priv_flags & IFF_EBRIDGE))
3486 continue;
3487 cops = ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003488 }
3489
Roopa Prabhud2976532016-08-30 21:56:45 -07003490 if (idx < s_idx)
3491 goto cont;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003492
Roopa Prabhud2976532016-08-30 21:56:45 -07003493 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3494 if (cops && cops->ndo_fdb_dump) {
3495 err = cops->ndo_fdb_dump(skb, cb,
3496 br_dev, dev,
3497 &fidx);
3498 if (err == -EMSGSIZE)
3499 goto out;
3500 }
3501 }
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003502
Roopa Prabhud2976532016-08-30 21:56:45 -07003503 if (dev->netdev_ops->ndo_fdb_dump)
3504 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
3505 dev, NULL,
3506 &fidx);
3507 else
3508 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
3509 &fidx);
3510 if (err == -EMSGSIZE)
3511 goto out;
3512
3513 cops = NULL;
3514
3515 /* reset fdb offset to 0 for rest of the interfaces */
3516 cb->args[2] = 0;
3517 fidx = 0;
3518cont:
3519 idx++;
John Fastabend77162022012-04-15 06:43:56 +00003520 }
John Fastabend77162022012-04-15 06:43:56 +00003521 }
John Fastabend77162022012-04-15 06:43:56 +00003522
Roopa Prabhud2976532016-08-30 21:56:45 -07003523out:
3524 cb->args[0] = h;
3525 cb->args[1] = idx;
3526 cb->args[2] = fidx;
3527
John Fastabend77162022012-04-15 06:43:56 +00003528 return skb->len;
3529}
3530
Scott Feldman2c3c0312014-11-28 14:34:25 +01003531static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
3532 unsigned int attrnum, unsigned int flag)
3533{
3534 if (mask & flag)
3535 return nla_put_u8(skb, attrnum, !!(flags & flag));
3536 return 0;
3537}
3538
John Fastabend815cccb2012-10-24 08:13:09 +00003539int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Scott Feldman2c3c0312014-11-28 14:34:25 +01003540 struct net_device *dev, u16 mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003541 u32 flags, u32 mask, int nlflags,
3542 u32 filter_mask,
3543 int (*vlan_fill)(struct sk_buff *skb,
3544 struct net_device *dev,
3545 u32 filter_mask))
John Fastabend815cccb2012-10-24 08:13:09 +00003546{
3547 struct nlmsghdr *nlh;
3548 struct ifinfomsg *ifm;
3549 struct nlattr *br_afspec;
Scott Feldman2c3c0312014-11-28 14:34:25 +01003550 struct nlattr *protinfo;
John Fastabend815cccb2012-10-24 08:13:09 +00003551 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Jiri Pirko898e5062013-01-03 22:48:52 +00003552 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003553 int err = 0;
John Fastabend815cccb2012-10-24 08:13:09 +00003554
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02003555 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
John Fastabend815cccb2012-10-24 08:13:09 +00003556 if (nlh == NULL)
3557 return -EMSGSIZE;
3558
3559 ifm = nlmsg_data(nlh);
3560 ifm->ifi_family = AF_BRIDGE;
3561 ifm->__ifi_pad = 0;
3562 ifm->ifi_type = dev->type;
3563 ifm->ifi_index = dev->ifindex;
3564 ifm->ifi_flags = dev_get_flags(dev);
3565 ifm->ifi_change = 0;
3566
3567
3568 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
3569 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
3570 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00003571 (br_dev &&
3572 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
John Fastabend815cccb2012-10-24 08:13:09 +00003573 (dev->addr_len &&
3574 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02003575 (dev->ifindex != dev_get_iflink(dev) &&
3576 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
John Fastabend815cccb2012-10-24 08:13:09 +00003577 goto nla_put_failure;
3578
3579 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
3580 if (!br_afspec)
3581 goto nla_put_failure;
3582
Roopa Prabhu1d460b92014-12-08 14:04:20 -08003583 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
John Fastabend815cccb2012-10-24 08:13:09 +00003584 nla_nest_cancel(skb, br_afspec);
3585 goto nla_put_failure;
3586 }
Roopa Prabhu1d460b92014-12-08 14:04:20 -08003587
3588 if (mode != BRIDGE_MODE_UNDEF) {
3589 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
3590 nla_nest_cancel(skb, br_afspec);
3591 goto nla_put_failure;
3592 }
3593 }
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003594 if (vlan_fill) {
3595 err = vlan_fill(skb, dev, filter_mask);
3596 if (err) {
3597 nla_nest_cancel(skb, br_afspec);
3598 goto nla_put_failure;
3599 }
3600 }
John Fastabend815cccb2012-10-24 08:13:09 +00003601 nla_nest_end(skb, br_afspec);
3602
Scott Feldman2c3c0312014-11-28 14:34:25 +01003603 protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
3604 if (!protinfo)
3605 goto nla_put_failure;
3606
3607 if (brport_nla_put_flag(skb, flags, mask,
3608 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
3609 brport_nla_put_flag(skb, flags, mask,
3610 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
3611 brport_nla_put_flag(skb, flags, mask,
3612 IFLA_BRPORT_FAST_LEAVE,
3613 BR_MULTICAST_FAST_LEAVE) ||
3614 brport_nla_put_flag(skb, flags, mask,
3615 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
3616 brport_nla_put_flag(skb, flags, mask,
3617 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
3618 brport_nla_put_flag(skb, flags, mask,
3619 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
3620 brport_nla_put_flag(skb, flags, mask,
3621 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
3622 brport_nla_put_flag(skb, flags, mask,
3623 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
3624 nla_nest_cancel(skb, protinfo);
3625 goto nla_put_failure;
3626 }
3627
3628 nla_nest_end(skb, protinfo);
3629
Johannes Berg053c0952015-01-16 22:09:00 +01003630 nlmsg_end(skb, nlh);
3631 return 0;
John Fastabend815cccb2012-10-24 08:13:09 +00003632nla_put_failure:
3633 nlmsg_cancel(skb, nlh);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003634 return err ? err : -EMSGSIZE;
John Fastabend815cccb2012-10-24 08:13:09 +00003635}
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003636EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
John Fastabend815cccb2012-10-24 08:13:09 +00003637
John Fastabende5a55a82012-10-24 08:12:57 +00003638static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
3639{
3640 struct net *net = sock_net(skb->sk);
3641 struct net_device *dev;
3642 int idx = 0;
3643 u32 portid = NETLINK_CB(cb->skb).portid;
3644 u32 seq = cb->nlh->nlmsg_seq;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00003645 u32 filter_mask = 0;
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003646 int err;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00003647
Thomas Grafaa68c202014-11-26 13:42:20 +01003648 if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
3649 struct nlattr *extfilt;
3650
3651 extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
3652 IFLA_EXT_MASK);
3653 if (extfilt) {
3654 if (nla_len(extfilt) < sizeof(filter_mask))
3655 return -EINVAL;
3656
3657 filter_mask = nla_get_u32(extfilt);
3658 }
3659 }
John Fastabende5a55a82012-10-24 08:12:57 +00003660
3661 rcu_read_lock();
3662 for_each_netdev_rcu(net, dev) {
3663 const struct net_device_ops *ops = dev->netdev_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00003664 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
John Fastabende5a55a82012-10-24 08:12:57 +00003665
Jiri Pirko898e5062013-01-03 22:48:52 +00003666 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003667 if (idx >= cb->args[0]) {
3668 err = br_dev->netdev_ops->ndo_bridge_getlink(
3669 skb, portid, seq, dev,
3670 filter_mask, NLM_F_MULTI);
David Ahernf6c57752017-05-15 23:19:17 -07003671 if (err < 0 && err != -EOPNOTSUPP) {
3672 if (likely(skb->len))
3673 break;
3674
3675 goto out_err;
3676 }
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003677 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00003678 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00003679 }
3680
3681 if (ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003682 if (idx >= cb->args[0]) {
3683 err = ops->ndo_bridge_getlink(skb, portid,
3684 seq, dev,
3685 filter_mask,
3686 NLM_F_MULTI);
David Ahernf6c57752017-05-15 23:19:17 -07003687 if (err < 0 && err != -EOPNOTSUPP) {
3688 if (likely(skb->len))
3689 break;
3690
3691 goto out_err;
3692 }
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003693 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00003694 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00003695 }
3696 }
David Ahernf6c57752017-05-15 23:19:17 -07003697 err = skb->len;
3698out_err:
John Fastabende5a55a82012-10-24 08:12:57 +00003699 rcu_read_unlock();
3700 cb->args[0] = idx;
3701
David Ahernf6c57752017-05-15 23:19:17 -07003702 return err;
John Fastabende5a55a82012-10-24 08:12:57 +00003703}
3704
John Fastabend2469ffd2012-10-24 08:13:03 +00003705static inline size_t bridge_nlmsg_size(void)
3706{
3707 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3708 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3709 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3710 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
3711 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
3712 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
3713 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
3714 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
3715 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
3716 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
3717 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
3718}
3719
Roopa Prabhu02dba432015-01-14 20:02:25 -08003720static int rtnl_bridge_notify(struct net_device *dev)
John Fastabend2469ffd2012-10-24 08:13:03 +00003721{
3722 struct net *net = dev_net(dev);
John Fastabend2469ffd2012-10-24 08:13:03 +00003723 struct sk_buff *skb;
3724 int err = -EOPNOTSUPP;
3725
Roopa Prabhu02dba432015-01-14 20:02:25 -08003726 if (!dev->netdev_ops->ndo_bridge_getlink)
3727 return 0;
3728
John Fastabend2469ffd2012-10-24 08:13:03 +00003729 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
3730 if (!skb) {
3731 err = -ENOMEM;
3732 goto errout;
3733 }
3734
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02003735 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
Roopa Prabhu02dba432015-01-14 20:02:25 -08003736 if (err < 0)
3737 goto errout;
John Fastabend2469ffd2012-10-24 08:13:03 +00003738
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08003739 if (!skb->len)
3740 goto errout;
3741
John Fastabend2469ffd2012-10-24 08:13:03 +00003742 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
3743 return 0;
3744errout:
3745 WARN_ON(err == -EMSGSIZE);
3746 kfree_skb(skb);
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08003747 if (err)
3748 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
John Fastabend2469ffd2012-10-24 08:13:03 +00003749 return err;
3750}
3751
David Ahernc21ef3e2017-04-16 09:48:24 -07003752static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3753 struct netlink_ext_ack *extack)
John Fastabende5a55a82012-10-24 08:12:57 +00003754{
3755 struct net *net = sock_net(skb->sk);
3756 struct ifinfomsg *ifm;
3757 struct net_device *dev;
John Fastabend2469ffd2012-10-24 08:13:03 +00003758 struct nlattr *br_spec, *attr = NULL;
3759 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02003760 u16 flags = 0;
John Fastabendc38e01b2012-11-02 16:32:36 +00003761 bool have_flags = false;
John Fastabende5a55a82012-10-24 08:12:57 +00003762
3763 if (nlmsg_len(nlh) < sizeof(*ifm))
3764 return -EINVAL;
3765
3766 ifm = nlmsg_data(nlh);
3767 if (ifm->ifi_family != AF_BRIDGE)
3768 return -EPFNOSUPPORT;
3769
3770 dev = __dev_get_by_index(net, ifm->ifi_index);
3771 if (!dev) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003772 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabende5a55a82012-10-24 08:12:57 +00003773 return -ENODEV;
3774 }
3775
John Fastabend2469ffd2012-10-24 08:13:03 +00003776 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3777 if (br_spec) {
3778 nla_for_each_nested(attr, br_spec, rem) {
3779 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01003780 if (nla_len(attr) < sizeof(flags))
3781 return -EINVAL;
3782
John Fastabendc38e01b2012-11-02 16:32:36 +00003783 have_flags = true;
John Fastabend2469ffd2012-10-24 08:13:03 +00003784 flags = nla_get_u16(attr);
3785 break;
3786 }
3787 }
3788 }
3789
3790 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003791 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3792
3793 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
John Fastabend2469ffd2012-10-24 08:13:03 +00003794 err = -EOPNOTSUPP;
3795 goto out;
3796 }
3797
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003798 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
John Fastabende5a55a82012-10-24 08:12:57 +00003799 if (err)
3800 goto out;
John Fastabend2469ffd2012-10-24 08:13:03 +00003801
3802 flags &= ~BRIDGE_FLAGS_MASTER;
John Fastabende5a55a82012-10-24 08:12:57 +00003803 }
3804
John Fastabend2469ffd2012-10-24 08:13:03 +00003805 if ((flags & BRIDGE_FLAGS_SELF)) {
3806 if (!dev->netdev_ops->ndo_bridge_setlink)
3807 err = -EOPNOTSUPP;
3808 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003809 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
3810 flags);
Roopa Prabhu02dba432015-01-14 20:02:25 -08003811 if (!err) {
John Fastabend2469ffd2012-10-24 08:13:03 +00003812 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08003813
3814 /* Generate event to notify upper layer of bridge
3815 * change
3816 */
3817 err = rtnl_bridge_notify(dev);
3818 }
John Fastabend2469ffd2012-10-24 08:13:03 +00003819 }
3820
John Fastabendc38e01b2012-11-02 16:32:36 +00003821 if (have_flags)
John Fastabend2469ffd2012-10-24 08:13:03 +00003822 memcpy(nla_data(attr), &flags, sizeof(flags));
John Fastabende5a55a82012-10-24 08:12:57 +00003823out:
3824 return err;
3825}
3826
David Ahernc21ef3e2017-04-16 09:48:24 -07003827static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
3828 struct netlink_ext_ack *extack)
Vlad Yasevich407af322013-02-13 12:00:12 +00003829{
3830 struct net *net = sock_net(skb->sk);
3831 struct ifinfomsg *ifm;
3832 struct net_device *dev;
3833 struct nlattr *br_spec, *attr = NULL;
3834 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02003835 u16 flags = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +00003836 bool have_flags = false;
3837
3838 if (nlmsg_len(nlh) < sizeof(*ifm))
3839 return -EINVAL;
3840
3841 ifm = nlmsg_data(nlh);
3842 if (ifm->ifi_family != AF_BRIDGE)
3843 return -EPFNOSUPPORT;
3844
3845 dev = __dev_get_by_index(net, ifm->ifi_index);
3846 if (!dev) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003847 NL_SET_ERR_MSG(extack, "unknown ifindex");
Vlad Yasevich407af322013-02-13 12:00:12 +00003848 return -ENODEV;
3849 }
3850
3851 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3852 if (br_spec) {
3853 nla_for_each_nested(attr, br_spec, rem) {
3854 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01003855 if (nla_len(attr) < sizeof(flags))
3856 return -EINVAL;
3857
Vlad Yasevich407af322013-02-13 12:00:12 +00003858 have_flags = true;
3859 flags = nla_get_u16(attr);
3860 break;
3861 }
3862 }
3863 }
3864
Vlad Yasevich407af322013-02-13 12:00:12 +00003865 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3866 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3867
3868 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
3869 err = -EOPNOTSUPP;
3870 goto out;
3871 }
3872
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003873 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00003874 if (err)
3875 goto out;
3876
3877 flags &= ~BRIDGE_FLAGS_MASTER;
3878 }
3879
3880 if ((flags & BRIDGE_FLAGS_SELF)) {
3881 if (!dev->netdev_ops->ndo_bridge_dellink)
3882 err = -EOPNOTSUPP;
3883 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003884 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
3885 flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00003886
Roopa Prabhu02dba432015-01-14 20:02:25 -08003887 if (!err) {
Vlad Yasevich407af322013-02-13 12:00:12 +00003888 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08003889
3890 /* Generate event to notify upper layer of bridge
3891 * change
3892 */
3893 err = rtnl_bridge_notify(dev);
3894 }
Vlad Yasevich407af322013-02-13 12:00:12 +00003895 }
3896
3897 if (have_flags)
3898 memcpy(nla_data(attr), &flags, sizeof(flags));
Vlad Yasevich407af322013-02-13 12:00:12 +00003899out:
3900 return err;
3901}
3902
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003903static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
3904{
3905 return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
3906 (!idxattr || idxattr == attrid);
3907}
3908
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02003909#define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
3910static int rtnl_get_offload_stats_attr_size(int attr_id)
3911{
3912 switch (attr_id) {
3913 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
3914 return sizeof(struct rtnl_link_stats64);
3915 }
3916
3917 return 0;
3918}
3919
3920static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
3921 int *prividx)
3922{
3923 struct nlattr *attr = NULL;
3924 int attr_id, size;
3925 void *attr_data;
3926 int err;
3927
3928 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
3929 dev->netdev_ops->ndo_get_offload_stats))
3930 return -ENODATA;
3931
3932 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
3933 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
3934 if (attr_id < *prividx)
3935 continue;
3936
3937 size = rtnl_get_offload_stats_attr_size(attr_id);
3938 if (!size)
3939 continue;
3940
Or Gerlitz3df5b3c2016-11-22 23:09:54 +02003941 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02003942 continue;
3943
3944 attr = nla_reserve_64bit(skb, attr_id, size,
3945 IFLA_OFFLOAD_XSTATS_UNSPEC);
3946 if (!attr)
3947 goto nla_put_failure;
3948
3949 attr_data = nla_data(attr);
3950 memset(attr_data, 0, size);
3951 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
3952 attr_data);
3953 if (err)
3954 goto get_offload_stats_failure;
3955 }
3956
3957 if (!attr)
3958 return -ENODATA;
3959
3960 *prividx = 0;
3961 return 0;
3962
3963nla_put_failure:
3964 err = -EMSGSIZE;
3965get_offload_stats_failure:
3966 *prividx = attr_id;
3967 return err;
3968}
3969
3970static int rtnl_get_offload_stats_size(const struct net_device *dev)
3971{
3972 int nla_size = 0;
3973 int attr_id;
3974 int size;
3975
3976 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
3977 dev->netdev_ops->ndo_get_offload_stats))
3978 return 0;
3979
3980 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
3981 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
Or Gerlitz3df5b3c2016-11-22 23:09:54 +02003982 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02003983 continue;
3984 size = rtnl_get_offload_stats_attr_size(attr_id);
3985 nla_size += nla_total_size_64bit(size);
3986 }
3987
3988 if (nla_size != 0)
3989 nla_size += nla_total_size(0);
3990
3991 return nla_size;
3992}
3993
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003994static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
3995 int type, u32 pid, u32 seq, u32 change,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003996 unsigned int flags, unsigned int filter_mask,
3997 int *idxattr, int *prividx)
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003998{
3999 struct if_stats_msg *ifsm;
4000 struct nlmsghdr *nlh;
4001 struct nlattr *attr;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004002 int s_prividx = *prividx;
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004003 int err;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004004
4005 ASSERT_RTNL();
4006
4007 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
4008 if (!nlh)
4009 return -EMSGSIZE;
4010
4011 ifsm = nlmsg_data(nlh);
Nikolay Aleksandrovce024f42017-10-03 13:20:48 +03004012 ifsm->family = PF_UNSPEC;
4013 ifsm->pad1 = 0;
4014 ifsm->pad2 = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004015 ifsm->ifindex = dev->ifindex;
4016 ifsm->filter_mask = filter_mask;
4017
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004018 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004019 struct rtnl_link_stats64 *sp;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004020
Nicolas Dichtel58414d32016-04-21 18:58:25 +02004021 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
4022 sizeof(struct rtnl_link_stats64),
4023 IFLA_STATS_UNSPEC);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004024 if (!attr)
4025 goto nla_put_failure;
4026
4027 sp = nla_data(attr);
4028 dev_get_stats(dev, sp);
4029 }
4030
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004031 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
4032 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
4033
4034 if (ops && ops->fill_linkxstats) {
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004035 *idxattr = IFLA_STATS_LINK_XSTATS;
4036 attr = nla_nest_start(skb,
4037 IFLA_STATS_LINK_XSTATS);
4038 if (!attr)
4039 goto nla_put_failure;
4040
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004041 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
4042 nla_nest_end(skb, attr);
4043 if (err)
4044 goto nla_put_failure;
4045 *idxattr = 0;
4046 }
4047 }
4048
4049 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
4050 *idxattr)) {
4051 const struct rtnl_link_ops *ops = NULL;
4052 const struct net_device *master;
4053
4054 master = netdev_master_upper_dev_get(dev);
4055 if (master)
4056 ops = master->rtnl_link_ops;
4057 if (ops && ops->fill_linkxstats) {
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004058 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
4059 attr = nla_nest_start(skb,
4060 IFLA_STATS_LINK_XSTATS_SLAVE);
4061 if (!attr)
4062 goto nla_put_failure;
4063
4064 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004065 nla_nest_end(skb, attr);
4066 if (err)
4067 goto nla_put_failure;
4068 *idxattr = 0;
4069 }
4070 }
4071
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004072 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
4073 *idxattr)) {
4074 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
4075 attr = nla_nest_start(skb, IFLA_STATS_LINK_OFFLOAD_XSTATS);
4076 if (!attr)
4077 goto nla_put_failure;
4078
4079 err = rtnl_get_offload_stats(skb, dev, prividx);
4080 if (err == -ENODATA)
4081 nla_nest_cancel(skb, attr);
4082 else
4083 nla_nest_end(skb, attr);
4084
4085 if (err && err != -ENODATA)
4086 goto nla_put_failure;
4087 *idxattr = 0;
4088 }
4089
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004090 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
4091 struct rtnl_af_ops *af_ops;
4092
4093 *idxattr = IFLA_STATS_AF_SPEC;
4094 attr = nla_nest_start(skb, IFLA_STATS_AF_SPEC);
4095 if (!attr)
4096 goto nla_put_failure;
4097
Florian Westphal5fa85a02017-10-16 15:44:36 +02004098 rcu_read_lock();
4099 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004100 if (af_ops->fill_stats_af) {
4101 struct nlattr *af;
4102 int err;
4103
4104 af = nla_nest_start(skb, af_ops->family);
Florian Westphal5fa85a02017-10-16 15:44:36 +02004105 if (!af) {
4106 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004107 goto nla_put_failure;
Florian Westphal5fa85a02017-10-16 15:44:36 +02004108 }
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004109 err = af_ops->fill_stats_af(skb, dev);
4110
Florian Westphal5fa85a02017-10-16 15:44:36 +02004111 if (err == -ENODATA) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004112 nla_nest_cancel(skb, af);
Florian Westphal5fa85a02017-10-16 15:44:36 +02004113 } else if (err < 0) {
4114 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004115 goto nla_put_failure;
Florian Westphal5fa85a02017-10-16 15:44:36 +02004116 }
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004117
4118 nla_nest_end(skb, af);
4119 }
4120 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02004121 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004122
4123 nla_nest_end(skb, attr);
4124
4125 *idxattr = 0;
4126 }
4127
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004128 nlmsg_end(skb, nlh);
4129
4130 return 0;
4131
4132nla_put_failure:
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004133 /* not a multi message or no progress mean a real error */
4134 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
4135 nlmsg_cancel(skb, nlh);
4136 else
4137 nlmsg_end(skb, nlh);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004138
4139 return -EMSGSIZE;
4140}
4141
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004142static size_t if_nlmsg_stats_size(const struct net_device *dev,
4143 u32 filter_mask)
4144{
4145 size_t size = 0;
4146
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004147 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004148 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
4149
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004150 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
4151 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004152 int attr = IFLA_STATS_LINK_XSTATS;
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004153
4154 if (ops && ops->get_linkxstats_size) {
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004155 size += nla_total_size(ops->get_linkxstats_size(dev,
4156 attr));
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004157 /* for IFLA_STATS_LINK_XSTATS */
4158 size += nla_total_size(0);
4159 }
4160 }
4161
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004162 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
4163 struct net_device *_dev = (struct net_device *)dev;
4164 const struct rtnl_link_ops *ops = NULL;
4165 const struct net_device *master;
4166
4167 /* netdev_master_upper_dev_get can't take const */
4168 master = netdev_master_upper_dev_get(_dev);
4169 if (master)
4170 ops = master->rtnl_link_ops;
4171 if (ops && ops->get_linkxstats_size) {
4172 int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
4173
4174 size += nla_total_size(ops->get_linkxstats_size(dev,
4175 attr));
4176 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
4177 size += nla_total_size(0);
4178 }
4179 }
4180
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004181 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
4182 size += rtnl_get_offload_stats_size(dev);
4183
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004184 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
4185 struct rtnl_af_ops *af_ops;
4186
4187 /* for IFLA_STATS_AF_SPEC */
4188 size += nla_total_size(0);
4189
Florian Westphal5fa85a02017-10-16 15:44:36 +02004190 rcu_read_lock();
4191 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004192 if (af_ops->get_stats_af_size) {
4193 size += nla_total_size(
4194 af_ops->get_stats_af_size(dev));
4195
4196 /* for AF_* */
4197 size += nla_total_size(0);
4198 }
4199 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02004200 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004201 }
4202
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004203 return size;
4204}
4205
David Ahernc21ef3e2017-04-16 09:48:24 -07004206static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
4207 struct netlink_ext_ack *extack)
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004208{
4209 struct net *net = sock_net(skb->sk);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004210 struct net_device *dev = NULL;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004211 int idxattr = 0, prividx = 0;
4212 struct if_stats_msg *ifsm;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004213 struct sk_buff *nskb;
4214 u32 filter_mask;
4215 int err;
4216
Mathias Krause4775cc12016-12-28 17:52:15 +01004217 if (nlmsg_len(nlh) < sizeof(*ifsm))
4218 return -EINVAL;
4219
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004220 ifsm = nlmsg_data(nlh);
4221 if (ifsm->ifindex > 0)
4222 dev = __dev_get_by_index(net, ifsm->ifindex);
4223 else
4224 return -EINVAL;
4225
4226 if (!dev)
4227 return -ENODEV;
4228
4229 filter_mask = ifsm->filter_mask;
4230 if (!filter_mask)
4231 return -EINVAL;
4232
4233 nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
4234 if (!nskb)
4235 return -ENOBUFS;
4236
4237 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
4238 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004239 0, filter_mask, &idxattr, &prividx);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004240 if (err < 0) {
4241 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
4242 WARN_ON(err == -EMSGSIZE);
4243 kfree_skb(nskb);
4244 } else {
4245 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
4246 }
4247
4248 return err;
4249}
4250
4251static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
4252{
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004253 int h, s_h, err, s_idx, s_idxattr, s_prividx;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004254 struct net *net = sock_net(skb->sk);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004255 unsigned int flags = NLM_F_MULTI;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004256 struct if_stats_msg *ifsm;
4257 struct hlist_head *head;
4258 struct net_device *dev;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004259 u32 filter_mask = 0;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004260 int idx = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004261
4262 s_h = cb->args[0];
4263 s_idx = cb->args[1];
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004264 s_idxattr = cb->args[2];
4265 s_prividx = cb->args[3];
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004266
4267 cb->seq = net->dev_base_seq;
4268
Mathias Krause4775cc12016-12-28 17:52:15 +01004269 if (nlmsg_len(cb->nlh) < sizeof(*ifsm))
4270 return -EINVAL;
4271
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004272 ifsm = nlmsg_data(cb->nlh);
4273 filter_mask = ifsm->filter_mask;
4274 if (!filter_mask)
4275 return -EINVAL;
4276
4277 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4278 idx = 0;
4279 head = &net->dev_index_head[h];
4280 hlist_for_each_entry(dev, head, index_hlist) {
4281 if (idx < s_idx)
4282 goto cont;
4283 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
4284 NETLINK_CB(cb->skb).portid,
4285 cb->nlh->nlmsg_seq, 0,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004286 flags, filter_mask,
4287 &s_idxattr, &s_prividx);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004288 /* If we ran out of room on the first message,
4289 * we're in trouble
4290 */
4291 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
4292
4293 if (err < 0)
4294 goto out;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004295 s_prividx = 0;
4296 s_idxattr = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004297 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
4298cont:
4299 idx++;
4300 }
4301 }
4302out:
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004303 cb->args[3] = s_prividx;
4304 cb->args[2] = s_idxattr;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004305 cb->args[1] = idx;
4306 cb->args[0] = h;
4307
4308 return skb->len;
4309}
4310
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311/* Process one rtnetlink message. */
4312
Johannes Berg2d4bc932017-04-12 14:34:04 +02004313static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
4314 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004316 struct net *net = sock_net(skb->sk);
Florian Westphal6853dd42017-08-09 20:41:51 +02004317 struct rtnl_link *handlers;
4318 int err = -EOPNOTSUPP;
Thomas Grafe2849862007-03-22 11:48:11 -07004319 rtnl_doit_func doit;
Florian Westphal62256f92017-08-09 20:41:52 +02004320 unsigned int flags;
Alexander Kuleshov617cfc72016-01-10 21:26:57 +06004321 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 int family;
4323 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 if (type > RTM_MAX)
Thomas Graf038890f2007-04-05 14:35:52 -07004327 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328
4329 type -= RTM_BASE;
4330
4331 /* All the messages must have at least 1 byte length */
Hong zhi guo573ce262013-03-27 06:47:04 +00004332 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 return 0;
4334
Hong zhi guo573ce262013-03-27 06:47:04 +00004335 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 kind = type&3;
4337
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07004338 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07004339 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340
Florian Westphal5c2bb9b2017-08-10 16:52:58 +02004341 if (family >= ARRAY_SIZE(rtnl_msg_handlers))
Florian Westphal6853dd42017-08-09 20:41:51 +02004342 family = PF_UNSPEC;
4343
4344 rcu_read_lock();
4345 handlers = rcu_dereference(rtnl_msg_handlers[family]);
4346 if (!handlers) {
4347 family = PF_UNSPEC;
4348 handlers = rcu_dereference(rtnl_msg_handlers[family]);
4349 }
4350
David S. Millerb8f3ab42011-01-18 12:40:38 -08004351 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004352 struct sock *rtnl;
Thomas Grafe2849862007-03-22 11:48:11 -07004353 rtnl_dumpit_func dumpit;
Greg Rosec7ac8672011-06-10 01:27:09 +00004354 u16 min_dump_alloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355
Florian Westphal6853dd42017-08-09 20:41:51 +02004356 dumpit = READ_ONCE(handlers[type].dumpit);
4357 if (!dumpit) {
4358 family = PF_UNSPEC;
4359 handlers = rcu_dereference(rtnl_msg_handlers[PF_UNSPEC]);
4360 if (!handlers)
4361 goto err_unlock;
Florian Westphal0cc09022017-08-09 20:41:50 +02004362
Florian Westphal6853dd42017-08-09 20:41:51 +02004363 dumpit = READ_ONCE(handlers[type].dumpit);
4364 if (!dumpit)
4365 goto err_unlock;
4366 }
Florian Westphale1fa6d22017-08-09 20:41:47 +02004367
Florian Westphal019a3162017-08-09 20:41:49 +02004368 refcount_inc(&rtnl_msg_handlers_ref[family]);
4369
Florian Westphal5c2bb9b2017-08-10 16:52:58 +02004370 if (type == RTM_GETLINK - RTM_BASE)
Florian Westphale1fa6d22017-08-09 20:41:47 +02004371 min_dump_alloc = rtnl_calcit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372
Florian Westphal6853dd42017-08-09 20:41:51 +02004373 rcu_read_unlock();
4374
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004375 rtnl = net->rtnl;
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00004376 {
4377 struct netlink_dump_control c = {
4378 .dump = dumpit,
4379 .min_dump_alloc = min_dump_alloc,
4380 };
4381 err = netlink_dump_start(rtnl, skb, nlh, &c);
4382 }
Florian Westphal019a3162017-08-09 20:41:49 +02004383 refcount_dec(&rtnl_msg_handlers_ref[family]);
Eric Dumazet2907c352011-05-25 07:34:04 +00004384 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 }
4386
Florian Westphal8caa38b2017-08-10 16:53:01 +02004387 doit = READ_ONCE(handlers[type].doit);
4388 if (!doit) {
4389 family = PF_UNSPEC;
4390 handlers = rcu_dereference(rtnl_msg_handlers[family]);
4391 }
4392
Florian Westphal62256f92017-08-09 20:41:52 +02004393 flags = READ_ONCE(handlers[type].flags);
4394 if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
4395 refcount_inc(&rtnl_msg_handlers_ref[family]);
4396 doit = READ_ONCE(handlers[type].doit);
4397 rcu_read_unlock();
4398 if (doit)
4399 err = doit(skb, nlh, extack);
4400 refcount_dec(&rtnl_msg_handlers_ref[family]);
4401 return err;
4402 }
4403
Florian Westphal6853dd42017-08-09 20:41:51 +02004404 rcu_read_unlock();
4405
Florian Westphal0cc09022017-08-09 20:41:50 +02004406 rtnl_lock();
Florian Westphal6853dd42017-08-09 20:41:51 +02004407 handlers = rtnl_dereference(rtnl_msg_handlers[family]);
4408 if (handlers) {
4409 doit = READ_ONCE(handlers[type].doit);
4410 if (doit)
4411 err = doit(skb, nlh, extack);
4412 }
Florian Westphal0cc09022017-08-09 20:41:50 +02004413 rtnl_unlock();
Florian Westphal0cc09022017-08-09 20:41:50 +02004414 return err;
4415
4416err_unlock:
Florian Westphal6853dd42017-08-09 20:41:51 +02004417 rcu_read_unlock();
Florian Westphal0cc09022017-08-09 20:41:50 +02004418 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419}
4420
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07004421static void rtnetlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07004423 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424}
4425
Julien Gomes5f729ea2017-06-20 13:54:16 -07004426static int rtnetlink_bind(struct net *net, int group)
4427{
4428 switch (group) {
4429 case RTNLGRP_IPV4_MROUTE_R:
4430 case RTNLGRP_IPV6_MROUTE_R:
4431 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
4432 return -EPERM;
4433 break;
4434 }
4435 return 0;
4436}
4437
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
4439{
Jiri Pirko351638e2013-05-28 01:30:21 +00004440 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02004441
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 switch (event) {
Vlad Yasevich5138e862017-04-04 09:23:41 -04004443 case NETDEV_REBOOT:
Xin Long8a212582017-10-15 18:13:41 +08004444 case NETDEV_CHANGEMTU:
David Ahern37536542017-07-19 10:22:40 -07004445 case NETDEV_CHANGEADDR:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004446 case NETDEV_CHANGENAME:
4447 case NETDEV_FEAT_CHANGE:
4448 case NETDEV_BONDING_FAILOVER:
Xin Longe6e66592017-10-15 18:13:43 +08004449 case NETDEV_POST_TYPE_CHANGE:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004450 case NETDEV_NOTIFY_PEERS:
Xin Longdc709f32017-10-15 18:13:44 +08004451 case NETDEV_CHANGEUPPER:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004452 case NETDEV_RESEND_IGMP:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004453 case NETDEV_CHANGEINFODATA:
Xin Longeeda3fb2017-10-24 13:54:19 +08004454 case NETDEV_CHANGELOWERSTATE:
Xin Longebdcf042017-10-15 18:13:42 +08004455 case NETDEV_CHANGE_TX_QUEUE_LEN:
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04004456 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02004457 GFP_KERNEL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 break;
4459 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 break;
4461 }
4462 return NOTIFY_DONE;
4463}
4464
4465static struct notifier_block rtnetlink_dev_notifier = {
4466 .notifier_call = rtnetlink_event,
4467};
4468
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004469
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004470static int __net_init rtnetlink_net_init(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004471{
4472 struct sock *sk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00004473 struct netlink_kernel_cfg cfg = {
4474 .groups = RTNLGRP_MAX,
4475 .input = rtnetlink_rcv,
4476 .cb_mutex = &rtnl_mutex,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00004477 .flags = NL_CFG_F_NONROOT_RECV,
Julien Gomes5f729ea2017-06-20 13:54:16 -07004478 .bind = rtnetlink_bind,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00004479 };
4480
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00004481 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004482 if (!sk)
4483 return -ENOMEM;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004484 net->rtnl = sk;
4485 return 0;
4486}
4487
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004488static void __net_exit rtnetlink_net_exit(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004489{
Denis V. Lunev775516b2008-01-18 23:55:19 -08004490 netlink_kernel_release(net->rtnl);
4491 net->rtnl = NULL;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004492}
4493
4494static struct pernet_operations rtnetlink_net_ops = {
4495 .init = rtnetlink_net_init,
4496 .exit = rtnetlink_net_exit,
4497};
4498
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499void __init rtnetlink_init(void)
4500{
Florian Westphald38a6512017-08-10 16:53:00 +02004501 int i;
4502
4503 for (i = 0; i < ARRAY_SIZE(rtnl_msg_handlers_ref); i++)
4504 refcount_set(&rtnl_msg_handlers_ref[i], 1);
4505
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004506 if (register_pernet_subsys(&rtnetlink_net_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 panic("rtnetlink_init: cannot initialize rtnetlink\n");
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004508
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 register_netdevice_notifier(&rtnetlink_dev_notifier);
Thomas Graf340d17f2007-03-22 11:49:22 -07004510
Greg Rosec7ac8672011-06-10 01:27:09 +00004511 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
Florian Westphalb97bac62017-08-09 20:41:48 +02004512 rtnl_dump_ifinfo, 0);
4513 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
4514 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
4515 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
Thomas Graf687ad8c2007-03-22 11:59:42 -07004516
Florian Westphalb97bac62017-08-09 20:41:48 +02004517 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
4518 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
4519 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
John Fastabend77162022012-04-15 06:43:56 +00004520
Florian Westphalb97bac62017-08-09 20:41:48 +02004521 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
4522 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
4523 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, 0);
John Fastabende5a55a82012-10-24 08:12:57 +00004524
Florian Westphalb97bac62017-08-09 20:41:48 +02004525 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
4526 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
4527 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004528
4529 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
Florian Westphalb97bac62017-08-09 20:41:48 +02004530 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531}