blob: e4ae0319e189915e81a2d3083c81903aa241b07c [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
Kees Cookccf8dbc2018-05-30 15:20:52 -070062#define RTNL_MAX_TYPE 48
63#define RTNL_SLAVE_MAX_TYPE 36
64
Eric Dumazete0d087a2009-11-07 01:26:17 -080065struct rtnl_link {
Thomas Grafe2849862007-03-22 11:48:11 -070066 rtnl_doit_func doit;
67 rtnl_dumpit_func dumpit;
Florian Westphale4202512017-12-02 21:44:06 +010068 struct module *owner;
Florian Westphal62256f92017-08-09 20:41:52 +020069 unsigned int flags;
Florian Westphaladdf9b92017-12-02 21:44:05 +010070 struct rcu_head rcu;
Thomas Grafe2849862007-03-22 11:48:11 -070071};
72
Stephen Hemminger6756ae42006-03-20 22:23:58 -080073static DEFINE_MUTEX(rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75void rtnl_lock(void)
76{
Stephen Hemminger6756ae42006-03-20 22:23:58 -080077 mutex_lock(&rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
Eric Dumazete0d087a2009-11-07 01:26:17 -080079EXPORT_SYMBOL(rtnl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Kirill Tkhai79ffdfc2018-03-14 22:17:20 +030081int rtnl_lock_killable(void)
82{
83 return mutex_lock_killable(&rtnl_mutex);
84}
85EXPORT_SYMBOL(rtnl_lock_killable);
86
Eric Dumazet1b5c5492016-06-13 20:21:50 -070087static struct sk_buff *defer_kfree_skb_list;
88void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail)
89{
90 if (head && tail) {
91 tail->next = defer_kfree_skb_list;
92 defer_kfree_skb_list = head;
93 }
94}
95EXPORT_SYMBOL(rtnl_kfree_skbs);
96
Stephen Hemminger6756ae42006-03-20 22:23:58 -080097void __rtnl_unlock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Eric Dumazet1b5c5492016-06-13 20:21:50 -070099 struct sk_buff *head = defer_kfree_skb_list;
100
101 defer_kfree_skb_list = NULL;
102
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800103 mutex_unlock(&rtnl_mutex);
Eric Dumazet1b5c5492016-06-13 20:21:50 -0700104
105 while (head) {
106 struct sk_buff *next = head->next;
107
108 kfree_skb(head);
109 cond_resched();
110 head = next;
111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114void rtnl_unlock(void)
115{
Herbert Xu58ec3b42008-10-07 15:50:03 -0700116 /* This fellow will unlock it for us. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 netdev_run_todo();
118}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800119EXPORT_SYMBOL(rtnl_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800121int rtnl_trylock(void)
122{
123 return mutex_trylock(&rtnl_mutex);
124}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800125EXPORT_SYMBOL(rtnl_trylock);
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800126
Patrick McHardyc9c10142008-04-23 22:10:48 -0700127int rtnl_is_locked(void)
128{
129 return mutex_is_locked(&rtnl_mutex);
130}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800131EXPORT_SYMBOL(rtnl_is_locked);
Patrick McHardyc9c10142008-04-23 22:10:48 -0700132
Paul E. McKenneya898def2010-02-22 17:04:49 -0800133#ifdef CONFIG_PROVE_LOCKING
Yaowei Bai0cbf3342015-10-08 21:29:02 +0800134bool lockdep_rtnl_is_held(void)
Paul E. McKenneya898def2010-02-22 17:04:49 -0800135{
136 return lockdep_is_held(&rtnl_mutex);
137}
138EXPORT_SYMBOL(lockdep_rtnl_is_held);
139#endif /* #ifdef CONFIG_PROVE_LOCKING */
140
Florian Westphalb0e9fe12017-12-04 22:42:30 +0100141static struct rtnl_link *__rcu *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
Thomas Grafe2849862007-03-22 11:48:11 -0700142
143static inline int rtm_msgindex(int msgtype)
144{
145 int msgindex = msgtype - RTM_BASE;
146
147 /*
148 * msgindex < 0 implies someone tried to register a netlink
149 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
150 * the message type has not been added to linux/rtnetlink.h
151 */
152 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
153
154 return msgindex;
155}
156
Florian Westphaladdf9b92017-12-02 21:44:05 +0100157static struct rtnl_link *rtnl_get_link(int protocol, int msgtype)
158{
159 struct rtnl_link **tab;
160
161 if (protocol >= ARRAY_SIZE(rtnl_msg_handlers))
162 protocol = PF_UNSPEC;
163
164 tab = rcu_dereference_rtnl(rtnl_msg_handlers[protocol]);
165 if (!tab)
166 tab = rcu_dereference_rtnl(rtnl_msg_handlers[PF_UNSPEC]);
167
168 return tab[msgtype];
169}
170
Florian Westphale4202512017-12-02 21:44:06 +0100171static int rtnl_register_internal(struct module *owner,
172 int protocol, int msgtype,
173 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
174 unsigned int flags)
Thomas Grafe2849862007-03-22 11:48:11 -0700175{
Florian Westphalb0e9fe12017-12-04 22:42:30 +0100176 struct rtnl_link *link, *old;
177 struct rtnl_link __rcu **tab;
Thomas Grafe2849862007-03-22 11:48:11 -0700178 int msgindex;
Florian Westphaladdf9b92017-12-02 21:44:05 +0100179 int ret = -ENOBUFS;
Thomas Grafe2849862007-03-22 11:48:11 -0700180
Patrick McHardy25239ce2010-04-26 16:02:05 +0200181 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700182 msgindex = rtm_msgindex(msgtype);
183
Florian Westphaladdf9b92017-12-02 21:44:05 +0100184 rtnl_lock();
185 tab = rtnl_msg_handlers[protocol];
Thomas Grafe2849862007-03-22 11:48:11 -0700186 if (tab == NULL) {
Florian Westphaladdf9b92017-12-02 21:44:05 +0100187 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(void *), GFP_KERNEL);
188 if (!tab)
189 goto unlock;
Thomas Grafe2849862007-03-22 11:48:11 -0700190
Florian Westphaladdf9b92017-12-02 21:44:05 +0100191 /* ensures we see the 0 stores */
Florian Westphal6853dd42017-08-09 20:41:51 +0200192 rcu_assign_pointer(rtnl_msg_handlers[protocol], tab);
Thomas Grafe2849862007-03-22 11:48:11 -0700193 }
194
Florian Westphaladdf9b92017-12-02 21:44:05 +0100195 old = rtnl_dereference(tab[msgindex]);
196 if (old) {
197 link = kmemdup(old, sizeof(*old), GFP_KERNEL);
198 if (!link)
199 goto unlock;
200 } else {
201 link = kzalloc(sizeof(*link), GFP_KERNEL);
202 if (!link)
203 goto unlock;
204 }
Thomas Grafe2849862007-03-22 11:48:11 -0700205
Florian Westphale4202512017-12-02 21:44:06 +0100206 WARN_ON(link->owner && link->owner != owner);
207 link->owner = owner;
208
Florian Westphaladdf9b92017-12-02 21:44:05 +0100209 WARN_ON(doit && link->doit && link->doit != doit);
210 if (doit)
211 link->doit = doit;
212 WARN_ON(dumpit && link->dumpit && link->dumpit != dumpit);
213 if (dumpit)
214 link->dumpit = dumpit;
215
216 link->flags |= flags;
217
218 /* publish protocol:msgtype */
219 rcu_assign_pointer(tab[msgindex], link);
220 ret = 0;
221 if (old)
222 kfree_rcu(old, rcu);
223unlock:
224 rtnl_unlock();
225 return ret;
Thomas Grafe2849862007-03-22 11:48:11 -0700226}
Florian Westphale4202512017-12-02 21:44:06 +0100227
228/**
229 * rtnl_register_module - Register a rtnetlink message type
230 *
231 * @owner: module registering the hook (THIS_MODULE)
232 * @protocol: Protocol family or PF_UNSPEC
233 * @msgtype: rtnetlink message type
234 * @doit: Function pointer called for each request message
235 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
236 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
237 *
238 * Like rtnl_register, but for use by removable modules.
239 */
240int rtnl_register_module(struct module *owner,
241 int protocol, int msgtype,
242 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
243 unsigned int flags)
244{
245 return rtnl_register_internal(owner, protocol, msgtype,
246 doit, dumpit, flags);
247}
248EXPORT_SYMBOL_GPL(rtnl_register_module);
249
250/**
Florian Westphal16feebc2017-12-02 21:44:08 +0100251 * rtnl_register - Register a rtnetlink message type
Florian Westphale4202512017-12-02 21:44:06 +0100252 * @protocol: Protocol family or PF_UNSPEC
253 * @msgtype: rtnetlink message type
254 * @doit: Function pointer called for each request message
255 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
256 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
257 *
258 * Registers the specified function pointers (at least one of them has
259 * to be non-NULL) to be called whenever a request message for the
260 * specified protocol family and message type is received.
261 *
262 * The special protocol family PF_UNSPEC may be used to define fallback
263 * function pointers for the case when no entry for the specific protocol
264 * family exists.
Thomas Grafe2849862007-03-22 11:48:11 -0700265 */
266void rtnl_register(int protocol, int msgtype,
Greg Rosec7ac8672011-06-10 01:27:09 +0000267 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
Florian Westphalb97bac62017-08-09 20:41:48 +0200268 unsigned int flags)
Thomas Grafe2849862007-03-22 11:48:11 -0700269{
Florian Westphal16feebc2017-12-02 21:44:08 +0100270 int err;
271
272 err = rtnl_register_internal(NULL, protocol, msgtype, doit, dumpit,
273 flags);
274 if (err)
275 pr_err("Unable to register rtnetlink message handler, "
276 "protocol = %d, message type = %d\n", protocol, msgtype);
Thomas Grafe2849862007-03-22 11:48:11 -0700277}
Thomas Grafe2849862007-03-22 11:48:11 -0700278
279/**
280 * rtnl_unregister - Unregister a rtnetlink message type
281 * @protocol: Protocol family or PF_UNSPEC
282 * @msgtype: rtnetlink message type
283 *
284 * Returns 0 on success or a negative error code.
285 */
286int rtnl_unregister(int protocol, int msgtype)
287{
Florian Westphaladdf9b92017-12-02 21:44:05 +0100288 struct rtnl_link **tab, *link;
Thomas Grafe2849862007-03-22 11:48:11 -0700289 int msgindex;
290
Patrick McHardy25239ce2010-04-26 16:02:05 +0200291 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700292 msgindex = rtm_msgindex(msgtype);
293
Florian Westphal6853dd42017-08-09 20:41:51 +0200294 rtnl_lock();
Florian Westphaladdf9b92017-12-02 21:44:05 +0100295 tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
296 if (!tab) {
Florian Westphal6853dd42017-08-09 20:41:51 +0200297 rtnl_unlock();
Thomas Grafe2849862007-03-22 11:48:11 -0700298 return -ENOENT;
Florian Westphal6853dd42017-08-09 20:41:51 +0200299 }
Thomas Grafe2849862007-03-22 11:48:11 -0700300
Florian Westphaladdf9b92017-12-02 21:44:05 +0100301 link = tab[msgindex];
302 rcu_assign_pointer(tab[msgindex], NULL);
Florian Westphal6853dd42017-08-09 20:41:51 +0200303 rtnl_unlock();
Thomas Grafe2849862007-03-22 11:48:11 -0700304
Florian Westphaladdf9b92017-12-02 21:44:05 +0100305 kfree_rcu(link, rcu);
306
Thomas Grafe2849862007-03-22 11:48:11 -0700307 return 0;
308}
Thomas Grafe2849862007-03-22 11:48:11 -0700309EXPORT_SYMBOL_GPL(rtnl_unregister);
310
311/**
312 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
313 * @protocol : Protocol family or PF_UNSPEC
314 *
315 * Identical to calling rtnl_unregster() for all registered message types
316 * of a certain protocol family.
317 */
318void rtnl_unregister_all(int protocol)
319{
Florian Westphaladdf9b92017-12-02 21:44:05 +0100320 struct rtnl_link **tab, *link;
321 int msgindex;
Florian Westphal019a3162017-08-09 20:41:49 +0200322
Patrick McHardy25239ce2010-04-26 16:02:05 +0200323 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700324
Florian Westphal019a3162017-08-09 20:41:49 +0200325 rtnl_lock();
Florian Westphaladdf9b92017-12-02 21:44:05 +0100326 tab = rtnl_msg_handlers[protocol];
Sabrina Dubrocaf707ef62018-08-28 13:40:53 +0200327 if (!tab) {
328 rtnl_unlock();
329 return;
330 }
Florian Westphal6853dd42017-08-09 20:41:51 +0200331 RCU_INIT_POINTER(rtnl_msg_handlers[protocol], NULL);
Florian Westphaladdf9b92017-12-02 21:44:05 +0100332 for (msgindex = 0; msgindex < RTM_NR_MSGTYPES; msgindex++) {
333 link = tab[msgindex];
334 if (!link)
335 continue;
336
337 rcu_assign_pointer(tab[msgindex], NULL);
338 kfree_rcu(link, rcu);
339 }
Florian Westphal019a3162017-08-09 20:41:49 +0200340 rtnl_unlock();
341
Florian Westphal6853dd42017-08-09 20:41:51 +0200342 synchronize_net();
343
Florian Westphaladdf9b92017-12-02 21:44:05 +0100344 kfree(tab);
Thomas Grafe2849862007-03-22 11:48:11 -0700345}
Thomas Grafe2849862007-03-22 11:48:11 -0700346EXPORT_SYMBOL_GPL(rtnl_unregister_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Patrick McHardy38f7b872007-06-13 12:03:51 -0700348static LIST_HEAD(link_ops);
349
Eric Dumazetc63044f2011-12-13 11:38:00 +0000350static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
351{
352 const struct rtnl_link_ops *ops;
353
354 list_for_each_entry(ops, &link_ops, list) {
355 if (!strcmp(ops->kind, kind))
356 return ops;
357 }
358 return NULL;
359}
360
Patrick McHardy38f7b872007-06-13 12:03:51 -0700361/**
362 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
363 * @ops: struct rtnl_link_ops * to register
364 *
365 * The caller must hold the rtnl_mutex. This function should be used
366 * by drivers that create devices during module initialization. It
367 * must be called before registering the devices.
368 *
369 * Returns 0 on success or a negative error code.
370 */
371int __rtnl_link_register(struct rtnl_link_ops *ops)
372{
Eric Dumazetc63044f2011-12-13 11:38:00 +0000373 if (rtnl_link_ops_get(ops->kind))
374 return -EEXIST;
375
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +0200376 /* The check for setup is here because if ops
377 * does not have that filled up, it is not possible
378 * to use the ops for creating device. So do not
379 * fill up dellink as well. That disables rtnl_dellink.
380 */
381 if (ops->setup && !ops->dellink)
Eric Dumazet23289a32009-10-27 07:06:36 +0000382 ops->dellink = unregister_netdevice_queue;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700383
Patrick McHardy38f7b872007-06-13 12:03:51 -0700384 list_add_tail(&ops->list, &link_ops);
385 return 0;
386}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700387EXPORT_SYMBOL_GPL(__rtnl_link_register);
388
389/**
390 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
391 * @ops: struct rtnl_link_ops * to register
392 *
393 * Returns 0 on success or a negative error code.
394 */
395int rtnl_link_register(struct rtnl_link_ops *ops)
396{
397 int err;
398
Kees Cookccf8dbc2018-05-30 15:20:52 -0700399 /* Sanity-check max sizes to avoid stack buffer overflow. */
400 if (WARN_ON(ops->maxtype > RTNL_MAX_TYPE ||
401 ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE))
402 return -EINVAL;
403
Patrick McHardy38f7b872007-06-13 12:03:51 -0700404 rtnl_lock();
405 err = __rtnl_link_register(ops);
406 rtnl_unlock();
407 return err;
408}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700409EXPORT_SYMBOL_GPL(rtnl_link_register);
410
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700411static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
412{
413 struct net_device *dev;
Eric Dumazet23289a32009-10-27 07:06:36 +0000414 LIST_HEAD(list_kill);
415
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700416 for_each_netdev(net, dev) {
Eric Dumazet23289a32009-10-27 07:06:36 +0000417 if (dev->rtnl_link_ops == ops)
418 ops->dellink(dev, &list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700419 }
Eric Dumazet23289a32009-10-27 07:06:36 +0000420 unregister_netdevice_many(&list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700421}
422
Patrick McHardy38f7b872007-06-13 12:03:51 -0700423/**
424 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
425 * @ops: struct rtnl_link_ops * to unregister
426 *
Kirill Tkhai554873e2018-03-30 19:38:37 +0300427 * The caller must hold the rtnl_mutex and guarantee net_namespace_list
428 * integrity (hold pernet_ops_rwsem for writing to close the race
429 * with setup_net() and cleanup_net()).
Patrick McHardy38f7b872007-06-13 12:03:51 -0700430 */
431void __rtnl_link_unregister(struct rtnl_link_ops *ops)
432{
Eric W. Biederman881d9662007-09-17 11:56:21 -0700433 struct net *net;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700434
Eric W. Biederman881d9662007-09-17 11:56:21 -0700435 for_each_net(net) {
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700436 __rtnl_kill_links(net, ops);
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700437 }
Patrick McHardy38f7b872007-06-13 12:03:51 -0700438 list_del(&ops->list);
439}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700440EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
441
Cong Wang200b9162014-05-12 15:11:20 -0700442/* Return with the rtnl_lock held when there are no network
443 * devices unregistering in any network namespace.
444 */
445static void rtnl_lock_unregistering_all(void)
446{
447 struct net *net;
448 bool unregistering;
Peter Zijlstraff960a72014-10-29 17:04:56 +0100449 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Cong Wang200b9162014-05-12 15:11:20 -0700450
Peter Zijlstraff960a72014-10-29 17:04:56 +0100451 add_wait_queue(&netdev_unregistering_wq, &wait);
Cong Wang200b9162014-05-12 15:11:20 -0700452 for (;;) {
Cong Wang200b9162014-05-12 15:11:20 -0700453 unregistering = false;
454 rtnl_lock();
Kirill Tkhaif0b07bb12018-03-29 19:20:32 +0300455 /* We held write locked pernet_ops_rwsem, and parallel
456 * setup_net() and cleanup_net() are not possible.
457 */
Cong Wang200b9162014-05-12 15:11:20 -0700458 for_each_net(net) {
459 if (net->dev_unreg_count > 0) {
460 unregistering = true;
461 break;
462 }
463 }
464 if (!unregistering)
465 break;
466 __rtnl_unlock();
Peter Zijlstraff960a72014-10-29 17:04:56 +0100467
468 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Cong Wang200b9162014-05-12 15:11:20 -0700469 }
Peter Zijlstraff960a72014-10-29 17:04:56 +0100470 remove_wait_queue(&netdev_unregistering_wq, &wait);
Cong Wang200b9162014-05-12 15:11:20 -0700471}
472
Patrick McHardy38f7b872007-06-13 12:03:51 -0700473/**
474 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
475 * @ops: struct rtnl_link_ops * to unregister
476 */
477void rtnl_link_unregister(struct rtnl_link_ops *ops)
478{
Kirill Tkhai8518e9b2018-03-27 18:02:32 +0300479 /* Close the race with setup_net() and cleanup_net() */
Kirill Tkhai4420bf22018-03-27 18:02:23 +0300480 down_write(&pernet_ops_rwsem);
Cong Wang200b9162014-05-12 15:11:20 -0700481 rtnl_lock_unregistering_all();
Patrick McHardy38f7b872007-06-13 12:03:51 -0700482 __rtnl_link_unregister(ops);
483 rtnl_unlock();
Kirill Tkhai4420bf22018-03-27 18:02:23 +0300484 up_write(&pernet_ops_rwsem);
Patrick McHardy38f7b872007-06-13 12:03:51 -0700485}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700486EXPORT_SYMBOL_GPL(rtnl_link_unregister);
487
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100488static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
489{
490 struct net_device *master_dev;
491 const struct rtnl_link_ops *ops;
Florian Westphal8515ae32017-08-10 16:52:59 +0200492 size_t size = 0;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100493
Florian Westphal8515ae32017-08-10 16:52:59 +0200494 rcu_read_lock();
495
496 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100497 if (!master_dev)
Florian Westphal8515ae32017-08-10 16:52:59 +0200498 goto out;
499
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100500 ops = master_dev->rtnl_link_ops;
Fernando Luis Vazquez Cao6049f252014-02-04 19:35:02 +0900501 if (!ops || !ops->get_slave_size)
Florian Westphal8515ae32017-08-10 16:52:59 +0200502 goto out;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100503 /* IFLA_INFO_SLAVE_DATA + nested data */
Florian Westphal8515ae32017-08-10 16:52:59 +0200504 size = nla_total_size(sizeof(struct nlattr)) +
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100505 ops->get_slave_size(master_dev, dev);
Florian Westphal8515ae32017-08-10 16:52:59 +0200506
507out:
508 rcu_read_unlock();
509 return size;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100510}
511
Patrick McHardy38f7b872007-06-13 12:03:51 -0700512static size_t rtnl_link_get_size(const struct net_device *dev)
513{
514 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
515 size_t size;
516
517 if (!ops)
518 return 0;
519
Thomas Graf369cf772010-11-11 15:47:59 +0000520 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
521 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700522
523 if (ops->get_size)
524 /* IFLA_INFO_DATA + nested data */
Thomas Graf369cf772010-11-11 15:47:59 +0000525 size += nla_total_size(sizeof(struct nlattr)) +
Patrick McHardy38f7b872007-06-13 12:03:51 -0700526 ops->get_size(dev);
527
528 if (ops->get_xstats_size)
Thomas Graf369cf772010-11-11 15:47:59 +0000529 /* IFLA_INFO_XSTATS */
530 size += nla_total_size(ops->get_xstats_size(dev));
Patrick McHardy38f7b872007-06-13 12:03:51 -0700531
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100532 size += rtnl_link_get_slave_info_data_size(dev);
533
Patrick McHardy38f7b872007-06-13 12:03:51 -0700534 return size;
535}
536
Thomas Graff8ff1822010-11-16 04:30:14 +0000537static LIST_HEAD(rtnl_af_ops);
538
539static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
540{
541 const struct rtnl_af_ops *ops;
542
Florian Westphal5fa85a02017-10-16 15:44:36 +0200543 list_for_each_entry_rcu(ops, &rtnl_af_ops, list) {
Thomas Graff8ff1822010-11-16 04:30:14 +0000544 if (ops->family == family)
545 return ops;
546 }
547
548 return NULL;
549}
550
551/**
Thomas Graff8ff1822010-11-16 04:30:14 +0000552 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
553 * @ops: struct rtnl_af_ops * to register
554 *
555 * Returns 0 on success or a negative error code.
556 */
stephen hemminger3678a9d2013-12-30 10:41:32 -0800557void rtnl_af_register(struct rtnl_af_ops *ops)
Thomas Graff8ff1822010-11-16 04:30:14 +0000558{
Thomas Graff8ff1822010-11-16 04:30:14 +0000559 rtnl_lock();
Florian Westphal5fa85a02017-10-16 15:44:36 +0200560 list_add_tail_rcu(&ops->list, &rtnl_af_ops);
Thomas Graff8ff1822010-11-16 04:30:14 +0000561 rtnl_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +0000562}
563EXPORT_SYMBOL_GPL(rtnl_af_register);
564
565/**
Thomas Graff8ff1822010-11-16 04:30:14 +0000566 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
567 * @ops: struct rtnl_af_ops * to unregister
568 */
569void rtnl_af_unregister(struct rtnl_af_ops *ops)
570{
571 rtnl_lock();
Florian Westphal5fa85a02017-10-16 15:44:36 +0200572 list_del_rcu(&ops->list);
Thomas Graff8ff1822010-11-16 04:30:14 +0000573 rtnl_unlock();
Florian Westphal5fa85a02017-10-16 15:44:36 +0200574
575 synchronize_rcu();
Thomas Graff8ff1822010-11-16 04:30:14 +0000576}
577EXPORT_SYMBOL_GPL(rtnl_af_unregister);
578
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700579static size_t rtnl_link_get_af_size(const struct net_device *dev,
580 u32 ext_filter_mask)
Thomas Graff8ff1822010-11-16 04:30:14 +0000581{
582 struct rtnl_af_ops *af_ops;
583 size_t size;
584
585 /* IFLA_AF_SPEC */
586 size = nla_total_size(sizeof(struct nlattr));
587
Florian Westphal5fa85a02017-10-16 15:44:36 +0200588 rcu_read_lock();
589 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Thomas Graff8ff1822010-11-16 04:30:14 +0000590 if (af_ops->get_link_af_size) {
591 /* AF_* + nested data */
592 size += nla_total_size(sizeof(struct nlattr)) +
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700593 af_ops->get_link_af_size(dev, ext_filter_mask);
Thomas Graff8ff1822010-11-16 04:30:14 +0000594 }
595 }
Florian Westphal5fa85a02017-10-16 15:44:36 +0200596 rcu_read_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +0000597
598 return size;
599}
600
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100601static bool rtnl_have_link_slave_info(const struct net_device *dev)
602{
603 struct net_device *master_dev;
Florian Westphal4c82a952017-09-26 13:58:43 +0200604 bool ret = false;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100605
Florian Westphal4c82a952017-09-26 13:58:43 +0200606 rcu_read_lock();
607
608 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
Jiri Pirko813f0202014-01-23 19:19:21 +0100609 if (master_dev && master_dev->rtnl_link_ops)
Florian Westphal4c82a952017-09-26 13:58:43 +0200610 ret = true;
611 rcu_read_unlock();
612 return ret;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100613}
614
615static int rtnl_link_slave_info_fill(struct sk_buff *skb,
616 const struct net_device *dev)
617{
618 struct net_device *master_dev;
619 const struct rtnl_link_ops *ops;
620 struct nlattr *slave_data;
621 int err;
622
623 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
624 if (!master_dev)
625 return 0;
626 ops = master_dev->rtnl_link_ops;
627 if (!ops)
628 return 0;
629 if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
630 return -EMSGSIZE;
631 if (ops->fill_slave_info) {
632 slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA);
633 if (!slave_data)
634 return -EMSGSIZE;
635 err = ops->fill_slave_info(skb, master_dev, dev);
636 if (err < 0)
637 goto err_cancel_slave_data;
638 nla_nest_end(skb, slave_data);
639 }
640 return 0;
641
642err_cancel_slave_data:
643 nla_nest_cancel(skb, slave_data);
644 return err;
645}
646
647static int rtnl_link_info_fill(struct sk_buff *skb,
648 const struct net_device *dev)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700649{
650 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100651 struct nlattr *data;
652 int err;
653
654 if (!ops)
655 return 0;
656 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
657 return -EMSGSIZE;
658 if (ops->fill_xstats) {
659 err = ops->fill_xstats(skb, dev);
660 if (err < 0)
661 return err;
662 }
663 if (ops->fill_info) {
664 data = nla_nest_start(skb, IFLA_INFO_DATA);
665 if (data == NULL)
666 return -EMSGSIZE;
667 err = ops->fill_info(skb, dev);
668 if (err < 0)
669 goto err_cancel_data;
670 nla_nest_end(skb, data);
671 }
672 return 0;
673
674err_cancel_data:
675 nla_nest_cancel(skb, data);
676 return err;
677}
678
679static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
680{
681 struct nlattr *linkinfo;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700682 int err = -EMSGSIZE;
683
684 linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
685 if (linkinfo == NULL)
686 goto out;
687
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100688 err = rtnl_link_info_fill(skb, dev);
689 if (err < 0)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700690 goto err_cancel_link;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100691
692 err = rtnl_link_slave_info_fill(skb, dev);
693 if (err < 0)
694 goto err_cancel_link;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700695
696 nla_nest_end(skb, linkinfo);
697 return 0;
698
Patrick McHardy38f7b872007-06-13 12:03:51 -0700699err_cancel_link:
700 nla_nest_cancel(skb, linkinfo);
701out:
702 return err;
703}
704
Eric Dumazet95c96172012-04-15 05:58:06 +0000705int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800707 struct sock *rtnl = net->rtnl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 int err = 0;
709
Patrick McHardyac6d4392005-08-14 19:29:52 -0700710 NETLINK_CB(skb).dst_group = group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if (echo)
Reshetova, Elena63354792017-06-30 13:07:58 +0300712 refcount_inc(&skb->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
714 if (echo)
715 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
716 return err;
717}
718
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800719int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
Thomas Graf2942e902006-08-15 00:30:25 -0700720{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800721 struct sock *rtnl = net->rtnl;
722
Thomas Graf2942e902006-08-15 00:30:25 -0700723 return nlmsg_unicast(rtnl, skb, pid);
724}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800725EXPORT_SYMBOL(rtnl_unicast);
Thomas Graf2942e902006-08-15 00:30:25 -0700726
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800727void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
728 struct nlmsghdr *nlh, gfp_t flags)
Thomas Graf97676b62006-08-15 00:31:41 -0700729{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800730 struct sock *rtnl = net->rtnl;
Thomas Graf97676b62006-08-15 00:31:41 -0700731 int report = 0;
732
733 if (nlh)
734 report = nlmsg_report(nlh);
735
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800736 nlmsg_notify(rtnl, skb, pid, group, report, flags);
Thomas Graf97676b62006-08-15 00:31:41 -0700737}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800738EXPORT_SYMBOL(rtnl_notify);
Thomas Graf97676b62006-08-15 00:31:41 -0700739
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800740void rtnl_set_sk_err(struct net *net, u32 group, int error)
Thomas Graf97676b62006-08-15 00:31:41 -0700741{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800742 struct sock *rtnl = net->rtnl;
743
Thomas Graf97676b62006-08-15 00:31:41 -0700744 netlink_set_err(rtnl, 0, group, error);
745}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800746EXPORT_SYMBOL(rtnl_set_sk_err);
Thomas Graf97676b62006-08-15 00:31:41 -0700747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
749{
Thomas Graf2d7202b2006-08-22 00:01:27 -0700750 struct nlattr *mx;
751 int i, valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Thomas Graf2d7202b2006-08-22 00:01:27 -0700753 mx = nla_nest_start(skb, RTA_METRICS);
754 if (mx == NULL)
755 return -ENOBUFS;
756
757 for (i = 0; i < RTAX_MAX; i++) {
758 if (metrics[i]) {
Daniel Borkmannea697632015-01-05 23:57:47 +0100759 if (i == RTAX_CC_ALGO - 1) {
760 char tmp[TCP_CA_NAME_MAX], *name;
761
762 name = tcp_ca_get_name_by_key(metrics[i], tmp);
763 if (!name)
764 continue;
765 if (nla_put_string(skb, i + 1, name))
766 goto nla_put_failure;
Daniel Borkmannc3a8d942015-08-31 15:58:47 +0200767 } else if (i == RTAX_FEATURES - 1) {
768 u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
769
Phil Sutterf8edcd12016-08-23 13:14:31 +0200770 if (!user_features)
771 continue;
Daniel Borkmannc3a8d942015-08-31 15:58:47 +0200772 BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
773 if (nla_put_u32(skb, i + 1, user_features))
774 goto nla_put_failure;
Daniel Borkmannea697632015-01-05 23:57:47 +0100775 } else {
776 if (nla_put_u32(skb, i + 1, metrics[i]))
777 goto nla_put_failure;
778 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700779 valid++;
Thomas Graf2d7202b2006-08-22 00:01:27 -0700780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
David S. Millera57d27f2006-08-22 22:20:14 -0700783 if (!valid) {
784 nla_nest_cancel(skb, mx);
785 return 0;
786 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700787
788 return nla_nest_end(skb, mx);
789
790nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700791 nla_nest_cancel(skb, mx);
792 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800794EXPORT_SYMBOL(rtnetlink_put_metrics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Thomas Grafe3703b32006-11-27 09:27:07 -0800796int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
David S. Miller87a50692012-07-10 05:06:14 -0700797 long expires, u32 error)
Thomas Grafe3703b32006-11-27 09:27:07 -0800798{
799 struct rta_cacheinfo ci = {
Thomas Grafe3703b32006-11-27 09:27:07 -0800800 .rta_error = error,
801 .rta_id = id,
Thomas Grafe3703b32006-11-27 09:27:07 -0800802 };
803
David Ahern39407462018-04-17 17:33:08 -0700804 if (dst) {
805 ci.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse);
806 ci.rta_used = dst->__use;
807 ci.rta_clntref = atomic_read(&dst->__refcnt);
808 }
Li Wei82539472012-07-29 16:01:30 +0000809 if (expires) {
810 unsigned long clock;
Thomas Grafe3703b32006-11-27 09:27:07 -0800811
Li Wei82539472012-07-29 16:01:30 +0000812 clock = jiffies_to_clock_t(abs(expires));
813 clock = min_t(unsigned long, clock, INT_MAX);
814 ci.rta_expires = (expires > 0) ? clock : -clock;
815 }
Thomas Grafe3703b32006-11-27 09:27:07 -0800816 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
817}
Thomas Grafe3703b32006-11-27 09:27:07 -0800818EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
David S. Miller93b2d4a2008-02-17 18:35:07 -0800820static void set_operstate(struct net_device *dev, unsigned char transition)
Stefan Rompfb00055a2006-03-20 17:09:11 -0800821{
822 unsigned char operstate = dev->operstate;
823
Eric Dumazete0d087a2009-11-07 01:26:17 -0800824 switch (transition) {
Stefan Rompfb00055a2006-03-20 17:09:11 -0800825 case IF_OPER_UP:
826 if ((operstate == IF_OPER_DORMANT ||
827 operstate == IF_OPER_UNKNOWN) &&
828 !netif_dormant(dev))
829 operstate = IF_OPER_UP;
830 break;
831
832 case IF_OPER_DORMANT:
833 if (operstate == IF_OPER_UP ||
834 operstate == IF_OPER_UNKNOWN)
835 operstate = IF_OPER_DORMANT;
836 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700837 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800838
839 if (dev->operstate != operstate) {
840 write_lock_bh(&dev_base_lock);
841 dev->operstate = operstate;
842 write_unlock_bh(&dev_base_lock);
David S. Miller93b2d4a2008-02-17 18:35:07 -0800843 netdev_state_change(dev);
844 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800845}
846
Jiri Bencb1beb682012-07-27 02:58:22 +0000847static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
848{
849 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
850 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
851}
852
Patrick McHardy3729d502010-02-26 06:34:54 +0000853static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
854 const struct ifinfomsg *ifm)
855{
856 unsigned int flags = ifm->ifi_flags;
857
858 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
859 if (ifm->ifi_change)
860 flags = (flags & ifm->ifi_change) |
Jiri Bencb1beb682012-07-27 02:58:22 +0000861 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
Patrick McHardy3729d502010-02-26 06:34:54 +0000862
863 return flags;
864}
865
Thomas Grafb60c5112006-08-04 23:05:34 -0700866static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000867 const struct rtnl_link_stats64 *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
Thomas Grafb60c5112006-08-04 23:05:34 -0700869 a->rx_packets = b->rx_packets;
870 a->tx_packets = b->tx_packets;
871 a->rx_bytes = b->rx_bytes;
872 a->tx_bytes = b->tx_bytes;
873 a->rx_errors = b->rx_errors;
874 a->tx_errors = b->tx_errors;
875 a->rx_dropped = b->rx_dropped;
876 a->tx_dropped = b->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Thomas Grafb60c5112006-08-04 23:05:34 -0700878 a->multicast = b->multicast;
879 a->collisions = b->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Thomas Grafb60c5112006-08-04 23:05:34 -0700881 a->rx_length_errors = b->rx_length_errors;
882 a->rx_over_errors = b->rx_over_errors;
883 a->rx_crc_errors = b->rx_crc_errors;
884 a->rx_frame_errors = b->rx_frame_errors;
885 a->rx_fifo_errors = b->rx_fifo_errors;
886 a->rx_missed_errors = b->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Thomas Grafb60c5112006-08-04 23:05:34 -0700888 a->tx_aborted_errors = b->tx_aborted_errors;
889 a->tx_carrier_errors = b->tx_carrier_errors;
890 a->tx_fifo_errors = b->tx_fifo_errors;
891 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
892 a->tx_window_errors = b->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Thomas Grafb60c5112006-08-04 23:05:34 -0700894 a->rx_compressed = b->rx_compressed;
895 a->tx_compressed = b->tx_compressed;
Jarod Wilson6e7333d2016-02-01 18:51:05 -0500896
897 a->rx_nohandler = b->rx_nohandler;
Jan Engelhardt10708f32010-03-11 09:57:29 +0000898}
899
Chris Wrightc02db8c2010-05-16 01:05:45 -0700900/* All VF info */
Greg Rose115c9b82012-02-21 16:54:48 -0500901static inline int rtnl_vfinfo_size(const struct net_device *dev,
902 u32 ext_filter_mask)
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000903{
Phil Sutter9af15c32017-01-18 14:04:39 +0100904 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
Chris Wrightc02db8c2010-05-16 01:05:45 -0700905 int num_vfs = dev_num_vf(dev->dev.parent);
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100906 size_t size = nla_total_size(0);
Scott Feldman045de012010-05-28 03:42:43 -0700907 size += num_vfs *
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100908 (nla_total_size(0) +
909 nla_total_size(sizeof(struct ifla_vf_mac)) +
910 nla_total_size(sizeof(struct ifla_vf_vlan)) +
911 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
Moshe Shemesh79aab092016-09-22 12:11:15 +0300912 nla_total_size(MAX_VLAN_LIST_LEN *
913 sizeof(struct ifla_vf_vlan_info)) +
Sucheta Chakrabortyed616682014-05-22 09:59:05 -0400914 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100915 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
Jiri Benc945a3672014-08-08 16:44:32 +0200916 nla_total_size(sizeof(struct ifla_vf_rate)) +
Vlad Zolotarov01a3d792015-03-30 21:35:23 +0300917 nla_total_size(sizeof(struct ifla_vf_link_state)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300918 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100919 nla_total_size(0) + /* nest IFLA_VF_STATS */
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300920 /* IFLA_VF_STATS_RX_PACKETS */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200921 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300922 /* IFLA_VF_STATS_TX_PACKETS */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200923 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300924 /* IFLA_VF_STATS_RX_BYTES */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200925 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300926 /* IFLA_VF_STATS_TX_BYTES */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200927 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300928 /* IFLA_VF_STATS_BROADCAST */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200929 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300930 /* IFLA_VF_STATS_MULTICAST */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200931 nla_total_size_64bit(sizeof(__u64)) +
Eugenia Emantayevc5a9f6f2017-07-17 13:47:07 +0300932 /* IFLA_VF_STATS_RX_DROPPED */
933 nla_total_size_64bit(sizeof(__u64)) +
934 /* IFLA_VF_STATS_TX_DROPPED */
935 nla_total_size_64bit(sizeof(__u64)) +
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +0000936 nla_total_size(sizeof(struct ifla_vf_trust)));
Chris Wrightc02db8c2010-05-16 01:05:45 -0700937 return size;
938 } else
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000939 return 0;
940}
941
David Gibsonc53864f2014-04-24 10:22:36 +1000942static size_t rtnl_port_size(const struct net_device *dev,
943 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -0700944{
945 size_t port_size = nla_total_size(4) /* PORT_VF */
946 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
Scott Feldman57b61082010-05-17 22:49:55 -0700947 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
948 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
949 + nla_total_size(1) /* PROT_VDP_REQUEST */
950 + nla_total_size(2); /* PORT_VDP_RESPONSE */
951 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
952 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
953 + port_size;
954 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
955 + port_size;
956
David Gibsonc53864f2014-04-24 10:22:36 +1000957 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
958 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -0700959 return 0;
960 if (dev_num_vf(dev->dev.parent))
961 return port_self_size + vf_ports_size +
962 vf_port_size * dev_num_vf(dev->dev.parent);
963 else
964 return port_self_size;
965}
966
David S. Millerb5cdae32017-04-18 15:36:58 -0400967static size_t rtnl_xdp_size(void)
Brenden Blancod1fdd912016-07-19 12:16:49 -0700968{
Sabrina Dubrocab3cfaa32016-11-15 11:16:35 +0100969 size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */
Martin KaFai Lau58038692017-06-15 17:29:09 -0700970 nla_total_size(1) + /* XDP_ATTACHED */
Jakub Kicinskia25717d2018-07-11 20:36:41 -0700971 nla_total_size(4) + /* XDP_PROG_ID (or 1st mode) */
Jakub Kicinski4f91da22018-07-11 20:36:38 -0700972 nla_total_size(4); /* XDP_<mode>_PROG_ID */
Brenden Blancod1fdd912016-07-19 12:16:49 -0700973
David S. Millerb5cdae32017-04-18 15:36:58 -0400974 return xdp_size;
Brenden Blancod1fdd912016-07-19 12:16:49 -0700975}
976
Greg Rose115c9b82012-02-21 16:54:48 -0500977static noinline size_t if_nlmsg_size(const struct net_device *dev,
978 u32 ext_filter_mask)
Thomas Graf339bf982006-11-10 14:10:15 -0800979{
980 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
981 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700982 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
Thomas Graf339bf982006-11-10 14:10:15 -0800983 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
Nicolas Dichtel270cb4d2016-04-26 10:06:16 +0200984 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
Thomas Graf339bf982006-11-10 14:10:15 -0800985 + nla_total_size(sizeof(struct rtnl_link_stats))
David S. Miller35c58452016-04-19 19:49:29 -0400986 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
Thomas Graf339bf982006-11-10 14:10:15 -0800987 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
988 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
989 + nla_total_size(4) /* IFLA_TXQLEN */
990 + nla_total_size(4) /* IFLA_WEIGHT */
991 + nla_total_size(4) /* IFLA_MTU */
992 + nla_total_size(4) /* IFLA_LINK */
993 + nla_total_size(4) /* IFLA_MASTER */
Jiri Pirko9a572472012-12-27 23:49:39 +0000994 + nla_total_size(1) /* IFLA_CARRIER */
Ben Greearedbc0bb2012-03-29 12:51:30 +0000995 + nla_total_size(4) /* IFLA_PROMISCUITY */
Jiri Pirko76ff5cc2012-07-20 02:28:48 +0000996 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
997 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
Tobias Klauser69197562016-11-30 14:30:37 +0100998 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
999 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
Thomas Graf339bf982006-11-10 14:10:15 -08001000 + nla_total_size(1) /* IFLA_OPERSTATE */
Patrick McHardy38f7b872007-06-13 12:03:51 -07001001 + nla_total_size(1) /* IFLA_LINKMODE */
david decotigny2d3b4792014-03-29 09:48:35 -07001002 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001003 + nla_total_size(4) /* IFLA_LINK_NETNSID */
Serhey Popovychdb833d42017-06-20 14:35:23 +03001004 + nla_total_size(4) /* IFLA_GROUP */
Greg Rose115c9b82012-02-21 16:54:48 -05001005 + nla_total_size(ext_filter_mask
1006 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
1007 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
David Gibsonc53864f2014-04-24 10:22:36 +10001008 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
Thomas Graff8ff1822010-11-16 04:30:14 +00001009 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
Arad, Ronenb1974ed2015-10-19 09:23:28 -07001010 + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
Jiri Pirko82f28412014-11-28 14:34:18 +01001011 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001012 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
Nicolas Dichtelc57c7a92016-03-31 18:10:31 +02001013 + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
David S. Millerb5cdae32017-04-18 15:36:58 -04001014 + rtnl_xdp_size() /* IFLA_XDP */
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001015 + nla_total_size(4) /* IFLA_EVENT */
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02001016 + nla_total_size(4) /* IFLA_NEW_NETNSID */
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01001017 + nla_total_size(4) /* IFLA_NEW_IFINDEX */
Colin Ian King03ac7382017-11-06 15:04:54 +00001018 + nla_total_size(1) /* IFLA_PROTO_DOWN */
Christian Brauner7e4a8d52018-09-04 21:53:53 +02001019 + nla_total_size(4) /* IFLA_TARGET_NETNSID */
David Decotignyb2d3bcf2018-01-18 09:59:13 -08001020 + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */
1021 + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */
Stephen Hemminger3e7a50c2018-07-27 13:43:22 -07001022 + nla_total_size(4) /* IFLA_MIN_MTU */
1023 + nla_total_size(4) /* IFLA_MAX_MTU */
Jiri Benc79e1ad12017-11-02 17:04:38 -02001024 + 0;
Thomas Graf339bf982006-11-10 14:10:15 -08001025}
1026
Scott Feldman57b61082010-05-17 22:49:55 -07001027static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
1028{
1029 struct nlattr *vf_ports;
1030 struct nlattr *vf_port;
1031 int vf;
1032 int err;
1033
1034 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
1035 if (!vf_ports)
1036 return -EMSGSIZE;
1037
1038 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
1039 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
Scott Feldman8ca94182010-05-28 03:42:18 -07001040 if (!vf_port)
1041 goto nla_put_failure;
David S. Millera6574342012-04-01 20:12:00 -04001042 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
1043 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001044 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
Scott Feldman8ca94182010-05-28 03:42:18 -07001045 if (err == -EMSGSIZE)
1046 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001047 if (err) {
Scott Feldman57b61082010-05-17 22:49:55 -07001048 nla_nest_cancel(skb, vf_port);
1049 continue;
1050 }
1051 nla_nest_end(skb, vf_port);
1052 }
1053
1054 nla_nest_end(skb, vf_ports);
1055
1056 return 0;
Scott Feldman8ca94182010-05-28 03:42:18 -07001057
1058nla_put_failure:
1059 nla_nest_cancel(skb, vf_ports);
1060 return -EMSGSIZE;
Scott Feldman57b61082010-05-17 22:49:55 -07001061}
1062
1063static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
1064{
1065 struct nlattr *port_self;
1066 int err;
1067
1068 port_self = nla_nest_start(skb, IFLA_PORT_SELF);
1069 if (!port_self)
1070 return -EMSGSIZE;
1071
1072 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
1073 if (err) {
1074 nla_nest_cancel(skb, port_self);
Scott Feldman8ca94182010-05-28 03:42:18 -07001075 return (err == -EMSGSIZE) ? err : 0;
Scott Feldman57b61082010-05-17 22:49:55 -07001076 }
1077
1078 nla_nest_end(skb, port_self);
1079
1080 return 0;
1081}
1082
David Gibsonc53864f2014-04-24 10:22:36 +10001083static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1084 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -07001085{
1086 int err;
1087
David Gibsonc53864f2014-04-24 10:22:36 +10001088 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1089 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -07001090 return 0;
1091
1092 err = rtnl_port_self_fill(skb, dev);
1093 if (err)
1094 return err;
1095
1096 if (dev_num_vf(dev->dev.parent)) {
1097 err = rtnl_vf_ports_fill(skb, dev);
1098 if (err)
1099 return err;
1100 }
1101
1102 return 0;
1103}
1104
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001105static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
1106{
1107 int err;
Jiri Pirko02637fc2014-11-28 14:34:16 +01001108 struct netdev_phys_item_id ppid;
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001109
1110 err = dev_get_phys_port_id(dev, &ppid);
1111 if (err) {
1112 if (err == -EOPNOTSUPP)
1113 return 0;
1114 return err;
1115 }
1116
1117 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1118 return -EMSGSIZE;
1119
1120 return 0;
1121}
1122
David Aherndb24a902015-03-17 20:23:15 -06001123static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1124{
1125 char name[IFNAMSIZ];
1126 int err;
1127
1128 err = dev_get_phys_port_name(dev, name, sizeof(name));
1129 if (err) {
1130 if (err == -EOPNOTSUPP)
1131 return 0;
1132 return err;
1133 }
1134
Michal Schmidt77ef0332017-05-04 16:48:58 +02001135 if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
David Aherndb24a902015-03-17 20:23:15 -06001136 return -EMSGSIZE;
1137
1138 return 0;
1139}
1140
Jiri Pirko82f28412014-11-28 14:34:18 +01001141static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1142{
1143 int err;
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001144 struct switchdev_attr attr = {
Ido Schimmel6ff64f62015-12-15 16:03:35 +01001145 .orig_dev = dev,
Jiri Pirko1f868392015-10-01 11:03:42 +02001146 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001147 .flags = SWITCHDEV_F_NO_RECURSE,
1148 };
Jiri Pirko82f28412014-11-28 14:34:18 +01001149
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001150 err = switchdev_port_attr_get(dev, &attr);
Jiri Pirko82f28412014-11-28 14:34:18 +01001151 if (err) {
1152 if (err == -EOPNOTSUPP)
1153 return 0;
1154 return err;
1155 }
1156
Scott Feldman42275bd2015-05-13 11:16:50 -07001157 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len,
1158 attr.u.ppid.id))
Jiri Pirko82f28412014-11-28 14:34:18 +01001159 return -EMSGSIZE;
1160
1161 return 0;
1162}
1163
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001164static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1165 struct net_device *dev)
1166{
Roopa Prabhu550bce52016-04-15 20:36:25 -07001167 struct rtnl_link_stats64 *sp;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001168 struct nlattr *attr;
1169
Nicolas Dichtel58414d32016-04-21 18:58:25 +02001170 attr = nla_reserve_64bit(skb, IFLA_STATS64,
1171 sizeof(struct rtnl_link_stats64), IFLA_PAD);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001172 if (!attr)
1173 return -EMSGSIZE;
1174
Roopa Prabhu550bce52016-04-15 20:36:25 -07001175 sp = nla_data(attr);
1176 dev_get_stats(dev, sp);
1177
1178 attr = nla_reserve(skb, IFLA_STATS,
1179 sizeof(struct rtnl_link_stats));
1180 if (!attr)
1181 return -EMSGSIZE;
1182
1183 copy_rtnl_link_stats(nla_data(attr), sp);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001184
1185 return 0;
1186}
1187
1188static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1189 struct net_device *dev,
1190 int vfs_num,
1191 struct nlattr *vfinfo)
1192{
1193 struct ifla_vf_rss_query_en vf_rss_query_en;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001194 struct nlattr *vf, *vfstats, *vfvlanlist;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001195 struct ifla_vf_link_state vf_linkstate;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001196 struct ifla_vf_vlan_info vf_vlan_info;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001197 struct ifla_vf_spoofchk vf_spoofchk;
1198 struct ifla_vf_tx_rate vf_tx_rate;
1199 struct ifla_vf_stats vf_stats;
1200 struct ifla_vf_trust vf_trust;
1201 struct ifla_vf_vlan vf_vlan;
1202 struct ifla_vf_rate vf_rate;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001203 struct ifla_vf_mac vf_mac;
1204 struct ifla_vf_info ivi;
1205
Mintz, Yuval0eed9cf2017-06-07 21:00:33 +03001206 memset(&ivi, 0, sizeof(ivi));
1207
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001208 /* Not all SR-IOV capable drivers support the
1209 * spoofcheck and "RSS query enable" query. Preset to
1210 * -1 so the user space tool can detect that the driver
1211 * didn't report anything.
1212 */
1213 ivi.spoofchk = -1;
1214 ivi.rss_query_en = -1;
1215 ivi.trusted = -1;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001216 /* The default value for VF link state is "auto"
1217 * IFLA_VF_LINK_STATE_AUTO which equals zero
1218 */
1219 ivi.linkstate = 0;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001220 /* VLAN Protocol by default is 802.1Q */
1221 ivi.vlan_proto = htons(ETH_P_8021Q);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001222 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1223 return 0;
1224
Dan Carpenter775f4f02016-10-13 11:45:28 +03001225 memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
1226
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001227 vf_mac.vf =
1228 vf_vlan.vf =
Moshe Shemesh79aab092016-09-22 12:11:15 +03001229 vf_vlan_info.vf =
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001230 vf_rate.vf =
1231 vf_tx_rate.vf =
1232 vf_spoofchk.vf =
1233 vf_linkstate.vf =
1234 vf_rss_query_en.vf =
1235 vf_trust.vf = ivi.vf;
1236
1237 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1238 vf_vlan.vlan = ivi.vlan;
1239 vf_vlan.qos = ivi.qos;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001240 vf_vlan_info.vlan = ivi.vlan;
1241 vf_vlan_info.qos = ivi.qos;
1242 vf_vlan_info.vlan_proto = ivi.vlan_proto;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001243 vf_tx_rate.rate = ivi.max_tx_rate;
1244 vf_rate.min_tx_rate = ivi.min_tx_rate;
1245 vf_rate.max_tx_rate = ivi.max_tx_rate;
1246 vf_spoofchk.setting = ivi.spoofchk;
1247 vf_linkstate.link_state = ivi.linkstate;
1248 vf_rss_query_en.setting = ivi.rss_query_en;
1249 vf_trust.setting = ivi.trusted;
1250 vf = nla_nest_start(skb, IFLA_VF_INFO);
Moshe Shemesh79aab092016-09-22 12:11:15 +03001251 if (!vf)
1252 goto nla_put_vfinfo_failure;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001253 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1254 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1255 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1256 &vf_rate) ||
1257 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1258 &vf_tx_rate) ||
1259 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1260 &vf_spoofchk) ||
1261 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1262 &vf_linkstate) ||
1263 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1264 sizeof(vf_rss_query_en),
1265 &vf_rss_query_en) ||
1266 nla_put(skb, IFLA_VF_TRUST,
1267 sizeof(vf_trust), &vf_trust))
Moshe Shemesh79aab092016-09-22 12:11:15 +03001268 goto nla_put_vf_failure;
1269 vfvlanlist = nla_nest_start(skb, IFLA_VF_VLAN_LIST);
1270 if (!vfvlanlist)
1271 goto nla_put_vf_failure;
1272 if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
1273 &vf_vlan_info)) {
1274 nla_nest_cancel(skb, vfvlanlist);
1275 goto nla_put_vf_failure;
1276 }
1277 nla_nest_end(skb, vfvlanlist);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001278 memset(&vf_stats, 0, sizeof(vf_stats));
1279 if (dev->netdev_ops->ndo_get_vf_stats)
1280 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1281 &vf_stats);
1282 vfstats = nla_nest_start(skb, IFLA_VF_STATS);
Moshe Shemesh79aab092016-09-22 12:11:15 +03001283 if (!vfstats)
1284 goto nla_put_vf_failure;
Nicolas Dichtel343a6d82016-04-25 10:25:14 +02001285 if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1286 vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1287 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1288 vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1289 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1290 vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1291 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1292 vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1293 nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1294 vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1295 nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
Eugenia Emantayevc5a9f6f2017-07-17 13:47:07 +03001296 vf_stats.multicast, IFLA_VF_STATS_PAD) ||
1297 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED,
1298 vf_stats.rx_dropped, IFLA_VF_STATS_PAD) ||
1299 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED,
1300 vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) {
Moshe Shemesh79aab092016-09-22 12:11:15 +03001301 nla_nest_cancel(skb, vfstats);
1302 goto nla_put_vf_failure;
1303 }
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001304 nla_nest_end(skb, vfstats);
1305 nla_nest_end(skb, vf);
1306 return 0;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001307
1308nla_put_vf_failure:
1309 nla_nest_cancel(skb, vf);
1310nla_put_vfinfo_failure:
1311 nla_nest_cancel(skb, vfinfo);
1312 return -EMSGSIZE;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001313}
1314
Florian Westphal250fc3d2017-09-26 13:58:41 +02001315static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb,
1316 struct net_device *dev,
1317 u32 ext_filter_mask)
1318{
1319 struct nlattr *vfinfo;
1320 int i, num_vfs;
1321
1322 if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0))
1323 return 0;
1324
1325 num_vfs = dev_num_vf(dev->dev.parent);
1326 if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs))
1327 return -EMSGSIZE;
1328
1329 if (!dev->netdev_ops->ndo_get_vf_config)
1330 return 0;
1331
1332 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1333 if (!vfinfo)
1334 return -EMSGSIZE;
1335
1336 for (i = 0; i < num_vfs; i++) {
1337 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1338 return -EMSGSIZE;
1339 }
1340
1341 nla_nest_end(skb, vfinfo);
1342 return 0;
1343}
1344
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001345static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1346{
Kangjie Lu5f8e4472016-05-03 16:46:24 -04001347 struct rtnl_link_ifmap map;
1348
1349 memset(&map, 0, sizeof(map));
1350 map.mem_start = dev->mem_start;
1351 map.mem_end = dev->mem_end;
1352 map.base_addr = dev->base_addr;
1353 map.irq = dev->irq;
1354 map.dma = dev->dma;
1355 map.port = dev->if_port;
1356
Nicolas Dichtel270cb4d2016-04-26 10:06:16 +02001357 if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001358 return -EMSGSIZE;
1359
1360 return 0;
1361}
1362
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001363static u32 rtnl_xdp_prog_skb(struct net_device *dev)
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001364{
Martin KaFai Lau58038692017-06-15 17:29:09 -07001365 const struct bpf_prog *generic_xdp_prog;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001366
1367 ASSERT_RTNL();
1368
Martin KaFai Lau58038692017-06-15 17:29:09 -07001369 generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001370 if (!generic_xdp_prog)
1371 return 0;
1372 return generic_xdp_prog->aux->id;
1373}
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001374
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001375static u32 rtnl_xdp_prog_drv(struct net_device *dev)
1376{
1377 return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf, XDP_QUERY_PROG);
1378}
Jakub Kicinski118b4aa2017-12-01 15:08:55 -08001379
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001380static u32 rtnl_xdp_prog_hw(struct net_device *dev)
1381{
1382 return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf,
1383 XDP_QUERY_PROG_HW);
1384}
1385
1386static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev,
1387 u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr,
1388 u32 (*get_prog_id)(struct net_device *dev))
1389{
1390 u32 curr_id;
1391 int err;
1392
1393 curr_id = get_prog_id(dev);
1394 if (!curr_id)
1395 return 0;
1396
1397 *prog_id = curr_id;
1398 err = nla_put_u32(skb, attr, curr_id);
1399 if (err)
1400 return err;
1401
1402 if (*mode != XDP_ATTACHED_NONE)
1403 *mode = XDP_ATTACHED_MULTI;
1404 else
1405 *mode = tgt_mode;
1406
1407 return 0;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001408}
1409
Brenden Blancod1fdd912016-07-19 12:16:49 -07001410static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1411{
Brenden Blancod1fdd912016-07-19 12:16:49 -07001412 struct nlattr *xdp;
Martin KaFai Lau58038692017-06-15 17:29:09 -07001413 u32 prog_id;
Brenden Blancod1fdd912016-07-19 12:16:49 -07001414 int err;
Jakub Kicinski4f91da22018-07-11 20:36:38 -07001415 u8 mode;
Brenden Blancod1fdd912016-07-19 12:16:49 -07001416
Brenden Blancod1fdd912016-07-19 12:16:49 -07001417 xdp = nla_nest_start(skb, IFLA_XDP);
1418 if (!xdp)
1419 return -EMSGSIZE;
David S. Millerb5cdae32017-04-18 15:36:58 -04001420
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001421 prog_id = 0;
1422 mode = XDP_ATTACHED_NONE;
Jakub Kicinski202aabe2018-07-16 19:08:50 -07001423 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB,
1424 IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb);
1425 if (err)
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001426 goto err_cancel;
Jakub Kicinski202aabe2018-07-16 19:08:50 -07001427 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV,
1428 IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv);
1429 if (err)
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001430 goto err_cancel;
Jakub Kicinski202aabe2018-07-16 19:08:50 -07001431 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW,
1432 IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw);
1433 if (err)
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001434 goto err_cancel;
1435
Jakub Kicinski4f91da22018-07-11 20:36:38 -07001436 err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode);
Brenden Blancod1fdd912016-07-19 12:16:49 -07001437 if (err)
1438 goto err_cancel;
1439
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001440 if (prog_id && mode != XDP_ATTACHED_MULTI) {
Martin KaFai Lau58038692017-06-15 17:29:09 -07001441 err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
1442 if (err)
1443 goto err_cancel;
1444 }
1445
Brenden Blancod1fdd912016-07-19 12:16:49 -07001446 nla_nest_end(skb, xdp);
1447 return 0;
1448
1449err_cancel:
1450 nla_nest_cancel(skb, xdp);
1451 return err;
1452}
1453
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001454static u32 rtnl_get_event(unsigned long event)
1455{
1456 u32 rtnl_event_type = IFLA_EVENT_NONE;
1457
1458 switch (event) {
1459 case NETDEV_REBOOT:
1460 rtnl_event_type = IFLA_EVENT_REBOOT;
1461 break;
1462 case NETDEV_FEAT_CHANGE:
1463 rtnl_event_type = IFLA_EVENT_FEATURES;
1464 break;
1465 case NETDEV_BONDING_FAILOVER:
1466 rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
1467 break;
1468 case NETDEV_NOTIFY_PEERS:
1469 rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
1470 break;
1471 case NETDEV_RESEND_IGMP:
1472 rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
1473 break;
1474 case NETDEV_CHANGEINFODATA:
1475 rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
1476 break;
1477 default:
1478 break;
1479 }
1480
1481 return rtnl_event_type;
1482}
1483
Florian Westphal79110a02017-09-26 13:58:40 +02001484static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
1485{
1486 const struct net_device *upper_dev;
1487 int ret = 0;
1488
1489 rcu_read_lock();
1490
1491 upper_dev = netdev_master_upper_dev_get_rcu(dev);
1492 if (upper_dev)
1493 ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);
1494
1495 rcu_read_unlock();
1496 return ret;
1497}
1498
1499static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev)
1500{
1501 int ifindex = dev_get_iflink(dev);
1502
1503 if (dev->ifindex == ifindex)
1504 return 0;
1505
1506 return nla_put_u32(skb, IFLA_LINK, ifindex);
1507}
1508
Florian Westphal6c557002017-10-02 23:50:05 +02001509static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
1510 struct net_device *dev)
1511{
1512 char buf[IFALIASZ];
1513 int ret;
1514
1515 ret = dev_get_alias(dev, buf, sizeof(buf));
1516 return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
1517}
1518
Florian Westphalb1e66b92017-09-26 13:58:42 +02001519static int rtnl_fill_link_netnsid(struct sk_buff *skb,
Jiri Benc79e1ad12017-11-02 17:04:38 -02001520 const struct net_device *dev,
1521 struct net *src_net)
Florian Westphalb1e66b92017-09-26 13:58:42 +02001522{
1523 if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
1524 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1525
1526 if (!net_eq(dev_net(dev), link_net)) {
Jiri Benc79e1ad12017-11-02 17:04:38 -02001527 int id = peernet2id_alloc(src_net, link_net);
Florian Westphalb1e66b92017-09-26 13:58:42 +02001528
1529 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1530 return -EMSGSIZE;
1531 }
1532 }
1533
1534 return 0;
1535}
1536
Florian Westphal070cbf52017-10-16 15:44:35 +02001537static int rtnl_fill_link_af(struct sk_buff *skb,
1538 const struct net_device *dev,
1539 u32 ext_filter_mask)
1540{
1541 const struct rtnl_af_ops *af_ops;
1542 struct nlattr *af_spec;
1543
1544 af_spec = nla_nest_start(skb, IFLA_AF_SPEC);
1545 if (!af_spec)
1546 return -EMSGSIZE;
1547
Florian Westphal5fa85a02017-10-16 15:44:36 +02001548 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Florian Westphal070cbf52017-10-16 15:44:35 +02001549 struct nlattr *af;
1550 int err;
1551
1552 if (!af_ops->fill_link_af)
1553 continue;
1554
1555 af = nla_nest_start(skb, af_ops->family);
1556 if (!af)
1557 return -EMSGSIZE;
1558
1559 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1560 /*
1561 * Caller may return ENODATA to indicate that there
1562 * was no data to be dumped. This is not an error, it
1563 * means we should trim the attribute header and
1564 * continue.
1565 */
1566 if (err == -ENODATA)
1567 nla_nest_cancel(skb, af);
1568 else if (err < 0)
1569 return -EMSGSIZE;
1570
1571 nla_nest_end(skb, af);
1572 }
1573
1574 nla_nest_end(skb, af_spec);
1575 return 0;
1576}
1577
Jiri Benc79e1ad12017-11-02 17:04:38 -02001578static int rtnl_fill_ifinfo(struct sk_buff *skb,
1579 struct net_device *dev, struct net *src_net,
Patrick McHardy575c3e22007-05-22 17:00:49 -07001580 int type, u32 pid, u32 seq, u32 change,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001581 unsigned int flags, u32 ext_filter_mask,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01001582 u32 event, int *new_nsid, int new_ifindex,
1583 int tgt_netnsid)
Thomas Grafb60c5112006-08-04 23:05:34 -07001584{
1585 struct ifinfomsg *ifm;
1586 struct nlmsghdr *nlh;
1587
Eric Dumazet2907c352011-05-25 07:34:04 +00001588 ASSERT_RTNL();
Thomas Grafb60c5112006-08-04 23:05:34 -07001589 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1590 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08001591 return -EMSGSIZE;
Thomas Grafb60c5112006-08-04 23:05:34 -07001592
1593 ifm = nlmsg_data(nlh);
1594 ifm->ifi_family = AF_UNSPEC;
1595 ifm->__ifi_pad = 0;
1596 ifm->ifi_type = dev->type;
1597 ifm->ifi_index = dev->ifindex;
1598 ifm->ifi_flags = dev_get_flags(dev);
1599 ifm->ifi_change = change;
1600
Christian Brauner7e4a8d52018-09-04 21:53:53 +02001601 if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid))
Jiri Benc79e1ad12017-11-02 17:04:38 -02001602 goto nla_put_failure;
1603
David S. Millera6574342012-04-01 20:12:00 -04001604 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1605 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1606 nla_put_u8(skb, IFLA_OPERSTATE,
1607 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1608 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1609 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
Stephen Hemminger3e7a50c2018-07-27 13:43:22 -07001610 nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) ||
1611 nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) ||
David S. Millera6574342012-04-01 20:12:00 -04001612 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
Ben Greearedbc0bb2012-03-29 12:51:30 +00001613 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001614 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
Eric Dumazetc70ce022016-03-21 09:55:10 -07001615 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1616 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001617#ifdef CONFIG_RPS
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001618 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001619#endif
Florian Westphal79110a02017-09-26 13:58:40 +02001620 nla_put_iflink(skb, dev) ||
1621 put_master_ifindex(skb, dev) ||
Jiri Pirko9a572472012-12-27 23:49:39 +00001622 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
David S. Millera6574342012-04-01 20:12:00 -04001623 (dev->qdisc &&
1624 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
Florian Westphal6c557002017-10-02 23:50:05 +02001625 nla_put_ifalias(skb, dev) ||
david decotigny2d3b4792014-03-29 09:48:35 -07001626 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
David Decotignyb2d3bcf2018-01-18 09:59:13 -08001627 atomic_read(&dev->carrier_up_count) +
1628 atomic_read(&dev->carrier_down_count)) ||
1629 nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down) ||
1630 nla_put_u32(skb, IFLA_CARRIER_UP_COUNT,
1631 atomic_read(&dev->carrier_up_count)) ||
1632 nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT,
1633 atomic_read(&dev->carrier_down_count)))
David S. Millera6574342012-04-01 20:12:00 -04001634 goto nla_put_failure;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001635
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001636 if (event != IFLA_EVENT_NONE) {
1637 if (nla_put_u32(skb, IFLA_EVENT, event))
1638 goto nla_put_failure;
1639 }
1640
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001641 if (rtnl_fill_link_ifmap(skb, dev))
1642 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
1644 if (dev->addr_len) {
David S. Millera6574342012-04-01 20:12:00 -04001645 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1646 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1647 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 }
1649
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001650 if (rtnl_phys_port_id_fill(skb, dev))
1651 goto nla_put_failure;
1652
David Aherndb24a902015-03-17 20:23:15 -06001653 if (rtnl_phys_port_name_fill(skb, dev))
1654 goto nla_put_failure;
1655
Jiri Pirko82f28412014-11-28 14:34:18 +01001656 if (rtnl_phys_switch_id_fill(skb, dev))
1657 goto nla_put_failure;
1658
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001659 if (rtnl_fill_stats(skb, dev))
Pavel Emelyanov96e74082008-05-21 14:12:46 -07001660 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Florian Westphal250fc3d2017-09-26 13:58:41 +02001662 if (rtnl_fill_vf(skb, dev, ext_filter_mask))
David S. Millera6574342012-04-01 20:12:00 -04001663 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001664
David Gibsonc53864f2014-04-24 10:22:36 +10001665 if (rtnl_port_fill(skb, dev, ext_filter_mask))
Scott Feldman57b61082010-05-17 22:49:55 -07001666 goto nla_put_failure;
1667
Brenden Blancod1fdd912016-07-19 12:16:49 -07001668 if (rtnl_xdp_fill(skb, dev))
1669 goto nla_put_failure;
1670
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001671 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07001672 if (rtnl_link_fill(skb, dev) < 0)
1673 goto nla_put_failure;
1674 }
1675
Jiri Benc79e1ad12017-11-02 17:04:38 -02001676 if (rtnl_fill_link_netnsid(skb, dev, src_net))
Florian Westphalb1e66b92017-09-26 13:58:42 +02001677 goto nla_put_failure;
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001678
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02001679 if (new_nsid &&
1680 nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0)
1681 goto nla_put_failure;
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01001682 if (new_ifindex &&
1683 nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0)
1684 goto nla_put_failure;
1685
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02001686
Florian Westphal5fa85a02017-10-16 15:44:36 +02001687 rcu_read_lock();
Florian Westphal070cbf52017-10-16 15:44:35 +02001688 if (rtnl_fill_link_af(skb, dev, ext_filter_mask))
Florian Westphal5fa85a02017-10-16 15:44:36 +02001689 goto nla_put_failure_rcu;
1690 rcu_read_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +00001691
Johannes Berg053c0952015-01-16 22:09:00 +01001692 nlmsg_end(skb, nlh);
1693 return 0;
Thomas Grafb60c5112006-08-04 23:05:34 -07001694
Florian Westphal5fa85a02017-10-16 15:44:36 +02001695nla_put_failure_rcu:
1696 rcu_read_unlock();
Thomas Grafb60c5112006-08-04 23:05:34 -07001697nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001698 nlmsg_cancel(skb, nlh);
1699 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700}
1701
Jiri Pirkof7b12602014-02-18 20:53:18 +01001702static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1703 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1704 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1705 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1706 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1707 [IFLA_MTU] = { .type = NLA_U32 },
1708 [IFLA_LINK] = { .type = NLA_U32 },
1709 [IFLA_MASTER] = { .type = NLA_U32 },
1710 [IFLA_CARRIER] = { .type = NLA_U8 },
1711 [IFLA_TXQLEN] = { .type = NLA_U32 },
1712 [IFLA_WEIGHT] = { .type = NLA_U32 },
1713 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1714 [IFLA_LINKMODE] = { .type = NLA_U8 },
1715 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1716 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1717 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
Nicolas Dichtel2459b4c2017-10-11 16:24:48 +02001718 /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
1719 * allow 0-length string (needed to remove an alias).
1720 */
1721 [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001722 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1723 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1724 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1725 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1726 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1727 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1728 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1729 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
Stephen Hemminger46e6b992017-12-07 15:40:19 -08001730 [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 },
1731 [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 },
Jiri Pirko02637fc2014-11-28 14:34:16 +01001732 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
david decotigny2d3b4792014-03-29 09:48:35 -07001733 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
Jiri Pirko82f28412014-11-28 14:34:18 +01001734 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
Nicolas Dichtel317f4812015-01-15 15:11:18 +01001735 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001736 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001737 [IFLA_XDP] = { .type = NLA_NESTED },
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001738 [IFLA_EVENT] = { .type = NLA_U32 },
Serhey Popovychdb833d42017-06-20 14:35:23 +03001739 [IFLA_GROUP] = { .type = NLA_U32 },
Christian Brauner7e4a8d52018-09-04 21:53:53 +02001740 [IFLA_TARGET_NETNSID] = { .type = NLA_S32 },
David Decotignyb2d3bcf2018-01-18 09:59:13 -08001741 [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 },
1742 [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 },
Stephen Hemminger3e7a50c2018-07-27 13:43:22 -07001743 [IFLA_MIN_MTU] = { .type = NLA_U32 },
1744 [IFLA_MAX_MTU] = { .type = NLA_U32 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001745};
1746
1747static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1748 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1749 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1750 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1751 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1752};
1753
Jiri Pirkof7b12602014-02-18 20:53:18 +01001754static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
Daniel Borkmann364d5712015-02-05 18:44:04 +01001755 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1756 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
Moshe Shemesh79aab092016-09-22 12:11:15 +03001757 [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED },
Daniel Borkmann364d5712015-02-05 18:44:04 +01001758 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1759 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1760 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1761 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001762 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001763 [IFLA_VF_STATS] = { .type = NLA_NESTED },
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00001764 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
Eli Cohencc8e27c2016-03-11 22:58:34 +02001765 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1766 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001767};
1768
Jiri Pirkof7b12602014-02-18 20:53:18 +01001769static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1770 [IFLA_PORT_VF] = { .type = NLA_U32 },
1771 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1772 .len = PORT_PROFILE_MAX },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001773 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1774 .len = PORT_UUID_MAX },
1775 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1776 .len = PORT_UUID_MAX },
1777 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1778 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
Daniel Borkmann025331d2017-02-17 01:56:11 +01001779
1780 /* Unused, but we need to keep it here since user space could
1781 * fill it. It's also broken with regard to NLA_BINARY use in
1782 * combination with structs.
1783 */
1784 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1785 .len = sizeof(struct ifla_port_vsi) },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001786};
1787
Brenden Blancod1fdd912016-07-19 12:16:49 -07001788static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
1789 [IFLA_XDP_FD] = { .type = NLA_S32 },
1790 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 },
Daniel Borkmann85de8572016-11-28 23:16:54 +01001791 [IFLA_XDP_FLAGS] = { .type = NLA_U32 },
Martin KaFai Lau58038692017-06-15 17:29:09 -07001792 [IFLA_XDP_PROG_ID] = { .type = NLA_U32 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001793};
1794
David Aherndc599f72016-02-02 08:17:07 -08001795static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1796{
1797 const struct rtnl_link_ops *ops = NULL;
1798 struct nlattr *linfo[IFLA_INFO_MAX + 1];
1799
Johannes Bergfceb6432017-04-12 14:34:07 +02001800 if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla,
1801 ifla_info_policy, NULL) < 0)
David Aherndc599f72016-02-02 08:17:07 -08001802 return NULL;
1803
1804 if (linfo[IFLA_INFO_KIND]) {
1805 char kind[MODULE_NAME_LEN];
1806
1807 nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1808 ops = rtnl_link_ops_get(kind);
1809 }
1810
1811 return ops;
1812}
1813
1814static bool link_master_filtered(struct net_device *dev, int master_idx)
1815{
1816 struct net_device *master;
1817
1818 if (!master_idx)
1819 return false;
1820
1821 master = netdev_master_upper_dev_get(dev);
1822 if (!master || master->ifindex != master_idx)
1823 return true;
1824
1825 return false;
1826}
1827
1828static bool link_kind_filtered(const struct net_device *dev,
1829 const struct rtnl_link_ops *kind_ops)
1830{
1831 if (kind_ops && dev->rtnl_link_ops != kind_ops)
1832 return true;
1833
1834 return false;
1835}
1836
1837static bool link_dump_filtered(struct net_device *dev,
1838 int master_idx,
1839 const struct rtnl_link_ops *kind_ops)
1840{
1841 if (link_master_filtered(dev, master_idx) ||
1842 link_kind_filtered(dev, kind_ops))
1843 return true;
1844
1845 return false;
1846}
1847
Christian Braunerc383edc2018-09-04 21:53:47 +02001848/**
1849 * rtnl_get_net_ns_capable - Get netns if sufficiently privileged.
1850 * @sk: netlink socket
1851 * @netnsid: network namespace identifier
1852 *
1853 * Returns the network namespace identified by netnsid on success or an error
1854 * pointer on failure.
1855 */
1856struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid)
Jiri Benc79e1ad12017-11-02 17:04:38 -02001857{
1858 struct net *net;
1859
Andrei Vaginf428fe42018-01-02 23:27:33 -08001860 net = get_net_ns_by_id(sock_net(sk), netnsid);
Jiri Benc79e1ad12017-11-02 17:04:38 -02001861 if (!net)
1862 return ERR_PTR(-EINVAL);
1863
1864 /* For now, the caller is required to have CAP_NET_ADMIN in
1865 * the user namespace owning the target net ns.
1866 */
Andrei Vaginf428fe42018-01-02 23:27:33 -08001867 if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) {
Jiri Benc79e1ad12017-11-02 17:04:38 -02001868 put_net(net);
1869 return ERR_PTR(-EACCES);
1870 }
1871 return net;
1872}
Christian Braunerc383edc2018-09-04 21:53:47 +02001873EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable);
Jiri Benc79e1ad12017-11-02 17:04:38 -02001874
Thomas Grafb60c5112006-08-04 23:05:34 -07001875static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001877 struct net *net = sock_net(skb->sk);
Jiri Benc79e1ad12017-11-02 17:04:38 -02001878 struct net *tgt_net = net;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001879 int h, s_h;
1880 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 struct net_device *dev;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001882 struct hlist_head *head;
Greg Rose115c9b82012-02-21 16:54:48 -05001883 struct nlattr *tb[IFLA_MAX+1];
1884 u32 ext_filter_mask = 0;
David Aherndc599f72016-02-02 08:17:07 -08001885 const struct rtnl_link_ops *kind_ops = NULL;
1886 unsigned int flags = NLM_F_MULTI;
1887 int master_idx = 0;
Jiri Benc79e1ad12017-11-02 17:04:38 -02001888 int netnsid = -1;
David Gibson973462b2014-04-24 10:22:35 +10001889 int err;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001890 int hdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001892 s_h = cb->args[0];
1893 s_idx = cb->args[1];
1894
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001895 /* A hack to preserve kernel<->userspace interface.
1896 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1897 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1898 * what iproute2 < v3.9.0 used.
1899 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1900 * attribute, its netlink message is shorter than struct ifinfomsg.
1901 */
1902 hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1903 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1904
Johannes Bergfceb6432017-04-12 14:34:07 +02001905 if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX,
1906 ifla_policy, NULL) >= 0) {
Christian Brauner7e4a8d52018-09-04 21:53:53 +02001907 if (tb[IFLA_TARGET_NETNSID]) {
1908 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
Christian Braunerc383edc2018-09-04 21:53:47 +02001909 tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid);
Jiri Benc79e1ad12017-11-02 17:04:38 -02001910 if (IS_ERR(tgt_net)) {
1911 tgt_net = net;
1912 netnsid = -1;
1913 }
1914 }
1915
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001916 if (tb[IFLA_EXT_MASK])
1917 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
David Aherndc599f72016-02-02 08:17:07 -08001918
1919 if (tb[IFLA_MASTER])
1920 master_idx = nla_get_u32(tb[IFLA_MASTER]);
1921
1922 if (tb[IFLA_LINKINFO])
1923 kind_ops = linkinfo_to_kind_ops(tb[IFLA_LINKINFO]);
1924
1925 if (master_idx || kind_ops)
1926 flags |= NLM_F_DUMP_FILTERED;
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001927 }
Greg Rose115c9b82012-02-21 16:54:48 -05001928
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001929 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1930 idx = 0;
Jiri Benc79e1ad12017-11-02 17:04:38 -02001931 head = &tgt_net->dev_index_head[h];
Eric Dumazetcac5e652015-02-27 09:42:50 -08001932 hlist_for_each_entry(dev, head, index_hlist) {
David Aherndc599f72016-02-02 08:17:07 -08001933 if (link_dump_filtered(dev, master_idx, kind_ops))
Zhang Shengju3f0ae052016-11-19 23:28:32 +08001934 goto cont;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001935 if (idx < s_idx)
1936 goto cont;
Jiri Benc79e1ad12017-11-02 17:04:38 -02001937 err = rtnl_fill_ifinfo(skb, dev, net,
1938 RTM_NEWLINK,
David Gibson973462b2014-04-24 10:22:35 +10001939 NETLINK_CB(cb->skb).portid,
1940 cb->nlh->nlmsg_seq, 0,
David Aherndc599f72016-02-02 08:17:07 -08001941 flags,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01001942 ext_filter_mask, 0, NULL, 0,
Jiri Benc79e1ad12017-11-02 17:04:38 -02001943 netnsid);
David Gibson973462b2014-04-24 10:22:35 +10001944
David Ahernf6c57752017-05-15 23:19:17 -07001945 if (err < 0) {
1946 if (likely(skb->len))
1947 goto out;
1948
1949 goto out_err;
1950 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07001951cont:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001952 idx++;
1953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 }
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001955out:
David Ahernf6c57752017-05-15 23:19:17 -07001956 err = skb->len;
1957out_err:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001958 cb->args[1] = idx;
1959 cb->args[0] = h;
Jakub Sitnickid0225782017-08-09 17:39:12 +02001960 cb->seq = net->dev_base_seq;
1961 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Jiri Benc79e1ad12017-11-02 17:04:38 -02001962 if (netnsid >= 0)
1963 put_net(tgt_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
David Ahernf6c57752017-05-15 23:19:17 -07001965 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966}
1967
Johannes Bergfceb6432017-04-12 14:34:07 +02001968int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
1969 struct netlink_ext_ack *exterr)
Jiri Pirkof7b12602014-02-18 20:53:18 +01001970{
Johannes Bergfceb6432017-04-12 14:34:07 +02001971 return nla_parse(tb, IFLA_MAX, head, len, ifla_policy, exterr);
Jiri Pirkof7b12602014-02-18 20:53:18 +01001972}
1973EXPORT_SYMBOL(rtnl_nla_parse_ifla);
Scott Feldman57b61082010-05-17 22:49:55 -07001974
Eric W. Biederman81adee42009-11-08 00:53:51 -08001975struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1976{
1977 struct net *net;
1978 /* Examine the link attributes and figure out which
1979 * network namespace we are talking about.
1980 */
1981 if (tb[IFLA_NET_NS_PID])
1982 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001983 else if (tb[IFLA_NET_NS_FD])
1984 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
Eric W. Biederman81adee42009-11-08 00:53:51 -08001985 else
1986 net = get_net(src_net);
1987 return net;
1988}
1989EXPORT_SYMBOL(rtnl_link_get_net);
1990
Christian Brauner7c4f63b2018-01-24 15:26:32 +01001991/* Figure out which network namespace we are talking about by
1992 * examining the link attributes in the following order:
1993 *
1994 * 1. IFLA_NET_NS_PID
1995 * 2. IFLA_NET_NS_FD
Christian Brauner7e4a8d52018-09-04 21:53:53 +02001996 * 3. IFLA_TARGET_NETNSID
Christian Brauner7c4f63b2018-01-24 15:26:32 +01001997 */
1998static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net,
1999 struct nlattr *tb[])
2000{
2001 struct net *net;
2002
2003 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])
2004 return rtnl_link_get_net(src_net, tb);
2005
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002006 if (!tb[IFLA_TARGET_NETNSID])
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002007 return get_net(src_net);
2008
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002009 net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID]));
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002010 if (!net)
2011 return ERR_PTR(-EINVAL);
2012
2013 return net;
2014}
2015
2016static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb,
2017 struct net *src_net,
2018 struct nlattr *tb[], int cap)
2019{
2020 struct net *net;
2021
2022 net = rtnl_link_get_net_by_nlattr(src_net, tb);
2023 if (IS_ERR(net))
2024 return net;
2025
2026 if (!netlink_ns_capable(skb, net->user_ns, cap)) {
2027 put_net(net);
2028 return ERR_PTR(-EPERM);
2029 }
2030
2031 return net;
2032}
2033
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002034/* Verify that rtnetlink requests do not pass additional properties
2035 * potentially referring to different network namespaces.
2036 */
2037static int rtnl_ensure_unique_netns(struct nlattr *tb[],
2038 struct netlink_ext_ack *extack,
2039 bool netns_id_only)
2040{
2041
2042 if (netns_id_only) {
2043 if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD])
2044 return 0;
2045
2046 NL_SET_ERR_MSG(extack, "specified netns attribute not supported");
2047 return -EOPNOTSUPP;
2048 }
2049
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002050 if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]))
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002051 goto invalid_attr;
2052
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002053 if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD]))
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002054 goto invalid_attr;
2055
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002056 if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID]))
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002057 goto invalid_attr;
2058
2059 return 0;
2060
2061invalid_attr:
2062 NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified");
2063 return -EINVAL;
2064}
2065
Thomas Graf1840bb12008-02-23 19:54:36 -08002066static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
2067{
2068 if (dev) {
2069 if (tb[IFLA_ADDRESS] &&
2070 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
2071 return -EINVAL;
2072
2073 if (tb[IFLA_BROADCAST] &&
2074 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
2075 return -EINVAL;
2076 }
2077
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002078 if (tb[IFLA_AF_SPEC]) {
2079 struct nlattr *af;
2080 int rem, err;
2081
2082 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2083 const struct rtnl_af_ops *af_ops;
2084
Florian Westphal5fa85a02017-10-16 15:44:36 +02002085 rcu_read_lock();
2086 af_ops = rtnl_af_lookup(nla_type(af));
2087 if (!af_ops) {
2088 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002089 return -EAFNOSUPPORT;
Florian Westphal5fa85a02017-10-16 15:44:36 +02002090 }
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002091
Florian Westphal5fa85a02017-10-16 15:44:36 +02002092 if (!af_ops->set_link_af) {
2093 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002094 return -EOPNOTSUPP;
Florian Westphal5fa85a02017-10-16 15:44:36 +02002095 }
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002096
2097 if (af_ops->validate_link_af) {
Kurt Van Dijck6d3a9a62011-01-26 04:55:24 +00002098 err = af_ops->validate_link_af(dev, af);
Florian Westphal5fa85a02017-10-16 15:44:36 +02002099 if (err < 0) {
2100 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002101 return err;
Florian Westphal5fa85a02017-10-16 15:44:36 +02002102 }
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002103 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02002104
2105 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002106 }
2107 }
2108
Thomas Graf1840bb12008-02-23 19:54:36 -08002109 return 0;
2110}
2111
Eli Cohencc8e27c2016-03-11 22:58:34 +02002112static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
2113 int guid_type)
2114{
2115 const struct net_device_ops *ops = dev->netdev_ops;
2116
2117 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
2118}
2119
2120static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
2121{
2122 if (dev->type != ARPHRD_INFINIBAND)
2123 return -EOPNOTSUPP;
2124
2125 return handle_infiniband_guid(dev, ivt, guid_type);
2126}
2127
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002128static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
Chris Wrightc02db8c2010-05-16 01:05:45 -07002129{
Chris Wrightc02db8c2010-05-16 01:05:45 -07002130 const struct net_device_ops *ops = dev->netdev_ops;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002131 int err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002132
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002133 if (tb[IFLA_VF_MAC]) {
2134 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03002135
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002136 err = -EOPNOTSUPP;
2137 if (ops->ndo_set_vf_mac)
2138 err = ops->ndo_set_vf_mac(dev, ivm->vf,
2139 ivm->mac);
2140 if (err < 0)
2141 return err;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002142 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002143
2144 if (tb[IFLA_VF_VLAN]) {
2145 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
2146
2147 err = -EOPNOTSUPP;
2148 if (ops->ndo_set_vf_vlan)
2149 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
Moshe Shemesh79aab092016-09-22 12:11:15 +03002150 ivv->qos,
2151 htons(ETH_P_8021Q));
2152 if (err < 0)
2153 return err;
2154 }
2155
2156 if (tb[IFLA_VF_VLAN_LIST]) {
2157 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
2158 struct nlattr *attr;
2159 int rem, len = 0;
2160
2161 err = -EOPNOTSUPP;
2162 if (!ops->ndo_set_vf_vlan)
2163 return err;
2164
2165 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
2166 if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
2167 nla_len(attr) < NLA_HDRLEN) {
2168 return -EINVAL;
2169 }
2170 if (len >= MAX_VLAN_LIST_LEN)
2171 return -EOPNOTSUPP;
2172 ivvl[len] = nla_data(attr);
2173
2174 len++;
2175 }
Arnd Bergmannfa34cd92016-09-30 18:13:49 +02002176 if (len == 0)
2177 return -EINVAL;
2178
Moshe Shemesh79aab092016-09-22 12:11:15 +03002179 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
2180 ivvl[0]->qos, ivvl[0]->vlan_proto);
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002181 if (err < 0)
2182 return err;
2183 }
2184
2185 if (tb[IFLA_VF_TX_RATE]) {
2186 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
2187 struct ifla_vf_info ivf;
2188
2189 err = -EOPNOTSUPP;
2190 if (ops->ndo_get_vf_config)
2191 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
2192 if (err < 0)
2193 return err;
2194
2195 err = -EOPNOTSUPP;
2196 if (ops->ndo_set_vf_rate)
2197 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2198 ivf.min_tx_rate,
2199 ivt->rate);
2200 if (err < 0)
2201 return err;
2202 }
2203
2204 if (tb[IFLA_VF_RATE]) {
2205 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
2206
2207 err = -EOPNOTSUPP;
2208 if (ops->ndo_set_vf_rate)
2209 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2210 ivt->min_tx_rate,
2211 ivt->max_tx_rate);
2212 if (err < 0)
2213 return err;
2214 }
2215
2216 if (tb[IFLA_VF_SPOOFCHK]) {
2217 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
2218
2219 err = -EOPNOTSUPP;
2220 if (ops->ndo_set_vf_spoofchk)
2221 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
2222 ivs->setting);
2223 if (err < 0)
2224 return err;
2225 }
2226
2227 if (tb[IFLA_VF_LINK_STATE]) {
2228 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
2229
2230 err = -EOPNOTSUPP;
2231 if (ops->ndo_set_vf_link_state)
2232 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
2233 ivl->link_state);
2234 if (err < 0)
2235 return err;
2236 }
2237
2238 if (tb[IFLA_VF_RSS_QUERY_EN]) {
2239 struct ifla_vf_rss_query_en *ivrssq_en;
2240
2241 err = -EOPNOTSUPP;
2242 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
2243 if (ops->ndo_set_vf_rss_query_en)
2244 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
2245 ivrssq_en->setting);
2246 if (err < 0)
2247 return err;
2248 }
2249
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00002250 if (tb[IFLA_VF_TRUST]) {
2251 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
2252
2253 err = -EOPNOTSUPP;
2254 if (ops->ndo_set_vf_trust)
2255 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
2256 if (err < 0)
2257 return err;
2258 }
2259
Eli Cohencc8e27c2016-03-11 22:58:34 +02002260 if (tb[IFLA_VF_IB_NODE_GUID]) {
2261 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
2262
2263 if (!ops->ndo_set_vf_guid)
2264 return -EOPNOTSUPP;
2265
2266 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
2267 }
2268
2269 if (tb[IFLA_VF_IB_PORT_GUID]) {
2270 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
2271
2272 if (!ops->ndo_set_vf_guid)
2273 return -EOPNOTSUPP;
2274
2275 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
2276 }
2277
Chris Wrightc02db8c2010-05-16 01:05:45 -07002278 return err;
2279}
2280
David Ahern33eaf2a2017-10-04 17:48:46 -07002281static int do_set_master(struct net_device *dev, int ifindex,
2282 struct netlink_ext_ack *extack)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002283{
Jiri Pirko898e5062013-01-03 22:48:52 +00002284 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002285 const struct net_device_ops *ops;
2286 int err;
2287
Jiri Pirko898e5062013-01-03 22:48:52 +00002288 if (upper_dev) {
2289 if (upper_dev->ifindex == ifindex)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002290 return 0;
Jiri Pirko898e5062013-01-03 22:48:52 +00002291 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002292 if (ops->ndo_del_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002293 err = ops->ndo_del_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002294 if (err)
2295 return err;
2296 } else {
2297 return -EOPNOTSUPP;
2298 }
2299 }
2300
2301 if (ifindex) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002302 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
2303 if (!upper_dev)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002304 return -EINVAL;
Jiri Pirko898e5062013-01-03 22:48:52 +00002305 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002306 if (ops->ndo_add_slave) {
David Ahern33eaf2a2017-10-04 17:48:46 -07002307 err = ops->ndo_add_slave(upper_dev, dev, extack);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002308 if (err)
2309 return err;
2310 } else {
2311 return -EOPNOTSUPP;
2312 }
2313 }
2314 return 0;
2315}
2316
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002317#define DO_SETLINK_MODIFIED 0x01
Nicolas Dichtelba998902014-09-01 16:07:29 +02002318/* notify flag means notify + modified. */
2319#define DO_SETLINK_NOTIFY 0x03
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002320static int do_setlink(const struct sk_buff *skb,
2321 struct net_device *dev, struct ifinfomsg *ifm,
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002322 struct netlink_ext_ack *extack,
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002323 struct nlattr **tb, char *ifname, int status)
Patrick McHardy0157f602007-06-13 12:03:36 -07002324{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002325 const struct net_device_ops *ops = dev->netdev_ops;
Patrick McHardy0157f602007-06-13 12:03:36 -07002326 int err;
2327
Eric Dumazet644c7ee2018-06-05 09:25:19 -07002328 err = validate_linkmsg(dev, tb);
2329 if (err < 0)
2330 return err;
2331
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002332 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) {
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002333 struct net *net = rtnl_link_get_net_capable(skb, dev_net(dev),
2334 tb, CAP_NET_ADMIN);
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002335 if (IS_ERR(net)) {
2336 err = PTR_ERR(net);
2337 goto errout;
2338 }
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002339
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002340 err = dev_change_net_namespace(dev, net, ifname);
2341 put_net(net);
2342 if (err)
2343 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002344 status |= DO_SETLINK_MODIFIED;
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002345 }
2346
Patrick McHardy0157f602007-06-13 12:03:36 -07002347 if (tb[IFLA_MAP]) {
2348 struct rtnl_link_ifmap *u_map;
2349 struct ifmap k_map;
2350
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002351 if (!ops->ndo_set_config) {
Patrick McHardy0157f602007-06-13 12:03:36 -07002352 err = -EOPNOTSUPP;
2353 goto errout;
2354 }
2355
2356 if (!netif_device_present(dev)) {
2357 err = -ENODEV;
2358 goto errout;
2359 }
2360
2361 u_map = nla_data(tb[IFLA_MAP]);
2362 k_map.mem_start = (unsigned long) u_map->mem_start;
2363 k_map.mem_end = (unsigned long) u_map->mem_end;
2364 k_map.base_addr = (unsigned short) u_map->base_addr;
2365 k_map.irq = (unsigned char) u_map->irq;
2366 k_map.dma = (unsigned char) u_map->dma;
2367 k_map.port = (unsigned char) u_map->port;
2368
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002369 err = ops->ndo_set_config(dev, &k_map);
Patrick McHardy0157f602007-06-13 12:03:36 -07002370 if (err < 0)
2371 goto errout;
2372
Nicolas Dichtelba998902014-09-01 16:07:29 +02002373 status |= DO_SETLINK_NOTIFY;
Patrick McHardy0157f602007-06-13 12:03:36 -07002374 }
2375
2376 if (tb[IFLA_ADDRESS]) {
2377 struct sockaddr *sa;
2378 int len;
2379
WANG Cong153711f2017-07-20 11:27:57 -07002380 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2381 sizeof(*sa));
Patrick McHardy0157f602007-06-13 12:03:36 -07002382 sa = kmalloc(len, GFP_KERNEL);
2383 if (!sa) {
2384 err = -ENOMEM;
2385 goto errout;
2386 }
2387 sa->sa_family = dev->type;
2388 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
2389 dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00002390 err = dev_set_mac_address(dev, sa);
Patrick McHardy0157f602007-06-13 12:03:36 -07002391 kfree(sa);
2392 if (err)
2393 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002394 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002395 }
2396
2397 if (tb[IFLA_MTU]) {
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07002398 err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack);
Patrick McHardy0157f602007-06-13 12:03:36 -07002399 if (err < 0)
2400 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002401 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002402 }
2403
Vlad Dogarucbda10f2011-01-13 23:38:30 +00002404 if (tb[IFLA_GROUP]) {
2405 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Nicolas Dichtelba998902014-09-01 16:07:29 +02002406 status |= DO_SETLINK_NOTIFY;
Vlad Dogarucbda10f2011-01-13 23:38:30 +00002407 }
2408
Patrick McHardy0157f602007-06-13 12:03:36 -07002409 /*
2410 * Interface selected by interface index but interface
2411 * name provided implies that a name change has been
2412 * requested.
2413 */
2414 if (ifm->ifi_index > 0 && ifname[0]) {
2415 err = dev_change_name(dev, ifname);
2416 if (err < 0)
2417 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002418 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002419 }
2420
Stephen Hemminger0b815a12008-09-22 21:28:11 -07002421 if (tb[IFLA_IFALIAS]) {
2422 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2423 nla_len(tb[IFLA_IFALIAS]));
2424 if (err < 0)
2425 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002426 status |= DO_SETLINK_NOTIFY;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07002427 }
2428
Patrick McHardy0157f602007-06-13 12:03:36 -07002429 if (tb[IFLA_BROADCAST]) {
2430 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00002431 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Patrick McHardy0157f602007-06-13 12:03:36 -07002432 }
2433
2434 if (ifm->ifi_flags || ifm->ifi_change) {
Patrick McHardy3729d502010-02-26 06:34:54 +00002435 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
Johannes Berg5f9021c2008-11-16 23:20:31 -08002436 if (err < 0)
2437 goto errout;
Patrick McHardy0157f602007-06-13 12:03:36 -07002438 }
2439
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002440 if (tb[IFLA_MASTER]) {
David Ahern33eaf2a2017-10-04 17:48:46 -07002441 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002442 if (err)
2443 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002444 status |= DO_SETLINK_MODIFIED;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002445 }
2446
Jiri Pirko9a572472012-12-27 23:49:39 +00002447 if (tb[IFLA_CARRIER]) {
2448 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2449 if (err)
2450 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002451 status |= DO_SETLINK_MODIFIED;
Jiri Pirko9a572472012-12-27 23:49:39 +00002452 }
2453
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002454 if (tb[IFLA_TXQLEN]) {
Alexey Dobriyan0cd29502017-05-17 13:30:44 +03002455 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002456
Cong Wang6a643dd2018-01-25 18:26:22 -08002457 err = dev_change_tx_queue_len(dev, value);
2458 if (err)
2459 goto errout;
2460 status |= DO_SETLINK_MODIFIED;
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002461 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002462
Stephen Hemminger46e6b992017-12-07 15:40:19 -08002463 if (tb[IFLA_GSO_MAX_SIZE]) {
2464 u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]);
2465
2466 if (max_size > GSO_MAX_SIZE) {
2467 err = -EINVAL;
2468 goto errout;
2469 }
2470
2471 if (dev->gso_max_size ^ max_size) {
2472 netif_set_gso_max_size(dev, max_size);
2473 status |= DO_SETLINK_MODIFIED;
2474 }
2475 }
2476
2477 if (tb[IFLA_GSO_MAX_SEGS]) {
2478 u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
2479
2480 if (max_segs > GSO_MAX_SEGS) {
2481 err = -EINVAL;
2482 goto errout;
2483 }
2484
2485 if (dev->gso_max_segs ^ max_segs) {
2486 dev->gso_max_segs = max_segs;
2487 status |= DO_SETLINK_MODIFIED;
2488 }
2489 }
2490
Patrick McHardy0157f602007-06-13 12:03:36 -07002491 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002492 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Patrick McHardy0157f602007-06-13 12:03:36 -07002493
2494 if (tb[IFLA_LINKMODE]) {
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002495 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2496
David S. Miller93b2d4a2008-02-17 18:35:07 -08002497 write_lock_bh(&dev_base_lock);
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002498 if (dev->link_mode ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02002499 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002500 dev->link_mode = value;
David S. Miller93b2d4a2008-02-17 18:35:07 -08002501 write_unlock_bh(&dev_base_lock);
Patrick McHardy0157f602007-06-13 12:03:36 -07002502 }
2503
Chris Wrightc02db8c2010-05-16 01:05:45 -07002504 if (tb[IFLA_VFINFO_LIST]) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002505 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
Chris Wrightc02db8c2010-05-16 01:05:45 -07002506 struct nlattr *attr;
2507 int rem;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002508
Chris Wrightc02db8c2010-05-16 01:05:45 -07002509 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002510 if (nla_type(attr) != IFLA_VF_INFO ||
2511 nla_len(attr) < NLA_HDRLEN) {
David Howells253683b2010-05-21 02:25:27 +00002512 err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002513 goto errout;
David Howells253683b2010-05-21 02:25:27 +00002514 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002515 err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr,
Johannes Bergfceb6432017-04-12 14:34:07 +02002516 ifla_vf_policy, NULL);
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002517 if (err < 0)
2518 goto errout;
2519 err = do_setvfinfo(dev, vfinfo);
Chris Wrightc02db8c2010-05-16 01:05:45 -07002520 if (err < 0)
2521 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002522 status |= DO_SETLINK_NOTIFY;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002523 }
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00002524 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002525 err = 0;
2526
Scott Feldman57b61082010-05-17 22:49:55 -07002527 if (tb[IFLA_VF_PORTS]) {
2528 struct nlattr *port[IFLA_PORT_MAX+1];
2529 struct nlattr *attr;
2530 int vf;
2531 int rem;
2532
2533 err = -EOPNOTSUPP;
2534 if (!ops->ndo_set_vf_port)
2535 goto errout;
2536
2537 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
Daniel Borkmann035d2102015-07-13 00:06:02 +02002538 if (nla_type(attr) != IFLA_VF_PORT ||
2539 nla_len(attr) < NLA_HDRLEN) {
2540 err = -EINVAL;
2541 goto errout;
2542 }
2543 err = nla_parse_nested(port, IFLA_PORT_MAX, attr,
Johannes Bergfceb6432017-04-12 14:34:07 +02002544 ifla_port_policy, NULL);
Scott Feldman57b61082010-05-17 22:49:55 -07002545 if (err < 0)
2546 goto errout;
2547 if (!port[IFLA_PORT_VF]) {
2548 err = -EOPNOTSUPP;
2549 goto errout;
2550 }
2551 vf = nla_get_u32(port[IFLA_PORT_VF]);
2552 err = ops->ndo_set_vf_port(dev, vf, port);
2553 if (err < 0)
2554 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002555 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07002556 }
2557 }
2558 err = 0;
2559
2560 if (tb[IFLA_PORT_SELF]) {
2561 struct nlattr *port[IFLA_PORT_MAX+1];
2562
2563 err = nla_parse_nested(port, IFLA_PORT_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +02002564 tb[IFLA_PORT_SELF], ifla_port_policy,
2565 NULL);
Scott Feldman57b61082010-05-17 22:49:55 -07002566 if (err < 0)
2567 goto errout;
2568
2569 err = -EOPNOTSUPP;
2570 if (ops->ndo_set_vf_port)
2571 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
2572 if (err < 0)
2573 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002574 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07002575 }
Thomas Graff8ff1822010-11-16 04:30:14 +00002576
2577 if (tb[IFLA_AF_SPEC]) {
2578 struct nlattr *af;
2579 int rem;
2580
2581 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2582 const struct rtnl_af_ops *af_ops;
2583
Florian Westphal5fa85a02017-10-16 15:44:36 +02002584 rcu_read_lock();
2585
Gustavo A. R. Silva058c8d52017-10-20 19:43:11 -05002586 BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
Thomas Graff8ff1822010-11-16 04:30:14 +00002587
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002588 err = af_ops->set_link_af(dev, af);
Florian Westphal5fa85a02017-10-16 15:44:36 +02002589 if (err < 0) {
2590 rcu_read_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +00002591 goto errout;
Florian Westphal5fa85a02017-10-16 15:44:36 +02002592 }
Thomas Graff8ff1822010-11-16 04:30:14 +00002593
Florian Westphal5fa85a02017-10-16 15:44:36 +02002594 rcu_read_unlock();
Nicolas Dichtelba998902014-09-01 16:07:29 +02002595 status |= DO_SETLINK_NOTIFY;
Thomas Graff8ff1822010-11-16 04:30:14 +00002596 }
2597 }
Scott Feldman57b61082010-05-17 22:49:55 -07002598 err = 0;
2599
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07002600 if (tb[IFLA_PROTO_DOWN]) {
2601 err = dev_change_proto_down(dev,
2602 nla_get_u8(tb[IFLA_PROTO_DOWN]));
2603 if (err)
2604 goto errout;
2605 status |= DO_SETLINK_NOTIFY;
2606 }
2607
Brenden Blancod1fdd912016-07-19 12:16:49 -07002608 if (tb[IFLA_XDP]) {
2609 struct nlattr *xdp[IFLA_XDP_MAX + 1];
Daniel Borkmann85de8572016-11-28 23:16:54 +01002610 u32 xdp_flags = 0;
Brenden Blancod1fdd912016-07-19 12:16:49 -07002611
2612 err = nla_parse_nested(xdp, IFLA_XDP_MAX, tb[IFLA_XDP],
Johannes Bergfceb6432017-04-12 14:34:07 +02002613 ifla_xdp_policy, NULL);
Brenden Blancod1fdd912016-07-19 12:16:49 -07002614 if (err < 0)
2615 goto errout;
2616
Martin KaFai Lau58038692017-06-15 17:29:09 -07002617 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
Brenden Blanco262d8622016-07-20 17:22:34 -07002618 err = -EINVAL;
2619 goto errout;
2620 }
Daniel Borkmann85de8572016-11-28 23:16:54 +01002621
2622 if (xdp[IFLA_XDP_FLAGS]) {
2623 xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
2624 if (xdp_flags & ~XDP_FLAGS_MASK) {
2625 err = -EINVAL;
2626 goto errout;
2627 }
Jakub Kicinskiee5d0322017-06-21 18:25:04 -07002628 if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
Daniel Borkmann0489df92017-05-12 01:04:45 +02002629 err = -EINVAL;
2630 goto errout;
2631 }
Daniel Borkmann85de8572016-11-28 23:16:54 +01002632 }
2633
Brenden Blancod1fdd912016-07-19 12:16:49 -07002634 if (xdp[IFLA_XDP_FD]) {
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002635 err = dev_change_xdp_fd(dev, extack,
Daniel Borkmann85de8572016-11-28 23:16:54 +01002636 nla_get_s32(xdp[IFLA_XDP_FD]),
2637 xdp_flags);
Brenden Blancod1fdd912016-07-19 12:16:49 -07002638 if (err)
2639 goto errout;
2640 status |= DO_SETLINK_NOTIFY;
2641 }
2642 }
2643
Patrick McHardy0157f602007-06-13 12:03:36 -07002644errout:
Nicolas Dichtelba998902014-09-01 16:07:29 +02002645 if (status & DO_SETLINK_MODIFIED) {
Xin Long64ff90c2017-10-15 18:13:45 +08002646 if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
Nicolas Dichtelba998902014-09-01 16:07:29 +02002647 netdev_state_change(dev);
2648
2649 if (err < 0)
2650 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",
2651 dev->name);
2652 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002653
Patrick McHardy0157f602007-06-13 12:03:36 -07002654 return err;
2655}
2656
David Ahernc21ef3e2017-04-16 09:48:24 -07002657static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2658 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002660 struct net *net = sock_net(skb->sk);
Thomas Grafda5e0492006-08-10 21:17:37 -07002661 struct ifinfomsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 struct net_device *dev;
Patrick McHardy0157f602007-06-13 12:03:36 -07002663 int err;
Thomas Grafda5e0492006-08-10 21:17:37 -07002664 struct nlattr *tb[IFLA_MAX+1];
2665 char ifname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666
David Ahernc21ef3e2017-04-16 09:48:24 -07002667 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy,
2668 extack);
Thomas Grafda5e0492006-08-10 21:17:37 -07002669 if (err < 0)
2670 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002672 err = rtnl_ensure_unique_netns(tb, extack, false);
2673 if (err < 0)
2674 goto errout;
2675
Thomas Graf5176f912006-08-26 20:13:18 -07002676 if (tb[IFLA_IFNAME])
2677 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
Patrick McHardy78e5b8912006-09-13 20:35:36 -07002678 else
2679 ifname[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 err = -EINVAL;
Thomas Grafda5e0492006-08-10 21:17:37 -07002682 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -07002683 if (ifm->ifi_index > 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00002684 dev = __dev_get_by_index(net, ifm->ifi_index);
Thomas Grafda5e0492006-08-10 21:17:37 -07002685 else if (tb[IFLA_IFNAME])
Eric Dumazeta3d12892009-10-21 10:59:31 +00002686 dev = __dev_get_by_name(net, ifname);
Thomas Grafda5e0492006-08-10 21:17:37 -07002687 else
2688 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689
Thomas Grafda5e0492006-08-10 21:17:37 -07002690 if (dev == NULL) {
2691 err = -ENODEV;
2692 goto errout;
2693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002695 err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
Thomas Grafda5e0492006-08-10 21:17:37 -07002696errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 return err;
2698}
2699
WANG Cong66400d52015-03-24 11:53:31 -07002700static int rtnl_group_dellink(const struct net *net, int group)
2701{
2702 struct net_device *dev, *aux;
2703 LIST_HEAD(list_kill);
2704 bool found = false;
2705
2706 if (!group)
2707 return -EPERM;
2708
2709 for_each_netdev(net, dev) {
2710 if (dev->group == group) {
2711 const struct rtnl_link_ops *ops;
2712
2713 found = true;
2714 ops = dev->rtnl_link_ops;
2715 if (!ops || !ops->dellink)
2716 return -EOPNOTSUPP;
2717 }
2718 }
2719
2720 if (!found)
2721 return -ENODEV;
2722
2723 for_each_netdev_safe(net, dev, aux) {
2724 if (dev->group == group) {
2725 const struct rtnl_link_ops *ops;
2726
2727 ops = dev->rtnl_link_ops;
2728 ops->dellink(dev, &list_kill);
2729 }
2730 }
2731 unregister_netdevice_many(&list_kill);
2732
2733 return 0;
2734}
2735
Thomas Graf614732e2015-07-21 10:44:06 +02002736int rtnl_delete_link(struct net_device *dev)
2737{
2738 const struct rtnl_link_ops *ops;
2739 LIST_HEAD(list_kill);
2740
2741 ops = dev->rtnl_link_ops;
2742 if (!ops || !ops->dellink)
2743 return -EOPNOTSUPP;
2744
2745 ops->dellink(dev, &list_kill);
2746 unregister_netdevice_many(&list_kill);
2747
2748 return 0;
2749}
2750EXPORT_SYMBOL_GPL(rtnl_delete_link);
2751
David Ahernc21ef3e2017-04-16 09:48:24 -07002752static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
2753 struct netlink_ext_ack *extack)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002754{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002755 struct net *net = sock_net(skb->sk);
Christian Braunerb61ad682018-01-24 15:26:34 +01002756 struct net *tgt_net = net;
2757 struct net_device *dev = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002758 struct ifinfomsg *ifm;
2759 char ifname[IFNAMSIZ];
2760 struct nlattr *tb[IFLA_MAX+1];
2761 int err;
Christian Braunerb61ad682018-01-24 15:26:34 +01002762 int netnsid = -1;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002763
David Ahernc21ef3e2017-04-16 09:48:24 -07002764 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002765 if (err < 0)
2766 return err;
2767
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002768 err = rtnl_ensure_unique_netns(tb, extack, true);
2769 if (err < 0)
2770 return err;
2771
Patrick McHardy38f7b872007-06-13 12:03:51 -07002772 if (tb[IFLA_IFNAME])
2773 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2774
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002775 if (tb[IFLA_TARGET_NETNSID]) {
2776 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
Christian Braunerc383edc2018-09-04 21:53:47 +02002777 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
Christian Braunerb61ad682018-01-24 15:26:34 +01002778 if (IS_ERR(tgt_net))
2779 return PTR_ERR(tgt_net);
2780 }
2781
2782 err = -EINVAL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002783 ifm = nlmsg_data(nlh);
2784 if (ifm->ifi_index > 0)
Christian Braunerb61ad682018-01-24 15:26:34 +01002785 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002786 else if (tb[IFLA_IFNAME])
Christian Braunerb61ad682018-01-24 15:26:34 +01002787 dev = __dev_get_by_name(tgt_net, ifname);
WANG Cong66400d52015-03-24 11:53:31 -07002788 else if (tb[IFLA_GROUP])
Christian Braunerb61ad682018-01-24 15:26:34 +01002789 err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP]));
Patrick McHardy38f7b872007-06-13 12:03:51 -07002790 else
Christian Braunerb61ad682018-01-24 15:26:34 +01002791 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002792
Christian Braunerb61ad682018-01-24 15:26:34 +01002793 if (!dev) {
2794 if (tb[IFLA_IFNAME] || ifm->ifi_index > 0)
2795 err = -ENODEV;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002796
Christian Braunerb61ad682018-01-24 15:26:34 +01002797 goto out;
2798 }
2799
2800 err = rtnl_delete_link(dev);
2801
2802out:
2803 if (netnsid >= 0)
2804 put_net(tgt_net);
2805
2806 return err;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002807}
2808
Patrick McHardy3729d502010-02-26 06:34:54 +00002809int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
2810{
2811 unsigned int old_flags;
2812 int err;
2813
2814 old_flags = dev->flags;
2815 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
2816 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
2817 if (err < 0)
2818 return err;
2819 }
2820
Roopa Prabhu8d356b82018-07-04 16:46:29 -07002821 if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) {
2822 __dev_notify_flags(dev, old_flags, 0U);
2823 } else {
2824 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
2825 __dev_notify_flags(dev, old_flags, ~0U);
2826 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002827 return 0;
2828}
2829EXPORT_SYMBOL(rtnl_configure_link);
2830
Rami Rosenc0713562012-11-30 01:08:47 +00002831struct net_device *rtnl_create_link(struct net *net,
Thomas Graf78ebb0d2015-04-10 01:45:53 +02002832 const char *ifname, unsigned char name_assign_type,
Tom Gundersen55177502014-07-14 16:37:25 +02002833 const struct rtnl_link_ops *ops, struct nlattr *tb[])
Pavel Emelianove7199282007-08-08 22:16:38 -07002834{
Pavel Emelianove7199282007-08-08 22:16:38 -07002835 struct net_device *dev;
Jiri Pirkod40156a2012-07-20 02:28:47 +00002836 unsigned int num_tx_queues = 1;
2837 unsigned int num_rx_queues = 1;
Pavel Emelianove7199282007-08-08 22:16:38 -07002838
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002839 if (tb[IFLA_NUM_TX_QUEUES])
2840 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
2841 else if (ops->get_num_tx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002842 num_tx_queues = ops->get_num_tx_queues();
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002843
2844 if (tb[IFLA_NUM_RX_QUEUES])
2845 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
2846 else if (ops->get_num_rx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002847 num_rx_queues = ops->get_num_rx_queues();
stephen hemmingerefacb302012-04-10 18:34:43 +00002848
Tom Gundersen55177502014-07-14 16:37:25 +02002849 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
Tom Gundersenc835a672014-07-14 16:37:24 +02002850 ops->setup, num_tx_queues, num_rx_queues);
Pavel Emelianove7199282007-08-08 22:16:38 -07002851 if (!dev)
Tobias Klauserd1892e42017-02-20 16:32:06 +01002852 return ERR_PTR(-ENOMEM);
Pavel Emelianove7199282007-08-08 22:16:38 -07002853
Eric W. Biederman81adee42009-11-08 00:53:51 -08002854 dev_net_set(dev, net);
2855 dev->rtnl_link_ops = ops;
Patrick McHardy3729d502010-02-26 06:34:54 +00002856 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
Eric W. Biederman81adee42009-11-08 00:53:51 -08002857
Pavel Emelianove7199282007-08-08 22:16:38 -07002858 if (tb[IFLA_MTU])
2859 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002860 if (tb[IFLA_ADDRESS]) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002861 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
2862 nla_len(tb[IFLA_ADDRESS]));
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002863 dev->addr_assign_type = NET_ADDR_SET;
2864 }
Pavel Emelianove7199282007-08-08 22:16:38 -07002865 if (tb[IFLA_BROADCAST])
2866 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
2867 nla_len(tb[IFLA_BROADCAST]));
2868 if (tb[IFLA_TXQLEN])
2869 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
2870 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002871 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Pavel Emelianove7199282007-08-08 22:16:38 -07002872 if (tb[IFLA_LINKMODE])
2873 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
Patrick McHardyffa934f2011-01-20 03:00:42 +00002874 if (tb[IFLA_GROUP])
2875 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Stephen Hemminger46e6b992017-12-07 15:40:19 -08002876 if (tb[IFLA_GSO_MAX_SIZE])
2877 netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE]));
2878 if (tb[IFLA_GSO_MAX_SEGS])
Stephen Hemmingera0b586f2017-12-08 15:34:13 -08002879 dev->gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
Pavel Emelianove7199282007-08-08 22:16:38 -07002880
2881 return dev;
Pavel Emelianove7199282007-08-08 22:16:38 -07002882}
Eric Dumazete0d087a2009-11-07 01:26:17 -08002883EXPORT_SYMBOL(rtnl_create_link);
Pavel Emelianove7199282007-08-08 22:16:38 -07002884
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002885static int rtnl_group_changelink(const struct sk_buff *skb,
2886 struct net *net, int group,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002887 struct ifinfomsg *ifm,
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002888 struct netlink_ext_ack *extack,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002889 struct nlattr **tb)
2890{
WANG Congd0795352015-03-23 16:31:09 -07002891 struct net_device *dev, *aux;
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002892 int err;
2893
WANG Congd0795352015-03-23 16:31:09 -07002894 for_each_netdev_safe(net, dev, aux) {
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002895 if (dev->group == group) {
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002896 err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002897 if (err < 0)
2898 return err;
2899 }
2900 }
2901
2902 return 0;
2903}
2904
David Ahernc21ef3e2017-04-16 09:48:24 -07002905static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2906 struct netlink_ext_ack *extack)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002907{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002908 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002909 const struct rtnl_link_ops *ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002910 const struct rtnl_link_ops *m_ops = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002911 struct net_device *dev;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002912 struct net_device *master_dev = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002913 struct ifinfomsg *ifm;
2914 char kind[MODULE_NAME_LEN];
2915 char ifname[IFNAMSIZ];
2916 struct nlattr *tb[IFLA_MAX+1];
2917 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
Tom Gundersen55177502014-07-14 16:37:25 +02002918 unsigned char name_assign_type = NET_NAME_USER;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002919 int err;
2920
Johannes Berg95a5afc2008-10-16 15:24:51 -07002921#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002922replay:
Thomas Graf8072f082007-07-31 14:13:50 -07002923#endif
David Ahernc21ef3e2017-04-16 09:48:24 -07002924 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002925 if (err < 0)
2926 return err;
2927
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002928 err = rtnl_ensure_unique_netns(tb, extack, false);
2929 if (err < 0)
2930 return err;
2931
Patrick McHardy38f7b872007-06-13 12:03:51 -07002932 if (tb[IFLA_IFNAME])
2933 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2934 else
2935 ifname[0] = '\0';
2936
2937 ifm = nlmsg_data(nlh);
2938 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07002939 dev = __dev_get_by_index(net, ifm->ifi_index);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002940 else {
2941 if (ifname[0])
2942 dev = __dev_get_by_name(net, ifname);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002943 else
2944 dev = NULL;
2945 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002946
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002947 if (dev) {
2948 master_dev = netdev_master_upper_dev_get(dev);
2949 if (master_dev)
2950 m_ops = master_dev->rtnl_link_ops;
2951 }
2952
Eric Dumazete0d087a2009-11-07 01:26:17 -08002953 err = validate_linkmsg(dev, tb);
2954 if (err < 0)
Thomas Graf1840bb12008-02-23 19:54:36 -08002955 return err;
2956
Patrick McHardy38f7b872007-06-13 12:03:51 -07002957 if (tb[IFLA_LINKINFO]) {
2958 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +02002959 tb[IFLA_LINKINFO], ifla_info_policy,
2960 NULL);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002961 if (err < 0)
2962 return err;
2963 } else
2964 memset(linkinfo, 0, sizeof(linkinfo));
2965
2966 if (linkinfo[IFLA_INFO_KIND]) {
2967 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
2968 ops = rtnl_link_ops_get(kind);
2969 } else {
2970 kind[0] = '\0';
2971 ops = NULL;
2972 }
2973
2974 if (1) {
Kees Cookccf8dbc2018-05-30 15:20:52 -07002975 struct nlattr *attr[RTNL_MAX_TYPE + 1];
2976 struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1];
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002977 struct nlattr **data = NULL;
2978 struct nlattr **slave_data = NULL;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002979 struct net *dest_net, *link_net = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002980
2981 if (ops) {
Kees Cookccf8dbc2018-05-30 15:20:52 -07002982 if (ops->maxtype > RTNL_MAX_TYPE)
2983 return -EINVAL;
2984
Patrick McHardy38f7b872007-06-13 12:03:51 -07002985 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
2986 err = nla_parse_nested(attr, ops->maxtype,
2987 linkinfo[IFLA_INFO_DATA],
Johannes Bergfceb6432017-04-12 14:34:07 +02002988 ops->policy, NULL);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002989 if (err < 0)
2990 return err;
2991 data = attr;
2992 }
2993 if (ops->validate) {
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02002994 err = ops->validate(tb, data, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002995 if (err < 0)
2996 return err;
2997 }
2998 }
2999
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01003000 if (m_ops) {
Prashant Bholeaf066ed2018-06-01 17:16:58 +09003001 if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)
Kees Cookccf8dbc2018-05-30 15:20:52 -07003002 return -EINVAL;
3003
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01003004 if (m_ops->slave_maxtype &&
3005 linkinfo[IFLA_INFO_SLAVE_DATA]) {
3006 err = nla_parse_nested(slave_attr,
3007 m_ops->slave_maxtype,
3008 linkinfo[IFLA_INFO_SLAVE_DATA],
Johannes Bergfceb6432017-04-12 14:34:07 +02003009 m_ops->slave_policy,
3010 NULL);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01003011 if (err < 0)
3012 return err;
3013 slave_data = slave_attr;
3014 }
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01003015 }
3016
Patrick McHardy38f7b872007-06-13 12:03:51 -07003017 if (dev) {
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02003018 int status = 0;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003019
3020 if (nlh->nlmsg_flags & NLM_F_EXCL)
3021 return -EEXIST;
3022 if (nlh->nlmsg_flags & NLM_F_REPLACE)
3023 return -EOPNOTSUPP;
3024
3025 if (linkinfo[IFLA_INFO_DATA]) {
3026 if (!ops || ops != dev->rtnl_link_ops ||
3027 !ops->changelink)
3028 return -EOPNOTSUPP;
3029
Matthias Schifferad744b22017-06-25 23:56:00 +02003030 err = ops->changelink(dev, tb, data, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07003031 if (err < 0)
3032 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02003033 status |= DO_SETLINK_NOTIFY;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003034 }
3035
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01003036 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
3037 if (!m_ops || !m_ops->slave_changelink)
3038 return -EOPNOTSUPP;
3039
3040 err = m_ops->slave_changelink(master_dev, dev,
Matthias Schiffer17dd0ec2017-06-25 23:56:02 +02003041 tb, slave_data,
3042 extack);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01003043 if (err < 0)
3044 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02003045 status |= DO_SETLINK_NOTIFY;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01003046 }
3047
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07003048 return do_setlink(skb, dev, ifm, extack, tb, ifname,
3049 status);
Patrick McHardy38f7b872007-06-13 12:03:51 -07003050 }
3051
Patrick McHardyffa934f2011-01-20 03:00:42 +00003052 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
3053 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07003054 return rtnl_group_changelink(skb, net,
Patrick McHardyffa934f2011-01-20 03:00:42 +00003055 nla_get_u32(tb[IFLA_GROUP]),
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07003056 ifm, extack, tb);
Patrick McHardy38f7b872007-06-13 12:03:51 -07003057 return -ENODEV;
Patrick McHardyffa934f2011-01-20 03:00:42 +00003058 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07003059
Theuns Verwoerd160ca012017-01-31 12:23:46 +13003060 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
Patrick McHardy38f7b872007-06-13 12:03:51 -07003061 return -EOPNOTSUPP;
3062
3063 if (!ops) {
Johannes Berg95a5afc2008-10-16 15:24:51 -07003064#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07003065 if (kind[0]) {
3066 __rtnl_unlock();
3067 request_module("rtnl-link-%s", kind);
3068 rtnl_lock();
3069 ops = rtnl_link_ops_get(kind);
3070 if (ops)
3071 goto replay;
3072 }
3073#endif
3074 return -EOPNOTSUPP;
3075 }
3076
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02003077 if (!ops->setup)
3078 return -EOPNOTSUPP;
3079
Tom Gundersen55177502014-07-14 16:37:25 +02003080 if (!ifname[0]) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07003081 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
Tom Gundersen55177502014-07-14 16:37:25 +02003082 name_assign_type = NET_NAME_ENUM;
3083 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07003084
Christian Brauner5bb8ed02018-01-29 18:07:20 +01003085 dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN);
Eric W. Biederman13ad1772011-01-29 14:57:22 +00003086 if (IS_ERR(dest_net))
3087 return PTR_ERR(dest_net);
3088
Nicolas Dichtel317f4812015-01-15 15:11:18 +01003089 if (tb[IFLA_LINK_NETNSID]) {
3090 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
3091
3092 link_net = get_net_ns_by_id(dest_net, id);
3093 if (!link_net) {
3094 err = -EINVAL;
3095 goto out;
3096 }
Eric W. Biederman06615be2015-02-26 16:20:07 -06003097 err = -EPERM;
3098 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
3099 goto out;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01003100 }
3101
3102 dev = rtnl_create_link(link_net ? : dest_net, ifname,
3103 name_assign_type, ops, tb);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00003104 if (IS_ERR(dev)) {
Pavel Emelianove7199282007-08-08 22:16:38 -07003105 err = PTR_ERR(dev);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00003106 goto out;
3107 }
3108
3109 dev->ifindex = ifm->ifi_index;
3110
Cong Wang0e0eee22014-02-11 15:51:30 -08003111 if (ops->newlink) {
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02003112 err = ops->newlink(link_net ? : net, dev, tb, data,
3113 extack);
Cong Wang0e0eee22014-02-11 15:51:30 -08003114 /* Drivers should call free_netdev() in ->destructor
Cong Wange51fb152014-06-03 16:40:47 -07003115 * and unregister it on failure after registration
3116 * so that device could be finally freed in rtnl_unlock.
Cong Wang0e0eee22014-02-11 15:51:30 -08003117 */
Cong Wange51fb152014-06-03 16:40:47 -07003118 if (err < 0) {
3119 /* If device is not registered at all, free it now */
3120 if (dev->reg_state == NETREG_UNINITIALIZED)
3121 free_netdev(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08003122 goto out;
Cong Wange51fb152014-06-03 16:40:47 -07003123 }
Cong Wang0e0eee22014-02-11 15:51:30 -08003124 } else {
Patrick McHardy2d85cba2007-07-11 19:42:13 -07003125 err = register_netdevice(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08003126 if (err < 0) {
3127 free_netdev(dev);
3128 goto out;
3129 }
Dan Carpenterfce9b9b2013-08-14 12:35:42 +03003130 }
Patrick McHardy3729d502010-02-26 06:34:54 +00003131 err = rtnl_configure_link(dev, ifm);
David S. Miller43638902015-03-10 21:58:32 -04003132 if (err < 0)
3133 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01003134 if (link_net) {
Nicolas Dichtel317f4812015-01-15 15:11:18 +01003135 err = dev_change_net_namespace(dev, dest_net, ifname);
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01003136 if (err < 0)
David S. Miller43638902015-03-10 21:58:32 -04003137 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01003138 }
Theuns Verwoerd160ca012017-01-31 12:23:46 +13003139 if (tb[IFLA_MASTER]) {
David Ahern33eaf2a2017-10-04 17:48:46 -07003140 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]),
3141 extack);
Theuns Verwoerd160ca012017-01-31 12:23:46 +13003142 if (err)
3143 goto out_unregister;
3144 }
Patrick McHardy3729d502010-02-26 06:34:54 +00003145out:
Nicolas Dichtel317f4812015-01-15 15:11:18 +01003146 if (link_net)
3147 put_net(link_net);
Eric W. Biederman81adee42009-11-08 00:53:51 -08003148 put_net(dest_net);
Patrick McHardy38f7b872007-06-13 12:03:51 -07003149 return err;
David S. Miller43638902015-03-10 21:58:32 -04003150out_unregister:
3151 if (ops->newlink) {
3152 LIST_HEAD(list_kill);
3153
3154 ops->dellink(dev, &list_kill);
3155 unregister_netdevice_many(&list_kill);
3156 } else {
3157 unregister_netdevice(dev);
3158 }
3159 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003160 }
3161}
3162
David Ahernc21ef3e2017-04-16 09:48:24 -07003163static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3164 struct netlink_ext_ack *extack)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003165{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003166 struct net *net = sock_net(skb->sk);
Jiri Benc79e1ad12017-11-02 17:04:38 -02003167 struct net *tgt_net = net;
Thomas Grafb60c5112006-08-04 23:05:34 -07003168 struct ifinfomsg *ifm;
Eric Dumazeta3d12892009-10-21 10:59:31 +00003169 char ifname[IFNAMSIZ];
Thomas Grafb60c5112006-08-04 23:05:34 -07003170 struct nlattr *tb[IFLA_MAX+1];
3171 struct net_device *dev = NULL;
3172 struct sk_buff *nskb;
Jiri Benc79e1ad12017-11-02 17:04:38 -02003173 int netnsid = -1;
Thomas Graf339bf982006-11-10 14:10:15 -08003174 int err;
Greg Rose115c9b82012-02-21 16:54:48 -05003175 u32 ext_filter_mask = 0;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003176
David Ahernc21ef3e2017-04-16 09:48:24 -07003177 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Thomas Grafb60c5112006-08-04 23:05:34 -07003178 if (err < 0)
Eric Sesterhenn9918f232006-09-26 23:26:38 -07003179 return err;
Thomas Grafb60c5112006-08-04 23:05:34 -07003180
Christian Brauner4ff66ca2018-02-07 13:53:20 +01003181 err = rtnl_ensure_unique_netns(tb, extack, true);
3182 if (err < 0)
3183 return err;
3184
Christian Brauner7e4a8d52018-09-04 21:53:53 +02003185 if (tb[IFLA_TARGET_NETNSID]) {
3186 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
Christian Braunerc383edc2018-09-04 21:53:47 +02003187 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
Jiri Benc79e1ad12017-11-02 17:04:38 -02003188 if (IS_ERR(tgt_net))
3189 return PTR_ERR(tgt_net);
3190 }
3191
Eric Dumazeta3d12892009-10-21 10:59:31 +00003192 if (tb[IFLA_IFNAME])
3193 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
3194
Greg Rose115c9b82012-02-21 16:54:48 -05003195 if (tb[IFLA_EXT_MASK])
3196 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3197
Jiri Benc79e1ad12017-11-02 17:04:38 -02003198 err = -EINVAL;
Thomas Grafb60c5112006-08-04 23:05:34 -07003199 ifm = nlmsg_data(nlh);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003200 if (ifm->ifi_index > 0)
Jiri Benc79e1ad12017-11-02 17:04:38 -02003201 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003202 else if (tb[IFLA_IFNAME])
Jiri Benc79e1ad12017-11-02 17:04:38 -02003203 dev = __dev_get_by_name(tgt_net, ifname);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003204 else
Jiri Benc79e1ad12017-11-02 17:04:38 -02003205 goto out;
Thomas Grafb60c5112006-08-04 23:05:34 -07003206
Jiri Benc79e1ad12017-11-02 17:04:38 -02003207 err = -ENODEV;
Eric Dumazeta3d12892009-10-21 10:59:31 +00003208 if (dev == NULL)
Jiri Benc79e1ad12017-11-02 17:04:38 -02003209 goto out;
Eric Dumazeta3d12892009-10-21 10:59:31 +00003210
Jiri Benc79e1ad12017-11-02 17:04:38 -02003211 err = -ENOBUFS;
Greg Rose115c9b82012-02-21 16:54:48 -05003212 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003213 if (nskb == NULL)
Jiri Benc79e1ad12017-11-02 17:04:38 -02003214 goto out;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003215
Jiri Benc79e1ad12017-11-02 17:04:38 -02003216 err = rtnl_fill_ifinfo(nskb, dev, net,
3217 RTM_NEWLINK, NETLINK_CB(skb).portid,
3218 nlh->nlmsg_seq, 0, 0, ext_filter_mask,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003219 0, NULL, 0, netnsid);
Patrick McHardy26932562007-01-31 23:16:40 -08003220 if (err < 0) {
3221 /* -EMSGSIZE implies BUG in if_nlmsg_size */
3222 WARN_ON(err == -EMSGSIZE);
3223 kfree_skb(nskb);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003224 } else
Eric W. Biederman15e47302012-09-07 20:12:54 +00003225 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
Jiri Benc79e1ad12017-11-02 17:04:38 -02003226out:
3227 if (netnsid >= 0)
3228 put_net(tgt_net);
Thomas Grafb60c5112006-08-04 23:05:34 -07003229
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003230 return err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003231}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003232
Greg Rose115c9b82012-02-21 16:54:48 -05003233static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
Greg Rosec7ac8672011-06-10 01:27:09 +00003234{
Greg Rose115c9b82012-02-21 16:54:48 -05003235 struct net *net = sock_net(skb->sk);
3236 struct net_device *dev;
3237 struct nlattr *tb[IFLA_MAX+1];
3238 u32 ext_filter_mask = 0;
3239 u16 min_ifinfo_dump_size = 0;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02003240 int hdrlen;
Greg Rose115c9b82012-02-21 16:54:48 -05003241
Michal Schmidte5eca6d2014-05-28 14:15:19 +02003242 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3243 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
3244 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
3245
Johannes Bergfceb6432017-04-12 14:34:07 +02003246 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00003247 if (tb[IFLA_EXT_MASK])
3248 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3249 }
Greg Rose115c9b82012-02-21 16:54:48 -05003250
3251 if (!ext_filter_mask)
3252 return NLMSG_GOODSIZE;
3253 /*
3254 * traverse the list of net devices and compute the minimum
3255 * buffer size based upon the filter mask.
3256 */
Florian Westphal6853dd42017-08-09 20:41:51 +02003257 rcu_read_lock();
3258 for_each_netdev_rcu(net, dev) {
Greg Rose115c9b82012-02-21 16:54:48 -05003259 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
3260 if_nlmsg_size(dev,
3261 ext_filter_mask));
3262 }
Florian Westphal6853dd42017-08-09 20:41:51 +02003263 rcu_read_unlock();
Greg Rose115c9b82012-02-21 16:54:48 -05003264
Zhang Shengju93af2052016-11-22 14:14:28 +08003265 return nlmsg_total_size(min_ifinfo_dump_size);
Greg Rosec7ac8672011-06-10 01:27:09 +00003266}
3267
Adrian Bunk42bad1d2007-04-26 00:57:41 -07003268static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269{
3270 int idx;
3271 int s_idx = cb->family;
Christian Brauner87ccbb12018-09-04 21:53:51 +02003272 int type = cb->nlh->nlmsg_type - RTM_BASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273
3274 if (s_idx == 0)
3275 s_idx = 1;
Florian Westphal6853dd42017-08-09 20:41:51 +02003276
Patrick McHardy25239ce2010-04-26 16:02:05 +02003277 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
Florian Westphaladdf9b92017-12-02 21:44:05 +01003278 struct rtnl_link **tab;
Florian Westphaladdf9b92017-12-02 21:44:05 +01003279 struct rtnl_link *link;
Florian Westphal6853dd42017-08-09 20:41:51 +02003280 rtnl_dumpit_func dumpit;
3281
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 if (idx < s_idx || idx == PF_PACKET)
3283 continue;
Florian Westphal6853dd42017-08-09 20:41:51 +02003284
Florian Westphaladdf9b92017-12-02 21:44:05 +01003285 if (type < 0 || type >= RTM_NR_MSGTYPES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 continue;
Florian Westphal6853dd42017-08-09 20:41:51 +02003287
Florian Westphaladdf9b92017-12-02 21:44:05 +01003288 tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]);
3289 if (!tab)
3290 continue;
3291
3292 link = tab[type];
3293 if (!link)
3294 continue;
3295
3296 dumpit = link->dumpit;
Florian Westphal6853dd42017-08-09 20:41:51 +02003297 if (!dumpit)
3298 continue;
3299
Nicolas Dichtel04652772013-03-22 06:28:42 +00003300 if (idx > s_idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 memset(&cb->args[0], 0, sizeof(cb->args));
Nicolas Dichtel04652772013-03-22 06:28:42 +00003302 cb->prev_seq = 0;
3303 cb->seq = 0;
3304 }
Florian Westphal6853dd42017-08-09 20:41:51 +02003305 if (dumpit(skb, cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 break;
3307 }
3308 cb->family = idx;
3309
3310 return skb->len;
3311}
3312
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003313struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003314 unsigned int change,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003315 u32 event, gfp_t flags, int *new_nsid,
3316 int new_ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003318 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 struct sk_buff *skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003320 int err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00003321 size_t if_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322
Alexei Starovoitov7f294052013-10-23 16:02:42 -07003323 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003324 if (skb == NULL)
3325 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326
Jiri Benc79e1ad12017-11-02 17:04:38 -02003327 err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
3328 type, 0, 0, change, 0, 0, event,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003329 new_nsid, new_ifindex, -1);
Patrick McHardy26932562007-01-31 23:16:40 -08003330 if (err < 0) {
3331 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
3332 WARN_ON(err == -EMSGSIZE);
3333 kfree_skb(skb);
3334 goto errout;
3335 }
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003336 return skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003337errout:
3338 if (err < 0)
Eric W. Biederman4b3da702007-11-19 22:27:40 -08003339 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003340 return NULL;
3341}
3342
3343void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
3344{
3345 struct net *net = dev_net(dev);
3346
3347 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
3348}
3349
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003350static void rtmsg_ifinfo_event(int type, struct net_device *dev,
3351 unsigned int change, u32 event,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003352 gfp_t flags, int *new_nsid, int new_ifindex)
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003353{
3354 struct sk_buff *skb;
3355
Nicolas Dichteled2a80a2015-05-13 14:19:42 +02003356 if (dev->reg_state != NETREG_REGISTERED)
3357 return;
3358
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003359 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid,
3360 new_ifindex);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003361 if (skb)
3362 rtmsg_ifinfo_send(skb, dev, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363}
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003364
3365void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
3366 gfp_t flags)
3367{
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003368 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3369 NULL, 0);
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003370}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003372void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003373 gfp_t flags, int *new_nsid, int new_ifindex)
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003374{
3375 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003376 new_nsid, new_ifindex);
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003377}
3378
John Fastabendd83b0602012-04-15 06:44:08 +00003379static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
3380 struct net_device *dev,
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003381 u8 *addr, u16 vid, u32 pid, u32 seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003382 int type, unsigned int flags,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003383 int nlflags, u16 ndm_state)
John Fastabendd83b0602012-04-15 06:44:08 +00003384{
3385 struct nlmsghdr *nlh;
3386 struct ndmsg *ndm;
3387
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003388 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
John Fastabendd83b0602012-04-15 06:44:08 +00003389 if (!nlh)
3390 return -EMSGSIZE;
3391
3392 ndm = nlmsg_data(nlh);
3393 ndm->ndm_family = AF_BRIDGE;
3394 ndm->ndm_pad1 = 0;
3395 ndm->ndm_pad2 = 0;
3396 ndm->ndm_flags = flags;
3397 ndm->ndm_type = 0;
3398 ndm->ndm_ifindex = dev->ifindex;
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003399 ndm->ndm_state = ndm_state;
John Fastabendd83b0602012-04-15 06:44:08 +00003400
3401 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
3402 goto nla_put_failure;
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003403 if (vid)
3404 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
3405 goto nla_put_failure;
John Fastabendd83b0602012-04-15 06:44:08 +00003406
Johannes Berg053c0952015-01-16 22:09:00 +01003407 nlmsg_end(skb, nlh);
3408 return 0;
John Fastabendd83b0602012-04-15 06:44:08 +00003409
3410nla_put_failure:
3411 nlmsg_cancel(skb, nlh);
3412 return -EMSGSIZE;
3413}
3414
John Fastabend3ff661c2012-04-15 06:44:14 +00003415static inline size_t rtnl_fdb_nlmsg_size(void)
3416{
Sabrina Dubrocaf82ef3e2016-11-18 15:50:39 +01003417 return NLMSG_ALIGN(sizeof(struct ndmsg)) +
3418 nla_total_size(ETH_ALEN) + /* NDA_LLADDR */
3419 nla_total_size(sizeof(u16)) + /* NDA_VLAN */
3420 0;
John Fastabend3ff661c2012-04-15 06:44:14 +00003421}
3422
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003423static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
3424 u16 ndm_state)
John Fastabend3ff661c2012-04-15 06:44:14 +00003425{
3426 struct net *net = dev_net(dev);
3427 struct sk_buff *skb;
3428 int err = -ENOBUFS;
3429
3430 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
3431 if (!skb)
3432 goto errout;
3433
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003434 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003435 0, 0, type, NTF_SELF, 0, ndm_state);
John Fastabend3ff661c2012-04-15 06:44:14 +00003436 if (err < 0) {
3437 kfree_skb(skb);
3438 goto errout;
3439 }
3440
3441 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
3442 return;
3443errout:
3444 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
3445}
3446
Vlad Yasevich090096b2013-03-06 15:39:42 +00003447/**
3448 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3449 */
3450int ndo_dflt_fdb_add(struct ndmsg *ndm,
3451 struct nlattr *tb[],
3452 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003453 const unsigned char *addr, u16 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003454 u16 flags)
3455{
3456 int err = -EINVAL;
3457
3458 /* If aging addresses are supported device will need to
3459 * implement its own handler for this.
3460 */
3461 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
3462 pr_info("%s: FDB only supports static addresses\n", dev->name);
3463 return err;
3464 }
3465
Or Gerlitz65891fe2014-12-14 18:19:05 +02003466 if (vid) {
3467 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
3468 return err;
3469 }
3470
Vlad Yasevich090096b2013-03-06 15:39:42 +00003471 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3472 err = dev_uc_add_excl(dev, addr);
3473 else if (is_multicast_ether_addr(addr))
3474 err = dev_mc_add_excl(dev, addr);
3475
3476 /* Only return duplicate errors if NLM_F_EXCL is set */
3477 if (err == -EEXIST && !(flags & NLM_F_EXCL))
3478 err = 0;
3479
3480 return err;
3481}
3482EXPORT_SYMBOL(ndo_dflt_fdb_add);
3483
Florian Westphalb88d12e2017-10-10 17:10:04 +02003484static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
3485 struct netlink_ext_ack *extack)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003486{
3487 u16 vid = 0;
3488
3489 if (vlan_attr) {
3490 if (nla_len(vlan_attr) != sizeof(u16)) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003491 NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
Jiri Pirkof6f64242014-11-28 14:34:15 +01003492 return -EINVAL;
3493 }
3494
3495 vid = nla_get_u16(vlan_attr);
3496
3497 if (!vid || vid >= VLAN_VID_MASK) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003498 NL_SET_ERR_MSG(extack, "invalid vlan id");
Jiri Pirkof6f64242014-11-28 14:34:15 +01003499 return -EINVAL;
3500 }
3501 }
3502 *p_vid = vid;
3503 return 0;
3504}
3505
David Ahernc21ef3e2017-04-16 09:48:24 -07003506static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
3507 struct netlink_ext_ack *extack)
John Fastabend77162022012-04-15 06:43:56 +00003508{
3509 struct net *net = sock_net(skb->sk);
John Fastabend77162022012-04-15 06:43:56 +00003510 struct ndmsg *ndm;
3511 struct nlattr *tb[NDA_MAX+1];
3512 struct net_device *dev;
3513 u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01003514 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00003515 int err;
3516
David Ahernc21ef3e2017-04-16 09:48:24 -07003517 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
John Fastabend77162022012-04-15 06:43:56 +00003518 if (err < 0)
3519 return err;
3520
3521 ndm = nlmsg_data(nlh);
3522 if (ndm->ndm_ifindex == 0) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003523 NL_SET_ERR_MSG(extack, "invalid ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003524 return -EINVAL;
3525 }
3526
3527 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3528 if (dev == NULL) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003529 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003530 return -ENODEV;
3531 }
3532
3533 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003534 NL_SET_ERR_MSG(extack, "invalid address");
John Fastabend77162022012-04-15 06:43:56 +00003535 return -EINVAL;
3536 }
3537
3538 addr = nla_data(tb[NDA_LLADDR]);
John Fastabend77162022012-04-15 06:43:56 +00003539
Florian Westphalb88d12e2017-10-10 17:10:04 +02003540 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
Jiri Pirkof6f64242014-11-28 14:34:15 +01003541 if (err)
3542 return err;
3543
John Fastabend77162022012-04-15 06:43:56 +00003544 err = -EOPNOTSUPP;
3545
3546 /* Support fdb on master device the net/bridge default case */
3547 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3548 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003549 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3550 const struct net_device_ops *ops = br_dev->netdev_ops;
3551
Jiri Pirkof6f64242014-11-28 14:34:15 +01003552 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
3553 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00003554 if (err)
3555 goto out;
3556 else
3557 ndm->ndm_flags &= ~NTF_MASTER;
3558 }
3559
3560 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00003561 if ((ndm->ndm_flags & NTF_SELF)) {
3562 if (dev->netdev_ops->ndo_fdb_add)
3563 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003564 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003565 nlh->nlmsg_flags);
3566 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01003567 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003568 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00003569
John Fastabend3ff661c2012-04-15 06:44:14 +00003570 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003571 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
3572 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00003573 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00003574 }
John Fastabend77162022012-04-15 06:43:56 +00003575 }
3576out:
3577 return err;
3578}
3579
Vlad Yasevich090096b2013-03-06 15:39:42 +00003580/**
3581 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3582 */
3583int ndo_dflt_fdb_del(struct ndmsg *ndm,
3584 struct nlattr *tb[],
3585 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003586 const unsigned char *addr, u16 vid)
Vlad Yasevich090096b2013-03-06 15:39:42 +00003587{
Alexander Duyckc8a89c42014-07-15 15:15:20 -07003588 int err = -EINVAL;
Vlad Yasevich090096b2013-03-06 15:39:42 +00003589
3590 /* If aging addresses are supported device will need to
3591 * implement its own handler for this.
3592 */
Sridhar Samudrala64535992013-08-08 15:19:48 -07003593 if (!(ndm->ndm_state & NUD_PERMANENT)) {
Vlad Yasevich090096b2013-03-06 15:39:42 +00003594 pr_info("%s: FDB only supports static addresses\n", dev->name);
Alexander Duyckc8a89c42014-07-15 15:15:20 -07003595 return err;
Vlad Yasevich090096b2013-03-06 15:39:42 +00003596 }
3597
3598 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3599 err = dev_uc_del(dev, addr);
3600 else if (is_multicast_ether_addr(addr))
3601 err = dev_mc_del(dev, addr);
Vlad Yasevich090096b2013-03-06 15:39:42 +00003602
3603 return err;
3604}
3605EXPORT_SYMBOL(ndo_dflt_fdb_del);
3606
David Ahernc21ef3e2017-04-16 09:48:24 -07003607static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
3608 struct netlink_ext_ack *extack)
John Fastabend77162022012-04-15 06:43:56 +00003609{
3610 struct net *net = sock_net(skb->sk);
3611 struct ndmsg *ndm;
Vlad Yasevich1690be62013-02-13 12:00:18 +00003612 struct nlattr *tb[NDA_MAX+1];
John Fastabend77162022012-04-15 06:43:56 +00003613 struct net_device *dev;
3614 int err = -EINVAL;
3615 __u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01003616 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00003617
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07003618 if (!netlink_capable(skb, CAP_NET_ADMIN))
Vlad Yasevich1690be62013-02-13 12:00:18 +00003619 return -EPERM;
3620
David Ahernc21ef3e2017-04-16 09:48:24 -07003621 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
Vlad Yasevich1690be62013-02-13 12:00:18 +00003622 if (err < 0)
3623 return err;
John Fastabend77162022012-04-15 06:43:56 +00003624
3625 ndm = nlmsg_data(nlh);
3626 if (ndm->ndm_ifindex == 0) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003627 NL_SET_ERR_MSG(extack, "invalid ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003628 return -EINVAL;
3629 }
3630
3631 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3632 if (dev == NULL) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003633 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003634 return -ENODEV;
3635 }
3636
Vlad Yasevich1690be62013-02-13 12:00:18 +00003637 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003638 NL_SET_ERR_MSG(extack, "invalid address");
John Fastabend77162022012-04-15 06:43:56 +00003639 return -EINVAL;
3640 }
3641
Vlad Yasevich1690be62013-02-13 12:00:18 +00003642 addr = nla_data(tb[NDA_LLADDR]);
Vlad Yasevich1690be62013-02-13 12:00:18 +00003643
Florian Westphalb88d12e2017-10-10 17:10:04 +02003644 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
Jiri Pirkof6f64242014-11-28 14:34:15 +01003645 if (err)
3646 return err;
3647
John Fastabend77162022012-04-15 06:43:56 +00003648 err = -EOPNOTSUPP;
3649
3650 /* Support fdb on master device the net/bridge default case */
3651 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3652 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003653 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3654 const struct net_device_ops *ops = br_dev->netdev_ops;
John Fastabend77162022012-04-15 06:43:56 +00003655
Jiri Pirko898e5062013-01-03 22:48:52 +00003656 if (ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003657 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00003658
3659 if (err)
3660 goto out;
3661 else
3662 ndm->ndm_flags &= ~NTF_MASTER;
3663 }
3664
3665 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00003666 if (ndm->ndm_flags & NTF_SELF) {
3667 if (dev->netdev_ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003668 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
3669 vid);
Vlad Yasevich090096b2013-03-06 15:39:42 +00003670 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01003671 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00003672
John Fastabend3ff661c2012-04-15 06:44:14 +00003673 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003674 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
3675 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00003676 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00003677 }
John Fastabend77162022012-04-15 06:43:56 +00003678 }
3679out:
3680 return err;
3681}
3682
John Fastabendd83b0602012-04-15 06:44:08 +00003683static int nlmsg_populate_fdb(struct sk_buff *skb,
3684 struct netlink_callback *cb,
3685 struct net_device *dev,
3686 int *idx,
3687 struct netdev_hw_addr_list *list)
3688{
3689 struct netdev_hw_addr *ha;
3690 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00003691 u32 portid, seq;
John Fastabendd83b0602012-04-15 06:44:08 +00003692
Eric W. Biederman15e47302012-09-07 20:12:54 +00003693 portid = NETLINK_CB(cb->skb).portid;
John Fastabendd83b0602012-04-15 06:44:08 +00003694 seq = cb->nlh->nlmsg_seq;
3695
3696 list_for_each_entry(ha, &list->list, list) {
Roopa Prabhud2976532016-08-30 21:56:45 -07003697 if (*idx < cb->args[2])
John Fastabendd83b0602012-04-15 06:44:08 +00003698 goto skip;
3699
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003700 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
John Fastabenda7a558f2012-11-01 16:23:10 +00003701 portid, seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003702 RTM_NEWNEIGH, NTF_SELF,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003703 NLM_F_MULTI, NUD_PERMANENT);
John Fastabendd83b0602012-04-15 06:44:08 +00003704 if (err < 0)
3705 return err;
3706skip:
3707 *idx += 1;
3708 }
3709 return 0;
3710}
3711
3712/**
Ben Hutchings2c530402012-07-10 10:55:09 +00003713 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
John Fastabendd83b0602012-04-15 06:44:08 +00003714 * @nlh: netlink message header
3715 * @dev: netdevice
3716 *
3717 * Default netdevice operation to dump the existing unicast address list.
John Fastabend91f3e7b2013-03-29 08:18:37 +00003718 * Returns number of addresses from list put in skb.
John Fastabendd83b0602012-04-15 06:44:08 +00003719 */
3720int ndo_dflt_fdb_dump(struct sk_buff *skb,
3721 struct netlink_callback *cb,
3722 struct net_device *dev,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04003723 struct net_device *filter_dev,
Roopa Prabhud2976532016-08-30 21:56:45 -07003724 int *idx)
John Fastabendd83b0602012-04-15 06:44:08 +00003725{
3726 int err;
3727
3728 netif_addr_lock_bh(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -07003729 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
John Fastabendd83b0602012-04-15 06:44:08 +00003730 if (err)
3731 goto out;
Zhang Shengju2934c9d2016-11-30 16:37:34 +08003732 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
John Fastabendd83b0602012-04-15 06:44:08 +00003733out:
3734 netif_addr_unlock_bh(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -07003735 return err;
John Fastabendd83b0602012-04-15 06:44:08 +00003736}
3737EXPORT_SYMBOL(ndo_dflt_fdb_dump);
3738
John Fastabend77162022012-04-15 06:43:56 +00003739static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
3740{
John Fastabend77162022012-04-15 06:43:56 +00003741 struct net_device *dev;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003742 struct nlattr *tb[IFLA_MAX+1];
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003743 struct net_device *br_dev = NULL;
3744 const struct net_device_ops *ops = NULL;
3745 const struct net_device_ops *cops = NULL;
3746 struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
3747 struct net *net = sock_net(skb->sk);
Roopa Prabhud2976532016-08-30 21:56:45 -07003748 struct hlist_head *head;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003749 int brport_idx = 0;
3750 int br_idx = 0;
Roopa Prabhud2976532016-08-30 21:56:45 -07003751 int h, s_h;
3752 int idx = 0, s_idx;
3753 int err = 0;
3754 int fidx = 0;
John Fastabend77162022012-04-15 06:43:56 +00003755
Alexander Potapenko0ff50e82017-05-23 13:20:28 +02003756 err = nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb,
3757 IFLA_MAX, ifla_policy, NULL);
3758 if (err < 0) {
3759 return -EINVAL;
3760 } else if (err == 0) {
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003761 if (tb[IFLA_MASTER])
3762 br_idx = nla_get_u32(tb[IFLA_MASTER]);
3763 }
John Fastabend77162022012-04-15 06:43:56 +00003764
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003765 brport_idx = ifm->ifi_index;
3766
3767 if (br_idx) {
3768 br_dev = __dev_get_by_index(net, br_idx);
3769 if (!br_dev)
3770 return -ENODEV;
3771
3772 ops = br_dev->netdev_ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003773 }
3774
Roopa Prabhud2976532016-08-30 21:56:45 -07003775 s_h = cb->args[0];
3776 s_idx = cb->args[1];
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003777
Roopa Prabhud2976532016-08-30 21:56:45 -07003778 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
3779 idx = 0;
3780 head = &net->dev_index_head[h];
3781 hlist_for_each_entry(dev, head, index_hlist) {
3782
3783 if (brport_idx && (dev->ifindex != brport_idx))
3784 continue;
3785
3786 if (!br_idx) { /* user did not specify a specific bridge */
3787 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3788 br_dev = netdev_master_upper_dev_get(dev);
3789 cops = br_dev->netdev_ops;
3790 }
3791 } else {
3792 if (dev != br_dev &&
3793 !(dev->priv_flags & IFF_BRIDGE_PORT))
3794 continue;
3795
3796 if (br_dev != netdev_master_upper_dev_get(dev) &&
3797 !(dev->priv_flags & IFF_EBRIDGE))
3798 continue;
3799 cops = ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003800 }
3801
Roopa Prabhud2976532016-08-30 21:56:45 -07003802 if (idx < s_idx)
3803 goto cont;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003804
Roopa Prabhud2976532016-08-30 21:56:45 -07003805 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3806 if (cops && cops->ndo_fdb_dump) {
3807 err = cops->ndo_fdb_dump(skb, cb,
3808 br_dev, dev,
3809 &fidx);
3810 if (err == -EMSGSIZE)
3811 goto out;
3812 }
3813 }
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003814
Roopa Prabhud2976532016-08-30 21:56:45 -07003815 if (dev->netdev_ops->ndo_fdb_dump)
3816 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
3817 dev, NULL,
3818 &fidx);
3819 else
3820 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
3821 &fidx);
3822 if (err == -EMSGSIZE)
3823 goto out;
3824
3825 cops = NULL;
3826
3827 /* reset fdb offset to 0 for rest of the interfaces */
3828 cb->args[2] = 0;
3829 fidx = 0;
3830cont:
3831 idx++;
John Fastabend77162022012-04-15 06:43:56 +00003832 }
John Fastabend77162022012-04-15 06:43:56 +00003833 }
John Fastabend77162022012-04-15 06:43:56 +00003834
Roopa Prabhud2976532016-08-30 21:56:45 -07003835out:
3836 cb->args[0] = h;
3837 cb->args[1] = idx;
3838 cb->args[2] = fidx;
3839
John Fastabend77162022012-04-15 06:43:56 +00003840 return skb->len;
3841}
3842
Scott Feldman2c3c0312014-11-28 14:34:25 +01003843static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
3844 unsigned int attrnum, unsigned int flag)
3845{
3846 if (mask & flag)
3847 return nla_put_u8(skb, attrnum, !!(flags & flag));
3848 return 0;
3849}
3850
John Fastabend815cccb2012-10-24 08:13:09 +00003851int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Scott Feldman2c3c0312014-11-28 14:34:25 +01003852 struct net_device *dev, u16 mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003853 u32 flags, u32 mask, int nlflags,
3854 u32 filter_mask,
3855 int (*vlan_fill)(struct sk_buff *skb,
3856 struct net_device *dev,
3857 u32 filter_mask))
John Fastabend815cccb2012-10-24 08:13:09 +00003858{
3859 struct nlmsghdr *nlh;
3860 struct ifinfomsg *ifm;
3861 struct nlattr *br_afspec;
Scott Feldman2c3c0312014-11-28 14:34:25 +01003862 struct nlattr *protinfo;
John Fastabend815cccb2012-10-24 08:13:09 +00003863 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Jiri Pirko898e5062013-01-03 22:48:52 +00003864 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003865 int err = 0;
John Fastabend815cccb2012-10-24 08:13:09 +00003866
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02003867 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
John Fastabend815cccb2012-10-24 08:13:09 +00003868 if (nlh == NULL)
3869 return -EMSGSIZE;
3870
3871 ifm = nlmsg_data(nlh);
3872 ifm->ifi_family = AF_BRIDGE;
3873 ifm->__ifi_pad = 0;
3874 ifm->ifi_type = dev->type;
3875 ifm->ifi_index = dev->ifindex;
3876 ifm->ifi_flags = dev_get_flags(dev);
3877 ifm->ifi_change = 0;
3878
3879
3880 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
3881 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
3882 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00003883 (br_dev &&
3884 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
John Fastabend815cccb2012-10-24 08:13:09 +00003885 (dev->addr_len &&
3886 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02003887 (dev->ifindex != dev_get_iflink(dev) &&
3888 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
John Fastabend815cccb2012-10-24 08:13:09 +00003889 goto nla_put_failure;
3890
3891 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
3892 if (!br_afspec)
3893 goto nla_put_failure;
3894
Roopa Prabhu1d460b92014-12-08 14:04:20 -08003895 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
John Fastabend815cccb2012-10-24 08:13:09 +00003896 nla_nest_cancel(skb, br_afspec);
3897 goto nla_put_failure;
3898 }
Roopa Prabhu1d460b92014-12-08 14:04:20 -08003899
3900 if (mode != BRIDGE_MODE_UNDEF) {
3901 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
3902 nla_nest_cancel(skb, br_afspec);
3903 goto nla_put_failure;
3904 }
3905 }
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003906 if (vlan_fill) {
3907 err = vlan_fill(skb, dev, filter_mask);
3908 if (err) {
3909 nla_nest_cancel(skb, br_afspec);
3910 goto nla_put_failure;
3911 }
3912 }
John Fastabend815cccb2012-10-24 08:13:09 +00003913 nla_nest_end(skb, br_afspec);
3914
Scott Feldman2c3c0312014-11-28 14:34:25 +01003915 protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
3916 if (!protinfo)
3917 goto nla_put_failure;
3918
3919 if (brport_nla_put_flag(skb, flags, mask,
3920 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
3921 brport_nla_put_flag(skb, flags, mask,
3922 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
3923 brport_nla_put_flag(skb, flags, mask,
3924 IFLA_BRPORT_FAST_LEAVE,
3925 BR_MULTICAST_FAST_LEAVE) ||
3926 brport_nla_put_flag(skb, flags, mask,
3927 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
3928 brport_nla_put_flag(skb, flags, mask,
3929 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
3930 brport_nla_put_flag(skb, flags, mask,
3931 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
3932 brport_nla_put_flag(skb, flags, mask,
3933 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
3934 brport_nla_put_flag(skb, flags, mask,
3935 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
3936 nla_nest_cancel(skb, protinfo);
3937 goto nla_put_failure;
3938 }
3939
3940 nla_nest_end(skb, protinfo);
3941
Johannes Berg053c0952015-01-16 22:09:00 +01003942 nlmsg_end(skb, nlh);
3943 return 0;
John Fastabend815cccb2012-10-24 08:13:09 +00003944nla_put_failure:
3945 nlmsg_cancel(skb, nlh);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003946 return err ? err : -EMSGSIZE;
John Fastabend815cccb2012-10-24 08:13:09 +00003947}
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003948EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
John Fastabend815cccb2012-10-24 08:13:09 +00003949
John Fastabende5a55a82012-10-24 08:12:57 +00003950static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
3951{
3952 struct net *net = sock_net(skb->sk);
3953 struct net_device *dev;
3954 int idx = 0;
3955 u32 portid = NETLINK_CB(cb->skb).portid;
3956 u32 seq = cb->nlh->nlmsg_seq;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00003957 u32 filter_mask = 0;
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003958 int err;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00003959
Thomas Grafaa68c202014-11-26 13:42:20 +01003960 if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
3961 struct nlattr *extfilt;
3962
3963 extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
3964 IFLA_EXT_MASK);
3965 if (extfilt) {
3966 if (nla_len(extfilt) < sizeof(filter_mask))
3967 return -EINVAL;
3968
3969 filter_mask = nla_get_u32(extfilt);
3970 }
3971 }
John Fastabende5a55a82012-10-24 08:12:57 +00003972
3973 rcu_read_lock();
3974 for_each_netdev_rcu(net, dev) {
3975 const struct net_device_ops *ops = dev->netdev_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00003976 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
John Fastabende5a55a82012-10-24 08:12:57 +00003977
Jiri Pirko898e5062013-01-03 22:48:52 +00003978 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003979 if (idx >= cb->args[0]) {
3980 err = br_dev->netdev_ops->ndo_bridge_getlink(
3981 skb, portid, seq, dev,
3982 filter_mask, NLM_F_MULTI);
David Ahernf6c57752017-05-15 23:19:17 -07003983 if (err < 0 && err != -EOPNOTSUPP) {
3984 if (likely(skb->len))
3985 break;
3986
3987 goto out_err;
3988 }
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003989 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00003990 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00003991 }
3992
3993 if (ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003994 if (idx >= cb->args[0]) {
3995 err = ops->ndo_bridge_getlink(skb, portid,
3996 seq, dev,
3997 filter_mask,
3998 NLM_F_MULTI);
David Ahernf6c57752017-05-15 23:19:17 -07003999 if (err < 0 && err != -EOPNOTSUPP) {
4000 if (likely(skb->len))
4001 break;
4002
4003 goto out_err;
4004 }
Roopa Prabhud64f69b2015-09-15 14:44:29 -07004005 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00004006 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00004007 }
4008 }
David Ahernf6c57752017-05-15 23:19:17 -07004009 err = skb->len;
4010out_err:
John Fastabende5a55a82012-10-24 08:12:57 +00004011 rcu_read_unlock();
4012 cb->args[0] = idx;
4013
David Ahernf6c57752017-05-15 23:19:17 -07004014 return err;
John Fastabende5a55a82012-10-24 08:12:57 +00004015}
4016
John Fastabend2469ffd2012-10-24 08:13:03 +00004017static inline size_t bridge_nlmsg_size(void)
4018{
4019 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4020 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4021 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4022 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
4023 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
4024 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
4025 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
4026 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
4027 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
4028 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
4029 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
4030}
4031
Roopa Prabhu02dba432015-01-14 20:02:25 -08004032static int rtnl_bridge_notify(struct net_device *dev)
John Fastabend2469ffd2012-10-24 08:13:03 +00004033{
4034 struct net *net = dev_net(dev);
John Fastabend2469ffd2012-10-24 08:13:03 +00004035 struct sk_buff *skb;
4036 int err = -EOPNOTSUPP;
4037
Roopa Prabhu02dba432015-01-14 20:02:25 -08004038 if (!dev->netdev_ops->ndo_bridge_getlink)
4039 return 0;
4040
John Fastabend2469ffd2012-10-24 08:13:03 +00004041 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
4042 if (!skb) {
4043 err = -ENOMEM;
4044 goto errout;
4045 }
4046
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02004047 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
Roopa Prabhu02dba432015-01-14 20:02:25 -08004048 if (err < 0)
4049 goto errout;
John Fastabend2469ffd2012-10-24 08:13:03 +00004050
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08004051 if (!skb->len)
4052 goto errout;
4053
John Fastabend2469ffd2012-10-24 08:13:03 +00004054 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
4055 return 0;
4056errout:
4057 WARN_ON(err == -EMSGSIZE);
4058 kfree_skb(skb);
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08004059 if (err)
4060 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
John Fastabend2469ffd2012-10-24 08:13:03 +00004061 return err;
4062}
4063
David Ahernc21ef3e2017-04-16 09:48:24 -07004064static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
4065 struct netlink_ext_ack *extack)
John Fastabende5a55a82012-10-24 08:12:57 +00004066{
4067 struct net *net = sock_net(skb->sk);
4068 struct ifinfomsg *ifm;
4069 struct net_device *dev;
John Fastabend2469ffd2012-10-24 08:13:03 +00004070 struct nlattr *br_spec, *attr = NULL;
4071 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02004072 u16 flags = 0;
John Fastabendc38e01b2012-11-02 16:32:36 +00004073 bool have_flags = false;
John Fastabende5a55a82012-10-24 08:12:57 +00004074
4075 if (nlmsg_len(nlh) < sizeof(*ifm))
4076 return -EINVAL;
4077
4078 ifm = nlmsg_data(nlh);
4079 if (ifm->ifi_family != AF_BRIDGE)
4080 return -EPFNOSUPPORT;
4081
4082 dev = __dev_get_by_index(net, ifm->ifi_index);
4083 if (!dev) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004084 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabende5a55a82012-10-24 08:12:57 +00004085 return -ENODEV;
4086 }
4087
John Fastabend2469ffd2012-10-24 08:13:03 +00004088 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4089 if (br_spec) {
4090 nla_for_each_nested(attr, br_spec, rem) {
4091 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01004092 if (nla_len(attr) < sizeof(flags))
4093 return -EINVAL;
4094
John Fastabendc38e01b2012-11-02 16:32:36 +00004095 have_flags = true;
John Fastabend2469ffd2012-10-24 08:13:03 +00004096 flags = nla_get_u16(attr);
4097 break;
4098 }
4099 }
4100 }
4101
4102 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00004103 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4104
4105 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
John Fastabend2469ffd2012-10-24 08:13:03 +00004106 err = -EOPNOTSUPP;
4107 goto out;
4108 }
4109
Roopa Prabhuadd511b2015-01-29 22:40:12 -08004110 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
John Fastabende5a55a82012-10-24 08:12:57 +00004111 if (err)
4112 goto out;
John Fastabend2469ffd2012-10-24 08:13:03 +00004113
4114 flags &= ~BRIDGE_FLAGS_MASTER;
John Fastabende5a55a82012-10-24 08:12:57 +00004115 }
4116
John Fastabend2469ffd2012-10-24 08:13:03 +00004117 if ((flags & BRIDGE_FLAGS_SELF)) {
4118 if (!dev->netdev_ops->ndo_bridge_setlink)
4119 err = -EOPNOTSUPP;
4120 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08004121 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
4122 flags);
Roopa Prabhu02dba432015-01-14 20:02:25 -08004123 if (!err) {
John Fastabend2469ffd2012-10-24 08:13:03 +00004124 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08004125
4126 /* Generate event to notify upper layer of bridge
4127 * change
4128 */
4129 err = rtnl_bridge_notify(dev);
4130 }
John Fastabend2469ffd2012-10-24 08:13:03 +00004131 }
4132
John Fastabendc38e01b2012-11-02 16:32:36 +00004133 if (have_flags)
John Fastabend2469ffd2012-10-24 08:13:03 +00004134 memcpy(nla_data(attr), &flags, sizeof(flags));
John Fastabende5a55a82012-10-24 08:12:57 +00004135out:
4136 return err;
4137}
4138
David Ahernc21ef3e2017-04-16 09:48:24 -07004139static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
4140 struct netlink_ext_ack *extack)
Vlad Yasevich407af322013-02-13 12:00:12 +00004141{
4142 struct net *net = sock_net(skb->sk);
4143 struct ifinfomsg *ifm;
4144 struct net_device *dev;
4145 struct nlattr *br_spec, *attr = NULL;
4146 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02004147 u16 flags = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +00004148 bool have_flags = false;
4149
4150 if (nlmsg_len(nlh) < sizeof(*ifm))
4151 return -EINVAL;
4152
4153 ifm = nlmsg_data(nlh);
4154 if (ifm->ifi_family != AF_BRIDGE)
4155 return -EPFNOSUPPORT;
4156
4157 dev = __dev_get_by_index(net, ifm->ifi_index);
4158 if (!dev) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004159 NL_SET_ERR_MSG(extack, "unknown ifindex");
Vlad Yasevich407af322013-02-13 12:00:12 +00004160 return -ENODEV;
4161 }
4162
4163 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4164 if (br_spec) {
4165 nla_for_each_nested(attr, br_spec, rem) {
4166 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01004167 if (nla_len(attr) < sizeof(flags))
4168 return -EINVAL;
4169
Vlad Yasevich407af322013-02-13 12:00:12 +00004170 have_flags = true;
4171 flags = nla_get_u16(attr);
4172 break;
4173 }
4174 }
4175 }
4176
Vlad Yasevich407af322013-02-13 12:00:12 +00004177 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
4178 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4179
4180 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
4181 err = -EOPNOTSUPP;
4182 goto out;
4183 }
4184
Roopa Prabhuadd511b2015-01-29 22:40:12 -08004185 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00004186 if (err)
4187 goto out;
4188
4189 flags &= ~BRIDGE_FLAGS_MASTER;
4190 }
4191
4192 if ((flags & BRIDGE_FLAGS_SELF)) {
4193 if (!dev->netdev_ops->ndo_bridge_dellink)
4194 err = -EOPNOTSUPP;
4195 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08004196 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
4197 flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00004198
Roopa Prabhu02dba432015-01-14 20:02:25 -08004199 if (!err) {
Vlad Yasevich407af322013-02-13 12:00:12 +00004200 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08004201
4202 /* Generate event to notify upper layer of bridge
4203 * change
4204 */
4205 err = rtnl_bridge_notify(dev);
4206 }
Vlad Yasevich407af322013-02-13 12:00:12 +00004207 }
4208
4209 if (have_flags)
4210 memcpy(nla_data(attr), &flags, sizeof(flags));
Vlad Yasevich407af322013-02-13 12:00:12 +00004211out:
4212 return err;
4213}
4214
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004215static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
4216{
4217 return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
4218 (!idxattr || idxattr == attrid);
4219}
4220
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004221#define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
4222static int rtnl_get_offload_stats_attr_size(int attr_id)
4223{
4224 switch (attr_id) {
4225 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
4226 return sizeof(struct rtnl_link_stats64);
4227 }
4228
4229 return 0;
4230}
4231
4232static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
4233 int *prividx)
4234{
4235 struct nlattr *attr = NULL;
4236 int attr_id, size;
4237 void *attr_data;
4238 int err;
4239
4240 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
4241 dev->netdev_ops->ndo_get_offload_stats))
4242 return -ENODATA;
4243
4244 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
4245 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
4246 if (attr_id < *prividx)
4247 continue;
4248
4249 size = rtnl_get_offload_stats_attr_size(attr_id);
4250 if (!size)
4251 continue;
4252
Or Gerlitz3df5b3c2016-11-22 23:09:54 +02004253 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004254 continue;
4255
4256 attr = nla_reserve_64bit(skb, attr_id, size,
4257 IFLA_OFFLOAD_XSTATS_UNSPEC);
4258 if (!attr)
4259 goto nla_put_failure;
4260
4261 attr_data = nla_data(attr);
4262 memset(attr_data, 0, size);
4263 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
4264 attr_data);
4265 if (err)
4266 goto get_offload_stats_failure;
4267 }
4268
4269 if (!attr)
4270 return -ENODATA;
4271
4272 *prividx = 0;
4273 return 0;
4274
4275nla_put_failure:
4276 err = -EMSGSIZE;
4277get_offload_stats_failure:
4278 *prividx = attr_id;
4279 return err;
4280}
4281
4282static int rtnl_get_offload_stats_size(const struct net_device *dev)
4283{
4284 int nla_size = 0;
4285 int attr_id;
4286 int size;
4287
4288 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
4289 dev->netdev_ops->ndo_get_offload_stats))
4290 return 0;
4291
4292 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
4293 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
Or Gerlitz3df5b3c2016-11-22 23:09:54 +02004294 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004295 continue;
4296 size = rtnl_get_offload_stats_attr_size(attr_id);
4297 nla_size += nla_total_size_64bit(size);
4298 }
4299
4300 if (nla_size != 0)
4301 nla_size += nla_total_size(0);
4302
4303 return nla_size;
4304}
4305
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004306static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
4307 int type, u32 pid, u32 seq, u32 change,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004308 unsigned int flags, unsigned int filter_mask,
4309 int *idxattr, int *prividx)
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004310{
4311 struct if_stats_msg *ifsm;
4312 struct nlmsghdr *nlh;
4313 struct nlattr *attr;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004314 int s_prividx = *prividx;
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004315 int err;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004316
4317 ASSERT_RTNL();
4318
4319 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
4320 if (!nlh)
4321 return -EMSGSIZE;
4322
4323 ifsm = nlmsg_data(nlh);
Nikolay Aleksandrovce024f42017-10-03 13:20:48 +03004324 ifsm->family = PF_UNSPEC;
4325 ifsm->pad1 = 0;
4326 ifsm->pad2 = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004327 ifsm->ifindex = dev->ifindex;
4328 ifsm->filter_mask = filter_mask;
4329
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004330 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004331 struct rtnl_link_stats64 *sp;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004332
Nicolas Dichtel58414d32016-04-21 18:58:25 +02004333 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
4334 sizeof(struct rtnl_link_stats64),
4335 IFLA_STATS_UNSPEC);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004336 if (!attr)
4337 goto nla_put_failure;
4338
4339 sp = nla_data(attr);
4340 dev_get_stats(dev, sp);
4341 }
4342
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004343 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
4344 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
4345
4346 if (ops && ops->fill_linkxstats) {
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004347 *idxattr = IFLA_STATS_LINK_XSTATS;
4348 attr = nla_nest_start(skb,
4349 IFLA_STATS_LINK_XSTATS);
4350 if (!attr)
4351 goto nla_put_failure;
4352
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004353 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
4354 nla_nest_end(skb, attr);
4355 if (err)
4356 goto nla_put_failure;
4357 *idxattr = 0;
4358 }
4359 }
4360
4361 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
4362 *idxattr)) {
4363 const struct rtnl_link_ops *ops = NULL;
4364 const struct net_device *master;
4365
4366 master = netdev_master_upper_dev_get(dev);
4367 if (master)
4368 ops = master->rtnl_link_ops;
4369 if (ops && ops->fill_linkxstats) {
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004370 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
4371 attr = nla_nest_start(skb,
4372 IFLA_STATS_LINK_XSTATS_SLAVE);
4373 if (!attr)
4374 goto nla_put_failure;
4375
4376 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004377 nla_nest_end(skb, attr);
4378 if (err)
4379 goto nla_put_failure;
4380 *idxattr = 0;
4381 }
4382 }
4383
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004384 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
4385 *idxattr)) {
4386 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
4387 attr = nla_nest_start(skb, IFLA_STATS_LINK_OFFLOAD_XSTATS);
4388 if (!attr)
4389 goto nla_put_failure;
4390
4391 err = rtnl_get_offload_stats(skb, dev, prividx);
4392 if (err == -ENODATA)
4393 nla_nest_cancel(skb, attr);
4394 else
4395 nla_nest_end(skb, attr);
4396
4397 if (err && err != -ENODATA)
4398 goto nla_put_failure;
4399 *idxattr = 0;
4400 }
4401
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004402 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
4403 struct rtnl_af_ops *af_ops;
4404
4405 *idxattr = IFLA_STATS_AF_SPEC;
4406 attr = nla_nest_start(skb, IFLA_STATS_AF_SPEC);
4407 if (!attr)
4408 goto nla_put_failure;
4409
Florian Westphal5fa85a02017-10-16 15:44:36 +02004410 rcu_read_lock();
4411 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004412 if (af_ops->fill_stats_af) {
4413 struct nlattr *af;
4414 int err;
4415
4416 af = nla_nest_start(skb, af_ops->family);
Florian Westphal5fa85a02017-10-16 15:44:36 +02004417 if (!af) {
4418 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004419 goto nla_put_failure;
Florian Westphal5fa85a02017-10-16 15:44:36 +02004420 }
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004421 err = af_ops->fill_stats_af(skb, dev);
4422
Florian Westphal5fa85a02017-10-16 15:44:36 +02004423 if (err == -ENODATA) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004424 nla_nest_cancel(skb, af);
Florian Westphal5fa85a02017-10-16 15:44:36 +02004425 } else if (err < 0) {
4426 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004427 goto nla_put_failure;
Florian Westphal5fa85a02017-10-16 15:44:36 +02004428 }
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004429
4430 nla_nest_end(skb, af);
4431 }
4432 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02004433 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004434
4435 nla_nest_end(skb, attr);
4436
4437 *idxattr = 0;
4438 }
4439
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004440 nlmsg_end(skb, nlh);
4441
4442 return 0;
4443
4444nla_put_failure:
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004445 /* not a multi message or no progress mean a real error */
4446 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
4447 nlmsg_cancel(skb, nlh);
4448 else
4449 nlmsg_end(skb, nlh);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004450
4451 return -EMSGSIZE;
4452}
4453
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004454static size_t if_nlmsg_stats_size(const struct net_device *dev,
4455 u32 filter_mask)
4456{
4457 size_t size = 0;
4458
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004459 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004460 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
4461
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004462 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
4463 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004464 int attr = IFLA_STATS_LINK_XSTATS;
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004465
4466 if (ops && ops->get_linkxstats_size) {
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004467 size += nla_total_size(ops->get_linkxstats_size(dev,
4468 attr));
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004469 /* for IFLA_STATS_LINK_XSTATS */
4470 size += nla_total_size(0);
4471 }
4472 }
4473
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004474 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
4475 struct net_device *_dev = (struct net_device *)dev;
4476 const struct rtnl_link_ops *ops = NULL;
4477 const struct net_device *master;
4478
4479 /* netdev_master_upper_dev_get can't take const */
4480 master = netdev_master_upper_dev_get(_dev);
4481 if (master)
4482 ops = master->rtnl_link_ops;
4483 if (ops && ops->get_linkxstats_size) {
4484 int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
4485
4486 size += nla_total_size(ops->get_linkxstats_size(dev,
4487 attr));
4488 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
4489 size += nla_total_size(0);
4490 }
4491 }
4492
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004493 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
4494 size += rtnl_get_offload_stats_size(dev);
4495
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004496 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
4497 struct rtnl_af_ops *af_ops;
4498
4499 /* for IFLA_STATS_AF_SPEC */
4500 size += nla_total_size(0);
4501
Florian Westphal5fa85a02017-10-16 15:44:36 +02004502 rcu_read_lock();
4503 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004504 if (af_ops->get_stats_af_size) {
4505 size += nla_total_size(
4506 af_ops->get_stats_af_size(dev));
4507
4508 /* for AF_* */
4509 size += nla_total_size(0);
4510 }
4511 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02004512 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004513 }
4514
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004515 return size;
4516}
4517
David Ahernc21ef3e2017-04-16 09:48:24 -07004518static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
4519 struct netlink_ext_ack *extack)
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004520{
4521 struct net *net = sock_net(skb->sk);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004522 struct net_device *dev = NULL;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004523 int idxattr = 0, prividx = 0;
4524 struct if_stats_msg *ifsm;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004525 struct sk_buff *nskb;
4526 u32 filter_mask;
4527 int err;
4528
Mathias Krause4775cc12016-12-28 17:52:15 +01004529 if (nlmsg_len(nlh) < sizeof(*ifsm))
4530 return -EINVAL;
4531
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004532 ifsm = nlmsg_data(nlh);
4533 if (ifsm->ifindex > 0)
4534 dev = __dev_get_by_index(net, ifsm->ifindex);
4535 else
4536 return -EINVAL;
4537
4538 if (!dev)
4539 return -ENODEV;
4540
4541 filter_mask = ifsm->filter_mask;
4542 if (!filter_mask)
4543 return -EINVAL;
4544
4545 nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
4546 if (!nskb)
4547 return -ENOBUFS;
4548
4549 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
4550 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004551 0, filter_mask, &idxattr, &prividx);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004552 if (err < 0) {
4553 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
4554 WARN_ON(err == -EMSGSIZE);
4555 kfree_skb(nskb);
4556 } else {
4557 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
4558 }
4559
4560 return err;
4561}
4562
4563static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
4564{
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004565 int h, s_h, err, s_idx, s_idxattr, s_prividx;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004566 struct net *net = sock_net(skb->sk);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004567 unsigned int flags = NLM_F_MULTI;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004568 struct if_stats_msg *ifsm;
4569 struct hlist_head *head;
4570 struct net_device *dev;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004571 u32 filter_mask = 0;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004572 int idx = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004573
4574 s_h = cb->args[0];
4575 s_idx = cb->args[1];
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004576 s_idxattr = cb->args[2];
4577 s_prividx = cb->args[3];
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004578
4579 cb->seq = net->dev_base_seq;
4580
Mathias Krause4775cc12016-12-28 17:52:15 +01004581 if (nlmsg_len(cb->nlh) < sizeof(*ifsm))
4582 return -EINVAL;
4583
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004584 ifsm = nlmsg_data(cb->nlh);
4585 filter_mask = ifsm->filter_mask;
4586 if (!filter_mask)
4587 return -EINVAL;
4588
4589 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4590 idx = 0;
4591 head = &net->dev_index_head[h];
4592 hlist_for_each_entry(dev, head, index_hlist) {
4593 if (idx < s_idx)
4594 goto cont;
4595 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
4596 NETLINK_CB(cb->skb).portid,
4597 cb->nlh->nlmsg_seq, 0,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004598 flags, filter_mask,
4599 &s_idxattr, &s_prividx);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004600 /* If we ran out of room on the first message,
4601 * we're in trouble
4602 */
4603 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
4604
4605 if (err < 0)
4606 goto out;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004607 s_prividx = 0;
4608 s_idxattr = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004609 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
4610cont:
4611 idx++;
4612 }
4613 }
4614out:
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004615 cb->args[3] = s_prividx;
4616 cb->args[2] = s_idxattr;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004617 cb->args[1] = idx;
4618 cb->args[0] = h;
4619
4620 return skb->len;
4621}
4622
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623/* Process one rtnetlink message. */
4624
Johannes Berg2d4bc932017-04-12 14:34:04 +02004625static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
4626 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004628 struct net *net = sock_net(skb->sk);
Florian Westphaladdf9b92017-12-02 21:44:05 +01004629 struct rtnl_link *link;
Florian Westphale4202512017-12-02 21:44:06 +01004630 struct module *owner;
Florian Westphal6853dd42017-08-09 20:41:51 +02004631 int err = -EOPNOTSUPP;
Thomas Grafe2849862007-03-22 11:48:11 -07004632 rtnl_doit_func doit;
Florian Westphal62256f92017-08-09 20:41:52 +02004633 unsigned int flags;
Alexander Kuleshov617cfc72016-01-10 21:26:57 +06004634 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 int family;
4636 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 if (type > RTM_MAX)
Thomas Graf038890f2007-04-05 14:35:52 -07004640 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641
4642 type -= RTM_BASE;
4643
4644 /* All the messages must have at least 1 byte length */
Hong zhi guo573ce262013-03-27 06:47:04 +00004645 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 return 0;
4647
Hong zhi guo573ce262013-03-27 06:47:04 +00004648 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 kind = type&3;
4650
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07004651 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07004652 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653
Florian Westphal6853dd42017-08-09 20:41:51 +02004654 rcu_read_lock();
David S. Millerb8f3ab42011-01-18 12:40:38 -08004655 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004656 struct sock *rtnl;
Thomas Grafe2849862007-03-22 11:48:11 -07004657 rtnl_dumpit_func dumpit;
Greg Rosec7ac8672011-06-10 01:27:09 +00004658 u16 min_dump_alloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659
Florian Westphaladdf9b92017-12-02 21:44:05 +01004660 link = rtnl_get_link(family, type);
4661 if (!link || !link->dumpit) {
Florian Westphal6853dd42017-08-09 20:41:51 +02004662 family = PF_UNSPEC;
Florian Westphaladdf9b92017-12-02 21:44:05 +01004663 link = rtnl_get_link(family, type);
4664 if (!link || !link->dumpit)
Florian Westphal6853dd42017-08-09 20:41:51 +02004665 goto err_unlock;
4666 }
Florian Westphale4202512017-12-02 21:44:06 +01004667 owner = link->owner;
Florian Westphaladdf9b92017-12-02 21:44:05 +01004668 dumpit = link->dumpit;
Florian Westphale1fa6d22017-08-09 20:41:47 +02004669
Florian Westphal5c2bb9b2017-08-10 16:52:58 +02004670 if (type == RTM_GETLINK - RTM_BASE)
Florian Westphale1fa6d22017-08-09 20:41:47 +02004671 min_dump_alloc = rtnl_calcit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672
Florian Westphale4202512017-12-02 21:44:06 +01004673 err = 0;
4674 /* need to do this before rcu_read_unlock() */
4675 if (!try_module_get(owner))
4676 err = -EPROTONOSUPPORT;
4677
Florian Westphal6853dd42017-08-09 20:41:51 +02004678 rcu_read_unlock();
4679
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004680 rtnl = net->rtnl;
Florian Westphale4202512017-12-02 21:44:06 +01004681 if (err == 0) {
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00004682 struct netlink_dump_control c = {
4683 .dump = dumpit,
4684 .min_dump_alloc = min_dump_alloc,
Florian Westphale4202512017-12-02 21:44:06 +01004685 .module = owner,
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00004686 };
4687 err = netlink_dump_start(rtnl, skb, nlh, &c);
Florian Westphale4202512017-12-02 21:44:06 +01004688 /* netlink_dump_start() will keep a reference on
4689 * module if dump is still in progress.
4690 */
4691 module_put(owner);
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00004692 }
Eric Dumazet2907c352011-05-25 07:34:04 +00004693 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 }
4695
Florian Westphaladdf9b92017-12-02 21:44:05 +01004696 link = rtnl_get_link(family, type);
4697 if (!link || !link->doit) {
Florian Westphal8caa38b2017-08-10 16:53:01 +02004698 family = PF_UNSPEC;
Florian Westphaladdf9b92017-12-02 21:44:05 +01004699 link = rtnl_get_link(PF_UNSPEC, type);
4700 if (!link || !link->doit)
4701 goto out_unlock;
Florian Westphal8caa38b2017-08-10 16:53:01 +02004702 }
4703
Florian Westphale4202512017-12-02 21:44:06 +01004704 owner = link->owner;
4705 if (!try_module_get(owner)) {
4706 err = -EPROTONOSUPPORT;
4707 goto out_unlock;
4708 }
4709
Florian Westphaladdf9b92017-12-02 21:44:05 +01004710 flags = link->flags;
Florian Westphal62256f92017-08-09 20:41:52 +02004711 if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
Florian Westphaladdf9b92017-12-02 21:44:05 +01004712 doit = link->doit;
Florian Westphal62256f92017-08-09 20:41:52 +02004713 rcu_read_unlock();
4714 if (doit)
4715 err = doit(skb, nlh, extack);
Florian Westphale4202512017-12-02 21:44:06 +01004716 module_put(owner);
Florian Westphal62256f92017-08-09 20:41:52 +02004717 return err;
4718 }
Florian Westphal6853dd42017-08-09 20:41:51 +02004719 rcu_read_unlock();
4720
Florian Westphal0cc09022017-08-09 20:41:50 +02004721 rtnl_lock();
Florian Westphaladdf9b92017-12-02 21:44:05 +01004722 link = rtnl_get_link(family, type);
4723 if (link && link->doit)
4724 err = link->doit(skb, nlh, extack);
Florian Westphal0cc09022017-08-09 20:41:50 +02004725 rtnl_unlock();
Florian Westphaladdf9b92017-12-02 21:44:05 +01004726
Florian Westphale4202512017-12-02 21:44:06 +01004727 module_put(owner);
4728
Florian Westphaladdf9b92017-12-02 21:44:05 +01004729 return err;
4730
4731out_unlock:
4732 rcu_read_unlock();
Florian Westphal0cc09022017-08-09 20:41:50 +02004733 return err;
4734
4735err_unlock:
Florian Westphal6853dd42017-08-09 20:41:51 +02004736 rcu_read_unlock();
Florian Westphal0cc09022017-08-09 20:41:50 +02004737 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738}
4739
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07004740static void rtnetlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07004742 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743}
4744
Julien Gomes5f729ea2017-06-20 13:54:16 -07004745static int rtnetlink_bind(struct net *net, int group)
4746{
4747 switch (group) {
4748 case RTNLGRP_IPV4_MROUTE_R:
4749 case RTNLGRP_IPV6_MROUTE_R:
4750 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
4751 return -EPERM;
4752 break;
4753 }
4754 return 0;
4755}
4756
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
4758{
Jiri Pirko351638e2013-05-28 01:30:21 +00004759 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02004760
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 switch (event) {
Vlad Yasevich5138e862017-04-04 09:23:41 -04004762 case NETDEV_REBOOT:
Xin Long8a212582017-10-15 18:13:41 +08004763 case NETDEV_CHANGEMTU:
David Ahern37536542017-07-19 10:22:40 -07004764 case NETDEV_CHANGEADDR:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004765 case NETDEV_CHANGENAME:
4766 case NETDEV_FEAT_CHANGE:
4767 case NETDEV_BONDING_FAILOVER:
Xin Longe6e66592017-10-15 18:13:43 +08004768 case NETDEV_POST_TYPE_CHANGE:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004769 case NETDEV_NOTIFY_PEERS:
Xin Longdc709f32017-10-15 18:13:44 +08004770 case NETDEV_CHANGEUPPER:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004771 case NETDEV_RESEND_IGMP:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004772 case NETDEV_CHANGEINFODATA:
Xin Longeeda3fb2017-10-24 13:54:19 +08004773 case NETDEV_CHANGELOWERSTATE:
Xin Longebdcf042017-10-15 18:13:42 +08004774 case NETDEV_CHANGE_TX_QUEUE_LEN:
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04004775 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01004776 GFP_KERNEL, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 break;
4778 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 break;
4780 }
4781 return NOTIFY_DONE;
4782}
4783
4784static struct notifier_block rtnetlink_dev_notifier = {
4785 .notifier_call = rtnetlink_event,
4786};
4787
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004788
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004789static int __net_init rtnetlink_net_init(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004790{
4791 struct sock *sk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00004792 struct netlink_kernel_cfg cfg = {
4793 .groups = RTNLGRP_MAX,
4794 .input = rtnetlink_rcv,
4795 .cb_mutex = &rtnl_mutex,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00004796 .flags = NL_CFG_F_NONROOT_RECV,
Julien Gomes5f729ea2017-06-20 13:54:16 -07004797 .bind = rtnetlink_bind,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00004798 };
4799
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00004800 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004801 if (!sk)
4802 return -ENOMEM;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004803 net->rtnl = sk;
4804 return 0;
4805}
4806
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004807static void __net_exit rtnetlink_net_exit(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004808{
Denis V. Lunev775516b2008-01-18 23:55:19 -08004809 netlink_kernel_release(net->rtnl);
4810 net->rtnl = NULL;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004811}
4812
4813static struct pernet_operations rtnetlink_net_ops = {
4814 .init = rtnetlink_net_init,
4815 .exit = rtnetlink_net_exit,
4816};
4817
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818void __init rtnetlink_init(void)
4819{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004820 if (register_pernet_subsys(&rtnetlink_net_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 panic("rtnetlink_init: cannot initialize rtnetlink\n");
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004822
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823 register_netdevice_notifier(&rtnetlink_dev_notifier);
Thomas Graf340d17f2007-03-22 11:49:22 -07004824
Greg Rosec7ac8672011-06-10 01:27:09 +00004825 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
Florian Westphalb97bac62017-08-09 20:41:48 +02004826 rtnl_dump_ifinfo, 0);
4827 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
4828 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
4829 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
Thomas Graf687ad8c2007-03-22 11:59:42 -07004830
Florian Westphalb97bac62017-08-09 20:41:48 +02004831 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
4832 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
4833 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
John Fastabend77162022012-04-15 06:43:56 +00004834
Florian Westphalb97bac62017-08-09 20:41:48 +02004835 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
4836 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
4837 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, 0);
John Fastabende5a55a82012-10-24 08:12:57 +00004838
Florian Westphalb97bac62017-08-09 20:41:48 +02004839 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
4840 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
4841 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004842
4843 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
Florian Westphalb97bac62017-08-09 20:41:48 +02004844 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845}