blob: 087f2434813a8b1fb0b4129643c1cae61c53dff6 [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 Westphal019a3162017-08-09 20:41:49 +0200265 while (refcount_read(&rtnl_msg_handlers_ref[protocol]) > 0)
266 schedule();
267 kfree(handlers);
Thomas Grafe2849862007-03-22 11:48:11 -0700268}
Thomas Grafe2849862007-03-22 11:48:11 -0700269EXPORT_SYMBOL_GPL(rtnl_unregister_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Patrick McHardy38f7b872007-06-13 12:03:51 -0700271static LIST_HEAD(link_ops);
272
Eric Dumazetc63044f2011-12-13 11:38:00 +0000273static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
274{
275 const struct rtnl_link_ops *ops;
276
277 list_for_each_entry(ops, &link_ops, list) {
278 if (!strcmp(ops->kind, kind))
279 return ops;
280 }
281 return NULL;
282}
283
Patrick McHardy38f7b872007-06-13 12:03:51 -0700284/**
285 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
286 * @ops: struct rtnl_link_ops * to register
287 *
288 * The caller must hold the rtnl_mutex. This function should be used
289 * by drivers that create devices during module initialization. It
290 * must be called before registering the devices.
291 *
292 * Returns 0 on success or a negative error code.
293 */
294int __rtnl_link_register(struct rtnl_link_ops *ops)
295{
Eric Dumazetc63044f2011-12-13 11:38:00 +0000296 if (rtnl_link_ops_get(ops->kind))
297 return -EEXIST;
298
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +0200299 /* The check for setup is here because if ops
300 * does not have that filled up, it is not possible
301 * to use the ops for creating device. So do not
302 * fill up dellink as well. That disables rtnl_dellink.
303 */
304 if (ops->setup && !ops->dellink)
Eric Dumazet23289a32009-10-27 07:06:36 +0000305 ops->dellink = unregister_netdevice_queue;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700306
Patrick McHardy38f7b872007-06-13 12:03:51 -0700307 list_add_tail(&ops->list, &link_ops);
308 return 0;
309}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700310EXPORT_SYMBOL_GPL(__rtnl_link_register);
311
312/**
313 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
314 * @ops: struct rtnl_link_ops * to register
315 *
316 * Returns 0 on success or a negative error code.
317 */
318int rtnl_link_register(struct rtnl_link_ops *ops)
319{
320 int err;
321
322 rtnl_lock();
323 err = __rtnl_link_register(ops);
324 rtnl_unlock();
325 return err;
326}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700327EXPORT_SYMBOL_GPL(rtnl_link_register);
328
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700329static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
330{
331 struct net_device *dev;
Eric Dumazet23289a32009-10-27 07:06:36 +0000332 LIST_HEAD(list_kill);
333
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700334 for_each_netdev(net, dev) {
Eric Dumazet23289a32009-10-27 07:06:36 +0000335 if (dev->rtnl_link_ops == ops)
336 ops->dellink(dev, &list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700337 }
Eric Dumazet23289a32009-10-27 07:06:36 +0000338 unregister_netdevice_many(&list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700339}
340
Patrick McHardy38f7b872007-06-13 12:03:51 -0700341/**
342 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
343 * @ops: struct rtnl_link_ops * to unregister
344 *
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700345 * The caller must hold the rtnl_mutex.
Patrick McHardy38f7b872007-06-13 12:03:51 -0700346 */
347void __rtnl_link_unregister(struct rtnl_link_ops *ops)
348{
Eric W. Biederman881d9662007-09-17 11:56:21 -0700349 struct net *net;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700350
Eric W. Biederman881d9662007-09-17 11:56:21 -0700351 for_each_net(net) {
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700352 __rtnl_kill_links(net, ops);
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700353 }
Patrick McHardy38f7b872007-06-13 12:03:51 -0700354 list_del(&ops->list);
355}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700356EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
357
Cong Wang200b9162014-05-12 15:11:20 -0700358/* Return with the rtnl_lock held when there are no network
359 * devices unregistering in any network namespace.
360 */
361static void rtnl_lock_unregistering_all(void)
362{
363 struct net *net;
364 bool unregistering;
Peter Zijlstraff960a72014-10-29 17:04:56 +0100365 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Cong Wang200b9162014-05-12 15:11:20 -0700366
Peter Zijlstraff960a72014-10-29 17:04:56 +0100367 add_wait_queue(&netdev_unregistering_wq, &wait);
Cong Wang200b9162014-05-12 15:11:20 -0700368 for (;;) {
Cong Wang200b9162014-05-12 15:11:20 -0700369 unregistering = false;
370 rtnl_lock();
371 for_each_net(net) {
372 if (net->dev_unreg_count > 0) {
373 unregistering = true;
374 break;
375 }
376 }
377 if (!unregistering)
378 break;
379 __rtnl_unlock();
Peter Zijlstraff960a72014-10-29 17:04:56 +0100380
381 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Cong Wang200b9162014-05-12 15:11:20 -0700382 }
Peter Zijlstraff960a72014-10-29 17:04:56 +0100383 remove_wait_queue(&netdev_unregistering_wq, &wait);
Cong Wang200b9162014-05-12 15:11:20 -0700384}
385
Patrick McHardy38f7b872007-06-13 12:03:51 -0700386/**
387 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
388 * @ops: struct rtnl_link_ops * to unregister
389 */
390void rtnl_link_unregister(struct rtnl_link_ops *ops)
391{
Cong Wang200b9162014-05-12 15:11:20 -0700392 /* Close the race with cleanup_net() */
393 mutex_lock(&net_mutex);
394 rtnl_lock_unregistering_all();
Patrick McHardy38f7b872007-06-13 12:03:51 -0700395 __rtnl_link_unregister(ops);
396 rtnl_unlock();
Cong Wang200b9162014-05-12 15:11:20 -0700397 mutex_unlock(&net_mutex);
Patrick McHardy38f7b872007-06-13 12:03:51 -0700398}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700399EXPORT_SYMBOL_GPL(rtnl_link_unregister);
400
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100401static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
402{
403 struct net_device *master_dev;
404 const struct rtnl_link_ops *ops;
Florian Westphal8515ae32017-08-10 16:52:59 +0200405 size_t size = 0;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100406
Florian Westphal8515ae32017-08-10 16:52:59 +0200407 rcu_read_lock();
408
409 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100410 if (!master_dev)
Florian Westphal8515ae32017-08-10 16:52:59 +0200411 goto out;
412
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100413 ops = master_dev->rtnl_link_ops;
Fernando Luis Vazquez Cao6049f252014-02-04 19:35:02 +0900414 if (!ops || !ops->get_slave_size)
Florian Westphal8515ae32017-08-10 16:52:59 +0200415 goto out;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100416 /* IFLA_INFO_SLAVE_DATA + nested data */
Florian Westphal8515ae32017-08-10 16:52:59 +0200417 size = nla_total_size(sizeof(struct nlattr)) +
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100418 ops->get_slave_size(master_dev, dev);
Florian Westphal8515ae32017-08-10 16:52:59 +0200419
420out:
421 rcu_read_unlock();
422 return size;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100423}
424
Patrick McHardy38f7b872007-06-13 12:03:51 -0700425static size_t rtnl_link_get_size(const struct net_device *dev)
426{
427 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
428 size_t size;
429
430 if (!ops)
431 return 0;
432
Thomas Graf369cf772010-11-11 15:47:59 +0000433 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
434 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700435
436 if (ops->get_size)
437 /* IFLA_INFO_DATA + nested data */
Thomas Graf369cf772010-11-11 15:47:59 +0000438 size += nla_total_size(sizeof(struct nlattr)) +
Patrick McHardy38f7b872007-06-13 12:03:51 -0700439 ops->get_size(dev);
440
441 if (ops->get_xstats_size)
Thomas Graf369cf772010-11-11 15:47:59 +0000442 /* IFLA_INFO_XSTATS */
443 size += nla_total_size(ops->get_xstats_size(dev));
Patrick McHardy38f7b872007-06-13 12:03:51 -0700444
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100445 size += rtnl_link_get_slave_info_data_size(dev);
446
Patrick McHardy38f7b872007-06-13 12:03:51 -0700447 return size;
448}
449
Thomas Graff8ff1822010-11-16 04:30:14 +0000450static LIST_HEAD(rtnl_af_ops);
451
452static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
453{
454 const struct rtnl_af_ops *ops;
455
456 list_for_each_entry(ops, &rtnl_af_ops, list) {
457 if (ops->family == family)
458 return ops;
459 }
460
461 return NULL;
462}
463
464/**
Thomas Graff8ff1822010-11-16 04:30:14 +0000465 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
466 * @ops: struct rtnl_af_ops * to register
467 *
468 * Returns 0 on success or a negative error code.
469 */
stephen hemminger3678a9d2013-12-30 10:41:32 -0800470void rtnl_af_register(struct rtnl_af_ops *ops)
Thomas Graff8ff1822010-11-16 04:30:14 +0000471{
Thomas Graff8ff1822010-11-16 04:30:14 +0000472 rtnl_lock();
stephen hemminger3678a9d2013-12-30 10:41:32 -0800473 list_add_tail(&ops->list, &rtnl_af_ops);
Thomas Graff8ff1822010-11-16 04:30:14 +0000474 rtnl_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +0000475}
476EXPORT_SYMBOL_GPL(rtnl_af_register);
477
478/**
479 * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
480 * @ops: struct rtnl_af_ops * to unregister
481 *
482 * The caller must hold the rtnl_mutex.
483 */
484void __rtnl_af_unregister(struct rtnl_af_ops *ops)
485{
486 list_del(&ops->list);
487}
488EXPORT_SYMBOL_GPL(__rtnl_af_unregister);
489
490/**
491 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
492 * @ops: struct rtnl_af_ops * to unregister
493 */
494void rtnl_af_unregister(struct rtnl_af_ops *ops)
495{
496 rtnl_lock();
497 __rtnl_af_unregister(ops);
498 rtnl_unlock();
499}
500EXPORT_SYMBOL_GPL(rtnl_af_unregister);
501
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700502static size_t rtnl_link_get_af_size(const struct net_device *dev,
503 u32 ext_filter_mask)
Thomas Graff8ff1822010-11-16 04:30:14 +0000504{
505 struct rtnl_af_ops *af_ops;
506 size_t size;
507
508 /* IFLA_AF_SPEC */
509 size = nla_total_size(sizeof(struct nlattr));
510
511 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
512 if (af_ops->get_link_af_size) {
513 /* AF_* + nested data */
514 size += nla_total_size(sizeof(struct nlattr)) +
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700515 af_ops->get_link_af_size(dev, ext_filter_mask);
Thomas Graff8ff1822010-11-16 04:30:14 +0000516 }
517 }
518
519 return size;
520}
521
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100522static bool rtnl_have_link_slave_info(const struct net_device *dev)
523{
524 struct net_device *master_dev;
525
526 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
Jiri Pirko813f0202014-01-23 19:19:21 +0100527 if (master_dev && master_dev->rtnl_link_ops)
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100528 return true;
529 return false;
530}
531
532static int rtnl_link_slave_info_fill(struct sk_buff *skb,
533 const struct net_device *dev)
534{
535 struct net_device *master_dev;
536 const struct rtnl_link_ops *ops;
537 struct nlattr *slave_data;
538 int err;
539
540 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
541 if (!master_dev)
542 return 0;
543 ops = master_dev->rtnl_link_ops;
544 if (!ops)
545 return 0;
546 if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
547 return -EMSGSIZE;
548 if (ops->fill_slave_info) {
549 slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA);
550 if (!slave_data)
551 return -EMSGSIZE;
552 err = ops->fill_slave_info(skb, master_dev, dev);
553 if (err < 0)
554 goto err_cancel_slave_data;
555 nla_nest_end(skb, slave_data);
556 }
557 return 0;
558
559err_cancel_slave_data:
560 nla_nest_cancel(skb, slave_data);
561 return err;
562}
563
564static int rtnl_link_info_fill(struct sk_buff *skb,
565 const struct net_device *dev)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700566{
567 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100568 struct nlattr *data;
569 int err;
570
571 if (!ops)
572 return 0;
573 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
574 return -EMSGSIZE;
575 if (ops->fill_xstats) {
576 err = ops->fill_xstats(skb, dev);
577 if (err < 0)
578 return err;
579 }
580 if (ops->fill_info) {
581 data = nla_nest_start(skb, IFLA_INFO_DATA);
582 if (data == NULL)
583 return -EMSGSIZE;
584 err = ops->fill_info(skb, dev);
585 if (err < 0)
586 goto err_cancel_data;
587 nla_nest_end(skb, data);
588 }
589 return 0;
590
591err_cancel_data:
592 nla_nest_cancel(skb, data);
593 return err;
594}
595
596static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
597{
598 struct nlattr *linkinfo;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700599 int err = -EMSGSIZE;
600
601 linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
602 if (linkinfo == NULL)
603 goto out;
604
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100605 err = rtnl_link_info_fill(skb, dev);
606 if (err < 0)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700607 goto err_cancel_link;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100608
609 err = rtnl_link_slave_info_fill(skb, dev);
610 if (err < 0)
611 goto err_cancel_link;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700612
613 nla_nest_end(skb, linkinfo);
614 return 0;
615
Patrick McHardy38f7b872007-06-13 12:03:51 -0700616err_cancel_link:
617 nla_nest_cancel(skb, linkinfo);
618out:
619 return err;
620}
621
Eric Dumazet95c96172012-04-15 05:58:06 +0000622int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800624 struct sock *rtnl = net->rtnl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 int err = 0;
626
Patrick McHardyac6d4392005-08-14 19:29:52 -0700627 NETLINK_CB(skb).dst_group = group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if (echo)
Reshetova, Elena63354792017-06-30 13:07:58 +0300629 refcount_inc(&skb->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
631 if (echo)
632 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
633 return err;
634}
635
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800636int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
Thomas Graf2942e902006-08-15 00:30:25 -0700637{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800638 struct sock *rtnl = net->rtnl;
639
Thomas Graf2942e902006-08-15 00:30:25 -0700640 return nlmsg_unicast(rtnl, skb, pid);
641}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800642EXPORT_SYMBOL(rtnl_unicast);
Thomas Graf2942e902006-08-15 00:30:25 -0700643
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800644void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
645 struct nlmsghdr *nlh, gfp_t flags)
Thomas Graf97676b62006-08-15 00:31:41 -0700646{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800647 struct sock *rtnl = net->rtnl;
Thomas Graf97676b62006-08-15 00:31:41 -0700648 int report = 0;
649
650 if (nlh)
651 report = nlmsg_report(nlh);
652
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800653 nlmsg_notify(rtnl, skb, pid, group, report, flags);
Thomas Graf97676b62006-08-15 00:31:41 -0700654}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800655EXPORT_SYMBOL(rtnl_notify);
Thomas Graf97676b62006-08-15 00:31:41 -0700656
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800657void rtnl_set_sk_err(struct net *net, u32 group, int error)
Thomas Graf97676b62006-08-15 00:31:41 -0700658{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800659 struct sock *rtnl = net->rtnl;
660
Thomas Graf97676b62006-08-15 00:31:41 -0700661 netlink_set_err(rtnl, 0, group, error);
662}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800663EXPORT_SYMBOL(rtnl_set_sk_err);
Thomas Graf97676b62006-08-15 00:31:41 -0700664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
666{
Thomas Graf2d7202b2006-08-22 00:01:27 -0700667 struct nlattr *mx;
668 int i, valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Thomas Graf2d7202b2006-08-22 00:01:27 -0700670 mx = nla_nest_start(skb, RTA_METRICS);
671 if (mx == NULL)
672 return -ENOBUFS;
673
674 for (i = 0; i < RTAX_MAX; i++) {
675 if (metrics[i]) {
Daniel Borkmannea697632015-01-05 23:57:47 +0100676 if (i == RTAX_CC_ALGO - 1) {
677 char tmp[TCP_CA_NAME_MAX], *name;
678
679 name = tcp_ca_get_name_by_key(metrics[i], tmp);
680 if (!name)
681 continue;
682 if (nla_put_string(skb, i + 1, name))
683 goto nla_put_failure;
Daniel Borkmannc3a8d942015-08-31 15:58:47 +0200684 } else if (i == RTAX_FEATURES - 1) {
685 u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
686
Phil Sutterf8edcd12016-08-23 13:14:31 +0200687 if (!user_features)
688 continue;
Daniel Borkmannc3a8d942015-08-31 15:58:47 +0200689 BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
690 if (nla_put_u32(skb, i + 1, user_features))
691 goto nla_put_failure;
Daniel Borkmannea697632015-01-05 23:57:47 +0100692 } else {
693 if (nla_put_u32(skb, i + 1, metrics[i]))
694 goto nla_put_failure;
695 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700696 valid++;
Thomas Graf2d7202b2006-08-22 00:01:27 -0700697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
David S. Millera57d27f2006-08-22 22:20:14 -0700700 if (!valid) {
701 nla_nest_cancel(skb, mx);
702 return 0;
703 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700704
705 return nla_nest_end(skb, mx);
706
707nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700708 nla_nest_cancel(skb, mx);
709 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800711EXPORT_SYMBOL(rtnetlink_put_metrics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Thomas Grafe3703b32006-11-27 09:27:07 -0800713int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
David S. Miller87a50692012-07-10 05:06:14 -0700714 long expires, u32 error)
Thomas Grafe3703b32006-11-27 09:27:07 -0800715{
716 struct rta_cacheinfo ci = {
Eric Dumazeta399a802012-08-08 21:13:53 +0000717 .rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse),
Thomas Grafe3703b32006-11-27 09:27:07 -0800718 .rta_used = dst->__use,
719 .rta_clntref = atomic_read(&(dst->__refcnt)),
720 .rta_error = error,
721 .rta_id = id,
Thomas Grafe3703b32006-11-27 09:27:07 -0800722 };
723
Li Wei82539472012-07-29 16:01:30 +0000724 if (expires) {
725 unsigned long clock;
Thomas Grafe3703b32006-11-27 09:27:07 -0800726
Li Wei82539472012-07-29 16:01:30 +0000727 clock = jiffies_to_clock_t(abs(expires));
728 clock = min_t(unsigned long, clock, INT_MAX);
729 ci.rta_expires = (expires > 0) ? clock : -clock;
730 }
Thomas Grafe3703b32006-11-27 09:27:07 -0800731 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
732}
Thomas Grafe3703b32006-11-27 09:27:07 -0800733EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
David S. Miller93b2d4a2008-02-17 18:35:07 -0800735static void set_operstate(struct net_device *dev, unsigned char transition)
Stefan Rompfb00055a2006-03-20 17:09:11 -0800736{
737 unsigned char operstate = dev->operstate;
738
Eric Dumazete0d087a2009-11-07 01:26:17 -0800739 switch (transition) {
Stefan Rompfb00055a2006-03-20 17:09:11 -0800740 case IF_OPER_UP:
741 if ((operstate == IF_OPER_DORMANT ||
742 operstate == IF_OPER_UNKNOWN) &&
743 !netif_dormant(dev))
744 operstate = IF_OPER_UP;
745 break;
746
747 case IF_OPER_DORMANT:
748 if (operstate == IF_OPER_UP ||
749 operstate == IF_OPER_UNKNOWN)
750 operstate = IF_OPER_DORMANT;
751 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700752 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800753
754 if (dev->operstate != operstate) {
755 write_lock_bh(&dev_base_lock);
756 dev->operstate = operstate;
757 write_unlock_bh(&dev_base_lock);
David S. Miller93b2d4a2008-02-17 18:35:07 -0800758 netdev_state_change(dev);
759 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800760}
761
Jiri Bencb1beb682012-07-27 02:58:22 +0000762static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
763{
764 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
765 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
766}
767
Patrick McHardy3729d502010-02-26 06:34:54 +0000768static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
769 const struct ifinfomsg *ifm)
770{
771 unsigned int flags = ifm->ifi_flags;
772
773 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
774 if (ifm->ifi_change)
775 flags = (flags & ifm->ifi_change) |
Jiri Bencb1beb682012-07-27 02:58:22 +0000776 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
Patrick McHardy3729d502010-02-26 06:34:54 +0000777
778 return flags;
779}
780
Thomas Grafb60c5112006-08-04 23:05:34 -0700781static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000782 const struct rtnl_link_stats64 *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
Thomas Grafb60c5112006-08-04 23:05:34 -0700784 a->rx_packets = b->rx_packets;
785 a->tx_packets = b->tx_packets;
786 a->rx_bytes = b->rx_bytes;
787 a->tx_bytes = b->tx_bytes;
788 a->rx_errors = b->rx_errors;
789 a->tx_errors = b->tx_errors;
790 a->rx_dropped = b->rx_dropped;
791 a->tx_dropped = b->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Thomas Grafb60c5112006-08-04 23:05:34 -0700793 a->multicast = b->multicast;
794 a->collisions = b->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Thomas Grafb60c5112006-08-04 23:05:34 -0700796 a->rx_length_errors = b->rx_length_errors;
797 a->rx_over_errors = b->rx_over_errors;
798 a->rx_crc_errors = b->rx_crc_errors;
799 a->rx_frame_errors = b->rx_frame_errors;
800 a->rx_fifo_errors = b->rx_fifo_errors;
801 a->rx_missed_errors = b->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Thomas Grafb60c5112006-08-04 23:05:34 -0700803 a->tx_aborted_errors = b->tx_aborted_errors;
804 a->tx_carrier_errors = b->tx_carrier_errors;
805 a->tx_fifo_errors = b->tx_fifo_errors;
806 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
807 a->tx_window_errors = b->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Thomas Grafb60c5112006-08-04 23:05:34 -0700809 a->rx_compressed = b->rx_compressed;
810 a->tx_compressed = b->tx_compressed;
Jarod Wilson6e7333d2016-02-01 18:51:05 -0500811
812 a->rx_nohandler = b->rx_nohandler;
Jan Engelhardt10708f32010-03-11 09:57:29 +0000813}
814
Chris Wrightc02db8c2010-05-16 01:05:45 -0700815/* All VF info */
Greg Rose115c9b82012-02-21 16:54:48 -0500816static inline int rtnl_vfinfo_size(const struct net_device *dev,
817 u32 ext_filter_mask)
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000818{
Phil Sutter9af15c32017-01-18 14:04:39 +0100819 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
Chris Wrightc02db8c2010-05-16 01:05:45 -0700820 int num_vfs = dev_num_vf(dev->dev.parent);
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100821 size_t size = nla_total_size(0);
Scott Feldman045de012010-05-28 03:42:43 -0700822 size += num_vfs *
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100823 (nla_total_size(0) +
824 nla_total_size(sizeof(struct ifla_vf_mac)) +
825 nla_total_size(sizeof(struct ifla_vf_vlan)) +
826 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
Moshe Shemesh79aab092016-09-22 12:11:15 +0300827 nla_total_size(MAX_VLAN_LIST_LEN *
828 sizeof(struct ifla_vf_vlan_info)) +
Sucheta Chakrabortyed616682014-05-22 09:59:05 -0400829 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100830 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
Jiri Benc945a3672014-08-08 16:44:32 +0200831 nla_total_size(sizeof(struct ifla_vf_rate)) +
Vlad Zolotarov01a3d792015-03-30 21:35:23 +0300832 nla_total_size(sizeof(struct ifla_vf_link_state)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300833 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100834 nla_total_size(0) + /* nest IFLA_VF_STATS */
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300835 /* IFLA_VF_STATS_RX_PACKETS */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200836 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300837 /* IFLA_VF_STATS_TX_PACKETS */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200838 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300839 /* IFLA_VF_STATS_RX_BYTES */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200840 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300841 /* IFLA_VF_STATS_TX_BYTES */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200842 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300843 /* IFLA_VF_STATS_BROADCAST */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200844 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300845 /* IFLA_VF_STATS_MULTICAST */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200846 nla_total_size_64bit(sizeof(__u64)) +
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +0000847 nla_total_size(sizeof(struct ifla_vf_trust)));
Chris Wrightc02db8c2010-05-16 01:05:45 -0700848 return size;
849 } else
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000850 return 0;
851}
852
David Gibsonc53864f2014-04-24 10:22:36 +1000853static size_t rtnl_port_size(const struct net_device *dev,
854 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -0700855{
856 size_t port_size = nla_total_size(4) /* PORT_VF */
857 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
Scott Feldman57b61082010-05-17 22:49:55 -0700858 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
859 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
860 + nla_total_size(1) /* PROT_VDP_REQUEST */
861 + nla_total_size(2); /* PORT_VDP_RESPONSE */
862 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
863 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
864 + port_size;
865 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
866 + port_size;
867
David Gibsonc53864f2014-04-24 10:22:36 +1000868 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
869 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -0700870 return 0;
871 if (dev_num_vf(dev->dev.parent))
872 return port_self_size + vf_ports_size +
873 vf_port_size * dev_num_vf(dev->dev.parent);
874 else
875 return port_self_size;
876}
877
David S. Millerb5cdae32017-04-18 15:36:58 -0400878static size_t rtnl_xdp_size(void)
Brenden Blancod1fdd912016-07-19 12:16:49 -0700879{
Sabrina Dubrocab3cfaa32016-11-15 11:16:35 +0100880 size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */
Martin KaFai Lau58038692017-06-15 17:29:09 -0700881 nla_total_size(1) + /* XDP_ATTACHED */
882 nla_total_size(4); /* XDP_PROG_ID */
Brenden Blancod1fdd912016-07-19 12:16:49 -0700883
David S. Millerb5cdae32017-04-18 15:36:58 -0400884 return xdp_size;
Brenden Blancod1fdd912016-07-19 12:16:49 -0700885}
886
Greg Rose115c9b82012-02-21 16:54:48 -0500887static noinline size_t if_nlmsg_size(const struct net_device *dev,
888 u32 ext_filter_mask)
Thomas Graf339bf982006-11-10 14:10:15 -0800889{
890 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
891 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700892 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
Thomas Graf339bf982006-11-10 14:10:15 -0800893 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
Nicolas Dichtel270cb4d2016-04-26 10:06:16 +0200894 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
Thomas Graf339bf982006-11-10 14:10:15 -0800895 + nla_total_size(sizeof(struct rtnl_link_stats))
David S. Miller35c58452016-04-19 19:49:29 -0400896 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
Thomas Graf339bf982006-11-10 14:10:15 -0800897 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
898 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
899 + nla_total_size(4) /* IFLA_TXQLEN */
900 + nla_total_size(4) /* IFLA_WEIGHT */
901 + nla_total_size(4) /* IFLA_MTU */
902 + nla_total_size(4) /* IFLA_LINK */
903 + nla_total_size(4) /* IFLA_MASTER */
Jiri Pirko9a572472012-12-27 23:49:39 +0000904 + nla_total_size(1) /* IFLA_CARRIER */
Ben Greearedbc0bb2012-03-29 12:51:30 +0000905 + nla_total_size(4) /* IFLA_PROMISCUITY */
Jiri Pirko76ff5cc2012-07-20 02:28:48 +0000906 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
907 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
Tobias Klauser69197562016-11-30 14:30:37 +0100908 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
909 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
Thomas Graf339bf982006-11-10 14:10:15 -0800910 + nla_total_size(1) /* IFLA_OPERSTATE */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700911 + nla_total_size(1) /* IFLA_LINKMODE */
david decotigny2d3b4792014-03-29 09:48:35 -0700912 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
Nicolas Dichteld37512a2015-01-15 15:11:16 +0100913 + nla_total_size(4) /* IFLA_LINK_NETNSID */
Serhey Popovychdb833d42017-06-20 14:35:23 +0300914 + nla_total_size(4) /* IFLA_GROUP */
Greg Rose115c9b82012-02-21 16:54:48 -0500915 + nla_total_size(ext_filter_mask
916 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
917 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
David Gibsonc53864f2014-04-24 10:22:36 +1000918 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
Thomas Graff8ff1822010-11-16 04:30:14 +0000919 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700920 + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
Jiri Pirko82f28412014-11-28 14:34:18 +0100921 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
Anuradha Karuppiah88d63782015-07-14 13:43:20 -0700922 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
Nicolas Dichtelc57c7a92016-03-31 18:10:31 +0200923 + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
David S. Millerb5cdae32017-04-18 15:36:58 -0400924 + rtnl_xdp_size() /* IFLA_XDP */
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -0400925 + nla_total_size(4) /* IFLA_EVENT */
Anuradha Karuppiah88d63782015-07-14 13:43:20 -0700926 + nla_total_size(1); /* IFLA_PROTO_DOWN */
927
Thomas Graf339bf982006-11-10 14:10:15 -0800928}
929
Scott Feldman57b61082010-05-17 22:49:55 -0700930static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
931{
932 struct nlattr *vf_ports;
933 struct nlattr *vf_port;
934 int vf;
935 int err;
936
937 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
938 if (!vf_ports)
939 return -EMSGSIZE;
940
941 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
942 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
Scott Feldman8ca94182010-05-28 03:42:18 -0700943 if (!vf_port)
944 goto nla_put_failure;
David S. Millera6574342012-04-01 20:12:00 -0400945 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
946 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -0700947 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
Scott Feldman8ca94182010-05-28 03:42:18 -0700948 if (err == -EMSGSIZE)
949 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -0700950 if (err) {
Scott Feldman57b61082010-05-17 22:49:55 -0700951 nla_nest_cancel(skb, vf_port);
952 continue;
953 }
954 nla_nest_end(skb, vf_port);
955 }
956
957 nla_nest_end(skb, vf_ports);
958
959 return 0;
Scott Feldman8ca94182010-05-28 03:42:18 -0700960
961nla_put_failure:
962 nla_nest_cancel(skb, vf_ports);
963 return -EMSGSIZE;
Scott Feldman57b61082010-05-17 22:49:55 -0700964}
965
966static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
967{
968 struct nlattr *port_self;
969 int err;
970
971 port_self = nla_nest_start(skb, IFLA_PORT_SELF);
972 if (!port_self)
973 return -EMSGSIZE;
974
975 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
976 if (err) {
977 nla_nest_cancel(skb, port_self);
Scott Feldman8ca94182010-05-28 03:42:18 -0700978 return (err == -EMSGSIZE) ? err : 0;
Scott Feldman57b61082010-05-17 22:49:55 -0700979 }
980
981 nla_nest_end(skb, port_self);
982
983 return 0;
984}
985
David Gibsonc53864f2014-04-24 10:22:36 +1000986static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
987 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -0700988{
989 int err;
990
David Gibsonc53864f2014-04-24 10:22:36 +1000991 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
992 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -0700993 return 0;
994
995 err = rtnl_port_self_fill(skb, dev);
996 if (err)
997 return err;
998
999 if (dev_num_vf(dev->dev.parent)) {
1000 err = rtnl_vf_ports_fill(skb, dev);
1001 if (err)
1002 return err;
1003 }
1004
1005 return 0;
1006}
1007
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001008static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
1009{
1010 int err;
Jiri Pirko02637fc2014-11-28 14:34:16 +01001011 struct netdev_phys_item_id ppid;
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001012
1013 err = dev_get_phys_port_id(dev, &ppid);
1014 if (err) {
1015 if (err == -EOPNOTSUPP)
1016 return 0;
1017 return err;
1018 }
1019
1020 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1021 return -EMSGSIZE;
1022
1023 return 0;
1024}
1025
David Aherndb24a902015-03-17 20:23:15 -06001026static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1027{
1028 char name[IFNAMSIZ];
1029 int err;
1030
1031 err = dev_get_phys_port_name(dev, name, sizeof(name));
1032 if (err) {
1033 if (err == -EOPNOTSUPP)
1034 return 0;
1035 return err;
1036 }
1037
Michal Schmidt77ef0332017-05-04 16:48:58 +02001038 if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
David Aherndb24a902015-03-17 20:23:15 -06001039 return -EMSGSIZE;
1040
1041 return 0;
1042}
1043
Jiri Pirko82f28412014-11-28 14:34:18 +01001044static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1045{
1046 int err;
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001047 struct switchdev_attr attr = {
Ido Schimmel6ff64f62015-12-15 16:03:35 +01001048 .orig_dev = dev,
Jiri Pirko1f868392015-10-01 11:03:42 +02001049 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001050 .flags = SWITCHDEV_F_NO_RECURSE,
1051 };
Jiri Pirko82f28412014-11-28 14:34:18 +01001052
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001053 err = switchdev_port_attr_get(dev, &attr);
Jiri Pirko82f28412014-11-28 14:34:18 +01001054 if (err) {
1055 if (err == -EOPNOTSUPP)
1056 return 0;
1057 return err;
1058 }
1059
Scott Feldman42275bd2015-05-13 11:16:50 -07001060 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len,
1061 attr.u.ppid.id))
Jiri Pirko82f28412014-11-28 14:34:18 +01001062 return -EMSGSIZE;
1063
1064 return 0;
1065}
1066
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001067static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1068 struct net_device *dev)
1069{
Roopa Prabhu550bce52016-04-15 20:36:25 -07001070 struct rtnl_link_stats64 *sp;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001071 struct nlattr *attr;
1072
Nicolas Dichtel58414d32016-04-21 18:58:25 +02001073 attr = nla_reserve_64bit(skb, IFLA_STATS64,
1074 sizeof(struct rtnl_link_stats64), IFLA_PAD);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001075 if (!attr)
1076 return -EMSGSIZE;
1077
Roopa Prabhu550bce52016-04-15 20:36:25 -07001078 sp = nla_data(attr);
1079 dev_get_stats(dev, sp);
1080
1081 attr = nla_reserve(skb, IFLA_STATS,
1082 sizeof(struct rtnl_link_stats));
1083 if (!attr)
1084 return -EMSGSIZE;
1085
1086 copy_rtnl_link_stats(nla_data(attr), sp);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001087
1088 return 0;
1089}
1090
1091static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1092 struct net_device *dev,
1093 int vfs_num,
1094 struct nlattr *vfinfo)
1095{
1096 struct ifla_vf_rss_query_en vf_rss_query_en;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001097 struct nlattr *vf, *vfstats, *vfvlanlist;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001098 struct ifla_vf_link_state vf_linkstate;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001099 struct ifla_vf_vlan_info vf_vlan_info;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001100 struct ifla_vf_spoofchk vf_spoofchk;
1101 struct ifla_vf_tx_rate vf_tx_rate;
1102 struct ifla_vf_stats vf_stats;
1103 struct ifla_vf_trust vf_trust;
1104 struct ifla_vf_vlan vf_vlan;
1105 struct ifla_vf_rate vf_rate;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001106 struct ifla_vf_mac vf_mac;
1107 struct ifla_vf_info ivi;
1108
Mintz, Yuval0eed9cf2017-06-07 21:00:33 +03001109 memset(&ivi, 0, sizeof(ivi));
1110
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001111 /* Not all SR-IOV capable drivers support the
1112 * spoofcheck and "RSS query enable" query. Preset to
1113 * -1 so the user space tool can detect that the driver
1114 * didn't report anything.
1115 */
1116 ivi.spoofchk = -1;
1117 ivi.rss_query_en = -1;
1118 ivi.trusted = -1;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001119 /* The default value for VF link state is "auto"
1120 * IFLA_VF_LINK_STATE_AUTO which equals zero
1121 */
1122 ivi.linkstate = 0;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001123 /* VLAN Protocol by default is 802.1Q */
1124 ivi.vlan_proto = htons(ETH_P_8021Q);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001125 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1126 return 0;
1127
Dan Carpenter775f4f02016-10-13 11:45:28 +03001128 memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
1129
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001130 vf_mac.vf =
1131 vf_vlan.vf =
Moshe Shemesh79aab092016-09-22 12:11:15 +03001132 vf_vlan_info.vf =
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001133 vf_rate.vf =
1134 vf_tx_rate.vf =
1135 vf_spoofchk.vf =
1136 vf_linkstate.vf =
1137 vf_rss_query_en.vf =
1138 vf_trust.vf = ivi.vf;
1139
1140 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1141 vf_vlan.vlan = ivi.vlan;
1142 vf_vlan.qos = ivi.qos;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001143 vf_vlan_info.vlan = ivi.vlan;
1144 vf_vlan_info.qos = ivi.qos;
1145 vf_vlan_info.vlan_proto = ivi.vlan_proto;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001146 vf_tx_rate.rate = ivi.max_tx_rate;
1147 vf_rate.min_tx_rate = ivi.min_tx_rate;
1148 vf_rate.max_tx_rate = ivi.max_tx_rate;
1149 vf_spoofchk.setting = ivi.spoofchk;
1150 vf_linkstate.link_state = ivi.linkstate;
1151 vf_rss_query_en.setting = ivi.rss_query_en;
1152 vf_trust.setting = ivi.trusted;
1153 vf = nla_nest_start(skb, IFLA_VF_INFO);
Moshe Shemesh79aab092016-09-22 12:11:15 +03001154 if (!vf)
1155 goto nla_put_vfinfo_failure;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001156 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1157 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1158 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1159 &vf_rate) ||
1160 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1161 &vf_tx_rate) ||
1162 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1163 &vf_spoofchk) ||
1164 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1165 &vf_linkstate) ||
1166 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1167 sizeof(vf_rss_query_en),
1168 &vf_rss_query_en) ||
1169 nla_put(skb, IFLA_VF_TRUST,
1170 sizeof(vf_trust), &vf_trust))
Moshe Shemesh79aab092016-09-22 12:11:15 +03001171 goto nla_put_vf_failure;
1172 vfvlanlist = nla_nest_start(skb, IFLA_VF_VLAN_LIST);
1173 if (!vfvlanlist)
1174 goto nla_put_vf_failure;
1175 if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
1176 &vf_vlan_info)) {
1177 nla_nest_cancel(skb, vfvlanlist);
1178 goto nla_put_vf_failure;
1179 }
1180 nla_nest_end(skb, vfvlanlist);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001181 memset(&vf_stats, 0, sizeof(vf_stats));
1182 if (dev->netdev_ops->ndo_get_vf_stats)
1183 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1184 &vf_stats);
1185 vfstats = nla_nest_start(skb, IFLA_VF_STATS);
Moshe Shemesh79aab092016-09-22 12:11:15 +03001186 if (!vfstats)
1187 goto nla_put_vf_failure;
Nicolas Dichtel343a6d82016-04-25 10:25:14 +02001188 if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1189 vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1190 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1191 vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1192 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1193 vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1194 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1195 vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1196 nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1197 vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1198 nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
Moshe Shemesh79aab092016-09-22 12:11:15 +03001199 vf_stats.multicast, IFLA_VF_STATS_PAD)) {
1200 nla_nest_cancel(skb, vfstats);
1201 goto nla_put_vf_failure;
1202 }
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001203 nla_nest_end(skb, vfstats);
1204 nla_nest_end(skb, vf);
1205 return 0;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001206
1207nla_put_vf_failure:
1208 nla_nest_cancel(skb, vf);
1209nla_put_vfinfo_failure:
1210 nla_nest_cancel(skb, vfinfo);
1211 return -EMSGSIZE;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001212}
1213
1214static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1215{
Kangjie Lu5f8e4472016-05-03 16:46:24 -04001216 struct rtnl_link_ifmap map;
1217
1218 memset(&map, 0, sizeof(map));
1219 map.mem_start = dev->mem_start;
1220 map.mem_end = dev->mem_end;
1221 map.base_addr = dev->base_addr;
1222 map.irq = dev->irq;
1223 map.dma = dev->dma;
1224 map.port = dev->if_port;
1225
Nicolas Dichtel270cb4d2016-04-26 10:06:16 +02001226 if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001227 return -EMSGSIZE;
1228
1229 return 0;
1230}
1231
Martin KaFai Lau58038692017-06-15 17:29:09 -07001232static u8 rtnl_xdp_attached_mode(struct net_device *dev, u32 *prog_id)
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001233{
1234 const struct net_device_ops *ops = dev->netdev_ops;
Martin KaFai Lau58038692017-06-15 17:29:09 -07001235 const struct bpf_prog *generic_xdp_prog;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001236
1237 ASSERT_RTNL();
1238
Martin KaFai Lau58038692017-06-15 17:29:09 -07001239 *prog_id = 0;
1240 generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
1241 if (generic_xdp_prog) {
1242 *prog_id = generic_xdp_prog->aux->id;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001243 return XDP_ATTACHED_SKB;
Martin KaFai Lau58038692017-06-15 17:29:09 -07001244 }
Jakub Kicinskice158e52017-06-21 18:25:09 -07001245 if (!ops->ndo_xdp)
1246 return XDP_ATTACHED_NONE;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001247
Jakub Kicinskice158e52017-06-21 18:25:09 -07001248 return __dev_xdp_attached(dev, ops->ndo_xdp, prog_id);
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001249}
1250
Brenden Blancod1fdd912016-07-19 12:16:49 -07001251static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1252{
Brenden Blancod1fdd912016-07-19 12:16:49 -07001253 struct nlattr *xdp;
Martin KaFai Lau58038692017-06-15 17:29:09 -07001254 u32 prog_id;
Brenden Blancod1fdd912016-07-19 12:16:49 -07001255 int err;
1256
Brenden Blancod1fdd912016-07-19 12:16:49 -07001257 xdp = nla_nest_start(skb, IFLA_XDP);
1258 if (!xdp)
1259 return -EMSGSIZE;
David S. Millerb5cdae32017-04-18 15:36:58 -04001260
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001261 err = nla_put_u8(skb, IFLA_XDP_ATTACHED,
Martin KaFai Lau58038692017-06-15 17:29:09 -07001262 rtnl_xdp_attached_mode(dev, &prog_id));
Brenden Blancod1fdd912016-07-19 12:16:49 -07001263 if (err)
1264 goto err_cancel;
1265
Martin KaFai Lau58038692017-06-15 17:29:09 -07001266 if (prog_id) {
1267 err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
1268 if (err)
1269 goto err_cancel;
1270 }
1271
Brenden Blancod1fdd912016-07-19 12:16:49 -07001272 nla_nest_end(skb, xdp);
1273 return 0;
1274
1275err_cancel:
1276 nla_nest_cancel(skb, xdp);
1277 return err;
1278}
1279
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001280static u32 rtnl_get_event(unsigned long event)
1281{
1282 u32 rtnl_event_type = IFLA_EVENT_NONE;
1283
1284 switch (event) {
1285 case NETDEV_REBOOT:
1286 rtnl_event_type = IFLA_EVENT_REBOOT;
1287 break;
1288 case NETDEV_FEAT_CHANGE:
1289 rtnl_event_type = IFLA_EVENT_FEATURES;
1290 break;
1291 case NETDEV_BONDING_FAILOVER:
1292 rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
1293 break;
1294 case NETDEV_NOTIFY_PEERS:
1295 rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
1296 break;
1297 case NETDEV_RESEND_IGMP:
1298 rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
1299 break;
1300 case NETDEV_CHANGEINFODATA:
1301 rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
1302 break;
1303 default:
1304 break;
1305 }
1306
1307 return rtnl_event_type;
1308}
1309
Thomas Grafb60c5112006-08-04 23:05:34 -07001310static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
Patrick McHardy575c3e22007-05-22 17:00:49 -07001311 int type, u32 pid, u32 seq, u32 change,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001312 unsigned int flags, u32 ext_filter_mask,
1313 u32 event)
Thomas Grafb60c5112006-08-04 23:05:34 -07001314{
1315 struct ifinfomsg *ifm;
1316 struct nlmsghdr *nlh;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001317 struct nlattr *af_spec;
Thomas Graff8ff1822010-11-16 04:30:14 +00001318 struct rtnl_af_ops *af_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00001319 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Thomas Grafb60c5112006-08-04 23:05:34 -07001320
Eric Dumazet2907c352011-05-25 07:34:04 +00001321 ASSERT_RTNL();
Thomas Grafb60c5112006-08-04 23:05:34 -07001322 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1323 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08001324 return -EMSGSIZE;
Thomas Grafb60c5112006-08-04 23:05:34 -07001325
1326 ifm = nlmsg_data(nlh);
1327 ifm->ifi_family = AF_UNSPEC;
1328 ifm->__ifi_pad = 0;
1329 ifm->ifi_type = dev->type;
1330 ifm->ifi_index = dev->ifindex;
1331 ifm->ifi_flags = dev_get_flags(dev);
1332 ifm->ifi_change = change;
1333
David S. Millera6574342012-04-01 20:12:00 -04001334 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1335 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1336 nla_put_u8(skb, IFLA_OPERSTATE,
1337 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1338 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1339 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1340 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
Ben Greearedbc0bb2012-03-29 12:51:30 +00001341 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001342 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
Eric Dumazetc70ce022016-03-21 09:55:10 -07001343 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1344 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001345#ifdef CONFIG_RPS
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001346 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001347#endif
Nicolas Dichtela54acb32015-04-02 17:07:00 +02001348 (dev->ifindex != dev_get_iflink(dev) &&
1349 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00001350 (upper_dev &&
1351 nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
Jiri Pirko9a572472012-12-27 23:49:39 +00001352 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
David S. Millera6574342012-04-01 20:12:00 -04001353 (dev->qdisc &&
1354 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1355 (dev->ifalias &&
david decotigny2d3b4792014-03-29 09:48:35 -07001356 nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) ||
1357 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001358 atomic_read(&dev->carrier_changes)) ||
1359 nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
David S. Millera6574342012-04-01 20:12:00 -04001360 goto nla_put_failure;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001361
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001362 if (event != IFLA_EVENT_NONE) {
1363 if (nla_put_u32(skb, IFLA_EVENT, event))
1364 goto nla_put_failure;
1365 }
1366
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001367 if (rtnl_fill_link_ifmap(skb, dev))
1368 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370 if (dev->addr_len) {
David S. Millera6574342012-04-01 20:12:00 -04001371 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1372 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1373 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 }
1375
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001376 if (rtnl_phys_port_id_fill(skb, dev))
1377 goto nla_put_failure;
1378
David Aherndb24a902015-03-17 20:23:15 -06001379 if (rtnl_phys_port_name_fill(skb, dev))
1380 goto nla_put_failure;
1381
Jiri Pirko82f28412014-11-28 14:34:18 +01001382 if (rtnl_phys_switch_id_fill(skb, dev))
1383 goto nla_put_failure;
1384
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001385 if (rtnl_fill_stats(skb, dev))
Pavel Emelyanov96e74082008-05-21 14:12:46 -07001386 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
David S. Millera6574342012-04-01 20:12:00 -04001388 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
1389 nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
1390 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001391
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001392 if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent &&
1393 ext_filter_mask & RTEXT_FILTER_VF) {
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001394 int i;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001395 struct nlattr *vfinfo;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001396 int num_vfs = dev_num_vf(dev->dev.parent);
1397
Chris Wrightc02db8c2010-05-16 01:05:45 -07001398 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1399 if (!vfinfo)
1400 goto nla_put_failure;
1401 for (i = 0; i < num_vfs; i++) {
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001402 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
Chris Wrightc02db8c2010-05-16 01:05:45 -07001403 goto nla_put_failure;
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001404 }
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001405
Chris Wrightc02db8c2010-05-16 01:05:45 -07001406 nla_nest_end(skb, vfinfo);
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001407 }
Scott Feldman57b61082010-05-17 22:49:55 -07001408
David Gibsonc53864f2014-04-24 10:22:36 +10001409 if (rtnl_port_fill(skb, dev, ext_filter_mask))
Scott Feldman57b61082010-05-17 22:49:55 -07001410 goto nla_put_failure;
1411
Brenden Blancod1fdd912016-07-19 12:16:49 -07001412 if (rtnl_xdp_fill(skb, dev))
1413 goto nla_put_failure;
1414
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001415 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07001416 if (rtnl_link_fill(skb, dev) < 0)
1417 goto nla_put_failure;
1418 }
1419
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001420 if (dev->rtnl_link_ops &&
1421 dev->rtnl_link_ops->get_link_net) {
1422 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1423
1424 if (!net_eq(dev_net(dev), link_net)) {
Nicolas Dichtel7a0877d2015-05-07 11:02:49 +02001425 int id = peernet2id_alloc(dev_net(dev), link_net);
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001426
1427 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1428 goto nla_put_failure;
1429 }
1430 }
1431
Thomas Graff8ff1822010-11-16 04:30:14 +00001432 if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1433 goto nla_put_failure;
1434
1435 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1436 if (af_ops->fill_link_af) {
1437 struct nlattr *af;
1438 int err;
1439
1440 if (!(af = nla_nest_start(skb, af_ops->family)))
1441 goto nla_put_failure;
1442
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04001443 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
Thomas Graff8ff1822010-11-16 04:30:14 +00001444
1445 /*
1446 * Caller may return ENODATA to indicate that there
1447 * was no data to be dumped. This is not an error, it
1448 * means we should trim the attribute header and
1449 * continue.
1450 */
1451 if (err == -ENODATA)
1452 nla_nest_cancel(skb, af);
1453 else if (err < 0)
1454 goto nla_put_failure;
1455
1456 nla_nest_end(skb, af);
1457 }
1458 }
1459
1460 nla_nest_end(skb, af_spec);
1461
Johannes Berg053c0952015-01-16 22:09:00 +01001462 nlmsg_end(skb, nlh);
1463 return 0;
Thomas Grafb60c5112006-08-04 23:05:34 -07001464
1465nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001466 nlmsg_cancel(skb, nlh);
1467 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468}
1469
Jiri Pirkof7b12602014-02-18 20:53:18 +01001470static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1471 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1472 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1473 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1474 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1475 [IFLA_MTU] = { .type = NLA_U32 },
1476 [IFLA_LINK] = { .type = NLA_U32 },
1477 [IFLA_MASTER] = { .type = NLA_U32 },
1478 [IFLA_CARRIER] = { .type = NLA_U8 },
1479 [IFLA_TXQLEN] = { .type = NLA_U32 },
1480 [IFLA_WEIGHT] = { .type = NLA_U32 },
1481 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1482 [IFLA_LINKMODE] = { .type = NLA_U8 },
1483 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1484 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1485 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
1486 [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 },
1487 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1488 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1489 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1490 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1491 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1492 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1493 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1494 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
Jiri Pirko02637fc2014-11-28 14:34:16 +01001495 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
david decotigny2d3b4792014-03-29 09:48:35 -07001496 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
Jiri Pirko82f28412014-11-28 14:34:18 +01001497 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
Nicolas Dichtel317f4812015-01-15 15:11:18 +01001498 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001499 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001500 [IFLA_XDP] = { .type = NLA_NESTED },
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001501 [IFLA_EVENT] = { .type = NLA_U32 },
Serhey Popovychdb833d42017-06-20 14:35:23 +03001502 [IFLA_GROUP] = { .type = NLA_U32 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001503};
1504
1505static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1506 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1507 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1508 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1509 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1510};
1511
Jiri Pirkof7b12602014-02-18 20:53:18 +01001512static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
Daniel Borkmann364d5712015-02-05 18:44:04 +01001513 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1514 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
Moshe Shemesh79aab092016-09-22 12:11:15 +03001515 [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED },
Daniel Borkmann364d5712015-02-05 18:44:04 +01001516 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1517 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1518 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1519 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001520 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001521 [IFLA_VF_STATS] = { .type = NLA_NESTED },
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00001522 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
Eli Cohencc8e27c2016-03-11 22:58:34 +02001523 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1524 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001525};
1526
Jiri Pirkof7b12602014-02-18 20:53:18 +01001527static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1528 [IFLA_PORT_VF] = { .type = NLA_U32 },
1529 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1530 .len = PORT_PROFILE_MAX },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001531 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1532 .len = PORT_UUID_MAX },
1533 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1534 .len = PORT_UUID_MAX },
1535 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1536 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
Daniel Borkmann025331d2017-02-17 01:56:11 +01001537
1538 /* Unused, but we need to keep it here since user space could
1539 * fill it. It's also broken with regard to NLA_BINARY use in
1540 * combination with structs.
1541 */
1542 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1543 .len = sizeof(struct ifla_port_vsi) },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001544};
1545
Brenden Blancod1fdd912016-07-19 12:16:49 -07001546static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
1547 [IFLA_XDP_FD] = { .type = NLA_S32 },
1548 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 },
Daniel Borkmann85de8572016-11-28 23:16:54 +01001549 [IFLA_XDP_FLAGS] = { .type = NLA_U32 },
Martin KaFai Lau58038692017-06-15 17:29:09 -07001550 [IFLA_XDP_PROG_ID] = { .type = NLA_U32 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001551};
1552
David Aherndc599f72016-02-02 08:17:07 -08001553static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1554{
1555 const struct rtnl_link_ops *ops = NULL;
1556 struct nlattr *linfo[IFLA_INFO_MAX + 1];
1557
Johannes Bergfceb6432017-04-12 14:34:07 +02001558 if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla,
1559 ifla_info_policy, NULL) < 0)
David Aherndc599f72016-02-02 08:17:07 -08001560 return NULL;
1561
1562 if (linfo[IFLA_INFO_KIND]) {
1563 char kind[MODULE_NAME_LEN];
1564
1565 nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1566 ops = rtnl_link_ops_get(kind);
1567 }
1568
1569 return ops;
1570}
1571
1572static bool link_master_filtered(struct net_device *dev, int master_idx)
1573{
1574 struct net_device *master;
1575
1576 if (!master_idx)
1577 return false;
1578
1579 master = netdev_master_upper_dev_get(dev);
1580 if (!master || master->ifindex != master_idx)
1581 return true;
1582
1583 return false;
1584}
1585
1586static bool link_kind_filtered(const struct net_device *dev,
1587 const struct rtnl_link_ops *kind_ops)
1588{
1589 if (kind_ops && dev->rtnl_link_ops != kind_ops)
1590 return true;
1591
1592 return false;
1593}
1594
1595static bool link_dump_filtered(struct net_device *dev,
1596 int master_idx,
1597 const struct rtnl_link_ops *kind_ops)
1598{
1599 if (link_master_filtered(dev, master_idx) ||
1600 link_kind_filtered(dev, kind_ops))
1601 return true;
1602
1603 return false;
1604}
1605
Thomas Grafb60c5112006-08-04 23:05:34 -07001606static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001608 struct net *net = sock_net(skb->sk);
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001609 int h, s_h;
1610 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 struct net_device *dev;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001612 struct hlist_head *head;
Greg Rose115c9b82012-02-21 16:54:48 -05001613 struct nlattr *tb[IFLA_MAX+1];
1614 u32 ext_filter_mask = 0;
David Aherndc599f72016-02-02 08:17:07 -08001615 const struct rtnl_link_ops *kind_ops = NULL;
1616 unsigned int flags = NLM_F_MULTI;
1617 int master_idx = 0;
David Gibson973462b2014-04-24 10:22:35 +10001618 int err;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001619 int hdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001621 s_h = cb->args[0];
1622 s_idx = cb->args[1];
1623
Thomas Graf4e985ad2011-06-21 03:11:20 +00001624 cb->seq = net->dev_base_seq;
1625
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001626 /* A hack to preserve kernel<->userspace interface.
1627 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1628 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1629 * what iproute2 < v3.9.0 used.
1630 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1631 * attribute, its netlink message is shorter than struct ifinfomsg.
1632 */
1633 hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1634 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1635
Johannes Bergfceb6432017-04-12 14:34:07 +02001636 if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX,
1637 ifla_policy, NULL) >= 0) {
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001638 if (tb[IFLA_EXT_MASK])
1639 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
David Aherndc599f72016-02-02 08:17:07 -08001640
1641 if (tb[IFLA_MASTER])
1642 master_idx = nla_get_u32(tb[IFLA_MASTER]);
1643
1644 if (tb[IFLA_LINKINFO])
1645 kind_ops = linkinfo_to_kind_ops(tb[IFLA_LINKINFO]);
1646
1647 if (master_idx || kind_ops)
1648 flags |= NLM_F_DUMP_FILTERED;
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001649 }
Greg Rose115c9b82012-02-21 16:54:48 -05001650
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001651 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1652 idx = 0;
1653 head = &net->dev_index_head[h];
Eric Dumazetcac5e652015-02-27 09:42:50 -08001654 hlist_for_each_entry(dev, head, index_hlist) {
David Aherndc599f72016-02-02 08:17:07 -08001655 if (link_dump_filtered(dev, master_idx, kind_ops))
Zhang Shengju3f0ae052016-11-19 23:28:32 +08001656 goto cont;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001657 if (idx < s_idx)
1658 goto cont;
David Gibson973462b2014-04-24 10:22:35 +10001659 err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
1660 NETLINK_CB(cb->skb).portid,
1661 cb->nlh->nlmsg_seq, 0,
David Aherndc599f72016-02-02 08:17:07 -08001662 flags,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001663 ext_filter_mask, 0);
David Gibson973462b2014-04-24 10:22:35 +10001664
David Ahernf6c57752017-05-15 23:19:17 -07001665 if (err < 0) {
1666 if (likely(skb->len))
1667 goto out;
1668
1669 goto out_err;
1670 }
Thomas Graf4e985ad2011-06-21 03:11:20 +00001671
1672 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Pavel Emelianov7562f872007-05-03 15:13:45 -07001673cont:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001674 idx++;
1675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 }
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001677out:
David Ahernf6c57752017-05-15 23:19:17 -07001678 err = skb->len;
1679out_err:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001680 cb->args[1] = idx;
1681 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
David Ahernf6c57752017-05-15 23:19:17 -07001683 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684}
1685
Johannes Bergfceb6432017-04-12 14:34:07 +02001686int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
1687 struct netlink_ext_ack *exterr)
Jiri Pirkof7b12602014-02-18 20:53:18 +01001688{
Johannes Bergfceb6432017-04-12 14:34:07 +02001689 return nla_parse(tb, IFLA_MAX, head, len, ifla_policy, exterr);
Jiri Pirkof7b12602014-02-18 20:53:18 +01001690}
1691EXPORT_SYMBOL(rtnl_nla_parse_ifla);
Scott Feldman57b61082010-05-17 22:49:55 -07001692
Eric W. Biederman81adee42009-11-08 00:53:51 -08001693struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1694{
1695 struct net *net;
1696 /* Examine the link attributes and figure out which
1697 * network namespace we are talking about.
1698 */
1699 if (tb[IFLA_NET_NS_PID])
1700 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001701 else if (tb[IFLA_NET_NS_FD])
1702 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
Eric W. Biederman81adee42009-11-08 00:53:51 -08001703 else
1704 net = get_net(src_net);
1705 return net;
1706}
1707EXPORT_SYMBOL(rtnl_link_get_net);
1708
Thomas Graf1840bb12008-02-23 19:54:36 -08001709static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1710{
1711 if (dev) {
1712 if (tb[IFLA_ADDRESS] &&
1713 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1714 return -EINVAL;
1715
1716 if (tb[IFLA_BROADCAST] &&
1717 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1718 return -EINVAL;
1719 }
1720
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001721 if (tb[IFLA_AF_SPEC]) {
1722 struct nlattr *af;
1723 int rem, err;
1724
1725 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1726 const struct rtnl_af_ops *af_ops;
1727
1728 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1729 return -EAFNOSUPPORT;
1730
1731 if (!af_ops->set_link_af)
1732 return -EOPNOTSUPP;
1733
1734 if (af_ops->validate_link_af) {
Kurt Van Dijck6d3a9a62011-01-26 04:55:24 +00001735 err = af_ops->validate_link_af(dev, af);
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001736 if (err < 0)
1737 return err;
1738 }
1739 }
1740 }
1741
Thomas Graf1840bb12008-02-23 19:54:36 -08001742 return 0;
1743}
1744
Eli Cohencc8e27c2016-03-11 22:58:34 +02001745static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
1746 int guid_type)
1747{
1748 const struct net_device_ops *ops = dev->netdev_ops;
1749
1750 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
1751}
1752
1753static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
1754{
1755 if (dev->type != ARPHRD_INFINIBAND)
1756 return -EOPNOTSUPP;
1757
1758 return handle_infiniband_guid(dev, ivt, guid_type);
1759}
1760
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001761static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
Chris Wrightc02db8c2010-05-16 01:05:45 -07001762{
Chris Wrightc02db8c2010-05-16 01:05:45 -07001763 const struct net_device_ops *ops = dev->netdev_ops;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001764 int err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001765
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001766 if (tb[IFLA_VF_MAC]) {
1767 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001768
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001769 err = -EOPNOTSUPP;
1770 if (ops->ndo_set_vf_mac)
1771 err = ops->ndo_set_vf_mac(dev, ivm->vf,
1772 ivm->mac);
1773 if (err < 0)
1774 return err;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001775 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001776
1777 if (tb[IFLA_VF_VLAN]) {
1778 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
1779
1780 err = -EOPNOTSUPP;
1781 if (ops->ndo_set_vf_vlan)
1782 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
Moshe Shemesh79aab092016-09-22 12:11:15 +03001783 ivv->qos,
1784 htons(ETH_P_8021Q));
1785 if (err < 0)
1786 return err;
1787 }
1788
1789 if (tb[IFLA_VF_VLAN_LIST]) {
1790 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
1791 struct nlattr *attr;
1792 int rem, len = 0;
1793
1794 err = -EOPNOTSUPP;
1795 if (!ops->ndo_set_vf_vlan)
1796 return err;
1797
1798 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
1799 if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
1800 nla_len(attr) < NLA_HDRLEN) {
1801 return -EINVAL;
1802 }
1803 if (len >= MAX_VLAN_LIST_LEN)
1804 return -EOPNOTSUPP;
1805 ivvl[len] = nla_data(attr);
1806
1807 len++;
1808 }
Arnd Bergmannfa34cd92016-09-30 18:13:49 +02001809 if (len == 0)
1810 return -EINVAL;
1811
Moshe Shemesh79aab092016-09-22 12:11:15 +03001812 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
1813 ivvl[0]->qos, ivvl[0]->vlan_proto);
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001814 if (err < 0)
1815 return err;
1816 }
1817
1818 if (tb[IFLA_VF_TX_RATE]) {
1819 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
1820 struct ifla_vf_info ivf;
1821
1822 err = -EOPNOTSUPP;
1823 if (ops->ndo_get_vf_config)
1824 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
1825 if (err < 0)
1826 return err;
1827
1828 err = -EOPNOTSUPP;
1829 if (ops->ndo_set_vf_rate)
1830 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1831 ivf.min_tx_rate,
1832 ivt->rate);
1833 if (err < 0)
1834 return err;
1835 }
1836
1837 if (tb[IFLA_VF_RATE]) {
1838 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
1839
1840 err = -EOPNOTSUPP;
1841 if (ops->ndo_set_vf_rate)
1842 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1843 ivt->min_tx_rate,
1844 ivt->max_tx_rate);
1845 if (err < 0)
1846 return err;
1847 }
1848
1849 if (tb[IFLA_VF_SPOOFCHK]) {
1850 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
1851
1852 err = -EOPNOTSUPP;
1853 if (ops->ndo_set_vf_spoofchk)
1854 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
1855 ivs->setting);
1856 if (err < 0)
1857 return err;
1858 }
1859
1860 if (tb[IFLA_VF_LINK_STATE]) {
1861 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
1862
1863 err = -EOPNOTSUPP;
1864 if (ops->ndo_set_vf_link_state)
1865 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
1866 ivl->link_state);
1867 if (err < 0)
1868 return err;
1869 }
1870
1871 if (tb[IFLA_VF_RSS_QUERY_EN]) {
1872 struct ifla_vf_rss_query_en *ivrssq_en;
1873
1874 err = -EOPNOTSUPP;
1875 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
1876 if (ops->ndo_set_vf_rss_query_en)
1877 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
1878 ivrssq_en->setting);
1879 if (err < 0)
1880 return err;
1881 }
1882
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00001883 if (tb[IFLA_VF_TRUST]) {
1884 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
1885
1886 err = -EOPNOTSUPP;
1887 if (ops->ndo_set_vf_trust)
1888 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
1889 if (err < 0)
1890 return err;
1891 }
1892
Eli Cohencc8e27c2016-03-11 22:58:34 +02001893 if (tb[IFLA_VF_IB_NODE_GUID]) {
1894 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
1895
1896 if (!ops->ndo_set_vf_guid)
1897 return -EOPNOTSUPP;
1898
1899 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
1900 }
1901
1902 if (tb[IFLA_VF_IB_PORT_GUID]) {
1903 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
1904
1905 if (!ops->ndo_set_vf_guid)
1906 return -EOPNOTSUPP;
1907
1908 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
1909 }
1910
Chris Wrightc02db8c2010-05-16 01:05:45 -07001911 return err;
1912}
1913
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001914static int do_set_master(struct net_device *dev, int ifindex)
1915{
Jiri Pirko898e5062013-01-03 22:48:52 +00001916 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001917 const struct net_device_ops *ops;
1918 int err;
1919
Jiri Pirko898e5062013-01-03 22:48:52 +00001920 if (upper_dev) {
1921 if (upper_dev->ifindex == ifindex)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001922 return 0;
Jiri Pirko898e5062013-01-03 22:48:52 +00001923 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001924 if (ops->ndo_del_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001925 err = ops->ndo_del_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001926 if (err)
1927 return err;
1928 } else {
1929 return -EOPNOTSUPP;
1930 }
1931 }
1932
1933 if (ifindex) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001934 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
1935 if (!upper_dev)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001936 return -EINVAL;
Jiri Pirko898e5062013-01-03 22:48:52 +00001937 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001938 if (ops->ndo_add_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001939 err = ops->ndo_add_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001940 if (err)
1941 return err;
1942 } else {
1943 return -EOPNOTSUPP;
1944 }
1945 }
1946 return 0;
1947}
1948
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001949#define DO_SETLINK_MODIFIED 0x01
Nicolas Dichtelba998902014-09-01 16:07:29 +02001950/* notify flag means notify + modified. */
1951#define DO_SETLINK_NOTIFY 0x03
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001952static int do_setlink(const struct sk_buff *skb,
1953 struct net_device *dev, struct ifinfomsg *ifm,
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07001954 struct netlink_ext_ack *extack,
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001955 struct nlattr **tb, char *ifname, int status)
Patrick McHardy0157f602007-06-13 12:03:36 -07001956{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001957 const struct net_device_ops *ops = dev->netdev_ops;
Patrick McHardy0157f602007-06-13 12:03:36 -07001958 int err;
1959
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001960 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
Eric W. Biederman81adee42009-11-08 00:53:51 -08001961 struct net *net = rtnl_link_get_net(dev_net(dev), tb);
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001962 if (IS_ERR(net)) {
1963 err = PTR_ERR(net);
1964 goto errout;
1965 }
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001966 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
Nicolas Dichtele0ebde02014-11-27 10:16:15 +01001967 put_net(net);
Eric W. Biedermanb51642f2012-11-16 03:03:11 +00001968 err = -EPERM;
1969 goto errout;
1970 }
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001971 err = dev_change_net_namespace(dev, net, ifname);
1972 put_net(net);
1973 if (err)
1974 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001975 status |= DO_SETLINK_MODIFIED;
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001976 }
1977
Patrick McHardy0157f602007-06-13 12:03:36 -07001978 if (tb[IFLA_MAP]) {
1979 struct rtnl_link_ifmap *u_map;
1980 struct ifmap k_map;
1981
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001982 if (!ops->ndo_set_config) {
Patrick McHardy0157f602007-06-13 12:03:36 -07001983 err = -EOPNOTSUPP;
1984 goto errout;
1985 }
1986
1987 if (!netif_device_present(dev)) {
1988 err = -ENODEV;
1989 goto errout;
1990 }
1991
1992 u_map = nla_data(tb[IFLA_MAP]);
1993 k_map.mem_start = (unsigned long) u_map->mem_start;
1994 k_map.mem_end = (unsigned long) u_map->mem_end;
1995 k_map.base_addr = (unsigned short) u_map->base_addr;
1996 k_map.irq = (unsigned char) u_map->irq;
1997 k_map.dma = (unsigned char) u_map->dma;
1998 k_map.port = (unsigned char) u_map->port;
1999
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002000 err = ops->ndo_set_config(dev, &k_map);
Patrick McHardy0157f602007-06-13 12:03:36 -07002001 if (err < 0)
2002 goto errout;
2003
Nicolas Dichtelba998902014-09-01 16:07:29 +02002004 status |= DO_SETLINK_NOTIFY;
Patrick McHardy0157f602007-06-13 12:03:36 -07002005 }
2006
2007 if (tb[IFLA_ADDRESS]) {
2008 struct sockaddr *sa;
2009 int len;
2010
WANG Cong153711f2017-07-20 11:27:57 -07002011 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2012 sizeof(*sa));
Patrick McHardy0157f602007-06-13 12:03:36 -07002013 sa = kmalloc(len, GFP_KERNEL);
2014 if (!sa) {
2015 err = -ENOMEM;
2016 goto errout;
2017 }
2018 sa->sa_family = dev->type;
2019 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
2020 dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00002021 err = dev_set_mac_address(dev, sa);
Patrick McHardy0157f602007-06-13 12:03:36 -07002022 kfree(sa);
2023 if (err)
2024 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002025 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002026 }
2027
2028 if (tb[IFLA_MTU]) {
2029 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
2030 if (err < 0)
2031 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002032 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002033 }
2034
Vlad Dogarucbda10f2011-01-13 23:38:30 +00002035 if (tb[IFLA_GROUP]) {
2036 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Nicolas Dichtelba998902014-09-01 16:07:29 +02002037 status |= DO_SETLINK_NOTIFY;
Vlad Dogarucbda10f2011-01-13 23:38:30 +00002038 }
2039
Patrick McHardy0157f602007-06-13 12:03:36 -07002040 /*
2041 * Interface selected by interface index but interface
2042 * name provided implies that a name change has been
2043 * requested.
2044 */
2045 if (ifm->ifi_index > 0 && ifname[0]) {
2046 err = dev_change_name(dev, ifname);
2047 if (err < 0)
2048 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002049 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002050 }
2051
Stephen Hemminger0b815a12008-09-22 21:28:11 -07002052 if (tb[IFLA_IFALIAS]) {
2053 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2054 nla_len(tb[IFLA_IFALIAS]));
2055 if (err < 0)
2056 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002057 status |= DO_SETLINK_NOTIFY;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07002058 }
2059
Patrick McHardy0157f602007-06-13 12:03:36 -07002060 if (tb[IFLA_BROADCAST]) {
2061 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00002062 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Patrick McHardy0157f602007-06-13 12:03:36 -07002063 }
2064
2065 if (ifm->ifi_flags || ifm->ifi_change) {
Patrick McHardy3729d502010-02-26 06:34:54 +00002066 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
Johannes Berg5f9021c2008-11-16 23:20:31 -08002067 if (err < 0)
2068 goto errout;
Patrick McHardy0157f602007-06-13 12:03:36 -07002069 }
2070
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002071 if (tb[IFLA_MASTER]) {
2072 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
2073 if (err)
2074 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002075 status |= DO_SETLINK_MODIFIED;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002076 }
2077
Jiri Pirko9a572472012-12-27 23:49:39 +00002078 if (tb[IFLA_CARRIER]) {
2079 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2080 if (err)
2081 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002082 status |= DO_SETLINK_MODIFIED;
Jiri Pirko9a572472012-12-27 23:49:39 +00002083 }
2084
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002085 if (tb[IFLA_TXQLEN]) {
Alexey Dobriyan0cd29502017-05-17 13:30:44 +03002086 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
2087 unsigned int orig_len = dev->tx_queue_len;
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002088
Jason Wang08294a22016-06-30 14:45:35 +08002089 if (dev->tx_queue_len ^ value) {
2090 dev->tx_queue_len = value;
2091 err = call_netdevice_notifiers(
2092 NETDEV_CHANGE_TX_QUEUE_LEN, dev);
2093 err = notifier_to_errno(err);
2094 if (err) {
2095 dev->tx_queue_len = orig_len;
2096 goto errout;
2097 }
Nicolas Dichtelba998902014-09-01 16:07:29 +02002098 status |= DO_SETLINK_NOTIFY;
Jason Wang08294a22016-06-30 14:45:35 +08002099 }
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002100 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002101
Patrick McHardy0157f602007-06-13 12:03:36 -07002102 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002103 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Patrick McHardy0157f602007-06-13 12:03:36 -07002104
2105 if (tb[IFLA_LINKMODE]) {
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002106 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2107
David S. Miller93b2d4a2008-02-17 18:35:07 -08002108 write_lock_bh(&dev_base_lock);
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002109 if (dev->link_mode ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02002110 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002111 dev->link_mode = value;
David S. Miller93b2d4a2008-02-17 18:35:07 -08002112 write_unlock_bh(&dev_base_lock);
Patrick McHardy0157f602007-06-13 12:03:36 -07002113 }
2114
Chris Wrightc02db8c2010-05-16 01:05:45 -07002115 if (tb[IFLA_VFINFO_LIST]) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002116 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
Chris Wrightc02db8c2010-05-16 01:05:45 -07002117 struct nlattr *attr;
2118 int rem;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002119
Chris Wrightc02db8c2010-05-16 01:05:45 -07002120 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002121 if (nla_type(attr) != IFLA_VF_INFO ||
2122 nla_len(attr) < NLA_HDRLEN) {
David Howells253683b2010-05-21 02:25:27 +00002123 err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002124 goto errout;
David Howells253683b2010-05-21 02:25:27 +00002125 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002126 err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr,
Johannes Bergfceb6432017-04-12 14:34:07 +02002127 ifla_vf_policy, NULL);
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002128 if (err < 0)
2129 goto errout;
2130 err = do_setvfinfo(dev, vfinfo);
Chris Wrightc02db8c2010-05-16 01:05:45 -07002131 if (err < 0)
2132 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002133 status |= DO_SETLINK_NOTIFY;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002134 }
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00002135 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002136 err = 0;
2137
Scott Feldman57b61082010-05-17 22:49:55 -07002138 if (tb[IFLA_VF_PORTS]) {
2139 struct nlattr *port[IFLA_PORT_MAX+1];
2140 struct nlattr *attr;
2141 int vf;
2142 int rem;
2143
2144 err = -EOPNOTSUPP;
2145 if (!ops->ndo_set_vf_port)
2146 goto errout;
2147
2148 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
Daniel Borkmann035d2102015-07-13 00:06:02 +02002149 if (nla_type(attr) != IFLA_VF_PORT ||
2150 nla_len(attr) < NLA_HDRLEN) {
2151 err = -EINVAL;
2152 goto errout;
2153 }
2154 err = nla_parse_nested(port, IFLA_PORT_MAX, attr,
Johannes Bergfceb6432017-04-12 14:34:07 +02002155 ifla_port_policy, NULL);
Scott Feldman57b61082010-05-17 22:49:55 -07002156 if (err < 0)
2157 goto errout;
2158 if (!port[IFLA_PORT_VF]) {
2159 err = -EOPNOTSUPP;
2160 goto errout;
2161 }
2162 vf = nla_get_u32(port[IFLA_PORT_VF]);
2163 err = ops->ndo_set_vf_port(dev, vf, port);
2164 if (err < 0)
2165 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002166 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07002167 }
2168 }
2169 err = 0;
2170
2171 if (tb[IFLA_PORT_SELF]) {
2172 struct nlattr *port[IFLA_PORT_MAX+1];
2173
2174 err = nla_parse_nested(port, IFLA_PORT_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +02002175 tb[IFLA_PORT_SELF], ifla_port_policy,
2176 NULL);
Scott Feldman57b61082010-05-17 22:49:55 -07002177 if (err < 0)
2178 goto errout;
2179
2180 err = -EOPNOTSUPP;
2181 if (ops->ndo_set_vf_port)
2182 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
2183 if (err < 0)
2184 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002185 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07002186 }
Thomas Graff8ff1822010-11-16 04:30:14 +00002187
2188 if (tb[IFLA_AF_SPEC]) {
2189 struct nlattr *af;
2190 int rem;
2191
2192 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2193 const struct rtnl_af_ops *af_ops;
2194
2195 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002196 BUG();
Thomas Graff8ff1822010-11-16 04:30:14 +00002197
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002198 err = af_ops->set_link_af(dev, af);
Thomas Graff8ff1822010-11-16 04:30:14 +00002199 if (err < 0)
2200 goto errout;
2201
Nicolas Dichtelba998902014-09-01 16:07:29 +02002202 status |= DO_SETLINK_NOTIFY;
Thomas Graff8ff1822010-11-16 04:30:14 +00002203 }
2204 }
Scott Feldman57b61082010-05-17 22:49:55 -07002205 err = 0;
2206
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07002207 if (tb[IFLA_PROTO_DOWN]) {
2208 err = dev_change_proto_down(dev,
2209 nla_get_u8(tb[IFLA_PROTO_DOWN]));
2210 if (err)
2211 goto errout;
2212 status |= DO_SETLINK_NOTIFY;
2213 }
2214
Brenden Blancod1fdd912016-07-19 12:16:49 -07002215 if (tb[IFLA_XDP]) {
2216 struct nlattr *xdp[IFLA_XDP_MAX + 1];
Daniel Borkmann85de8572016-11-28 23:16:54 +01002217 u32 xdp_flags = 0;
Brenden Blancod1fdd912016-07-19 12:16:49 -07002218
2219 err = nla_parse_nested(xdp, IFLA_XDP_MAX, tb[IFLA_XDP],
Johannes Bergfceb6432017-04-12 14:34:07 +02002220 ifla_xdp_policy, NULL);
Brenden Blancod1fdd912016-07-19 12:16:49 -07002221 if (err < 0)
2222 goto errout;
2223
Martin KaFai Lau58038692017-06-15 17:29:09 -07002224 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
Brenden Blanco262d8622016-07-20 17:22:34 -07002225 err = -EINVAL;
2226 goto errout;
2227 }
Daniel Borkmann85de8572016-11-28 23:16:54 +01002228
2229 if (xdp[IFLA_XDP_FLAGS]) {
2230 xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
2231 if (xdp_flags & ~XDP_FLAGS_MASK) {
2232 err = -EINVAL;
2233 goto errout;
2234 }
Jakub Kicinskiee5d0322017-06-21 18:25:04 -07002235 if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
Daniel Borkmann0489df92017-05-12 01:04:45 +02002236 err = -EINVAL;
2237 goto errout;
2238 }
Daniel Borkmann85de8572016-11-28 23:16:54 +01002239 }
2240
Brenden Blancod1fdd912016-07-19 12:16:49 -07002241 if (xdp[IFLA_XDP_FD]) {
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002242 err = dev_change_xdp_fd(dev, extack,
Daniel Borkmann85de8572016-11-28 23:16:54 +01002243 nla_get_s32(xdp[IFLA_XDP_FD]),
2244 xdp_flags);
Brenden Blancod1fdd912016-07-19 12:16:49 -07002245 if (err)
2246 goto errout;
2247 status |= DO_SETLINK_NOTIFY;
2248 }
2249 }
2250
Patrick McHardy0157f602007-06-13 12:03:36 -07002251errout:
Nicolas Dichtelba998902014-09-01 16:07:29 +02002252 if (status & DO_SETLINK_MODIFIED) {
2253 if (status & DO_SETLINK_NOTIFY)
2254 netdev_state_change(dev);
2255
2256 if (err < 0)
2257 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",
2258 dev->name);
2259 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002260
Patrick McHardy0157f602007-06-13 12:03:36 -07002261 return err;
2262}
2263
David Ahernc21ef3e2017-04-16 09:48:24 -07002264static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2265 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002267 struct net *net = sock_net(skb->sk);
Thomas Grafda5e0492006-08-10 21:17:37 -07002268 struct ifinfomsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 struct net_device *dev;
Patrick McHardy0157f602007-06-13 12:03:36 -07002270 int err;
Thomas Grafda5e0492006-08-10 21:17:37 -07002271 struct nlattr *tb[IFLA_MAX+1];
2272 char ifname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
David Ahernc21ef3e2017-04-16 09:48:24 -07002274 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy,
2275 extack);
Thomas Grafda5e0492006-08-10 21:17:37 -07002276 if (err < 0)
2277 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
Thomas Graf5176f912006-08-26 20:13:18 -07002279 if (tb[IFLA_IFNAME])
2280 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
Patrick McHardy78e5b8912006-09-13 20:35:36 -07002281 else
2282 ifname[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 err = -EINVAL;
Thomas Grafda5e0492006-08-10 21:17:37 -07002285 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -07002286 if (ifm->ifi_index > 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00002287 dev = __dev_get_by_index(net, ifm->ifi_index);
Thomas Grafda5e0492006-08-10 21:17:37 -07002288 else if (tb[IFLA_IFNAME])
Eric Dumazeta3d12892009-10-21 10:59:31 +00002289 dev = __dev_get_by_name(net, ifname);
Thomas Grafda5e0492006-08-10 21:17:37 -07002290 else
2291 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
Thomas Grafda5e0492006-08-10 21:17:37 -07002293 if (dev == NULL) {
2294 err = -ENODEV;
2295 goto errout;
2296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
Eric Dumazete0d087a2009-11-07 01:26:17 -08002298 err = validate_linkmsg(dev, tb);
2299 if (err < 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00002300 goto errout;
Thomas Grafda5e0492006-08-10 21:17:37 -07002301
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002302 err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
Thomas Grafda5e0492006-08-10 21:17:37 -07002303errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 return err;
2305}
2306
WANG Cong66400d52015-03-24 11:53:31 -07002307static int rtnl_group_dellink(const struct net *net, int group)
2308{
2309 struct net_device *dev, *aux;
2310 LIST_HEAD(list_kill);
2311 bool found = false;
2312
2313 if (!group)
2314 return -EPERM;
2315
2316 for_each_netdev(net, dev) {
2317 if (dev->group == group) {
2318 const struct rtnl_link_ops *ops;
2319
2320 found = true;
2321 ops = dev->rtnl_link_ops;
2322 if (!ops || !ops->dellink)
2323 return -EOPNOTSUPP;
2324 }
2325 }
2326
2327 if (!found)
2328 return -ENODEV;
2329
2330 for_each_netdev_safe(net, dev, aux) {
2331 if (dev->group == group) {
2332 const struct rtnl_link_ops *ops;
2333
2334 ops = dev->rtnl_link_ops;
2335 ops->dellink(dev, &list_kill);
2336 }
2337 }
2338 unregister_netdevice_many(&list_kill);
2339
2340 return 0;
2341}
2342
Thomas Graf614732e2015-07-21 10:44:06 +02002343int rtnl_delete_link(struct net_device *dev)
2344{
2345 const struct rtnl_link_ops *ops;
2346 LIST_HEAD(list_kill);
2347
2348 ops = dev->rtnl_link_ops;
2349 if (!ops || !ops->dellink)
2350 return -EOPNOTSUPP;
2351
2352 ops->dellink(dev, &list_kill);
2353 unregister_netdevice_many(&list_kill);
2354
2355 return 0;
2356}
2357EXPORT_SYMBOL_GPL(rtnl_delete_link);
2358
David Ahernc21ef3e2017-04-16 09:48:24 -07002359static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
2360 struct netlink_ext_ack *extack)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002361{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002362 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002363 struct net_device *dev;
2364 struct ifinfomsg *ifm;
2365 char ifname[IFNAMSIZ];
2366 struct nlattr *tb[IFLA_MAX+1];
2367 int err;
2368
David Ahernc21ef3e2017-04-16 09:48:24 -07002369 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002370 if (err < 0)
2371 return err;
2372
2373 if (tb[IFLA_IFNAME])
2374 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2375
2376 ifm = nlmsg_data(nlh);
2377 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07002378 dev = __dev_get_by_index(net, ifm->ifi_index);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002379 else if (tb[IFLA_IFNAME])
Eric W. Biederman881d9662007-09-17 11:56:21 -07002380 dev = __dev_get_by_name(net, ifname);
WANG Cong66400d52015-03-24 11:53:31 -07002381 else if (tb[IFLA_GROUP])
2382 return rtnl_group_dellink(net, nla_get_u32(tb[IFLA_GROUP]));
Patrick McHardy38f7b872007-06-13 12:03:51 -07002383 else
2384 return -EINVAL;
2385
2386 if (!dev)
2387 return -ENODEV;
2388
Thomas Graf614732e2015-07-21 10:44:06 +02002389 return rtnl_delete_link(dev);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002390}
2391
Patrick McHardy3729d502010-02-26 06:34:54 +00002392int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
2393{
2394 unsigned int old_flags;
2395 int err;
2396
2397 old_flags = dev->flags;
2398 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
2399 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
2400 if (err < 0)
2401 return err;
2402 }
2403
2404 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
Patrick McHardy3729d502010-02-26 06:34:54 +00002405
Nicolas Dichtela528c212013-09-25 12:02:44 +02002406 __dev_notify_flags(dev, old_flags, ~0U);
Patrick McHardy3729d502010-02-26 06:34:54 +00002407 return 0;
2408}
2409EXPORT_SYMBOL(rtnl_configure_link);
2410
Rami Rosenc0713562012-11-30 01:08:47 +00002411struct net_device *rtnl_create_link(struct net *net,
Thomas Graf78ebb0d2015-04-10 01:45:53 +02002412 const char *ifname, unsigned char name_assign_type,
Tom Gundersen55177502014-07-14 16:37:25 +02002413 const struct rtnl_link_ops *ops, struct nlattr *tb[])
Pavel Emelianove7199282007-08-08 22:16:38 -07002414{
Pavel Emelianove7199282007-08-08 22:16:38 -07002415 struct net_device *dev;
Jiri Pirkod40156a2012-07-20 02:28:47 +00002416 unsigned int num_tx_queues = 1;
2417 unsigned int num_rx_queues = 1;
Pavel Emelianove7199282007-08-08 22:16:38 -07002418
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002419 if (tb[IFLA_NUM_TX_QUEUES])
2420 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
2421 else if (ops->get_num_tx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002422 num_tx_queues = ops->get_num_tx_queues();
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002423
2424 if (tb[IFLA_NUM_RX_QUEUES])
2425 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
2426 else if (ops->get_num_rx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002427 num_rx_queues = ops->get_num_rx_queues();
stephen hemmingerefacb302012-04-10 18:34:43 +00002428
Tom Gundersen55177502014-07-14 16:37:25 +02002429 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
Tom Gundersenc835a672014-07-14 16:37:24 +02002430 ops->setup, num_tx_queues, num_rx_queues);
Pavel Emelianove7199282007-08-08 22:16:38 -07002431 if (!dev)
Tobias Klauserd1892e42017-02-20 16:32:06 +01002432 return ERR_PTR(-ENOMEM);
Pavel Emelianove7199282007-08-08 22:16:38 -07002433
Eric W. Biederman81adee42009-11-08 00:53:51 -08002434 dev_net_set(dev, net);
2435 dev->rtnl_link_ops = ops;
Patrick McHardy3729d502010-02-26 06:34:54 +00002436 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
Eric W. Biederman81adee42009-11-08 00:53:51 -08002437
Pavel Emelianove7199282007-08-08 22:16:38 -07002438 if (tb[IFLA_MTU])
2439 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002440 if (tb[IFLA_ADDRESS]) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002441 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
2442 nla_len(tb[IFLA_ADDRESS]));
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002443 dev->addr_assign_type = NET_ADDR_SET;
2444 }
Pavel Emelianove7199282007-08-08 22:16:38 -07002445 if (tb[IFLA_BROADCAST])
2446 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
2447 nla_len(tb[IFLA_BROADCAST]));
2448 if (tb[IFLA_TXQLEN])
2449 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
2450 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002451 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Pavel Emelianove7199282007-08-08 22:16:38 -07002452 if (tb[IFLA_LINKMODE])
2453 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
Patrick McHardyffa934f2011-01-20 03:00:42 +00002454 if (tb[IFLA_GROUP])
2455 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Pavel Emelianove7199282007-08-08 22:16:38 -07002456
2457 return dev;
Pavel Emelianove7199282007-08-08 22:16:38 -07002458}
Eric Dumazete0d087a2009-11-07 01:26:17 -08002459EXPORT_SYMBOL(rtnl_create_link);
Pavel Emelianove7199282007-08-08 22:16:38 -07002460
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002461static int rtnl_group_changelink(const struct sk_buff *skb,
2462 struct net *net, int group,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002463 struct ifinfomsg *ifm,
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002464 struct netlink_ext_ack *extack,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002465 struct nlattr **tb)
2466{
WANG Congd0795352015-03-23 16:31:09 -07002467 struct net_device *dev, *aux;
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002468 int err;
2469
WANG Congd0795352015-03-23 16:31:09 -07002470 for_each_netdev_safe(net, dev, aux) {
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002471 if (dev->group == group) {
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002472 err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002473 if (err < 0)
2474 return err;
2475 }
2476 }
2477
2478 return 0;
2479}
2480
David Ahernc21ef3e2017-04-16 09:48:24 -07002481static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2482 struct netlink_ext_ack *extack)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002483{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002484 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002485 const struct rtnl_link_ops *ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002486 const struct rtnl_link_ops *m_ops = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002487 struct net_device *dev;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002488 struct net_device *master_dev = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002489 struct ifinfomsg *ifm;
2490 char kind[MODULE_NAME_LEN];
2491 char ifname[IFNAMSIZ];
2492 struct nlattr *tb[IFLA_MAX+1];
2493 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
Tom Gundersen55177502014-07-14 16:37:25 +02002494 unsigned char name_assign_type = NET_NAME_USER;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002495 int err;
2496
Johannes Berg95a5afc2008-10-16 15:24:51 -07002497#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002498replay:
Thomas Graf8072f082007-07-31 14:13:50 -07002499#endif
David Ahernc21ef3e2017-04-16 09:48:24 -07002500 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002501 if (err < 0)
2502 return err;
2503
2504 if (tb[IFLA_IFNAME])
2505 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2506 else
2507 ifname[0] = '\0';
2508
2509 ifm = nlmsg_data(nlh);
2510 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07002511 dev = __dev_get_by_index(net, ifm->ifi_index);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002512 else {
2513 if (ifname[0])
2514 dev = __dev_get_by_name(net, ifname);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002515 else
2516 dev = NULL;
2517 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002518
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002519 if (dev) {
2520 master_dev = netdev_master_upper_dev_get(dev);
2521 if (master_dev)
2522 m_ops = master_dev->rtnl_link_ops;
2523 }
2524
Eric Dumazete0d087a2009-11-07 01:26:17 -08002525 err = validate_linkmsg(dev, tb);
2526 if (err < 0)
Thomas Graf1840bb12008-02-23 19:54:36 -08002527 return err;
2528
Patrick McHardy38f7b872007-06-13 12:03:51 -07002529 if (tb[IFLA_LINKINFO]) {
2530 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +02002531 tb[IFLA_LINKINFO], ifla_info_policy,
2532 NULL);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002533 if (err < 0)
2534 return err;
2535 } else
2536 memset(linkinfo, 0, sizeof(linkinfo));
2537
2538 if (linkinfo[IFLA_INFO_KIND]) {
2539 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
2540 ops = rtnl_link_ops_get(kind);
2541 } else {
2542 kind[0] = '\0';
2543 ops = NULL;
2544 }
2545
2546 if (1) {
Sasha Levin4e10fd52015-02-24 14:14:35 -05002547 struct nlattr *attr[ops ? ops->maxtype + 1 : 1];
2548 struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1];
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002549 struct nlattr **data = NULL;
2550 struct nlattr **slave_data = NULL;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002551 struct net *dest_net, *link_net = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002552
2553 if (ops) {
2554 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
2555 err = nla_parse_nested(attr, ops->maxtype,
2556 linkinfo[IFLA_INFO_DATA],
Johannes Bergfceb6432017-04-12 14:34:07 +02002557 ops->policy, NULL);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002558 if (err < 0)
2559 return err;
2560 data = attr;
2561 }
2562 if (ops->validate) {
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02002563 err = ops->validate(tb, data, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002564 if (err < 0)
2565 return err;
2566 }
2567 }
2568
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002569 if (m_ops) {
2570 if (m_ops->slave_maxtype &&
2571 linkinfo[IFLA_INFO_SLAVE_DATA]) {
2572 err = nla_parse_nested(slave_attr,
2573 m_ops->slave_maxtype,
2574 linkinfo[IFLA_INFO_SLAVE_DATA],
Johannes Bergfceb6432017-04-12 14:34:07 +02002575 m_ops->slave_policy,
2576 NULL);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002577 if (err < 0)
2578 return err;
2579 slave_data = slave_attr;
2580 }
2581 if (m_ops->slave_validate) {
Matthias Schifferd116ffc2017-06-25 23:56:03 +02002582 err = m_ops->slave_validate(tb, slave_data,
2583 extack);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002584 if (err < 0)
2585 return err;
2586 }
2587 }
2588
Patrick McHardy38f7b872007-06-13 12:03:51 -07002589 if (dev) {
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002590 int status = 0;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002591
2592 if (nlh->nlmsg_flags & NLM_F_EXCL)
2593 return -EEXIST;
2594 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2595 return -EOPNOTSUPP;
2596
2597 if (linkinfo[IFLA_INFO_DATA]) {
2598 if (!ops || ops != dev->rtnl_link_ops ||
2599 !ops->changelink)
2600 return -EOPNOTSUPP;
2601
Matthias Schifferad744b22017-06-25 23:56:00 +02002602 err = ops->changelink(dev, tb, data, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002603 if (err < 0)
2604 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002605 status |= DO_SETLINK_NOTIFY;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002606 }
2607
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002608 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2609 if (!m_ops || !m_ops->slave_changelink)
2610 return -EOPNOTSUPP;
2611
2612 err = m_ops->slave_changelink(master_dev, dev,
Matthias Schiffer17dd0ec2017-06-25 23:56:02 +02002613 tb, slave_data,
2614 extack);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002615 if (err < 0)
2616 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002617 status |= DO_SETLINK_NOTIFY;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002618 }
2619
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002620 return do_setlink(skb, dev, ifm, extack, tb, ifname,
2621 status);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002622 }
2623
Patrick McHardyffa934f2011-01-20 03:00:42 +00002624 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2625 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002626 return rtnl_group_changelink(skb, net,
Patrick McHardyffa934f2011-01-20 03:00:42 +00002627 nla_get_u32(tb[IFLA_GROUP]),
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002628 ifm, extack, tb);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002629 return -ENODEV;
Patrick McHardyffa934f2011-01-20 03:00:42 +00002630 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002631
Theuns Verwoerd160ca012017-01-31 12:23:46 +13002632 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
Patrick McHardy38f7b872007-06-13 12:03:51 -07002633 return -EOPNOTSUPP;
2634
2635 if (!ops) {
Johannes Berg95a5afc2008-10-16 15:24:51 -07002636#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002637 if (kind[0]) {
2638 __rtnl_unlock();
2639 request_module("rtnl-link-%s", kind);
2640 rtnl_lock();
2641 ops = rtnl_link_ops_get(kind);
2642 if (ops)
2643 goto replay;
2644 }
2645#endif
2646 return -EOPNOTSUPP;
2647 }
2648
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02002649 if (!ops->setup)
2650 return -EOPNOTSUPP;
2651
Tom Gundersen55177502014-07-14 16:37:25 +02002652 if (!ifname[0]) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07002653 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
Tom Gundersen55177502014-07-14 16:37:25 +02002654 name_assign_type = NET_NAME_ENUM;
2655 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002656
Eric W. Biederman81adee42009-11-08 00:53:51 -08002657 dest_net = rtnl_link_get_net(net, tb);
Eric W. Biederman13ad1772011-01-29 14:57:22 +00002658 if (IS_ERR(dest_net))
2659 return PTR_ERR(dest_net);
2660
Eric W. Biederman505ce412015-02-26 16:19:00 -06002661 err = -EPERM;
2662 if (!netlink_ns_capable(skb, dest_net->user_ns, CAP_NET_ADMIN))
2663 goto out;
2664
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002665 if (tb[IFLA_LINK_NETNSID]) {
2666 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
2667
2668 link_net = get_net_ns_by_id(dest_net, id);
2669 if (!link_net) {
2670 err = -EINVAL;
2671 goto out;
2672 }
Eric W. Biederman06615be2015-02-26 16:20:07 -06002673 err = -EPERM;
2674 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
2675 goto out;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002676 }
2677
2678 dev = rtnl_create_link(link_net ? : dest_net, ifname,
2679 name_assign_type, ops, tb);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002680 if (IS_ERR(dev)) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002681 err = PTR_ERR(dev);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002682 goto out;
2683 }
2684
2685 dev->ifindex = ifm->ifi_index;
2686
Cong Wang0e0eee22014-02-11 15:51:30 -08002687 if (ops->newlink) {
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02002688 err = ops->newlink(link_net ? : net, dev, tb, data,
2689 extack);
Cong Wang0e0eee22014-02-11 15:51:30 -08002690 /* Drivers should call free_netdev() in ->destructor
Cong Wange51fb152014-06-03 16:40:47 -07002691 * and unregister it on failure after registration
2692 * so that device could be finally freed in rtnl_unlock.
Cong Wang0e0eee22014-02-11 15:51:30 -08002693 */
Cong Wange51fb152014-06-03 16:40:47 -07002694 if (err < 0) {
2695 /* If device is not registered at all, free it now */
2696 if (dev->reg_state == NETREG_UNINITIALIZED)
2697 free_netdev(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002698 goto out;
Cong Wange51fb152014-06-03 16:40:47 -07002699 }
Cong Wang0e0eee22014-02-11 15:51:30 -08002700 } else {
Patrick McHardy2d85cba2007-07-11 19:42:13 -07002701 err = register_netdevice(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002702 if (err < 0) {
2703 free_netdev(dev);
2704 goto out;
2705 }
Dan Carpenterfce9b9b2013-08-14 12:35:42 +03002706 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002707 err = rtnl_configure_link(dev, ifm);
David S. Miller43638902015-03-10 21:58:32 -04002708 if (err < 0)
2709 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002710 if (link_net) {
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002711 err = dev_change_net_namespace(dev, dest_net, ifname);
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002712 if (err < 0)
David S. Miller43638902015-03-10 21:58:32 -04002713 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002714 }
Theuns Verwoerd160ca012017-01-31 12:23:46 +13002715 if (tb[IFLA_MASTER]) {
2716 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
2717 if (err)
2718 goto out_unregister;
2719 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002720out:
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002721 if (link_net)
2722 put_net(link_net);
Eric W. Biederman81adee42009-11-08 00:53:51 -08002723 put_net(dest_net);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002724 return err;
David S. Miller43638902015-03-10 21:58:32 -04002725out_unregister:
2726 if (ops->newlink) {
2727 LIST_HEAD(list_kill);
2728
2729 ops->dellink(dev, &list_kill);
2730 unregister_netdevice_many(&list_kill);
2731 } else {
2732 unregister_netdevice(dev);
2733 }
2734 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002735 }
2736}
2737
David Ahernc21ef3e2017-04-16 09:48:24 -07002738static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2739 struct netlink_ext_ack *extack)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002740{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002741 struct net *net = sock_net(skb->sk);
Thomas Grafb60c5112006-08-04 23:05:34 -07002742 struct ifinfomsg *ifm;
Eric Dumazeta3d12892009-10-21 10:59:31 +00002743 char ifname[IFNAMSIZ];
Thomas Grafb60c5112006-08-04 23:05:34 -07002744 struct nlattr *tb[IFLA_MAX+1];
2745 struct net_device *dev = NULL;
2746 struct sk_buff *nskb;
Thomas Graf339bf982006-11-10 14:10:15 -08002747 int err;
Greg Rose115c9b82012-02-21 16:54:48 -05002748 u32 ext_filter_mask = 0;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002749
David Ahernc21ef3e2017-04-16 09:48:24 -07002750 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Thomas Grafb60c5112006-08-04 23:05:34 -07002751 if (err < 0)
Eric Sesterhenn9918f232006-09-26 23:26:38 -07002752 return err;
Thomas Grafb60c5112006-08-04 23:05:34 -07002753
Eric Dumazeta3d12892009-10-21 10:59:31 +00002754 if (tb[IFLA_IFNAME])
2755 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2756
Greg Rose115c9b82012-02-21 16:54:48 -05002757 if (tb[IFLA_EXT_MASK])
2758 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2759
Thomas Grafb60c5112006-08-04 23:05:34 -07002760 ifm = nlmsg_data(nlh);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002761 if (ifm->ifi_index > 0)
2762 dev = __dev_get_by_index(net, ifm->ifi_index);
2763 else if (tb[IFLA_IFNAME])
2764 dev = __dev_get_by_name(net, ifname);
2765 else
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002766 return -EINVAL;
Thomas Grafb60c5112006-08-04 23:05:34 -07002767
Eric Dumazeta3d12892009-10-21 10:59:31 +00002768 if (dev == NULL)
2769 return -ENODEV;
2770
Greg Rose115c9b82012-02-21 16:54:48 -05002771 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002772 if (nskb == NULL)
2773 return -ENOBUFS;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002774
Eric W. Biederman15e47302012-09-07 20:12:54 +00002775 err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04002776 nlh->nlmsg_seq, 0, 0, ext_filter_mask, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08002777 if (err < 0) {
2778 /* -EMSGSIZE implies BUG in if_nlmsg_size */
2779 WARN_ON(err == -EMSGSIZE);
2780 kfree_skb(nskb);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002781 } else
Eric W. Biederman15e47302012-09-07 20:12:54 +00002782 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
Thomas Grafb60c5112006-08-04 23:05:34 -07002783
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002784 return err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002785}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002786
Greg Rose115c9b82012-02-21 16:54:48 -05002787static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
Greg Rosec7ac8672011-06-10 01:27:09 +00002788{
Greg Rose115c9b82012-02-21 16:54:48 -05002789 struct net *net = sock_net(skb->sk);
2790 struct net_device *dev;
2791 struct nlattr *tb[IFLA_MAX+1];
2792 u32 ext_filter_mask = 0;
2793 u16 min_ifinfo_dump_size = 0;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02002794 int hdrlen;
Greg Rose115c9b82012-02-21 16:54:48 -05002795
Michal Schmidte5eca6d2014-05-28 14:15:19 +02002796 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2797 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2798 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2799
Johannes Bergfceb6432017-04-12 14:34:07 +02002800 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00002801 if (tb[IFLA_EXT_MASK])
2802 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2803 }
Greg Rose115c9b82012-02-21 16:54:48 -05002804
2805 if (!ext_filter_mask)
2806 return NLMSG_GOODSIZE;
2807 /*
2808 * traverse the list of net devices and compute the minimum
2809 * buffer size based upon the filter mask.
2810 */
Florian Westphal6853dd42017-08-09 20:41:51 +02002811 rcu_read_lock();
2812 for_each_netdev_rcu(net, dev) {
Greg Rose115c9b82012-02-21 16:54:48 -05002813 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
2814 if_nlmsg_size(dev,
2815 ext_filter_mask));
2816 }
Florian Westphal6853dd42017-08-09 20:41:51 +02002817 rcu_read_unlock();
Greg Rose115c9b82012-02-21 16:54:48 -05002818
Zhang Shengju93af2052016-11-22 14:14:28 +08002819 return nlmsg_total_size(min_ifinfo_dump_size);
Greg Rosec7ac8672011-06-10 01:27:09 +00002820}
2821
Adrian Bunk42bad1d2007-04-26 00:57:41 -07002822static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823{
2824 int idx;
2825 int s_idx = cb->family;
2826
2827 if (s_idx == 0)
2828 s_idx = 1;
Florian Westphal6853dd42017-08-09 20:41:51 +02002829
Patrick McHardy25239ce2010-04-26 16:02:05 +02002830 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 int type = cb->nlh->nlmsg_type-RTM_BASE;
Florian Westphal6853dd42017-08-09 20:41:51 +02002832 struct rtnl_link *handlers;
2833 rtnl_dumpit_func dumpit;
2834
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 if (idx < s_idx || idx == PF_PACKET)
2836 continue;
Florian Westphal6853dd42017-08-09 20:41:51 +02002837
2838 handlers = rtnl_dereference(rtnl_msg_handlers[idx]);
2839 if (!handlers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 continue;
Florian Westphal6853dd42017-08-09 20:41:51 +02002841
2842 dumpit = READ_ONCE(handlers[type].dumpit);
2843 if (!dumpit)
2844 continue;
2845
Nicolas Dichtel04652772013-03-22 06:28:42 +00002846 if (idx > s_idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 memset(&cb->args[0], 0, sizeof(cb->args));
Nicolas Dichtel04652772013-03-22 06:28:42 +00002848 cb->prev_seq = 0;
2849 cb->seq = 0;
2850 }
Florian Westphal6853dd42017-08-09 20:41:51 +02002851 if (dumpit(skb, cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 break;
2853 }
2854 cb->family = idx;
2855
2856 return skb->len;
2857}
2858
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002859struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04002860 unsigned int change,
2861 u32 event, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002863 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 struct sk_buff *skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002865 int err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00002866 size_t if_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867
Alexei Starovoitov7f294052013-10-23 16:02:42 -07002868 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002869 if (skb == NULL)
2870 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04002872 err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0, event);
Patrick McHardy26932562007-01-31 23:16:40 -08002873 if (err < 0) {
2874 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
2875 WARN_ON(err == -EMSGSIZE);
2876 kfree_skb(skb);
2877 goto errout;
2878 }
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002879 return skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002880errout:
2881 if (err < 0)
Eric W. Biederman4b3da702007-11-19 22:27:40 -08002882 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002883 return NULL;
2884}
2885
2886void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
2887{
2888 struct net *net = dev_net(dev);
2889
2890 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
2891}
2892
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04002893static void rtmsg_ifinfo_event(int type, struct net_device *dev,
2894 unsigned int change, u32 event,
2895 gfp_t flags)
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002896{
2897 struct sk_buff *skb;
2898
Nicolas Dichteled2a80a2015-05-13 14:19:42 +02002899 if (dev->reg_state != NETREG_REGISTERED)
2900 return;
2901
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04002902 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002903 if (skb)
2904 rtmsg_ifinfo_send(skb, dev, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905}
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04002906
2907void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
2908 gfp_t flags)
2909{
Vlad Yasevich8c6c9182017-05-30 10:07:02 -04002910 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags);
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04002911}
Jiri Pirko471cb5a2013-01-03 22:49:01 +00002912EXPORT_SYMBOL(rtmsg_ifinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913
John Fastabendd83b0602012-04-15 06:44:08 +00002914static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
2915 struct net_device *dev,
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002916 u8 *addr, u16 vid, u32 pid, u32 seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002917 int type, unsigned int flags,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002918 int nlflags, u16 ndm_state)
John Fastabendd83b0602012-04-15 06:44:08 +00002919{
2920 struct nlmsghdr *nlh;
2921 struct ndmsg *ndm;
2922
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002923 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
John Fastabendd83b0602012-04-15 06:44:08 +00002924 if (!nlh)
2925 return -EMSGSIZE;
2926
2927 ndm = nlmsg_data(nlh);
2928 ndm->ndm_family = AF_BRIDGE;
2929 ndm->ndm_pad1 = 0;
2930 ndm->ndm_pad2 = 0;
2931 ndm->ndm_flags = flags;
2932 ndm->ndm_type = 0;
2933 ndm->ndm_ifindex = dev->ifindex;
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002934 ndm->ndm_state = ndm_state;
John Fastabendd83b0602012-04-15 06:44:08 +00002935
2936 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
2937 goto nla_put_failure;
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002938 if (vid)
2939 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
2940 goto nla_put_failure;
John Fastabendd83b0602012-04-15 06:44:08 +00002941
Johannes Berg053c0952015-01-16 22:09:00 +01002942 nlmsg_end(skb, nlh);
2943 return 0;
John Fastabendd83b0602012-04-15 06:44:08 +00002944
2945nla_put_failure:
2946 nlmsg_cancel(skb, nlh);
2947 return -EMSGSIZE;
2948}
2949
John Fastabend3ff661c2012-04-15 06:44:14 +00002950static inline size_t rtnl_fdb_nlmsg_size(void)
2951{
Sabrina Dubrocaf82ef3e2016-11-18 15:50:39 +01002952 return NLMSG_ALIGN(sizeof(struct ndmsg)) +
2953 nla_total_size(ETH_ALEN) + /* NDA_LLADDR */
2954 nla_total_size(sizeof(u16)) + /* NDA_VLAN */
2955 0;
John Fastabend3ff661c2012-04-15 06:44:14 +00002956}
2957
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002958static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
2959 u16 ndm_state)
John Fastabend3ff661c2012-04-15 06:44:14 +00002960{
2961 struct net *net = dev_net(dev);
2962 struct sk_buff *skb;
2963 int err = -ENOBUFS;
2964
2965 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
2966 if (!skb)
2967 goto errout;
2968
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002969 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002970 0, 0, type, NTF_SELF, 0, ndm_state);
John Fastabend3ff661c2012-04-15 06:44:14 +00002971 if (err < 0) {
2972 kfree_skb(skb);
2973 goto errout;
2974 }
2975
2976 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
2977 return;
2978errout:
2979 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
2980}
2981
Vlad Yasevich090096b2013-03-06 15:39:42 +00002982/**
2983 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2984 */
2985int ndo_dflt_fdb_add(struct ndmsg *ndm,
2986 struct nlattr *tb[],
2987 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01002988 const unsigned char *addr, u16 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00002989 u16 flags)
2990{
2991 int err = -EINVAL;
2992
2993 /* If aging addresses are supported device will need to
2994 * implement its own handler for this.
2995 */
2996 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2997 pr_info("%s: FDB only supports static addresses\n", dev->name);
2998 return err;
2999 }
3000
Or Gerlitz65891fe2014-12-14 18:19:05 +02003001 if (vid) {
3002 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
3003 return err;
3004 }
3005
Vlad Yasevich090096b2013-03-06 15:39:42 +00003006 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3007 err = dev_uc_add_excl(dev, addr);
3008 else if (is_multicast_ether_addr(addr))
3009 err = dev_mc_add_excl(dev, addr);
3010
3011 /* Only return duplicate errors if NLM_F_EXCL is set */
3012 if (err == -EEXIST && !(flags & NLM_F_EXCL))
3013 err = 0;
3014
3015 return err;
3016}
3017EXPORT_SYMBOL(ndo_dflt_fdb_add);
3018
Jiri Pirkof6f64242014-11-28 14:34:15 +01003019static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid)
3020{
3021 u16 vid = 0;
3022
3023 if (vlan_attr) {
3024 if (nla_len(vlan_attr) != sizeof(u16)) {
3025 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n");
3026 return -EINVAL;
3027 }
3028
3029 vid = nla_get_u16(vlan_attr);
3030
3031 if (!vid || vid >= VLAN_VID_MASK) {
3032 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n",
3033 vid);
3034 return -EINVAL;
3035 }
3036 }
3037 *p_vid = vid;
3038 return 0;
3039}
3040
David Ahernc21ef3e2017-04-16 09:48:24 -07003041static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
3042 struct netlink_ext_ack *extack)
John Fastabend77162022012-04-15 06:43:56 +00003043{
3044 struct net *net = sock_net(skb->sk);
John Fastabend77162022012-04-15 06:43:56 +00003045 struct ndmsg *ndm;
3046 struct nlattr *tb[NDA_MAX+1];
3047 struct net_device *dev;
3048 u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01003049 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00003050 int err;
3051
David Ahernc21ef3e2017-04-16 09:48:24 -07003052 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
John Fastabend77162022012-04-15 06:43:56 +00003053 if (err < 0)
3054 return err;
3055
3056 ndm = nlmsg_data(nlh);
3057 if (ndm->ndm_ifindex == 0) {
3058 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
3059 return -EINVAL;
3060 }
3061
3062 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3063 if (dev == NULL) {
3064 pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
3065 return -ENODEV;
3066 }
3067
3068 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3069 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
3070 return -EINVAL;
3071 }
3072
3073 addr = nla_data(tb[NDA_LLADDR]);
John Fastabend77162022012-04-15 06:43:56 +00003074
Jiri Pirkof6f64242014-11-28 14:34:15 +01003075 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
3076 if (err)
3077 return err;
3078
John Fastabend77162022012-04-15 06:43:56 +00003079 err = -EOPNOTSUPP;
3080
3081 /* Support fdb on master device the net/bridge default case */
3082 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3083 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003084 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3085 const struct net_device_ops *ops = br_dev->netdev_ops;
3086
Jiri Pirkof6f64242014-11-28 14:34:15 +01003087 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
3088 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00003089 if (err)
3090 goto out;
3091 else
3092 ndm->ndm_flags &= ~NTF_MASTER;
3093 }
3094
3095 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00003096 if ((ndm->ndm_flags & NTF_SELF)) {
3097 if (dev->netdev_ops->ndo_fdb_add)
3098 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003099 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003100 nlh->nlmsg_flags);
3101 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01003102 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003103 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00003104
John Fastabend3ff661c2012-04-15 06:44:14 +00003105 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003106 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
3107 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00003108 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00003109 }
John Fastabend77162022012-04-15 06:43:56 +00003110 }
3111out:
3112 return err;
3113}
3114
Vlad Yasevich090096b2013-03-06 15:39:42 +00003115/**
3116 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3117 */
3118int ndo_dflt_fdb_del(struct ndmsg *ndm,
3119 struct nlattr *tb[],
3120 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003121 const unsigned char *addr, u16 vid)
Vlad Yasevich090096b2013-03-06 15:39:42 +00003122{
Alexander Duyckc8a89c42014-07-15 15:15:20 -07003123 int err = -EINVAL;
Vlad Yasevich090096b2013-03-06 15:39:42 +00003124
3125 /* If aging addresses are supported device will need to
3126 * implement its own handler for this.
3127 */
Sridhar Samudrala64535992013-08-08 15:19:48 -07003128 if (!(ndm->ndm_state & NUD_PERMANENT)) {
Vlad Yasevich090096b2013-03-06 15:39:42 +00003129 pr_info("%s: FDB only supports static addresses\n", dev->name);
Alexander Duyckc8a89c42014-07-15 15:15:20 -07003130 return err;
Vlad Yasevich090096b2013-03-06 15:39:42 +00003131 }
3132
3133 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3134 err = dev_uc_del(dev, addr);
3135 else if (is_multicast_ether_addr(addr))
3136 err = dev_mc_del(dev, addr);
Vlad Yasevich090096b2013-03-06 15:39:42 +00003137
3138 return err;
3139}
3140EXPORT_SYMBOL(ndo_dflt_fdb_del);
3141
David Ahernc21ef3e2017-04-16 09:48:24 -07003142static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
3143 struct netlink_ext_ack *extack)
John Fastabend77162022012-04-15 06:43:56 +00003144{
3145 struct net *net = sock_net(skb->sk);
3146 struct ndmsg *ndm;
Vlad Yasevich1690be62013-02-13 12:00:18 +00003147 struct nlattr *tb[NDA_MAX+1];
John Fastabend77162022012-04-15 06:43:56 +00003148 struct net_device *dev;
3149 int err = -EINVAL;
3150 __u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01003151 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00003152
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07003153 if (!netlink_capable(skb, CAP_NET_ADMIN))
Vlad Yasevich1690be62013-02-13 12:00:18 +00003154 return -EPERM;
3155
David Ahernc21ef3e2017-04-16 09:48:24 -07003156 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
Vlad Yasevich1690be62013-02-13 12:00:18 +00003157 if (err < 0)
3158 return err;
John Fastabend77162022012-04-15 06:43:56 +00003159
3160 ndm = nlmsg_data(nlh);
3161 if (ndm->ndm_ifindex == 0) {
3162 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
3163 return -EINVAL;
3164 }
3165
3166 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3167 if (dev == NULL) {
3168 pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
3169 return -ENODEV;
3170 }
3171
Vlad Yasevich1690be62013-02-13 12:00:18 +00003172 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3173 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
John Fastabend77162022012-04-15 06:43:56 +00003174 return -EINVAL;
3175 }
3176
Vlad Yasevich1690be62013-02-13 12:00:18 +00003177 addr = nla_data(tb[NDA_LLADDR]);
Vlad Yasevich1690be62013-02-13 12:00:18 +00003178
Jiri Pirkof6f64242014-11-28 14:34:15 +01003179 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
3180 if (err)
3181 return err;
3182
John Fastabend77162022012-04-15 06:43:56 +00003183 err = -EOPNOTSUPP;
3184
3185 /* Support fdb on master device the net/bridge default case */
3186 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3187 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003188 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3189 const struct net_device_ops *ops = br_dev->netdev_ops;
John Fastabend77162022012-04-15 06:43:56 +00003190
Jiri Pirko898e5062013-01-03 22:48:52 +00003191 if (ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003192 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00003193
3194 if (err)
3195 goto out;
3196 else
3197 ndm->ndm_flags &= ~NTF_MASTER;
3198 }
3199
3200 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00003201 if (ndm->ndm_flags & NTF_SELF) {
3202 if (dev->netdev_ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003203 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
3204 vid);
Vlad Yasevich090096b2013-03-06 15:39:42 +00003205 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01003206 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00003207
John Fastabend3ff661c2012-04-15 06:44:14 +00003208 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003209 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
3210 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00003211 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00003212 }
John Fastabend77162022012-04-15 06:43:56 +00003213 }
3214out:
3215 return err;
3216}
3217
John Fastabendd83b0602012-04-15 06:44:08 +00003218static int nlmsg_populate_fdb(struct sk_buff *skb,
3219 struct netlink_callback *cb,
3220 struct net_device *dev,
3221 int *idx,
3222 struct netdev_hw_addr_list *list)
3223{
3224 struct netdev_hw_addr *ha;
3225 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00003226 u32 portid, seq;
John Fastabendd83b0602012-04-15 06:44:08 +00003227
Eric W. Biederman15e47302012-09-07 20:12:54 +00003228 portid = NETLINK_CB(cb->skb).portid;
John Fastabendd83b0602012-04-15 06:44:08 +00003229 seq = cb->nlh->nlmsg_seq;
3230
3231 list_for_each_entry(ha, &list->list, list) {
Roopa Prabhud2976532016-08-30 21:56:45 -07003232 if (*idx < cb->args[2])
John Fastabendd83b0602012-04-15 06:44:08 +00003233 goto skip;
3234
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003235 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
John Fastabenda7a558f2012-11-01 16:23:10 +00003236 portid, seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003237 RTM_NEWNEIGH, NTF_SELF,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003238 NLM_F_MULTI, NUD_PERMANENT);
John Fastabendd83b0602012-04-15 06:44:08 +00003239 if (err < 0)
3240 return err;
3241skip:
3242 *idx += 1;
3243 }
3244 return 0;
3245}
3246
3247/**
Ben Hutchings2c530402012-07-10 10:55:09 +00003248 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
John Fastabendd83b0602012-04-15 06:44:08 +00003249 * @nlh: netlink message header
3250 * @dev: netdevice
3251 *
3252 * Default netdevice operation to dump the existing unicast address list.
John Fastabend91f3e7b2013-03-29 08:18:37 +00003253 * Returns number of addresses from list put in skb.
John Fastabendd83b0602012-04-15 06:44:08 +00003254 */
3255int ndo_dflt_fdb_dump(struct sk_buff *skb,
3256 struct netlink_callback *cb,
3257 struct net_device *dev,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04003258 struct net_device *filter_dev,
Roopa Prabhud2976532016-08-30 21:56:45 -07003259 int *idx)
John Fastabendd83b0602012-04-15 06:44:08 +00003260{
3261 int err;
3262
3263 netif_addr_lock_bh(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -07003264 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
John Fastabendd83b0602012-04-15 06:44:08 +00003265 if (err)
3266 goto out;
Zhang Shengju2934c9d2016-11-30 16:37:34 +08003267 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
John Fastabendd83b0602012-04-15 06:44:08 +00003268out:
3269 netif_addr_unlock_bh(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -07003270 return err;
John Fastabendd83b0602012-04-15 06:44:08 +00003271}
3272EXPORT_SYMBOL(ndo_dflt_fdb_dump);
3273
John Fastabend77162022012-04-15 06:43:56 +00003274static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
3275{
John Fastabend77162022012-04-15 06:43:56 +00003276 struct net_device *dev;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003277 struct nlattr *tb[IFLA_MAX+1];
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003278 struct net_device *br_dev = NULL;
3279 const struct net_device_ops *ops = NULL;
3280 const struct net_device_ops *cops = NULL;
3281 struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
3282 struct net *net = sock_net(skb->sk);
Roopa Prabhud2976532016-08-30 21:56:45 -07003283 struct hlist_head *head;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003284 int brport_idx = 0;
3285 int br_idx = 0;
Roopa Prabhud2976532016-08-30 21:56:45 -07003286 int h, s_h;
3287 int idx = 0, s_idx;
3288 int err = 0;
3289 int fidx = 0;
John Fastabend77162022012-04-15 06:43:56 +00003290
Alexander Potapenko0ff50e82017-05-23 13:20:28 +02003291 err = nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb,
3292 IFLA_MAX, ifla_policy, NULL);
3293 if (err < 0) {
3294 return -EINVAL;
3295 } else if (err == 0) {
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003296 if (tb[IFLA_MASTER])
3297 br_idx = nla_get_u32(tb[IFLA_MASTER]);
3298 }
John Fastabend77162022012-04-15 06:43:56 +00003299
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003300 brport_idx = ifm->ifi_index;
3301
3302 if (br_idx) {
3303 br_dev = __dev_get_by_index(net, br_idx);
3304 if (!br_dev)
3305 return -ENODEV;
3306
3307 ops = br_dev->netdev_ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003308 }
3309
Roopa Prabhud2976532016-08-30 21:56:45 -07003310 s_h = cb->args[0];
3311 s_idx = cb->args[1];
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003312
Roopa Prabhud2976532016-08-30 21:56:45 -07003313 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
3314 idx = 0;
3315 head = &net->dev_index_head[h];
3316 hlist_for_each_entry(dev, head, index_hlist) {
3317
3318 if (brport_idx && (dev->ifindex != brport_idx))
3319 continue;
3320
3321 if (!br_idx) { /* user did not specify a specific bridge */
3322 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3323 br_dev = netdev_master_upper_dev_get(dev);
3324 cops = br_dev->netdev_ops;
3325 }
3326 } else {
3327 if (dev != br_dev &&
3328 !(dev->priv_flags & IFF_BRIDGE_PORT))
3329 continue;
3330
3331 if (br_dev != netdev_master_upper_dev_get(dev) &&
3332 !(dev->priv_flags & IFF_EBRIDGE))
3333 continue;
3334 cops = ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003335 }
3336
Roopa Prabhud2976532016-08-30 21:56:45 -07003337 if (idx < s_idx)
3338 goto cont;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003339
Roopa Prabhud2976532016-08-30 21:56:45 -07003340 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3341 if (cops && cops->ndo_fdb_dump) {
3342 err = cops->ndo_fdb_dump(skb, cb,
3343 br_dev, dev,
3344 &fidx);
3345 if (err == -EMSGSIZE)
3346 goto out;
3347 }
3348 }
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003349
Roopa Prabhud2976532016-08-30 21:56:45 -07003350 if (dev->netdev_ops->ndo_fdb_dump)
3351 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
3352 dev, NULL,
3353 &fidx);
3354 else
3355 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
3356 &fidx);
3357 if (err == -EMSGSIZE)
3358 goto out;
3359
3360 cops = NULL;
3361
3362 /* reset fdb offset to 0 for rest of the interfaces */
3363 cb->args[2] = 0;
3364 fidx = 0;
3365cont:
3366 idx++;
John Fastabend77162022012-04-15 06:43:56 +00003367 }
John Fastabend77162022012-04-15 06:43:56 +00003368 }
John Fastabend77162022012-04-15 06:43:56 +00003369
Roopa Prabhud2976532016-08-30 21:56:45 -07003370out:
3371 cb->args[0] = h;
3372 cb->args[1] = idx;
3373 cb->args[2] = fidx;
3374
John Fastabend77162022012-04-15 06:43:56 +00003375 return skb->len;
3376}
3377
Scott Feldman2c3c0312014-11-28 14:34:25 +01003378static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
3379 unsigned int attrnum, unsigned int flag)
3380{
3381 if (mask & flag)
3382 return nla_put_u8(skb, attrnum, !!(flags & flag));
3383 return 0;
3384}
3385
John Fastabend815cccb2012-10-24 08:13:09 +00003386int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Scott Feldman2c3c0312014-11-28 14:34:25 +01003387 struct net_device *dev, u16 mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003388 u32 flags, u32 mask, int nlflags,
3389 u32 filter_mask,
3390 int (*vlan_fill)(struct sk_buff *skb,
3391 struct net_device *dev,
3392 u32 filter_mask))
John Fastabend815cccb2012-10-24 08:13:09 +00003393{
3394 struct nlmsghdr *nlh;
3395 struct ifinfomsg *ifm;
3396 struct nlattr *br_afspec;
Scott Feldman2c3c0312014-11-28 14:34:25 +01003397 struct nlattr *protinfo;
John Fastabend815cccb2012-10-24 08:13:09 +00003398 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Jiri Pirko898e5062013-01-03 22:48:52 +00003399 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003400 int err = 0;
John Fastabend815cccb2012-10-24 08:13:09 +00003401
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02003402 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
John Fastabend815cccb2012-10-24 08:13:09 +00003403 if (nlh == NULL)
3404 return -EMSGSIZE;
3405
3406 ifm = nlmsg_data(nlh);
3407 ifm->ifi_family = AF_BRIDGE;
3408 ifm->__ifi_pad = 0;
3409 ifm->ifi_type = dev->type;
3410 ifm->ifi_index = dev->ifindex;
3411 ifm->ifi_flags = dev_get_flags(dev);
3412 ifm->ifi_change = 0;
3413
3414
3415 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
3416 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
3417 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00003418 (br_dev &&
3419 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
John Fastabend815cccb2012-10-24 08:13:09 +00003420 (dev->addr_len &&
3421 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02003422 (dev->ifindex != dev_get_iflink(dev) &&
3423 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
John Fastabend815cccb2012-10-24 08:13:09 +00003424 goto nla_put_failure;
3425
3426 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
3427 if (!br_afspec)
3428 goto nla_put_failure;
3429
Roopa Prabhu1d460b92014-12-08 14:04:20 -08003430 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
John Fastabend815cccb2012-10-24 08:13:09 +00003431 nla_nest_cancel(skb, br_afspec);
3432 goto nla_put_failure;
3433 }
Roopa Prabhu1d460b92014-12-08 14:04:20 -08003434
3435 if (mode != BRIDGE_MODE_UNDEF) {
3436 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
3437 nla_nest_cancel(skb, br_afspec);
3438 goto nla_put_failure;
3439 }
3440 }
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003441 if (vlan_fill) {
3442 err = vlan_fill(skb, dev, filter_mask);
3443 if (err) {
3444 nla_nest_cancel(skb, br_afspec);
3445 goto nla_put_failure;
3446 }
3447 }
John Fastabend815cccb2012-10-24 08:13:09 +00003448 nla_nest_end(skb, br_afspec);
3449
Scott Feldman2c3c0312014-11-28 14:34:25 +01003450 protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
3451 if (!protinfo)
3452 goto nla_put_failure;
3453
3454 if (brport_nla_put_flag(skb, flags, mask,
3455 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
3456 brport_nla_put_flag(skb, flags, mask,
3457 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
3458 brport_nla_put_flag(skb, flags, mask,
3459 IFLA_BRPORT_FAST_LEAVE,
3460 BR_MULTICAST_FAST_LEAVE) ||
3461 brport_nla_put_flag(skb, flags, mask,
3462 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
3463 brport_nla_put_flag(skb, flags, mask,
3464 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
3465 brport_nla_put_flag(skb, flags, mask,
3466 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
3467 brport_nla_put_flag(skb, flags, mask,
3468 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
3469 brport_nla_put_flag(skb, flags, mask,
3470 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
3471 nla_nest_cancel(skb, protinfo);
3472 goto nla_put_failure;
3473 }
3474
3475 nla_nest_end(skb, protinfo);
3476
Johannes Berg053c0952015-01-16 22:09:00 +01003477 nlmsg_end(skb, nlh);
3478 return 0;
John Fastabend815cccb2012-10-24 08:13:09 +00003479nla_put_failure:
3480 nlmsg_cancel(skb, nlh);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003481 return err ? err : -EMSGSIZE;
John Fastabend815cccb2012-10-24 08:13:09 +00003482}
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003483EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
John Fastabend815cccb2012-10-24 08:13:09 +00003484
John Fastabende5a55a82012-10-24 08:12:57 +00003485static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
3486{
3487 struct net *net = sock_net(skb->sk);
3488 struct net_device *dev;
3489 int idx = 0;
3490 u32 portid = NETLINK_CB(cb->skb).portid;
3491 u32 seq = cb->nlh->nlmsg_seq;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00003492 u32 filter_mask = 0;
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003493 int err;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00003494
Thomas Grafaa68c202014-11-26 13:42:20 +01003495 if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
3496 struct nlattr *extfilt;
3497
3498 extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
3499 IFLA_EXT_MASK);
3500 if (extfilt) {
3501 if (nla_len(extfilt) < sizeof(filter_mask))
3502 return -EINVAL;
3503
3504 filter_mask = nla_get_u32(extfilt);
3505 }
3506 }
John Fastabende5a55a82012-10-24 08:12:57 +00003507
3508 rcu_read_lock();
3509 for_each_netdev_rcu(net, dev) {
3510 const struct net_device_ops *ops = dev->netdev_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00003511 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
John Fastabende5a55a82012-10-24 08:12:57 +00003512
Jiri Pirko898e5062013-01-03 22:48:52 +00003513 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003514 if (idx >= cb->args[0]) {
3515 err = br_dev->netdev_ops->ndo_bridge_getlink(
3516 skb, portid, seq, dev,
3517 filter_mask, NLM_F_MULTI);
David Ahernf6c57752017-05-15 23:19:17 -07003518 if (err < 0 && err != -EOPNOTSUPP) {
3519 if (likely(skb->len))
3520 break;
3521
3522 goto out_err;
3523 }
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003524 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00003525 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00003526 }
3527
3528 if (ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003529 if (idx >= cb->args[0]) {
3530 err = ops->ndo_bridge_getlink(skb, portid,
3531 seq, dev,
3532 filter_mask,
3533 NLM_F_MULTI);
David Ahernf6c57752017-05-15 23:19:17 -07003534 if (err < 0 && err != -EOPNOTSUPP) {
3535 if (likely(skb->len))
3536 break;
3537
3538 goto out_err;
3539 }
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003540 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00003541 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00003542 }
3543 }
David Ahernf6c57752017-05-15 23:19:17 -07003544 err = skb->len;
3545out_err:
John Fastabende5a55a82012-10-24 08:12:57 +00003546 rcu_read_unlock();
3547 cb->args[0] = idx;
3548
David Ahernf6c57752017-05-15 23:19:17 -07003549 return err;
John Fastabende5a55a82012-10-24 08:12:57 +00003550}
3551
John Fastabend2469ffd2012-10-24 08:13:03 +00003552static inline size_t bridge_nlmsg_size(void)
3553{
3554 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3555 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3556 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3557 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
3558 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
3559 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
3560 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
3561 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
3562 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
3563 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
3564 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
3565}
3566
Roopa Prabhu02dba432015-01-14 20:02:25 -08003567static int rtnl_bridge_notify(struct net_device *dev)
John Fastabend2469ffd2012-10-24 08:13:03 +00003568{
3569 struct net *net = dev_net(dev);
John Fastabend2469ffd2012-10-24 08:13:03 +00003570 struct sk_buff *skb;
3571 int err = -EOPNOTSUPP;
3572
Roopa Prabhu02dba432015-01-14 20:02:25 -08003573 if (!dev->netdev_ops->ndo_bridge_getlink)
3574 return 0;
3575
John Fastabend2469ffd2012-10-24 08:13:03 +00003576 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
3577 if (!skb) {
3578 err = -ENOMEM;
3579 goto errout;
3580 }
3581
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02003582 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
Roopa Prabhu02dba432015-01-14 20:02:25 -08003583 if (err < 0)
3584 goto errout;
John Fastabend2469ffd2012-10-24 08:13:03 +00003585
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08003586 if (!skb->len)
3587 goto errout;
3588
John Fastabend2469ffd2012-10-24 08:13:03 +00003589 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
3590 return 0;
3591errout:
3592 WARN_ON(err == -EMSGSIZE);
3593 kfree_skb(skb);
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08003594 if (err)
3595 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
John Fastabend2469ffd2012-10-24 08:13:03 +00003596 return err;
3597}
3598
David Ahernc21ef3e2017-04-16 09:48:24 -07003599static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3600 struct netlink_ext_ack *extack)
John Fastabende5a55a82012-10-24 08:12:57 +00003601{
3602 struct net *net = sock_net(skb->sk);
3603 struct ifinfomsg *ifm;
3604 struct net_device *dev;
John Fastabend2469ffd2012-10-24 08:13:03 +00003605 struct nlattr *br_spec, *attr = NULL;
3606 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02003607 u16 flags = 0;
John Fastabendc38e01b2012-11-02 16:32:36 +00003608 bool have_flags = false;
John Fastabende5a55a82012-10-24 08:12:57 +00003609
3610 if (nlmsg_len(nlh) < sizeof(*ifm))
3611 return -EINVAL;
3612
3613 ifm = nlmsg_data(nlh);
3614 if (ifm->ifi_family != AF_BRIDGE)
3615 return -EPFNOSUPPORT;
3616
3617 dev = __dev_get_by_index(net, ifm->ifi_index);
3618 if (!dev) {
3619 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3620 return -ENODEV;
3621 }
3622
John Fastabend2469ffd2012-10-24 08:13:03 +00003623 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3624 if (br_spec) {
3625 nla_for_each_nested(attr, br_spec, rem) {
3626 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01003627 if (nla_len(attr) < sizeof(flags))
3628 return -EINVAL;
3629
John Fastabendc38e01b2012-11-02 16:32:36 +00003630 have_flags = true;
John Fastabend2469ffd2012-10-24 08:13:03 +00003631 flags = nla_get_u16(attr);
3632 break;
3633 }
3634 }
3635 }
3636
3637 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003638 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3639
3640 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
John Fastabend2469ffd2012-10-24 08:13:03 +00003641 err = -EOPNOTSUPP;
3642 goto out;
3643 }
3644
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003645 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
John Fastabende5a55a82012-10-24 08:12:57 +00003646 if (err)
3647 goto out;
John Fastabend2469ffd2012-10-24 08:13:03 +00003648
3649 flags &= ~BRIDGE_FLAGS_MASTER;
John Fastabende5a55a82012-10-24 08:12:57 +00003650 }
3651
John Fastabend2469ffd2012-10-24 08:13:03 +00003652 if ((flags & BRIDGE_FLAGS_SELF)) {
3653 if (!dev->netdev_ops->ndo_bridge_setlink)
3654 err = -EOPNOTSUPP;
3655 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003656 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
3657 flags);
Roopa Prabhu02dba432015-01-14 20:02:25 -08003658 if (!err) {
John Fastabend2469ffd2012-10-24 08:13:03 +00003659 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08003660
3661 /* Generate event to notify upper layer of bridge
3662 * change
3663 */
3664 err = rtnl_bridge_notify(dev);
3665 }
John Fastabend2469ffd2012-10-24 08:13:03 +00003666 }
3667
John Fastabendc38e01b2012-11-02 16:32:36 +00003668 if (have_flags)
John Fastabend2469ffd2012-10-24 08:13:03 +00003669 memcpy(nla_data(attr), &flags, sizeof(flags));
John Fastabende5a55a82012-10-24 08:12:57 +00003670out:
3671 return err;
3672}
3673
David Ahernc21ef3e2017-04-16 09:48:24 -07003674static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
3675 struct netlink_ext_ack *extack)
Vlad Yasevich407af322013-02-13 12:00:12 +00003676{
3677 struct net *net = sock_net(skb->sk);
3678 struct ifinfomsg *ifm;
3679 struct net_device *dev;
3680 struct nlattr *br_spec, *attr = NULL;
3681 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02003682 u16 flags = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +00003683 bool have_flags = false;
3684
3685 if (nlmsg_len(nlh) < sizeof(*ifm))
3686 return -EINVAL;
3687
3688 ifm = nlmsg_data(nlh);
3689 if (ifm->ifi_family != AF_BRIDGE)
3690 return -EPFNOSUPPORT;
3691
3692 dev = __dev_get_by_index(net, ifm->ifi_index);
3693 if (!dev) {
3694 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3695 return -ENODEV;
3696 }
3697
3698 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3699 if (br_spec) {
3700 nla_for_each_nested(attr, br_spec, rem) {
3701 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01003702 if (nla_len(attr) < sizeof(flags))
3703 return -EINVAL;
3704
Vlad Yasevich407af322013-02-13 12:00:12 +00003705 have_flags = true;
3706 flags = nla_get_u16(attr);
3707 break;
3708 }
3709 }
3710 }
3711
Vlad Yasevich407af322013-02-13 12:00:12 +00003712 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3713 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3714
3715 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
3716 err = -EOPNOTSUPP;
3717 goto out;
3718 }
3719
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003720 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00003721 if (err)
3722 goto out;
3723
3724 flags &= ~BRIDGE_FLAGS_MASTER;
3725 }
3726
3727 if ((flags & BRIDGE_FLAGS_SELF)) {
3728 if (!dev->netdev_ops->ndo_bridge_dellink)
3729 err = -EOPNOTSUPP;
3730 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003731 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
3732 flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00003733
Roopa Prabhu02dba432015-01-14 20:02:25 -08003734 if (!err) {
Vlad Yasevich407af322013-02-13 12:00:12 +00003735 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08003736
3737 /* Generate event to notify upper layer of bridge
3738 * change
3739 */
3740 err = rtnl_bridge_notify(dev);
3741 }
Vlad Yasevich407af322013-02-13 12:00:12 +00003742 }
3743
3744 if (have_flags)
3745 memcpy(nla_data(attr), &flags, sizeof(flags));
Vlad Yasevich407af322013-02-13 12:00:12 +00003746out:
3747 return err;
3748}
3749
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003750static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
3751{
3752 return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
3753 (!idxattr || idxattr == attrid);
3754}
3755
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02003756#define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
3757static int rtnl_get_offload_stats_attr_size(int attr_id)
3758{
3759 switch (attr_id) {
3760 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
3761 return sizeof(struct rtnl_link_stats64);
3762 }
3763
3764 return 0;
3765}
3766
3767static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
3768 int *prividx)
3769{
3770 struct nlattr *attr = NULL;
3771 int attr_id, size;
3772 void *attr_data;
3773 int err;
3774
3775 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
3776 dev->netdev_ops->ndo_get_offload_stats))
3777 return -ENODATA;
3778
3779 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
3780 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
3781 if (attr_id < *prividx)
3782 continue;
3783
3784 size = rtnl_get_offload_stats_attr_size(attr_id);
3785 if (!size)
3786 continue;
3787
Or Gerlitz3df5b3c2016-11-22 23:09:54 +02003788 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02003789 continue;
3790
3791 attr = nla_reserve_64bit(skb, attr_id, size,
3792 IFLA_OFFLOAD_XSTATS_UNSPEC);
3793 if (!attr)
3794 goto nla_put_failure;
3795
3796 attr_data = nla_data(attr);
3797 memset(attr_data, 0, size);
3798 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
3799 attr_data);
3800 if (err)
3801 goto get_offload_stats_failure;
3802 }
3803
3804 if (!attr)
3805 return -ENODATA;
3806
3807 *prividx = 0;
3808 return 0;
3809
3810nla_put_failure:
3811 err = -EMSGSIZE;
3812get_offload_stats_failure:
3813 *prividx = attr_id;
3814 return err;
3815}
3816
3817static int rtnl_get_offload_stats_size(const struct net_device *dev)
3818{
3819 int nla_size = 0;
3820 int attr_id;
3821 int size;
3822
3823 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
3824 dev->netdev_ops->ndo_get_offload_stats))
3825 return 0;
3826
3827 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
3828 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
Or Gerlitz3df5b3c2016-11-22 23:09:54 +02003829 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02003830 continue;
3831 size = rtnl_get_offload_stats_attr_size(attr_id);
3832 nla_size += nla_total_size_64bit(size);
3833 }
3834
3835 if (nla_size != 0)
3836 nla_size += nla_total_size(0);
3837
3838 return nla_size;
3839}
3840
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003841static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
3842 int type, u32 pid, u32 seq, u32 change,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003843 unsigned int flags, unsigned int filter_mask,
3844 int *idxattr, int *prividx)
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003845{
3846 struct if_stats_msg *ifsm;
3847 struct nlmsghdr *nlh;
3848 struct nlattr *attr;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003849 int s_prividx = *prividx;
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02003850 int err;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003851
3852 ASSERT_RTNL();
3853
3854 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
3855 if (!nlh)
3856 return -EMSGSIZE;
3857
3858 ifsm = nlmsg_data(nlh);
3859 ifsm->ifindex = dev->ifindex;
3860 ifsm->filter_mask = filter_mask;
3861
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003862 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003863 struct rtnl_link_stats64 *sp;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003864
Nicolas Dichtel58414d32016-04-21 18:58:25 +02003865 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
3866 sizeof(struct rtnl_link_stats64),
3867 IFLA_STATS_UNSPEC);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003868 if (!attr)
3869 goto nla_put_failure;
3870
3871 sp = nla_data(attr);
3872 dev_get_stats(dev, sp);
3873 }
3874
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02003875 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
3876 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
3877
3878 if (ops && ops->fill_linkxstats) {
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02003879 *idxattr = IFLA_STATS_LINK_XSTATS;
3880 attr = nla_nest_start(skb,
3881 IFLA_STATS_LINK_XSTATS);
3882 if (!attr)
3883 goto nla_put_failure;
3884
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02003885 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
3886 nla_nest_end(skb, attr);
3887 if (err)
3888 goto nla_put_failure;
3889 *idxattr = 0;
3890 }
3891 }
3892
3893 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
3894 *idxattr)) {
3895 const struct rtnl_link_ops *ops = NULL;
3896 const struct net_device *master;
3897
3898 master = netdev_master_upper_dev_get(dev);
3899 if (master)
3900 ops = master->rtnl_link_ops;
3901 if (ops && ops->fill_linkxstats) {
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02003902 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
3903 attr = nla_nest_start(skb,
3904 IFLA_STATS_LINK_XSTATS_SLAVE);
3905 if (!attr)
3906 goto nla_put_failure;
3907
3908 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02003909 nla_nest_end(skb, attr);
3910 if (err)
3911 goto nla_put_failure;
3912 *idxattr = 0;
3913 }
3914 }
3915
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02003916 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
3917 *idxattr)) {
3918 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
3919 attr = nla_nest_start(skb, IFLA_STATS_LINK_OFFLOAD_XSTATS);
3920 if (!attr)
3921 goto nla_put_failure;
3922
3923 err = rtnl_get_offload_stats(skb, dev, prividx);
3924 if (err == -ENODATA)
3925 nla_nest_cancel(skb, attr);
3926 else
3927 nla_nest_end(skb, attr);
3928
3929 if (err && err != -ENODATA)
3930 goto nla_put_failure;
3931 *idxattr = 0;
3932 }
3933
Robert Shearmanaefb4d42017-01-16 14:16:36 +00003934 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
3935 struct rtnl_af_ops *af_ops;
3936
3937 *idxattr = IFLA_STATS_AF_SPEC;
3938 attr = nla_nest_start(skb, IFLA_STATS_AF_SPEC);
3939 if (!attr)
3940 goto nla_put_failure;
3941
3942 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
3943 if (af_ops->fill_stats_af) {
3944 struct nlattr *af;
3945 int err;
3946
3947 af = nla_nest_start(skb, af_ops->family);
3948 if (!af)
3949 goto nla_put_failure;
3950
3951 err = af_ops->fill_stats_af(skb, dev);
3952
3953 if (err == -ENODATA)
3954 nla_nest_cancel(skb, af);
3955 else if (err < 0)
3956 goto nla_put_failure;
3957
3958 nla_nest_end(skb, af);
3959 }
3960 }
3961
3962 nla_nest_end(skb, attr);
3963
3964 *idxattr = 0;
3965 }
3966
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003967 nlmsg_end(skb, nlh);
3968
3969 return 0;
3970
3971nla_put_failure:
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003972 /* not a multi message or no progress mean a real error */
3973 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
3974 nlmsg_cancel(skb, nlh);
3975 else
3976 nlmsg_end(skb, nlh);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003977
3978 return -EMSGSIZE;
3979}
3980
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003981static size_t if_nlmsg_stats_size(const struct net_device *dev,
3982 u32 filter_mask)
3983{
3984 size_t size = 0;
3985
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003986 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003987 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
3988
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02003989 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
3990 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02003991 int attr = IFLA_STATS_LINK_XSTATS;
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02003992
3993 if (ops && ops->get_linkxstats_size) {
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02003994 size += nla_total_size(ops->get_linkxstats_size(dev,
3995 attr));
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02003996 /* for IFLA_STATS_LINK_XSTATS */
3997 size += nla_total_size(0);
3998 }
3999 }
4000
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004001 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
4002 struct net_device *_dev = (struct net_device *)dev;
4003 const struct rtnl_link_ops *ops = NULL;
4004 const struct net_device *master;
4005
4006 /* netdev_master_upper_dev_get can't take const */
4007 master = netdev_master_upper_dev_get(_dev);
4008 if (master)
4009 ops = master->rtnl_link_ops;
4010 if (ops && ops->get_linkxstats_size) {
4011 int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
4012
4013 size += nla_total_size(ops->get_linkxstats_size(dev,
4014 attr));
4015 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
4016 size += nla_total_size(0);
4017 }
4018 }
4019
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004020 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
4021 size += rtnl_get_offload_stats_size(dev);
4022
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004023 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
4024 struct rtnl_af_ops *af_ops;
4025
4026 /* for IFLA_STATS_AF_SPEC */
4027 size += nla_total_size(0);
4028
4029 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
4030 if (af_ops->get_stats_af_size) {
4031 size += nla_total_size(
4032 af_ops->get_stats_af_size(dev));
4033
4034 /* for AF_* */
4035 size += nla_total_size(0);
4036 }
4037 }
4038 }
4039
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004040 return size;
4041}
4042
David Ahernc21ef3e2017-04-16 09:48:24 -07004043static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
4044 struct netlink_ext_ack *extack)
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004045{
4046 struct net *net = sock_net(skb->sk);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004047 struct net_device *dev = NULL;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004048 int idxattr = 0, prividx = 0;
4049 struct if_stats_msg *ifsm;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004050 struct sk_buff *nskb;
4051 u32 filter_mask;
4052 int err;
4053
Mathias Krause4775cc12016-12-28 17:52:15 +01004054 if (nlmsg_len(nlh) < sizeof(*ifsm))
4055 return -EINVAL;
4056
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004057 ifsm = nlmsg_data(nlh);
4058 if (ifsm->ifindex > 0)
4059 dev = __dev_get_by_index(net, ifsm->ifindex);
4060 else
4061 return -EINVAL;
4062
4063 if (!dev)
4064 return -ENODEV;
4065
4066 filter_mask = ifsm->filter_mask;
4067 if (!filter_mask)
4068 return -EINVAL;
4069
4070 nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
4071 if (!nskb)
4072 return -ENOBUFS;
4073
4074 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
4075 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004076 0, filter_mask, &idxattr, &prividx);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004077 if (err < 0) {
4078 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
4079 WARN_ON(err == -EMSGSIZE);
4080 kfree_skb(nskb);
4081 } else {
4082 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
4083 }
4084
4085 return err;
4086}
4087
4088static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
4089{
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004090 int h, s_h, err, s_idx, s_idxattr, s_prividx;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004091 struct net *net = sock_net(skb->sk);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004092 unsigned int flags = NLM_F_MULTI;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004093 struct if_stats_msg *ifsm;
4094 struct hlist_head *head;
4095 struct net_device *dev;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004096 u32 filter_mask = 0;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004097 int idx = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004098
4099 s_h = cb->args[0];
4100 s_idx = cb->args[1];
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004101 s_idxattr = cb->args[2];
4102 s_prividx = cb->args[3];
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004103
4104 cb->seq = net->dev_base_seq;
4105
Mathias Krause4775cc12016-12-28 17:52:15 +01004106 if (nlmsg_len(cb->nlh) < sizeof(*ifsm))
4107 return -EINVAL;
4108
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004109 ifsm = nlmsg_data(cb->nlh);
4110 filter_mask = ifsm->filter_mask;
4111 if (!filter_mask)
4112 return -EINVAL;
4113
4114 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4115 idx = 0;
4116 head = &net->dev_index_head[h];
4117 hlist_for_each_entry(dev, head, index_hlist) {
4118 if (idx < s_idx)
4119 goto cont;
4120 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
4121 NETLINK_CB(cb->skb).portid,
4122 cb->nlh->nlmsg_seq, 0,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004123 flags, filter_mask,
4124 &s_idxattr, &s_prividx);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004125 /* If we ran out of room on the first message,
4126 * we're in trouble
4127 */
4128 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
4129
4130 if (err < 0)
4131 goto out;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004132 s_prividx = 0;
4133 s_idxattr = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004134 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
4135cont:
4136 idx++;
4137 }
4138 }
4139out:
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004140 cb->args[3] = s_prividx;
4141 cb->args[2] = s_idxattr;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004142 cb->args[1] = idx;
4143 cb->args[0] = h;
4144
4145 return skb->len;
4146}
4147
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148/* Process one rtnetlink message. */
4149
Johannes Berg2d4bc932017-04-12 14:34:04 +02004150static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
4151 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004153 struct net *net = sock_net(skb->sk);
Florian Westphal6853dd42017-08-09 20:41:51 +02004154 struct rtnl_link *handlers;
4155 int err = -EOPNOTSUPP;
Thomas Grafe2849862007-03-22 11:48:11 -07004156 rtnl_doit_func doit;
Florian Westphal62256f92017-08-09 20:41:52 +02004157 unsigned int flags;
Alexander Kuleshov617cfc72016-01-10 21:26:57 +06004158 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 int family;
4160 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 if (type > RTM_MAX)
Thomas Graf038890f2007-04-05 14:35:52 -07004164 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165
4166 type -= RTM_BASE;
4167
4168 /* All the messages must have at least 1 byte length */
Hong zhi guo573ce262013-03-27 06:47:04 +00004169 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 return 0;
4171
Hong zhi guo573ce262013-03-27 06:47:04 +00004172 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 kind = type&3;
4174
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07004175 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07004176 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177
Florian Westphal5c2bb9b2017-08-10 16:52:58 +02004178 if (family >= ARRAY_SIZE(rtnl_msg_handlers))
Florian Westphal6853dd42017-08-09 20:41:51 +02004179 family = PF_UNSPEC;
4180
4181 rcu_read_lock();
4182 handlers = rcu_dereference(rtnl_msg_handlers[family]);
4183 if (!handlers) {
4184 family = PF_UNSPEC;
4185 handlers = rcu_dereference(rtnl_msg_handlers[family]);
4186 }
4187
David S. Millerb8f3ab42011-01-18 12:40:38 -08004188 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004189 struct sock *rtnl;
Thomas Grafe2849862007-03-22 11:48:11 -07004190 rtnl_dumpit_func dumpit;
Greg Rosec7ac8672011-06-10 01:27:09 +00004191 u16 min_dump_alloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192
Florian Westphal6853dd42017-08-09 20:41:51 +02004193 dumpit = READ_ONCE(handlers[type].dumpit);
4194 if (!dumpit) {
4195 family = PF_UNSPEC;
4196 handlers = rcu_dereference(rtnl_msg_handlers[PF_UNSPEC]);
4197 if (!handlers)
4198 goto err_unlock;
Florian Westphal0cc09022017-08-09 20:41:50 +02004199
Florian Westphal6853dd42017-08-09 20:41:51 +02004200 dumpit = READ_ONCE(handlers[type].dumpit);
4201 if (!dumpit)
4202 goto err_unlock;
4203 }
Florian Westphale1fa6d22017-08-09 20:41:47 +02004204
Florian Westphal019a3162017-08-09 20:41:49 +02004205 refcount_inc(&rtnl_msg_handlers_ref[family]);
4206
Florian Westphal5c2bb9b2017-08-10 16:52:58 +02004207 if (type == RTM_GETLINK - RTM_BASE)
Florian Westphale1fa6d22017-08-09 20:41:47 +02004208 min_dump_alloc = rtnl_calcit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209
Florian Westphal6853dd42017-08-09 20:41:51 +02004210 rcu_read_unlock();
4211
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004212 rtnl = net->rtnl;
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00004213 {
4214 struct netlink_dump_control c = {
4215 .dump = dumpit,
4216 .min_dump_alloc = min_dump_alloc,
4217 };
4218 err = netlink_dump_start(rtnl, skb, nlh, &c);
4219 }
Florian Westphal019a3162017-08-09 20:41:49 +02004220 refcount_dec(&rtnl_msg_handlers_ref[family]);
Eric Dumazet2907c352011-05-25 07:34:04 +00004221 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 }
4223
Florian Westphal62256f92017-08-09 20:41:52 +02004224 flags = READ_ONCE(handlers[type].flags);
4225 if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
4226 refcount_inc(&rtnl_msg_handlers_ref[family]);
4227 doit = READ_ONCE(handlers[type].doit);
4228 rcu_read_unlock();
4229 if (doit)
4230 err = doit(skb, nlh, extack);
4231 refcount_dec(&rtnl_msg_handlers_ref[family]);
4232 return err;
4233 }
4234
Florian Westphal6853dd42017-08-09 20:41:51 +02004235 rcu_read_unlock();
4236
Florian Westphal0cc09022017-08-09 20:41:50 +02004237 rtnl_lock();
Florian Westphal6853dd42017-08-09 20:41:51 +02004238 handlers = rtnl_dereference(rtnl_msg_handlers[family]);
4239 if (handlers) {
4240 doit = READ_ONCE(handlers[type].doit);
4241 if (doit)
4242 err = doit(skb, nlh, extack);
4243 }
Florian Westphal0cc09022017-08-09 20:41:50 +02004244 rtnl_unlock();
Florian Westphal0cc09022017-08-09 20:41:50 +02004245 return err;
4246
4247err_unlock:
Florian Westphal6853dd42017-08-09 20:41:51 +02004248 rcu_read_unlock();
Florian Westphal0cc09022017-08-09 20:41:50 +02004249 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250}
4251
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07004252static void rtnetlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07004254 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255}
4256
Julien Gomes5f729ea2017-06-20 13:54:16 -07004257static int rtnetlink_bind(struct net *net, int group)
4258{
4259 switch (group) {
4260 case RTNLGRP_IPV4_MROUTE_R:
4261 case RTNLGRP_IPV6_MROUTE_R:
4262 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
4263 return -EPERM;
4264 break;
4265 }
4266 return 0;
4267}
4268
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
4270{
Jiri Pirko351638e2013-05-28 01:30:21 +00004271 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02004272
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 switch (event) {
Vlad Yasevich5138e862017-04-04 09:23:41 -04004274 case NETDEV_REBOOT:
David Ahern37536542017-07-19 10:22:40 -07004275 case NETDEV_CHANGEADDR:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004276 case NETDEV_CHANGENAME:
4277 case NETDEV_FEAT_CHANGE:
4278 case NETDEV_BONDING_FAILOVER:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004279 case NETDEV_NOTIFY_PEERS:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004280 case NETDEV_RESEND_IGMP:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004281 case NETDEV_CHANGEINFODATA:
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04004282 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
4283 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 break;
4285 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 break;
4287 }
4288 return NOTIFY_DONE;
4289}
4290
4291static struct notifier_block rtnetlink_dev_notifier = {
4292 .notifier_call = rtnetlink_event,
4293};
4294
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004295
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004296static int __net_init rtnetlink_net_init(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004297{
4298 struct sock *sk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00004299 struct netlink_kernel_cfg cfg = {
4300 .groups = RTNLGRP_MAX,
4301 .input = rtnetlink_rcv,
4302 .cb_mutex = &rtnl_mutex,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00004303 .flags = NL_CFG_F_NONROOT_RECV,
Julien Gomes5f729ea2017-06-20 13:54:16 -07004304 .bind = rtnetlink_bind,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00004305 };
4306
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00004307 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004308 if (!sk)
4309 return -ENOMEM;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004310 net->rtnl = sk;
4311 return 0;
4312}
4313
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004314static void __net_exit rtnetlink_net_exit(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004315{
Denis V. Lunev775516b2008-01-18 23:55:19 -08004316 netlink_kernel_release(net->rtnl);
4317 net->rtnl = NULL;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004318}
4319
4320static struct pernet_operations rtnetlink_net_ops = {
4321 .init = rtnetlink_net_init,
4322 .exit = rtnetlink_net_exit,
4323};
4324
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325void __init rtnetlink_init(void)
4326{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004327 if (register_pernet_subsys(&rtnetlink_net_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 panic("rtnetlink_init: cannot initialize rtnetlink\n");
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004329
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 register_netdevice_notifier(&rtnetlink_dev_notifier);
Thomas Graf340d17f2007-03-22 11:49:22 -07004331
Greg Rosec7ac8672011-06-10 01:27:09 +00004332 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
Florian Westphalb97bac62017-08-09 20:41:48 +02004333 rtnl_dump_ifinfo, 0);
4334 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
4335 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
4336 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
Thomas Graf687ad8c2007-03-22 11:59:42 -07004337
Florian Westphalb97bac62017-08-09 20:41:48 +02004338 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
4339 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
4340 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
John Fastabend77162022012-04-15 06:43:56 +00004341
Florian Westphalb97bac62017-08-09 20:41:48 +02004342 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
4343 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
4344 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, 0);
John Fastabende5a55a82012-10-24 08:12:57 +00004345
Florian Westphalb97bac62017-08-09 20:41:48 +02004346 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
4347 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
4348 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004349
4350 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
Florian Westphalb97bac62017-08-09 20:41:48 +02004351 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352}