blob: 5ace48926b196666265a7f95b77779cbdd1ff848 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * Routing netlink socket interface: protocol independent part.
7 *
8 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
14 *
15 * Fixes:
16 * Vitaly E. Lavrov RTA_OK arithmetics was wrong.
17 */
18
Jakub Kicinskiee5d0322017-06-21 18:25:04 -070019#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/errno.h>
21#include <linux/module.h>
22#include <linux/types.h>
23#include <linux/socket.h>
24#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/timer.h>
26#include <linux/string.h>
27#include <linux/sockios.h>
28#include <linux/net.h>
29#include <linux/fcntl.h>
30#include <linux/mm.h>
31#include <linux/slab.h>
32#include <linux/interrupt.h>
33#include <linux/capability.h>
34#include <linux/skbuff.h>
35#include <linux/init.h>
36#include <linux/security.h>
Stephen Hemminger6756ae42006-03-20 22:23:58 -080037#include <linux/mutex.h>
Thomas Graf18237302006-08-04 23:04:54 -070038#include <linux/if_addr.h>
John Fastabend77162022012-04-15 06:43:56 +000039#include <linux/if_bridge.h>
Jiri Pirkof6f64242014-11-28 14:34:15 +010040#include <linux/if_vlan.h>
Williams, Mitch Aebc08a62010-02-10 01:44:05 +000041#include <linux/pci.h>
John Fastabend77162022012-04-15 06:43:56 +000042#include <linux/etherdevice.h>
Martin KaFai Lau58038692017-06-15 17:29:09 -070043#include <linux/bpf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080045#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#include <linux/inet.h>
48#include <linux/netdevice.h>
Jiri Pirko82f28412014-11-28 14:34:18 +010049#include <net/switchdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <net/ip.h>
51#include <net/protocol.h>
52#include <net/arp.h>
53#include <net/route.h>
54#include <net/udp.h>
Daniel Borkmannea697632015-01-05 23:57:47 +010055#include <net/tcp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <net/sock.h>
57#include <net/pkt_sched.h>
Thomas Graf14c0b972006-08-04 03:38:38 -070058#include <net/fib_rules.h>
Thomas Grafe2849862007-03-22 11:48:11 -070059#include <net/rtnetlink.h>
Johannes Berg30ffee82009-07-10 09:51:35 +000060#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Eric Dumazete0d087a2009-11-07 01:26:17 -080062struct rtnl_link {
Thomas Grafe2849862007-03-22 11:48:11 -070063 rtnl_doit_func doit;
64 rtnl_dumpit_func dumpit;
Florian Westphal62256f92017-08-09 20:41:52 +020065 unsigned int flags;
Thomas Grafe2849862007-03-22 11:48:11 -070066};
67
Stephen Hemminger6756ae42006-03-20 22:23:58 -080068static DEFINE_MUTEX(rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70void rtnl_lock(void)
71{
Stephen Hemminger6756ae42006-03-20 22:23:58 -080072 mutex_lock(&rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
Eric Dumazete0d087a2009-11-07 01:26:17 -080074EXPORT_SYMBOL(rtnl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Eric Dumazet1b5c5492016-06-13 20:21:50 -070076static struct sk_buff *defer_kfree_skb_list;
77void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail)
78{
79 if (head && tail) {
80 tail->next = defer_kfree_skb_list;
81 defer_kfree_skb_list = head;
82 }
83}
84EXPORT_SYMBOL(rtnl_kfree_skbs);
85
Stephen Hemminger6756ae42006-03-20 22:23:58 -080086void __rtnl_unlock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Eric Dumazet1b5c5492016-06-13 20:21:50 -070088 struct sk_buff *head = defer_kfree_skb_list;
89
90 defer_kfree_skb_list = NULL;
91
Stephen Hemminger6756ae42006-03-20 22:23:58 -080092 mutex_unlock(&rtnl_mutex);
Eric Dumazet1b5c5492016-06-13 20:21:50 -070093
94 while (head) {
95 struct sk_buff *next = head->next;
96
97 kfree_skb(head);
98 cond_resched();
99 head = next;
100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103void rtnl_unlock(void)
104{
Herbert Xu58ec3b42008-10-07 15:50:03 -0700105 /* This fellow will unlock it for us. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 netdev_run_todo();
107}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800108EXPORT_SYMBOL(rtnl_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800110int rtnl_trylock(void)
111{
112 return mutex_trylock(&rtnl_mutex);
113}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800114EXPORT_SYMBOL(rtnl_trylock);
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800115
Patrick McHardyc9c10142008-04-23 22:10:48 -0700116int rtnl_is_locked(void)
117{
118 return mutex_is_locked(&rtnl_mutex);
119}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800120EXPORT_SYMBOL(rtnl_is_locked);
Patrick McHardyc9c10142008-04-23 22:10:48 -0700121
Paul E. McKenneya898def2010-02-22 17:04:49 -0800122#ifdef CONFIG_PROVE_LOCKING
Yaowei Bai0cbf3342015-10-08 21:29:02 +0800123bool lockdep_rtnl_is_held(void)
Paul E. McKenneya898def2010-02-22 17:04:49 -0800124{
125 return lockdep_is_held(&rtnl_mutex);
126}
127EXPORT_SYMBOL(lockdep_rtnl_is_held);
128#endif /* #ifdef CONFIG_PROVE_LOCKING */
129
Florian Westphal6853dd42017-08-09 20:41:51 +0200130static struct rtnl_link __rcu *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
Florian Westphal019a3162017-08-09 20:41:49 +0200131static refcount_t rtnl_msg_handlers_ref[RTNL_FAMILY_MAX + 1];
Thomas Grafe2849862007-03-22 11:48:11 -0700132
133static inline int rtm_msgindex(int msgtype)
134{
135 int msgindex = msgtype - RTM_BASE;
136
137 /*
138 * msgindex < 0 implies someone tried to register a netlink
139 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
140 * the message type has not been added to linux/rtnetlink.h
141 */
142 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
143
144 return msgindex;
145}
146
Thomas Grafe2849862007-03-22 11:48:11 -0700147/**
148 * __rtnl_register - Register a rtnetlink message type
149 * @protocol: Protocol family or PF_UNSPEC
150 * @msgtype: rtnetlink message type
151 * @doit: Function pointer called for each request message
152 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
Florian Westphalb97bac62017-08-09 20:41:48 +0200153 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
Thomas Grafe2849862007-03-22 11:48:11 -0700154 *
155 * Registers the specified function pointers (at least one of them has
156 * to be non-NULL) to be called whenever a request message for the
157 * specified protocol family and message type is received.
158 *
159 * The special protocol family PF_UNSPEC may be used to define fallback
160 * function pointers for the case when no entry for the specific protocol
161 * family exists.
162 *
163 * Returns 0 on success or a negative error code.
164 */
165int __rtnl_register(int protocol, int msgtype,
Greg Rosec7ac8672011-06-10 01:27:09 +0000166 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
Florian Westphalb97bac62017-08-09 20:41:48 +0200167 unsigned int flags)
Thomas Grafe2849862007-03-22 11:48:11 -0700168{
169 struct rtnl_link *tab;
170 int msgindex;
171
Patrick McHardy25239ce2010-04-26 16:02:05 +0200172 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700173 msgindex = rtm_msgindex(msgtype);
174
Florian Westphal377cb242017-08-10 16:52:57 +0200175 tab = rcu_dereference_raw(rtnl_msg_handlers[protocol]);
Thomas Grafe2849862007-03-22 11:48:11 -0700176 if (tab == NULL) {
177 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
178 if (tab == NULL)
179 return -ENOBUFS;
180
Florian Westphal6853dd42017-08-09 20:41:51 +0200181 rcu_assign_pointer(rtnl_msg_handlers[protocol], tab);
Thomas Grafe2849862007-03-22 11:48:11 -0700182 }
183
184 if (doit)
185 tab[msgindex].doit = doit;
Thomas Grafe2849862007-03-22 11:48:11 -0700186 if (dumpit)
187 tab[msgindex].dumpit = dumpit;
Florian Westphal62256f92017-08-09 20:41:52 +0200188 tab[msgindex].flags |= flags;
Thomas Grafe2849862007-03-22 11:48:11 -0700189
190 return 0;
191}
Thomas Grafe2849862007-03-22 11:48:11 -0700192EXPORT_SYMBOL_GPL(__rtnl_register);
193
194/**
195 * rtnl_register - Register a rtnetlink message type
196 *
197 * Identical to __rtnl_register() but panics on failure. This is useful
198 * as failure of this function is very unlikely, it can only happen due
199 * to lack of memory when allocating the chain to store all message
200 * handlers for a protocol. Meant for use in init functions where lack
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300201 * of memory implies no sense in continuing.
Thomas Grafe2849862007-03-22 11:48:11 -0700202 */
203void rtnl_register(int protocol, int msgtype,
Greg Rosec7ac8672011-06-10 01:27:09 +0000204 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
Florian Westphalb97bac62017-08-09 20:41:48 +0200205 unsigned int flags)
Thomas Grafe2849862007-03-22 11:48:11 -0700206{
Florian Westphalb97bac62017-08-09 20:41:48 +0200207 if (__rtnl_register(protocol, msgtype, doit, dumpit, flags) < 0)
Thomas Grafe2849862007-03-22 11:48:11 -0700208 panic("Unable to register rtnetlink message handler, "
209 "protocol = %d, message type = %d\n",
210 protocol, msgtype);
211}
Thomas Grafe2849862007-03-22 11:48:11 -0700212EXPORT_SYMBOL_GPL(rtnl_register);
213
214/**
215 * rtnl_unregister - Unregister a rtnetlink message type
216 * @protocol: Protocol family or PF_UNSPEC
217 * @msgtype: rtnetlink message type
218 *
219 * Returns 0 on success or a negative error code.
220 */
221int rtnl_unregister(int protocol, int msgtype)
222{
Florian Westphal6853dd42017-08-09 20:41:51 +0200223 struct rtnl_link *handlers;
Thomas Grafe2849862007-03-22 11:48:11 -0700224 int msgindex;
225
Patrick McHardy25239ce2010-04-26 16:02:05 +0200226 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700227 msgindex = rtm_msgindex(msgtype);
228
Florian Westphal6853dd42017-08-09 20:41:51 +0200229 rtnl_lock();
230 handlers = rtnl_dereference(rtnl_msg_handlers[protocol]);
231 if (!handlers) {
232 rtnl_unlock();
Thomas Grafe2849862007-03-22 11:48:11 -0700233 return -ENOENT;
Florian Westphal6853dd42017-08-09 20:41:51 +0200234 }
Thomas Grafe2849862007-03-22 11:48:11 -0700235
Florian Westphal6853dd42017-08-09 20:41:51 +0200236 handlers[msgindex].doit = NULL;
237 handlers[msgindex].dumpit = NULL;
Florian Westphal62256f92017-08-09 20:41:52 +0200238 handlers[msgindex].flags = 0;
Florian Westphal6853dd42017-08-09 20:41:51 +0200239 rtnl_unlock();
Thomas Grafe2849862007-03-22 11:48:11 -0700240
241 return 0;
242}
Thomas Grafe2849862007-03-22 11:48:11 -0700243EXPORT_SYMBOL_GPL(rtnl_unregister);
244
245/**
246 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
247 * @protocol : Protocol family or PF_UNSPEC
248 *
249 * Identical to calling rtnl_unregster() for all registered message types
250 * of a certain protocol family.
251 */
252void rtnl_unregister_all(int protocol)
253{
Florian Westphal019a3162017-08-09 20:41:49 +0200254 struct rtnl_link *handlers;
255
Patrick McHardy25239ce2010-04-26 16:02:05 +0200256 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700257
Florian Westphal019a3162017-08-09 20:41:49 +0200258 rtnl_lock();
Florian Westphal6853dd42017-08-09 20:41:51 +0200259 handlers = rtnl_dereference(rtnl_msg_handlers[protocol]);
260 RCU_INIT_POINTER(rtnl_msg_handlers[protocol], NULL);
Florian Westphal019a3162017-08-09 20:41:49 +0200261 rtnl_unlock();
262
Florian Westphal6853dd42017-08-09 20:41:51 +0200263 synchronize_net();
264
Florian Westphald38a6512017-08-10 16:53:00 +0200265 while (refcount_read(&rtnl_msg_handlers_ref[protocol]) > 1)
Florian Westphal019a3162017-08-09 20:41:49 +0200266 schedule();
267 kfree(handlers);
Thomas Grafe2849862007-03-22 11:48:11 -0700268}
Thomas Grafe2849862007-03-22 11:48:11 -0700269EXPORT_SYMBOL_GPL(rtnl_unregister_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Patrick McHardy38f7b872007-06-13 12:03:51 -0700271static LIST_HEAD(link_ops);
272
Eric Dumazetc63044f2011-12-13 11:38:00 +0000273static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
274{
275 const struct rtnl_link_ops *ops;
276
277 list_for_each_entry(ops, &link_ops, list) {
278 if (!strcmp(ops->kind, kind))
279 return ops;
280 }
281 return NULL;
282}
283
Patrick McHardy38f7b872007-06-13 12:03:51 -0700284/**
285 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
286 * @ops: struct rtnl_link_ops * to register
287 *
288 * The caller must hold the rtnl_mutex. This function should be used
289 * by drivers that create devices during module initialization. It
290 * must be called before registering the devices.
291 *
292 * Returns 0 on success or a negative error code.
293 */
294int __rtnl_link_register(struct rtnl_link_ops *ops)
295{
Eric Dumazetc63044f2011-12-13 11:38:00 +0000296 if (rtnl_link_ops_get(ops->kind))
297 return -EEXIST;
298
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +0200299 /* The check for setup is here because if ops
300 * does not have that filled up, it is not possible
301 * to use the ops for creating device. So do not
302 * fill up dellink as well. That disables rtnl_dellink.
303 */
304 if (ops->setup && !ops->dellink)
Eric Dumazet23289a32009-10-27 07:06:36 +0000305 ops->dellink = unregister_netdevice_queue;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700306
Patrick McHardy38f7b872007-06-13 12:03:51 -0700307 list_add_tail(&ops->list, &link_ops);
308 return 0;
309}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700310EXPORT_SYMBOL_GPL(__rtnl_link_register);
311
312/**
313 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
314 * @ops: struct rtnl_link_ops * to register
315 *
316 * Returns 0 on success or a negative error code.
317 */
318int rtnl_link_register(struct rtnl_link_ops *ops)
319{
320 int err;
321
322 rtnl_lock();
323 err = __rtnl_link_register(ops);
324 rtnl_unlock();
325 return err;
326}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700327EXPORT_SYMBOL_GPL(rtnl_link_register);
328
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700329static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
330{
331 struct net_device *dev;
Eric Dumazet23289a32009-10-27 07:06:36 +0000332 LIST_HEAD(list_kill);
333
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700334 for_each_netdev(net, dev) {
Eric Dumazet23289a32009-10-27 07:06:36 +0000335 if (dev->rtnl_link_ops == ops)
336 ops->dellink(dev, &list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700337 }
Eric Dumazet23289a32009-10-27 07:06:36 +0000338 unregister_netdevice_many(&list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700339}
340
Patrick McHardy38f7b872007-06-13 12:03:51 -0700341/**
342 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
343 * @ops: struct rtnl_link_ops * to unregister
344 *
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700345 * The caller must hold the rtnl_mutex.
Patrick McHardy38f7b872007-06-13 12:03:51 -0700346 */
347void __rtnl_link_unregister(struct rtnl_link_ops *ops)
348{
Eric W. Biederman881d9662007-09-17 11:56:21 -0700349 struct net *net;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700350
Eric W. Biederman881d9662007-09-17 11:56:21 -0700351 for_each_net(net) {
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700352 __rtnl_kill_links(net, ops);
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700353 }
Patrick McHardy38f7b872007-06-13 12:03:51 -0700354 list_del(&ops->list);
355}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700356EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
357
Cong Wang200b9162014-05-12 15:11:20 -0700358/* Return with the rtnl_lock held when there are no network
359 * devices unregistering in any network namespace.
360 */
361static void rtnl_lock_unregistering_all(void)
362{
363 struct net *net;
364 bool unregistering;
Peter Zijlstraff960a72014-10-29 17:04:56 +0100365 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Cong Wang200b9162014-05-12 15:11:20 -0700366
Peter Zijlstraff960a72014-10-29 17:04:56 +0100367 add_wait_queue(&netdev_unregistering_wq, &wait);
Cong Wang200b9162014-05-12 15:11:20 -0700368 for (;;) {
Cong Wang200b9162014-05-12 15:11:20 -0700369 unregistering = false;
370 rtnl_lock();
371 for_each_net(net) {
372 if (net->dev_unreg_count > 0) {
373 unregistering = true;
374 break;
375 }
376 }
377 if (!unregistering)
378 break;
379 __rtnl_unlock();
Peter Zijlstraff960a72014-10-29 17:04:56 +0100380
381 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Cong Wang200b9162014-05-12 15:11:20 -0700382 }
Peter Zijlstraff960a72014-10-29 17:04:56 +0100383 remove_wait_queue(&netdev_unregistering_wq, &wait);
Cong Wang200b9162014-05-12 15:11:20 -0700384}
385
Patrick McHardy38f7b872007-06-13 12:03:51 -0700386/**
387 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
388 * @ops: struct rtnl_link_ops * to unregister
389 */
390void rtnl_link_unregister(struct rtnl_link_ops *ops)
391{
Cong Wang200b9162014-05-12 15:11:20 -0700392 /* Close the race with cleanup_net() */
393 mutex_lock(&net_mutex);
394 rtnl_lock_unregistering_all();
Patrick McHardy38f7b872007-06-13 12:03:51 -0700395 __rtnl_link_unregister(ops);
396 rtnl_unlock();
Cong Wang200b9162014-05-12 15:11:20 -0700397 mutex_unlock(&net_mutex);
Patrick McHardy38f7b872007-06-13 12:03:51 -0700398}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700399EXPORT_SYMBOL_GPL(rtnl_link_unregister);
400
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100401static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
402{
403 struct net_device *master_dev;
404 const struct rtnl_link_ops *ops;
Florian Westphal8515ae32017-08-10 16:52:59 +0200405 size_t size = 0;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100406
Florian Westphal8515ae32017-08-10 16:52:59 +0200407 rcu_read_lock();
408
409 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100410 if (!master_dev)
Florian Westphal8515ae32017-08-10 16:52:59 +0200411 goto out;
412
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100413 ops = master_dev->rtnl_link_ops;
Fernando Luis Vazquez Cao6049f252014-02-04 19:35:02 +0900414 if (!ops || !ops->get_slave_size)
Florian Westphal8515ae32017-08-10 16:52:59 +0200415 goto out;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100416 /* IFLA_INFO_SLAVE_DATA + nested data */
Florian Westphal8515ae32017-08-10 16:52:59 +0200417 size = nla_total_size(sizeof(struct nlattr)) +
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100418 ops->get_slave_size(master_dev, dev);
Florian Westphal8515ae32017-08-10 16:52:59 +0200419
420out:
421 rcu_read_unlock();
422 return size;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100423}
424
Patrick McHardy38f7b872007-06-13 12:03:51 -0700425static size_t rtnl_link_get_size(const struct net_device *dev)
426{
427 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
428 size_t size;
429
430 if (!ops)
431 return 0;
432
Thomas Graf369cf772010-11-11 15:47:59 +0000433 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
434 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700435
436 if (ops->get_size)
437 /* IFLA_INFO_DATA + nested data */
Thomas Graf369cf772010-11-11 15:47:59 +0000438 size += nla_total_size(sizeof(struct nlattr)) +
Patrick McHardy38f7b872007-06-13 12:03:51 -0700439 ops->get_size(dev);
440
441 if (ops->get_xstats_size)
Thomas Graf369cf772010-11-11 15:47:59 +0000442 /* IFLA_INFO_XSTATS */
443 size += nla_total_size(ops->get_xstats_size(dev));
Patrick McHardy38f7b872007-06-13 12:03:51 -0700444
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100445 size += rtnl_link_get_slave_info_data_size(dev);
446
Patrick McHardy38f7b872007-06-13 12:03:51 -0700447 return size;
448}
449
Thomas Graff8ff1822010-11-16 04:30:14 +0000450static LIST_HEAD(rtnl_af_ops);
451
452static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
453{
454 const struct rtnl_af_ops *ops;
455
456 list_for_each_entry(ops, &rtnl_af_ops, list) {
457 if (ops->family == family)
458 return ops;
459 }
460
461 return NULL;
462}
463
464/**
Thomas Graff8ff1822010-11-16 04:30:14 +0000465 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
466 * @ops: struct rtnl_af_ops * to register
467 *
468 * Returns 0 on success or a negative error code.
469 */
stephen hemminger3678a9d2013-12-30 10:41:32 -0800470void rtnl_af_register(struct rtnl_af_ops *ops)
Thomas Graff8ff1822010-11-16 04:30:14 +0000471{
Thomas Graff8ff1822010-11-16 04:30:14 +0000472 rtnl_lock();
stephen hemminger3678a9d2013-12-30 10:41:32 -0800473 list_add_tail(&ops->list, &rtnl_af_ops);
Thomas Graff8ff1822010-11-16 04:30:14 +0000474 rtnl_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +0000475}
476EXPORT_SYMBOL_GPL(rtnl_af_register);
477
478/**
479 * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
480 * @ops: struct rtnl_af_ops * to unregister
481 *
482 * The caller must hold the rtnl_mutex.
483 */
484void __rtnl_af_unregister(struct rtnl_af_ops *ops)
485{
486 list_del(&ops->list);
487}
488EXPORT_SYMBOL_GPL(__rtnl_af_unregister);
489
490/**
491 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
492 * @ops: struct rtnl_af_ops * to unregister
493 */
494void rtnl_af_unregister(struct rtnl_af_ops *ops)
495{
496 rtnl_lock();
497 __rtnl_af_unregister(ops);
498 rtnl_unlock();
499}
500EXPORT_SYMBOL_GPL(rtnl_af_unregister);
501
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700502static size_t rtnl_link_get_af_size(const struct net_device *dev,
503 u32 ext_filter_mask)
Thomas Graff8ff1822010-11-16 04:30:14 +0000504{
505 struct rtnl_af_ops *af_ops;
506 size_t size;
507
508 /* IFLA_AF_SPEC */
509 size = nla_total_size(sizeof(struct nlattr));
510
511 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
512 if (af_ops->get_link_af_size) {
513 /* AF_* + nested data */
514 size += nla_total_size(sizeof(struct nlattr)) +
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700515 af_ops->get_link_af_size(dev, ext_filter_mask);
Thomas Graff8ff1822010-11-16 04:30:14 +0000516 }
517 }
518
519 return size;
520}
521
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100522static bool rtnl_have_link_slave_info(const struct net_device *dev)
523{
524 struct net_device *master_dev;
525
526 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
Jiri Pirko813f0202014-01-23 19:19:21 +0100527 if (master_dev && master_dev->rtnl_link_ops)
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100528 return true;
529 return false;
530}
531
532static int rtnl_link_slave_info_fill(struct sk_buff *skb,
533 const struct net_device *dev)
534{
535 struct net_device *master_dev;
536 const struct rtnl_link_ops *ops;
537 struct nlattr *slave_data;
538 int err;
539
540 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
541 if (!master_dev)
542 return 0;
543 ops = master_dev->rtnl_link_ops;
544 if (!ops)
545 return 0;
546 if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
547 return -EMSGSIZE;
548 if (ops->fill_slave_info) {
549 slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA);
550 if (!slave_data)
551 return -EMSGSIZE;
552 err = ops->fill_slave_info(skb, master_dev, dev);
553 if (err < 0)
554 goto err_cancel_slave_data;
555 nla_nest_end(skb, slave_data);
556 }
557 return 0;
558
559err_cancel_slave_data:
560 nla_nest_cancel(skb, slave_data);
561 return err;
562}
563
564static int rtnl_link_info_fill(struct sk_buff *skb,
565 const struct net_device *dev)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700566{
567 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100568 struct nlattr *data;
569 int err;
570
571 if (!ops)
572 return 0;
573 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
574 return -EMSGSIZE;
575 if (ops->fill_xstats) {
576 err = ops->fill_xstats(skb, dev);
577 if (err < 0)
578 return err;
579 }
580 if (ops->fill_info) {
581 data = nla_nest_start(skb, IFLA_INFO_DATA);
582 if (data == NULL)
583 return -EMSGSIZE;
584 err = ops->fill_info(skb, dev);
585 if (err < 0)
586 goto err_cancel_data;
587 nla_nest_end(skb, data);
588 }
589 return 0;
590
591err_cancel_data:
592 nla_nest_cancel(skb, data);
593 return err;
594}
595
596static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
597{
598 struct nlattr *linkinfo;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700599 int err = -EMSGSIZE;
600
601 linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
602 if (linkinfo == NULL)
603 goto out;
604
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100605 err = rtnl_link_info_fill(skb, dev);
606 if (err < 0)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700607 goto err_cancel_link;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100608
609 err = rtnl_link_slave_info_fill(skb, dev);
610 if (err < 0)
611 goto err_cancel_link;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700612
613 nla_nest_end(skb, linkinfo);
614 return 0;
615
Patrick McHardy38f7b872007-06-13 12:03:51 -0700616err_cancel_link:
617 nla_nest_cancel(skb, linkinfo);
618out:
619 return err;
620}
621
Eric Dumazet95c96172012-04-15 05:58:06 +0000622int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800624 struct sock *rtnl = net->rtnl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 int err = 0;
626
Patrick McHardyac6d4392005-08-14 19:29:52 -0700627 NETLINK_CB(skb).dst_group = group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if (echo)
Reshetova, Elena63354792017-06-30 13:07:58 +0300629 refcount_inc(&skb->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
631 if (echo)
632 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
633 return err;
634}
635
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800636int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
Thomas Graf2942e902006-08-15 00:30:25 -0700637{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800638 struct sock *rtnl = net->rtnl;
639
Thomas Graf2942e902006-08-15 00:30:25 -0700640 return nlmsg_unicast(rtnl, skb, pid);
641}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800642EXPORT_SYMBOL(rtnl_unicast);
Thomas Graf2942e902006-08-15 00:30:25 -0700643
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800644void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
645 struct nlmsghdr *nlh, gfp_t flags)
Thomas Graf97676b62006-08-15 00:31:41 -0700646{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800647 struct sock *rtnl = net->rtnl;
Thomas Graf97676b62006-08-15 00:31:41 -0700648 int report = 0;
649
650 if (nlh)
651 report = nlmsg_report(nlh);
652
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800653 nlmsg_notify(rtnl, skb, pid, group, report, flags);
Thomas Graf97676b62006-08-15 00:31:41 -0700654}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800655EXPORT_SYMBOL(rtnl_notify);
Thomas Graf97676b62006-08-15 00:31:41 -0700656
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800657void rtnl_set_sk_err(struct net *net, u32 group, int error)
Thomas Graf97676b62006-08-15 00:31:41 -0700658{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800659 struct sock *rtnl = net->rtnl;
660
Thomas Graf97676b62006-08-15 00:31:41 -0700661 netlink_set_err(rtnl, 0, group, error);
662}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800663EXPORT_SYMBOL(rtnl_set_sk_err);
Thomas Graf97676b62006-08-15 00:31:41 -0700664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
666{
Thomas Graf2d7202b2006-08-22 00:01:27 -0700667 struct nlattr *mx;
668 int i, valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Thomas Graf2d7202b2006-08-22 00:01:27 -0700670 mx = nla_nest_start(skb, RTA_METRICS);
671 if (mx == NULL)
672 return -ENOBUFS;
673
674 for (i = 0; i < RTAX_MAX; i++) {
675 if (metrics[i]) {
Daniel Borkmannea697632015-01-05 23:57:47 +0100676 if (i == RTAX_CC_ALGO - 1) {
677 char tmp[TCP_CA_NAME_MAX], *name;
678
679 name = tcp_ca_get_name_by_key(metrics[i], tmp);
680 if (!name)
681 continue;
682 if (nla_put_string(skb, i + 1, name))
683 goto nla_put_failure;
Daniel Borkmannc3a8d942015-08-31 15:58:47 +0200684 } else if (i == RTAX_FEATURES - 1) {
685 u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
686
Phil Sutterf8edcd12016-08-23 13:14:31 +0200687 if (!user_features)
688 continue;
Daniel Borkmannc3a8d942015-08-31 15:58:47 +0200689 BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
690 if (nla_put_u32(skb, i + 1, user_features))
691 goto nla_put_failure;
Daniel Borkmannea697632015-01-05 23:57:47 +0100692 } else {
693 if (nla_put_u32(skb, i + 1, metrics[i]))
694 goto nla_put_failure;
695 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700696 valid++;
Thomas Graf2d7202b2006-08-22 00:01:27 -0700697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
David S. Millera57d27f2006-08-22 22:20:14 -0700700 if (!valid) {
701 nla_nest_cancel(skb, mx);
702 return 0;
703 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700704
705 return nla_nest_end(skb, mx);
706
707nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700708 nla_nest_cancel(skb, mx);
709 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800711EXPORT_SYMBOL(rtnetlink_put_metrics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Thomas Grafe3703b32006-11-27 09:27:07 -0800713int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
David S. Miller87a50692012-07-10 05:06:14 -0700714 long expires, u32 error)
Thomas Grafe3703b32006-11-27 09:27:07 -0800715{
716 struct rta_cacheinfo ci = {
Eric Dumazeta399a802012-08-08 21:13:53 +0000717 .rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse),
Thomas Grafe3703b32006-11-27 09:27:07 -0800718 .rta_used = dst->__use,
719 .rta_clntref = atomic_read(&(dst->__refcnt)),
720 .rta_error = error,
721 .rta_id = id,
Thomas Grafe3703b32006-11-27 09:27:07 -0800722 };
723
Li Wei82539472012-07-29 16:01:30 +0000724 if (expires) {
725 unsigned long clock;
Thomas Grafe3703b32006-11-27 09:27:07 -0800726
Li Wei82539472012-07-29 16:01:30 +0000727 clock = jiffies_to_clock_t(abs(expires));
728 clock = min_t(unsigned long, clock, INT_MAX);
729 ci.rta_expires = (expires > 0) ? clock : -clock;
730 }
Thomas Grafe3703b32006-11-27 09:27:07 -0800731 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
732}
Thomas Grafe3703b32006-11-27 09:27:07 -0800733EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
David S. Miller93b2d4a2008-02-17 18:35:07 -0800735static void set_operstate(struct net_device *dev, unsigned char transition)
Stefan Rompfb00055a2006-03-20 17:09:11 -0800736{
737 unsigned char operstate = dev->operstate;
738
Eric Dumazete0d087a2009-11-07 01:26:17 -0800739 switch (transition) {
Stefan Rompfb00055a2006-03-20 17:09:11 -0800740 case IF_OPER_UP:
741 if ((operstate == IF_OPER_DORMANT ||
742 operstate == IF_OPER_UNKNOWN) &&
743 !netif_dormant(dev))
744 operstate = IF_OPER_UP;
745 break;
746
747 case IF_OPER_DORMANT:
748 if (operstate == IF_OPER_UP ||
749 operstate == IF_OPER_UNKNOWN)
750 operstate = IF_OPER_DORMANT;
751 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700752 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800753
754 if (dev->operstate != operstate) {
755 write_lock_bh(&dev_base_lock);
756 dev->operstate = operstate;
757 write_unlock_bh(&dev_base_lock);
David S. Miller93b2d4a2008-02-17 18:35:07 -0800758 netdev_state_change(dev);
759 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800760}
761
Jiri Bencb1beb682012-07-27 02:58:22 +0000762static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
763{
764 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
765 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
766}
767
Patrick McHardy3729d502010-02-26 06:34:54 +0000768static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
769 const struct ifinfomsg *ifm)
770{
771 unsigned int flags = ifm->ifi_flags;
772
773 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
774 if (ifm->ifi_change)
775 flags = (flags & ifm->ifi_change) |
Jiri Bencb1beb682012-07-27 02:58:22 +0000776 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
Patrick McHardy3729d502010-02-26 06:34:54 +0000777
778 return flags;
779}
780
Thomas Grafb60c5112006-08-04 23:05:34 -0700781static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000782 const struct rtnl_link_stats64 *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
Thomas Grafb60c5112006-08-04 23:05:34 -0700784 a->rx_packets = b->rx_packets;
785 a->tx_packets = b->tx_packets;
786 a->rx_bytes = b->rx_bytes;
787 a->tx_bytes = b->tx_bytes;
788 a->rx_errors = b->rx_errors;
789 a->tx_errors = b->tx_errors;
790 a->rx_dropped = b->rx_dropped;
791 a->tx_dropped = b->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Thomas Grafb60c5112006-08-04 23:05:34 -0700793 a->multicast = b->multicast;
794 a->collisions = b->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Thomas Grafb60c5112006-08-04 23:05:34 -0700796 a->rx_length_errors = b->rx_length_errors;
797 a->rx_over_errors = b->rx_over_errors;
798 a->rx_crc_errors = b->rx_crc_errors;
799 a->rx_frame_errors = b->rx_frame_errors;
800 a->rx_fifo_errors = b->rx_fifo_errors;
801 a->rx_missed_errors = b->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Thomas Grafb60c5112006-08-04 23:05:34 -0700803 a->tx_aborted_errors = b->tx_aborted_errors;
804 a->tx_carrier_errors = b->tx_carrier_errors;
805 a->tx_fifo_errors = b->tx_fifo_errors;
806 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
807 a->tx_window_errors = b->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Thomas Grafb60c5112006-08-04 23:05:34 -0700809 a->rx_compressed = b->rx_compressed;
810 a->tx_compressed = b->tx_compressed;
Jarod Wilson6e7333d2016-02-01 18:51:05 -0500811
812 a->rx_nohandler = b->rx_nohandler;
Jan Engelhardt10708f32010-03-11 09:57:29 +0000813}
814
Chris Wrightc02db8c2010-05-16 01:05:45 -0700815/* All VF info */
Greg Rose115c9b82012-02-21 16:54:48 -0500816static inline int rtnl_vfinfo_size(const struct net_device *dev,
817 u32 ext_filter_mask)
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000818{
Phil Sutter9af15c32017-01-18 14:04:39 +0100819 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
Chris Wrightc02db8c2010-05-16 01:05:45 -0700820 int num_vfs = dev_num_vf(dev->dev.parent);
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100821 size_t size = nla_total_size(0);
Scott Feldman045de012010-05-28 03:42:43 -0700822 size += num_vfs *
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100823 (nla_total_size(0) +
824 nla_total_size(sizeof(struct ifla_vf_mac)) +
825 nla_total_size(sizeof(struct ifla_vf_vlan)) +
826 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
Moshe Shemesh79aab092016-09-22 12:11:15 +0300827 nla_total_size(MAX_VLAN_LIST_LEN *
828 sizeof(struct ifla_vf_vlan_info)) +
Sucheta Chakrabortyed616682014-05-22 09:59:05 -0400829 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100830 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
Jiri Benc945a3672014-08-08 16:44:32 +0200831 nla_total_size(sizeof(struct ifla_vf_rate)) +
Vlad Zolotarov01a3d792015-03-30 21:35:23 +0300832 nla_total_size(sizeof(struct ifla_vf_link_state)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300833 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100834 nla_total_size(0) + /* nest IFLA_VF_STATS */
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300835 /* IFLA_VF_STATS_RX_PACKETS */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200836 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300837 /* IFLA_VF_STATS_TX_PACKETS */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200838 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300839 /* IFLA_VF_STATS_RX_BYTES */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200840 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300841 /* IFLA_VF_STATS_TX_BYTES */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200842 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300843 /* IFLA_VF_STATS_BROADCAST */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200844 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300845 /* IFLA_VF_STATS_MULTICAST */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200846 nla_total_size_64bit(sizeof(__u64)) +
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +0000847 nla_total_size(sizeof(struct ifla_vf_trust)));
Chris Wrightc02db8c2010-05-16 01:05:45 -0700848 return size;
849 } else
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000850 return 0;
851}
852
David Gibsonc53864f2014-04-24 10:22:36 +1000853static size_t rtnl_port_size(const struct net_device *dev,
854 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -0700855{
856 size_t port_size = nla_total_size(4) /* PORT_VF */
857 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
Scott Feldman57b61082010-05-17 22:49:55 -0700858 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
859 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
860 + nla_total_size(1) /* PROT_VDP_REQUEST */
861 + nla_total_size(2); /* PORT_VDP_RESPONSE */
862 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
863 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
864 + port_size;
865 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
866 + port_size;
867
David Gibsonc53864f2014-04-24 10:22:36 +1000868 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
869 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -0700870 return 0;
871 if (dev_num_vf(dev->dev.parent))
872 return port_self_size + vf_ports_size +
873 vf_port_size * dev_num_vf(dev->dev.parent);
874 else
875 return port_self_size;
876}
877
David S. Millerb5cdae32017-04-18 15:36:58 -0400878static size_t rtnl_xdp_size(void)
Brenden Blancod1fdd912016-07-19 12:16:49 -0700879{
Sabrina Dubrocab3cfaa32016-11-15 11:16:35 +0100880 size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */
Martin KaFai Lau58038692017-06-15 17:29:09 -0700881 nla_total_size(1) + /* XDP_ATTACHED */
882 nla_total_size(4); /* XDP_PROG_ID */
Brenden Blancod1fdd912016-07-19 12:16:49 -0700883
David S. Millerb5cdae32017-04-18 15:36:58 -0400884 return xdp_size;
Brenden Blancod1fdd912016-07-19 12:16:49 -0700885}
886
Greg Rose115c9b82012-02-21 16:54:48 -0500887static noinline size_t if_nlmsg_size(const struct net_device *dev,
888 u32 ext_filter_mask)
Thomas Graf339bf982006-11-10 14:10:15 -0800889{
890 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
891 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700892 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
Thomas Graf339bf982006-11-10 14:10:15 -0800893 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
Nicolas Dichtel270cb4d2016-04-26 10:06:16 +0200894 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
Thomas Graf339bf982006-11-10 14:10:15 -0800895 + nla_total_size(sizeof(struct rtnl_link_stats))
David S. Miller35c58452016-04-19 19:49:29 -0400896 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
Thomas Graf339bf982006-11-10 14:10:15 -0800897 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
898 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
899 + nla_total_size(4) /* IFLA_TXQLEN */
900 + nla_total_size(4) /* IFLA_WEIGHT */
901 + nla_total_size(4) /* IFLA_MTU */
902 + nla_total_size(4) /* IFLA_LINK */
903 + nla_total_size(4) /* IFLA_MASTER */
Jiri Pirko9a572472012-12-27 23:49:39 +0000904 + nla_total_size(1) /* IFLA_CARRIER */
Ben Greearedbc0bb2012-03-29 12:51:30 +0000905 + nla_total_size(4) /* IFLA_PROMISCUITY */
Jiri Pirko76ff5cc2012-07-20 02:28:48 +0000906 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
907 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
Tobias Klauser69197562016-11-30 14:30:37 +0100908 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
909 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
Thomas Graf339bf982006-11-10 14:10:15 -0800910 + nla_total_size(1) /* IFLA_OPERSTATE */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700911 + nla_total_size(1) /* IFLA_LINKMODE */
david decotigny2d3b4792014-03-29 09:48:35 -0700912 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
Nicolas Dichteld37512a2015-01-15 15:11:16 +0100913 + nla_total_size(4) /* IFLA_LINK_NETNSID */
Serhey Popovychdb833d42017-06-20 14:35:23 +0300914 + nla_total_size(4) /* IFLA_GROUP */
Greg Rose115c9b82012-02-21 16:54:48 -0500915 + nla_total_size(ext_filter_mask
916 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
917 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
David Gibsonc53864f2014-04-24 10:22:36 +1000918 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
Thomas Graff8ff1822010-11-16 04:30:14 +0000919 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700920 + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
Jiri Pirko82f28412014-11-28 14:34:18 +0100921 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
Anuradha Karuppiah88d63782015-07-14 13:43:20 -0700922 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
Nicolas Dichtelc57c7a92016-03-31 18:10:31 +0200923 + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
David S. Millerb5cdae32017-04-18 15:36:58 -0400924 + rtnl_xdp_size() /* IFLA_XDP */
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -0400925 + nla_total_size(4) /* IFLA_EVENT */
Anuradha Karuppiah88d63782015-07-14 13:43:20 -0700926 + nla_total_size(1); /* IFLA_PROTO_DOWN */
927
Thomas Graf339bf982006-11-10 14:10:15 -0800928}
929
Scott Feldman57b61082010-05-17 22:49:55 -0700930static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
931{
932 struct nlattr *vf_ports;
933 struct nlattr *vf_port;
934 int vf;
935 int err;
936
937 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
938 if (!vf_ports)
939 return -EMSGSIZE;
940
941 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
942 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
Scott Feldman8ca94182010-05-28 03:42:18 -0700943 if (!vf_port)
944 goto nla_put_failure;
David S. Millera6574342012-04-01 20:12:00 -0400945 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
946 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -0700947 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
Scott Feldman8ca94182010-05-28 03:42:18 -0700948 if (err == -EMSGSIZE)
949 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -0700950 if (err) {
Scott Feldman57b61082010-05-17 22:49:55 -0700951 nla_nest_cancel(skb, vf_port);
952 continue;
953 }
954 nla_nest_end(skb, vf_port);
955 }
956
957 nla_nest_end(skb, vf_ports);
958
959 return 0;
Scott Feldman8ca94182010-05-28 03:42:18 -0700960
961nla_put_failure:
962 nla_nest_cancel(skb, vf_ports);
963 return -EMSGSIZE;
Scott Feldman57b61082010-05-17 22:49:55 -0700964}
965
966static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
967{
968 struct nlattr *port_self;
969 int err;
970
971 port_self = nla_nest_start(skb, IFLA_PORT_SELF);
972 if (!port_self)
973 return -EMSGSIZE;
974
975 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
976 if (err) {
977 nla_nest_cancel(skb, port_self);
Scott Feldman8ca94182010-05-28 03:42:18 -0700978 return (err == -EMSGSIZE) ? err : 0;
Scott Feldman57b61082010-05-17 22:49:55 -0700979 }
980
981 nla_nest_end(skb, port_self);
982
983 return 0;
984}
985
David Gibsonc53864f2014-04-24 10:22:36 +1000986static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
987 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -0700988{
989 int err;
990
David Gibsonc53864f2014-04-24 10:22:36 +1000991 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
992 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -0700993 return 0;
994
995 err = rtnl_port_self_fill(skb, dev);
996 if (err)
997 return err;
998
999 if (dev_num_vf(dev->dev.parent)) {
1000 err = rtnl_vf_ports_fill(skb, dev);
1001 if (err)
1002 return err;
1003 }
1004
1005 return 0;
1006}
1007
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001008static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
1009{
1010 int err;
Jiri Pirko02637fc2014-11-28 14:34:16 +01001011 struct netdev_phys_item_id ppid;
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001012
1013 err = dev_get_phys_port_id(dev, &ppid);
1014 if (err) {
1015 if (err == -EOPNOTSUPP)
1016 return 0;
1017 return err;
1018 }
1019
1020 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1021 return -EMSGSIZE;
1022
1023 return 0;
1024}
1025
David Aherndb24a902015-03-17 20:23:15 -06001026static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1027{
1028 char name[IFNAMSIZ];
1029 int err;
1030
1031 err = dev_get_phys_port_name(dev, name, sizeof(name));
1032 if (err) {
1033 if (err == -EOPNOTSUPP)
1034 return 0;
1035 return err;
1036 }
1037
Michal Schmidt77ef0332017-05-04 16:48:58 +02001038 if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
David Aherndb24a902015-03-17 20:23:15 -06001039 return -EMSGSIZE;
1040
1041 return 0;
1042}
1043
Jiri Pirko82f28412014-11-28 14:34:18 +01001044static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1045{
1046 int err;
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001047 struct switchdev_attr attr = {
Ido Schimmel6ff64f62015-12-15 16:03:35 +01001048 .orig_dev = dev,
Jiri Pirko1f868392015-10-01 11:03:42 +02001049 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001050 .flags = SWITCHDEV_F_NO_RECURSE,
1051 };
Jiri Pirko82f28412014-11-28 14:34:18 +01001052
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001053 err = switchdev_port_attr_get(dev, &attr);
Jiri Pirko82f28412014-11-28 14:34:18 +01001054 if (err) {
1055 if (err == -EOPNOTSUPP)
1056 return 0;
1057 return err;
1058 }
1059
Scott Feldman42275bd2015-05-13 11:16:50 -07001060 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len,
1061 attr.u.ppid.id))
Jiri Pirko82f28412014-11-28 14:34:18 +01001062 return -EMSGSIZE;
1063
1064 return 0;
1065}
1066
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001067static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1068 struct net_device *dev)
1069{
Roopa Prabhu550bce52016-04-15 20:36:25 -07001070 struct rtnl_link_stats64 *sp;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001071 struct nlattr *attr;
1072
Nicolas Dichtel58414d32016-04-21 18:58:25 +02001073 attr = nla_reserve_64bit(skb, IFLA_STATS64,
1074 sizeof(struct rtnl_link_stats64), IFLA_PAD);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001075 if (!attr)
1076 return -EMSGSIZE;
1077
Roopa Prabhu550bce52016-04-15 20:36:25 -07001078 sp = nla_data(attr);
1079 dev_get_stats(dev, sp);
1080
1081 attr = nla_reserve(skb, IFLA_STATS,
1082 sizeof(struct rtnl_link_stats));
1083 if (!attr)
1084 return -EMSGSIZE;
1085
1086 copy_rtnl_link_stats(nla_data(attr), sp);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001087
1088 return 0;
1089}
1090
1091static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1092 struct net_device *dev,
1093 int vfs_num,
1094 struct nlattr *vfinfo)
1095{
1096 struct ifla_vf_rss_query_en vf_rss_query_en;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001097 struct nlattr *vf, *vfstats, *vfvlanlist;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001098 struct ifla_vf_link_state vf_linkstate;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001099 struct ifla_vf_vlan_info vf_vlan_info;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001100 struct ifla_vf_spoofchk vf_spoofchk;
1101 struct ifla_vf_tx_rate vf_tx_rate;
1102 struct ifla_vf_stats vf_stats;
1103 struct ifla_vf_trust vf_trust;
1104 struct ifla_vf_vlan vf_vlan;
1105 struct ifla_vf_rate vf_rate;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001106 struct ifla_vf_mac vf_mac;
1107 struct ifla_vf_info ivi;
1108
Mintz, Yuval0eed9cf2017-06-07 21:00:33 +03001109 memset(&ivi, 0, sizeof(ivi));
1110
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001111 /* Not all SR-IOV capable drivers support the
1112 * spoofcheck and "RSS query enable" query. Preset to
1113 * -1 so the user space tool can detect that the driver
1114 * didn't report anything.
1115 */
1116 ivi.spoofchk = -1;
1117 ivi.rss_query_en = -1;
1118 ivi.trusted = -1;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001119 /* The default value for VF link state is "auto"
1120 * IFLA_VF_LINK_STATE_AUTO which equals zero
1121 */
1122 ivi.linkstate = 0;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001123 /* VLAN Protocol by default is 802.1Q */
1124 ivi.vlan_proto = htons(ETH_P_8021Q);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001125 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1126 return 0;
1127
Dan Carpenter775f4f02016-10-13 11:45:28 +03001128 memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
1129
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001130 vf_mac.vf =
1131 vf_vlan.vf =
Moshe Shemesh79aab092016-09-22 12:11:15 +03001132 vf_vlan_info.vf =
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001133 vf_rate.vf =
1134 vf_tx_rate.vf =
1135 vf_spoofchk.vf =
1136 vf_linkstate.vf =
1137 vf_rss_query_en.vf =
1138 vf_trust.vf = ivi.vf;
1139
1140 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1141 vf_vlan.vlan = ivi.vlan;
1142 vf_vlan.qos = ivi.qos;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001143 vf_vlan_info.vlan = ivi.vlan;
1144 vf_vlan_info.qos = ivi.qos;
1145 vf_vlan_info.vlan_proto = ivi.vlan_proto;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001146 vf_tx_rate.rate = ivi.max_tx_rate;
1147 vf_rate.min_tx_rate = ivi.min_tx_rate;
1148 vf_rate.max_tx_rate = ivi.max_tx_rate;
1149 vf_spoofchk.setting = ivi.spoofchk;
1150 vf_linkstate.link_state = ivi.linkstate;
1151 vf_rss_query_en.setting = ivi.rss_query_en;
1152 vf_trust.setting = ivi.trusted;
1153 vf = nla_nest_start(skb, IFLA_VF_INFO);
Moshe Shemesh79aab092016-09-22 12:11:15 +03001154 if (!vf)
1155 goto nla_put_vfinfo_failure;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001156 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1157 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1158 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1159 &vf_rate) ||
1160 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1161 &vf_tx_rate) ||
1162 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1163 &vf_spoofchk) ||
1164 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1165 &vf_linkstate) ||
1166 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1167 sizeof(vf_rss_query_en),
1168 &vf_rss_query_en) ||
1169 nla_put(skb, IFLA_VF_TRUST,
1170 sizeof(vf_trust), &vf_trust))
Moshe Shemesh79aab092016-09-22 12:11:15 +03001171 goto nla_put_vf_failure;
1172 vfvlanlist = nla_nest_start(skb, IFLA_VF_VLAN_LIST);
1173 if (!vfvlanlist)
1174 goto nla_put_vf_failure;
1175 if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
1176 &vf_vlan_info)) {
1177 nla_nest_cancel(skb, vfvlanlist);
1178 goto nla_put_vf_failure;
1179 }
1180 nla_nest_end(skb, vfvlanlist);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001181 memset(&vf_stats, 0, sizeof(vf_stats));
1182 if (dev->netdev_ops->ndo_get_vf_stats)
1183 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1184 &vf_stats);
1185 vfstats = nla_nest_start(skb, IFLA_VF_STATS);
Moshe Shemesh79aab092016-09-22 12:11:15 +03001186 if (!vfstats)
1187 goto nla_put_vf_failure;
Nicolas Dichtel343a6d82016-04-25 10:25:14 +02001188 if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1189 vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1190 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1191 vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1192 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1193 vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1194 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1195 vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1196 nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1197 vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1198 nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
Moshe Shemesh79aab092016-09-22 12:11:15 +03001199 vf_stats.multicast, IFLA_VF_STATS_PAD)) {
1200 nla_nest_cancel(skb, vfstats);
1201 goto nla_put_vf_failure;
1202 }
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001203 nla_nest_end(skb, vfstats);
1204 nla_nest_end(skb, vf);
1205 return 0;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001206
1207nla_put_vf_failure:
1208 nla_nest_cancel(skb, vf);
1209nla_put_vfinfo_failure:
1210 nla_nest_cancel(skb, vfinfo);
1211 return -EMSGSIZE;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001212}
1213
1214static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1215{
Kangjie Lu5f8e4472016-05-03 16:46:24 -04001216 struct rtnl_link_ifmap map;
1217
1218 memset(&map, 0, sizeof(map));
1219 map.mem_start = dev->mem_start;
1220 map.mem_end = dev->mem_end;
1221 map.base_addr = dev->base_addr;
1222 map.irq = dev->irq;
1223 map.dma = dev->dma;
1224 map.port = dev->if_port;
1225
Nicolas Dichtel270cb4d2016-04-26 10:06:16 +02001226 if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001227 return -EMSGSIZE;
1228
1229 return 0;
1230}
1231
Martin KaFai Lau58038692017-06-15 17:29:09 -07001232static u8 rtnl_xdp_attached_mode(struct net_device *dev, u32 *prog_id)
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001233{
1234 const struct net_device_ops *ops = dev->netdev_ops;
Martin KaFai Lau58038692017-06-15 17:29:09 -07001235 const struct bpf_prog *generic_xdp_prog;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001236
1237 ASSERT_RTNL();
1238
Martin KaFai Lau58038692017-06-15 17:29:09 -07001239 *prog_id = 0;
1240 generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
1241 if (generic_xdp_prog) {
1242 *prog_id = generic_xdp_prog->aux->id;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001243 return XDP_ATTACHED_SKB;
Martin KaFai Lau58038692017-06-15 17:29:09 -07001244 }
Jakub Kicinskice158e52017-06-21 18:25:09 -07001245 if (!ops->ndo_xdp)
1246 return XDP_ATTACHED_NONE;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001247
Jakub Kicinskice158e52017-06-21 18:25:09 -07001248 return __dev_xdp_attached(dev, ops->ndo_xdp, prog_id);
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001249}
1250
Brenden Blancod1fdd912016-07-19 12:16:49 -07001251static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1252{
Brenden Blancod1fdd912016-07-19 12:16:49 -07001253 struct nlattr *xdp;
Martin KaFai Lau58038692017-06-15 17:29:09 -07001254 u32 prog_id;
Brenden Blancod1fdd912016-07-19 12:16:49 -07001255 int err;
1256
Brenden Blancod1fdd912016-07-19 12:16:49 -07001257 xdp = nla_nest_start(skb, IFLA_XDP);
1258 if (!xdp)
1259 return -EMSGSIZE;
David S. Millerb5cdae32017-04-18 15:36:58 -04001260
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001261 err = nla_put_u8(skb, IFLA_XDP_ATTACHED,
Martin KaFai Lau58038692017-06-15 17:29:09 -07001262 rtnl_xdp_attached_mode(dev, &prog_id));
Brenden Blancod1fdd912016-07-19 12:16:49 -07001263 if (err)
1264 goto err_cancel;
1265
Martin KaFai Lau58038692017-06-15 17:29:09 -07001266 if (prog_id) {
1267 err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
1268 if (err)
1269 goto err_cancel;
1270 }
1271
Brenden Blancod1fdd912016-07-19 12:16:49 -07001272 nla_nest_end(skb, xdp);
1273 return 0;
1274
1275err_cancel:
1276 nla_nest_cancel(skb, xdp);
1277 return err;
1278}
1279
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001280static u32 rtnl_get_event(unsigned long event)
1281{
1282 u32 rtnl_event_type = IFLA_EVENT_NONE;
1283
1284 switch (event) {
1285 case NETDEV_REBOOT:
1286 rtnl_event_type = IFLA_EVENT_REBOOT;
1287 break;
1288 case NETDEV_FEAT_CHANGE:
1289 rtnl_event_type = IFLA_EVENT_FEATURES;
1290 break;
1291 case NETDEV_BONDING_FAILOVER:
1292 rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
1293 break;
1294 case NETDEV_NOTIFY_PEERS:
1295 rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
1296 break;
1297 case NETDEV_RESEND_IGMP:
1298 rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
1299 break;
1300 case NETDEV_CHANGEINFODATA:
1301 rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
1302 break;
1303 default:
1304 break;
1305 }
1306
1307 return rtnl_event_type;
1308}
1309
Thomas Grafb60c5112006-08-04 23:05:34 -07001310static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
Patrick McHardy575c3e22007-05-22 17:00:49 -07001311 int type, u32 pid, u32 seq, u32 change,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001312 unsigned int flags, u32 ext_filter_mask,
1313 u32 event)
Thomas Grafb60c5112006-08-04 23:05:34 -07001314{
1315 struct ifinfomsg *ifm;
1316 struct nlmsghdr *nlh;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001317 struct nlattr *af_spec;
Thomas Graff8ff1822010-11-16 04:30:14 +00001318 struct rtnl_af_ops *af_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00001319 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Thomas Grafb60c5112006-08-04 23:05:34 -07001320
Eric Dumazet2907c352011-05-25 07:34:04 +00001321 ASSERT_RTNL();
Thomas Grafb60c5112006-08-04 23:05:34 -07001322 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1323 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08001324 return -EMSGSIZE;
Thomas Grafb60c5112006-08-04 23:05:34 -07001325
1326 ifm = nlmsg_data(nlh);
1327 ifm->ifi_family = AF_UNSPEC;
1328 ifm->__ifi_pad = 0;
1329 ifm->ifi_type = dev->type;
1330 ifm->ifi_index = dev->ifindex;
1331 ifm->ifi_flags = dev_get_flags(dev);
1332 ifm->ifi_change = change;
1333
David S. Millera6574342012-04-01 20:12:00 -04001334 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1335 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1336 nla_put_u8(skb, IFLA_OPERSTATE,
1337 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1338 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1339 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1340 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
Ben Greearedbc0bb2012-03-29 12:51:30 +00001341 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001342 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
Eric Dumazetc70ce022016-03-21 09:55:10 -07001343 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1344 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001345#ifdef CONFIG_RPS
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001346 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001347#endif
Nicolas Dichtela54acb32015-04-02 17:07:00 +02001348 (dev->ifindex != dev_get_iflink(dev) &&
1349 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00001350 (upper_dev &&
1351 nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
Jiri Pirko9a572472012-12-27 23:49:39 +00001352 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
David S. Millera6574342012-04-01 20:12:00 -04001353 (dev->qdisc &&
1354 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1355 (dev->ifalias &&
david decotigny2d3b4792014-03-29 09:48:35 -07001356 nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) ||
1357 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001358 atomic_read(&dev->carrier_changes)) ||
1359 nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
David S. Millera6574342012-04-01 20:12:00 -04001360 goto nla_put_failure;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001361
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001362 if (event != IFLA_EVENT_NONE) {
1363 if (nla_put_u32(skb, IFLA_EVENT, event))
1364 goto nla_put_failure;
1365 }
1366
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001367 if (rtnl_fill_link_ifmap(skb, dev))
1368 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370 if (dev->addr_len) {
David S. Millera6574342012-04-01 20:12:00 -04001371 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1372 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1373 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 }
1375
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001376 if (rtnl_phys_port_id_fill(skb, dev))
1377 goto nla_put_failure;
1378
David Aherndb24a902015-03-17 20:23:15 -06001379 if (rtnl_phys_port_name_fill(skb, dev))
1380 goto nla_put_failure;
1381
Jiri Pirko82f28412014-11-28 14:34:18 +01001382 if (rtnl_phys_switch_id_fill(skb, dev))
1383 goto nla_put_failure;
1384
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001385 if (rtnl_fill_stats(skb, dev))
Pavel Emelyanov96e74082008-05-21 14:12:46 -07001386 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
David S. Millera6574342012-04-01 20:12:00 -04001388 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
1389 nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
1390 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001391
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001392 if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent &&
1393 ext_filter_mask & RTEXT_FILTER_VF) {
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001394 int i;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001395 struct nlattr *vfinfo;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001396 int num_vfs = dev_num_vf(dev->dev.parent);
1397
Chris Wrightc02db8c2010-05-16 01:05:45 -07001398 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1399 if (!vfinfo)
1400 goto nla_put_failure;
1401 for (i = 0; i < num_vfs; i++) {
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001402 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
Chris Wrightc02db8c2010-05-16 01:05:45 -07001403 goto nla_put_failure;
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001404 }
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001405
Chris Wrightc02db8c2010-05-16 01:05:45 -07001406 nla_nest_end(skb, vfinfo);
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001407 }
Scott Feldman57b61082010-05-17 22:49:55 -07001408
David Gibsonc53864f2014-04-24 10:22:36 +10001409 if (rtnl_port_fill(skb, dev, ext_filter_mask))
Scott Feldman57b61082010-05-17 22:49:55 -07001410 goto nla_put_failure;
1411
Brenden Blancod1fdd912016-07-19 12:16:49 -07001412 if (rtnl_xdp_fill(skb, dev))
1413 goto nla_put_failure;
1414
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001415 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07001416 if (rtnl_link_fill(skb, dev) < 0)
1417 goto nla_put_failure;
1418 }
1419
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001420 if (dev->rtnl_link_ops &&
1421 dev->rtnl_link_ops->get_link_net) {
1422 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1423
1424 if (!net_eq(dev_net(dev), link_net)) {
Nicolas Dichtel7a0877d2015-05-07 11:02:49 +02001425 int id = peernet2id_alloc(dev_net(dev), link_net);
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001426
1427 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1428 goto nla_put_failure;
1429 }
1430 }
1431
Thomas Graff8ff1822010-11-16 04:30:14 +00001432 if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1433 goto nla_put_failure;
1434
1435 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1436 if (af_ops->fill_link_af) {
1437 struct nlattr *af;
1438 int err;
1439
1440 if (!(af = nla_nest_start(skb, af_ops->family)))
1441 goto nla_put_failure;
1442
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04001443 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
Thomas Graff8ff1822010-11-16 04:30:14 +00001444
1445 /*
1446 * Caller may return ENODATA to indicate that there
1447 * was no data to be dumped. This is not an error, it
1448 * means we should trim the attribute header and
1449 * continue.
1450 */
1451 if (err == -ENODATA)
1452 nla_nest_cancel(skb, af);
1453 else if (err < 0)
1454 goto nla_put_failure;
1455
1456 nla_nest_end(skb, af);
1457 }
1458 }
1459
1460 nla_nest_end(skb, af_spec);
1461
Johannes Berg053c0952015-01-16 22:09:00 +01001462 nlmsg_end(skb, nlh);
1463 return 0;
Thomas Grafb60c5112006-08-04 23:05:34 -07001464
1465nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001466 nlmsg_cancel(skb, nlh);
1467 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468}
1469
Jiri Pirkof7b12602014-02-18 20:53:18 +01001470static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1471 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1472 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1473 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1474 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1475 [IFLA_MTU] = { .type = NLA_U32 },
1476 [IFLA_LINK] = { .type = NLA_U32 },
1477 [IFLA_MASTER] = { .type = NLA_U32 },
1478 [IFLA_CARRIER] = { .type = NLA_U8 },
1479 [IFLA_TXQLEN] = { .type = NLA_U32 },
1480 [IFLA_WEIGHT] = { .type = NLA_U32 },
1481 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1482 [IFLA_LINKMODE] = { .type = NLA_U8 },
1483 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1484 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1485 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
Nicolas Dichtel2459b4c2017-10-11 16:24:48 +02001486 /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
1487 * allow 0-length string (needed to remove an alias).
1488 */
1489 [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001490 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1491 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1492 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1493 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1494 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1495 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1496 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1497 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
Jiri Pirko02637fc2014-11-28 14:34:16 +01001498 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
david decotigny2d3b4792014-03-29 09:48:35 -07001499 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
Jiri Pirko82f28412014-11-28 14:34:18 +01001500 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
Nicolas Dichtel317f4812015-01-15 15:11:18 +01001501 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001502 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001503 [IFLA_XDP] = { .type = NLA_NESTED },
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001504 [IFLA_EVENT] = { .type = NLA_U32 },
Serhey Popovychdb833d42017-06-20 14:35:23 +03001505 [IFLA_GROUP] = { .type = NLA_U32 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001506};
1507
1508static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1509 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1510 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1511 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1512 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1513};
1514
Jiri Pirkof7b12602014-02-18 20:53:18 +01001515static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
Daniel Borkmann364d5712015-02-05 18:44:04 +01001516 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1517 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
Moshe Shemesh79aab092016-09-22 12:11:15 +03001518 [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED },
Daniel Borkmann364d5712015-02-05 18:44:04 +01001519 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1520 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1521 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1522 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001523 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001524 [IFLA_VF_STATS] = { .type = NLA_NESTED },
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00001525 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
Eli Cohencc8e27c2016-03-11 22:58:34 +02001526 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1527 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001528};
1529
Jiri Pirkof7b12602014-02-18 20:53:18 +01001530static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1531 [IFLA_PORT_VF] = { .type = NLA_U32 },
1532 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1533 .len = PORT_PROFILE_MAX },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001534 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1535 .len = PORT_UUID_MAX },
1536 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1537 .len = PORT_UUID_MAX },
1538 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1539 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
Daniel Borkmann025331d2017-02-17 01:56:11 +01001540
1541 /* Unused, but we need to keep it here since user space could
1542 * fill it. It's also broken with regard to NLA_BINARY use in
1543 * combination with structs.
1544 */
1545 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1546 .len = sizeof(struct ifla_port_vsi) },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001547};
1548
Brenden Blancod1fdd912016-07-19 12:16:49 -07001549static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
1550 [IFLA_XDP_FD] = { .type = NLA_S32 },
1551 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 },
Daniel Borkmann85de8572016-11-28 23:16:54 +01001552 [IFLA_XDP_FLAGS] = { .type = NLA_U32 },
Martin KaFai Lau58038692017-06-15 17:29:09 -07001553 [IFLA_XDP_PROG_ID] = { .type = NLA_U32 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001554};
1555
David Aherndc599f72016-02-02 08:17:07 -08001556static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1557{
1558 const struct rtnl_link_ops *ops = NULL;
1559 struct nlattr *linfo[IFLA_INFO_MAX + 1];
1560
Johannes Bergfceb6432017-04-12 14:34:07 +02001561 if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla,
1562 ifla_info_policy, NULL) < 0)
David Aherndc599f72016-02-02 08:17:07 -08001563 return NULL;
1564
1565 if (linfo[IFLA_INFO_KIND]) {
1566 char kind[MODULE_NAME_LEN];
1567
1568 nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1569 ops = rtnl_link_ops_get(kind);
1570 }
1571
1572 return ops;
1573}
1574
1575static bool link_master_filtered(struct net_device *dev, int master_idx)
1576{
1577 struct net_device *master;
1578
1579 if (!master_idx)
1580 return false;
1581
1582 master = netdev_master_upper_dev_get(dev);
1583 if (!master || master->ifindex != master_idx)
1584 return true;
1585
1586 return false;
1587}
1588
1589static bool link_kind_filtered(const struct net_device *dev,
1590 const struct rtnl_link_ops *kind_ops)
1591{
1592 if (kind_ops && dev->rtnl_link_ops != kind_ops)
1593 return true;
1594
1595 return false;
1596}
1597
1598static bool link_dump_filtered(struct net_device *dev,
1599 int master_idx,
1600 const struct rtnl_link_ops *kind_ops)
1601{
1602 if (link_master_filtered(dev, master_idx) ||
1603 link_kind_filtered(dev, kind_ops))
1604 return true;
1605
1606 return false;
1607}
1608
Thomas Grafb60c5112006-08-04 23:05:34 -07001609static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001611 struct net *net = sock_net(skb->sk);
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001612 int h, s_h;
1613 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 struct net_device *dev;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001615 struct hlist_head *head;
Greg Rose115c9b82012-02-21 16:54:48 -05001616 struct nlattr *tb[IFLA_MAX+1];
1617 u32 ext_filter_mask = 0;
David Aherndc599f72016-02-02 08:17:07 -08001618 const struct rtnl_link_ops *kind_ops = NULL;
1619 unsigned int flags = NLM_F_MULTI;
1620 int master_idx = 0;
David Gibson973462b2014-04-24 10:22:35 +10001621 int err;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001622 int hdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001624 s_h = cb->args[0];
1625 s_idx = cb->args[1];
1626
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001627 /* A hack to preserve kernel<->userspace interface.
1628 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1629 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1630 * what iproute2 < v3.9.0 used.
1631 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1632 * attribute, its netlink message is shorter than struct ifinfomsg.
1633 */
1634 hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1635 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1636
Johannes Bergfceb6432017-04-12 14:34:07 +02001637 if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX,
1638 ifla_policy, NULL) >= 0) {
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001639 if (tb[IFLA_EXT_MASK])
1640 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
David Aherndc599f72016-02-02 08:17:07 -08001641
1642 if (tb[IFLA_MASTER])
1643 master_idx = nla_get_u32(tb[IFLA_MASTER]);
1644
1645 if (tb[IFLA_LINKINFO])
1646 kind_ops = linkinfo_to_kind_ops(tb[IFLA_LINKINFO]);
1647
1648 if (master_idx || kind_ops)
1649 flags |= NLM_F_DUMP_FILTERED;
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001650 }
Greg Rose115c9b82012-02-21 16:54:48 -05001651
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001652 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1653 idx = 0;
1654 head = &net->dev_index_head[h];
Eric Dumazetcac5e652015-02-27 09:42:50 -08001655 hlist_for_each_entry(dev, head, index_hlist) {
David Aherndc599f72016-02-02 08:17:07 -08001656 if (link_dump_filtered(dev, master_idx, kind_ops))
Zhang Shengju3f0ae052016-11-19 23:28:32 +08001657 goto cont;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001658 if (idx < s_idx)
1659 goto cont;
David Gibson973462b2014-04-24 10:22:35 +10001660 err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
1661 NETLINK_CB(cb->skb).portid,
1662 cb->nlh->nlmsg_seq, 0,
David Aherndc599f72016-02-02 08:17:07 -08001663 flags,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001664 ext_filter_mask, 0);
David Gibson973462b2014-04-24 10:22:35 +10001665
David Ahernf6c57752017-05-15 23:19:17 -07001666 if (err < 0) {
1667 if (likely(skb->len))
1668 goto out;
1669
1670 goto out_err;
1671 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07001672cont:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001673 idx++;
1674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 }
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001676out:
David Ahernf6c57752017-05-15 23:19:17 -07001677 err = skb->len;
1678out_err:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001679 cb->args[1] = idx;
1680 cb->args[0] = h;
Jakub Sitnickid0225782017-08-09 17:39:12 +02001681 cb->seq = net->dev_base_seq;
1682 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
David Ahernf6c57752017-05-15 23:19:17 -07001684 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685}
1686
Johannes Bergfceb6432017-04-12 14:34:07 +02001687int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
1688 struct netlink_ext_ack *exterr)
Jiri Pirkof7b12602014-02-18 20:53:18 +01001689{
Johannes Bergfceb6432017-04-12 14:34:07 +02001690 return nla_parse(tb, IFLA_MAX, head, len, ifla_policy, exterr);
Jiri Pirkof7b12602014-02-18 20:53:18 +01001691}
1692EXPORT_SYMBOL(rtnl_nla_parse_ifla);
Scott Feldman57b61082010-05-17 22:49:55 -07001693
Eric W. Biederman81adee42009-11-08 00:53:51 -08001694struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1695{
1696 struct net *net;
1697 /* Examine the link attributes and figure out which
1698 * network namespace we are talking about.
1699 */
1700 if (tb[IFLA_NET_NS_PID])
1701 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001702 else if (tb[IFLA_NET_NS_FD])
1703 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
Eric W. Biederman81adee42009-11-08 00:53:51 -08001704 else
1705 net = get_net(src_net);
1706 return net;
1707}
1708EXPORT_SYMBOL(rtnl_link_get_net);
1709
Thomas Graf1840bb12008-02-23 19:54:36 -08001710static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1711{
1712 if (dev) {
1713 if (tb[IFLA_ADDRESS] &&
1714 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1715 return -EINVAL;
1716
1717 if (tb[IFLA_BROADCAST] &&
1718 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1719 return -EINVAL;
1720 }
1721
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001722 if (tb[IFLA_AF_SPEC]) {
1723 struct nlattr *af;
1724 int rem, err;
1725
1726 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1727 const struct rtnl_af_ops *af_ops;
1728
1729 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1730 return -EAFNOSUPPORT;
1731
1732 if (!af_ops->set_link_af)
1733 return -EOPNOTSUPP;
1734
1735 if (af_ops->validate_link_af) {
Kurt Van Dijck6d3a9a62011-01-26 04:55:24 +00001736 err = af_ops->validate_link_af(dev, af);
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001737 if (err < 0)
1738 return err;
1739 }
1740 }
1741 }
1742
Thomas Graf1840bb12008-02-23 19:54:36 -08001743 return 0;
1744}
1745
Eli Cohencc8e27c2016-03-11 22:58:34 +02001746static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
1747 int guid_type)
1748{
1749 const struct net_device_ops *ops = dev->netdev_ops;
1750
1751 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
1752}
1753
1754static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
1755{
1756 if (dev->type != ARPHRD_INFINIBAND)
1757 return -EOPNOTSUPP;
1758
1759 return handle_infiniband_guid(dev, ivt, guid_type);
1760}
1761
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001762static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
Chris Wrightc02db8c2010-05-16 01:05:45 -07001763{
Chris Wrightc02db8c2010-05-16 01:05:45 -07001764 const struct net_device_ops *ops = dev->netdev_ops;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001765 int err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001766
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001767 if (tb[IFLA_VF_MAC]) {
1768 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001769
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001770 err = -EOPNOTSUPP;
1771 if (ops->ndo_set_vf_mac)
1772 err = ops->ndo_set_vf_mac(dev, ivm->vf,
1773 ivm->mac);
1774 if (err < 0)
1775 return err;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001776 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001777
1778 if (tb[IFLA_VF_VLAN]) {
1779 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
1780
1781 err = -EOPNOTSUPP;
1782 if (ops->ndo_set_vf_vlan)
1783 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
Moshe Shemesh79aab092016-09-22 12:11:15 +03001784 ivv->qos,
1785 htons(ETH_P_8021Q));
1786 if (err < 0)
1787 return err;
1788 }
1789
1790 if (tb[IFLA_VF_VLAN_LIST]) {
1791 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
1792 struct nlattr *attr;
1793 int rem, len = 0;
1794
1795 err = -EOPNOTSUPP;
1796 if (!ops->ndo_set_vf_vlan)
1797 return err;
1798
1799 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
1800 if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
1801 nla_len(attr) < NLA_HDRLEN) {
1802 return -EINVAL;
1803 }
1804 if (len >= MAX_VLAN_LIST_LEN)
1805 return -EOPNOTSUPP;
1806 ivvl[len] = nla_data(attr);
1807
1808 len++;
1809 }
Arnd Bergmannfa34cd92016-09-30 18:13:49 +02001810 if (len == 0)
1811 return -EINVAL;
1812
Moshe Shemesh79aab092016-09-22 12:11:15 +03001813 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
1814 ivvl[0]->qos, ivvl[0]->vlan_proto);
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001815 if (err < 0)
1816 return err;
1817 }
1818
1819 if (tb[IFLA_VF_TX_RATE]) {
1820 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
1821 struct ifla_vf_info ivf;
1822
1823 err = -EOPNOTSUPP;
1824 if (ops->ndo_get_vf_config)
1825 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
1826 if (err < 0)
1827 return err;
1828
1829 err = -EOPNOTSUPP;
1830 if (ops->ndo_set_vf_rate)
1831 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1832 ivf.min_tx_rate,
1833 ivt->rate);
1834 if (err < 0)
1835 return err;
1836 }
1837
1838 if (tb[IFLA_VF_RATE]) {
1839 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
1840
1841 err = -EOPNOTSUPP;
1842 if (ops->ndo_set_vf_rate)
1843 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1844 ivt->min_tx_rate,
1845 ivt->max_tx_rate);
1846 if (err < 0)
1847 return err;
1848 }
1849
1850 if (tb[IFLA_VF_SPOOFCHK]) {
1851 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
1852
1853 err = -EOPNOTSUPP;
1854 if (ops->ndo_set_vf_spoofchk)
1855 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
1856 ivs->setting);
1857 if (err < 0)
1858 return err;
1859 }
1860
1861 if (tb[IFLA_VF_LINK_STATE]) {
1862 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
1863
1864 err = -EOPNOTSUPP;
1865 if (ops->ndo_set_vf_link_state)
1866 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
1867 ivl->link_state);
1868 if (err < 0)
1869 return err;
1870 }
1871
1872 if (tb[IFLA_VF_RSS_QUERY_EN]) {
1873 struct ifla_vf_rss_query_en *ivrssq_en;
1874
1875 err = -EOPNOTSUPP;
1876 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
1877 if (ops->ndo_set_vf_rss_query_en)
1878 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
1879 ivrssq_en->setting);
1880 if (err < 0)
1881 return err;
1882 }
1883
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00001884 if (tb[IFLA_VF_TRUST]) {
1885 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
1886
1887 err = -EOPNOTSUPP;
1888 if (ops->ndo_set_vf_trust)
1889 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
1890 if (err < 0)
1891 return err;
1892 }
1893
Eli Cohencc8e27c2016-03-11 22:58:34 +02001894 if (tb[IFLA_VF_IB_NODE_GUID]) {
1895 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
1896
1897 if (!ops->ndo_set_vf_guid)
1898 return -EOPNOTSUPP;
1899
1900 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
1901 }
1902
1903 if (tb[IFLA_VF_IB_PORT_GUID]) {
1904 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
1905
1906 if (!ops->ndo_set_vf_guid)
1907 return -EOPNOTSUPP;
1908
1909 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
1910 }
1911
Chris Wrightc02db8c2010-05-16 01:05:45 -07001912 return err;
1913}
1914
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001915static int do_set_master(struct net_device *dev, int ifindex)
1916{
Jiri Pirko898e5062013-01-03 22:48:52 +00001917 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001918 const struct net_device_ops *ops;
1919 int err;
1920
Jiri Pirko898e5062013-01-03 22:48:52 +00001921 if (upper_dev) {
1922 if (upper_dev->ifindex == ifindex)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001923 return 0;
Jiri Pirko898e5062013-01-03 22:48:52 +00001924 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001925 if (ops->ndo_del_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001926 err = ops->ndo_del_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001927 if (err)
1928 return err;
1929 } else {
1930 return -EOPNOTSUPP;
1931 }
1932 }
1933
1934 if (ifindex) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001935 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
1936 if (!upper_dev)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001937 return -EINVAL;
Jiri Pirko898e5062013-01-03 22:48:52 +00001938 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001939 if (ops->ndo_add_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001940 err = ops->ndo_add_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001941 if (err)
1942 return err;
1943 } else {
1944 return -EOPNOTSUPP;
1945 }
1946 }
1947 return 0;
1948}
1949
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001950#define DO_SETLINK_MODIFIED 0x01
Nicolas Dichtelba998902014-09-01 16:07:29 +02001951/* notify flag means notify + modified. */
1952#define DO_SETLINK_NOTIFY 0x03
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001953static int do_setlink(const struct sk_buff *skb,
1954 struct net_device *dev, struct ifinfomsg *ifm,
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07001955 struct netlink_ext_ack *extack,
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001956 struct nlattr **tb, char *ifname, int status)
Patrick McHardy0157f602007-06-13 12:03:36 -07001957{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001958 const struct net_device_ops *ops = dev->netdev_ops;
Patrick McHardy0157f602007-06-13 12:03:36 -07001959 int err;
1960
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001961 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
Eric W. Biederman81adee42009-11-08 00:53:51 -08001962 struct net *net = rtnl_link_get_net(dev_net(dev), tb);
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001963 if (IS_ERR(net)) {
1964 err = PTR_ERR(net);
1965 goto errout;
1966 }
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001967 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
Nicolas Dichtele0ebde02014-11-27 10:16:15 +01001968 put_net(net);
Eric W. Biedermanb51642f2012-11-16 03:03:11 +00001969 err = -EPERM;
1970 goto errout;
1971 }
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001972 err = dev_change_net_namespace(dev, net, ifname);
1973 put_net(net);
1974 if (err)
1975 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001976 status |= DO_SETLINK_MODIFIED;
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001977 }
1978
Patrick McHardy0157f602007-06-13 12:03:36 -07001979 if (tb[IFLA_MAP]) {
1980 struct rtnl_link_ifmap *u_map;
1981 struct ifmap k_map;
1982
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001983 if (!ops->ndo_set_config) {
Patrick McHardy0157f602007-06-13 12:03:36 -07001984 err = -EOPNOTSUPP;
1985 goto errout;
1986 }
1987
1988 if (!netif_device_present(dev)) {
1989 err = -ENODEV;
1990 goto errout;
1991 }
1992
1993 u_map = nla_data(tb[IFLA_MAP]);
1994 k_map.mem_start = (unsigned long) u_map->mem_start;
1995 k_map.mem_end = (unsigned long) u_map->mem_end;
1996 k_map.base_addr = (unsigned short) u_map->base_addr;
1997 k_map.irq = (unsigned char) u_map->irq;
1998 k_map.dma = (unsigned char) u_map->dma;
1999 k_map.port = (unsigned char) u_map->port;
2000
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002001 err = ops->ndo_set_config(dev, &k_map);
Patrick McHardy0157f602007-06-13 12:03:36 -07002002 if (err < 0)
2003 goto errout;
2004
Nicolas Dichtelba998902014-09-01 16:07:29 +02002005 status |= DO_SETLINK_NOTIFY;
Patrick McHardy0157f602007-06-13 12:03:36 -07002006 }
2007
2008 if (tb[IFLA_ADDRESS]) {
2009 struct sockaddr *sa;
2010 int len;
2011
WANG Cong153711f2017-07-20 11:27:57 -07002012 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2013 sizeof(*sa));
Patrick McHardy0157f602007-06-13 12:03:36 -07002014 sa = kmalloc(len, GFP_KERNEL);
2015 if (!sa) {
2016 err = -ENOMEM;
2017 goto errout;
2018 }
2019 sa->sa_family = dev->type;
2020 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
2021 dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00002022 err = dev_set_mac_address(dev, sa);
Patrick McHardy0157f602007-06-13 12:03:36 -07002023 kfree(sa);
2024 if (err)
2025 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002026 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002027 }
2028
2029 if (tb[IFLA_MTU]) {
2030 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
2031 if (err < 0)
2032 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002033 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002034 }
2035
Vlad Dogarucbda10f2011-01-13 23:38:30 +00002036 if (tb[IFLA_GROUP]) {
2037 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Nicolas Dichtelba998902014-09-01 16:07:29 +02002038 status |= DO_SETLINK_NOTIFY;
Vlad Dogarucbda10f2011-01-13 23:38:30 +00002039 }
2040
Patrick McHardy0157f602007-06-13 12:03:36 -07002041 /*
2042 * Interface selected by interface index but interface
2043 * name provided implies that a name change has been
2044 * requested.
2045 */
2046 if (ifm->ifi_index > 0 && ifname[0]) {
2047 err = dev_change_name(dev, ifname);
2048 if (err < 0)
2049 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002050 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002051 }
2052
Stephen Hemminger0b815a12008-09-22 21:28:11 -07002053 if (tb[IFLA_IFALIAS]) {
2054 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2055 nla_len(tb[IFLA_IFALIAS]));
2056 if (err < 0)
2057 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002058 status |= DO_SETLINK_NOTIFY;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07002059 }
2060
Patrick McHardy0157f602007-06-13 12:03:36 -07002061 if (tb[IFLA_BROADCAST]) {
2062 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00002063 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Patrick McHardy0157f602007-06-13 12:03:36 -07002064 }
2065
2066 if (ifm->ifi_flags || ifm->ifi_change) {
Patrick McHardy3729d502010-02-26 06:34:54 +00002067 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
Johannes Berg5f9021c2008-11-16 23:20:31 -08002068 if (err < 0)
2069 goto errout;
Patrick McHardy0157f602007-06-13 12:03:36 -07002070 }
2071
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002072 if (tb[IFLA_MASTER]) {
2073 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
2074 if (err)
2075 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002076 status |= DO_SETLINK_MODIFIED;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002077 }
2078
Jiri Pirko9a572472012-12-27 23:49:39 +00002079 if (tb[IFLA_CARRIER]) {
2080 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2081 if (err)
2082 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002083 status |= DO_SETLINK_MODIFIED;
Jiri Pirko9a572472012-12-27 23:49:39 +00002084 }
2085
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002086 if (tb[IFLA_TXQLEN]) {
Alexey Dobriyan0cd29502017-05-17 13:30:44 +03002087 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
2088 unsigned int orig_len = dev->tx_queue_len;
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002089
Jason Wang08294a22016-06-30 14:45:35 +08002090 if (dev->tx_queue_len ^ value) {
2091 dev->tx_queue_len = value;
2092 err = call_netdevice_notifiers(
2093 NETDEV_CHANGE_TX_QUEUE_LEN, dev);
2094 err = notifier_to_errno(err);
2095 if (err) {
2096 dev->tx_queue_len = orig_len;
2097 goto errout;
2098 }
Xin Long2d7f6692017-10-15 18:13:46 +08002099 status |= DO_SETLINK_MODIFIED;
Jason Wang08294a22016-06-30 14:45:35 +08002100 }
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002101 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002102
Patrick McHardy0157f602007-06-13 12:03:36 -07002103 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002104 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Patrick McHardy0157f602007-06-13 12:03:36 -07002105
2106 if (tb[IFLA_LINKMODE]) {
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002107 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2108
David S. Miller93b2d4a2008-02-17 18:35:07 -08002109 write_lock_bh(&dev_base_lock);
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002110 if (dev->link_mode ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02002111 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002112 dev->link_mode = value;
David S. Miller93b2d4a2008-02-17 18:35:07 -08002113 write_unlock_bh(&dev_base_lock);
Patrick McHardy0157f602007-06-13 12:03:36 -07002114 }
2115
Chris Wrightc02db8c2010-05-16 01:05:45 -07002116 if (tb[IFLA_VFINFO_LIST]) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002117 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
Chris Wrightc02db8c2010-05-16 01:05:45 -07002118 struct nlattr *attr;
2119 int rem;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002120
Chris Wrightc02db8c2010-05-16 01:05:45 -07002121 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002122 if (nla_type(attr) != IFLA_VF_INFO ||
2123 nla_len(attr) < NLA_HDRLEN) {
David Howells253683b2010-05-21 02:25:27 +00002124 err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002125 goto errout;
David Howells253683b2010-05-21 02:25:27 +00002126 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002127 err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr,
Johannes Bergfceb6432017-04-12 14:34:07 +02002128 ifla_vf_policy, NULL);
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002129 if (err < 0)
2130 goto errout;
2131 err = do_setvfinfo(dev, vfinfo);
Chris Wrightc02db8c2010-05-16 01:05:45 -07002132 if (err < 0)
2133 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002134 status |= DO_SETLINK_NOTIFY;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002135 }
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00002136 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002137 err = 0;
2138
Scott Feldman57b61082010-05-17 22:49:55 -07002139 if (tb[IFLA_VF_PORTS]) {
2140 struct nlattr *port[IFLA_PORT_MAX+1];
2141 struct nlattr *attr;
2142 int vf;
2143 int rem;
2144
2145 err = -EOPNOTSUPP;
2146 if (!ops->ndo_set_vf_port)
2147 goto errout;
2148
2149 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
Daniel Borkmann035d2102015-07-13 00:06:02 +02002150 if (nla_type(attr) != IFLA_VF_PORT ||
2151 nla_len(attr) < NLA_HDRLEN) {
2152 err = -EINVAL;
2153 goto errout;
2154 }
2155 err = nla_parse_nested(port, IFLA_PORT_MAX, attr,
Johannes Bergfceb6432017-04-12 14:34:07 +02002156 ifla_port_policy, NULL);
Scott Feldman57b61082010-05-17 22:49:55 -07002157 if (err < 0)
2158 goto errout;
2159 if (!port[IFLA_PORT_VF]) {
2160 err = -EOPNOTSUPP;
2161 goto errout;
2162 }
2163 vf = nla_get_u32(port[IFLA_PORT_VF]);
2164 err = ops->ndo_set_vf_port(dev, vf, port);
2165 if (err < 0)
2166 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002167 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07002168 }
2169 }
2170 err = 0;
2171
2172 if (tb[IFLA_PORT_SELF]) {
2173 struct nlattr *port[IFLA_PORT_MAX+1];
2174
2175 err = nla_parse_nested(port, IFLA_PORT_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +02002176 tb[IFLA_PORT_SELF], ifla_port_policy,
2177 NULL);
Scott Feldman57b61082010-05-17 22:49:55 -07002178 if (err < 0)
2179 goto errout;
2180
2181 err = -EOPNOTSUPP;
2182 if (ops->ndo_set_vf_port)
2183 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
2184 if (err < 0)
2185 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002186 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07002187 }
Thomas Graff8ff1822010-11-16 04:30:14 +00002188
2189 if (tb[IFLA_AF_SPEC]) {
2190 struct nlattr *af;
2191 int rem;
2192
2193 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2194 const struct rtnl_af_ops *af_ops;
2195
2196 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002197 BUG();
Thomas Graff8ff1822010-11-16 04:30:14 +00002198
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002199 err = af_ops->set_link_af(dev, af);
Thomas Graff8ff1822010-11-16 04:30:14 +00002200 if (err < 0)
2201 goto errout;
2202
Nicolas Dichtelba998902014-09-01 16:07:29 +02002203 status |= DO_SETLINK_NOTIFY;
Thomas Graff8ff1822010-11-16 04:30:14 +00002204 }
2205 }
Scott Feldman57b61082010-05-17 22:49:55 -07002206 err = 0;
2207
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07002208 if (tb[IFLA_PROTO_DOWN]) {
2209 err = dev_change_proto_down(dev,
2210 nla_get_u8(tb[IFLA_PROTO_DOWN]));
2211 if (err)
2212 goto errout;
2213 status |= DO_SETLINK_NOTIFY;
2214 }
2215
Brenden Blancod1fdd912016-07-19 12:16:49 -07002216 if (tb[IFLA_XDP]) {
2217 struct nlattr *xdp[IFLA_XDP_MAX + 1];
Daniel Borkmann85de8572016-11-28 23:16:54 +01002218 u32 xdp_flags = 0;
Brenden Blancod1fdd912016-07-19 12:16:49 -07002219
2220 err = nla_parse_nested(xdp, IFLA_XDP_MAX, tb[IFLA_XDP],
Johannes Bergfceb6432017-04-12 14:34:07 +02002221 ifla_xdp_policy, NULL);
Brenden Blancod1fdd912016-07-19 12:16:49 -07002222 if (err < 0)
2223 goto errout;
2224
Martin KaFai Lau58038692017-06-15 17:29:09 -07002225 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
Brenden Blanco262d8622016-07-20 17:22:34 -07002226 err = -EINVAL;
2227 goto errout;
2228 }
Daniel Borkmann85de8572016-11-28 23:16:54 +01002229
2230 if (xdp[IFLA_XDP_FLAGS]) {
2231 xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
2232 if (xdp_flags & ~XDP_FLAGS_MASK) {
2233 err = -EINVAL;
2234 goto errout;
2235 }
Jakub Kicinskiee5d0322017-06-21 18:25:04 -07002236 if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
Daniel Borkmann0489df92017-05-12 01:04:45 +02002237 err = -EINVAL;
2238 goto errout;
2239 }
Daniel Borkmann85de8572016-11-28 23:16:54 +01002240 }
2241
Brenden Blancod1fdd912016-07-19 12:16:49 -07002242 if (xdp[IFLA_XDP_FD]) {
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002243 err = dev_change_xdp_fd(dev, extack,
Daniel Borkmann85de8572016-11-28 23:16:54 +01002244 nla_get_s32(xdp[IFLA_XDP_FD]),
2245 xdp_flags);
Brenden Blancod1fdd912016-07-19 12:16:49 -07002246 if (err)
2247 goto errout;
2248 status |= DO_SETLINK_NOTIFY;
2249 }
2250 }
2251
Patrick McHardy0157f602007-06-13 12:03:36 -07002252errout:
Nicolas Dichtelba998902014-09-01 16:07:29 +02002253 if (status & DO_SETLINK_MODIFIED) {
Xin Long64ff90c2017-10-15 18:13:45 +08002254 if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
Nicolas Dichtelba998902014-09-01 16:07:29 +02002255 netdev_state_change(dev);
2256
2257 if (err < 0)
2258 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",
2259 dev->name);
2260 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002261
Patrick McHardy0157f602007-06-13 12:03:36 -07002262 return err;
2263}
2264
David Ahernc21ef3e2017-04-16 09:48:24 -07002265static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2266 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002268 struct net *net = sock_net(skb->sk);
Thomas Grafda5e0492006-08-10 21:17:37 -07002269 struct ifinfomsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 struct net_device *dev;
Patrick McHardy0157f602007-06-13 12:03:36 -07002271 int err;
Thomas Grafda5e0492006-08-10 21:17:37 -07002272 struct nlattr *tb[IFLA_MAX+1];
2273 char ifname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
David Ahernc21ef3e2017-04-16 09:48:24 -07002275 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy,
2276 extack);
Thomas Grafda5e0492006-08-10 21:17:37 -07002277 if (err < 0)
2278 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
Thomas Graf5176f912006-08-26 20:13:18 -07002280 if (tb[IFLA_IFNAME])
2281 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
Patrick McHardy78e5b8912006-09-13 20:35:36 -07002282 else
2283 ifname[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 err = -EINVAL;
Thomas Grafda5e0492006-08-10 21:17:37 -07002286 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -07002287 if (ifm->ifi_index > 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00002288 dev = __dev_get_by_index(net, ifm->ifi_index);
Thomas Grafda5e0492006-08-10 21:17:37 -07002289 else if (tb[IFLA_IFNAME])
Eric Dumazeta3d12892009-10-21 10:59:31 +00002290 dev = __dev_get_by_name(net, ifname);
Thomas Grafda5e0492006-08-10 21:17:37 -07002291 else
2292 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
Thomas Grafda5e0492006-08-10 21:17:37 -07002294 if (dev == NULL) {
2295 err = -ENODEV;
2296 goto errout;
2297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
Eric Dumazete0d087a2009-11-07 01:26:17 -08002299 err = validate_linkmsg(dev, tb);
2300 if (err < 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00002301 goto errout;
Thomas Grafda5e0492006-08-10 21:17:37 -07002302
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002303 err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
Thomas Grafda5e0492006-08-10 21:17:37 -07002304errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 return err;
2306}
2307
WANG Cong66400d52015-03-24 11:53:31 -07002308static int rtnl_group_dellink(const struct net *net, int group)
2309{
2310 struct net_device *dev, *aux;
2311 LIST_HEAD(list_kill);
2312 bool found = false;
2313
2314 if (!group)
2315 return -EPERM;
2316
2317 for_each_netdev(net, dev) {
2318 if (dev->group == group) {
2319 const struct rtnl_link_ops *ops;
2320
2321 found = true;
2322 ops = dev->rtnl_link_ops;
2323 if (!ops || !ops->dellink)
2324 return -EOPNOTSUPP;
2325 }
2326 }
2327
2328 if (!found)
2329 return -ENODEV;
2330
2331 for_each_netdev_safe(net, dev, aux) {
2332 if (dev->group == group) {
2333 const struct rtnl_link_ops *ops;
2334
2335 ops = dev->rtnl_link_ops;
2336 ops->dellink(dev, &list_kill);
2337 }
2338 }
2339 unregister_netdevice_many(&list_kill);
2340
2341 return 0;
2342}
2343
Thomas Graf614732e2015-07-21 10:44:06 +02002344int rtnl_delete_link(struct net_device *dev)
2345{
2346 const struct rtnl_link_ops *ops;
2347 LIST_HEAD(list_kill);
2348
2349 ops = dev->rtnl_link_ops;
2350 if (!ops || !ops->dellink)
2351 return -EOPNOTSUPP;
2352
2353 ops->dellink(dev, &list_kill);
2354 unregister_netdevice_many(&list_kill);
2355
2356 return 0;
2357}
2358EXPORT_SYMBOL_GPL(rtnl_delete_link);
2359
David Ahernc21ef3e2017-04-16 09:48:24 -07002360static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
2361 struct netlink_ext_ack *extack)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002362{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002363 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002364 struct net_device *dev;
2365 struct ifinfomsg *ifm;
2366 char ifname[IFNAMSIZ];
2367 struct nlattr *tb[IFLA_MAX+1];
2368 int err;
2369
David Ahernc21ef3e2017-04-16 09:48:24 -07002370 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002371 if (err < 0)
2372 return err;
2373
2374 if (tb[IFLA_IFNAME])
2375 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2376
2377 ifm = nlmsg_data(nlh);
2378 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07002379 dev = __dev_get_by_index(net, ifm->ifi_index);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002380 else if (tb[IFLA_IFNAME])
Eric W. Biederman881d9662007-09-17 11:56:21 -07002381 dev = __dev_get_by_name(net, ifname);
WANG Cong66400d52015-03-24 11:53:31 -07002382 else if (tb[IFLA_GROUP])
2383 return rtnl_group_dellink(net, nla_get_u32(tb[IFLA_GROUP]));
Patrick McHardy38f7b872007-06-13 12:03:51 -07002384 else
2385 return -EINVAL;
2386
2387 if (!dev)
2388 return -ENODEV;
2389
Thomas Graf614732e2015-07-21 10:44:06 +02002390 return rtnl_delete_link(dev);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002391}
2392
Patrick McHardy3729d502010-02-26 06:34:54 +00002393int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
2394{
2395 unsigned int old_flags;
2396 int err;
2397
2398 old_flags = dev->flags;
2399 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
2400 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
2401 if (err < 0)
2402 return err;
2403 }
2404
2405 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
Patrick McHardy3729d502010-02-26 06:34:54 +00002406
Nicolas Dichtela528c212013-09-25 12:02:44 +02002407 __dev_notify_flags(dev, old_flags, ~0U);
Patrick McHardy3729d502010-02-26 06:34:54 +00002408 return 0;
2409}
2410EXPORT_SYMBOL(rtnl_configure_link);
2411
Rami Rosenc0713562012-11-30 01:08:47 +00002412struct net_device *rtnl_create_link(struct net *net,
Thomas Graf78ebb0d2015-04-10 01:45:53 +02002413 const char *ifname, unsigned char name_assign_type,
Tom Gundersen55177502014-07-14 16:37:25 +02002414 const struct rtnl_link_ops *ops, struct nlattr *tb[])
Pavel Emelianove7199282007-08-08 22:16:38 -07002415{
Pavel Emelianove7199282007-08-08 22:16:38 -07002416 struct net_device *dev;
Jiri Pirkod40156a2012-07-20 02:28:47 +00002417 unsigned int num_tx_queues = 1;
2418 unsigned int num_rx_queues = 1;
Pavel Emelianove7199282007-08-08 22:16:38 -07002419
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002420 if (tb[IFLA_NUM_TX_QUEUES])
2421 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
2422 else if (ops->get_num_tx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002423 num_tx_queues = ops->get_num_tx_queues();
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002424
2425 if (tb[IFLA_NUM_RX_QUEUES])
2426 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
2427 else if (ops->get_num_rx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002428 num_rx_queues = ops->get_num_rx_queues();
stephen hemmingerefacb302012-04-10 18:34:43 +00002429
Tom Gundersen55177502014-07-14 16:37:25 +02002430 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
Tom Gundersenc835a672014-07-14 16:37:24 +02002431 ops->setup, num_tx_queues, num_rx_queues);
Pavel Emelianove7199282007-08-08 22:16:38 -07002432 if (!dev)
Tobias Klauserd1892e42017-02-20 16:32:06 +01002433 return ERR_PTR(-ENOMEM);
Pavel Emelianove7199282007-08-08 22:16:38 -07002434
Eric W. Biederman81adee42009-11-08 00:53:51 -08002435 dev_net_set(dev, net);
2436 dev->rtnl_link_ops = ops;
Patrick McHardy3729d502010-02-26 06:34:54 +00002437 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
Eric W. Biederman81adee42009-11-08 00:53:51 -08002438
Pavel Emelianove7199282007-08-08 22:16:38 -07002439 if (tb[IFLA_MTU])
2440 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002441 if (tb[IFLA_ADDRESS]) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002442 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
2443 nla_len(tb[IFLA_ADDRESS]));
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002444 dev->addr_assign_type = NET_ADDR_SET;
2445 }
Pavel Emelianove7199282007-08-08 22:16:38 -07002446 if (tb[IFLA_BROADCAST])
2447 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
2448 nla_len(tb[IFLA_BROADCAST]));
2449 if (tb[IFLA_TXQLEN])
2450 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
2451 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002452 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Pavel Emelianove7199282007-08-08 22:16:38 -07002453 if (tb[IFLA_LINKMODE])
2454 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
Patrick McHardyffa934f2011-01-20 03:00:42 +00002455 if (tb[IFLA_GROUP])
2456 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Pavel Emelianove7199282007-08-08 22:16:38 -07002457
2458 return dev;
Pavel Emelianove7199282007-08-08 22:16:38 -07002459}
Eric Dumazete0d087a2009-11-07 01:26:17 -08002460EXPORT_SYMBOL(rtnl_create_link);
Pavel Emelianove7199282007-08-08 22:16:38 -07002461
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002462static int rtnl_group_changelink(const struct sk_buff *skb,
2463 struct net *net, int group,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002464 struct ifinfomsg *ifm,
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002465 struct netlink_ext_ack *extack,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002466 struct nlattr **tb)
2467{
WANG Congd0795352015-03-23 16:31:09 -07002468 struct net_device *dev, *aux;
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002469 int err;
2470
WANG Congd0795352015-03-23 16:31:09 -07002471 for_each_netdev_safe(net, dev, aux) {
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002472 if (dev->group == group) {
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002473 err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002474 if (err < 0)
2475 return err;
2476 }
2477 }
2478
2479 return 0;
2480}
2481
David Ahernc21ef3e2017-04-16 09:48:24 -07002482static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2483 struct netlink_ext_ack *extack)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002484{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002485 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002486 const struct rtnl_link_ops *ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002487 const struct rtnl_link_ops *m_ops = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002488 struct net_device *dev;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002489 struct net_device *master_dev = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002490 struct ifinfomsg *ifm;
2491 char kind[MODULE_NAME_LEN];
2492 char ifname[IFNAMSIZ];
2493 struct nlattr *tb[IFLA_MAX+1];
2494 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
Tom Gundersen55177502014-07-14 16:37:25 +02002495 unsigned char name_assign_type = NET_NAME_USER;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002496 int err;
2497
Johannes Berg95a5afc2008-10-16 15:24:51 -07002498#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002499replay:
Thomas Graf8072f082007-07-31 14:13:50 -07002500#endif
David Ahernc21ef3e2017-04-16 09:48:24 -07002501 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002502 if (err < 0)
2503 return err;
2504
2505 if (tb[IFLA_IFNAME])
2506 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2507 else
2508 ifname[0] = '\0';
2509
2510 ifm = nlmsg_data(nlh);
2511 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07002512 dev = __dev_get_by_index(net, ifm->ifi_index);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002513 else {
2514 if (ifname[0])
2515 dev = __dev_get_by_name(net, ifname);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002516 else
2517 dev = NULL;
2518 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002519
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002520 if (dev) {
2521 master_dev = netdev_master_upper_dev_get(dev);
2522 if (master_dev)
2523 m_ops = master_dev->rtnl_link_ops;
2524 }
2525
Eric Dumazete0d087a2009-11-07 01:26:17 -08002526 err = validate_linkmsg(dev, tb);
2527 if (err < 0)
Thomas Graf1840bb12008-02-23 19:54:36 -08002528 return err;
2529
Patrick McHardy38f7b872007-06-13 12:03:51 -07002530 if (tb[IFLA_LINKINFO]) {
2531 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +02002532 tb[IFLA_LINKINFO], ifla_info_policy,
2533 NULL);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002534 if (err < 0)
2535 return err;
2536 } else
2537 memset(linkinfo, 0, sizeof(linkinfo));
2538
2539 if (linkinfo[IFLA_INFO_KIND]) {
2540 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
2541 ops = rtnl_link_ops_get(kind);
2542 } else {
2543 kind[0] = '\0';
2544 ops = NULL;
2545 }
2546
2547 if (1) {
Sasha Levin4e10fd52015-02-24 14:14:35 -05002548 struct nlattr *attr[ops ? ops->maxtype + 1 : 1];
2549 struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1];
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002550 struct nlattr **data = NULL;
2551 struct nlattr **slave_data = NULL;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002552 struct net *dest_net, *link_net = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002553
2554 if (ops) {
2555 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
2556 err = nla_parse_nested(attr, ops->maxtype,
2557 linkinfo[IFLA_INFO_DATA],
Johannes Bergfceb6432017-04-12 14:34:07 +02002558 ops->policy, NULL);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002559 if (err < 0)
2560 return err;
2561 data = attr;
2562 }
2563 if (ops->validate) {
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02002564 err = ops->validate(tb, data, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002565 if (err < 0)
2566 return err;
2567 }
2568 }
2569
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002570 if (m_ops) {
2571 if (m_ops->slave_maxtype &&
2572 linkinfo[IFLA_INFO_SLAVE_DATA]) {
2573 err = nla_parse_nested(slave_attr,
2574 m_ops->slave_maxtype,
2575 linkinfo[IFLA_INFO_SLAVE_DATA],
Johannes Bergfceb6432017-04-12 14:34:07 +02002576 m_ops->slave_policy,
2577 NULL);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002578 if (err < 0)
2579 return err;
2580 slave_data = slave_attr;
2581 }
2582 if (m_ops->slave_validate) {
Matthias Schifferd116ffc2017-06-25 23:56:03 +02002583 err = m_ops->slave_validate(tb, slave_data,
2584 extack);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002585 if (err < 0)
2586 return err;
2587 }
2588 }
2589
Patrick McHardy38f7b872007-06-13 12:03:51 -07002590 if (dev) {
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002591 int status = 0;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002592
2593 if (nlh->nlmsg_flags & NLM_F_EXCL)
2594 return -EEXIST;
2595 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2596 return -EOPNOTSUPP;
2597
2598 if (linkinfo[IFLA_INFO_DATA]) {
2599 if (!ops || ops != dev->rtnl_link_ops ||
2600 !ops->changelink)
2601 return -EOPNOTSUPP;
2602
Matthias Schifferad744b22017-06-25 23:56:00 +02002603 err = ops->changelink(dev, tb, data, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002604 if (err < 0)
2605 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002606 status |= DO_SETLINK_NOTIFY;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002607 }
2608
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002609 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2610 if (!m_ops || !m_ops->slave_changelink)
2611 return -EOPNOTSUPP;
2612
2613 err = m_ops->slave_changelink(master_dev, dev,
Matthias Schiffer17dd0ec2017-06-25 23:56:02 +02002614 tb, slave_data,
2615 extack);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002616 if (err < 0)
2617 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002618 status |= DO_SETLINK_NOTIFY;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002619 }
2620
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002621 return do_setlink(skb, dev, ifm, extack, tb, ifname,
2622 status);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002623 }
2624
Patrick McHardyffa934f2011-01-20 03:00:42 +00002625 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2626 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002627 return rtnl_group_changelink(skb, net,
Patrick McHardyffa934f2011-01-20 03:00:42 +00002628 nla_get_u32(tb[IFLA_GROUP]),
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002629 ifm, extack, tb);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002630 return -ENODEV;
Patrick McHardyffa934f2011-01-20 03:00:42 +00002631 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002632
Theuns Verwoerd160ca012017-01-31 12:23:46 +13002633 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
Patrick McHardy38f7b872007-06-13 12:03:51 -07002634 return -EOPNOTSUPP;
2635
2636 if (!ops) {
Johannes Berg95a5afc2008-10-16 15:24:51 -07002637#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002638 if (kind[0]) {
2639 __rtnl_unlock();
2640 request_module("rtnl-link-%s", kind);
2641 rtnl_lock();
2642 ops = rtnl_link_ops_get(kind);
2643 if (ops)
2644 goto replay;
2645 }
2646#endif
2647 return -EOPNOTSUPP;
2648 }
2649
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02002650 if (!ops->setup)
2651 return -EOPNOTSUPP;
2652
Tom Gundersen55177502014-07-14 16:37:25 +02002653 if (!ifname[0]) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07002654 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
Tom Gundersen55177502014-07-14 16:37:25 +02002655 name_assign_type = NET_NAME_ENUM;
2656 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002657
Eric W. Biederman81adee42009-11-08 00:53:51 -08002658 dest_net = rtnl_link_get_net(net, tb);
Eric W. Biederman13ad1772011-01-29 14:57:22 +00002659 if (IS_ERR(dest_net))
2660 return PTR_ERR(dest_net);
2661
Eric W. Biederman505ce412015-02-26 16:19:00 -06002662 err = -EPERM;
2663 if (!netlink_ns_capable(skb, dest_net->user_ns, CAP_NET_ADMIN))
2664 goto out;
2665
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002666 if (tb[IFLA_LINK_NETNSID]) {
2667 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
2668
2669 link_net = get_net_ns_by_id(dest_net, id);
2670 if (!link_net) {
2671 err = -EINVAL;
2672 goto out;
2673 }
Eric W. Biederman06615be2015-02-26 16:20:07 -06002674 err = -EPERM;
2675 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
2676 goto out;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002677 }
2678
2679 dev = rtnl_create_link(link_net ? : dest_net, ifname,
2680 name_assign_type, ops, tb);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002681 if (IS_ERR(dev)) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002682 err = PTR_ERR(dev);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002683 goto out;
2684 }
2685
2686 dev->ifindex = ifm->ifi_index;
2687
Cong Wang0e0eee22014-02-11 15:51:30 -08002688 if (ops->newlink) {
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02002689 err = ops->newlink(link_net ? : net, dev, tb, data,
2690 extack);
Cong Wang0e0eee22014-02-11 15:51:30 -08002691 /* Drivers should call free_netdev() in ->destructor
Cong Wange51fb152014-06-03 16:40:47 -07002692 * and unregister it on failure after registration
2693 * so that device could be finally freed in rtnl_unlock.
Cong Wang0e0eee22014-02-11 15:51:30 -08002694 */
Cong Wange51fb152014-06-03 16:40:47 -07002695 if (err < 0) {
2696 /* If device is not registered at all, free it now */
2697 if (dev->reg_state == NETREG_UNINITIALIZED)
2698 free_netdev(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002699 goto out;
Cong Wange51fb152014-06-03 16:40:47 -07002700 }
Cong Wang0e0eee22014-02-11 15:51:30 -08002701 } else {
Patrick McHardy2d85cba2007-07-11 19:42:13 -07002702 err = register_netdevice(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002703 if (err < 0) {
2704 free_netdev(dev);
2705 goto out;
2706 }
Dan Carpenterfce9b9b2013-08-14 12:35:42 +03002707 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002708 err = rtnl_configure_link(dev, ifm);
David S. Miller43638902015-03-10 21:58:32 -04002709 if (err < 0)
2710 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002711 if (link_net) {
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002712 err = dev_change_net_namespace(dev, dest_net, ifname);
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002713 if (err < 0)
David S. Miller43638902015-03-10 21:58:32 -04002714 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002715 }
Theuns Verwoerd160ca012017-01-31 12:23:46 +13002716 if (tb[IFLA_MASTER]) {
2717 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
2718 if (err)
2719 goto out_unregister;
2720 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002721out:
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002722 if (link_net)
2723 put_net(link_net);
Eric W. Biederman81adee42009-11-08 00:53:51 -08002724 put_net(dest_net);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002725 return err;
David S. Miller43638902015-03-10 21:58:32 -04002726out_unregister:
2727 if (ops->newlink) {
2728 LIST_HEAD(list_kill);
2729
2730 ops->dellink(dev, &list_kill);
2731 unregister_netdevice_many(&list_kill);
2732 } else {
2733 unregister_netdevice(dev);
2734 }
2735 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002736 }
2737}
2738
David Ahernc21ef3e2017-04-16 09:48:24 -07002739static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2740 struct netlink_ext_ack *extack)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002741{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002742 struct net *net = sock_net(skb->sk);
Thomas Grafb60c5112006-08-04 23:05:34 -07002743 struct ifinfomsg *ifm;
Eric Dumazeta3d12892009-10-21 10:59:31 +00002744 char ifname[IFNAMSIZ];
Thomas Grafb60c5112006-08-04 23:05:34 -07002745 struct nlattr *tb[IFLA_MAX+1];
2746 struct net_device *dev = NULL;
2747 struct sk_buff *nskb;
Thomas Graf339bf982006-11-10 14:10:15 -08002748 int err;
Greg Rose115c9b82012-02-21 16:54:48 -05002749 u32 ext_filter_mask = 0;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002750
David Ahernc21ef3e2017-04-16 09:48:24 -07002751 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Thomas Grafb60c5112006-08-04 23:05:34 -07002752 if (err < 0)
Eric Sesterhenn9918f232006-09-26 23:26:38 -07002753 return err;
Thomas Grafb60c5112006-08-04 23:05:34 -07002754
Eric Dumazeta3d12892009-10-21 10:59:31 +00002755 if (tb[IFLA_IFNAME])
2756 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2757
Greg Rose115c9b82012-02-21 16:54:48 -05002758 if (tb[IFLA_EXT_MASK])
2759 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2760
Thomas Grafb60c5112006-08-04 23:05:34 -07002761 ifm = nlmsg_data(nlh);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002762 if (ifm->ifi_index > 0)
2763 dev = __dev_get_by_index(net, ifm->ifi_index);
2764 else if (tb[IFLA_IFNAME])
2765 dev = __dev_get_by_name(net, ifname);
2766 else
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002767 return -EINVAL;
Thomas Grafb60c5112006-08-04 23:05:34 -07002768
Eric Dumazeta3d12892009-10-21 10:59:31 +00002769 if (dev == NULL)
2770 return -ENODEV;
2771
Greg Rose115c9b82012-02-21 16:54:48 -05002772 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002773 if (nskb == NULL)
2774 return -ENOBUFS;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002775
Eric W. Biederman15e47302012-09-07 20:12:54 +00002776 err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04002777 nlh->nlmsg_seq, 0, 0, ext_filter_mask, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08002778 if (err < 0) {
2779 /* -EMSGSIZE implies BUG in if_nlmsg_size */
2780 WARN_ON(err == -EMSGSIZE);
2781 kfree_skb(nskb);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002782 } else
Eric W. Biederman15e47302012-09-07 20:12:54 +00002783 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
Thomas Grafb60c5112006-08-04 23:05:34 -07002784
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002785 return err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002786}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002787
Greg Rose115c9b82012-02-21 16:54:48 -05002788static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
Greg Rosec7ac8672011-06-10 01:27:09 +00002789{
Greg Rose115c9b82012-02-21 16:54:48 -05002790 struct net *net = sock_net(skb->sk);
2791 struct net_device *dev;
2792 struct nlattr *tb[IFLA_MAX+1];
2793 u32 ext_filter_mask = 0;
2794 u16 min_ifinfo_dump_size = 0;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02002795 int hdrlen;
Greg Rose115c9b82012-02-21 16:54:48 -05002796
Michal Schmidte5eca6d2014-05-28 14:15:19 +02002797 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2798 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2799 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2800
Johannes Bergfceb6432017-04-12 14:34:07 +02002801 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00002802 if (tb[IFLA_EXT_MASK])
2803 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2804 }
Greg Rose115c9b82012-02-21 16:54:48 -05002805
2806 if (!ext_filter_mask)
2807 return NLMSG_GOODSIZE;
2808 /*
2809 * traverse the list of net devices and compute the minimum
2810 * buffer size based upon the filter mask.
2811 */
Florian Westphal6853dd42017-08-09 20:41:51 +02002812 rcu_read_lock();
2813 for_each_netdev_rcu(net, dev) {
Greg Rose115c9b82012-02-21 16:54:48 -05002814 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
2815 if_nlmsg_size(dev,
2816 ext_filter_mask));
2817 }
Florian Westphal6853dd42017-08-09 20:41:51 +02002818 rcu_read_unlock();
Greg Rose115c9b82012-02-21 16:54:48 -05002819
Zhang Shengju93af2052016-11-22 14:14:28 +08002820 return nlmsg_total_size(min_ifinfo_dump_size);
Greg Rosec7ac8672011-06-10 01:27:09 +00002821}
2822
Adrian Bunk42bad1d2007-04-26 00:57:41 -07002823static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824{
2825 int idx;
2826 int s_idx = cb->family;
2827
2828 if (s_idx == 0)
2829 s_idx = 1;
Florian Westphal6853dd42017-08-09 20:41:51 +02002830
Patrick McHardy25239ce2010-04-26 16:02:05 +02002831 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 int type = cb->nlh->nlmsg_type-RTM_BASE;
Florian Westphal6853dd42017-08-09 20:41:51 +02002833 struct rtnl_link *handlers;
2834 rtnl_dumpit_func dumpit;
2835
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 if (idx < s_idx || idx == PF_PACKET)
2837 continue;
Florian Westphal6853dd42017-08-09 20:41:51 +02002838
2839 handlers = rtnl_dereference(rtnl_msg_handlers[idx]);
2840 if (!handlers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 continue;
Florian Westphal6853dd42017-08-09 20:41:51 +02002842
2843 dumpit = READ_ONCE(handlers[type].dumpit);
2844 if (!dumpit)
2845 continue;
2846
Nicolas Dichtel04652772013-03-22 06:28:42 +00002847 if (idx > s_idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 memset(&cb->args[0], 0, sizeof(cb->args));
Nicolas Dichtel04652772013-03-22 06:28:42 +00002849 cb->prev_seq = 0;
2850 cb->seq = 0;
2851 }
Florian Westphal6853dd42017-08-09 20:41:51 +02002852 if (dumpit(skb, cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 break;
2854 }
2855 cb->family = idx;
2856
2857 return skb->len;
2858}
2859
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002860struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04002861 unsigned int change,
2862 u32 event, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002864 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 struct sk_buff *skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002866 int err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00002867 size_t if_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868
Alexei Starovoitov7f294052013-10-23 16:02:42 -07002869 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002870 if (skb == NULL)
2871 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04002873 err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0, event);
Patrick McHardy26932562007-01-31 23:16:40 -08002874 if (err < 0) {
2875 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
2876 WARN_ON(err == -EMSGSIZE);
2877 kfree_skb(skb);
2878 goto errout;
2879 }
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002880 return skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002881errout:
2882 if (err < 0)
Eric W. Biederman4b3da702007-11-19 22:27:40 -08002883 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002884 return NULL;
2885}
2886
2887void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
2888{
2889 struct net *net = dev_net(dev);
2890
2891 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
2892}
2893
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04002894static void rtmsg_ifinfo_event(int type, struct net_device *dev,
2895 unsigned int change, u32 event,
2896 gfp_t flags)
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002897{
2898 struct sk_buff *skb;
2899
Nicolas Dichteled2a80a2015-05-13 14:19:42 +02002900 if (dev->reg_state != NETREG_REGISTERED)
2901 return;
2902
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04002903 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002904 if (skb)
2905 rtmsg_ifinfo_send(skb, dev, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906}
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04002907
2908void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
2909 gfp_t flags)
2910{
Vlad Yasevich8c6c9182017-05-30 10:07:02 -04002911 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags);
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04002912}
Jiri Pirko471cb5a2013-01-03 22:49:01 +00002913EXPORT_SYMBOL(rtmsg_ifinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914
John Fastabendd83b0602012-04-15 06:44:08 +00002915static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
2916 struct net_device *dev,
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002917 u8 *addr, u16 vid, u32 pid, u32 seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002918 int type, unsigned int flags,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002919 int nlflags, u16 ndm_state)
John Fastabendd83b0602012-04-15 06:44:08 +00002920{
2921 struct nlmsghdr *nlh;
2922 struct ndmsg *ndm;
2923
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002924 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
John Fastabendd83b0602012-04-15 06:44:08 +00002925 if (!nlh)
2926 return -EMSGSIZE;
2927
2928 ndm = nlmsg_data(nlh);
2929 ndm->ndm_family = AF_BRIDGE;
2930 ndm->ndm_pad1 = 0;
2931 ndm->ndm_pad2 = 0;
2932 ndm->ndm_flags = flags;
2933 ndm->ndm_type = 0;
2934 ndm->ndm_ifindex = dev->ifindex;
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002935 ndm->ndm_state = ndm_state;
John Fastabendd83b0602012-04-15 06:44:08 +00002936
2937 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
2938 goto nla_put_failure;
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002939 if (vid)
2940 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
2941 goto nla_put_failure;
John Fastabendd83b0602012-04-15 06:44:08 +00002942
Johannes Berg053c0952015-01-16 22:09:00 +01002943 nlmsg_end(skb, nlh);
2944 return 0;
John Fastabendd83b0602012-04-15 06:44:08 +00002945
2946nla_put_failure:
2947 nlmsg_cancel(skb, nlh);
2948 return -EMSGSIZE;
2949}
2950
John Fastabend3ff661c2012-04-15 06:44:14 +00002951static inline size_t rtnl_fdb_nlmsg_size(void)
2952{
Sabrina Dubrocaf82ef3e2016-11-18 15:50:39 +01002953 return NLMSG_ALIGN(sizeof(struct ndmsg)) +
2954 nla_total_size(ETH_ALEN) + /* NDA_LLADDR */
2955 nla_total_size(sizeof(u16)) + /* NDA_VLAN */
2956 0;
John Fastabend3ff661c2012-04-15 06:44:14 +00002957}
2958
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002959static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
2960 u16 ndm_state)
John Fastabend3ff661c2012-04-15 06:44:14 +00002961{
2962 struct net *net = dev_net(dev);
2963 struct sk_buff *skb;
2964 int err = -ENOBUFS;
2965
2966 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
2967 if (!skb)
2968 goto errout;
2969
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002970 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002971 0, 0, type, NTF_SELF, 0, ndm_state);
John Fastabend3ff661c2012-04-15 06:44:14 +00002972 if (err < 0) {
2973 kfree_skb(skb);
2974 goto errout;
2975 }
2976
2977 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
2978 return;
2979errout:
2980 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
2981}
2982
Vlad Yasevich090096b2013-03-06 15:39:42 +00002983/**
2984 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2985 */
2986int ndo_dflt_fdb_add(struct ndmsg *ndm,
2987 struct nlattr *tb[],
2988 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01002989 const unsigned char *addr, u16 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00002990 u16 flags)
2991{
2992 int err = -EINVAL;
2993
2994 /* If aging addresses are supported device will need to
2995 * implement its own handler for this.
2996 */
2997 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2998 pr_info("%s: FDB only supports static addresses\n", dev->name);
2999 return err;
3000 }
3001
Or Gerlitz65891fe2014-12-14 18:19:05 +02003002 if (vid) {
3003 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
3004 return err;
3005 }
3006
Vlad Yasevich090096b2013-03-06 15:39:42 +00003007 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3008 err = dev_uc_add_excl(dev, addr);
3009 else if (is_multicast_ether_addr(addr))
3010 err = dev_mc_add_excl(dev, addr);
3011
3012 /* Only return duplicate errors if NLM_F_EXCL is set */
3013 if (err == -EEXIST && !(flags & NLM_F_EXCL))
3014 err = 0;
3015
3016 return err;
3017}
3018EXPORT_SYMBOL(ndo_dflt_fdb_add);
3019
Jiri Pirkof6f64242014-11-28 14:34:15 +01003020static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid)
3021{
3022 u16 vid = 0;
3023
3024 if (vlan_attr) {
3025 if (nla_len(vlan_attr) != sizeof(u16)) {
3026 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n");
3027 return -EINVAL;
3028 }
3029
3030 vid = nla_get_u16(vlan_attr);
3031
3032 if (!vid || vid >= VLAN_VID_MASK) {
3033 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n",
3034 vid);
3035 return -EINVAL;
3036 }
3037 }
3038 *p_vid = vid;
3039 return 0;
3040}
3041
David Ahernc21ef3e2017-04-16 09:48:24 -07003042static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
3043 struct netlink_ext_ack *extack)
John Fastabend77162022012-04-15 06:43:56 +00003044{
3045 struct net *net = sock_net(skb->sk);
John Fastabend77162022012-04-15 06:43:56 +00003046 struct ndmsg *ndm;
3047 struct nlattr *tb[NDA_MAX+1];
3048 struct net_device *dev;
3049 u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01003050 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00003051 int err;
3052
David Ahernc21ef3e2017-04-16 09:48:24 -07003053 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
John Fastabend77162022012-04-15 06:43:56 +00003054 if (err < 0)
3055 return err;
3056
3057 ndm = nlmsg_data(nlh);
3058 if (ndm->ndm_ifindex == 0) {
3059 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
3060 return -EINVAL;
3061 }
3062
3063 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3064 if (dev == NULL) {
3065 pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
3066 return -ENODEV;
3067 }
3068
3069 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3070 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
3071 return -EINVAL;
3072 }
3073
3074 addr = nla_data(tb[NDA_LLADDR]);
John Fastabend77162022012-04-15 06:43:56 +00003075
Jiri Pirkof6f64242014-11-28 14:34:15 +01003076 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
3077 if (err)
3078 return err;
3079
John Fastabend77162022012-04-15 06:43:56 +00003080 err = -EOPNOTSUPP;
3081
3082 /* Support fdb on master device the net/bridge default case */
3083 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3084 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003085 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3086 const struct net_device_ops *ops = br_dev->netdev_ops;
3087
Jiri Pirkof6f64242014-11-28 14:34:15 +01003088 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
3089 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00003090 if (err)
3091 goto out;
3092 else
3093 ndm->ndm_flags &= ~NTF_MASTER;
3094 }
3095
3096 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00003097 if ((ndm->ndm_flags & NTF_SELF)) {
3098 if (dev->netdev_ops->ndo_fdb_add)
3099 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003100 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003101 nlh->nlmsg_flags);
3102 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01003103 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003104 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00003105
John Fastabend3ff661c2012-04-15 06:44:14 +00003106 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003107 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
3108 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00003109 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00003110 }
John Fastabend77162022012-04-15 06:43:56 +00003111 }
3112out:
3113 return err;
3114}
3115
Vlad Yasevich090096b2013-03-06 15:39:42 +00003116/**
3117 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3118 */
3119int ndo_dflt_fdb_del(struct ndmsg *ndm,
3120 struct nlattr *tb[],
3121 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003122 const unsigned char *addr, u16 vid)
Vlad Yasevich090096b2013-03-06 15:39:42 +00003123{
Alexander Duyckc8a89c42014-07-15 15:15:20 -07003124 int err = -EINVAL;
Vlad Yasevich090096b2013-03-06 15:39:42 +00003125
3126 /* If aging addresses are supported device will need to
3127 * implement its own handler for this.
3128 */
Sridhar Samudrala64535992013-08-08 15:19:48 -07003129 if (!(ndm->ndm_state & NUD_PERMANENT)) {
Vlad Yasevich090096b2013-03-06 15:39:42 +00003130 pr_info("%s: FDB only supports static addresses\n", dev->name);
Alexander Duyckc8a89c42014-07-15 15:15:20 -07003131 return err;
Vlad Yasevich090096b2013-03-06 15:39:42 +00003132 }
3133
3134 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3135 err = dev_uc_del(dev, addr);
3136 else if (is_multicast_ether_addr(addr))
3137 err = dev_mc_del(dev, addr);
Vlad Yasevich090096b2013-03-06 15:39:42 +00003138
3139 return err;
3140}
3141EXPORT_SYMBOL(ndo_dflt_fdb_del);
3142
David Ahernc21ef3e2017-04-16 09:48:24 -07003143static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
3144 struct netlink_ext_ack *extack)
John Fastabend77162022012-04-15 06:43:56 +00003145{
3146 struct net *net = sock_net(skb->sk);
3147 struct ndmsg *ndm;
Vlad Yasevich1690be62013-02-13 12:00:18 +00003148 struct nlattr *tb[NDA_MAX+1];
John Fastabend77162022012-04-15 06:43:56 +00003149 struct net_device *dev;
3150 int err = -EINVAL;
3151 __u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01003152 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00003153
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07003154 if (!netlink_capable(skb, CAP_NET_ADMIN))
Vlad Yasevich1690be62013-02-13 12:00:18 +00003155 return -EPERM;
3156
David Ahernc21ef3e2017-04-16 09:48:24 -07003157 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
Vlad Yasevich1690be62013-02-13 12:00:18 +00003158 if (err < 0)
3159 return err;
John Fastabend77162022012-04-15 06:43:56 +00003160
3161 ndm = nlmsg_data(nlh);
3162 if (ndm->ndm_ifindex == 0) {
3163 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
3164 return -EINVAL;
3165 }
3166
3167 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3168 if (dev == NULL) {
3169 pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
3170 return -ENODEV;
3171 }
3172
Vlad Yasevich1690be62013-02-13 12:00:18 +00003173 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3174 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
John Fastabend77162022012-04-15 06:43:56 +00003175 return -EINVAL;
3176 }
3177
Vlad Yasevich1690be62013-02-13 12:00:18 +00003178 addr = nla_data(tb[NDA_LLADDR]);
Vlad Yasevich1690be62013-02-13 12:00:18 +00003179
Jiri Pirkof6f64242014-11-28 14:34:15 +01003180 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
3181 if (err)
3182 return err;
3183
John Fastabend77162022012-04-15 06:43:56 +00003184 err = -EOPNOTSUPP;
3185
3186 /* Support fdb on master device the net/bridge default case */
3187 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3188 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003189 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3190 const struct net_device_ops *ops = br_dev->netdev_ops;
John Fastabend77162022012-04-15 06:43:56 +00003191
Jiri Pirko898e5062013-01-03 22:48:52 +00003192 if (ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003193 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00003194
3195 if (err)
3196 goto out;
3197 else
3198 ndm->ndm_flags &= ~NTF_MASTER;
3199 }
3200
3201 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00003202 if (ndm->ndm_flags & NTF_SELF) {
3203 if (dev->netdev_ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003204 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
3205 vid);
Vlad Yasevich090096b2013-03-06 15:39:42 +00003206 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01003207 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00003208
John Fastabend3ff661c2012-04-15 06:44:14 +00003209 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003210 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
3211 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00003212 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00003213 }
John Fastabend77162022012-04-15 06:43:56 +00003214 }
3215out:
3216 return err;
3217}
3218
John Fastabendd83b0602012-04-15 06:44:08 +00003219static int nlmsg_populate_fdb(struct sk_buff *skb,
3220 struct netlink_callback *cb,
3221 struct net_device *dev,
3222 int *idx,
3223 struct netdev_hw_addr_list *list)
3224{
3225 struct netdev_hw_addr *ha;
3226 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00003227 u32 portid, seq;
John Fastabendd83b0602012-04-15 06:44:08 +00003228
Eric W. Biederman15e47302012-09-07 20:12:54 +00003229 portid = NETLINK_CB(cb->skb).portid;
John Fastabendd83b0602012-04-15 06:44:08 +00003230 seq = cb->nlh->nlmsg_seq;
3231
3232 list_for_each_entry(ha, &list->list, list) {
Roopa Prabhud2976532016-08-30 21:56:45 -07003233 if (*idx < cb->args[2])
John Fastabendd83b0602012-04-15 06:44:08 +00003234 goto skip;
3235
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003236 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
John Fastabenda7a558f2012-11-01 16:23:10 +00003237 portid, seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003238 RTM_NEWNEIGH, NTF_SELF,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003239 NLM_F_MULTI, NUD_PERMANENT);
John Fastabendd83b0602012-04-15 06:44:08 +00003240 if (err < 0)
3241 return err;
3242skip:
3243 *idx += 1;
3244 }
3245 return 0;
3246}
3247
3248/**
Ben Hutchings2c530402012-07-10 10:55:09 +00003249 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
John Fastabendd83b0602012-04-15 06:44:08 +00003250 * @nlh: netlink message header
3251 * @dev: netdevice
3252 *
3253 * Default netdevice operation to dump the existing unicast address list.
John Fastabend91f3e7b2013-03-29 08:18:37 +00003254 * Returns number of addresses from list put in skb.
John Fastabendd83b0602012-04-15 06:44:08 +00003255 */
3256int ndo_dflt_fdb_dump(struct sk_buff *skb,
3257 struct netlink_callback *cb,
3258 struct net_device *dev,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04003259 struct net_device *filter_dev,
Roopa Prabhud2976532016-08-30 21:56:45 -07003260 int *idx)
John Fastabendd83b0602012-04-15 06:44:08 +00003261{
3262 int err;
3263
3264 netif_addr_lock_bh(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -07003265 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
John Fastabendd83b0602012-04-15 06:44:08 +00003266 if (err)
3267 goto out;
Zhang Shengju2934c9d2016-11-30 16:37:34 +08003268 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
John Fastabendd83b0602012-04-15 06:44:08 +00003269out:
3270 netif_addr_unlock_bh(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -07003271 return err;
John Fastabendd83b0602012-04-15 06:44:08 +00003272}
3273EXPORT_SYMBOL(ndo_dflt_fdb_dump);
3274
John Fastabend77162022012-04-15 06:43:56 +00003275static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
3276{
John Fastabend77162022012-04-15 06:43:56 +00003277 struct net_device *dev;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003278 struct nlattr *tb[IFLA_MAX+1];
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003279 struct net_device *br_dev = NULL;
3280 const struct net_device_ops *ops = NULL;
3281 const struct net_device_ops *cops = NULL;
3282 struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
3283 struct net *net = sock_net(skb->sk);
Roopa Prabhud2976532016-08-30 21:56:45 -07003284 struct hlist_head *head;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003285 int brport_idx = 0;
3286 int br_idx = 0;
Roopa Prabhud2976532016-08-30 21:56:45 -07003287 int h, s_h;
3288 int idx = 0, s_idx;
3289 int err = 0;
3290 int fidx = 0;
John Fastabend77162022012-04-15 06:43:56 +00003291
Alexander Potapenko0ff50e82017-05-23 13:20:28 +02003292 err = nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb,
3293 IFLA_MAX, ifla_policy, NULL);
3294 if (err < 0) {
3295 return -EINVAL;
3296 } else if (err == 0) {
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003297 if (tb[IFLA_MASTER])
3298 br_idx = nla_get_u32(tb[IFLA_MASTER]);
3299 }
John Fastabend77162022012-04-15 06:43:56 +00003300
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003301 brport_idx = ifm->ifi_index;
3302
3303 if (br_idx) {
3304 br_dev = __dev_get_by_index(net, br_idx);
3305 if (!br_dev)
3306 return -ENODEV;
3307
3308 ops = br_dev->netdev_ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003309 }
3310
Roopa Prabhud2976532016-08-30 21:56:45 -07003311 s_h = cb->args[0];
3312 s_idx = cb->args[1];
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003313
Roopa Prabhud2976532016-08-30 21:56:45 -07003314 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
3315 idx = 0;
3316 head = &net->dev_index_head[h];
3317 hlist_for_each_entry(dev, head, index_hlist) {
3318
3319 if (brport_idx && (dev->ifindex != brport_idx))
3320 continue;
3321
3322 if (!br_idx) { /* user did not specify a specific bridge */
3323 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3324 br_dev = netdev_master_upper_dev_get(dev);
3325 cops = br_dev->netdev_ops;
3326 }
3327 } else {
3328 if (dev != br_dev &&
3329 !(dev->priv_flags & IFF_BRIDGE_PORT))
3330 continue;
3331
3332 if (br_dev != netdev_master_upper_dev_get(dev) &&
3333 !(dev->priv_flags & IFF_EBRIDGE))
3334 continue;
3335 cops = ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003336 }
3337
Roopa Prabhud2976532016-08-30 21:56:45 -07003338 if (idx < s_idx)
3339 goto cont;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003340
Roopa Prabhud2976532016-08-30 21:56:45 -07003341 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3342 if (cops && cops->ndo_fdb_dump) {
3343 err = cops->ndo_fdb_dump(skb, cb,
3344 br_dev, dev,
3345 &fidx);
3346 if (err == -EMSGSIZE)
3347 goto out;
3348 }
3349 }
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003350
Roopa Prabhud2976532016-08-30 21:56:45 -07003351 if (dev->netdev_ops->ndo_fdb_dump)
3352 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
3353 dev, NULL,
3354 &fidx);
3355 else
3356 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
3357 &fidx);
3358 if (err == -EMSGSIZE)
3359 goto out;
3360
3361 cops = NULL;
3362
3363 /* reset fdb offset to 0 for rest of the interfaces */
3364 cb->args[2] = 0;
3365 fidx = 0;
3366cont:
3367 idx++;
John Fastabend77162022012-04-15 06:43:56 +00003368 }
John Fastabend77162022012-04-15 06:43:56 +00003369 }
John Fastabend77162022012-04-15 06:43:56 +00003370
Roopa Prabhud2976532016-08-30 21:56:45 -07003371out:
3372 cb->args[0] = h;
3373 cb->args[1] = idx;
3374 cb->args[2] = fidx;
3375
John Fastabend77162022012-04-15 06:43:56 +00003376 return skb->len;
3377}
3378
Scott Feldman2c3c0312014-11-28 14:34:25 +01003379static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
3380 unsigned int attrnum, unsigned int flag)
3381{
3382 if (mask & flag)
3383 return nla_put_u8(skb, attrnum, !!(flags & flag));
3384 return 0;
3385}
3386
John Fastabend815cccb2012-10-24 08:13:09 +00003387int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Scott Feldman2c3c0312014-11-28 14:34:25 +01003388 struct net_device *dev, u16 mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003389 u32 flags, u32 mask, int nlflags,
3390 u32 filter_mask,
3391 int (*vlan_fill)(struct sk_buff *skb,
3392 struct net_device *dev,
3393 u32 filter_mask))
John Fastabend815cccb2012-10-24 08:13:09 +00003394{
3395 struct nlmsghdr *nlh;
3396 struct ifinfomsg *ifm;
3397 struct nlattr *br_afspec;
Scott Feldman2c3c0312014-11-28 14:34:25 +01003398 struct nlattr *protinfo;
John Fastabend815cccb2012-10-24 08:13:09 +00003399 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Jiri Pirko898e5062013-01-03 22:48:52 +00003400 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003401 int err = 0;
John Fastabend815cccb2012-10-24 08:13:09 +00003402
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02003403 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
John Fastabend815cccb2012-10-24 08:13:09 +00003404 if (nlh == NULL)
3405 return -EMSGSIZE;
3406
3407 ifm = nlmsg_data(nlh);
3408 ifm->ifi_family = AF_BRIDGE;
3409 ifm->__ifi_pad = 0;
3410 ifm->ifi_type = dev->type;
3411 ifm->ifi_index = dev->ifindex;
3412 ifm->ifi_flags = dev_get_flags(dev);
3413 ifm->ifi_change = 0;
3414
3415
3416 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
3417 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
3418 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00003419 (br_dev &&
3420 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
John Fastabend815cccb2012-10-24 08:13:09 +00003421 (dev->addr_len &&
3422 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02003423 (dev->ifindex != dev_get_iflink(dev) &&
3424 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
John Fastabend815cccb2012-10-24 08:13:09 +00003425 goto nla_put_failure;
3426
3427 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
3428 if (!br_afspec)
3429 goto nla_put_failure;
3430
Roopa Prabhu1d460b92014-12-08 14:04:20 -08003431 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
John Fastabend815cccb2012-10-24 08:13:09 +00003432 nla_nest_cancel(skb, br_afspec);
3433 goto nla_put_failure;
3434 }
Roopa Prabhu1d460b92014-12-08 14:04:20 -08003435
3436 if (mode != BRIDGE_MODE_UNDEF) {
3437 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
3438 nla_nest_cancel(skb, br_afspec);
3439 goto nla_put_failure;
3440 }
3441 }
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003442 if (vlan_fill) {
3443 err = vlan_fill(skb, dev, filter_mask);
3444 if (err) {
3445 nla_nest_cancel(skb, br_afspec);
3446 goto nla_put_failure;
3447 }
3448 }
John Fastabend815cccb2012-10-24 08:13:09 +00003449 nla_nest_end(skb, br_afspec);
3450
Scott Feldman2c3c0312014-11-28 14:34:25 +01003451 protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
3452 if (!protinfo)
3453 goto nla_put_failure;
3454
3455 if (brport_nla_put_flag(skb, flags, mask,
3456 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
3457 brport_nla_put_flag(skb, flags, mask,
3458 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
3459 brport_nla_put_flag(skb, flags, mask,
3460 IFLA_BRPORT_FAST_LEAVE,
3461 BR_MULTICAST_FAST_LEAVE) ||
3462 brport_nla_put_flag(skb, flags, mask,
3463 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
3464 brport_nla_put_flag(skb, flags, mask,
3465 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
3466 brport_nla_put_flag(skb, flags, mask,
3467 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
3468 brport_nla_put_flag(skb, flags, mask,
3469 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
3470 brport_nla_put_flag(skb, flags, mask,
3471 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
3472 nla_nest_cancel(skb, protinfo);
3473 goto nla_put_failure;
3474 }
3475
3476 nla_nest_end(skb, protinfo);
3477
Johannes Berg053c0952015-01-16 22:09:00 +01003478 nlmsg_end(skb, nlh);
3479 return 0;
John Fastabend815cccb2012-10-24 08:13:09 +00003480nla_put_failure:
3481 nlmsg_cancel(skb, nlh);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003482 return err ? err : -EMSGSIZE;
John Fastabend815cccb2012-10-24 08:13:09 +00003483}
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003484EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
John Fastabend815cccb2012-10-24 08:13:09 +00003485
John Fastabende5a55a82012-10-24 08:12:57 +00003486static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
3487{
3488 struct net *net = sock_net(skb->sk);
3489 struct net_device *dev;
3490 int idx = 0;
3491 u32 portid = NETLINK_CB(cb->skb).portid;
3492 u32 seq = cb->nlh->nlmsg_seq;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00003493 u32 filter_mask = 0;
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003494 int err;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00003495
Thomas Grafaa68c202014-11-26 13:42:20 +01003496 if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
3497 struct nlattr *extfilt;
3498
3499 extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
3500 IFLA_EXT_MASK);
3501 if (extfilt) {
3502 if (nla_len(extfilt) < sizeof(filter_mask))
3503 return -EINVAL;
3504
3505 filter_mask = nla_get_u32(extfilt);
3506 }
3507 }
John Fastabende5a55a82012-10-24 08:12:57 +00003508
3509 rcu_read_lock();
3510 for_each_netdev_rcu(net, dev) {
3511 const struct net_device_ops *ops = dev->netdev_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00003512 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
John Fastabende5a55a82012-10-24 08:12:57 +00003513
Jiri Pirko898e5062013-01-03 22:48:52 +00003514 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003515 if (idx >= cb->args[0]) {
3516 err = br_dev->netdev_ops->ndo_bridge_getlink(
3517 skb, portid, seq, dev,
3518 filter_mask, NLM_F_MULTI);
David Ahernf6c57752017-05-15 23:19:17 -07003519 if (err < 0 && err != -EOPNOTSUPP) {
3520 if (likely(skb->len))
3521 break;
3522
3523 goto out_err;
3524 }
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003525 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00003526 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00003527 }
3528
3529 if (ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003530 if (idx >= cb->args[0]) {
3531 err = ops->ndo_bridge_getlink(skb, portid,
3532 seq, dev,
3533 filter_mask,
3534 NLM_F_MULTI);
David Ahernf6c57752017-05-15 23:19:17 -07003535 if (err < 0 && err != -EOPNOTSUPP) {
3536 if (likely(skb->len))
3537 break;
3538
3539 goto out_err;
3540 }
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003541 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00003542 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00003543 }
3544 }
David Ahernf6c57752017-05-15 23:19:17 -07003545 err = skb->len;
3546out_err:
John Fastabende5a55a82012-10-24 08:12:57 +00003547 rcu_read_unlock();
3548 cb->args[0] = idx;
3549
David Ahernf6c57752017-05-15 23:19:17 -07003550 return err;
John Fastabende5a55a82012-10-24 08:12:57 +00003551}
3552
John Fastabend2469ffd2012-10-24 08:13:03 +00003553static inline size_t bridge_nlmsg_size(void)
3554{
3555 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3556 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3557 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3558 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
3559 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
3560 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
3561 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
3562 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
3563 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
3564 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
3565 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
3566}
3567
Roopa Prabhu02dba432015-01-14 20:02:25 -08003568static int rtnl_bridge_notify(struct net_device *dev)
John Fastabend2469ffd2012-10-24 08:13:03 +00003569{
3570 struct net *net = dev_net(dev);
John Fastabend2469ffd2012-10-24 08:13:03 +00003571 struct sk_buff *skb;
3572 int err = -EOPNOTSUPP;
3573
Roopa Prabhu02dba432015-01-14 20:02:25 -08003574 if (!dev->netdev_ops->ndo_bridge_getlink)
3575 return 0;
3576
John Fastabend2469ffd2012-10-24 08:13:03 +00003577 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
3578 if (!skb) {
3579 err = -ENOMEM;
3580 goto errout;
3581 }
3582
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02003583 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
Roopa Prabhu02dba432015-01-14 20:02:25 -08003584 if (err < 0)
3585 goto errout;
John Fastabend2469ffd2012-10-24 08:13:03 +00003586
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08003587 if (!skb->len)
3588 goto errout;
3589
John Fastabend2469ffd2012-10-24 08:13:03 +00003590 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
3591 return 0;
3592errout:
3593 WARN_ON(err == -EMSGSIZE);
3594 kfree_skb(skb);
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08003595 if (err)
3596 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
John Fastabend2469ffd2012-10-24 08:13:03 +00003597 return err;
3598}
3599
David Ahernc21ef3e2017-04-16 09:48:24 -07003600static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3601 struct netlink_ext_ack *extack)
John Fastabende5a55a82012-10-24 08:12:57 +00003602{
3603 struct net *net = sock_net(skb->sk);
3604 struct ifinfomsg *ifm;
3605 struct net_device *dev;
John Fastabend2469ffd2012-10-24 08:13:03 +00003606 struct nlattr *br_spec, *attr = NULL;
3607 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02003608 u16 flags = 0;
John Fastabendc38e01b2012-11-02 16:32:36 +00003609 bool have_flags = false;
John Fastabende5a55a82012-10-24 08:12:57 +00003610
3611 if (nlmsg_len(nlh) < sizeof(*ifm))
3612 return -EINVAL;
3613
3614 ifm = nlmsg_data(nlh);
3615 if (ifm->ifi_family != AF_BRIDGE)
3616 return -EPFNOSUPPORT;
3617
3618 dev = __dev_get_by_index(net, ifm->ifi_index);
3619 if (!dev) {
3620 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3621 return -ENODEV;
3622 }
3623
John Fastabend2469ffd2012-10-24 08:13:03 +00003624 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3625 if (br_spec) {
3626 nla_for_each_nested(attr, br_spec, rem) {
3627 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01003628 if (nla_len(attr) < sizeof(flags))
3629 return -EINVAL;
3630
John Fastabendc38e01b2012-11-02 16:32:36 +00003631 have_flags = true;
John Fastabend2469ffd2012-10-24 08:13:03 +00003632 flags = nla_get_u16(attr);
3633 break;
3634 }
3635 }
3636 }
3637
3638 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003639 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3640
3641 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
John Fastabend2469ffd2012-10-24 08:13:03 +00003642 err = -EOPNOTSUPP;
3643 goto out;
3644 }
3645
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003646 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
John Fastabende5a55a82012-10-24 08:12:57 +00003647 if (err)
3648 goto out;
John Fastabend2469ffd2012-10-24 08:13:03 +00003649
3650 flags &= ~BRIDGE_FLAGS_MASTER;
John Fastabende5a55a82012-10-24 08:12:57 +00003651 }
3652
John Fastabend2469ffd2012-10-24 08:13:03 +00003653 if ((flags & BRIDGE_FLAGS_SELF)) {
3654 if (!dev->netdev_ops->ndo_bridge_setlink)
3655 err = -EOPNOTSUPP;
3656 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003657 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
3658 flags);
Roopa Prabhu02dba432015-01-14 20:02:25 -08003659 if (!err) {
John Fastabend2469ffd2012-10-24 08:13:03 +00003660 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08003661
3662 /* Generate event to notify upper layer of bridge
3663 * change
3664 */
3665 err = rtnl_bridge_notify(dev);
3666 }
John Fastabend2469ffd2012-10-24 08:13:03 +00003667 }
3668
John Fastabendc38e01b2012-11-02 16:32:36 +00003669 if (have_flags)
John Fastabend2469ffd2012-10-24 08:13:03 +00003670 memcpy(nla_data(attr), &flags, sizeof(flags));
John Fastabende5a55a82012-10-24 08:12:57 +00003671out:
3672 return err;
3673}
3674
David Ahernc21ef3e2017-04-16 09:48:24 -07003675static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
3676 struct netlink_ext_ack *extack)
Vlad Yasevich407af322013-02-13 12:00:12 +00003677{
3678 struct net *net = sock_net(skb->sk);
3679 struct ifinfomsg *ifm;
3680 struct net_device *dev;
3681 struct nlattr *br_spec, *attr = NULL;
3682 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02003683 u16 flags = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +00003684 bool have_flags = false;
3685
3686 if (nlmsg_len(nlh) < sizeof(*ifm))
3687 return -EINVAL;
3688
3689 ifm = nlmsg_data(nlh);
3690 if (ifm->ifi_family != AF_BRIDGE)
3691 return -EPFNOSUPPORT;
3692
3693 dev = __dev_get_by_index(net, ifm->ifi_index);
3694 if (!dev) {
3695 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3696 return -ENODEV;
3697 }
3698
3699 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3700 if (br_spec) {
3701 nla_for_each_nested(attr, br_spec, rem) {
3702 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01003703 if (nla_len(attr) < sizeof(flags))
3704 return -EINVAL;
3705
Vlad Yasevich407af322013-02-13 12:00:12 +00003706 have_flags = true;
3707 flags = nla_get_u16(attr);
3708 break;
3709 }
3710 }
3711 }
3712
Vlad Yasevich407af322013-02-13 12:00:12 +00003713 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3714 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3715
3716 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
3717 err = -EOPNOTSUPP;
3718 goto out;
3719 }
3720
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003721 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00003722 if (err)
3723 goto out;
3724
3725 flags &= ~BRIDGE_FLAGS_MASTER;
3726 }
3727
3728 if ((flags & BRIDGE_FLAGS_SELF)) {
3729 if (!dev->netdev_ops->ndo_bridge_dellink)
3730 err = -EOPNOTSUPP;
3731 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003732 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
3733 flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00003734
Roopa Prabhu02dba432015-01-14 20:02:25 -08003735 if (!err) {
Vlad Yasevich407af322013-02-13 12:00:12 +00003736 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08003737
3738 /* Generate event to notify upper layer of bridge
3739 * change
3740 */
3741 err = rtnl_bridge_notify(dev);
3742 }
Vlad Yasevich407af322013-02-13 12:00:12 +00003743 }
3744
3745 if (have_flags)
3746 memcpy(nla_data(attr), &flags, sizeof(flags));
Vlad Yasevich407af322013-02-13 12:00:12 +00003747out:
3748 return err;
3749}
3750
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003751static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
3752{
3753 return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
3754 (!idxattr || idxattr == attrid);
3755}
3756
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02003757#define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
3758static int rtnl_get_offload_stats_attr_size(int attr_id)
3759{
3760 switch (attr_id) {
3761 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
3762 return sizeof(struct rtnl_link_stats64);
3763 }
3764
3765 return 0;
3766}
3767
3768static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
3769 int *prividx)
3770{
3771 struct nlattr *attr = NULL;
3772 int attr_id, size;
3773 void *attr_data;
3774 int err;
3775
3776 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
3777 dev->netdev_ops->ndo_get_offload_stats))
3778 return -ENODATA;
3779
3780 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
3781 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
3782 if (attr_id < *prividx)
3783 continue;
3784
3785 size = rtnl_get_offload_stats_attr_size(attr_id);
3786 if (!size)
3787 continue;
3788
Or Gerlitz3df5b3c2016-11-22 23:09:54 +02003789 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02003790 continue;
3791
3792 attr = nla_reserve_64bit(skb, attr_id, size,
3793 IFLA_OFFLOAD_XSTATS_UNSPEC);
3794 if (!attr)
3795 goto nla_put_failure;
3796
3797 attr_data = nla_data(attr);
3798 memset(attr_data, 0, size);
3799 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
3800 attr_data);
3801 if (err)
3802 goto get_offload_stats_failure;
3803 }
3804
3805 if (!attr)
3806 return -ENODATA;
3807
3808 *prividx = 0;
3809 return 0;
3810
3811nla_put_failure:
3812 err = -EMSGSIZE;
3813get_offload_stats_failure:
3814 *prividx = attr_id;
3815 return err;
3816}
3817
3818static int rtnl_get_offload_stats_size(const struct net_device *dev)
3819{
3820 int nla_size = 0;
3821 int attr_id;
3822 int size;
3823
3824 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
3825 dev->netdev_ops->ndo_get_offload_stats))
3826 return 0;
3827
3828 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
3829 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
Or Gerlitz3df5b3c2016-11-22 23:09:54 +02003830 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02003831 continue;
3832 size = rtnl_get_offload_stats_attr_size(attr_id);
3833 nla_size += nla_total_size_64bit(size);
3834 }
3835
3836 if (nla_size != 0)
3837 nla_size += nla_total_size(0);
3838
3839 return nla_size;
3840}
3841
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003842static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
3843 int type, u32 pid, u32 seq, u32 change,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003844 unsigned int flags, unsigned int filter_mask,
3845 int *idxattr, int *prividx)
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003846{
3847 struct if_stats_msg *ifsm;
3848 struct nlmsghdr *nlh;
3849 struct nlattr *attr;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003850 int s_prividx = *prividx;
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02003851 int err;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003852
3853 ASSERT_RTNL();
3854
3855 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
3856 if (!nlh)
3857 return -EMSGSIZE;
3858
3859 ifsm = nlmsg_data(nlh);
Nikolay Aleksandrovce024f42017-10-03 13:20:48 +03003860 ifsm->family = PF_UNSPEC;
3861 ifsm->pad1 = 0;
3862 ifsm->pad2 = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003863 ifsm->ifindex = dev->ifindex;
3864 ifsm->filter_mask = filter_mask;
3865
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003866 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003867 struct rtnl_link_stats64 *sp;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003868
Nicolas Dichtel58414d32016-04-21 18:58:25 +02003869 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
3870 sizeof(struct rtnl_link_stats64),
3871 IFLA_STATS_UNSPEC);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003872 if (!attr)
3873 goto nla_put_failure;
3874
3875 sp = nla_data(attr);
3876 dev_get_stats(dev, sp);
3877 }
3878
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02003879 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
3880 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
3881
3882 if (ops && ops->fill_linkxstats) {
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02003883 *idxattr = IFLA_STATS_LINK_XSTATS;
3884 attr = nla_nest_start(skb,
3885 IFLA_STATS_LINK_XSTATS);
3886 if (!attr)
3887 goto nla_put_failure;
3888
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02003889 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
3890 nla_nest_end(skb, attr);
3891 if (err)
3892 goto nla_put_failure;
3893 *idxattr = 0;
3894 }
3895 }
3896
3897 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
3898 *idxattr)) {
3899 const struct rtnl_link_ops *ops = NULL;
3900 const struct net_device *master;
3901
3902 master = netdev_master_upper_dev_get(dev);
3903 if (master)
3904 ops = master->rtnl_link_ops;
3905 if (ops && ops->fill_linkxstats) {
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02003906 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
3907 attr = nla_nest_start(skb,
3908 IFLA_STATS_LINK_XSTATS_SLAVE);
3909 if (!attr)
3910 goto nla_put_failure;
3911
3912 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02003913 nla_nest_end(skb, attr);
3914 if (err)
3915 goto nla_put_failure;
3916 *idxattr = 0;
3917 }
3918 }
3919
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02003920 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
3921 *idxattr)) {
3922 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
3923 attr = nla_nest_start(skb, IFLA_STATS_LINK_OFFLOAD_XSTATS);
3924 if (!attr)
3925 goto nla_put_failure;
3926
3927 err = rtnl_get_offload_stats(skb, dev, prividx);
3928 if (err == -ENODATA)
3929 nla_nest_cancel(skb, attr);
3930 else
3931 nla_nest_end(skb, attr);
3932
3933 if (err && err != -ENODATA)
3934 goto nla_put_failure;
3935 *idxattr = 0;
3936 }
3937
Robert Shearmanaefb4d42017-01-16 14:16:36 +00003938 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
3939 struct rtnl_af_ops *af_ops;
3940
3941 *idxattr = IFLA_STATS_AF_SPEC;
3942 attr = nla_nest_start(skb, IFLA_STATS_AF_SPEC);
3943 if (!attr)
3944 goto nla_put_failure;
3945
3946 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
3947 if (af_ops->fill_stats_af) {
3948 struct nlattr *af;
3949 int err;
3950
3951 af = nla_nest_start(skb, af_ops->family);
3952 if (!af)
3953 goto nla_put_failure;
3954
3955 err = af_ops->fill_stats_af(skb, dev);
3956
3957 if (err == -ENODATA)
3958 nla_nest_cancel(skb, af);
3959 else if (err < 0)
3960 goto nla_put_failure;
3961
3962 nla_nest_end(skb, af);
3963 }
3964 }
3965
3966 nla_nest_end(skb, attr);
3967
3968 *idxattr = 0;
3969 }
3970
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003971 nlmsg_end(skb, nlh);
3972
3973 return 0;
3974
3975nla_put_failure:
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003976 /* not a multi message or no progress mean a real error */
3977 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
3978 nlmsg_cancel(skb, nlh);
3979 else
3980 nlmsg_end(skb, nlh);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003981
3982 return -EMSGSIZE;
3983}
3984
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003985static size_t if_nlmsg_stats_size(const struct net_device *dev,
3986 u32 filter_mask)
3987{
3988 size_t size = 0;
3989
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003990 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003991 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
3992
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02003993 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
3994 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02003995 int attr = IFLA_STATS_LINK_XSTATS;
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02003996
3997 if (ops && ops->get_linkxstats_size) {
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02003998 size += nla_total_size(ops->get_linkxstats_size(dev,
3999 attr));
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004000 /* for IFLA_STATS_LINK_XSTATS */
4001 size += nla_total_size(0);
4002 }
4003 }
4004
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004005 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
4006 struct net_device *_dev = (struct net_device *)dev;
4007 const struct rtnl_link_ops *ops = NULL;
4008 const struct net_device *master;
4009
4010 /* netdev_master_upper_dev_get can't take const */
4011 master = netdev_master_upper_dev_get(_dev);
4012 if (master)
4013 ops = master->rtnl_link_ops;
4014 if (ops && ops->get_linkxstats_size) {
4015 int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
4016
4017 size += nla_total_size(ops->get_linkxstats_size(dev,
4018 attr));
4019 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
4020 size += nla_total_size(0);
4021 }
4022 }
4023
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004024 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
4025 size += rtnl_get_offload_stats_size(dev);
4026
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004027 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
4028 struct rtnl_af_ops *af_ops;
4029
4030 /* for IFLA_STATS_AF_SPEC */
4031 size += nla_total_size(0);
4032
4033 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
4034 if (af_ops->get_stats_af_size) {
4035 size += nla_total_size(
4036 af_ops->get_stats_af_size(dev));
4037
4038 /* for AF_* */
4039 size += nla_total_size(0);
4040 }
4041 }
4042 }
4043
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004044 return size;
4045}
4046
David Ahernc21ef3e2017-04-16 09:48:24 -07004047static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
4048 struct netlink_ext_ack *extack)
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004049{
4050 struct net *net = sock_net(skb->sk);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004051 struct net_device *dev = NULL;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004052 int idxattr = 0, prividx = 0;
4053 struct if_stats_msg *ifsm;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004054 struct sk_buff *nskb;
4055 u32 filter_mask;
4056 int err;
4057
Mathias Krause4775cc12016-12-28 17:52:15 +01004058 if (nlmsg_len(nlh) < sizeof(*ifsm))
4059 return -EINVAL;
4060
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004061 ifsm = nlmsg_data(nlh);
4062 if (ifsm->ifindex > 0)
4063 dev = __dev_get_by_index(net, ifsm->ifindex);
4064 else
4065 return -EINVAL;
4066
4067 if (!dev)
4068 return -ENODEV;
4069
4070 filter_mask = ifsm->filter_mask;
4071 if (!filter_mask)
4072 return -EINVAL;
4073
4074 nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
4075 if (!nskb)
4076 return -ENOBUFS;
4077
4078 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
4079 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004080 0, filter_mask, &idxattr, &prividx);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004081 if (err < 0) {
4082 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
4083 WARN_ON(err == -EMSGSIZE);
4084 kfree_skb(nskb);
4085 } else {
4086 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
4087 }
4088
4089 return err;
4090}
4091
4092static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
4093{
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004094 int h, s_h, err, s_idx, s_idxattr, s_prividx;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004095 struct net *net = sock_net(skb->sk);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004096 unsigned int flags = NLM_F_MULTI;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004097 struct if_stats_msg *ifsm;
4098 struct hlist_head *head;
4099 struct net_device *dev;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004100 u32 filter_mask = 0;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004101 int idx = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004102
4103 s_h = cb->args[0];
4104 s_idx = cb->args[1];
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004105 s_idxattr = cb->args[2];
4106 s_prividx = cb->args[3];
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004107
4108 cb->seq = net->dev_base_seq;
4109
Mathias Krause4775cc12016-12-28 17:52:15 +01004110 if (nlmsg_len(cb->nlh) < sizeof(*ifsm))
4111 return -EINVAL;
4112
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004113 ifsm = nlmsg_data(cb->nlh);
4114 filter_mask = ifsm->filter_mask;
4115 if (!filter_mask)
4116 return -EINVAL;
4117
4118 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4119 idx = 0;
4120 head = &net->dev_index_head[h];
4121 hlist_for_each_entry(dev, head, index_hlist) {
4122 if (idx < s_idx)
4123 goto cont;
4124 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
4125 NETLINK_CB(cb->skb).portid,
4126 cb->nlh->nlmsg_seq, 0,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004127 flags, filter_mask,
4128 &s_idxattr, &s_prividx);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004129 /* If we ran out of room on the first message,
4130 * we're in trouble
4131 */
4132 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
4133
4134 if (err < 0)
4135 goto out;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004136 s_prividx = 0;
4137 s_idxattr = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004138 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
4139cont:
4140 idx++;
4141 }
4142 }
4143out:
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004144 cb->args[3] = s_prividx;
4145 cb->args[2] = s_idxattr;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004146 cb->args[1] = idx;
4147 cb->args[0] = h;
4148
4149 return skb->len;
4150}
4151
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152/* Process one rtnetlink message. */
4153
Johannes Berg2d4bc932017-04-12 14:34:04 +02004154static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
4155 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004157 struct net *net = sock_net(skb->sk);
Florian Westphal6853dd42017-08-09 20:41:51 +02004158 struct rtnl_link *handlers;
4159 int err = -EOPNOTSUPP;
Thomas Grafe2849862007-03-22 11:48:11 -07004160 rtnl_doit_func doit;
Florian Westphal62256f92017-08-09 20:41:52 +02004161 unsigned int flags;
Alexander Kuleshov617cfc72016-01-10 21:26:57 +06004162 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 int family;
4164 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 if (type > RTM_MAX)
Thomas Graf038890f2007-04-05 14:35:52 -07004168 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169
4170 type -= RTM_BASE;
4171
4172 /* All the messages must have at least 1 byte length */
Hong zhi guo573ce262013-03-27 06:47:04 +00004173 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 return 0;
4175
Hong zhi guo573ce262013-03-27 06:47:04 +00004176 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 kind = type&3;
4178
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07004179 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07004180 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181
Florian Westphal5c2bb9b2017-08-10 16:52:58 +02004182 if (family >= ARRAY_SIZE(rtnl_msg_handlers))
Florian Westphal6853dd42017-08-09 20:41:51 +02004183 family = PF_UNSPEC;
4184
4185 rcu_read_lock();
4186 handlers = rcu_dereference(rtnl_msg_handlers[family]);
4187 if (!handlers) {
4188 family = PF_UNSPEC;
4189 handlers = rcu_dereference(rtnl_msg_handlers[family]);
4190 }
4191
David S. Millerb8f3ab42011-01-18 12:40:38 -08004192 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004193 struct sock *rtnl;
Thomas Grafe2849862007-03-22 11:48:11 -07004194 rtnl_dumpit_func dumpit;
Greg Rosec7ac8672011-06-10 01:27:09 +00004195 u16 min_dump_alloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196
Florian Westphal6853dd42017-08-09 20:41:51 +02004197 dumpit = READ_ONCE(handlers[type].dumpit);
4198 if (!dumpit) {
4199 family = PF_UNSPEC;
4200 handlers = rcu_dereference(rtnl_msg_handlers[PF_UNSPEC]);
4201 if (!handlers)
4202 goto err_unlock;
Florian Westphal0cc09022017-08-09 20:41:50 +02004203
Florian Westphal6853dd42017-08-09 20:41:51 +02004204 dumpit = READ_ONCE(handlers[type].dumpit);
4205 if (!dumpit)
4206 goto err_unlock;
4207 }
Florian Westphale1fa6d22017-08-09 20:41:47 +02004208
Florian Westphal019a3162017-08-09 20:41:49 +02004209 refcount_inc(&rtnl_msg_handlers_ref[family]);
4210
Florian Westphal5c2bb9b2017-08-10 16:52:58 +02004211 if (type == RTM_GETLINK - RTM_BASE)
Florian Westphale1fa6d22017-08-09 20:41:47 +02004212 min_dump_alloc = rtnl_calcit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213
Florian Westphal6853dd42017-08-09 20:41:51 +02004214 rcu_read_unlock();
4215
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004216 rtnl = net->rtnl;
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00004217 {
4218 struct netlink_dump_control c = {
4219 .dump = dumpit,
4220 .min_dump_alloc = min_dump_alloc,
4221 };
4222 err = netlink_dump_start(rtnl, skb, nlh, &c);
4223 }
Florian Westphal019a3162017-08-09 20:41:49 +02004224 refcount_dec(&rtnl_msg_handlers_ref[family]);
Eric Dumazet2907c352011-05-25 07:34:04 +00004225 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 }
4227
Florian Westphal8caa38b2017-08-10 16:53:01 +02004228 doit = READ_ONCE(handlers[type].doit);
4229 if (!doit) {
4230 family = PF_UNSPEC;
4231 handlers = rcu_dereference(rtnl_msg_handlers[family]);
4232 }
4233
Florian Westphal62256f92017-08-09 20:41:52 +02004234 flags = READ_ONCE(handlers[type].flags);
4235 if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
4236 refcount_inc(&rtnl_msg_handlers_ref[family]);
4237 doit = READ_ONCE(handlers[type].doit);
4238 rcu_read_unlock();
4239 if (doit)
4240 err = doit(skb, nlh, extack);
4241 refcount_dec(&rtnl_msg_handlers_ref[family]);
4242 return err;
4243 }
4244
Florian Westphal6853dd42017-08-09 20:41:51 +02004245 rcu_read_unlock();
4246
Florian Westphal0cc09022017-08-09 20:41:50 +02004247 rtnl_lock();
Florian Westphal6853dd42017-08-09 20:41:51 +02004248 handlers = rtnl_dereference(rtnl_msg_handlers[family]);
4249 if (handlers) {
4250 doit = READ_ONCE(handlers[type].doit);
4251 if (doit)
4252 err = doit(skb, nlh, extack);
4253 }
Florian Westphal0cc09022017-08-09 20:41:50 +02004254 rtnl_unlock();
Florian Westphal0cc09022017-08-09 20:41:50 +02004255 return err;
4256
4257err_unlock:
Florian Westphal6853dd42017-08-09 20:41:51 +02004258 rcu_read_unlock();
Florian Westphal0cc09022017-08-09 20:41:50 +02004259 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260}
4261
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07004262static void rtnetlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07004264 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265}
4266
Julien Gomes5f729ea2017-06-20 13:54:16 -07004267static int rtnetlink_bind(struct net *net, int group)
4268{
4269 switch (group) {
4270 case RTNLGRP_IPV4_MROUTE_R:
4271 case RTNLGRP_IPV6_MROUTE_R:
4272 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
4273 return -EPERM;
4274 break;
4275 }
4276 return 0;
4277}
4278
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
4280{
Jiri Pirko351638e2013-05-28 01:30:21 +00004281 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02004282
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 switch (event) {
Vlad Yasevich5138e862017-04-04 09:23:41 -04004284 case NETDEV_REBOOT:
Xin Long8a212582017-10-15 18:13:41 +08004285 case NETDEV_CHANGEMTU:
David Ahern37536542017-07-19 10:22:40 -07004286 case NETDEV_CHANGEADDR:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004287 case NETDEV_CHANGENAME:
4288 case NETDEV_FEAT_CHANGE:
4289 case NETDEV_BONDING_FAILOVER:
Xin Longe6e66592017-10-15 18:13:43 +08004290 case NETDEV_POST_TYPE_CHANGE:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004291 case NETDEV_NOTIFY_PEERS:
Xin Longdc709f32017-10-15 18:13:44 +08004292 case NETDEV_CHANGEUPPER:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004293 case NETDEV_RESEND_IGMP:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004294 case NETDEV_CHANGEINFODATA:
Xin Longebdcf042017-10-15 18:13:42 +08004295 case NETDEV_CHANGE_TX_QUEUE_LEN:
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04004296 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
4297 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 break;
4299 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 break;
4301 }
4302 return NOTIFY_DONE;
4303}
4304
4305static struct notifier_block rtnetlink_dev_notifier = {
4306 .notifier_call = rtnetlink_event,
4307};
4308
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004309
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004310static int __net_init rtnetlink_net_init(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004311{
4312 struct sock *sk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00004313 struct netlink_kernel_cfg cfg = {
4314 .groups = RTNLGRP_MAX,
4315 .input = rtnetlink_rcv,
4316 .cb_mutex = &rtnl_mutex,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00004317 .flags = NL_CFG_F_NONROOT_RECV,
Julien Gomes5f729ea2017-06-20 13:54:16 -07004318 .bind = rtnetlink_bind,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00004319 };
4320
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00004321 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004322 if (!sk)
4323 return -ENOMEM;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004324 net->rtnl = sk;
4325 return 0;
4326}
4327
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004328static void __net_exit rtnetlink_net_exit(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004329{
Denis V. Lunev775516b2008-01-18 23:55:19 -08004330 netlink_kernel_release(net->rtnl);
4331 net->rtnl = NULL;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004332}
4333
4334static struct pernet_operations rtnetlink_net_ops = {
4335 .init = rtnetlink_net_init,
4336 .exit = rtnetlink_net_exit,
4337};
4338
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339void __init rtnetlink_init(void)
4340{
Florian Westphald38a6512017-08-10 16:53:00 +02004341 int i;
4342
4343 for (i = 0; i < ARRAY_SIZE(rtnl_msg_handlers_ref); i++)
4344 refcount_set(&rtnl_msg_handlers_ref[i], 1);
4345
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004346 if (register_pernet_subsys(&rtnetlink_net_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 panic("rtnetlink_init: cannot initialize rtnetlink\n");
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004348
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 register_netdevice_notifier(&rtnetlink_dev_notifier);
Thomas Graf340d17f2007-03-22 11:49:22 -07004350
Greg Rosec7ac8672011-06-10 01:27:09 +00004351 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
Florian Westphalb97bac62017-08-09 20:41:48 +02004352 rtnl_dump_ifinfo, 0);
4353 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
4354 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
4355 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
Thomas Graf687ad8c2007-03-22 11:59:42 -07004356
Florian Westphalb97bac62017-08-09 20:41:48 +02004357 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
4358 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
4359 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
John Fastabend77162022012-04-15 06:43:56 +00004360
Florian Westphalb97bac62017-08-09 20:41:48 +02004361 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
4362 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
4363 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, 0);
John Fastabende5a55a82012-10-24 08:12:57 +00004364
Florian Westphalb97bac62017-08-09 20:41:48 +02004365 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
4366 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
4367 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004368
4369 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
Florian Westphalb97bac62017-08-09 20:41:48 +02004370 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371}