blob: ba724576764e333ca9e0007af05de62867eac374 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/errno.h>
20#include <linux/module.h>
21#include <linux/types.h>
22#include <linux/socket.h>
23#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/timer.h>
25#include <linux/string.h>
26#include <linux/sockios.h>
27#include <linux/net.h>
28#include <linux/fcntl.h>
29#include <linux/mm.h>
30#include <linux/slab.h>
31#include <linux/interrupt.h>
32#include <linux/capability.h>
33#include <linux/skbuff.h>
34#include <linux/init.h>
35#include <linux/security.h>
Stephen Hemminger6756ae42006-03-20 22:23:58 -080036#include <linux/mutex.h>
Thomas Graf18237302006-08-04 23:04:54 -070037#include <linux/if_addr.h>
John Fastabend77162022012-04-15 06:43:56 +000038#include <linux/if_bridge.h>
Jiri Pirkof6f64242014-11-28 14:34:15 +010039#include <linux/if_vlan.h>
Williams, Mitch Aebc08a62010-02-10 01:44:05 +000040#include <linux/pci.h>
John Fastabend77162022012-04-15 06:43:56 +000041#include <linux/etherdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#include <linux/inet.h>
46#include <linux/netdevice.h>
Jiri Pirko82f28412014-11-28 14:34:18 +010047#include <net/switchdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <net/ip.h>
49#include <net/protocol.h>
50#include <net/arp.h>
51#include <net/route.h>
52#include <net/udp.h>
Daniel Borkmannea697632015-01-05 23:57:47 +010053#include <net/tcp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <net/sock.h>
55#include <net/pkt_sched.h>
Thomas Graf14c0b972006-08-04 03:38:38 -070056#include <net/fib_rules.h>
Thomas Grafe2849862007-03-22 11:48:11 -070057#include <net/rtnetlink.h>
Johannes Berg30ffee82009-07-10 09:51:35 +000058#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Eric Dumazete0d087a2009-11-07 01:26:17 -080060struct rtnl_link {
Thomas Grafe2849862007-03-22 11:48:11 -070061 rtnl_doit_func doit;
62 rtnl_dumpit_func dumpit;
Greg Rosec7ac8672011-06-10 01:27:09 +000063 rtnl_calcit_func calcit;
Thomas Grafe2849862007-03-22 11:48:11 -070064};
65
Stephen Hemminger6756ae42006-03-20 22:23:58 -080066static DEFINE_MUTEX(rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68void rtnl_lock(void)
69{
Stephen Hemminger6756ae42006-03-20 22:23:58 -080070 mutex_lock(&rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
Eric Dumazete0d087a2009-11-07 01:26:17 -080072EXPORT_SYMBOL(rtnl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Eric Dumazet1b5c5492016-06-13 20:21:50 -070074static struct sk_buff *defer_kfree_skb_list;
75void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail)
76{
77 if (head && tail) {
78 tail->next = defer_kfree_skb_list;
79 defer_kfree_skb_list = head;
80 }
81}
82EXPORT_SYMBOL(rtnl_kfree_skbs);
83
Stephen Hemminger6756ae42006-03-20 22:23:58 -080084void __rtnl_unlock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Eric Dumazet1b5c5492016-06-13 20:21:50 -070086 struct sk_buff *head = defer_kfree_skb_list;
87
88 defer_kfree_skb_list = NULL;
89
Stephen Hemminger6756ae42006-03-20 22:23:58 -080090 mutex_unlock(&rtnl_mutex);
Eric Dumazet1b5c5492016-06-13 20:21:50 -070091
92 while (head) {
93 struct sk_buff *next = head->next;
94
95 kfree_skb(head);
96 cond_resched();
97 head = next;
98 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101void rtnl_unlock(void)
102{
Herbert Xu58ec3b42008-10-07 15:50:03 -0700103 /* This fellow will unlock it for us. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 netdev_run_todo();
105}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800106EXPORT_SYMBOL(rtnl_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800108int rtnl_trylock(void)
109{
110 return mutex_trylock(&rtnl_mutex);
111}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800112EXPORT_SYMBOL(rtnl_trylock);
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800113
Patrick McHardyc9c10142008-04-23 22:10:48 -0700114int rtnl_is_locked(void)
115{
116 return mutex_is_locked(&rtnl_mutex);
117}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800118EXPORT_SYMBOL(rtnl_is_locked);
Patrick McHardyc9c10142008-04-23 22:10:48 -0700119
Paul E. McKenneya898def2010-02-22 17:04:49 -0800120#ifdef CONFIG_PROVE_LOCKING
Yaowei Bai0cbf3342015-10-08 21:29:02 +0800121bool lockdep_rtnl_is_held(void)
Paul E. McKenneya898def2010-02-22 17:04:49 -0800122{
123 return lockdep_is_held(&rtnl_mutex);
124}
125EXPORT_SYMBOL(lockdep_rtnl_is_held);
126#endif /* #ifdef CONFIG_PROVE_LOCKING */
127
Patrick McHardy25239ce2010-04-26 16:02:05 +0200128static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
Thomas Grafe2849862007-03-22 11:48:11 -0700129
130static inline int rtm_msgindex(int msgtype)
131{
132 int msgindex = msgtype - RTM_BASE;
133
134 /*
135 * msgindex < 0 implies someone tried to register a netlink
136 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
137 * the message type has not been added to linux/rtnetlink.h
138 */
139 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
140
141 return msgindex;
142}
143
144static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
145{
146 struct rtnl_link *tab;
147
Patrick McHardy25239ce2010-04-26 16:02:05 +0200148 if (protocol <= RTNL_FAMILY_MAX)
Patrick McHardy0f87b1d2010-04-13 05:03:17 +0000149 tab = rtnl_msg_handlers[protocol];
150 else
151 tab = NULL;
152
Thomas Graf51057f22007-03-22 21:41:06 -0700153 if (tab == NULL || tab[msgindex].doit == NULL)
Thomas Grafe2849862007-03-22 11:48:11 -0700154 tab = rtnl_msg_handlers[PF_UNSPEC];
155
Hans Zhangc80bbea2012-10-22 22:21:23 +0000156 return tab[msgindex].doit;
Thomas Grafe2849862007-03-22 11:48:11 -0700157}
158
159static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
160{
161 struct rtnl_link *tab;
162
Patrick McHardy25239ce2010-04-26 16:02:05 +0200163 if (protocol <= RTNL_FAMILY_MAX)
Patrick McHardy0f87b1d2010-04-13 05:03:17 +0000164 tab = rtnl_msg_handlers[protocol];
165 else
166 tab = NULL;
167
Thomas Graf51057f22007-03-22 21:41:06 -0700168 if (tab == NULL || tab[msgindex].dumpit == NULL)
Thomas Grafe2849862007-03-22 11:48:11 -0700169 tab = rtnl_msg_handlers[PF_UNSPEC];
170
Hans Zhangc80bbea2012-10-22 22:21:23 +0000171 return tab[msgindex].dumpit;
Thomas Grafe2849862007-03-22 11:48:11 -0700172}
173
Greg Rosec7ac8672011-06-10 01:27:09 +0000174static rtnl_calcit_func rtnl_get_calcit(int protocol, int msgindex)
175{
176 struct rtnl_link *tab;
177
178 if (protocol <= RTNL_FAMILY_MAX)
179 tab = rtnl_msg_handlers[protocol];
180 else
181 tab = NULL;
182
183 if (tab == NULL || tab[msgindex].calcit == NULL)
184 tab = rtnl_msg_handlers[PF_UNSPEC];
185
Hans Zhangc80bbea2012-10-22 22:21:23 +0000186 return tab[msgindex].calcit;
Greg Rosec7ac8672011-06-10 01:27:09 +0000187}
188
Thomas Grafe2849862007-03-22 11:48:11 -0700189/**
190 * __rtnl_register - Register a rtnetlink message type
191 * @protocol: Protocol family or PF_UNSPEC
192 * @msgtype: rtnetlink message type
193 * @doit: Function pointer called for each request message
194 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
Greg Rosec7ac8672011-06-10 01:27:09 +0000195 * @calcit: Function pointer to calc size of dump message
Thomas Grafe2849862007-03-22 11:48:11 -0700196 *
197 * Registers the specified function pointers (at least one of them has
198 * to be non-NULL) to be called whenever a request message for the
199 * specified protocol family and message type is received.
200 *
201 * The special protocol family PF_UNSPEC may be used to define fallback
202 * function pointers for the case when no entry for the specific protocol
203 * family exists.
204 *
205 * Returns 0 on success or a negative error code.
206 */
207int __rtnl_register(int protocol, int msgtype,
Greg Rosec7ac8672011-06-10 01:27:09 +0000208 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
209 rtnl_calcit_func calcit)
Thomas Grafe2849862007-03-22 11:48:11 -0700210{
211 struct rtnl_link *tab;
212 int msgindex;
213
Patrick McHardy25239ce2010-04-26 16:02:05 +0200214 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700215 msgindex = rtm_msgindex(msgtype);
216
217 tab = rtnl_msg_handlers[protocol];
218 if (tab == NULL) {
219 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
220 if (tab == NULL)
221 return -ENOBUFS;
222
223 rtnl_msg_handlers[protocol] = tab;
224 }
225
226 if (doit)
227 tab[msgindex].doit = doit;
228
229 if (dumpit)
230 tab[msgindex].dumpit = dumpit;
231
Greg Rosec7ac8672011-06-10 01:27:09 +0000232 if (calcit)
233 tab[msgindex].calcit = calcit;
234
Thomas Grafe2849862007-03-22 11:48:11 -0700235 return 0;
236}
Thomas Grafe2849862007-03-22 11:48:11 -0700237EXPORT_SYMBOL_GPL(__rtnl_register);
238
239/**
240 * rtnl_register - Register a rtnetlink message type
241 *
242 * Identical to __rtnl_register() but panics on failure. This is useful
243 * as failure of this function is very unlikely, it can only happen due
244 * to lack of memory when allocating the chain to store all message
245 * handlers for a protocol. Meant for use in init functions where lack
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300246 * of memory implies no sense in continuing.
Thomas Grafe2849862007-03-22 11:48:11 -0700247 */
248void rtnl_register(int protocol, int msgtype,
Greg Rosec7ac8672011-06-10 01:27:09 +0000249 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
250 rtnl_calcit_func calcit)
Thomas Grafe2849862007-03-22 11:48:11 -0700251{
Greg Rosec7ac8672011-06-10 01:27:09 +0000252 if (__rtnl_register(protocol, msgtype, doit, dumpit, calcit) < 0)
Thomas Grafe2849862007-03-22 11:48:11 -0700253 panic("Unable to register rtnetlink message handler, "
254 "protocol = %d, message type = %d\n",
255 protocol, msgtype);
256}
Thomas Grafe2849862007-03-22 11:48:11 -0700257EXPORT_SYMBOL_GPL(rtnl_register);
258
259/**
260 * rtnl_unregister - Unregister a rtnetlink message type
261 * @protocol: Protocol family or PF_UNSPEC
262 * @msgtype: rtnetlink message type
263 *
264 * Returns 0 on success or a negative error code.
265 */
266int rtnl_unregister(int protocol, int msgtype)
267{
268 int msgindex;
269
Patrick McHardy25239ce2010-04-26 16:02:05 +0200270 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700271 msgindex = rtm_msgindex(msgtype);
272
273 if (rtnl_msg_handlers[protocol] == NULL)
274 return -ENOENT;
275
276 rtnl_msg_handlers[protocol][msgindex].doit = NULL;
277 rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
Mathias Krausef567e952016-11-07 23:22:19 +0100278 rtnl_msg_handlers[protocol][msgindex].calcit = NULL;
Thomas Grafe2849862007-03-22 11:48:11 -0700279
280 return 0;
281}
Thomas Grafe2849862007-03-22 11:48:11 -0700282EXPORT_SYMBOL_GPL(rtnl_unregister);
283
284/**
285 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
286 * @protocol : Protocol family or PF_UNSPEC
287 *
288 * Identical to calling rtnl_unregster() for all registered message types
289 * of a certain protocol family.
290 */
291void rtnl_unregister_all(int protocol)
292{
Patrick McHardy25239ce2010-04-26 16:02:05 +0200293 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700294
295 kfree(rtnl_msg_handlers[protocol]);
296 rtnl_msg_handlers[protocol] = NULL;
297}
Thomas Grafe2849862007-03-22 11:48:11 -0700298EXPORT_SYMBOL_GPL(rtnl_unregister_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Patrick McHardy38f7b872007-06-13 12:03:51 -0700300static LIST_HEAD(link_ops);
301
Eric Dumazetc63044f2011-12-13 11:38:00 +0000302static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
303{
304 const struct rtnl_link_ops *ops;
305
306 list_for_each_entry(ops, &link_ops, list) {
307 if (!strcmp(ops->kind, kind))
308 return ops;
309 }
310 return NULL;
311}
312
Patrick McHardy38f7b872007-06-13 12:03:51 -0700313/**
314 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
315 * @ops: struct rtnl_link_ops * to register
316 *
317 * The caller must hold the rtnl_mutex. This function should be used
318 * by drivers that create devices during module initialization. It
319 * must be called before registering the devices.
320 *
321 * Returns 0 on success or a negative error code.
322 */
323int __rtnl_link_register(struct rtnl_link_ops *ops)
324{
Eric Dumazetc63044f2011-12-13 11:38:00 +0000325 if (rtnl_link_ops_get(ops->kind))
326 return -EEXIST;
327
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +0200328 /* The check for setup is here because if ops
329 * does not have that filled up, it is not possible
330 * to use the ops for creating device. So do not
331 * fill up dellink as well. That disables rtnl_dellink.
332 */
333 if (ops->setup && !ops->dellink)
Eric Dumazet23289a32009-10-27 07:06:36 +0000334 ops->dellink = unregister_netdevice_queue;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700335
Patrick McHardy38f7b872007-06-13 12:03:51 -0700336 list_add_tail(&ops->list, &link_ops);
337 return 0;
338}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700339EXPORT_SYMBOL_GPL(__rtnl_link_register);
340
341/**
342 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
343 * @ops: struct rtnl_link_ops * to register
344 *
345 * Returns 0 on success or a negative error code.
346 */
347int rtnl_link_register(struct rtnl_link_ops *ops)
348{
349 int err;
350
351 rtnl_lock();
352 err = __rtnl_link_register(ops);
353 rtnl_unlock();
354 return err;
355}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700356EXPORT_SYMBOL_GPL(rtnl_link_register);
357
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700358static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
359{
360 struct net_device *dev;
Eric Dumazet23289a32009-10-27 07:06:36 +0000361 LIST_HEAD(list_kill);
362
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700363 for_each_netdev(net, dev) {
Eric Dumazet23289a32009-10-27 07:06:36 +0000364 if (dev->rtnl_link_ops == ops)
365 ops->dellink(dev, &list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700366 }
Eric Dumazet23289a32009-10-27 07:06:36 +0000367 unregister_netdevice_many(&list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700368}
369
Patrick McHardy38f7b872007-06-13 12:03:51 -0700370/**
371 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
372 * @ops: struct rtnl_link_ops * to unregister
373 *
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700374 * The caller must hold the rtnl_mutex.
Patrick McHardy38f7b872007-06-13 12:03:51 -0700375 */
376void __rtnl_link_unregister(struct rtnl_link_ops *ops)
377{
Eric W. Biederman881d9662007-09-17 11:56:21 -0700378 struct net *net;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700379
Eric W. Biederman881d9662007-09-17 11:56:21 -0700380 for_each_net(net) {
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700381 __rtnl_kill_links(net, ops);
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700382 }
Patrick McHardy38f7b872007-06-13 12:03:51 -0700383 list_del(&ops->list);
384}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700385EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
386
Cong Wang200b9162014-05-12 15:11:20 -0700387/* Return with the rtnl_lock held when there are no network
388 * devices unregistering in any network namespace.
389 */
390static void rtnl_lock_unregistering_all(void)
391{
392 struct net *net;
393 bool unregistering;
Peter Zijlstraff960a72014-10-29 17:04:56 +0100394 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Cong Wang200b9162014-05-12 15:11:20 -0700395
Peter Zijlstraff960a72014-10-29 17:04:56 +0100396 add_wait_queue(&netdev_unregistering_wq, &wait);
Cong Wang200b9162014-05-12 15:11:20 -0700397 for (;;) {
Cong Wang200b9162014-05-12 15:11:20 -0700398 unregistering = false;
399 rtnl_lock();
400 for_each_net(net) {
401 if (net->dev_unreg_count > 0) {
402 unregistering = true;
403 break;
404 }
405 }
406 if (!unregistering)
407 break;
408 __rtnl_unlock();
Peter Zijlstraff960a72014-10-29 17:04:56 +0100409
410 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Cong Wang200b9162014-05-12 15:11:20 -0700411 }
Peter Zijlstraff960a72014-10-29 17:04:56 +0100412 remove_wait_queue(&netdev_unregistering_wq, &wait);
Cong Wang200b9162014-05-12 15:11:20 -0700413}
414
Patrick McHardy38f7b872007-06-13 12:03:51 -0700415/**
416 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
417 * @ops: struct rtnl_link_ops * to unregister
418 */
419void rtnl_link_unregister(struct rtnl_link_ops *ops)
420{
Cong Wang200b9162014-05-12 15:11:20 -0700421 /* Close the race with cleanup_net() */
422 mutex_lock(&net_mutex);
423 rtnl_lock_unregistering_all();
Patrick McHardy38f7b872007-06-13 12:03:51 -0700424 __rtnl_link_unregister(ops);
425 rtnl_unlock();
Cong Wang200b9162014-05-12 15:11:20 -0700426 mutex_unlock(&net_mutex);
Patrick McHardy38f7b872007-06-13 12:03:51 -0700427}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700428EXPORT_SYMBOL_GPL(rtnl_link_unregister);
429
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100430static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
431{
432 struct net_device *master_dev;
433 const struct rtnl_link_ops *ops;
434
435 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
436 if (!master_dev)
437 return 0;
438 ops = master_dev->rtnl_link_ops;
Fernando Luis Vazquez Cao6049f252014-02-04 19:35:02 +0900439 if (!ops || !ops->get_slave_size)
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100440 return 0;
441 /* IFLA_INFO_SLAVE_DATA + nested data */
442 return nla_total_size(sizeof(struct nlattr)) +
443 ops->get_slave_size(master_dev, dev);
444}
445
Patrick McHardy38f7b872007-06-13 12:03:51 -0700446static size_t rtnl_link_get_size(const struct net_device *dev)
447{
448 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
449 size_t size;
450
451 if (!ops)
452 return 0;
453
Thomas Graf369cf772010-11-11 15:47:59 +0000454 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
455 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700456
457 if (ops->get_size)
458 /* IFLA_INFO_DATA + nested data */
Thomas Graf369cf772010-11-11 15:47:59 +0000459 size += nla_total_size(sizeof(struct nlattr)) +
Patrick McHardy38f7b872007-06-13 12:03:51 -0700460 ops->get_size(dev);
461
462 if (ops->get_xstats_size)
Thomas Graf369cf772010-11-11 15:47:59 +0000463 /* IFLA_INFO_XSTATS */
464 size += nla_total_size(ops->get_xstats_size(dev));
Patrick McHardy38f7b872007-06-13 12:03:51 -0700465
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100466 size += rtnl_link_get_slave_info_data_size(dev);
467
Patrick McHardy38f7b872007-06-13 12:03:51 -0700468 return size;
469}
470
Thomas Graff8ff1822010-11-16 04:30:14 +0000471static LIST_HEAD(rtnl_af_ops);
472
473static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
474{
475 const struct rtnl_af_ops *ops;
476
477 list_for_each_entry(ops, &rtnl_af_ops, list) {
478 if (ops->family == family)
479 return ops;
480 }
481
482 return NULL;
483}
484
485/**
Thomas Graff8ff1822010-11-16 04:30:14 +0000486 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
487 * @ops: struct rtnl_af_ops * to register
488 *
489 * Returns 0 on success or a negative error code.
490 */
stephen hemminger3678a9d2013-12-30 10:41:32 -0800491void rtnl_af_register(struct rtnl_af_ops *ops)
Thomas Graff8ff1822010-11-16 04:30:14 +0000492{
Thomas Graff8ff1822010-11-16 04:30:14 +0000493 rtnl_lock();
stephen hemminger3678a9d2013-12-30 10:41:32 -0800494 list_add_tail(&ops->list, &rtnl_af_ops);
Thomas Graff8ff1822010-11-16 04:30:14 +0000495 rtnl_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +0000496}
497EXPORT_SYMBOL_GPL(rtnl_af_register);
498
499/**
500 * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
501 * @ops: struct rtnl_af_ops * to unregister
502 *
503 * The caller must hold the rtnl_mutex.
504 */
505void __rtnl_af_unregister(struct rtnl_af_ops *ops)
506{
507 list_del(&ops->list);
508}
509EXPORT_SYMBOL_GPL(__rtnl_af_unregister);
510
511/**
512 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
513 * @ops: struct rtnl_af_ops * to unregister
514 */
515void rtnl_af_unregister(struct rtnl_af_ops *ops)
516{
517 rtnl_lock();
518 __rtnl_af_unregister(ops);
519 rtnl_unlock();
520}
521EXPORT_SYMBOL_GPL(rtnl_af_unregister);
522
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700523static size_t rtnl_link_get_af_size(const struct net_device *dev,
524 u32 ext_filter_mask)
Thomas Graff8ff1822010-11-16 04:30:14 +0000525{
526 struct rtnl_af_ops *af_ops;
527 size_t size;
528
529 /* IFLA_AF_SPEC */
530 size = nla_total_size(sizeof(struct nlattr));
531
532 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
533 if (af_ops->get_link_af_size) {
534 /* AF_* + nested data */
535 size += nla_total_size(sizeof(struct nlattr)) +
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700536 af_ops->get_link_af_size(dev, ext_filter_mask);
Thomas Graff8ff1822010-11-16 04:30:14 +0000537 }
538 }
539
540 return size;
541}
542
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100543static bool rtnl_have_link_slave_info(const struct net_device *dev)
544{
545 struct net_device *master_dev;
546
547 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
Jiri Pirko813f0202014-01-23 19:19:21 +0100548 if (master_dev && master_dev->rtnl_link_ops)
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100549 return true;
550 return false;
551}
552
553static int rtnl_link_slave_info_fill(struct sk_buff *skb,
554 const struct net_device *dev)
555{
556 struct net_device *master_dev;
557 const struct rtnl_link_ops *ops;
558 struct nlattr *slave_data;
559 int err;
560
561 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
562 if (!master_dev)
563 return 0;
564 ops = master_dev->rtnl_link_ops;
565 if (!ops)
566 return 0;
567 if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
568 return -EMSGSIZE;
569 if (ops->fill_slave_info) {
570 slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA);
571 if (!slave_data)
572 return -EMSGSIZE;
573 err = ops->fill_slave_info(skb, master_dev, dev);
574 if (err < 0)
575 goto err_cancel_slave_data;
576 nla_nest_end(skb, slave_data);
577 }
578 return 0;
579
580err_cancel_slave_data:
581 nla_nest_cancel(skb, slave_data);
582 return err;
583}
584
585static int rtnl_link_info_fill(struct sk_buff *skb,
586 const struct net_device *dev)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700587{
588 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100589 struct nlattr *data;
590 int err;
591
592 if (!ops)
593 return 0;
594 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
595 return -EMSGSIZE;
596 if (ops->fill_xstats) {
597 err = ops->fill_xstats(skb, dev);
598 if (err < 0)
599 return err;
600 }
601 if (ops->fill_info) {
602 data = nla_nest_start(skb, IFLA_INFO_DATA);
603 if (data == NULL)
604 return -EMSGSIZE;
605 err = ops->fill_info(skb, dev);
606 if (err < 0)
607 goto err_cancel_data;
608 nla_nest_end(skb, data);
609 }
610 return 0;
611
612err_cancel_data:
613 nla_nest_cancel(skb, data);
614 return err;
615}
616
617static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
618{
619 struct nlattr *linkinfo;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700620 int err = -EMSGSIZE;
621
622 linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
623 if (linkinfo == NULL)
624 goto out;
625
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100626 err = rtnl_link_info_fill(skb, dev);
627 if (err < 0)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700628 goto err_cancel_link;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100629
630 err = rtnl_link_slave_info_fill(skb, dev);
631 if (err < 0)
632 goto err_cancel_link;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700633
634 nla_nest_end(skb, linkinfo);
635 return 0;
636
Patrick McHardy38f7b872007-06-13 12:03:51 -0700637err_cancel_link:
638 nla_nest_cancel(skb, linkinfo);
639out:
640 return err;
641}
642
Eric Dumazet95c96172012-04-15 05:58:06 +0000643int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800645 struct sock *rtnl = net->rtnl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 int err = 0;
647
Patrick McHardyac6d4392005-08-14 19:29:52 -0700648 NETLINK_CB(skb).dst_group = group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 if (echo)
650 atomic_inc(&skb->users);
651 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
652 if (echo)
653 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
654 return err;
655}
656
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800657int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
Thomas Graf2942e902006-08-15 00:30:25 -0700658{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800659 struct sock *rtnl = net->rtnl;
660
Thomas Graf2942e902006-08-15 00:30:25 -0700661 return nlmsg_unicast(rtnl, skb, pid);
662}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800663EXPORT_SYMBOL(rtnl_unicast);
Thomas Graf2942e902006-08-15 00:30:25 -0700664
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800665void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
666 struct nlmsghdr *nlh, gfp_t flags)
Thomas Graf97676b62006-08-15 00:31:41 -0700667{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800668 struct sock *rtnl = net->rtnl;
Thomas Graf97676b62006-08-15 00:31:41 -0700669 int report = 0;
670
671 if (nlh)
672 report = nlmsg_report(nlh);
673
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800674 nlmsg_notify(rtnl, skb, pid, group, report, flags);
Thomas Graf97676b62006-08-15 00:31:41 -0700675}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800676EXPORT_SYMBOL(rtnl_notify);
Thomas Graf97676b62006-08-15 00:31:41 -0700677
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800678void rtnl_set_sk_err(struct net *net, u32 group, int error)
Thomas Graf97676b62006-08-15 00:31:41 -0700679{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800680 struct sock *rtnl = net->rtnl;
681
Thomas Graf97676b62006-08-15 00:31:41 -0700682 netlink_set_err(rtnl, 0, group, error);
683}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800684EXPORT_SYMBOL(rtnl_set_sk_err);
Thomas Graf97676b62006-08-15 00:31:41 -0700685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
687{
Thomas Graf2d7202b2006-08-22 00:01:27 -0700688 struct nlattr *mx;
689 int i, valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Thomas Graf2d7202b2006-08-22 00:01:27 -0700691 mx = nla_nest_start(skb, RTA_METRICS);
692 if (mx == NULL)
693 return -ENOBUFS;
694
695 for (i = 0; i < RTAX_MAX; i++) {
696 if (metrics[i]) {
Daniel Borkmannea697632015-01-05 23:57:47 +0100697 if (i == RTAX_CC_ALGO - 1) {
698 char tmp[TCP_CA_NAME_MAX], *name;
699
700 name = tcp_ca_get_name_by_key(metrics[i], tmp);
701 if (!name)
702 continue;
703 if (nla_put_string(skb, i + 1, name))
704 goto nla_put_failure;
Daniel Borkmannc3a8d942015-08-31 15:58:47 +0200705 } else if (i == RTAX_FEATURES - 1) {
706 u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
707
Phil Sutterf8edcd12016-08-23 13:14:31 +0200708 if (!user_features)
709 continue;
Daniel Borkmannc3a8d942015-08-31 15:58:47 +0200710 BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
711 if (nla_put_u32(skb, i + 1, user_features))
712 goto nla_put_failure;
Daniel Borkmannea697632015-01-05 23:57:47 +0100713 } else {
714 if (nla_put_u32(skb, i + 1, metrics[i]))
715 goto nla_put_failure;
716 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700717 valid++;
Thomas Graf2d7202b2006-08-22 00:01:27 -0700718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
David S. Millera57d27f2006-08-22 22:20:14 -0700721 if (!valid) {
722 nla_nest_cancel(skb, mx);
723 return 0;
724 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700725
726 return nla_nest_end(skb, mx);
727
728nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700729 nla_nest_cancel(skb, mx);
730 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800732EXPORT_SYMBOL(rtnetlink_put_metrics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Thomas Grafe3703b32006-11-27 09:27:07 -0800734int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
David S. Miller87a50692012-07-10 05:06:14 -0700735 long expires, u32 error)
Thomas Grafe3703b32006-11-27 09:27:07 -0800736{
737 struct rta_cacheinfo ci = {
Eric Dumazeta399a802012-08-08 21:13:53 +0000738 .rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse),
Thomas Grafe3703b32006-11-27 09:27:07 -0800739 .rta_used = dst->__use,
740 .rta_clntref = atomic_read(&(dst->__refcnt)),
741 .rta_error = error,
742 .rta_id = id,
Thomas Grafe3703b32006-11-27 09:27:07 -0800743 };
744
Li Wei82539472012-07-29 16:01:30 +0000745 if (expires) {
746 unsigned long clock;
Thomas Grafe3703b32006-11-27 09:27:07 -0800747
Li Wei82539472012-07-29 16:01:30 +0000748 clock = jiffies_to_clock_t(abs(expires));
749 clock = min_t(unsigned long, clock, INT_MAX);
750 ci.rta_expires = (expires > 0) ? clock : -clock;
751 }
Thomas Grafe3703b32006-11-27 09:27:07 -0800752 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
753}
Thomas Grafe3703b32006-11-27 09:27:07 -0800754EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
David S. Miller93b2d4a2008-02-17 18:35:07 -0800756static void set_operstate(struct net_device *dev, unsigned char transition)
Stefan Rompfb00055a2006-03-20 17:09:11 -0800757{
758 unsigned char operstate = dev->operstate;
759
Eric Dumazete0d087a2009-11-07 01:26:17 -0800760 switch (transition) {
Stefan Rompfb00055a2006-03-20 17:09:11 -0800761 case IF_OPER_UP:
762 if ((operstate == IF_OPER_DORMANT ||
763 operstate == IF_OPER_UNKNOWN) &&
764 !netif_dormant(dev))
765 operstate = IF_OPER_UP;
766 break;
767
768 case IF_OPER_DORMANT:
769 if (operstate == IF_OPER_UP ||
770 operstate == IF_OPER_UNKNOWN)
771 operstate = IF_OPER_DORMANT;
772 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700773 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800774
775 if (dev->operstate != operstate) {
776 write_lock_bh(&dev_base_lock);
777 dev->operstate = operstate;
778 write_unlock_bh(&dev_base_lock);
David S. Miller93b2d4a2008-02-17 18:35:07 -0800779 netdev_state_change(dev);
780 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800781}
782
Jiri Bencb1beb682012-07-27 02:58:22 +0000783static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
784{
785 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
786 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
787}
788
Patrick McHardy3729d502010-02-26 06:34:54 +0000789static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
790 const struct ifinfomsg *ifm)
791{
792 unsigned int flags = ifm->ifi_flags;
793
794 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
795 if (ifm->ifi_change)
796 flags = (flags & ifm->ifi_change) |
Jiri Bencb1beb682012-07-27 02:58:22 +0000797 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
Patrick McHardy3729d502010-02-26 06:34:54 +0000798
799 return flags;
800}
801
Thomas Grafb60c5112006-08-04 23:05:34 -0700802static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000803 const struct rtnl_link_stats64 *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
Thomas Grafb60c5112006-08-04 23:05:34 -0700805 a->rx_packets = b->rx_packets;
806 a->tx_packets = b->tx_packets;
807 a->rx_bytes = b->rx_bytes;
808 a->tx_bytes = b->tx_bytes;
809 a->rx_errors = b->rx_errors;
810 a->tx_errors = b->tx_errors;
811 a->rx_dropped = b->rx_dropped;
812 a->tx_dropped = b->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Thomas Grafb60c5112006-08-04 23:05:34 -0700814 a->multicast = b->multicast;
815 a->collisions = b->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Thomas Grafb60c5112006-08-04 23:05:34 -0700817 a->rx_length_errors = b->rx_length_errors;
818 a->rx_over_errors = b->rx_over_errors;
819 a->rx_crc_errors = b->rx_crc_errors;
820 a->rx_frame_errors = b->rx_frame_errors;
821 a->rx_fifo_errors = b->rx_fifo_errors;
822 a->rx_missed_errors = b->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Thomas Grafb60c5112006-08-04 23:05:34 -0700824 a->tx_aborted_errors = b->tx_aborted_errors;
825 a->tx_carrier_errors = b->tx_carrier_errors;
826 a->tx_fifo_errors = b->tx_fifo_errors;
827 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
828 a->tx_window_errors = b->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Thomas Grafb60c5112006-08-04 23:05:34 -0700830 a->rx_compressed = b->rx_compressed;
831 a->tx_compressed = b->tx_compressed;
Jarod Wilson6e7333d2016-02-01 18:51:05 -0500832
833 a->rx_nohandler = b->rx_nohandler;
Jan Engelhardt10708f32010-03-11 09:57:29 +0000834}
835
Chris Wrightc02db8c2010-05-16 01:05:45 -0700836/* All VF info */
Greg Rose115c9b82012-02-21 16:54:48 -0500837static inline int rtnl_vfinfo_size(const struct net_device *dev,
838 u32 ext_filter_mask)
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000839{
Greg Rose115c9b82012-02-21 16:54:48 -0500840 if (dev->dev.parent && dev_is_pci(dev->dev.parent) &&
841 (ext_filter_mask & RTEXT_FILTER_VF)) {
Chris Wrightc02db8c2010-05-16 01:05:45 -0700842 int num_vfs = dev_num_vf(dev->dev.parent);
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100843 size_t size = nla_total_size(0);
Scott Feldman045de012010-05-28 03:42:43 -0700844 size += num_vfs *
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100845 (nla_total_size(0) +
846 nla_total_size(sizeof(struct ifla_vf_mac)) +
847 nla_total_size(sizeof(struct ifla_vf_vlan)) +
848 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
Moshe Shemesh79aab092016-09-22 12:11:15 +0300849 nla_total_size(MAX_VLAN_LIST_LEN *
850 sizeof(struct ifla_vf_vlan_info)) +
Sucheta Chakrabortyed616682014-05-22 09:59:05 -0400851 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100852 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
Jiri Benc945a3672014-08-08 16:44:32 +0200853 nla_total_size(sizeof(struct ifla_vf_rate)) +
Vlad Zolotarov01a3d792015-03-30 21:35:23 +0300854 nla_total_size(sizeof(struct ifla_vf_link_state)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300855 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100856 nla_total_size(0) + /* nest IFLA_VF_STATS */
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300857 /* IFLA_VF_STATS_RX_PACKETS */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200858 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300859 /* IFLA_VF_STATS_TX_PACKETS */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200860 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300861 /* IFLA_VF_STATS_RX_BYTES */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200862 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300863 /* IFLA_VF_STATS_TX_BYTES */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200864 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300865 /* IFLA_VF_STATS_BROADCAST */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200866 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300867 /* IFLA_VF_STATS_MULTICAST */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200868 nla_total_size_64bit(sizeof(__u64)) +
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +0000869 nla_total_size(sizeof(struct ifla_vf_trust)));
Chris Wrightc02db8c2010-05-16 01:05:45 -0700870 return size;
871 } else
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000872 return 0;
873}
874
David Gibsonc53864f2014-04-24 10:22:36 +1000875static size_t rtnl_port_size(const struct net_device *dev,
876 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -0700877{
878 size_t port_size = nla_total_size(4) /* PORT_VF */
879 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
880 + nla_total_size(sizeof(struct ifla_port_vsi))
881 /* PORT_VSI_TYPE */
882 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
883 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
884 + nla_total_size(1) /* PROT_VDP_REQUEST */
885 + nla_total_size(2); /* PORT_VDP_RESPONSE */
886 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
887 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
888 + port_size;
889 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
890 + port_size;
891
David Gibsonc53864f2014-04-24 10:22:36 +1000892 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
893 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -0700894 return 0;
895 if (dev_num_vf(dev->dev.parent))
896 return port_self_size + vf_ports_size +
897 vf_port_size * dev_num_vf(dev->dev.parent);
898 else
899 return port_self_size;
900}
901
Brenden Blancod1fdd912016-07-19 12:16:49 -0700902static size_t rtnl_xdp_size(const struct net_device *dev)
903{
Sabrina Dubrocab3cfaa32016-11-15 11:16:35 +0100904 size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */
905 nla_total_size(1); /* XDP_ATTACHED */
Brenden Blancod1fdd912016-07-19 12:16:49 -0700906
907 if (!dev->netdev_ops->ndo_xdp)
908 return 0;
909 else
910 return xdp_size;
911}
912
Greg Rose115c9b82012-02-21 16:54:48 -0500913static noinline size_t if_nlmsg_size(const struct net_device *dev,
914 u32 ext_filter_mask)
Thomas Graf339bf982006-11-10 14:10:15 -0800915{
916 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
917 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700918 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
Thomas Graf339bf982006-11-10 14:10:15 -0800919 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
Nicolas Dichtel270cb4d2016-04-26 10:06:16 +0200920 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
Thomas Graf339bf982006-11-10 14:10:15 -0800921 + nla_total_size(sizeof(struct rtnl_link_stats))
David S. Miller35c58452016-04-19 19:49:29 -0400922 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
Thomas Graf339bf982006-11-10 14:10:15 -0800923 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
924 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
925 + nla_total_size(4) /* IFLA_TXQLEN */
926 + nla_total_size(4) /* IFLA_WEIGHT */
927 + nla_total_size(4) /* IFLA_MTU */
928 + nla_total_size(4) /* IFLA_LINK */
929 + nla_total_size(4) /* IFLA_MASTER */
Jiri Pirko9a572472012-12-27 23:49:39 +0000930 + nla_total_size(1) /* IFLA_CARRIER */
Ben Greearedbc0bb2012-03-29 12:51:30 +0000931 + nla_total_size(4) /* IFLA_PROMISCUITY */
Jiri Pirko76ff5cc2012-07-20 02:28:48 +0000932 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
933 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
Tobias Klauser69197562016-11-30 14:30:37 +0100934 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
935 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
Thomas Graf339bf982006-11-10 14:10:15 -0800936 + nla_total_size(1) /* IFLA_OPERSTATE */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700937 + nla_total_size(1) /* IFLA_LINKMODE */
david decotigny2d3b4792014-03-29 09:48:35 -0700938 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
Nicolas Dichteld37512a2015-01-15 15:11:16 +0100939 + nla_total_size(4) /* IFLA_LINK_NETNSID */
Serhey Popovych8e231632017-06-20 14:35:23 +0300940 + nla_total_size(4) /* IFLA_GROUP */
Greg Rose115c9b82012-02-21 16:54:48 -0500941 + nla_total_size(ext_filter_mask
942 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
943 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
David Gibsonc53864f2014-04-24 10:22:36 +1000944 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
Thomas Graff8ff1822010-11-16 04:30:14 +0000945 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700946 + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
Jiri Pirko82f28412014-11-28 14:34:18 +0100947 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
Anuradha Karuppiah88d63782015-07-14 13:43:20 -0700948 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
Nicolas Dichtelc57c7a92016-03-31 18:10:31 +0200949 + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
Brenden Blancod1fdd912016-07-19 12:16:49 -0700950 + rtnl_xdp_size(dev) /* IFLA_XDP */
Anuradha Karuppiah88d63782015-07-14 13:43:20 -0700951 + nla_total_size(1); /* IFLA_PROTO_DOWN */
952
Thomas Graf339bf982006-11-10 14:10:15 -0800953}
954
Scott Feldman57b61082010-05-17 22:49:55 -0700955static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
956{
957 struct nlattr *vf_ports;
958 struct nlattr *vf_port;
959 int vf;
960 int err;
961
962 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
963 if (!vf_ports)
964 return -EMSGSIZE;
965
966 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
967 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
Scott Feldman8ca94182010-05-28 03:42:18 -0700968 if (!vf_port)
969 goto nla_put_failure;
David S. Millera6574342012-04-01 20:12:00 -0400970 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
971 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -0700972 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
Scott Feldman8ca94182010-05-28 03:42:18 -0700973 if (err == -EMSGSIZE)
974 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -0700975 if (err) {
Scott Feldman57b61082010-05-17 22:49:55 -0700976 nla_nest_cancel(skb, vf_port);
977 continue;
978 }
979 nla_nest_end(skb, vf_port);
980 }
981
982 nla_nest_end(skb, vf_ports);
983
984 return 0;
Scott Feldman8ca94182010-05-28 03:42:18 -0700985
986nla_put_failure:
987 nla_nest_cancel(skb, vf_ports);
988 return -EMSGSIZE;
Scott Feldman57b61082010-05-17 22:49:55 -0700989}
990
991static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
992{
993 struct nlattr *port_self;
994 int err;
995
996 port_self = nla_nest_start(skb, IFLA_PORT_SELF);
997 if (!port_self)
998 return -EMSGSIZE;
999
1000 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
1001 if (err) {
1002 nla_nest_cancel(skb, port_self);
Scott Feldman8ca94182010-05-28 03:42:18 -07001003 return (err == -EMSGSIZE) ? err : 0;
Scott Feldman57b61082010-05-17 22:49:55 -07001004 }
1005
1006 nla_nest_end(skb, port_self);
1007
1008 return 0;
1009}
1010
David Gibsonc53864f2014-04-24 10:22:36 +10001011static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1012 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -07001013{
1014 int err;
1015
David Gibsonc53864f2014-04-24 10:22:36 +10001016 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1017 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -07001018 return 0;
1019
1020 err = rtnl_port_self_fill(skb, dev);
1021 if (err)
1022 return err;
1023
1024 if (dev_num_vf(dev->dev.parent)) {
1025 err = rtnl_vf_ports_fill(skb, dev);
1026 if (err)
1027 return err;
1028 }
1029
1030 return 0;
1031}
1032
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001033static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
1034{
1035 int err;
Jiri Pirko02637fc2014-11-28 14:34:16 +01001036 struct netdev_phys_item_id ppid;
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001037
1038 err = dev_get_phys_port_id(dev, &ppid);
1039 if (err) {
1040 if (err == -EOPNOTSUPP)
1041 return 0;
1042 return err;
1043 }
1044
1045 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1046 return -EMSGSIZE;
1047
1048 return 0;
1049}
1050
David Aherndb24a902015-03-17 20:23:15 -06001051static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1052{
1053 char name[IFNAMSIZ];
1054 int err;
1055
1056 err = dev_get_phys_port_name(dev, name, sizeof(name));
1057 if (err) {
1058 if (err == -EOPNOTSUPP)
1059 return 0;
1060 return err;
1061 }
1062
Michal Schmidt5d8e0772017-05-04 16:48:58 +02001063 if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
David Aherndb24a902015-03-17 20:23:15 -06001064 return -EMSGSIZE;
1065
1066 return 0;
1067}
1068
Jiri Pirko82f28412014-11-28 14:34:18 +01001069static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1070{
1071 int err;
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001072 struct switchdev_attr attr = {
Ido Schimmel6ff64f62015-12-15 16:03:35 +01001073 .orig_dev = dev,
Jiri Pirko1f868392015-10-01 11:03:42 +02001074 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001075 .flags = SWITCHDEV_F_NO_RECURSE,
1076 };
Jiri Pirko82f28412014-11-28 14:34:18 +01001077
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001078 err = switchdev_port_attr_get(dev, &attr);
Jiri Pirko82f28412014-11-28 14:34:18 +01001079 if (err) {
1080 if (err == -EOPNOTSUPP)
1081 return 0;
1082 return err;
1083 }
1084
Scott Feldman42275bd2015-05-13 11:16:50 -07001085 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len,
1086 attr.u.ppid.id))
Jiri Pirko82f28412014-11-28 14:34:18 +01001087 return -EMSGSIZE;
1088
1089 return 0;
1090}
1091
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001092static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1093 struct net_device *dev)
1094{
Roopa Prabhu550bce52016-04-15 20:36:25 -07001095 struct rtnl_link_stats64 *sp;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001096 struct nlattr *attr;
1097
Nicolas Dichtel58414d32016-04-21 18:58:25 +02001098 attr = nla_reserve_64bit(skb, IFLA_STATS64,
1099 sizeof(struct rtnl_link_stats64), IFLA_PAD);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001100 if (!attr)
1101 return -EMSGSIZE;
1102
Roopa Prabhu550bce52016-04-15 20:36:25 -07001103 sp = nla_data(attr);
1104 dev_get_stats(dev, sp);
1105
1106 attr = nla_reserve(skb, IFLA_STATS,
1107 sizeof(struct rtnl_link_stats));
1108 if (!attr)
1109 return -EMSGSIZE;
1110
1111 copy_rtnl_link_stats(nla_data(attr), sp);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001112
1113 return 0;
1114}
1115
1116static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1117 struct net_device *dev,
1118 int vfs_num,
1119 struct nlattr *vfinfo)
1120{
1121 struct ifla_vf_rss_query_en vf_rss_query_en;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001122 struct nlattr *vf, *vfstats, *vfvlanlist;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001123 struct ifla_vf_link_state vf_linkstate;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001124 struct ifla_vf_vlan_info vf_vlan_info;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001125 struct ifla_vf_spoofchk vf_spoofchk;
1126 struct ifla_vf_tx_rate vf_tx_rate;
1127 struct ifla_vf_stats vf_stats;
1128 struct ifla_vf_trust vf_trust;
1129 struct ifla_vf_vlan vf_vlan;
1130 struct ifla_vf_rate vf_rate;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001131 struct ifla_vf_mac vf_mac;
1132 struct ifla_vf_info ivi;
1133
Mintz, Yuvalb5cc68e2017-06-07 21:00:33 +03001134 memset(&ivi, 0, sizeof(ivi));
1135
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001136 /* Not all SR-IOV capable drivers support the
1137 * spoofcheck and "RSS query enable" query. Preset to
1138 * -1 so the user space tool can detect that the driver
1139 * didn't report anything.
1140 */
1141 ivi.spoofchk = -1;
1142 ivi.rss_query_en = -1;
1143 ivi.trusted = -1;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001144 /* The default value for VF link state is "auto"
1145 * IFLA_VF_LINK_STATE_AUTO which equals zero
1146 */
1147 ivi.linkstate = 0;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001148 /* VLAN Protocol by default is 802.1Q */
1149 ivi.vlan_proto = htons(ETH_P_8021Q);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001150 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1151 return 0;
1152
Dan Carpenter775f4f02016-10-13 11:45:28 +03001153 memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
1154
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001155 vf_mac.vf =
1156 vf_vlan.vf =
Moshe Shemesh79aab092016-09-22 12:11:15 +03001157 vf_vlan_info.vf =
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001158 vf_rate.vf =
1159 vf_tx_rate.vf =
1160 vf_spoofchk.vf =
1161 vf_linkstate.vf =
1162 vf_rss_query_en.vf =
1163 vf_trust.vf = ivi.vf;
1164
1165 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1166 vf_vlan.vlan = ivi.vlan;
1167 vf_vlan.qos = ivi.qos;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001168 vf_vlan_info.vlan = ivi.vlan;
1169 vf_vlan_info.qos = ivi.qos;
1170 vf_vlan_info.vlan_proto = ivi.vlan_proto;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001171 vf_tx_rate.rate = ivi.max_tx_rate;
1172 vf_rate.min_tx_rate = ivi.min_tx_rate;
1173 vf_rate.max_tx_rate = ivi.max_tx_rate;
1174 vf_spoofchk.setting = ivi.spoofchk;
1175 vf_linkstate.link_state = ivi.linkstate;
1176 vf_rss_query_en.setting = ivi.rss_query_en;
1177 vf_trust.setting = ivi.trusted;
1178 vf = nla_nest_start(skb, IFLA_VF_INFO);
Moshe Shemesh79aab092016-09-22 12:11:15 +03001179 if (!vf)
1180 goto nla_put_vfinfo_failure;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001181 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1182 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1183 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1184 &vf_rate) ||
1185 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1186 &vf_tx_rate) ||
1187 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1188 &vf_spoofchk) ||
1189 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1190 &vf_linkstate) ||
1191 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1192 sizeof(vf_rss_query_en),
1193 &vf_rss_query_en) ||
1194 nla_put(skb, IFLA_VF_TRUST,
1195 sizeof(vf_trust), &vf_trust))
Moshe Shemesh79aab092016-09-22 12:11:15 +03001196 goto nla_put_vf_failure;
1197 vfvlanlist = nla_nest_start(skb, IFLA_VF_VLAN_LIST);
1198 if (!vfvlanlist)
1199 goto nla_put_vf_failure;
1200 if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
1201 &vf_vlan_info)) {
1202 nla_nest_cancel(skb, vfvlanlist);
1203 goto nla_put_vf_failure;
1204 }
1205 nla_nest_end(skb, vfvlanlist);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001206 memset(&vf_stats, 0, sizeof(vf_stats));
1207 if (dev->netdev_ops->ndo_get_vf_stats)
1208 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1209 &vf_stats);
1210 vfstats = nla_nest_start(skb, IFLA_VF_STATS);
Moshe Shemesh79aab092016-09-22 12:11:15 +03001211 if (!vfstats)
1212 goto nla_put_vf_failure;
Nicolas Dichtel343a6d82016-04-25 10:25:14 +02001213 if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1214 vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1215 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1216 vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1217 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1218 vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1219 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1220 vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1221 nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1222 vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1223 nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
Moshe Shemesh79aab092016-09-22 12:11:15 +03001224 vf_stats.multicast, IFLA_VF_STATS_PAD)) {
1225 nla_nest_cancel(skb, vfstats);
1226 goto nla_put_vf_failure;
1227 }
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001228 nla_nest_end(skb, vfstats);
1229 nla_nest_end(skb, vf);
1230 return 0;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001231
1232nla_put_vf_failure:
1233 nla_nest_cancel(skb, vf);
1234nla_put_vfinfo_failure:
1235 nla_nest_cancel(skb, vfinfo);
1236 return -EMSGSIZE;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001237}
1238
1239static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1240{
Kangjie Lu5f8e4472016-05-03 16:46:24 -04001241 struct rtnl_link_ifmap map;
1242
1243 memset(&map, 0, sizeof(map));
1244 map.mem_start = dev->mem_start;
1245 map.mem_end = dev->mem_end;
1246 map.base_addr = dev->base_addr;
1247 map.irq = dev->irq;
1248 map.dma = dev->dma;
1249 map.port = dev->if_port;
1250
Nicolas Dichtel270cb4d2016-04-26 10:06:16 +02001251 if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001252 return -EMSGSIZE;
1253
1254 return 0;
1255}
1256
Brenden Blancod1fdd912016-07-19 12:16:49 -07001257static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1258{
1259 struct netdev_xdp xdp_op = {};
1260 struct nlattr *xdp;
1261 int err;
1262
1263 if (!dev->netdev_ops->ndo_xdp)
1264 return 0;
1265 xdp = nla_nest_start(skb, IFLA_XDP);
1266 if (!xdp)
1267 return -EMSGSIZE;
1268 xdp_op.command = XDP_QUERY_PROG;
1269 err = dev->netdev_ops->ndo_xdp(dev, &xdp_op);
1270 if (err)
1271 goto err_cancel;
1272 err = nla_put_u8(skb, IFLA_XDP_ATTACHED, xdp_op.prog_attached);
1273 if (err)
1274 goto err_cancel;
1275
1276 nla_nest_end(skb, xdp);
1277 return 0;
1278
1279err_cancel:
1280 nla_nest_cancel(skb, xdp);
1281 return err;
1282}
1283
Thomas Grafb60c5112006-08-04 23:05:34 -07001284static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
Patrick McHardy575c3e22007-05-22 17:00:49 -07001285 int type, u32 pid, u32 seq, u32 change,
Greg Rose115c9b82012-02-21 16:54:48 -05001286 unsigned int flags, u32 ext_filter_mask)
Thomas Grafb60c5112006-08-04 23:05:34 -07001287{
1288 struct ifinfomsg *ifm;
1289 struct nlmsghdr *nlh;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001290 struct nlattr *af_spec;
Thomas Graff8ff1822010-11-16 04:30:14 +00001291 struct rtnl_af_ops *af_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00001292 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Thomas Grafb60c5112006-08-04 23:05:34 -07001293
Eric Dumazet2907c352011-05-25 07:34:04 +00001294 ASSERT_RTNL();
Thomas Grafb60c5112006-08-04 23:05:34 -07001295 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1296 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08001297 return -EMSGSIZE;
Thomas Grafb60c5112006-08-04 23:05:34 -07001298
1299 ifm = nlmsg_data(nlh);
1300 ifm->ifi_family = AF_UNSPEC;
1301 ifm->__ifi_pad = 0;
1302 ifm->ifi_type = dev->type;
1303 ifm->ifi_index = dev->ifindex;
1304 ifm->ifi_flags = dev_get_flags(dev);
1305 ifm->ifi_change = change;
1306
David S. Millera6574342012-04-01 20:12:00 -04001307 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1308 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1309 nla_put_u8(skb, IFLA_OPERSTATE,
1310 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1311 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1312 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1313 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
Ben Greearedbc0bb2012-03-29 12:51:30 +00001314 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001315 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
Eric Dumazetc70ce022016-03-21 09:55:10 -07001316 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1317 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001318#ifdef CONFIG_RPS
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001319 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001320#endif
Nicolas Dichtela54acb32015-04-02 17:07:00 +02001321 (dev->ifindex != dev_get_iflink(dev) &&
1322 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00001323 (upper_dev &&
1324 nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
Jiri Pirko9a572472012-12-27 23:49:39 +00001325 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
David S. Millera6574342012-04-01 20:12:00 -04001326 (dev->qdisc &&
1327 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1328 (dev->ifalias &&
david decotigny2d3b4792014-03-29 09:48:35 -07001329 nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) ||
1330 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001331 atomic_read(&dev->carrier_changes)) ||
1332 nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
David S. Millera6574342012-04-01 20:12:00 -04001333 goto nla_put_failure;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001334
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001335 if (rtnl_fill_link_ifmap(skb, dev))
1336 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338 if (dev->addr_len) {
David S. Millera6574342012-04-01 20:12:00 -04001339 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1340 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1341 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 }
1343
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001344 if (rtnl_phys_port_id_fill(skb, dev))
1345 goto nla_put_failure;
1346
David Aherndb24a902015-03-17 20:23:15 -06001347 if (rtnl_phys_port_name_fill(skb, dev))
1348 goto nla_put_failure;
1349
Jiri Pirko82f28412014-11-28 14:34:18 +01001350 if (rtnl_phys_switch_id_fill(skb, dev))
1351 goto nla_put_failure;
1352
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001353 if (rtnl_fill_stats(skb, dev))
Pavel Emelyanov96e74082008-05-21 14:12:46 -07001354 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
David S. Millera6574342012-04-01 20:12:00 -04001356 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
1357 nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
1358 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001359
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001360 if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent &&
1361 ext_filter_mask & RTEXT_FILTER_VF) {
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001362 int i;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001363 struct nlattr *vfinfo;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001364 int num_vfs = dev_num_vf(dev->dev.parent);
1365
Chris Wrightc02db8c2010-05-16 01:05:45 -07001366 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1367 if (!vfinfo)
1368 goto nla_put_failure;
1369 for (i = 0; i < num_vfs; i++) {
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001370 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
Chris Wrightc02db8c2010-05-16 01:05:45 -07001371 goto nla_put_failure;
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001372 }
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001373
Chris Wrightc02db8c2010-05-16 01:05:45 -07001374 nla_nest_end(skb, vfinfo);
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001375 }
Scott Feldman57b61082010-05-17 22:49:55 -07001376
David Gibsonc53864f2014-04-24 10:22:36 +10001377 if (rtnl_port_fill(skb, dev, ext_filter_mask))
Scott Feldman57b61082010-05-17 22:49:55 -07001378 goto nla_put_failure;
1379
Brenden Blancod1fdd912016-07-19 12:16:49 -07001380 if (rtnl_xdp_fill(skb, dev))
1381 goto nla_put_failure;
1382
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001383 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07001384 if (rtnl_link_fill(skb, dev) < 0)
1385 goto nla_put_failure;
1386 }
1387
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001388 if (dev->rtnl_link_ops &&
1389 dev->rtnl_link_ops->get_link_net) {
1390 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1391
1392 if (!net_eq(dev_net(dev), link_net)) {
Nicolas Dichtel7a0877d2015-05-07 11:02:49 +02001393 int id = peernet2id_alloc(dev_net(dev), link_net);
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001394
1395 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1396 goto nla_put_failure;
1397 }
1398 }
1399
Thomas Graff8ff1822010-11-16 04:30:14 +00001400 if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1401 goto nla_put_failure;
1402
1403 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1404 if (af_ops->fill_link_af) {
1405 struct nlattr *af;
1406 int err;
1407
1408 if (!(af = nla_nest_start(skb, af_ops->family)))
1409 goto nla_put_failure;
1410
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04001411 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
Thomas Graff8ff1822010-11-16 04:30:14 +00001412
1413 /*
1414 * Caller may return ENODATA to indicate that there
1415 * was no data to be dumped. This is not an error, it
1416 * means we should trim the attribute header and
1417 * continue.
1418 */
1419 if (err == -ENODATA)
1420 nla_nest_cancel(skb, af);
1421 else if (err < 0)
1422 goto nla_put_failure;
1423
1424 nla_nest_end(skb, af);
1425 }
1426 }
1427
1428 nla_nest_end(skb, af_spec);
1429
Johannes Berg053c0952015-01-16 22:09:00 +01001430 nlmsg_end(skb, nlh);
1431 return 0;
Thomas Grafb60c5112006-08-04 23:05:34 -07001432
1433nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001434 nlmsg_cancel(skb, nlh);
1435 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436}
1437
Jiri Pirkof7b12602014-02-18 20:53:18 +01001438static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1439 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1440 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1441 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1442 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1443 [IFLA_MTU] = { .type = NLA_U32 },
1444 [IFLA_LINK] = { .type = NLA_U32 },
1445 [IFLA_MASTER] = { .type = NLA_U32 },
1446 [IFLA_CARRIER] = { .type = NLA_U8 },
1447 [IFLA_TXQLEN] = { .type = NLA_U32 },
1448 [IFLA_WEIGHT] = { .type = NLA_U32 },
1449 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1450 [IFLA_LINKMODE] = { .type = NLA_U8 },
1451 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1452 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1453 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
1454 [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 },
1455 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1456 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1457 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1458 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1459 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1460 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1461 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1462 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
Jiri Pirko02637fc2014-11-28 14:34:16 +01001463 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
david decotigny2d3b4792014-03-29 09:48:35 -07001464 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
Jiri Pirko82f28412014-11-28 14:34:18 +01001465 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
Nicolas Dichtel317f4812015-01-15 15:11:18 +01001466 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001467 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001468 [IFLA_XDP] = { .type = NLA_NESTED },
Serhey Popovych8e231632017-06-20 14:35:23 +03001469 [IFLA_GROUP] = { .type = NLA_U32 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001470};
1471
1472static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1473 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1474 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1475 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1476 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1477};
1478
Jiri Pirkof7b12602014-02-18 20:53:18 +01001479static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
Daniel Borkmann364d5712015-02-05 18:44:04 +01001480 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1481 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
Moshe Shemesh79aab092016-09-22 12:11:15 +03001482 [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED },
Daniel Borkmann364d5712015-02-05 18:44:04 +01001483 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1484 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1485 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1486 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001487 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001488 [IFLA_VF_STATS] = { .type = NLA_NESTED },
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00001489 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
Eli Cohencc8e27c2016-03-11 22:58:34 +02001490 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1491 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001492};
1493
Jiri Pirkof7b12602014-02-18 20:53:18 +01001494static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1495 [IFLA_PORT_VF] = { .type = NLA_U32 },
1496 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1497 .len = PORT_PROFILE_MAX },
1498 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1499 .len = sizeof(struct ifla_port_vsi)},
1500 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1501 .len = PORT_UUID_MAX },
1502 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1503 .len = PORT_UUID_MAX },
1504 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1505 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
1506};
1507
Brenden Blancod1fdd912016-07-19 12:16:49 -07001508static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
1509 [IFLA_XDP_FD] = { .type = NLA_S32 },
1510 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 },
1511};
1512
David Aherndc599f72016-02-02 08:17:07 -08001513static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1514{
1515 const struct rtnl_link_ops *ops = NULL;
1516 struct nlattr *linfo[IFLA_INFO_MAX + 1];
1517
1518 if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla, ifla_info_policy) < 0)
1519 return NULL;
1520
1521 if (linfo[IFLA_INFO_KIND]) {
1522 char kind[MODULE_NAME_LEN];
1523
1524 nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1525 ops = rtnl_link_ops_get(kind);
1526 }
1527
1528 return ops;
1529}
1530
1531static bool link_master_filtered(struct net_device *dev, int master_idx)
1532{
1533 struct net_device *master;
1534
1535 if (!master_idx)
1536 return false;
1537
1538 master = netdev_master_upper_dev_get(dev);
1539 if (!master || master->ifindex != master_idx)
1540 return true;
1541
1542 return false;
1543}
1544
1545static bool link_kind_filtered(const struct net_device *dev,
1546 const struct rtnl_link_ops *kind_ops)
1547{
1548 if (kind_ops && dev->rtnl_link_ops != kind_ops)
1549 return true;
1550
1551 return false;
1552}
1553
1554static bool link_dump_filtered(struct net_device *dev,
1555 int master_idx,
1556 const struct rtnl_link_ops *kind_ops)
1557{
1558 if (link_master_filtered(dev, master_idx) ||
1559 link_kind_filtered(dev, kind_ops))
1560 return true;
1561
1562 return false;
1563}
1564
Thomas Grafb60c5112006-08-04 23:05:34 -07001565static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001567 struct net *net = sock_net(skb->sk);
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001568 int h, s_h;
1569 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 struct net_device *dev;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001571 struct hlist_head *head;
Greg Rose115c9b82012-02-21 16:54:48 -05001572 struct nlattr *tb[IFLA_MAX+1];
1573 u32 ext_filter_mask = 0;
David Aherndc599f72016-02-02 08:17:07 -08001574 const struct rtnl_link_ops *kind_ops = NULL;
1575 unsigned int flags = NLM_F_MULTI;
1576 int master_idx = 0;
David Gibson973462b2014-04-24 10:22:35 +10001577 int err;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001578 int hdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001580 s_h = cb->args[0];
1581 s_idx = cb->args[1];
1582
Thomas Graf4e985ad2011-06-21 03:11:20 +00001583 cb->seq = net->dev_base_seq;
1584
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001585 /* A hack to preserve kernel<->userspace interface.
1586 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1587 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1588 * what iproute2 < v3.9.0 used.
1589 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1590 * attribute, its netlink message is shorter than struct ifinfomsg.
1591 */
1592 hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1593 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1594
1595 if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
Greg Rose115c9b82012-02-21 16:54:48 -05001596
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001597 if (tb[IFLA_EXT_MASK])
1598 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
David Aherndc599f72016-02-02 08:17:07 -08001599
1600 if (tb[IFLA_MASTER])
1601 master_idx = nla_get_u32(tb[IFLA_MASTER]);
1602
1603 if (tb[IFLA_LINKINFO])
1604 kind_ops = linkinfo_to_kind_ops(tb[IFLA_LINKINFO]);
1605
1606 if (master_idx || kind_ops)
1607 flags |= NLM_F_DUMP_FILTERED;
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001608 }
Greg Rose115c9b82012-02-21 16:54:48 -05001609
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001610 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1611 idx = 0;
1612 head = &net->dev_index_head[h];
Eric Dumazetcac5e652015-02-27 09:42:50 -08001613 hlist_for_each_entry(dev, head, index_hlist) {
David Aherndc599f72016-02-02 08:17:07 -08001614 if (link_dump_filtered(dev, master_idx, kind_ops))
Zhang Shengju3f0ae052016-11-19 23:28:32 +08001615 goto cont;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001616 if (idx < s_idx)
1617 goto cont;
David Gibson973462b2014-04-24 10:22:35 +10001618 err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
1619 NETLINK_CB(cb->skb).portid,
1620 cb->nlh->nlmsg_seq, 0,
David Aherndc599f72016-02-02 08:17:07 -08001621 flags,
David Gibson973462b2014-04-24 10:22:35 +10001622 ext_filter_mask);
David Gibson973462b2014-04-24 10:22:35 +10001623
David Ahern68647612017-05-15 23:19:17 -07001624 if (err < 0) {
1625 if (likely(skb->len))
1626 goto out;
1627
1628 goto out_err;
1629 }
Thomas Graf4e985ad2011-06-21 03:11:20 +00001630
1631 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Pavel Emelianov7562f872007-05-03 15:13:45 -07001632cont:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001633 idx++;
1634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 }
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001636out:
David Ahern68647612017-05-15 23:19:17 -07001637 err = skb->len;
1638out_err:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001639 cb->args[1] = idx;
1640 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
David Ahern68647612017-05-15 23:19:17 -07001642 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643}
1644
Jiri Pirkof7b12602014-02-18 20:53:18 +01001645int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len)
1646{
1647 return nla_parse(tb, IFLA_MAX, head, len, ifla_policy);
1648}
1649EXPORT_SYMBOL(rtnl_nla_parse_ifla);
Scott Feldman57b61082010-05-17 22:49:55 -07001650
Eric W. Biederman81adee42009-11-08 00:53:51 -08001651struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1652{
1653 struct net *net;
1654 /* Examine the link attributes and figure out which
1655 * network namespace we are talking about.
1656 */
1657 if (tb[IFLA_NET_NS_PID])
1658 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001659 else if (tb[IFLA_NET_NS_FD])
1660 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
Eric W. Biederman81adee42009-11-08 00:53:51 -08001661 else
1662 net = get_net(src_net);
1663 return net;
1664}
1665EXPORT_SYMBOL(rtnl_link_get_net);
1666
Thomas Graf1840bb12008-02-23 19:54:36 -08001667static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1668{
1669 if (dev) {
1670 if (tb[IFLA_ADDRESS] &&
1671 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1672 return -EINVAL;
1673
1674 if (tb[IFLA_BROADCAST] &&
1675 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1676 return -EINVAL;
1677 }
1678
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001679 if (tb[IFLA_AF_SPEC]) {
1680 struct nlattr *af;
1681 int rem, err;
1682
1683 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1684 const struct rtnl_af_ops *af_ops;
1685
1686 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1687 return -EAFNOSUPPORT;
1688
1689 if (!af_ops->set_link_af)
1690 return -EOPNOTSUPP;
1691
1692 if (af_ops->validate_link_af) {
Kurt Van Dijck6d3a9a62011-01-26 04:55:24 +00001693 err = af_ops->validate_link_af(dev, af);
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001694 if (err < 0)
1695 return err;
1696 }
1697 }
1698 }
1699
Thomas Graf1840bb12008-02-23 19:54:36 -08001700 return 0;
1701}
1702
Eli Cohencc8e27c2016-03-11 22:58:34 +02001703static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
1704 int guid_type)
1705{
1706 const struct net_device_ops *ops = dev->netdev_ops;
1707
1708 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
1709}
1710
1711static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
1712{
1713 if (dev->type != ARPHRD_INFINIBAND)
1714 return -EOPNOTSUPP;
1715
1716 return handle_infiniband_guid(dev, ivt, guid_type);
1717}
1718
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001719static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
Chris Wrightc02db8c2010-05-16 01:05:45 -07001720{
Chris Wrightc02db8c2010-05-16 01:05:45 -07001721 const struct net_device_ops *ops = dev->netdev_ops;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001722 int err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001723
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001724 if (tb[IFLA_VF_MAC]) {
1725 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001726
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001727 err = -EOPNOTSUPP;
1728 if (ops->ndo_set_vf_mac)
1729 err = ops->ndo_set_vf_mac(dev, ivm->vf,
1730 ivm->mac);
1731 if (err < 0)
1732 return err;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001733 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001734
1735 if (tb[IFLA_VF_VLAN]) {
1736 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
1737
1738 err = -EOPNOTSUPP;
1739 if (ops->ndo_set_vf_vlan)
1740 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
Moshe Shemesh79aab092016-09-22 12:11:15 +03001741 ivv->qos,
1742 htons(ETH_P_8021Q));
1743 if (err < 0)
1744 return err;
1745 }
1746
1747 if (tb[IFLA_VF_VLAN_LIST]) {
1748 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
1749 struct nlattr *attr;
1750 int rem, len = 0;
1751
1752 err = -EOPNOTSUPP;
1753 if (!ops->ndo_set_vf_vlan)
1754 return err;
1755
1756 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
1757 if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
1758 nla_len(attr) < NLA_HDRLEN) {
1759 return -EINVAL;
1760 }
1761 if (len >= MAX_VLAN_LIST_LEN)
1762 return -EOPNOTSUPP;
1763 ivvl[len] = nla_data(attr);
1764
1765 len++;
1766 }
Arnd Bergmannfa34cd92016-09-30 18:13:49 +02001767 if (len == 0)
1768 return -EINVAL;
1769
Moshe Shemesh79aab092016-09-22 12:11:15 +03001770 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
1771 ivvl[0]->qos, ivvl[0]->vlan_proto);
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001772 if (err < 0)
1773 return err;
1774 }
1775
1776 if (tb[IFLA_VF_TX_RATE]) {
1777 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
1778 struct ifla_vf_info ivf;
1779
1780 err = -EOPNOTSUPP;
1781 if (ops->ndo_get_vf_config)
1782 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
1783 if (err < 0)
1784 return err;
1785
1786 err = -EOPNOTSUPP;
1787 if (ops->ndo_set_vf_rate)
1788 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1789 ivf.min_tx_rate,
1790 ivt->rate);
1791 if (err < 0)
1792 return err;
1793 }
1794
1795 if (tb[IFLA_VF_RATE]) {
1796 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
1797
1798 err = -EOPNOTSUPP;
1799 if (ops->ndo_set_vf_rate)
1800 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1801 ivt->min_tx_rate,
1802 ivt->max_tx_rate);
1803 if (err < 0)
1804 return err;
1805 }
1806
1807 if (tb[IFLA_VF_SPOOFCHK]) {
1808 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
1809
1810 err = -EOPNOTSUPP;
1811 if (ops->ndo_set_vf_spoofchk)
1812 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
1813 ivs->setting);
1814 if (err < 0)
1815 return err;
1816 }
1817
1818 if (tb[IFLA_VF_LINK_STATE]) {
1819 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
1820
1821 err = -EOPNOTSUPP;
1822 if (ops->ndo_set_vf_link_state)
1823 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
1824 ivl->link_state);
1825 if (err < 0)
1826 return err;
1827 }
1828
1829 if (tb[IFLA_VF_RSS_QUERY_EN]) {
1830 struct ifla_vf_rss_query_en *ivrssq_en;
1831
1832 err = -EOPNOTSUPP;
1833 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
1834 if (ops->ndo_set_vf_rss_query_en)
1835 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
1836 ivrssq_en->setting);
1837 if (err < 0)
1838 return err;
1839 }
1840
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00001841 if (tb[IFLA_VF_TRUST]) {
1842 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
1843
1844 err = -EOPNOTSUPP;
1845 if (ops->ndo_set_vf_trust)
1846 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
1847 if (err < 0)
1848 return err;
1849 }
1850
Eli Cohencc8e27c2016-03-11 22:58:34 +02001851 if (tb[IFLA_VF_IB_NODE_GUID]) {
1852 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
1853
1854 if (!ops->ndo_set_vf_guid)
1855 return -EOPNOTSUPP;
1856
1857 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
1858 }
1859
1860 if (tb[IFLA_VF_IB_PORT_GUID]) {
1861 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
1862
1863 if (!ops->ndo_set_vf_guid)
1864 return -EOPNOTSUPP;
1865
1866 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
1867 }
1868
Chris Wrightc02db8c2010-05-16 01:05:45 -07001869 return err;
1870}
1871
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001872static int do_set_master(struct net_device *dev, int ifindex)
1873{
Jiri Pirko898e5062013-01-03 22:48:52 +00001874 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001875 const struct net_device_ops *ops;
1876 int err;
1877
Jiri Pirko898e5062013-01-03 22:48:52 +00001878 if (upper_dev) {
1879 if (upper_dev->ifindex == ifindex)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001880 return 0;
Jiri Pirko898e5062013-01-03 22:48:52 +00001881 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001882 if (ops->ndo_del_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001883 err = ops->ndo_del_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001884 if (err)
1885 return err;
1886 } else {
1887 return -EOPNOTSUPP;
1888 }
1889 }
1890
1891 if (ifindex) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001892 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
1893 if (!upper_dev)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001894 return -EINVAL;
Jiri Pirko898e5062013-01-03 22:48:52 +00001895 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001896 if (ops->ndo_add_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001897 err = ops->ndo_add_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001898 if (err)
1899 return err;
1900 } else {
1901 return -EOPNOTSUPP;
1902 }
1903 }
1904 return 0;
1905}
1906
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001907#define DO_SETLINK_MODIFIED 0x01
Nicolas Dichtelba998902014-09-01 16:07:29 +02001908/* notify flag means notify + modified. */
1909#define DO_SETLINK_NOTIFY 0x03
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001910static int do_setlink(const struct sk_buff *skb,
1911 struct net_device *dev, struct ifinfomsg *ifm,
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001912 struct nlattr **tb, char *ifname, int status)
Patrick McHardy0157f602007-06-13 12:03:36 -07001913{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001914 const struct net_device_ops *ops = dev->netdev_ops;
Patrick McHardy0157f602007-06-13 12:03:36 -07001915 int err;
1916
Eric Dumazetc5915362018-06-05 09:25:19 -07001917 err = validate_linkmsg(dev, tb);
1918 if (err < 0)
1919 return err;
1920
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001921 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
Eric W. Biederman81adee42009-11-08 00:53:51 -08001922 struct net *net = rtnl_link_get_net(dev_net(dev), tb);
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001923 if (IS_ERR(net)) {
1924 err = PTR_ERR(net);
1925 goto errout;
1926 }
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001927 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
Nicolas Dichtele0ebde02014-11-27 10:16:15 +01001928 put_net(net);
Eric W. Biedermanb51642f2012-11-16 03:03:11 +00001929 err = -EPERM;
1930 goto errout;
1931 }
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001932 err = dev_change_net_namespace(dev, net, ifname);
1933 put_net(net);
1934 if (err)
1935 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001936 status |= DO_SETLINK_MODIFIED;
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001937 }
1938
Patrick McHardy0157f602007-06-13 12:03:36 -07001939 if (tb[IFLA_MAP]) {
1940 struct rtnl_link_ifmap *u_map;
1941 struct ifmap k_map;
1942
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001943 if (!ops->ndo_set_config) {
Patrick McHardy0157f602007-06-13 12:03:36 -07001944 err = -EOPNOTSUPP;
1945 goto errout;
1946 }
1947
1948 if (!netif_device_present(dev)) {
1949 err = -ENODEV;
1950 goto errout;
1951 }
1952
1953 u_map = nla_data(tb[IFLA_MAP]);
1954 k_map.mem_start = (unsigned long) u_map->mem_start;
1955 k_map.mem_end = (unsigned long) u_map->mem_end;
1956 k_map.base_addr = (unsigned short) u_map->base_addr;
1957 k_map.irq = (unsigned char) u_map->irq;
1958 k_map.dma = (unsigned char) u_map->dma;
1959 k_map.port = (unsigned char) u_map->port;
1960
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001961 err = ops->ndo_set_config(dev, &k_map);
Patrick McHardy0157f602007-06-13 12:03:36 -07001962 if (err < 0)
1963 goto errout;
1964
Nicolas Dichtelba998902014-09-01 16:07:29 +02001965 status |= DO_SETLINK_NOTIFY;
Patrick McHardy0157f602007-06-13 12:03:36 -07001966 }
1967
1968 if (tb[IFLA_ADDRESS]) {
1969 struct sockaddr *sa;
1970 int len;
1971
WANG Congcda2bc92017-07-20 11:27:57 -07001972 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
1973 sizeof(*sa));
Patrick McHardy0157f602007-06-13 12:03:36 -07001974 sa = kmalloc(len, GFP_KERNEL);
1975 if (!sa) {
1976 err = -ENOMEM;
1977 goto errout;
1978 }
1979 sa->sa_family = dev->type;
1980 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
1981 dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00001982 err = dev_set_mac_address(dev, sa);
Patrick McHardy0157f602007-06-13 12:03:36 -07001983 kfree(sa);
1984 if (err)
1985 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001986 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07001987 }
1988
1989 if (tb[IFLA_MTU]) {
1990 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1991 if (err < 0)
1992 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001993 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07001994 }
1995
Vlad Dogarucbda10f2011-01-13 23:38:30 +00001996 if (tb[IFLA_GROUP]) {
1997 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Nicolas Dichtelba998902014-09-01 16:07:29 +02001998 status |= DO_SETLINK_NOTIFY;
Vlad Dogarucbda10f2011-01-13 23:38:30 +00001999 }
2000
Patrick McHardy0157f602007-06-13 12:03:36 -07002001 /*
2002 * Interface selected by interface index but interface
2003 * name provided implies that a name change has been
2004 * requested.
2005 */
2006 if (ifm->ifi_index > 0 && ifname[0]) {
2007 err = dev_change_name(dev, ifname);
2008 if (err < 0)
2009 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002010 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002011 }
2012
Stephen Hemminger0b815a12008-09-22 21:28:11 -07002013 if (tb[IFLA_IFALIAS]) {
2014 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2015 nla_len(tb[IFLA_IFALIAS]));
2016 if (err < 0)
2017 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002018 status |= DO_SETLINK_NOTIFY;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07002019 }
2020
Patrick McHardy0157f602007-06-13 12:03:36 -07002021 if (tb[IFLA_BROADCAST]) {
2022 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00002023 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Patrick McHardy0157f602007-06-13 12:03:36 -07002024 }
2025
2026 if (ifm->ifi_flags || ifm->ifi_change) {
Patrick McHardy3729d502010-02-26 06:34:54 +00002027 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
Johannes Berg5f9021c2008-11-16 23:20:31 -08002028 if (err < 0)
2029 goto errout;
Patrick McHardy0157f602007-06-13 12:03:36 -07002030 }
2031
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002032 if (tb[IFLA_MASTER]) {
2033 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
2034 if (err)
2035 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002036 status |= DO_SETLINK_MODIFIED;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002037 }
2038
Jiri Pirko9a572472012-12-27 23:49:39 +00002039 if (tb[IFLA_CARRIER]) {
2040 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2041 if (err)
2042 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002043 status |= DO_SETLINK_MODIFIED;
Jiri Pirko9a572472012-12-27 23:49:39 +00002044 }
2045
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002046 if (tb[IFLA_TXQLEN]) {
2047 unsigned long value = nla_get_u32(tb[IFLA_TXQLEN]);
Jason Wang08294a22016-06-30 14:45:35 +08002048 unsigned long orig_len = dev->tx_queue_len;
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002049
Jason Wang08294a22016-06-30 14:45:35 +08002050 if (dev->tx_queue_len ^ value) {
2051 dev->tx_queue_len = value;
2052 err = call_netdevice_notifiers(
2053 NETDEV_CHANGE_TX_QUEUE_LEN, dev);
2054 err = notifier_to_errno(err);
2055 if (err) {
2056 dev->tx_queue_len = orig_len;
2057 goto errout;
2058 }
Nicolas Dichtelba998902014-09-01 16:07:29 +02002059 status |= DO_SETLINK_NOTIFY;
Jason Wang08294a22016-06-30 14:45:35 +08002060 }
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002061 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002062
Patrick McHardy0157f602007-06-13 12:03:36 -07002063 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002064 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Patrick McHardy0157f602007-06-13 12:03:36 -07002065
2066 if (tb[IFLA_LINKMODE]) {
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002067 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2068
David S. Miller93b2d4a2008-02-17 18:35:07 -08002069 write_lock_bh(&dev_base_lock);
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002070 if (dev->link_mode ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02002071 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002072 dev->link_mode = value;
David S. Miller93b2d4a2008-02-17 18:35:07 -08002073 write_unlock_bh(&dev_base_lock);
Patrick McHardy0157f602007-06-13 12:03:36 -07002074 }
2075
Chris Wrightc02db8c2010-05-16 01:05:45 -07002076 if (tb[IFLA_VFINFO_LIST]) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002077 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
Chris Wrightc02db8c2010-05-16 01:05:45 -07002078 struct nlattr *attr;
2079 int rem;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002080
Chris Wrightc02db8c2010-05-16 01:05:45 -07002081 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002082 if (nla_type(attr) != IFLA_VF_INFO ||
2083 nla_len(attr) < NLA_HDRLEN) {
David Howells253683b2010-05-21 02:25:27 +00002084 err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002085 goto errout;
David Howells253683b2010-05-21 02:25:27 +00002086 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002087 err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr,
2088 ifla_vf_policy);
2089 if (err < 0)
2090 goto errout;
2091 err = do_setvfinfo(dev, vfinfo);
Chris Wrightc02db8c2010-05-16 01:05:45 -07002092 if (err < 0)
2093 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002094 status |= DO_SETLINK_NOTIFY;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002095 }
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00002096 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002097 err = 0;
2098
Scott Feldman57b61082010-05-17 22:49:55 -07002099 if (tb[IFLA_VF_PORTS]) {
2100 struct nlattr *port[IFLA_PORT_MAX+1];
2101 struct nlattr *attr;
2102 int vf;
2103 int rem;
2104
2105 err = -EOPNOTSUPP;
2106 if (!ops->ndo_set_vf_port)
2107 goto errout;
2108
2109 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
Daniel Borkmann035d2102015-07-13 00:06:02 +02002110 if (nla_type(attr) != IFLA_VF_PORT ||
2111 nla_len(attr) < NLA_HDRLEN) {
2112 err = -EINVAL;
2113 goto errout;
2114 }
2115 err = nla_parse_nested(port, IFLA_PORT_MAX, attr,
2116 ifla_port_policy);
Scott Feldman57b61082010-05-17 22:49:55 -07002117 if (err < 0)
2118 goto errout;
2119 if (!port[IFLA_PORT_VF]) {
2120 err = -EOPNOTSUPP;
2121 goto errout;
2122 }
2123 vf = nla_get_u32(port[IFLA_PORT_VF]);
2124 err = ops->ndo_set_vf_port(dev, vf, port);
2125 if (err < 0)
2126 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002127 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07002128 }
2129 }
2130 err = 0;
2131
2132 if (tb[IFLA_PORT_SELF]) {
2133 struct nlattr *port[IFLA_PORT_MAX+1];
2134
2135 err = nla_parse_nested(port, IFLA_PORT_MAX,
2136 tb[IFLA_PORT_SELF], ifla_port_policy);
2137 if (err < 0)
2138 goto errout;
2139
2140 err = -EOPNOTSUPP;
2141 if (ops->ndo_set_vf_port)
2142 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
2143 if (err < 0)
2144 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002145 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07002146 }
Thomas Graff8ff1822010-11-16 04:30:14 +00002147
2148 if (tb[IFLA_AF_SPEC]) {
2149 struct nlattr *af;
2150 int rem;
2151
2152 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2153 const struct rtnl_af_ops *af_ops;
2154
2155 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002156 BUG();
Thomas Graff8ff1822010-11-16 04:30:14 +00002157
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002158 err = af_ops->set_link_af(dev, af);
Thomas Graff8ff1822010-11-16 04:30:14 +00002159 if (err < 0)
2160 goto errout;
2161
Nicolas Dichtelba998902014-09-01 16:07:29 +02002162 status |= DO_SETLINK_NOTIFY;
Thomas Graff8ff1822010-11-16 04:30:14 +00002163 }
2164 }
Scott Feldman57b61082010-05-17 22:49:55 -07002165 err = 0;
2166
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07002167 if (tb[IFLA_PROTO_DOWN]) {
2168 err = dev_change_proto_down(dev,
2169 nla_get_u8(tb[IFLA_PROTO_DOWN]));
2170 if (err)
2171 goto errout;
2172 status |= DO_SETLINK_NOTIFY;
2173 }
2174
Brenden Blancod1fdd912016-07-19 12:16:49 -07002175 if (tb[IFLA_XDP]) {
2176 struct nlattr *xdp[IFLA_XDP_MAX + 1];
2177
2178 err = nla_parse_nested(xdp, IFLA_XDP_MAX, tb[IFLA_XDP],
2179 ifla_xdp_policy);
2180 if (err < 0)
2181 goto errout;
2182
Brenden Blanco262d8622016-07-20 17:22:34 -07002183 if (xdp[IFLA_XDP_ATTACHED]) {
2184 err = -EINVAL;
2185 goto errout;
2186 }
Brenden Blancod1fdd912016-07-19 12:16:49 -07002187 if (xdp[IFLA_XDP_FD]) {
2188 err = dev_change_xdp_fd(dev,
2189 nla_get_s32(xdp[IFLA_XDP_FD]));
2190 if (err)
2191 goto errout;
2192 status |= DO_SETLINK_NOTIFY;
2193 }
2194 }
2195
Patrick McHardy0157f602007-06-13 12:03:36 -07002196errout:
Nicolas Dichtelba998902014-09-01 16:07:29 +02002197 if (status & DO_SETLINK_MODIFIED) {
2198 if (status & DO_SETLINK_NOTIFY)
2199 netdev_state_change(dev);
2200
2201 if (err < 0)
2202 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",
2203 dev->name);
2204 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002205
Patrick McHardy0157f602007-06-13 12:03:36 -07002206 return err;
2207}
2208
Thomas Graf661d2962013-03-21 07:45:29 +00002209static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002211 struct net *net = sock_net(skb->sk);
Thomas Grafda5e0492006-08-10 21:17:37 -07002212 struct ifinfomsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 struct net_device *dev;
Patrick McHardy0157f602007-06-13 12:03:36 -07002214 int err;
Thomas Grafda5e0492006-08-10 21:17:37 -07002215 struct nlattr *tb[IFLA_MAX+1];
2216 char ifname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
Thomas Grafda5e0492006-08-10 21:17:37 -07002218 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2219 if (err < 0)
2220 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
Thomas Graf5176f912006-08-26 20:13:18 -07002222 if (tb[IFLA_IFNAME])
2223 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
Patrick McHardy78e5b8912006-09-13 20:35:36 -07002224 else
2225 ifname[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 err = -EINVAL;
Thomas Grafda5e0492006-08-10 21:17:37 -07002228 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -07002229 if (ifm->ifi_index > 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00002230 dev = __dev_get_by_index(net, ifm->ifi_index);
Thomas Grafda5e0492006-08-10 21:17:37 -07002231 else if (tb[IFLA_IFNAME])
Eric Dumazeta3d12892009-10-21 10:59:31 +00002232 dev = __dev_get_by_name(net, ifname);
Thomas Grafda5e0492006-08-10 21:17:37 -07002233 else
2234 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
Thomas Grafda5e0492006-08-10 21:17:37 -07002236 if (dev == NULL) {
2237 err = -ENODEV;
2238 goto errout;
2239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002241 err = do_setlink(skb, dev, ifm, tb, ifname, 0);
Thomas Grafda5e0492006-08-10 21:17:37 -07002242errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 return err;
2244}
2245
WANG Cong66400d52015-03-24 11:53:31 -07002246static int rtnl_group_dellink(const struct net *net, int group)
2247{
2248 struct net_device *dev, *aux;
2249 LIST_HEAD(list_kill);
2250 bool found = false;
2251
2252 if (!group)
2253 return -EPERM;
2254
2255 for_each_netdev(net, dev) {
2256 if (dev->group == group) {
2257 const struct rtnl_link_ops *ops;
2258
2259 found = true;
2260 ops = dev->rtnl_link_ops;
2261 if (!ops || !ops->dellink)
2262 return -EOPNOTSUPP;
2263 }
2264 }
2265
2266 if (!found)
2267 return -ENODEV;
2268
2269 for_each_netdev_safe(net, dev, aux) {
2270 if (dev->group == group) {
2271 const struct rtnl_link_ops *ops;
2272
2273 ops = dev->rtnl_link_ops;
2274 ops->dellink(dev, &list_kill);
2275 }
2276 }
2277 unregister_netdevice_many(&list_kill);
2278
2279 return 0;
2280}
2281
Thomas Graf614732e2015-07-21 10:44:06 +02002282int rtnl_delete_link(struct net_device *dev)
2283{
2284 const struct rtnl_link_ops *ops;
2285 LIST_HEAD(list_kill);
2286
2287 ops = dev->rtnl_link_ops;
2288 if (!ops || !ops->dellink)
2289 return -EOPNOTSUPP;
2290
2291 ops->dellink(dev, &list_kill);
2292 unregister_netdevice_many(&list_kill);
2293
2294 return 0;
2295}
2296EXPORT_SYMBOL_GPL(rtnl_delete_link);
2297
Thomas Graf661d2962013-03-21 07:45:29 +00002298static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002299{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002300 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002301 struct net_device *dev;
2302 struct ifinfomsg *ifm;
2303 char ifname[IFNAMSIZ];
2304 struct nlattr *tb[IFLA_MAX+1];
2305 int err;
2306
2307 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2308 if (err < 0)
2309 return err;
2310
2311 if (tb[IFLA_IFNAME])
2312 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2313
2314 ifm = nlmsg_data(nlh);
2315 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07002316 dev = __dev_get_by_index(net, ifm->ifi_index);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002317 else if (tb[IFLA_IFNAME])
Eric W. Biederman881d9662007-09-17 11:56:21 -07002318 dev = __dev_get_by_name(net, ifname);
WANG Cong66400d52015-03-24 11:53:31 -07002319 else if (tb[IFLA_GROUP])
2320 return rtnl_group_dellink(net, nla_get_u32(tb[IFLA_GROUP]));
Patrick McHardy38f7b872007-06-13 12:03:51 -07002321 else
2322 return -EINVAL;
2323
2324 if (!dev)
2325 return -ENODEV;
2326
Thomas Graf614732e2015-07-21 10:44:06 +02002327 return rtnl_delete_link(dev);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002328}
2329
Patrick McHardy3729d502010-02-26 06:34:54 +00002330int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
2331{
2332 unsigned int old_flags;
2333 int err;
2334
2335 old_flags = dev->flags;
2336 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
2337 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
2338 if (err < 0)
2339 return err;
2340 }
2341
Roopa Prabhu19b74792018-07-20 13:21:01 -07002342 if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) {
2343 __dev_notify_flags(dev, old_flags, 0U);
2344 } else {
2345 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
2346 __dev_notify_flags(dev, old_flags, ~0U);
2347 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002348 return 0;
2349}
2350EXPORT_SYMBOL(rtnl_configure_link);
2351
Rami Rosenc0713562012-11-30 01:08:47 +00002352struct net_device *rtnl_create_link(struct net *net,
Thomas Graf78ebb0d2015-04-10 01:45:53 +02002353 const char *ifname, unsigned char name_assign_type,
Tom Gundersen55177502014-07-14 16:37:25 +02002354 const struct rtnl_link_ops *ops, struct nlattr *tb[])
Pavel Emelianove7199282007-08-08 22:16:38 -07002355{
2356 int err;
2357 struct net_device *dev;
Jiri Pirkod40156a2012-07-20 02:28:47 +00002358 unsigned int num_tx_queues = 1;
2359 unsigned int num_rx_queues = 1;
Pavel Emelianove7199282007-08-08 22:16:38 -07002360
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002361 if (tb[IFLA_NUM_TX_QUEUES])
2362 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
2363 else if (ops->get_num_tx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002364 num_tx_queues = ops->get_num_tx_queues();
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002365
2366 if (tb[IFLA_NUM_RX_QUEUES])
2367 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
2368 else if (ops->get_num_rx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002369 num_rx_queues = ops->get_num_rx_queues();
stephen hemmingerefacb302012-04-10 18:34:43 +00002370
Eric Dumazet069bb7f2018-10-02 15:47:35 -07002371 if (num_tx_queues < 1 || num_tx_queues > 4096)
2372 return ERR_PTR(-EINVAL);
2373
2374 if (num_rx_queues < 1 || num_rx_queues > 4096)
2375 return ERR_PTR(-EINVAL);
2376
Pavel Emelianove7199282007-08-08 22:16:38 -07002377 err = -ENOMEM;
Tom Gundersen55177502014-07-14 16:37:25 +02002378 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
Tom Gundersenc835a672014-07-14 16:37:24 +02002379 ops->setup, num_tx_queues, num_rx_queues);
Pavel Emelianove7199282007-08-08 22:16:38 -07002380 if (!dev)
2381 goto err;
2382
Eric W. Biederman81adee42009-11-08 00:53:51 -08002383 dev_net_set(dev, net);
2384 dev->rtnl_link_ops = ops;
Patrick McHardy3729d502010-02-26 06:34:54 +00002385 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
Eric W. Biederman81adee42009-11-08 00:53:51 -08002386
Pavel Emelianove7199282007-08-08 22:16:38 -07002387 if (tb[IFLA_MTU])
2388 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002389 if (tb[IFLA_ADDRESS]) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002390 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
2391 nla_len(tb[IFLA_ADDRESS]));
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002392 dev->addr_assign_type = NET_ADDR_SET;
2393 }
Pavel Emelianove7199282007-08-08 22:16:38 -07002394 if (tb[IFLA_BROADCAST])
2395 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
2396 nla_len(tb[IFLA_BROADCAST]));
2397 if (tb[IFLA_TXQLEN])
2398 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
2399 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002400 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Pavel Emelianove7199282007-08-08 22:16:38 -07002401 if (tb[IFLA_LINKMODE])
2402 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
Patrick McHardyffa934f2011-01-20 03:00:42 +00002403 if (tb[IFLA_GROUP])
2404 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Pavel Emelianove7199282007-08-08 22:16:38 -07002405
2406 return dev;
2407
Pavel Emelianove7199282007-08-08 22:16:38 -07002408err:
2409 return ERR_PTR(err);
2410}
Eric Dumazete0d087a2009-11-07 01:26:17 -08002411EXPORT_SYMBOL(rtnl_create_link);
Pavel Emelianove7199282007-08-08 22:16:38 -07002412
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002413static int rtnl_group_changelink(const struct sk_buff *skb,
2414 struct net *net, int group,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002415 struct ifinfomsg *ifm,
2416 struct nlattr **tb)
2417{
WANG Congd0795352015-03-23 16:31:09 -07002418 struct net_device *dev, *aux;
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002419 int err;
2420
WANG Congd0795352015-03-23 16:31:09 -07002421 for_each_netdev_safe(net, dev, aux) {
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002422 if (dev->group == group) {
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002423 err = do_setlink(skb, dev, ifm, tb, NULL, 0);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002424 if (err < 0)
2425 return err;
2426 }
2427 }
2428
2429 return 0;
2430}
2431
Thomas Graf661d2962013-03-21 07:45:29 +00002432static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002433{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002434 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002435 const struct rtnl_link_ops *ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002436 const struct rtnl_link_ops *m_ops = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002437 struct net_device *dev;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002438 struct net_device *master_dev = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002439 struct ifinfomsg *ifm;
2440 char kind[MODULE_NAME_LEN];
2441 char ifname[IFNAMSIZ];
2442 struct nlattr *tb[IFLA_MAX+1];
2443 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
Tom Gundersen55177502014-07-14 16:37:25 +02002444 unsigned char name_assign_type = NET_NAME_USER;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002445 int err;
2446
Johannes Berg95a5afc2008-10-16 15:24:51 -07002447#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002448replay:
Thomas Graf8072f082007-07-31 14:13:50 -07002449#endif
Patrick McHardy38f7b872007-06-13 12:03:51 -07002450 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2451 if (err < 0)
2452 return err;
2453
2454 if (tb[IFLA_IFNAME])
2455 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2456 else
2457 ifname[0] = '\0';
2458
2459 ifm = nlmsg_data(nlh);
2460 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07002461 dev = __dev_get_by_index(net, ifm->ifi_index);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002462 else {
2463 if (ifname[0])
2464 dev = __dev_get_by_name(net, ifname);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002465 else
2466 dev = NULL;
2467 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002468
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002469 if (dev) {
2470 master_dev = netdev_master_upper_dev_get(dev);
2471 if (master_dev)
2472 m_ops = master_dev->rtnl_link_ops;
2473 }
2474
Eric Dumazete0d087a2009-11-07 01:26:17 -08002475 err = validate_linkmsg(dev, tb);
2476 if (err < 0)
Thomas Graf1840bb12008-02-23 19:54:36 -08002477 return err;
2478
Patrick McHardy38f7b872007-06-13 12:03:51 -07002479 if (tb[IFLA_LINKINFO]) {
2480 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
2481 tb[IFLA_LINKINFO], ifla_info_policy);
2482 if (err < 0)
2483 return err;
2484 } else
2485 memset(linkinfo, 0, sizeof(linkinfo));
2486
2487 if (linkinfo[IFLA_INFO_KIND]) {
2488 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
2489 ops = rtnl_link_ops_get(kind);
2490 } else {
2491 kind[0] = '\0';
2492 ops = NULL;
2493 }
2494
2495 if (1) {
Sasha Levin4e10fd52015-02-24 14:14:35 -05002496 struct nlattr *attr[ops ? ops->maxtype + 1 : 1];
2497 struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1];
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002498 struct nlattr **data = NULL;
2499 struct nlattr **slave_data = NULL;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002500 struct net *dest_net, *link_net = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002501
2502 if (ops) {
2503 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
2504 err = nla_parse_nested(attr, ops->maxtype,
2505 linkinfo[IFLA_INFO_DATA],
2506 ops->policy);
2507 if (err < 0)
2508 return err;
2509 data = attr;
2510 }
2511 if (ops->validate) {
2512 err = ops->validate(tb, data);
2513 if (err < 0)
2514 return err;
2515 }
2516 }
2517
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002518 if (m_ops) {
2519 if (m_ops->slave_maxtype &&
2520 linkinfo[IFLA_INFO_SLAVE_DATA]) {
2521 err = nla_parse_nested(slave_attr,
2522 m_ops->slave_maxtype,
2523 linkinfo[IFLA_INFO_SLAVE_DATA],
2524 m_ops->slave_policy);
2525 if (err < 0)
2526 return err;
2527 slave_data = slave_attr;
2528 }
2529 if (m_ops->slave_validate) {
2530 err = m_ops->slave_validate(tb, slave_data);
2531 if (err < 0)
2532 return err;
2533 }
2534 }
2535
Patrick McHardy38f7b872007-06-13 12:03:51 -07002536 if (dev) {
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002537 int status = 0;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002538
2539 if (nlh->nlmsg_flags & NLM_F_EXCL)
2540 return -EEXIST;
2541 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2542 return -EOPNOTSUPP;
2543
2544 if (linkinfo[IFLA_INFO_DATA]) {
2545 if (!ops || ops != dev->rtnl_link_ops ||
2546 !ops->changelink)
2547 return -EOPNOTSUPP;
2548
2549 err = ops->changelink(dev, tb, data);
2550 if (err < 0)
2551 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002552 status |= DO_SETLINK_NOTIFY;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002553 }
2554
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002555 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2556 if (!m_ops || !m_ops->slave_changelink)
2557 return -EOPNOTSUPP;
2558
2559 err = m_ops->slave_changelink(master_dev, dev,
2560 tb, slave_data);
2561 if (err < 0)
2562 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002563 status |= DO_SETLINK_NOTIFY;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002564 }
2565
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002566 return do_setlink(skb, dev, ifm, tb, ifname, status);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002567 }
2568
Patrick McHardyffa934f2011-01-20 03:00:42 +00002569 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2570 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002571 return rtnl_group_changelink(skb, net,
Patrick McHardyffa934f2011-01-20 03:00:42 +00002572 nla_get_u32(tb[IFLA_GROUP]),
2573 ifm, tb);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002574 return -ENODEV;
Patrick McHardyffa934f2011-01-20 03:00:42 +00002575 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002576
Patrick McHardy0e068772007-07-11 19:42:31 -07002577 if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
Patrick McHardy38f7b872007-06-13 12:03:51 -07002578 return -EOPNOTSUPP;
2579
2580 if (!ops) {
Johannes Berg95a5afc2008-10-16 15:24:51 -07002581#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002582 if (kind[0]) {
2583 __rtnl_unlock();
2584 request_module("rtnl-link-%s", kind);
2585 rtnl_lock();
2586 ops = rtnl_link_ops_get(kind);
2587 if (ops)
2588 goto replay;
2589 }
2590#endif
2591 return -EOPNOTSUPP;
2592 }
2593
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02002594 if (!ops->setup)
2595 return -EOPNOTSUPP;
2596
Tom Gundersen55177502014-07-14 16:37:25 +02002597 if (!ifname[0]) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07002598 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
Tom Gundersen55177502014-07-14 16:37:25 +02002599 name_assign_type = NET_NAME_ENUM;
2600 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002601
Eric W. Biederman81adee42009-11-08 00:53:51 -08002602 dest_net = rtnl_link_get_net(net, tb);
Eric W. Biederman13ad1772011-01-29 14:57:22 +00002603 if (IS_ERR(dest_net))
2604 return PTR_ERR(dest_net);
2605
Eric W. Biederman505ce412015-02-26 16:19:00 -06002606 err = -EPERM;
2607 if (!netlink_ns_capable(skb, dest_net->user_ns, CAP_NET_ADMIN))
2608 goto out;
2609
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002610 if (tb[IFLA_LINK_NETNSID]) {
2611 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
2612
2613 link_net = get_net_ns_by_id(dest_net, id);
2614 if (!link_net) {
2615 err = -EINVAL;
2616 goto out;
2617 }
Eric W. Biederman06615be2015-02-26 16:20:07 -06002618 err = -EPERM;
2619 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
2620 goto out;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002621 }
2622
2623 dev = rtnl_create_link(link_net ? : dest_net, ifname,
2624 name_assign_type, ops, tb);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002625 if (IS_ERR(dev)) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002626 err = PTR_ERR(dev);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002627 goto out;
2628 }
2629
2630 dev->ifindex = ifm->ifi_index;
2631
Cong Wang0e0eee22014-02-11 15:51:30 -08002632 if (ops->newlink) {
Nicolas Dichtel7b4ce692015-01-27 11:13:08 +01002633 err = ops->newlink(link_net ? : net, dev, tb, data);
Cong Wang0e0eee22014-02-11 15:51:30 -08002634 /* Drivers should call free_netdev() in ->destructor
Cong Wange51fb152014-06-03 16:40:47 -07002635 * and unregister it on failure after registration
2636 * so that device could be finally freed in rtnl_unlock.
Cong Wang0e0eee22014-02-11 15:51:30 -08002637 */
Cong Wange51fb152014-06-03 16:40:47 -07002638 if (err < 0) {
2639 /* If device is not registered at all, free it now */
2640 if (dev->reg_state == NETREG_UNINITIALIZED)
2641 free_netdev(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002642 goto out;
Cong Wange51fb152014-06-03 16:40:47 -07002643 }
Cong Wang0e0eee22014-02-11 15:51:30 -08002644 } else {
Patrick McHardy2d85cba2007-07-11 19:42:13 -07002645 err = register_netdevice(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002646 if (err < 0) {
2647 free_netdev(dev);
2648 goto out;
2649 }
Dan Carpenterfce9b9b2013-08-14 12:35:42 +03002650 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002651 err = rtnl_configure_link(dev, ifm);
David S. Miller43638902015-03-10 21:58:32 -04002652 if (err < 0)
2653 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002654 if (link_net) {
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002655 err = dev_change_net_namespace(dev, dest_net, ifname);
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002656 if (err < 0)
David S. Miller43638902015-03-10 21:58:32 -04002657 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002658 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002659out:
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002660 if (link_net)
2661 put_net(link_net);
Eric W. Biederman81adee42009-11-08 00:53:51 -08002662 put_net(dest_net);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002663 return err;
David S. Miller43638902015-03-10 21:58:32 -04002664out_unregister:
2665 if (ops->newlink) {
2666 LIST_HEAD(list_kill);
2667
2668 ops->dellink(dev, &list_kill);
2669 unregister_netdevice_many(&list_kill);
2670 } else {
2671 unregister_netdevice(dev);
2672 }
2673 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002674 }
2675}
2676
Thomas Graf661d2962013-03-21 07:45:29 +00002677static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002678{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002679 struct net *net = sock_net(skb->sk);
Thomas Grafb60c5112006-08-04 23:05:34 -07002680 struct ifinfomsg *ifm;
Eric Dumazeta3d12892009-10-21 10:59:31 +00002681 char ifname[IFNAMSIZ];
Thomas Grafb60c5112006-08-04 23:05:34 -07002682 struct nlattr *tb[IFLA_MAX+1];
2683 struct net_device *dev = NULL;
2684 struct sk_buff *nskb;
Thomas Graf339bf982006-11-10 14:10:15 -08002685 int err;
Greg Rose115c9b82012-02-21 16:54:48 -05002686 u32 ext_filter_mask = 0;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002687
Thomas Grafb60c5112006-08-04 23:05:34 -07002688 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2689 if (err < 0)
Eric Sesterhenn9918f232006-09-26 23:26:38 -07002690 return err;
Thomas Grafb60c5112006-08-04 23:05:34 -07002691
Eric Dumazeta3d12892009-10-21 10:59:31 +00002692 if (tb[IFLA_IFNAME])
2693 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2694
Greg Rose115c9b82012-02-21 16:54:48 -05002695 if (tb[IFLA_EXT_MASK])
2696 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2697
Thomas Grafb60c5112006-08-04 23:05:34 -07002698 ifm = nlmsg_data(nlh);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002699 if (ifm->ifi_index > 0)
2700 dev = __dev_get_by_index(net, ifm->ifi_index);
2701 else if (tb[IFLA_IFNAME])
2702 dev = __dev_get_by_name(net, ifname);
2703 else
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002704 return -EINVAL;
Thomas Grafb60c5112006-08-04 23:05:34 -07002705
Eric Dumazeta3d12892009-10-21 10:59:31 +00002706 if (dev == NULL)
2707 return -ENODEV;
2708
Greg Rose115c9b82012-02-21 16:54:48 -05002709 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002710 if (nskb == NULL)
2711 return -ENOBUFS;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002712
Eric W. Biederman15e47302012-09-07 20:12:54 +00002713 err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
Greg Rose115c9b82012-02-21 16:54:48 -05002714 nlh->nlmsg_seq, 0, 0, ext_filter_mask);
Patrick McHardy26932562007-01-31 23:16:40 -08002715 if (err < 0) {
2716 /* -EMSGSIZE implies BUG in if_nlmsg_size */
2717 WARN_ON(err == -EMSGSIZE);
2718 kfree_skb(nskb);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002719 } else
Eric W. Biederman15e47302012-09-07 20:12:54 +00002720 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
Thomas Grafb60c5112006-08-04 23:05:34 -07002721
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002722 return err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002723}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002724
Greg Rose115c9b82012-02-21 16:54:48 -05002725static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
Greg Rosec7ac8672011-06-10 01:27:09 +00002726{
Greg Rose115c9b82012-02-21 16:54:48 -05002727 struct net *net = sock_net(skb->sk);
2728 struct net_device *dev;
2729 struct nlattr *tb[IFLA_MAX+1];
2730 u32 ext_filter_mask = 0;
2731 u16 min_ifinfo_dump_size = 0;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02002732 int hdrlen;
Greg Rose115c9b82012-02-21 16:54:48 -05002733
Michal Schmidte5eca6d2014-05-28 14:15:19 +02002734 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2735 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2736 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2737
2738 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00002739 if (tb[IFLA_EXT_MASK])
2740 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2741 }
Greg Rose115c9b82012-02-21 16:54:48 -05002742
2743 if (!ext_filter_mask)
2744 return NLMSG_GOODSIZE;
2745 /*
2746 * traverse the list of net devices and compute the minimum
2747 * buffer size based upon the filter mask.
2748 */
2749 list_for_each_entry(dev, &net->dev_base_head, dev_list) {
2750 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
2751 if_nlmsg_size(dev,
2752 ext_filter_mask));
2753 }
2754
Zhang Shengju93af2052016-11-22 14:14:28 +08002755 return nlmsg_total_size(min_ifinfo_dump_size);
Greg Rosec7ac8672011-06-10 01:27:09 +00002756}
2757
Adrian Bunk42bad1d2007-04-26 00:57:41 -07002758static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759{
2760 int idx;
2761 int s_idx = cb->family;
2762
2763 if (s_idx == 0)
2764 s_idx = 1;
Patrick McHardy25239ce2010-04-26 16:02:05 +02002765 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 int type = cb->nlh->nlmsg_type-RTM_BASE;
2767 if (idx < s_idx || idx == PF_PACKET)
2768 continue;
Thomas Grafe2849862007-03-22 11:48:11 -07002769 if (rtnl_msg_handlers[idx] == NULL ||
2770 rtnl_msg_handlers[idx][type].dumpit == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 continue;
Nicolas Dichtel04652772013-03-22 06:28:42 +00002772 if (idx > s_idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 memset(&cb->args[0], 0, sizeof(cb->args));
Nicolas Dichtel04652772013-03-22 06:28:42 +00002774 cb->prev_seq = 0;
2775 cb->seq = 0;
2776 }
Thomas Grafe2849862007-03-22 11:48:11 -07002777 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 break;
2779 }
2780 cb->family = idx;
2781
2782 return skb->len;
2783}
2784
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002785struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
2786 unsigned int change, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002788 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 struct sk_buff *skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002790 int err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00002791 size_t if_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792
Alexei Starovoitov7f294052013-10-23 16:02:42 -07002793 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002794 if (skb == NULL)
2795 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
Greg Rose115c9b82012-02-21 16:54:48 -05002797 err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08002798 if (err < 0) {
2799 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
2800 WARN_ON(err == -EMSGSIZE);
2801 kfree_skb(skb);
2802 goto errout;
2803 }
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002804 return skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002805errout:
2806 if (err < 0)
Eric W. Biederman4b3da702007-11-19 22:27:40 -08002807 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002808 return NULL;
2809}
2810
2811void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
2812{
2813 struct net *net = dev_net(dev);
2814
2815 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
2816}
2817
2818void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
2819 gfp_t flags)
2820{
2821 struct sk_buff *skb;
2822
Nicolas Dichteled2a80a2015-05-13 14:19:42 +02002823 if (dev->reg_state != NETREG_REGISTERED)
2824 return;
2825
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002826 skb = rtmsg_ifinfo_build_skb(type, dev, change, flags);
2827 if (skb)
2828 rtmsg_ifinfo_send(skb, dev, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829}
Jiri Pirko471cb5a2013-01-03 22:49:01 +00002830EXPORT_SYMBOL(rtmsg_ifinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831
John Fastabendd83b0602012-04-15 06:44:08 +00002832static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
2833 struct net_device *dev,
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002834 u8 *addr, u16 vid, u32 pid, u32 seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002835 int type, unsigned int flags,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002836 int nlflags, u16 ndm_state)
John Fastabendd83b0602012-04-15 06:44:08 +00002837{
2838 struct nlmsghdr *nlh;
2839 struct ndmsg *ndm;
2840
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002841 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
John Fastabendd83b0602012-04-15 06:44:08 +00002842 if (!nlh)
2843 return -EMSGSIZE;
2844
2845 ndm = nlmsg_data(nlh);
2846 ndm->ndm_family = AF_BRIDGE;
2847 ndm->ndm_pad1 = 0;
2848 ndm->ndm_pad2 = 0;
2849 ndm->ndm_flags = flags;
2850 ndm->ndm_type = 0;
2851 ndm->ndm_ifindex = dev->ifindex;
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002852 ndm->ndm_state = ndm_state;
John Fastabendd83b0602012-04-15 06:44:08 +00002853
2854 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
2855 goto nla_put_failure;
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002856 if (vid)
2857 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
2858 goto nla_put_failure;
John Fastabendd83b0602012-04-15 06:44:08 +00002859
Johannes Berg053c0952015-01-16 22:09:00 +01002860 nlmsg_end(skb, nlh);
2861 return 0;
John Fastabendd83b0602012-04-15 06:44:08 +00002862
2863nla_put_failure:
2864 nlmsg_cancel(skb, nlh);
2865 return -EMSGSIZE;
2866}
2867
John Fastabend3ff661c2012-04-15 06:44:14 +00002868static inline size_t rtnl_fdb_nlmsg_size(void)
2869{
Sabrina Dubrocaf82ef3e2016-11-18 15:50:39 +01002870 return NLMSG_ALIGN(sizeof(struct ndmsg)) +
2871 nla_total_size(ETH_ALEN) + /* NDA_LLADDR */
2872 nla_total_size(sizeof(u16)) + /* NDA_VLAN */
2873 0;
John Fastabend3ff661c2012-04-15 06:44:14 +00002874}
2875
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002876static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
2877 u16 ndm_state)
John Fastabend3ff661c2012-04-15 06:44:14 +00002878{
2879 struct net *net = dev_net(dev);
2880 struct sk_buff *skb;
2881 int err = -ENOBUFS;
2882
2883 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
2884 if (!skb)
2885 goto errout;
2886
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002887 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002888 0, 0, type, NTF_SELF, 0, ndm_state);
John Fastabend3ff661c2012-04-15 06:44:14 +00002889 if (err < 0) {
2890 kfree_skb(skb);
2891 goto errout;
2892 }
2893
2894 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
2895 return;
2896errout:
2897 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
2898}
2899
Vlad Yasevich090096b2013-03-06 15:39:42 +00002900/**
2901 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2902 */
2903int ndo_dflt_fdb_add(struct ndmsg *ndm,
2904 struct nlattr *tb[],
2905 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01002906 const unsigned char *addr, u16 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00002907 u16 flags)
2908{
2909 int err = -EINVAL;
2910
2911 /* If aging addresses are supported device will need to
2912 * implement its own handler for this.
2913 */
2914 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2915 pr_info("%s: FDB only supports static addresses\n", dev->name);
2916 return err;
2917 }
2918
Or Gerlitz65891fe2014-12-14 18:19:05 +02002919 if (vid) {
2920 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
2921 return err;
2922 }
2923
Vlad Yasevich090096b2013-03-06 15:39:42 +00002924 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2925 err = dev_uc_add_excl(dev, addr);
2926 else if (is_multicast_ether_addr(addr))
2927 err = dev_mc_add_excl(dev, addr);
2928
2929 /* Only return duplicate errors if NLM_F_EXCL is set */
2930 if (err == -EEXIST && !(flags & NLM_F_EXCL))
2931 err = 0;
2932
2933 return err;
2934}
2935EXPORT_SYMBOL(ndo_dflt_fdb_add);
2936
Jiri Pirkof6f64242014-11-28 14:34:15 +01002937static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid)
2938{
2939 u16 vid = 0;
2940
2941 if (vlan_attr) {
2942 if (nla_len(vlan_attr) != sizeof(u16)) {
2943 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n");
2944 return -EINVAL;
2945 }
2946
2947 vid = nla_get_u16(vlan_attr);
2948
2949 if (!vid || vid >= VLAN_VID_MASK) {
2950 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n",
2951 vid);
2952 return -EINVAL;
2953 }
2954 }
2955 *p_vid = vid;
2956 return 0;
2957}
2958
Thomas Graf661d2962013-03-21 07:45:29 +00002959static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh)
John Fastabend77162022012-04-15 06:43:56 +00002960{
2961 struct net *net = sock_net(skb->sk);
John Fastabend77162022012-04-15 06:43:56 +00002962 struct ndmsg *ndm;
2963 struct nlattr *tb[NDA_MAX+1];
2964 struct net_device *dev;
2965 u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01002966 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00002967 int err;
2968
2969 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2970 if (err < 0)
2971 return err;
2972
2973 ndm = nlmsg_data(nlh);
2974 if (ndm->ndm_ifindex == 0) {
2975 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
2976 return -EINVAL;
2977 }
2978
2979 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2980 if (dev == NULL) {
2981 pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
2982 return -ENODEV;
2983 }
2984
2985 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2986 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
2987 return -EINVAL;
2988 }
2989
Ido Schimmel98197412018-10-29 20:36:43 +00002990 if (dev->type != ARPHRD_ETHER) {
2991 pr_info("PF_BRIDGE: FDB add only supported for Ethernet devices");
2992 return -EINVAL;
2993 }
2994
John Fastabend77162022012-04-15 06:43:56 +00002995 addr = nla_data(tb[NDA_LLADDR]);
John Fastabend77162022012-04-15 06:43:56 +00002996
Jiri Pirkof6f64242014-11-28 14:34:15 +01002997 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2998 if (err)
2999 return err;
3000
John Fastabend77162022012-04-15 06:43:56 +00003001 err = -EOPNOTSUPP;
3002
3003 /* Support fdb on master device the net/bridge default case */
3004 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3005 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003006 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3007 const struct net_device_ops *ops = br_dev->netdev_ops;
3008
Jiri Pirkof6f64242014-11-28 14:34:15 +01003009 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
3010 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00003011 if (err)
3012 goto out;
3013 else
3014 ndm->ndm_flags &= ~NTF_MASTER;
3015 }
3016
3017 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00003018 if ((ndm->ndm_flags & NTF_SELF)) {
3019 if (dev->netdev_ops->ndo_fdb_add)
3020 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003021 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003022 nlh->nlmsg_flags);
3023 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01003024 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003025 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00003026
John Fastabend3ff661c2012-04-15 06:44:14 +00003027 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003028 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
3029 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00003030 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00003031 }
John Fastabend77162022012-04-15 06:43:56 +00003032 }
3033out:
3034 return err;
3035}
3036
Vlad Yasevich090096b2013-03-06 15:39:42 +00003037/**
3038 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3039 */
3040int ndo_dflt_fdb_del(struct ndmsg *ndm,
3041 struct nlattr *tb[],
3042 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003043 const unsigned char *addr, u16 vid)
Vlad Yasevich090096b2013-03-06 15:39:42 +00003044{
Alexander Duyckc8a89c42014-07-15 15:15:20 -07003045 int err = -EINVAL;
Vlad Yasevich090096b2013-03-06 15:39:42 +00003046
3047 /* If aging addresses are supported device will need to
3048 * implement its own handler for this.
3049 */
Sridhar Samudrala64535992013-08-08 15:19:48 -07003050 if (!(ndm->ndm_state & NUD_PERMANENT)) {
Vlad Yasevich090096b2013-03-06 15:39:42 +00003051 pr_info("%s: FDB only supports static addresses\n", dev->name);
Alexander Duyckc8a89c42014-07-15 15:15:20 -07003052 return err;
Vlad Yasevich090096b2013-03-06 15:39:42 +00003053 }
3054
3055 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3056 err = dev_uc_del(dev, addr);
3057 else if (is_multicast_ether_addr(addr))
3058 err = dev_mc_del(dev, addr);
Vlad Yasevich090096b2013-03-06 15:39:42 +00003059
3060 return err;
3061}
3062EXPORT_SYMBOL(ndo_dflt_fdb_del);
3063
Thomas Graf661d2962013-03-21 07:45:29 +00003064static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh)
John Fastabend77162022012-04-15 06:43:56 +00003065{
3066 struct net *net = sock_net(skb->sk);
3067 struct ndmsg *ndm;
Vlad Yasevich1690be62013-02-13 12:00:18 +00003068 struct nlattr *tb[NDA_MAX+1];
John Fastabend77162022012-04-15 06:43:56 +00003069 struct net_device *dev;
3070 int err = -EINVAL;
3071 __u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01003072 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00003073
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07003074 if (!netlink_capable(skb, CAP_NET_ADMIN))
Vlad Yasevich1690be62013-02-13 12:00:18 +00003075 return -EPERM;
3076
3077 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
3078 if (err < 0)
3079 return err;
John Fastabend77162022012-04-15 06:43:56 +00003080
3081 ndm = nlmsg_data(nlh);
3082 if (ndm->ndm_ifindex == 0) {
3083 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
3084 return -EINVAL;
3085 }
3086
3087 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3088 if (dev == NULL) {
3089 pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
3090 return -ENODEV;
3091 }
3092
Vlad Yasevich1690be62013-02-13 12:00:18 +00003093 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3094 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
John Fastabend77162022012-04-15 06:43:56 +00003095 return -EINVAL;
3096 }
3097
Ido Schimmel98197412018-10-29 20:36:43 +00003098 if (dev->type != ARPHRD_ETHER) {
3099 pr_info("PF_BRIDGE: FDB delete only supported for Ethernet devices");
3100 return -EINVAL;
3101 }
3102
Vlad Yasevich1690be62013-02-13 12:00:18 +00003103 addr = nla_data(tb[NDA_LLADDR]);
Vlad Yasevich1690be62013-02-13 12:00:18 +00003104
Jiri Pirkof6f64242014-11-28 14:34:15 +01003105 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
3106 if (err)
3107 return err;
3108
John Fastabend77162022012-04-15 06:43:56 +00003109 err = -EOPNOTSUPP;
3110
3111 /* Support fdb on master device the net/bridge default case */
3112 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3113 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003114 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3115 const struct net_device_ops *ops = br_dev->netdev_ops;
John Fastabend77162022012-04-15 06:43:56 +00003116
Jiri Pirko898e5062013-01-03 22:48:52 +00003117 if (ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003118 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00003119
3120 if (err)
3121 goto out;
3122 else
3123 ndm->ndm_flags &= ~NTF_MASTER;
3124 }
3125
3126 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00003127 if (ndm->ndm_flags & NTF_SELF) {
3128 if (dev->netdev_ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003129 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
3130 vid);
Vlad Yasevich090096b2013-03-06 15:39:42 +00003131 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01003132 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00003133
John Fastabend3ff661c2012-04-15 06:44:14 +00003134 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003135 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
3136 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00003137 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00003138 }
John Fastabend77162022012-04-15 06:43:56 +00003139 }
3140out:
3141 return err;
3142}
3143
John Fastabendd83b0602012-04-15 06:44:08 +00003144static int nlmsg_populate_fdb(struct sk_buff *skb,
3145 struct netlink_callback *cb,
3146 struct net_device *dev,
3147 int *idx,
3148 struct netdev_hw_addr_list *list)
3149{
3150 struct netdev_hw_addr *ha;
3151 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00003152 u32 portid, seq;
John Fastabendd83b0602012-04-15 06:44:08 +00003153
Eric W. Biederman15e47302012-09-07 20:12:54 +00003154 portid = NETLINK_CB(cb->skb).portid;
John Fastabendd83b0602012-04-15 06:44:08 +00003155 seq = cb->nlh->nlmsg_seq;
3156
3157 list_for_each_entry(ha, &list->list, list) {
Roopa Prabhud2976532016-08-30 21:56:45 -07003158 if (*idx < cb->args[2])
John Fastabendd83b0602012-04-15 06:44:08 +00003159 goto skip;
3160
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003161 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
John Fastabenda7a558f2012-11-01 16:23:10 +00003162 portid, seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003163 RTM_NEWNEIGH, NTF_SELF,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003164 NLM_F_MULTI, NUD_PERMANENT);
John Fastabendd83b0602012-04-15 06:44:08 +00003165 if (err < 0)
3166 return err;
3167skip:
3168 *idx += 1;
3169 }
3170 return 0;
3171}
3172
3173/**
Ben Hutchings2c530402012-07-10 10:55:09 +00003174 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
John Fastabendd83b0602012-04-15 06:44:08 +00003175 * @nlh: netlink message header
3176 * @dev: netdevice
3177 *
3178 * Default netdevice operation to dump the existing unicast address list.
John Fastabend91f3e7b2013-03-29 08:18:37 +00003179 * Returns number of addresses from list put in skb.
John Fastabendd83b0602012-04-15 06:44:08 +00003180 */
3181int ndo_dflt_fdb_dump(struct sk_buff *skb,
3182 struct netlink_callback *cb,
3183 struct net_device *dev,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04003184 struct net_device *filter_dev,
Roopa Prabhud2976532016-08-30 21:56:45 -07003185 int *idx)
John Fastabendd83b0602012-04-15 06:44:08 +00003186{
3187 int err;
3188
Eric Dumazetfe252792018-12-04 09:40:35 -08003189 if (dev->type != ARPHRD_ETHER)
3190 return -EINVAL;
3191
John Fastabendd83b0602012-04-15 06:44:08 +00003192 netif_addr_lock_bh(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -07003193 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
John Fastabendd83b0602012-04-15 06:44:08 +00003194 if (err)
3195 goto out;
Roopa Prabhud2976532016-08-30 21:56:45 -07003196 nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
John Fastabendd83b0602012-04-15 06:44:08 +00003197out:
3198 netif_addr_unlock_bh(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -07003199 return err;
John Fastabendd83b0602012-04-15 06:44:08 +00003200}
3201EXPORT_SYMBOL(ndo_dflt_fdb_dump);
3202
John Fastabend77162022012-04-15 06:43:56 +00003203static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
3204{
John Fastabend77162022012-04-15 06:43:56 +00003205 struct net_device *dev;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003206 struct nlattr *tb[IFLA_MAX+1];
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003207 struct net_device *br_dev = NULL;
3208 const struct net_device_ops *ops = NULL;
3209 const struct net_device_ops *cops = NULL;
3210 struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
3211 struct net *net = sock_net(skb->sk);
Roopa Prabhud2976532016-08-30 21:56:45 -07003212 struct hlist_head *head;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003213 int brport_idx = 0;
3214 int br_idx = 0;
Roopa Prabhud2976532016-08-30 21:56:45 -07003215 int h, s_h;
3216 int idx = 0, s_idx;
3217 int err = 0;
3218 int fidx = 0;
John Fastabend77162022012-04-15 06:43:56 +00003219
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003220 if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
3221 ifla_policy) == 0) {
3222 if (tb[IFLA_MASTER])
3223 br_idx = nla_get_u32(tb[IFLA_MASTER]);
3224 }
John Fastabend77162022012-04-15 06:43:56 +00003225
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003226 brport_idx = ifm->ifi_index;
3227
3228 if (br_idx) {
3229 br_dev = __dev_get_by_index(net, br_idx);
3230 if (!br_dev)
3231 return -ENODEV;
3232
3233 ops = br_dev->netdev_ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003234 }
3235
Roopa Prabhud2976532016-08-30 21:56:45 -07003236 s_h = cb->args[0];
3237 s_idx = cb->args[1];
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003238
Roopa Prabhud2976532016-08-30 21:56:45 -07003239 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
3240 idx = 0;
3241 head = &net->dev_index_head[h];
3242 hlist_for_each_entry(dev, head, index_hlist) {
3243
3244 if (brport_idx && (dev->ifindex != brport_idx))
3245 continue;
3246
3247 if (!br_idx) { /* user did not specify a specific bridge */
3248 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3249 br_dev = netdev_master_upper_dev_get(dev);
3250 cops = br_dev->netdev_ops;
3251 }
3252 } else {
3253 if (dev != br_dev &&
3254 !(dev->priv_flags & IFF_BRIDGE_PORT))
3255 continue;
3256
3257 if (br_dev != netdev_master_upper_dev_get(dev) &&
3258 !(dev->priv_flags & IFF_EBRIDGE))
3259 continue;
3260 cops = ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003261 }
3262
Roopa Prabhud2976532016-08-30 21:56:45 -07003263 if (idx < s_idx)
3264 goto cont;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003265
Roopa Prabhud2976532016-08-30 21:56:45 -07003266 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3267 if (cops && cops->ndo_fdb_dump) {
3268 err = cops->ndo_fdb_dump(skb, cb,
3269 br_dev, dev,
3270 &fidx);
3271 if (err == -EMSGSIZE)
3272 goto out;
3273 }
3274 }
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003275
Roopa Prabhud2976532016-08-30 21:56:45 -07003276 if (dev->netdev_ops->ndo_fdb_dump)
3277 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
3278 dev, NULL,
3279 &fidx);
3280 else
3281 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
3282 &fidx);
3283 if (err == -EMSGSIZE)
3284 goto out;
3285
3286 cops = NULL;
3287
3288 /* reset fdb offset to 0 for rest of the interfaces */
3289 cb->args[2] = 0;
3290 fidx = 0;
3291cont:
3292 idx++;
John Fastabend77162022012-04-15 06:43:56 +00003293 }
John Fastabend77162022012-04-15 06:43:56 +00003294 }
John Fastabend77162022012-04-15 06:43:56 +00003295
Roopa Prabhud2976532016-08-30 21:56:45 -07003296out:
3297 cb->args[0] = h;
3298 cb->args[1] = idx;
3299 cb->args[2] = fidx;
3300
John Fastabend77162022012-04-15 06:43:56 +00003301 return skb->len;
3302}
3303
Scott Feldman2c3c0312014-11-28 14:34:25 +01003304static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
3305 unsigned int attrnum, unsigned int flag)
3306{
3307 if (mask & flag)
3308 return nla_put_u8(skb, attrnum, !!(flags & flag));
3309 return 0;
3310}
3311
John Fastabend815cccb2012-10-24 08:13:09 +00003312int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Scott Feldman2c3c0312014-11-28 14:34:25 +01003313 struct net_device *dev, u16 mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003314 u32 flags, u32 mask, int nlflags,
3315 u32 filter_mask,
3316 int (*vlan_fill)(struct sk_buff *skb,
3317 struct net_device *dev,
3318 u32 filter_mask))
John Fastabend815cccb2012-10-24 08:13:09 +00003319{
3320 struct nlmsghdr *nlh;
3321 struct ifinfomsg *ifm;
3322 struct nlattr *br_afspec;
Scott Feldman2c3c0312014-11-28 14:34:25 +01003323 struct nlattr *protinfo;
John Fastabend815cccb2012-10-24 08:13:09 +00003324 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Jiri Pirko898e5062013-01-03 22:48:52 +00003325 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003326 int err = 0;
John Fastabend815cccb2012-10-24 08:13:09 +00003327
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02003328 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
John Fastabend815cccb2012-10-24 08:13:09 +00003329 if (nlh == NULL)
3330 return -EMSGSIZE;
3331
3332 ifm = nlmsg_data(nlh);
3333 ifm->ifi_family = AF_BRIDGE;
3334 ifm->__ifi_pad = 0;
3335 ifm->ifi_type = dev->type;
3336 ifm->ifi_index = dev->ifindex;
3337 ifm->ifi_flags = dev_get_flags(dev);
3338 ifm->ifi_change = 0;
3339
3340
3341 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
3342 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
3343 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00003344 (br_dev &&
3345 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
John Fastabend815cccb2012-10-24 08:13:09 +00003346 (dev->addr_len &&
3347 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02003348 (dev->ifindex != dev_get_iflink(dev) &&
3349 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
John Fastabend815cccb2012-10-24 08:13:09 +00003350 goto nla_put_failure;
3351
3352 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
3353 if (!br_afspec)
3354 goto nla_put_failure;
3355
Roopa Prabhu1d460b92014-12-08 14:04:20 -08003356 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
John Fastabend815cccb2012-10-24 08:13:09 +00003357 nla_nest_cancel(skb, br_afspec);
3358 goto nla_put_failure;
3359 }
Roopa Prabhu1d460b92014-12-08 14:04:20 -08003360
3361 if (mode != BRIDGE_MODE_UNDEF) {
3362 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
3363 nla_nest_cancel(skb, br_afspec);
3364 goto nla_put_failure;
3365 }
3366 }
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003367 if (vlan_fill) {
3368 err = vlan_fill(skb, dev, filter_mask);
3369 if (err) {
3370 nla_nest_cancel(skb, br_afspec);
3371 goto nla_put_failure;
3372 }
3373 }
John Fastabend815cccb2012-10-24 08:13:09 +00003374 nla_nest_end(skb, br_afspec);
3375
Scott Feldman2c3c0312014-11-28 14:34:25 +01003376 protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
3377 if (!protinfo)
3378 goto nla_put_failure;
3379
3380 if (brport_nla_put_flag(skb, flags, mask,
3381 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
3382 brport_nla_put_flag(skb, flags, mask,
3383 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
3384 brport_nla_put_flag(skb, flags, mask,
3385 IFLA_BRPORT_FAST_LEAVE,
3386 BR_MULTICAST_FAST_LEAVE) ||
3387 brport_nla_put_flag(skb, flags, mask,
3388 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
3389 brport_nla_put_flag(skb, flags, mask,
3390 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
3391 brport_nla_put_flag(skb, flags, mask,
3392 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
3393 brport_nla_put_flag(skb, flags, mask,
3394 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
3395 brport_nla_put_flag(skb, flags, mask,
3396 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
3397 nla_nest_cancel(skb, protinfo);
3398 goto nla_put_failure;
3399 }
3400
3401 nla_nest_end(skb, protinfo);
3402
Johannes Berg053c0952015-01-16 22:09:00 +01003403 nlmsg_end(skb, nlh);
3404 return 0;
John Fastabend815cccb2012-10-24 08:13:09 +00003405nla_put_failure:
3406 nlmsg_cancel(skb, nlh);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003407 return err ? err : -EMSGSIZE;
John Fastabend815cccb2012-10-24 08:13:09 +00003408}
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003409EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
John Fastabend815cccb2012-10-24 08:13:09 +00003410
John Fastabende5a55a82012-10-24 08:12:57 +00003411static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
3412{
3413 struct net *net = sock_net(skb->sk);
3414 struct net_device *dev;
3415 int idx = 0;
3416 u32 portid = NETLINK_CB(cb->skb).portid;
3417 u32 seq = cb->nlh->nlmsg_seq;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00003418 u32 filter_mask = 0;
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003419 int err;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00003420
Thomas Grafaa68c202014-11-26 13:42:20 +01003421 if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
3422 struct nlattr *extfilt;
3423
3424 extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
3425 IFLA_EXT_MASK);
3426 if (extfilt) {
3427 if (nla_len(extfilt) < sizeof(filter_mask))
3428 return -EINVAL;
3429
3430 filter_mask = nla_get_u32(extfilt);
3431 }
3432 }
John Fastabende5a55a82012-10-24 08:12:57 +00003433
3434 rcu_read_lock();
3435 for_each_netdev_rcu(net, dev) {
3436 const struct net_device_ops *ops = dev->netdev_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00003437 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
John Fastabende5a55a82012-10-24 08:12:57 +00003438
Jiri Pirko898e5062013-01-03 22:48:52 +00003439 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003440 if (idx >= cb->args[0]) {
3441 err = br_dev->netdev_ops->ndo_bridge_getlink(
3442 skb, portid, seq, dev,
3443 filter_mask, NLM_F_MULTI);
David Ahern68647612017-05-15 23:19:17 -07003444 if (err < 0 && err != -EOPNOTSUPP) {
3445 if (likely(skb->len))
3446 break;
3447
3448 goto out_err;
3449 }
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003450 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00003451 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00003452 }
3453
3454 if (ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003455 if (idx >= cb->args[0]) {
3456 err = ops->ndo_bridge_getlink(skb, portid,
3457 seq, dev,
3458 filter_mask,
3459 NLM_F_MULTI);
David Ahern68647612017-05-15 23:19:17 -07003460 if (err < 0 && err != -EOPNOTSUPP) {
3461 if (likely(skb->len))
3462 break;
3463
3464 goto out_err;
3465 }
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003466 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00003467 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00003468 }
3469 }
David Ahern68647612017-05-15 23:19:17 -07003470 err = skb->len;
3471out_err:
John Fastabende5a55a82012-10-24 08:12:57 +00003472 rcu_read_unlock();
3473 cb->args[0] = idx;
3474
David Ahern68647612017-05-15 23:19:17 -07003475 return err;
John Fastabende5a55a82012-10-24 08:12:57 +00003476}
3477
John Fastabend2469ffd2012-10-24 08:13:03 +00003478static inline size_t bridge_nlmsg_size(void)
3479{
3480 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3481 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3482 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3483 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
3484 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
3485 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
3486 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
3487 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
3488 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
3489 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
3490 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
3491}
3492
Roopa Prabhu02dba432015-01-14 20:02:25 -08003493static int rtnl_bridge_notify(struct net_device *dev)
John Fastabend2469ffd2012-10-24 08:13:03 +00003494{
3495 struct net *net = dev_net(dev);
John Fastabend2469ffd2012-10-24 08:13:03 +00003496 struct sk_buff *skb;
3497 int err = -EOPNOTSUPP;
3498
Roopa Prabhu02dba432015-01-14 20:02:25 -08003499 if (!dev->netdev_ops->ndo_bridge_getlink)
3500 return 0;
3501
John Fastabend2469ffd2012-10-24 08:13:03 +00003502 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
3503 if (!skb) {
3504 err = -ENOMEM;
3505 goto errout;
3506 }
3507
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02003508 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
Roopa Prabhu02dba432015-01-14 20:02:25 -08003509 if (err < 0)
3510 goto errout;
John Fastabend2469ffd2012-10-24 08:13:03 +00003511
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08003512 if (!skb->len)
3513 goto errout;
3514
John Fastabend2469ffd2012-10-24 08:13:03 +00003515 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
3516 return 0;
3517errout:
3518 WARN_ON(err == -EMSGSIZE);
3519 kfree_skb(skb);
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08003520 if (err)
3521 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
John Fastabend2469ffd2012-10-24 08:13:03 +00003522 return err;
3523}
3524
Thomas Graf661d2962013-03-21 07:45:29 +00003525static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
John Fastabende5a55a82012-10-24 08:12:57 +00003526{
3527 struct net *net = sock_net(skb->sk);
3528 struct ifinfomsg *ifm;
3529 struct net_device *dev;
John Fastabend2469ffd2012-10-24 08:13:03 +00003530 struct nlattr *br_spec, *attr = NULL;
3531 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02003532 u16 flags = 0;
John Fastabendc38e01b2012-11-02 16:32:36 +00003533 bool have_flags = false;
John Fastabende5a55a82012-10-24 08:12:57 +00003534
3535 if (nlmsg_len(nlh) < sizeof(*ifm))
3536 return -EINVAL;
3537
3538 ifm = nlmsg_data(nlh);
3539 if (ifm->ifi_family != AF_BRIDGE)
3540 return -EPFNOSUPPORT;
3541
3542 dev = __dev_get_by_index(net, ifm->ifi_index);
3543 if (!dev) {
3544 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3545 return -ENODEV;
3546 }
3547
John Fastabend2469ffd2012-10-24 08:13:03 +00003548 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3549 if (br_spec) {
3550 nla_for_each_nested(attr, br_spec, rem) {
3551 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01003552 if (nla_len(attr) < sizeof(flags))
3553 return -EINVAL;
3554
John Fastabendc38e01b2012-11-02 16:32:36 +00003555 have_flags = true;
John Fastabend2469ffd2012-10-24 08:13:03 +00003556 flags = nla_get_u16(attr);
3557 break;
3558 }
3559 }
3560 }
3561
3562 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003563 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3564
3565 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
John Fastabend2469ffd2012-10-24 08:13:03 +00003566 err = -EOPNOTSUPP;
3567 goto out;
3568 }
3569
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003570 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
John Fastabende5a55a82012-10-24 08:12:57 +00003571 if (err)
3572 goto out;
John Fastabend2469ffd2012-10-24 08:13:03 +00003573
3574 flags &= ~BRIDGE_FLAGS_MASTER;
John Fastabende5a55a82012-10-24 08:12:57 +00003575 }
3576
John Fastabend2469ffd2012-10-24 08:13:03 +00003577 if ((flags & BRIDGE_FLAGS_SELF)) {
3578 if (!dev->netdev_ops->ndo_bridge_setlink)
3579 err = -EOPNOTSUPP;
3580 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003581 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
3582 flags);
Roopa Prabhu02dba432015-01-14 20:02:25 -08003583 if (!err) {
John Fastabend2469ffd2012-10-24 08:13:03 +00003584 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08003585
3586 /* Generate event to notify upper layer of bridge
3587 * change
3588 */
3589 err = rtnl_bridge_notify(dev);
3590 }
John Fastabend2469ffd2012-10-24 08:13:03 +00003591 }
3592
John Fastabendc38e01b2012-11-02 16:32:36 +00003593 if (have_flags)
John Fastabend2469ffd2012-10-24 08:13:03 +00003594 memcpy(nla_data(attr), &flags, sizeof(flags));
John Fastabende5a55a82012-10-24 08:12:57 +00003595out:
3596 return err;
3597}
3598
Thomas Graf661d2962013-03-21 07:45:29 +00003599static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
Vlad Yasevich407af322013-02-13 12:00:12 +00003600{
3601 struct net *net = sock_net(skb->sk);
3602 struct ifinfomsg *ifm;
3603 struct net_device *dev;
3604 struct nlattr *br_spec, *attr = NULL;
3605 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02003606 u16 flags = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +00003607 bool have_flags = false;
3608
3609 if (nlmsg_len(nlh) < sizeof(*ifm))
3610 return -EINVAL;
3611
3612 ifm = nlmsg_data(nlh);
3613 if (ifm->ifi_family != AF_BRIDGE)
3614 return -EPFNOSUPPORT;
3615
3616 dev = __dev_get_by_index(net, ifm->ifi_index);
3617 if (!dev) {
3618 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3619 return -ENODEV;
3620 }
3621
3622 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3623 if (br_spec) {
3624 nla_for_each_nested(attr, br_spec, rem) {
3625 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01003626 if (nla_len(attr) < sizeof(flags))
3627 return -EINVAL;
3628
Vlad Yasevich407af322013-02-13 12:00:12 +00003629 have_flags = true;
3630 flags = nla_get_u16(attr);
3631 break;
3632 }
3633 }
3634 }
3635
Vlad Yasevich407af322013-02-13 12:00:12 +00003636 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3637 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3638
3639 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
3640 err = -EOPNOTSUPP;
3641 goto out;
3642 }
3643
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003644 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00003645 if (err)
3646 goto out;
3647
3648 flags &= ~BRIDGE_FLAGS_MASTER;
3649 }
3650
3651 if ((flags & BRIDGE_FLAGS_SELF)) {
3652 if (!dev->netdev_ops->ndo_bridge_dellink)
3653 err = -EOPNOTSUPP;
3654 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003655 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
3656 flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00003657
Roopa Prabhu02dba432015-01-14 20:02:25 -08003658 if (!err) {
Vlad Yasevich407af322013-02-13 12:00:12 +00003659 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08003660
3661 /* Generate event to notify upper layer of bridge
3662 * change
3663 */
3664 err = rtnl_bridge_notify(dev);
3665 }
Vlad Yasevich407af322013-02-13 12:00:12 +00003666 }
3667
3668 if (have_flags)
3669 memcpy(nla_data(attr), &flags, sizeof(flags));
Vlad Yasevich407af322013-02-13 12:00:12 +00003670out:
3671 return err;
3672}
3673
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003674static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
3675{
3676 return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
3677 (!idxattr || idxattr == attrid);
3678}
3679
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02003680#define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
3681static int rtnl_get_offload_stats_attr_size(int attr_id)
3682{
3683 switch (attr_id) {
3684 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
3685 return sizeof(struct rtnl_link_stats64);
3686 }
3687
3688 return 0;
3689}
3690
3691static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
3692 int *prividx)
3693{
3694 struct nlattr *attr = NULL;
3695 int attr_id, size;
3696 void *attr_data;
3697 int err;
3698
3699 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
3700 dev->netdev_ops->ndo_get_offload_stats))
3701 return -ENODATA;
3702
3703 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
3704 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
3705 if (attr_id < *prividx)
3706 continue;
3707
3708 size = rtnl_get_offload_stats_attr_size(attr_id);
3709 if (!size)
3710 continue;
3711
3712 if (!dev->netdev_ops->ndo_has_offload_stats(attr_id))
3713 continue;
3714
3715 attr = nla_reserve_64bit(skb, attr_id, size,
3716 IFLA_OFFLOAD_XSTATS_UNSPEC);
3717 if (!attr)
3718 goto nla_put_failure;
3719
3720 attr_data = nla_data(attr);
3721 memset(attr_data, 0, size);
3722 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
3723 attr_data);
3724 if (err)
3725 goto get_offload_stats_failure;
3726 }
3727
3728 if (!attr)
3729 return -ENODATA;
3730
3731 *prividx = 0;
3732 return 0;
3733
3734nla_put_failure:
3735 err = -EMSGSIZE;
3736get_offload_stats_failure:
3737 *prividx = attr_id;
3738 return err;
3739}
3740
3741static int rtnl_get_offload_stats_size(const struct net_device *dev)
3742{
3743 int nla_size = 0;
3744 int attr_id;
3745 int size;
3746
3747 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
3748 dev->netdev_ops->ndo_get_offload_stats))
3749 return 0;
3750
3751 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
3752 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
3753 if (!dev->netdev_ops->ndo_has_offload_stats(attr_id))
3754 continue;
3755 size = rtnl_get_offload_stats_attr_size(attr_id);
3756 nla_size += nla_total_size_64bit(size);
3757 }
3758
3759 if (nla_size != 0)
3760 nla_size += nla_total_size(0);
3761
3762 return nla_size;
3763}
3764
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003765static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
3766 int type, u32 pid, u32 seq, u32 change,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003767 unsigned int flags, unsigned int filter_mask,
3768 int *idxattr, int *prividx)
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003769{
3770 struct if_stats_msg *ifsm;
3771 struct nlmsghdr *nlh;
3772 struct nlattr *attr;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003773 int s_prividx = *prividx;
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02003774 int err;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003775
3776 ASSERT_RTNL();
3777
3778 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
3779 if (!nlh)
3780 return -EMSGSIZE;
3781
3782 ifsm = nlmsg_data(nlh);
Nikolay Aleksandrov95206ea2017-10-03 13:20:48 +03003783 ifsm->family = PF_UNSPEC;
3784 ifsm->pad1 = 0;
3785 ifsm->pad2 = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003786 ifsm->ifindex = dev->ifindex;
3787 ifsm->filter_mask = filter_mask;
3788
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003789 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003790 struct rtnl_link_stats64 *sp;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003791
Nicolas Dichtel58414d32016-04-21 18:58:25 +02003792 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
3793 sizeof(struct rtnl_link_stats64),
3794 IFLA_STATS_UNSPEC);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003795 if (!attr)
3796 goto nla_put_failure;
3797
3798 sp = nla_data(attr);
3799 dev_get_stats(dev, sp);
3800 }
3801
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02003802 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
3803 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
3804
3805 if (ops && ops->fill_linkxstats) {
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02003806 *idxattr = IFLA_STATS_LINK_XSTATS;
3807 attr = nla_nest_start(skb,
3808 IFLA_STATS_LINK_XSTATS);
3809 if (!attr)
3810 goto nla_put_failure;
3811
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02003812 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
3813 nla_nest_end(skb, attr);
3814 if (err)
3815 goto nla_put_failure;
3816 *idxattr = 0;
3817 }
3818 }
3819
3820 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
3821 *idxattr)) {
3822 const struct rtnl_link_ops *ops = NULL;
3823 const struct net_device *master;
3824
3825 master = netdev_master_upper_dev_get(dev);
3826 if (master)
3827 ops = master->rtnl_link_ops;
3828 if (ops && ops->fill_linkxstats) {
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02003829 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
3830 attr = nla_nest_start(skb,
3831 IFLA_STATS_LINK_XSTATS_SLAVE);
3832 if (!attr)
3833 goto nla_put_failure;
3834
3835 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02003836 nla_nest_end(skb, attr);
3837 if (err)
3838 goto nla_put_failure;
3839 *idxattr = 0;
3840 }
3841 }
3842
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02003843 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
3844 *idxattr)) {
3845 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
3846 attr = nla_nest_start(skb, IFLA_STATS_LINK_OFFLOAD_XSTATS);
3847 if (!attr)
3848 goto nla_put_failure;
3849
3850 err = rtnl_get_offload_stats(skb, dev, prividx);
3851 if (err == -ENODATA)
3852 nla_nest_cancel(skb, attr);
3853 else
3854 nla_nest_end(skb, attr);
3855
3856 if (err && err != -ENODATA)
3857 goto nla_put_failure;
3858 *idxattr = 0;
3859 }
3860
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003861 nlmsg_end(skb, nlh);
3862
3863 return 0;
3864
3865nla_put_failure:
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003866 /* not a multi message or no progress mean a real error */
3867 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
3868 nlmsg_cancel(skb, nlh);
3869 else
3870 nlmsg_end(skb, nlh);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003871
3872 return -EMSGSIZE;
3873}
3874
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003875static size_t if_nlmsg_stats_size(const struct net_device *dev,
3876 u32 filter_mask)
3877{
3878 size_t size = 0;
3879
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003880 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003881 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
3882
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02003883 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
3884 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02003885 int attr = IFLA_STATS_LINK_XSTATS;
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02003886
3887 if (ops && ops->get_linkxstats_size) {
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02003888 size += nla_total_size(ops->get_linkxstats_size(dev,
3889 attr));
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02003890 /* for IFLA_STATS_LINK_XSTATS */
3891 size += nla_total_size(0);
3892 }
3893 }
3894
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02003895 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
3896 struct net_device *_dev = (struct net_device *)dev;
3897 const struct rtnl_link_ops *ops = NULL;
3898 const struct net_device *master;
3899
3900 /* netdev_master_upper_dev_get can't take const */
3901 master = netdev_master_upper_dev_get(_dev);
3902 if (master)
3903 ops = master->rtnl_link_ops;
3904 if (ops && ops->get_linkxstats_size) {
3905 int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
3906
3907 size += nla_total_size(ops->get_linkxstats_size(dev,
3908 attr));
3909 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
3910 size += nla_total_size(0);
3911 }
3912 }
3913
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02003914 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
3915 size += rtnl_get_offload_stats_size(dev);
3916
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003917 return size;
3918}
3919
3920static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh)
3921{
3922 struct net *net = sock_net(skb->sk);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003923 struct net_device *dev = NULL;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003924 int idxattr = 0, prividx = 0;
3925 struct if_stats_msg *ifsm;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003926 struct sk_buff *nskb;
3927 u32 filter_mask;
3928 int err;
3929
Mathias Krause2ffc6942016-12-28 17:52:15 +01003930 if (nlmsg_len(nlh) < sizeof(*ifsm))
3931 return -EINVAL;
3932
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003933 ifsm = nlmsg_data(nlh);
3934 if (ifsm->ifindex > 0)
3935 dev = __dev_get_by_index(net, ifsm->ifindex);
3936 else
3937 return -EINVAL;
3938
3939 if (!dev)
3940 return -ENODEV;
3941
3942 filter_mask = ifsm->filter_mask;
3943 if (!filter_mask)
3944 return -EINVAL;
3945
3946 nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
3947 if (!nskb)
3948 return -ENOBUFS;
3949
3950 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
3951 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003952 0, filter_mask, &idxattr, &prividx);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003953 if (err < 0) {
3954 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
3955 WARN_ON(err == -EMSGSIZE);
3956 kfree_skb(nskb);
3957 } else {
3958 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
3959 }
3960
3961 return err;
3962}
3963
3964static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
3965{
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003966 int h, s_h, err, s_idx, s_idxattr, s_prividx;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003967 struct net *net = sock_net(skb->sk);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003968 unsigned int flags = NLM_F_MULTI;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003969 struct if_stats_msg *ifsm;
3970 struct hlist_head *head;
3971 struct net_device *dev;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003972 u32 filter_mask = 0;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003973 int idx = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003974
3975 s_h = cb->args[0];
3976 s_idx = cb->args[1];
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003977 s_idxattr = cb->args[2];
3978 s_prividx = cb->args[3];
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003979
3980 cb->seq = net->dev_base_seq;
3981
Mathias Krause2ffc6942016-12-28 17:52:15 +01003982 if (nlmsg_len(cb->nlh) < sizeof(*ifsm))
3983 return -EINVAL;
3984
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003985 ifsm = nlmsg_data(cb->nlh);
3986 filter_mask = ifsm->filter_mask;
3987 if (!filter_mask)
3988 return -EINVAL;
3989
3990 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
3991 idx = 0;
3992 head = &net->dev_index_head[h];
3993 hlist_for_each_entry(dev, head, index_hlist) {
3994 if (idx < s_idx)
3995 goto cont;
3996 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
3997 NETLINK_CB(cb->skb).portid,
3998 cb->nlh->nlmsg_seq, 0,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003999 flags, filter_mask,
4000 &s_idxattr, &s_prividx);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004001 /* If we ran out of room on the first message,
4002 * we're in trouble
4003 */
4004 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
4005
4006 if (err < 0)
4007 goto out;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004008 s_prividx = 0;
4009 s_idxattr = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004010 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
4011cont:
4012 idx++;
4013 }
4014 }
4015out:
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004016 cb->args[3] = s_prividx;
4017 cb->args[2] = s_idxattr;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004018 cb->args[1] = idx;
4019 cb->args[0] = h;
4020
4021 return skb->len;
4022}
4023
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024/* Process one rtnetlink message. */
4025
Thomas Graf1d00a4e2007-03-22 23:30:12 -07004026static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004028 struct net *net = sock_net(skb->sk);
Thomas Grafe2849862007-03-22 11:48:11 -07004029 rtnl_doit_func doit;
Alexander Kuleshov617cfc72016-01-10 21:26:57 +06004030 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 int family;
4032 int type;
Eric Dumazet2907c352011-05-25 07:34:04 +00004033 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 if (type > RTM_MAX)
Thomas Graf038890f2007-04-05 14:35:52 -07004037 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038
4039 type -= RTM_BASE;
4040
4041 /* All the messages must have at least 1 byte length */
Hong zhi guo573ce262013-03-27 06:47:04 +00004042 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 return 0;
4044
Hong zhi guo573ce262013-03-27 06:47:04 +00004045 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 kind = type&3;
4047
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07004048 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07004049 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050
David S. Millerb8f3ab42011-01-18 12:40:38 -08004051 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004052 struct sock *rtnl;
Thomas Grafe2849862007-03-22 11:48:11 -07004053 rtnl_dumpit_func dumpit;
Greg Rosec7ac8672011-06-10 01:27:09 +00004054 rtnl_calcit_func calcit;
4055 u16 min_dump_alloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056
Thomas Grafe2849862007-03-22 11:48:11 -07004057 dumpit = rtnl_get_dumpit(family, type);
4058 if (dumpit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07004059 return -EOPNOTSUPP;
Greg Rosec7ac8672011-06-10 01:27:09 +00004060 calcit = rtnl_get_calcit(family, type);
4061 if (calcit)
Greg Rose115c9b82012-02-21 16:54:48 -05004062 min_dump_alloc = calcit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063
Eric Dumazet2907c352011-05-25 07:34:04 +00004064 __rtnl_unlock();
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004065 rtnl = net->rtnl;
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00004066 {
4067 struct netlink_dump_control c = {
4068 .dump = dumpit,
4069 .min_dump_alloc = min_dump_alloc,
4070 };
4071 err = netlink_dump_start(rtnl, skb, nlh, &c);
4072 }
Eric Dumazet2907c352011-05-25 07:34:04 +00004073 rtnl_lock();
4074 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 }
4076
Thomas Grafe2849862007-03-22 11:48:11 -07004077 doit = rtnl_get_doit(family, type);
4078 if (doit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07004079 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080
Thomas Graf661d2962013-03-21 07:45:29 +00004081 return doit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082}
4083
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07004084static void rtnetlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07004086 rtnl_lock();
4087 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
4088 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089}
4090
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
4092{
Jiri Pirko351638e2013-05-28 01:30:21 +00004093 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02004094
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 switch (event) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 case NETDEV_UP:
4097 case NETDEV_DOWN:
Patrick McHardy10de05a2010-02-26 06:34:50 +00004098 case NETDEV_PRE_UP:
Eric W. Biedermand90a9092009-12-12 22:11:15 +00004099 case NETDEV_POST_INIT:
4100 case NETDEV_REGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 case NETDEV_CHANGE:
Patrick McHardy755d0e72010-03-19 04:42:24 +00004102 case NETDEV_PRE_TYPE_CHANGE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 case NETDEV_GOING_DOWN:
Patrick McHardya2835762010-02-26 06:34:51 +00004104 case NETDEV_UNREGISTER:
Eric Dumazet0115e8e2012-08-22 17:19:46 +00004105 case NETDEV_UNREGISTER_FINAL:
Amerigo Wangac3d3f82011-05-19 23:06:32 +00004106 case NETDEV_RELEASE:
4107 case NETDEV_JOIN:
Moni Shoua61bd3852015-02-03 16:48:29 +02004108 case NETDEV_BONDING_INFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 break;
4110 default:
Alexei Starovoitov7f294052013-10-23 16:02:42 -07004111 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 break;
4113 }
4114 return NOTIFY_DONE;
4115}
4116
4117static struct notifier_block rtnetlink_dev_notifier = {
4118 .notifier_call = rtnetlink_event,
4119};
4120
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004121
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004122static int __net_init rtnetlink_net_init(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004123{
4124 struct sock *sk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00004125 struct netlink_kernel_cfg cfg = {
4126 .groups = RTNLGRP_MAX,
4127 .input = rtnetlink_rcv,
4128 .cb_mutex = &rtnl_mutex,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00004129 .flags = NL_CFG_F_NONROOT_RECV,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00004130 };
4131
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00004132 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004133 if (!sk)
4134 return -ENOMEM;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004135 net->rtnl = sk;
4136 return 0;
4137}
4138
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004139static void __net_exit rtnetlink_net_exit(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004140{
Denis V. Lunev775516b2008-01-18 23:55:19 -08004141 netlink_kernel_release(net->rtnl);
4142 net->rtnl = NULL;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004143}
4144
4145static struct pernet_operations rtnetlink_net_ops = {
4146 .init = rtnetlink_net_init,
4147 .exit = rtnetlink_net_exit,
4148};
4149
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150void __init rtnetlink_init(void)
4151{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004152 if (register_pernet_subsys(&rtnetlink_net_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 panic("rtnetlink_init: cannot initialize rtnetlink\n");
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004154
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 register_netdevice_notifier(&rtnetlink_dev_notifier);
Thomas Graf340d17f2007-03-22 11:49:22 -07004156
Greg Rosec7ac8672011-06-10 01:27:09 +00004157 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
4158 rtnl_dump_ifinfo, rtnl_calcit);
4159 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
4160 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
4161 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
Thomas Graf687ad8c2007-03-22 11:59:42 -07004162
Greg Rosec7ac8672011-06-10 01:27:09 +00004163 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
4164 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
John Fastabend77162022012-04-15 06:43:56 +00004165
4166 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL);
4167 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL);
4168 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL);
John Fastabende5a55a82012-10-24 08:12:57 +00004169
4170 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL);
Vlad Yasevich407af322013-02-13 12:00:12 +00004171 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL);
John Fastabende5a55a82012-10-24 08:12:57 +00004172 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004173
4174 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
4175 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176}