blob: 11b25fbf3dd29d92ed496eaf951ffe21c18e2cfe [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * Routing netlink socket interface: protocol independent part.
7 *
8 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
14 *
15 * Fixes:
16 * Vitaly E. Lavrov RTA_OK arithmetics was wrong.
17 */
18
Jakub Kicinskiee5d0322017-06-21 18:25:04 -070019#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/errno.h>
21#include <linux/module.h>
22#include <linux/types.h>
23#include <linux/socket.h>
24#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/timer.h>
26#include <linux/string.h>
27#include <linux/sockios.h>
28#include <linux/net.h>
29#include <linux/fcntl.h>
30#include <linux/mm.h>
31#include <linux/slab.h>
32#include <linux/interrupt.h>
33#include <linux/capability.h>
34#include <linux/skbuff.h>
35#include <linux/init.h>
36#include <linux/security.h>
Stephen Hemminger6756ae42006-03-20 22:23:58 -080037#include <linux/mutex.h>
Thomas Graf18237302006-08-04 23:04:54 -070038#include <linux/if_addr.h>
John Fastabend77162022012-04-15 06:43:56 +000039#include <linux/if_bridge.h>
Jiri Pirkof6f64242014-11-28 14:34:15 +010040#include <linux/if_vlan.h>
Williams, Mitch Aebc08a62010-02-10 01:44:05 +000041#include <linux/pci.h>
John Fastabend77162022012-04-15 06:43:56 +000042#include <linux/etherdevice.h>
Martin KaFai Lau58038692017-06-15 17:29:09 -070043#include <linux/bpf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080045#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#include <linux/inet.h>
48#include <linux/netdevice.h>
Jiri Pirko82f28412014-11-28 14:34:18 +010049#include <net/switchdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <net/ip.h>
51#include <net/protocol.h>
52#include <net/arp.h>
53#include <net/route.h>
54#include <net/udp.h>
Daniel Borkmannea697632015-01-05 23:57:47 +010055#include <net/tcp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <net/sock.h>
57#include <net/pkt_sched.h>
Thomas Graf14c0b972006-08-04 03:38:38 -070058#include <net/fib_rules.h>
Thomas Grafe2849862007-03-22 11:48:11 -070059#include <net/rtnetlink.h>
Johannes Berg30ffee82009-07-10 09:51:35 +000060#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Eric Dumazete0d087a2009-11-07 01:26:17 -080062struct rtnl_link {
Thomas Grafe2849862007-03-22 11:48:11 -070063 rtnl_doit_func doit;
64 rtnl_dumpit_func dumpit;
Greg Rosec7ac8672011-06-10 01:27:09 +000065 rtnl_calcit_func calcit;
Thomas Grafe2849862007-03-22 11:48:11 -070066};
67
Stephen Hemminger6756ae42006-03-20 22:23:58 -080068static DEFINE_MUTEX(rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70void rtnl_lock(void)
71{
Stephen Hemminger6756ae42006-03-20 22:23:58 -080072 mutex_lock(&rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
Eric Dumazete0d087a2009-11-07 01:26:17 -080074EXPORT_SYMBOL(rtnl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Eric Dumazet1b5c5492016-06-13 20:21:50 -070076static struct sk_buff *defer_kfree_skb_list;
77void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail)
78{
79 if (head && tail) {
80 tail->next = defer_kfree_skb_list;
81 defer_kfree_skb_list = head;
82 }
83}
84EXPORT_SYMBOL(rtnl_kfree_skbs);
85
Stephen Hemminger6756ae42006-03-20 22:23:58 -080086void __rtnl_unlock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Eric Dumazet1b5c5492016-06-13 20:21:50 -070088 struct sk_buff *head = defer_kfree_skb_list;
89
90 defer_kfree_skb_list = NULL;
91
Stephen Hemminger6756ae42006-03-20 22:23:58 -080092 mutex_unlock(&rtnl_mutex);
Eric Dumazet1b5c5492016-06-13 20:21:50 -070093
94 while (head) {
95 struct sk_buff *next = head->next;
96
97 kfree_skb(head);
98 cond_resched();
99 head = next;
100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103void rtnl_unlock(void)
104{
Herbert Xu58ec3b42008-10-07 15:50:03 -0700105 /* This fellow will unlock it for us. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 netdev_run_todo();
107}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800108EXPORT_SYMBOL(rtnl_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800110int rtnl_trylock(void)
111{
112 return mutex_trylock(&rtnl_mutex);
113}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800114EXPORT_SYMBOL(rtnl_trylock);
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800115
Patrick McHardyc9c10142008-04-23 22:10:48 -0700116int rtnl_is_locked(void)
117{
118 return mutex_is_locked(&rtnl_mutex);
119}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800120EXPORT_SYMBOL(rtnl_is_locked);
Patrick McHardyc9c10142008-04-23 22:10:48 -0700121
Paul E. McKenneya898def2010-02-22 17:04:49 -0800122#ifdef CONFIG_PROVE_LOCKING
Yaowei Bai0cbf3342015-10-08 21:29:02 +0800123bool lockdep_rtnl_is_held(void)
Paul E. McKenneya898def2010-02-22 17:04:49 -0800124{
125 return lockdep_is_held(&rtnl_mutex);
126}
127EXPORT_SYMBOL(lockdep_rtnl_is_held);
128#endif /* #ifdef CONFIG_PROVE_LOCKING */
129
Patrick McHardy25239ce2010-04-26 16:02:05 +0200130static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
Thomas Grafe2849862007-03-22 11:48:11 -0700131
132static inline int rtm_msgindex(int msgtype)
133{
134 int msgindex = msgtype - RTM_BASE;
135
136 /*
137 * msgindex < 0 implies someone tried to register a netlink
138 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
139 * the message type has not been added to linux/rtnetlink.h
140 */
141 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
142
143 return msgindex;
144}
145
146static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
147{
148 struct rtnl_link *tab;
149
Patrick McHardy25239ce2010-04-26 16:02:05 +0200150 if (protocol <= RTNL_FAMILY_MAX)
Patrick McHardy0f87b1d2010-04-13 05:03:17 +0000151 tab = rtnl_msg_handlers[protocol];
152 else
153 tab = NULL;
154
Thomas Graf51057f22007-03-22 21:41:06 -0700155 if (tab == NULL || tab[msgindex].doit == NULL)
Thomas Grafe2849862007-03-22 11:48:11 -0700156 tab = rtnl_msg_handlers[PF_UNSPEC];
157
Hans Zhangc80bbea2012-10-22 22:21:23 +0000158 return tab[msgindex].doit;
Thomas Grafe2849862007-03-22 11:48:11 -0700159}
160
161static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
162{
163 struct rtnl_link *tab;
164
Patrick McHardy25239ce2010-04-26 16:02:05 +0200165 if (protocol <= RTNL_FAMILY_MAX)
Patrick McHardy0f87b1d2010-04-13 05:03:17 +0000166 tab = rtnl_msg_handlers[protocol];
167 else
168 tab = NULL;
169
Thomas Graf51057f22007-03-22 21:41:06 -0700170 if (tab == NULL || tab[msgindex].dumpit == NULL)
Thomas Grafe2849862007-03-22 11:48:11 -0700171 tab = rtnl_msg_handlers[PF_UNSPEC];
172
Hans Zhangc80bbea2012-10-22 22:21:23 +0000173 return tab[msgindex].dumpit;
Thomas Grafe2849862007-03-22 11:48:11 -0700174}
175
Greg Rosec7ac8672011-06-10 01:27:09 +0000176static rtnl_calcit_func rtnl_get_calcit(int protocol, int msgindex)
177{
178 struct rtnl_link *tab;
179
180 if (protocol <= RTNL_FAMILY_MAX)
181 tab = rtnl_msg_handlers[protocol];
182 else
183 tab = NULL;
184
185 if (tab == NULL || tab[msgindex].calcit == NULL)
186 tab = rtnl_msg_handlers[PF_UNSPEC];
187
Hans Zhangc80bbea2012-10-22 22:21:23 +0000188 return tab[msgindex].calcit;
Greg Rosec7ac8672011-06-10 01:27:09 +0000189}
190
Thomas Grafe2849862007-03-22 11:48:11 -0700191/**
192 * __rtnl_register - Register a rtnetlink message type
193 * @protocol: Protocol family or PF_UNSPEC
194 * @msgtype: rtnetlink message type
195 * @doit: Function pointer called for each request message
196 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
Greg Rosec7ac8672011-06-10 01:27:09 +0000197 * @calcit: Function pointer to calc size of dump message
Thomas Grafe2849862007-03-22 11:48:11 -0700198 *
199 * Registers the specified function pointers (at least one of them has
200 * to be non-NULL) to be called whenever a request message for the
201 * specified protocol family and message type is received.
202 *
203 * The special protocol family PF_UNSPEC may be used to define fallback
204 * function pointers for the case when no entry for the specific protocol
205 * family exists.
206 *
207 * Returns 0 on success or a negative error code.
208 */
209int __rtnl_register(int protocol, int msgtype,
Greg Rosec7ac8672011-06-10 01:27:09 +0000210 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
211 rtnl_calcit_func calcit)
Thomas Grafe2849862007-03-22 11:48:11 -0700212{
213 struct rtnl_link *tab;
214 int msgindex;
215
Patrick McHardy25239ce2010-04-26 16:02:05 +0200216 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700217 msgindex = rtm_msgindex(msgtype);
218
219 tab = rtnl_msg_handlers[protocol];
220 if (tab == NULL) {
221 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
222 if (tab == NULL)
223 return -ENOBUFS;
224
225 rtnl_msg_handlers[protocol] = tab;
226 }
227
228 if (doit)
229 tab[msgindex].doit = doit;
230
231 if (dumpit)
232 tab[msgindex].dumpit = dumpit;
233
Greg Rosec7ac8672011-06-10 01:27:09 +0000234 if (calcit)
235 tab[msgindex].calcit = calcit;
236
Thomas Grafe2849862007-03-22 11:48:11 -0700237 return 0;
238}
Thomas Grafe2849862007-03-22 11:48:11 -0700239EXPORT_SYMBOL_GPL(__rtnl_register);
240
241/**
242 * rtnl_register - Register a rtnetlink message type
243 *
244 * Identical to __rtnl_register() but panics on failure. This is useful
245 * as failure of this function is very unlikely, it can only happen due
246 * to lack of memory when allocating the chain to store all message
247 * handlers for a protocol. Meant for use in init functions where lack
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300248 * of memory implies no sense in continuing.
Thomas Grafe2849862007-03-22 11:48:11 -0700249 */
250void rtnl_register(int protocol, int msgtype,
Greg Rosec7ac8672011-06-10 01:27:09 +0000251 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
252 rtnl_calcit_func calcit)
Thomas Grafe2849862007-03-22 11:48:11 -0700253{
Greg Rosec7ac8672011-06-10 01:27:09 +0000254 if (__rtnl_register(protocol, msgtype, doit, dumpit, calcit) < 0)
Thomas Grafe2849862007-03-22 11:48:11 -0700255 panic("Unable to register rtnetlink message handler, "
256 "protocol = %d, message type = %d\n",
257 protocol, msgtype);
258}
Thomas Grafe2849862007-03-22 11:48:11 -0700259EXPORT_SYMBOL_GPL(rtnl_register);
260
261/**
262 * rtnl_unregister - Unregister a rtnetlink message type
263 * @protocol: Protocol family or PF_UNSPEC
264 * @msgtype: rtnetlink message type
265 *
266 * Returns 0 on success or a negative error code.
267 */
268int rtnl_unregister(int protocol, int msgtype)
269{
270 int msgindex;
271
Patrick McHardy25239ce2010-04-26 16:02:05 +0200272 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700273 msgindex = rtm_msgindex(msgtype);
274
275 if (rtnl_msg_handlers[protocol] == NULL)
276 return -ENOENT;
277
278 rtnl_msg_handlers[protocol][msgindex].doit = NULL;
279 rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
Mathias Krausef567e952016-11-07 23:22:19 +0100280 rtnl_msg_handlers[protocol][msgindex].calcit = NULL;
Thomas Grafe2849862007-03-22 11:48:11 -0700281
282 return 0;
283}
Thomas Grafe2849862007-03-22 11:48:11 -0700284EXPORT_SYMBOL_GPL(rtnl_unregister);
285
286/**
287 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
288 * @protocol : Protocol family or PF_UNSPEC
289 *
290 * Identical to calling rtnl_unregster() for all registered message types
291 * of a certain protocol family.
292 */
293void rtnl_unregister_all(int protocol)
294{
Patrick McHardy25239ce2010-04-26 16:02:05 +0200295 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700296
297 kfree(rtnl_msg_handlers[protocol]);
298 rtnl_msg_handlers[protocol] = NULL;
299}
Thomas Grafe2849862007-03-22 11:48:11 -0700300EXPORT_SYMBOL_GPL(rtnl_unregister_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Patrick McHardy38f7b872007-06-13 12:03:51 -0700302static LIST_HEAD(link_ops);
303
Eric Dumazetc63044f2011-12-13 11:38:00 +0000304static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
305{
306 const struct rtnl_link_ops *ops;
307
308 list_for_each_entry(ops, &link_ops, list) {
309 if (!strcmp(ops->kind, kind))
310 return ops;
311 }
312 return NULL;
313}
314
Patrick McHardy38f7b872007-06-13 12:03:51 -0700315/**
316 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
317 * @ops: struct rtnl_link_ops * to register
318 *
319 * The caller must hold the rtnl_mutex. This function should be used
320 * by drivers that create devices during module initialization. It
321 * must be called before registering the devices.
322 *
323 * Returns 0 on success or a negative error code.
324 */
325int __rtnl_link_register(struct rtnl_link_ops *ops)
326{
Eric Dumazetc63044f2011-12-13 11:38:00 +0000327 if (rtnl_link_ops_get(ops->kind))
328 return -EEXIST;
329
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +0200330 /* The check for setup is here because if ops
331 * does not have that filled up, it is not possible
332 * to use the ops for creating device. So do not
333 * fill up dellink as well. That disables rtnl_dellink.
334 */
335 if (ops->setup && !ops->dellink)
Eric Dumazet23289a32009-10-27 07:06:36 +0000336 ops->dellink = unregister_netdevice_queue;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700337
Patrick McHardy38f7b872007-06-13 12:03:51 -0700338 list_add_tail(&ops->list, &link_ops);
339 return 0;
340}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700341EXPORT_SYMBOL_GPL(__rtnl_link_register);
342
343/**
344 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
345 * @ops: struct rtnl_link_ops * to register
346 *
347 * Returns 0 on success or a negative error code.
348 */
349int rtnl_link_register(struct rtnl_link_ops *ops)
350{
351 int err;
352
353 rtnl_lock();
354 err = __rtnl_link_register(ops);
355 rtnl_unlock();
356 return err;
357}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700358EXPORT_SYMBOL_GPL(rtnl_link_register);
359
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700360static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
361{
362 struct net_device *dev;
Eric Dumazet23289a32009-10-27 07:06:36 +0000363 LIST_HEAD(list_kill);
364
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700365 for_each_netdev(net, dev) {
Eric Dumazet23289a32009-10-27 07:06:36 +0000366 if (dev->rtnl_link_ops == ops)
367 ops->dellink(dev, &list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700368 }
Eric Dumazet23289a32009-10-27 07:06:36 +0000369 unregister_netdevice_many(&list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700370}
371
Patrick McHardy38f7b872007-06-13 12:03:51 -0700372/**
373 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
374 * @ops: struct rtnl_link_ops * to unregister
375 *
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700376 * The caller must hold the rtnl_mutex.
Patrick McHardy38f7b872007-06-13 12:03:51 -0700377 */
378void __rtnl_link_unregister(struct rtnl_link_ops *ops)
379{
Eric W. Biederman881d9662007-09-17 11:56:21 -0700380 struct net *net;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700381
Eric W. Biederman881d9662007-09-17 11:56:21 -0700382 for_each_net(net) {
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700383 __rtnl_kill_links(net, ops);
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700384 }
Patrick McHardy38f7b872007-06-13 12:03:51 -0700385 list_del(&ops->list);
386}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700387EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
388
Cong Wang200b9162014-05-12 15:11:20 -0700389/* Return with the rtnl_lock held when there are no network
390 * devices unregistering in any network namespace.
391 */
392static void rtnl_lock_unregistering_all(void)
393{
394 struct net *net;
395 bool unregistering;
Peter Zijlstraff960a72014-10-29 17:04:56 +0100396 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Cong Wang200b9162014-05-12 15:11:20 -0700397
Peter Zijlstraff960a72014-10-29 17:04:56 +0100398 add_wait_queue(&netdev_unregistering_wq, &wait);
Cong Wang200b9162014-05-12 15:11:20 -0700399 for (;;) {
Cong Wang200b9162014-05-12 15:11:20 -0700400 unregistering = false;
401 rtnl_lock();
402 for_each_net(net) {
403 if (net->dev_unreg_count > 0) {
404 unregistering = true;
405 break;
406 }
407 }
408 if (!unregistering)
409 break;
410 __rtnl_unlock();
Peter Zijlstraff960a72014-10-29 17:04:56 +0100411
412 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Cong Wang200b9162014-05-12 15:11:20 -0700413 }
Peter Zijlstraff960a72014-10-29 17:04:56 +0100414 remove_wait_queue(&netdev_unregistering_wq, &wait);
Cong Wang200b9162014-05-12 15:11:20 -0700415}
416
Patrick McHardy38f7b872007-06-13 12:03:51 -0700417/**
418 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
419 * @ops: struct rtnl_link_ops * to unregister
420 */
421void rtnl_link_unregister(struct rtnl_link_ops *ops)
422{
Cong Wang200b9162014-05-12 15:11:20 -0700423 /* Close the race with cleanup_net() */
424 mutex_lock(&net_mutex);
425 rtnl_lock_unregistering_all();
Patrick McHardy38f7b872007-06-13 12:03:51 -0700426 __rtnl_link_unregister(ops);
427 rtnl_unlock();
Cong Wang200b9162014-05-12 15:11:20 -0700428 mutex_unlock(&net_mutex);
Patrick McHardy38f7b872007-06-13 12:03:51 -0700429}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700430EXPORT_SYMBOL_GPL(rtnl_link_unregister);
431
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100432static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
433{
434 struct net_device *master_dev;
435 const struct rtnl_link_ops *ops;
436
437 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
438 if (!master_dev)
439 return 0;
440 ops = master_dev->rtnl_link_ops;
Fernando Luis Vazquez Cao6049f252014-02-04 19:35:02 +0900441 if (!ops || !ops->get_slave_size)
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100442 return 0;
443 /* IFLA_INFO_SLAVE_DATA + nested data */
444 return nla_total_size(sizeof(struct nlattr)) +
445 ops->get_slave_size(master_dev, dev);
446}
447
Patrick McHardy38f7b872007-06-13 12:03:51 -0700448static size_t rtnl_link_get_size(const struct net_device *dev)
449{
450 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
451 size_t size;
452
453 if (!ops)
454 return 0;
455
Thomas Graf369cf772010-11-11 15:47:59 +0000456 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
457 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700458
459 if (ops->get_size)
460 /* IFLA_INFO_DATA + nested data */
Thomas Graf369cf772010-11-11 15:47:59 +0000461 size += nla_total_size(sizeof(struct nlattr)) +
Patrick McHardy38f7b872007-06-13 12:03:51 -0700462 ops->get_size(dev);
463
464 if (ops->get_xstats_size)
Thomas Graf369cf772010-11-11 15:47:59 +0000465 /* IFLA_INFO_XSTATS */
466 size += nla_total_size(ops->get_xstats_size(dev));
Patrick McHardy38f7b872007-06-13 12:03:51 -0700467
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100468 size += rtnl_link_get_slave_info_data_size(dev);
469
Patrick McHardy38f7b872007-06-13 12:03:51 -0700470 return size;
471}
472
Thomas Graff8ff1822010-11-16 04:30:14 +0000473static LIST_HEAD(rtnl_af_ops);
474
475static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
476{
477 const struct rtnl_af_ops *ops;
478
479 list_for_each_entry(ops, &rtnl_af_ops, list) {
480 if (ops->family == family)
481 return ops;
482 }
483
484 return NULL;
485}
486
487/**
Thomas Graff8ff1822010-11-16 04:30:14 +0000488 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
489 * @ops: struct rtnl_af_ops * to register
490 *
491 * Returns 0 on success or a negative error code.
492 */
stephen hemminger3678a9d2013-12-30 10:41:32 -0800493void rtnl_af_register(struct rtnl_af_ops *ops)
Thomas Graff8ff1822010-11-16 04:30:14 +0000494{
Thomas Graff8ff1822010-11-16 04:30:14 +0000495 rtnl_lock();
stephen hemminger3678a9d2013-12-30 10:41:32 -0800496 list_add_tail(&ops->list, &rtnl_af_ops);
Thomas Graff8ff1822010-11-16 04:30:14 +0000497 rtnl_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +0000498}
499EXPORT_SYMBOL_GPL(rtnl_af_register);
500
501/**
502 * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
503 * @ops: struct rtnl_af_ops * to unregister
504 *
505 * The caller must hold the rtnl_mutex.
506 */
507void __rtnl_af_unregister(struct rtnl_af_ops *ops)
508{
509 list_del(&ops->list);
510}
511EXPORT_SYMBOL_GPL(__rtnl_af_unregister);
512
513/**
514 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
515 * @ops: struct rtnl_af_ops * to unregister
516 */
517void rtnl_af_unregister(struct rtnl_af_ops *ops)
518{
519 rtnl_lock();
520 __rtnl_af_unregister(ops);
521 rtnl_unlock();
522}
523EXPORT_SYMBOL_GPL(rtnl_af_unregister);
524
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700525static size_t rtnl_link_get_af_size(const struct net_device *dev,
526 u32 ext_filter_mask)
Thomas Graff8ff1822010-11-16 04:30:14 +0000527{
528 struct rtnl_af_ops *af_ops;
529 size_t size;
530
531 /* IFLA_AF_SPEC */
532 size = nla_total_size(sizeof(struct nlattr));
533
534 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
535 if (af_ops->get_link_af_size) {
536 /* AF_* + nested data */
537 size += nla_total_size(sizeof(struct nlattr)) +
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700538 af_ops->get_link_af_size(dev, ext_filter_mask);
Thomas Graff8ff1822010-11-16 04:30:14 +0000539 }
540 }
541
542 return size;
543}
544
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100545static bool rtnl_have_link_slave_info(const struct net_device *dev)
546{
547 struct net_device *master_dev;
548
549 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
Jiri Pirko813f0202014-01-23 19:19:21 +0100550 if (master_dev && master_dev->rtnl_link_ops)
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100551 return true;
552 return false;
553}
554
555static int rtnl_link_slave_info_fill(struct sk_buff *skb,
556 const struct net_device *dev)
557{
558 struct net_device *master_dev;
559 const struct rtnl_link_ops *ops;
560 struct nlattr *slave_data;
561 int err;
562
563 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
564 if (!master_dev)
565 return 0;
566 ops = master_dev->rtnl_link_ops;
567 if (!ops)
568 return 0;
569 if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
570 return -EMSGSIZE;
571 if (ops->fill_slave_info) {
572 slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA);
573 if (!slave_data)
574 return -EMSGSIZE;
575 err = ops->fill_slave_info(skb, master_dev, dev);
576 if (err < 0)
577 goto err_cancel_slave_data;
578 nla_nest_end(skb, slave_data);
579 }
580 return 0;
581
582err_cancel_slave_data:
583 nla_nest_cancel(skb, slave_data);
584 return err;
585}
586
587static int rtnl_link_info_fill(struct sk_buff *skb,
588 const struct net_device *dev)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700589{
590 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100591 struct nlattr *data;
592 int err;
593
594 if (!ops)
595 return 0;
596 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
597 return -EMSGSIZE;
598 if (ops->fill_xstats) {
599 err = ops->fill_xstats(skb, dev);
600 if (err < 0)
601 return err;
602 }
603 if (ops->fill_info) {
604 data = nla_nest_start(skb, IFLA_INFO_DATA);
605 if (data == NULL)
606 return -EMSGSIZE;
607 err = ops->fill_info(skb, dev);
608 if (err < 0)
609 goto err_cancel_data;
610 nla_nest_end(skb, data);
611 }
612 return 0;
613
614err_cancel_data:
615 nla_nest_cancel(skb, data);
616 return err;
617}
618
619static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
620{
621 struct nlattr *linkinfo;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700622 int err = -EMSGSIZE;
623
624 linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
625 if (linkinfo == NULL)
626 goto out;
627
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100628 err = rtnl_link_info_fill(skb, dev);
629 if (err < 0)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700630 goto err_cancel_link;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100631
632 err = rtnl_link_slave_info_fill(skb, dev);
633 if (err < 0)
634 goto err_cancel_link;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700635
636 nla_nest_end(skb, linkinfo);
637 return 0;
638
Patrick McHardy38f7b872007-06-13 12:03:51 -0700639err_cancel_link:
640 nla_nest_cancel(skb, linkinfo);
641out:
642 return err;
643}
644
Eric Dumazet95c96172012-04-15 05:58:06 +0000645int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800647 struct sock *rtnl = net->rtnl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 int err = 0;
649
Patrick McHardyac6d4392005-08-14 19:29:52 -0700650 NETLINK_CB(skb).dst_group = group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (echo)
Reshetova, Elena63354792017-06-30 13:07:58 +0300652 refcount_inc(&skb->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
654 if (echo)
655 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
656 return err;
657}
658
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800659int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
Thomas Graf2942e902006-08-15 00:30:25 -0700660{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800661 struct sock *rtnl = net->rtnl;
662
Thomas Graf2942e902006-08-15 00:30:25 -0700663 return nlmsg_unicast(rtnl, skb, pid);
664}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800665EXPORT_SYMBOL(rtnl_unicast);
Thomas Graf2942e902006-08-15 00:30:25 -0700666
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800667void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
668 struct nlmsghdr *nlh, gfp_t flags)
Thomas Graf97676b62006-08-15 00:31:41 -0700669{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800670 struct sock *rtnl = net->rtnl;
Thomas Graf97676b62006-08-15 00:31:41 -0700671 int report = 0;
672
673 if (nlh)
674 report = nlmsg_report(nlh);
675
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800676 nlmsg_notify(rtnl, skb, pid, group, report, flags);
Thomas Graf97676b62006-08-15 00:31:41 -0700677}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800678EXPORT_SYMBOL(rtnl_notify);
Thomas Graf97676b62006-08-15 00:31:41 -0700679
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800680void rtnl_set_sk_err(struct net *net, u32 group, int error)
Thomas Graf97676b62006-08-15 00:31:41 -0700681{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800682 struct sock *rtnl = net->rtnl;
683
Thomas Graf97676b62006-08-15 00:31:41 -0700684 netlink_set_err(rtnl, 0, group, error);
685}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800686EXPORT_SYMBOL(rtnl_set_sk_err);
Thomas Graf97676b62006-08-15 00:31:41 -0700687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
689{
Thomas Graf2d7202b2006-08-22 00:01:27 -0700690 struct nlattr *mx;
691 int i, valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Thomas Graf2d7202b2006-08-22 00:01:27 -0700693 mx = nla_nest_start(skb, RTA_METRICS);
694 if (mx == NULL)
695 return -ENOBUFS;
696
697 for (i = 0; i < RTAX_MAX; i++) {
698 if (metrics[i]) {
Daniel Borkmannea697632015-01-05 23:57:47 +0100699 if (i == RTAX_CC_ALGO - 1) {
700 char tmp[TCP_CA_NAME_MAX], *name;
701
702 name = tcp_ca_get_name_by_key(metrics[i], tmp);
703 if (!name)
704 continue;
705 if (nla_put_string(skb, i + 1, name))
706 goto nla_put_failure;
Daniel Borkmannc3a8d942015-08-31 15:58:47 +0200707 } else if (i == RTAX_FEATURES - 1) {
708 u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
709
Phil Sutterf8edcd12016-08-23 13:14:31 +0200710 if (!user_features)
711 continue;
Daniel Borkmannc3a8d942015-08-31 15:58:47 +0200712 BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
713 if (nla_put_u32(skb, i + 1, user_features))
714 goto nla_put_failure;
Daniel Borkmannea697632015-01-05 23:57:47 +0100715 } else {
716 if (nla_put_u32(skb, i + 1, metrics[i]))
717 goto nla_put_failure;
718 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700719 valid++;
Thomas Graf2d7202b2006-08-22 00:01:27 -0700720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
David S. Millera57d27f2006-08-22 22:20:14 -0700723 if (!valid) {
724 nla_nest_cancel(skb, mx);
725 return 0;
726 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700727
728 return nla_nest_end(skb, mx);
729
730nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700731 nla_nest_cancel(skb, mx);
732 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800734EXPORT_SYMBOL(rtnetlink_put_metrics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Thomas Grafe3703b32006-11-27 09:27:07 -0800736int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
David S. Miller87a50692012-07-10 05:06:14 -0700737 long expires, u32 error)
Thomas Grafe3703b32006-11-27 09:27:07 -0800738{
739 struct rta_cacheinfo ci = {
Eric Dumazeta399a802012-08-08 21:13:53 +0000740 .rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse),
Thomas Grafe3703b32006-11-27 09:27:07 -0800741 .rta_used = dst->__use,
742 .rta_clntref = atomic_read(&(dst->__refcnt)),
743 .rta_error = error,
744 .rta_id = id,
Thomas Grafe3703b32006-11-27 09:27:07 -0800745 };
746
Li Wei82539472012-07-29 16:01:30 +0000747 if (expires) {
748 unsigned long clock;
Thomas Grafe3703b32006-11-27 09:27:07 -0800749
Li Wei82539472012-07-29 16:01:30 +0000750 clock = jiffies_to_clock_t(abs(expires));
751 clock = min_t(unsigned long, clock, INT_MAX);
752 ci.rta_expires = (expires > 0) ? clock : -clock;
753 }
Thomas Grafe3703b32006-11-27 09:27:07 -0800754 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
755}
Thomas Grafe3703b32006-11-27 09:27:07 -0800756EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
David S. Miller93b2d4a2008-02-17 18:35:07 -0800758static void set_operstate(struct net_device *dev, unsigned char transition)
Stefan Rompfb00055a2006-03-20 17:09:11 -0800759{
760 unsigned char operstate = dev->operstate;
761
Eric Dumazete0d087a2009-11-07 01:26:17 -0800762 switch (transition) {
Stefan Rompfb00055a2006-03-20 17:09:11 -0800763 case IF_OPER_UP:
764 if ((operstate == IF_OPER_DORMANT ||
765 operstate == IF_OPER_UNKNOWN) &&
766 !netif_dormant(dev))
767 operstate = IF_OPER_UP;
768 break;
769
770 case IF_OPER_DORMANT:
771 if (operstate == IF_OPER_UP ||
772 operstate == IF_OPER_UNKNOWN)
773 operstate = IF_OPER_DORMANT;
774 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700775 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800776
777 if (dev->operstate != operstate) {
778 write_lock_bh(&dev_base_lock);
779 dev->operstate = operstate;
780 write_unlock_bh(&dev_base_lock);
David S. Miller93b2d4a2008-02-17 18:35:07 -0800781 netdev_state_change(dev);
782 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800783}
784
Jiri Bencb1beb682012-07-27 02:58:22 +0000785static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
786{
787 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
788 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
789}
790
Patrick McHardy3729d502010-02-26 06:34:54 +0000791static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
792 const struct ifinfomsg *ifm)
793{
794 unsigned int flags = ifm->ifi_flags;
795
796 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
797 if (ifm->ifi_change)
798 flags = (flags & ifm->ifi_change) |
Jiri Bencb1beb682012-07-27 02:58:22 +0000799 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
Patrick McHardy3729d502010-02-26 06:34:54 +0000800
801 return flags;
802}
803
Thomas Grafb60c5112006-08-04 23:05:34 -0700804static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000805 const struct rtnl_link_stats64 *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
Thomas Grafb60c5112006-08-04 23:05:34 -0700807 a->rx_packets = b->rx_packets;
808 a->tx_packets = b->tx_packets;
809 a->rx_bytes = b->rx_bytes;
810 a->tx_bytes = b->tx_bytes;
811 a->rx_errors = b->rx_errors;
812 a->tx_errors = b->tx_errors;
813 a->rx_dropped = b->rx_dropped;
814 a->tx_dropped = b->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Thomas Grafb60c5112006-08-04 23:05:34 -0700816 a->multicast = b->multicast;
817 a->collisions = b->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Thomas Grafb60c5112006-08-04 23:05:34 -0700819 a->rx_length_errors = b->rx_length_errors;
820 a->rx_over_errors = b->rx_over_errors;
821 a->rx_crc_errors = b->rx_crc_errors;
822 a->rx_frame_errors = b->rx_frame_errors;
823 a->rx_fifo_errors = b->rx_fifo_errors;
824 a->rx_missed_errors = b->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Thomas Grafb60c5112006-08-04 23:05:34 -0700826 a->tx_aborted_errors = b->tx_aborted_errors;
827 a->tx_carrier_errors = b->tx_carrier_errors;
828 a->tx_fifo_errors = b->tx_fifo_errors;
829 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
830 a->tx_window_errors = b->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Thomas Grafb60c5112006-08-04 23:05:34 -0700832 a->rx_compressed = b->rx_compressed;
833 a->tx_compressed = b->tx_compressed;
Jarod Wilson6e7333d2016-02-01 18:51:05 -0500834
835 a->rx_nohandler = b->rx_nohandler;
Jan Engelhardt10708f32010-03-11 09:57:29 +0000836}
837
Chris Wrightc02db8c2010-05-16 01:05:45 -0700838/* All VF info */
Greg Rose115c9b82012-02-21 16:54:48 -0500839static inline int rtnl_vfinfo_size(const struct net_device *dev,
840 u32 ext_filter_mask)
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000841{
Phil Sutter9af15c32017-01-18 14:04:39 +0100842 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
Chris Wrightc02db8c2010-05-16 01:05:45 -0700843 int num_vfs = dev_num_vf(dev->dev.parent);
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100844 size_t size = nla_total_size(0);
Scott Feldman045de012010-05-28 03:42:43 -0700845 size += num_vfs *
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100846 (nla_total_size(0) +
847 nla_total_size(sizeof(struct ifla_vf_mac)) +
848 nla_total_size(sizeof(struct ifla_vf_vlan)) +
849 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
Moshe Shemesh79aab092016-09-22 12:11:15 +0300850 nla_total_size(MAX_VLAN_LIST_LEN *
851 sizeof(struct ifla_vf_vlan_info)) +
Sucheta Chakrabortyed616682014-05-22 09:59:05 -0400852 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100853 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
Jiri Benc945a3672014-08-08 16:44:32 +0200854 nla_total_size(sizeof(struct ifla_vf_rate)) +
Vlad Zolotarov01a3d792015-03-30 21:35:23 +0300855 nla_total_size(sizeof(struct ifla_vf_link_state)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300856 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100857 nla_total_size(0) + /* nest IFLA_VF_STATS */
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300858 /* IFLA_VF_STATS_RX_PACKETS */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200859 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300860 /* IFLA_VF_STATS_TX_PACKETS */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200861 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300862 /* IFLA_VF_STATS_RX_BYTES */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200863 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300864 /* IFLA_VF_STATS_TX_BYTES */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200865 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300866 /* IFLA_VF_STATS_BROADCAST */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200867 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300868 /* IFLA_VF_STATS_MULTICAST */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200869 nla_total_size_64bit(sizeof(__u64)) +
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +0000870 nla_total_size(sizeof(struct ifla_vf_trust)));
Chris Wrightc02db8c2010-05-16 01:05:45 -0700871 return size;
872 } else
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000873 return 0;
874}
875
David Gibsonc53864f2014-04-24 10:22:36 +1000876static size_t rtnl_port_size(const struct net_device *dev,
877 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -0700878{
879 size_t port_size = nla_total_size(4) /* PORT_VF */
880 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
Scott Feldman57b61082010-05-17 22:49:55 -0700881 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
882 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
883 + nla_total_size(1) /* PROT_VDP_REQUEST */
884 + nla_total_size(2); /* PORT_VDP_RESPONSE */
885 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
886 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
887 + port_size;
888 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
889 + port_size;
890
David Gibsonc53864f2014-04-24 10:22:36 +1000891 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
892 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -0700893 return 0;
894 if (dev_num_vf(dev->dev.parent))
895 return port_self_size + vf_ports_size +
896 vf_port_size * dev_num_vf(dev->dev.parent);
897 else
898 return port_self_size;
899}
900
David S. Millerb5cdae32017-04-18 15:36:58 -0400901static size_t rtnl_xdp_size(void)
Brenden Blancod1fdd912016-07-19 12:16:49 -0700902{
Sabrina Dubrocab3cfaa32016-11-15 11:16:35 +0100903 size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */
Martin KaFai Lau58038692017-06-15 17:29:09 -0700904 nla_total_size(1) + /* XDP_ATTACHED */
905 nla_total_size(4); /* XDP_PROG_ID */
Brenden Blancod1fdd912016-07-19 12:16:49 -0700906
David S. Millerb5cdae32017-04-18 15:36:58 -0400907 return xdp_size;
Brenden Blancod1fdd912016-07-19 12:16:49 -0700908}
909
Greg Rose115c9b82012-02-21 16:54:48 -0500910static noinline size_t if_nlmsg_size(const struct net_device *dev,
911 u32 ext_filter_mask)
Thomas Graf339bf982006-11-10 14:10:15 -0800912{
913 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
914 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700915 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
Thomas Graf339bf982006-11-10 14:10:15 -0800916 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
Nicolas Dichtel270cb4d2016-04-26 10:06:16 +0200917 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
Thomas Graf339bf982006-11-10 14:10:15 -0800918 + nla_total_size(sizeof(struct rtnl_link_stats))
David S. Miller35c58452016-04-19 19:49:29 -0400919 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
Thomas Graf339bf982006-11-10 14:10:15 -0800920 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
921 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
922 + nla_total_size(4) /* IFLA_TXQLEN */
923 + nla_total_size(4) /* IFLA_WEIGHT */
924 + nla_total_size(4) /* IFLA_MTU */
925 + nla_total_size(4) /* IFLA_LINK */
926 + nla_total_size(4) /* IFLA_MASTER */
Jiri Pirko9a572472012-12-27 23:49:39 +0000927 + nla_total_size(1) /* IFLA_CARRIER */
Ben Greearedbc0bb2012-03-29 12:51:30 +0000928 + nla_total_size(4) /* IFLA_PROMISCUITY */
Jiri Pirko76ff5cc2012-07-20 02:28:48 +0000929 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
930 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
Tobias Klauser69197562016-11-30 14:30:37 +0100931 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
932 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
Thomas Graf339bf982006-11-10 14:10:15 -0800933 + nla_total_size(1) /* IFLA_OPERSTATE */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700934 + nla_total_size(1) /* IFLA_LINKMODE */
david decotigny2d3b4792014-03-29 09:48:35 -0700935 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
Nicolas Dichteld37512a2015-01-15 15:11:16 +0100936 + nla_total_size(4) /* IFLA_LINK_NETNSID */
Serhey Popovychdb833d42017-06-20 14:35:23 +0300937 + nla_total_size(4) /* IFLA_GROUP */
Greg Rose115c9b82012-02-21 16:54:48 -0500938 + nla_total_size(ext_filter_mask
939 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
940 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
David Gibsonc53864f2014-04-24 10:22:36 +1000941 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
Thomas Graff8ff1822010-11-16 04:30:14 +0000942 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700943 + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
Jiri Pirko82f28412014-11-28 14:34:18 +0100944 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
Anuradha Karuppiah88d63782015-07-14 13:43:20 -0700945 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
Nicolas Dichtelc57c7a92016-03-31 18:10:31 +0200946 + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
David S. Millerb5cdae32017-04-18 15:36:58 -0400947 + rtnl_xdp_size() /* IFLA_XDP */
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -0400948 + nla_total_size(4) /* IFLA_EVENT */
Anuradha Karuppiah88d63782015-07-14 13:43:20 -0700949 + nla_total_size(1); /* IFLA_PROTO_DOWN */
950
Thomas Graf339bf982006-11-10 14:10:15 -0800951}
952
Scott Feldman57b61082010-05-17 22:49:55 -0700953static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
954{
955 struct nlattr *vf_ports;
956 struct nlattr *vf_port;
957 int vf;
958 int err;
959
960 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
961 if (!vf_ports)
962 return -EMSGSIZE;
963
964 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
965 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
Scott Feldman8ca94182010-05-28 03:42:18 -0700966 if (!vf_port)
967 goto nla_put_failure;
David S. Millera6574342012-04-01 20:12:00 -0400968 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
969 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -0700970 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
Scott Feldman8ca94182010-05-28 03:42:18 -0700971 if (err == -EMSGSIZE)
972 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -0700973 if (err) {
Scott Feldman57b61082010-05-17 22:49:55 -0700974 nla_nest_cancel(skb, vf_port);
975 continue;
976 }
977 nla_nest_end(skb, vf_port);
978 }
979
980 nla_nest_end(skb, vf_ports);
981
982 return 0;
Scott Feldman8ca94182010-05-28 03:42:18 -0700983
984nla_put_failure:
985 nla_nest_cancel(skb, vf_ports);
986 return -EMSGSIZE;
Scott Feldman57b61082010-05-17 22:49:55 -0700987}
988
989static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
990{
991 struct nlattr *port_self;
992 int err;
993
994 port_self = nla_nest_start(skb, IFLA_PORT_SELF);
995 if (!port_self)
996 return -EMSGSIZE;
997
998 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
999 if (err) {
1000 nla_nest_cancel(skb, port_self);
Scott Feldman8ca94182010-05-28 03:42:18 -07001001 return (err == -EMSGSIZE) ? err : 0;
Scott Feldman57b61082010-05-17 22:49:55 -07001002 }
1003
1004 nla_nest_end(skb, port_self);
1005
1006 return 0;
1007}
1008
David Gibsonc53864f2014-04-24 10:22:36 +10001009static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1010 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -07001011{
1012 int err;
1013
David Gibsonc53864f2014-04-24 10:22:36 +10001014 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1015 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -07001016 return 0;
1017
1018 err = rtnl_port_self_fill(skb, dev);
1019 if (err)
1020 return err;
1021
1022 if (dev_num_vf(dev->dev.parent)) {
1023 err = rtnl_vf_ports_fill(skb, dev);
1024 if (err)
1025 return err;
1026 }
1027
1028 return 0;
1029}
1030
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001031static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
1032{
1033 int err;
Jiri Pirko02637fc2014-11-28 14:34:16 +01001034 struct netdev_phys_item_id ppid;
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001035
1036 err = dev_get_phys_port_id(dev, &ppid);
1037 if (err) {
1038 if (err == -EOPNOTSUPP)
1039 return 0;
1040 return err;
1041 }
1042
1043 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1044 return -EMSGSIZE;
1045
1046 return 0;
1047}
1048
David Aherndb24a902015-03-17 20:23:15 -06001049static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1050{
1051 char name[IFNAMSIZ];
1052 int err;
1053
1054 err = dev_get_phys_port_name(dev, name, sizeof(name));
1055 if (err) {
1056 if (err == -EOPNOTSUPP)
1057 return 0;
1058 return err;
1059 }
1060
Michal Schmidt77ef0332017-05-04 16:48:58 +02001061 if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
David Aherndb24a902015-03-17 20:23:15 -06001062 return -EMSGSIZE;
1063
1064 return 0;
1065}
1066
Jiri Pirko82f28412014-11-28 14:34:18 +01001067static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1068{
1069 int err;
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001070 struct switchdev_attr attr = {
Ido Schimmel6ff64f62015-12-15 16:03:35 +01001071 .orig_dev = dev,
Jiri Pirko1f868392015-10-01 11:03:42 +02001072 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001073 .flags = SWITCHDEV_F_NO_RECURSE,
1074 };
Jiri Pirko82f28412014-11-28 14:34:18 +01001075
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001076 err = switchdev_port_attr_get(dev, &attr);
Jiri Pirko82f28412014-11-28 14:34:18 +01001077 if (err) {
1078 if (err == -EOPNOTSUPP)
1079 return 0;
1080 return err;
1081 }
1082
Scott Feldman42275bd2015-05-13 11:16:50 -07001083 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len,
1084 attr.u.ppid.id))
Jiri Pirko82f28412014-11-28 14:34:18 +01001085 return -EMSGSIZE;
1086
1087 return 0;
1088}
1089
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001090static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1091 struct net_device *dev)
1092{
Roopa Prabhu550bce52016-04-15 20:36:25 -07001093 struct rtnl_link_stats64 *sp;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001094 struct nlattr *attr;
1095
Nicolas Dichtel58414d32016-04-21 18:58:25 +02001096 attr = nla_reserve_64bit(skb, IFLA_STATS64,
1097 sizeof(struct rtnl_link_stats64), IFLA_PAD);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001098 if (!attr)
1099 return -EMSGSIZE;
1100
Roopa Prabhu550bce52016-04-15 20:36:25 -07001101 sp = nla_data(attr);
1102 dev_get_stats(dev, sp);
1103
1104 attr = nla_reserve(skb, IFLA_STATS,
1105 sizeof(struct rtnl_link_stats));
1106 if (!attr)
1107 return -EMSGSIZE;
1108
1109 copy_rtnl_link_stats(nla_data(attr), sp);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001110
1111 return 0;
1112}
1113
1114static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1115 struct net_device *dev,
1116 int vfs_num,
1117 struct nlattr *vfinfo)
1118{
1119 struct ifla_vf_rss_query_en vf_rss_query_en;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001120 struct nlattr *vf, *vfstats, *vfvlanlist;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001121 struct ifla_vf_link_state vf_linkstate;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001122 struct ifla_vf_vlan_info vf_vlan_info;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001123 struct ifla_vf_spoofchk vf_spoofchk;
1124 struct ifla_vf_tx_rate vf_tx_rate;
1125 struct ifla_vf_stats vf_stats;
1126 struct ifla_vf_trust vf_trust;
1127 struct ifla_vf_vlan vf_vlan;
1128 struct ifla_vf_rate vf_rate;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001129 struct ifla_vf_mac vf_mac;
1130 struct ifla_vf_info ivi;
1131
Mintz, Yuval0eed9cf2017-06-07 21:00:33 +03001132 memset(&ivi, 0, sizeof(ivi));
1133
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001134 /* Not all SR-IOV capable drivers support the
1135 * spoofcheck and "RSS query enable" query. Preset to
1136 * -1 so the user space tool can detect that the driver
1137 * didn't report anything.
1138 */
1139 ivi.spoofchk = -1;
1140 ivi.rss_query_en = -1;
1141 ivi.trusted = -1;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001142 /* The default value for VF link state is "auto"
1143 * IFLA_VF_LINK_STATE_AUTO which equals zero
1144 */
1145 ivi.linkstate = 0;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001146 /* VLAN Protocol by default is 802.1Q */
1147 ivi.vlan_proto = htons(ETH_P_8021Q);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001148 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1149 return 0;
1150
Dan Carpenter775f4f02016-10-13 11:45:28 +03001151 memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
1152
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001153 vf_mac.vf =
1154 vf_vlan.vf =
Moshe Shemesh79aab092016-09-22 12:11:15 +03001155 vf_vlan_info.vf =
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001156 vf_rate.vf =
1157 vf_tx_rate.vf =
1158 vf_spoofchk.vf =
1159 vf_linkstate.vf =
1160 vf_rss_query_en.vf =
1161 vf_trust.vf = ivi.vf;
1162
1163 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1164 vf_vlan.vlan = ivi.vlan;
1165 vf_vlan.qos = ivi.qos;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001166 vf_vlan_info.vlan = ivi.vlan;
1167 vf_vlan_info.qos = ivi.qos;
1168 vf_vlan_info.vlan_proto = ivi.vlan_proto;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001169 vf_tx_rate.rate = ivi.max_tx_rate;
1170 vf_rate.min_tx_rate = ivi.min_tx_rate;
1171 vf_rate.max_tx_rate = ivi.max_tx_rate;
1172 vf_spoofchk.setting = ivi.spoofchk;
1173 vf_linkstate.link_state = ivi.linkstate;
1174 vf_rss_query_en.setting = ivi.rss_query_en;
1175 vf_trust.setting = ivi.trusted;
1176 vf = nla_nest_start(skb, IFLA_VF_INFO);
Moshe Shemesh79aab092016-09-22 12:11:15 +03001177 if (!vf)
1178 goto nla_put_vfinfo_failure;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001179 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1180 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1181 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1182 &vf_rate) ||
1183 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1184 &vf_tx_rate) ||
1185 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1186 &vf_spoofchk) ||
1187 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1188 &vf_linkstate) ||
1189 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1190 sizeof(vf_rss_query_en),
1191 &vf_rss_query_en) ||
1192 nla_put(skb, IFLA_VF_TRUST,
1193 sizeof(vf_trust), &vf_trust))
Moshe Shemesh79aab092016-09-22 12:11:15 +03001194 goto nla_put_vf_failure;
1195 vfvlanlist = nla_nest_start(skb, IFLA_VF_VLAN_LIST);
1196 if (!vfvlanlist)
1197 goto nla_put_vf_failure;
1198 if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
1199 &vf_vlan_info)) {
1200 nla_nest_cancel(skb, vfvlanlist);
1201 goto nla_put_vf_failure;
1202 }
1203 nla_nest_end(skb, vfvlanlist);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001204 memset(&vf_stats, 0, sizeof(vf_stats));
1205 if (dev->netdev_ops->ndo_get_vf_stats)
1206 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1207 &vf_stats);
1208 vfstats = nla_nest_start(skb, IFLA_VF_STATS);
Moshe Shemesh79aab092016-09-22 12:11:15 +03001209 if (!vfstats)
1210 goto nla_put_vf_failure;
Nicolas Dichtel343a6d82016-04-25 10:25:14 +02001211 if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1212 vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1213 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1214 vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1215 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1216 vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1217 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1218 vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1219 nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1220 vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1221 nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
Moshe Shemesh79aab092016-09-22 12:11:15 +03001222 vf_stats.multicast, IFLA_VF_STATS_PAD)) {
1223 nla_nest_cancel(skb, vfstats);
1224 goto nla_put_vf_failure;
1225 }
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001226 nla_nest_end(skb, vfstats);
1227 nla_nest_end(skb, vf);
1228 return 0;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001229
1230nla_put_vf_failure:
1231 nla_nest_cancel(skb, vf);
1232nla_put_vfinfo_failure:
1233 nla_nest_cancel(skb, vfinfo);
1234 return -EMSGSIZE;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001235}
1236
1237static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1238{
Kangjie Lu5f8e4472016-05-03 16:46:24 -04001239 struct rtnl_link_ifmap map;
1240
1241 memset(&map, 0, sizeof(map));
1242 map.mem_start = dev->mem_start;
1243 map.mem_end = dev->mem_end;
1244 map.base_addr = dev->base_addr;
1245 map.irq = dev->irq;
1246 map.dma = dev->dma;
1247 map.port = dev->if_port;
1248
Nicolas Dichtel270cb4d2016-04-26 10:06:16 +02001249 if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001250 return -EMSGSIZE;
1251
1252 return 0;
1253}
1254
Martin KaFai Lau58038692017-06-15 17:29:09 -07001255static u8 rtnl_xdp_attached_mode(struct net_device *dev, u32 *prog_id)
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001256{
1257 const struct net_device_ops *ops = dev->netdev_ops;
Martin KaFai Lau58038692017-06-15 17:29:09 -07001258 const struct bpf_prog *generic_xdp_prog;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001259
1260 ASSERT_RTNL();
1261
Martin KaFai Lau58038692017-06-15 17:29:09 -07001262 *prog_id = 0;
1263 generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
1264 if (generic_xdp_prog) {
1265 *prog_id = generic_xdp_prog->aux->id;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001266 return XDP_ATTACHED_SKB;
Martin KaFai Lau58038692017-06-15 17:29:09 -07001267 }
Jakub Kicinskice158e52017-06-21 18:25:09 -07001268 if (!ops->ndo_xdp)
1269 return XDP_ATTACHED_NONE;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001270
Jakub Kicinskice158e52017-06-21 18:25:09 -07001271 return __dev_xdp_attached(dev, ops->ndo_xdp, prog_id);
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001272}
1273
Brenden Blancod1fdd912016-07-19 12:16:49 -07001274static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1275{
Brenden Blancod1fdd912016-07-19 12:16:49 -07001276 struct nlattr *xdp;
Martin KaFai Lau58038692017-06-15 17:29:09 -07001277 u32 prog_id;
Brenden Blancod1fdd912016-07-19 12:16:49 -07001278 int err;
1279
Brenden Blancod1fdd912016-07-19 12:16:49 -07001280 xdp = nla_nest_start(skb, IFLA_XDP);
1281 if (!xdp)
1282 return -EMSGSIZE;
David S. Millerb5cdae32017-04-18 15:36:58 -04001283
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001284 err = nla_put_u8(skb, IFLA_XDP_ATTACHED,
Martin KaFai Lau58038692017-06-15 17:29:09 -07001285 rtnl_xdp_attached_mode(dev, &prog_id));
Brenden Blancod1fdd912016-07-19 12:16:49 -07001286 if (err)
1287 goto err_cancel;
1288
Martin KaFai Lau58038692017-06-15 17:29:09 -07001289 if (prog_id) {
1290 err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
1291 if (err)
1292 goto err_cancel;
1293 }
1294
Brenden Blancod1fdd912016-07-19 12:16:49 -07001295 nla_nest_end(skb, xdp);
1296 return 0;
1297
1298err_cancel:
1299 nla_nest_cancel(skb, xdp);
1300 return err;
1301}
1302
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001303static u32 rtnl_get_event(unsigned long event)
1304{
1305 u32 rtnl_event_type = IFLA_EVENT_NONE;
1306
1307 switch (event) {
1308 case NETDEV_REBOOT:
1309 rtnl_event_type = IFLA_EVENT_REBOOT;
1310 break;
1311 case NETDEV_FEAT_CHANGE:
1312 rtnl_event_type = IFLA_EVENT_FEATURES;
1313 break;
1314 case NETDEV_BONDING_FAILOVER:
1315 rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
1316 break;
1317 case NETDEV_NOTIFY_PEERS:
1318 rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
1319 break;
1320 case NETDEV_RESEND_IGMP:
1321 rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
1322 break;
1323 case NETDEV_CHANGEINFODATA:
1324 rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
1325 break;
1326 default:
1327 break;
1328 }
1329
1330 return rtnl_event_type;
1331}
1332
Thomas Grafb60c5112006-08-04 23:05:34 -07001333static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
Patrick McHardy575c3e22007-05-22 17:00:49 -07001334 int type, u32 pid, u32 seq, u32 change,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001335 unsigned int flags, u32 ext_filter_mask,
1336 u32 event)
Thomas Grafb60c5112006-08-04 23:05:34 -07001337{
1338 struct ifinfomsg *ifm;
1339 struct nlmsghdr *nlh;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001340 struct nlattr *af_spec;
Thomas Graff8ff1822010-11-16 04:30:14 +00001341 struct rtnl_af_ops *af_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00001342 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Thomas Grafb60c5112006-08-04 23:05:34 -07001343
Eric Dumazet2907c352011-05-25 07:34:04 +00001344 ASSERT_RTNL();
Thomas Grafb60c5112006-08-04 23:05:34 -07001345 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1346 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08001347 return -EMSGSIZE;
Thomas Grafb60c5112006-08-04 23:05:34 -07001348
1349 ifm = nlmsg_data(nlh);
1350 ifm->ifi_family = AF_UNSPEC;
1351 ifm->__ifi_pad = 0;
1352 ifm->ifi_type = dev->type;
1353 ifm->ifi_index = dev->ifindex;
1354 ifm->ifi_flags = dev_get_flags(dev);
1355 ifm->ifi_change = change;
1356
David S. Millera6574342012-04-01 20:12:00 -04001357 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1358 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1359 nla_put_u8(skb, IFLA_OPERSTATE,
1360 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1361 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1362 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1363 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
Ben Greearedbc0bb2012-03-29 12:51:30 +00001364 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001365 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
Eric Dumazetc70ce022016-03-21 09:55:10 -07001366 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1367 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001368#ifdef CONFIG_RPS
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001369 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001370#endif
Nicolas Dichtela54acb32015-04-02 17:07:00 +02001371 (dev->ifindex != dev_get_iflink(dev) &&
1372 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00001373 (upper_dev &&
1374 nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
Jiri Pirko9a572472012-12-27 23:49:39 +00001375 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
David S. Millera6574342012-04-01 20:12:00 -04001376 (dev->qdisc &&
1377 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1378 (dev->ifalias &&
david decotigny2d3b4792014-03-29 09:48:35 -07001379 nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) ||
1380 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001381 atomic_read(&dev->carrier_changes)) ||
1382 nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
David S. Millera6574342012-04-01 20:12:00 -04001383 goto nla_put_failure;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001384
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001385 if (event != IFLA_EVENT_NONE) {
1386 if (nla_put_u32(skb, IFLA_EVENT, event))
1387 goto nla_put_failure;
1388 }
1389
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001390 if (rtnl_fill_link_ifmap(skb, dev))
1391 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
1393 if (dev->addr_len) {
David S. Millera6574342012-04-01 20:12:00 -04001394 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1395 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1396 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 }
1398
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001399 if (rtnl_phys_port_id_fill(skb, dev))
1400 goto nla_put_failure;
1401
David Aherndb24a902015-03-17 20:23:15 -06001402 if (rtnl_phys_port_name_fill(skb, dev))
1403 goto nla_put_failure;
1404
Jiri Pirko82f28412014-11-28 14:34:18 +01001405 if (rtnl_phys_switch_id_fill(skb, dev))
1406 goto nla_put_failure;
1407
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001408 if (rtnl_fill_stats(skb, dev))
Pavel Emelyanov96e74082008-05-21 14:12:46 -07001409 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
David S. Millera6574342012-04-01 20:12:00 -04001411 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
1412 nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
1413 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001414
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001415 if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent &&
1416 ext_filter_mask & RTEXT_FILTER_VF) {
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001417 int i;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001418 struct nlattr *vfinfo;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001419 int num_vfs = dev_num_vf(dev->dev.parent);
1420
Chris Wrightc02db8c2010-05-16 01:05:45 -07001421 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1422 if (!vfinfo)
1423 goto nla_put_failure;
1424 for (i = 0; i < num_vfs; i++) {
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001425 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
Chris Wrightc02db8c2010-05-16 01:05:45 -07001426 goto nla_put_failure;
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001427 }
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001428
Chris Wrightc02db8c2010-05-16 01:05:45 -07001429 nla_nest_end(skb, vfinfo);
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001430 }
Scott Feldman57b61082010-05-17 22:49:55 -07001431
David Gibsonc53864f2014-04-24 10:22:36 +10001432 if (rtnl_port_fill(skb, dev, ext_filter_mask))
Scott Feldman57b61082010-05-17 22:49:55 -07001433 goto nla_put_failure;
1434
Brenden Blancod1fdd912016-07-19 12:16:49 -07001435 if (rtnl_xdp_fill(skb, dev))
1436 goto nla_put_failure;
1437
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001438 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07001439 if (rtnl_link_fill(skb, dev) < 0)
1440 goto nla_put_failure;
1441 }
1442
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001443 if (dev->rtnl_link_ops &&
1444 dev->rtnl_link_ops->get_link_net) {
1445 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1446
1447 if (!net_eq(dev_net(dev), link_net)) {
Nicolas Dichtel7a0877d2015-05-07 11:02:49 +02001448 int id = peernet2id_alloc(dev_net(dev), link_net);
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001449
1450 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1451 goto nla_put_failure;
1452 }
1453 }
1454
Thomas Graff8ff1822010-11-16 04:30:14 +00001455 if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1456 goto nla_put_failure;
1457
1458 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1459 if (af_ops->fill_link_af) {
1460 struct nlattr *af;
1461 int err;
1462
1463 if (!(af = nla_nest_start(skb, af_ops->family)))
1464 goto nla_put_failure;
1465
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04001466 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
Thomas Graff8ff1822010-11-16 04:30:14 +00001467
1468 /*
1469 * Caller may return ENODATA to indicate that there
1470 * was no data to be dumped. This is not an error, it
1471 * means we should trim the attribute header and
1472 * continue.
1473 */
1474 if (err == -ENODATA)
1475 nla_nest_cancel(skb, af);
1476 else if (err < 0)
1477 goto nla_put_failure;
1478
1479 nla_nest_end(skb, af);
1480 }
1481 }
1482
1483 nla_nest_end(skb, af_spec);
1484
Johannes Berg053c0952015-01-16 22:09:00 +01001485 nlmsg_end(skb, nlh);
1486 return 0;
Thomas Grafb60c5112006-08-04 23:05:34 -07001487
1488nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001489 nlmsg_cancel(skb, nlh);
1490 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491}
1492
Jiri Pirkof7b12602014-02-18 20:53:18 +01001493static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1494 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1495 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1496 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1497 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1498 [IFLA_MTU] = { .type = NLA_U32 },
1499 [IFLA_LINK] = { .type = NLA_U32 },
1500 [IFLA_MASTER] = { .type = NLA_U32 },
1501 [IFLA_CARRIER] = { .type = NLA_U8 },
1502 [IFLA_TXQLEN] = { .type = NLA_U32 },
1503 [IFLA_WEIGHT] = { .type = NLA_U32 },
1504 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1505 [IFLA_LINKMODE] = { .type = NLA_U8 },
1506 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1507 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1508 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
1509 [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 },
1510 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1511 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1512 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1513 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1514 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1515 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1516 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1517 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
Jiri Pirko02637fc2014-11-28 14:34:16 +01001518 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
david decotigny2d3b4792014-03-29 09:48:35 -07001519 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
Jiri Pirko82f28412014-11-28 14:34:18 +01001520 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
Nicolas Dichtel317f4812015-01-15 15:11:18 +01001521 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001522 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001523 [IFLA_XDP] = { .type = NLA_NESTED },
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001524 [IFLA_EVENT] = { .type = NLA_U32 },
Serhey Popovychdb833d42017-06-20 14:35:23 +03001525 [IFLA_GROUP] = { .type = NLA_U32 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001526};
1527
1528static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1529 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1530 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1531 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1532 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1533};
1534
Jiri Pirkof7b12602014-02-18 20:53:18 +01001535static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
Daniel Borkmann364d5712015-02-05 18:44:04 +01001536 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1537 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
Moshe Shemesh79aab092016-09-22 12:11:15 +03001538 [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED },
Daniel Borkmann364d5712015-02-05 18:44:04 +01001539 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1540 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1541 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1542 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001543 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001544 [IFLA_VF_STATS] = { .type = NLA_NESTED },
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00001545 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
Eli Cohencc8e27c2016-03-11 22:58:34 +02001546 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1547 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001548};
1549
Jiri Pirkof7b12602014-02-18 20:53:18 +01001550static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1551 [IFLA_PORT_VF] = { .type = NLA_U32 },
1552 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1553 .len = PORT_PROFILE_MAX },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001554 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1555 .len = PORT_UUID_MAX },
1556 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1557 .len = PORT_UUID_MAX },
1558 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1559 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
Daniel Borkmann025331d2017-02-17 01:56:11 +01001560
1561 /* Unused, but we need to keep it here since user space could
1562 * fill it. It's also broken with regard to NLA_BINARY use in
1563 * combination with structs.
1564 */
1565 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1566 .len = sizeof(struct ifla_port_vsi) },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001567};
1568
Brenden Blancod1fdd912016-07-19 12:16:49 -07001569static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
1570 [IFLA_XDP_FD] = { .type = NLA_S32 },
1571 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 },
Daniel Borkmann85de8572016-11-28 23:16:54 +01001572 [IFLA_XDP_FLAGS] = { .type = NLA_U32 },
Martin KaFai Lau58038692017-06-15 17:29:09 -07001573 [IFLA_XDP_PROG_ID] = { .type = NLA_U32 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001574};
1575
David Aherndc599f72016-02-02 08:17:07 -08001576static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1577{
1578 const struct rtnl_link_ops *ops = NULL;
1579 struct nlattr *linfo[IFLA_INFO_MAX + 1];
1580
Johannes Bergfceb6432017-04-12 14:34:07 +02001581 if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla,
1582 ifla_info_policy, NULL) < 0)
David Aherndc599f72016-02-02 08:17:07 -08001583 return NULL;
1584
1585 if (linfo[IFLA_INFO_KIND]) {
1586 char kind[MODULE_NAME_LEN];
1587
1588 nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1589 ops = rtnl_link_ops_get(kind);
1590 }
1591
1592 return ops;
1593}
1594
1595static bool link_master_filtered(struct net_device *dev, int master_idx)
1596{
1597 struct net_device *master;
1598
1599 if (!master_idx)
1600 return false;
1601
1602 master = netdev_master_upper_dev_get(dev);
1603 if (!master || master->ifindex != master_idx)
1604 return true;
1605
1606 return false;
1607}
1608
1609static bool link_kind_filtered(const struct net_device *dev,
1610 const struct rtnl_link_ops *kind_ops)
1611{
1612 if (kind_ops && dev->rtnl_link_ops != kind_ops)
1613 return true;
1614
1615 return false;
1616}
1617
1618static bool link_dump_filtered(struct net_device *dev,
1619 int master_idx,
1620 const struct rtnl_link_ops *kind_ops)
1621{
1622 if (link_master_filtered(dev, master_idx) ||
1623 link_kind_filtered(dev, kind_ops))
1624 return true;
1625
1626 return false;
1627}
1628
Thomas Grafb60c5112006-08-04 23:05:34 -07001629static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001631 struct net *net = sock_net(skb->sk);
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001632 int h, s_h;
1633 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 struct net_device *dev;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001635 struct hlist_head *head;
Greg Rose115c9b82012-02-21 16:54:48 -05001636 struct nlattr *tb[IFLA_MAX+1];
1637 u32 ext_filter_mask = 0;
David Aherndc599f72016-02-02 08:17:07 -08001638 const struct rtnl_link_ops *kind_ops = NULL;
1639 unsigned int flags = NLM_F_MULTI;
1640 int master_idx = 0;
David Gibson973462b2014-04-24 10:22:35 +10001641 int err;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001642 int hdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001644 s_h = cb->args[0];
1645 s_idx = cb->args[1];
1646
Thomas Graf4e985ad2011-06-21 03:11:20 +00001647 cb->seq = net->dev_base_seq;
1648
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001649 /* A hack to preserve kernel<->userspace interface.
1650 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1651 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1652 * what iproute2 < v3.9.0 used.
1653 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1654 * attribute, its netlink message is shorter than struct ifinfomsg.
1655 */
1656 hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1657 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1658
Johannes Bergfceb6432017-04-12 14:34:07 +02001659 if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX,
1660 ifla_policy, NULL) >= 0) {
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001661 if (tb[IFLA_EXT_MASK])
1662 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
David Aherndc599f72016-02-02 08:17:07 -08001663
1664 if (tb[IFLA_MASTER])
1665 master_idx = nla_get_u32(tb[IFLA_MASTER]);
1666
1667 if (tb[IFLA_LINKINFO])
1668 kind_ops = linkinfo_to_kind_ops(tb[IFLA_LINKINFO]);
1669
1670 if (master_idx || kind_ops)
1671 flags |= NLM_F_DUMP_FILTERED;
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001672 }
Greg Rose115c9b82012-02-21 16:54:48 -05001673
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001674 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1675 idx = 0;
1676 head = &net->dev_index_head[h];
Eric Dumazetcac5e652015-02-27 09:42:50 -08001677 hlist_for_each_entry(dev, head, index_hlist) {
David Aherndc599f72016-02-02 08:17:07 -08001678 if (link_dump_filtered(dev, master_idx, kind_ops))
Zhang Shengju3f0ae052016-11-19 23:28:32 +08001679 goto cont;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001680 if (idx < s_idx)
1681 goto cont;
David Gibson973462b2014-04-24 10:22:35 +10001682 err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
1683 NETLINK_CB(cb->skb).portid,
1684 cb->nlh->nlmsg_seq, 0,
David Aherndc599f72016-02-02 08:17:07 -08001685 flags,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001686 ext_filter_mask, 0);
David Gibson973462b2014-04-24 10:22:35 +10001687
David Ahernf6c57752017-05-15 23:19:17 -07001688 if (err < 0) {
1689 if (likely(skb->len))
1690 goto out;
1691
1692 goto out_err;
1693 }
Thomas Graf4e985ad2011-06-21 03:11:20 +00001694
1695 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Pavel Emelianov7562f872007-05-03 15:13:45 -07001696cont:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001697 idx++;
1698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 }
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001700out:
David Ahernf6c57752017-05-15 23:19:17 -07001701 err = skb->len;
1702out_err:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001703 cb->args[1] = idx;
1704 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
David Ahernf6c57752017-05-15 23:19:17 -07001706 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707}
1708
Johannes Bergfceb6432017-04-12 14:34:07 +02001709int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
1710 struct netlink_ext_ack *exterr)
Jiri Pirkof7b12602014-02-18 20:53:18 +01001711{
Johannes Bergfceb6432017-04-12 14:34:07 +02001712 return nla_parse(tb, IFLA_MAX, head, len, ifla_policy, exterr);
Jiri Pirkof7b12602014-02-18 20:53:18 +01001713}
1714EXPORT_SYMBOL(rtnl_nla_parse_ifla);
Scott Feldman57b61082010-05-17 22:49:55 -07001715
Eric W. Biederman81adee42009-11-08 00:53:51 -08001716struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1717{
1718 struct net *net;
1719 /* Examine the link attributes and figure out which
1720 * network namespace we are talking about.
1721 */
1722 if (tb[IFLA_NET_NS_PID])
1723 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001724 else if (tb[IFLA_NET_NS_FD])
1725 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
Eric W. Biederman81adee42009-11-08 00:53:51 -08001726 else
1727 net = get_net(src_net);
1728 return net;
1729}
1730EXPORT_SYMBOL(rtnl_link_get_net);
1731
Thomas Graf1840bb12008-02-23 19:54:36 -08001732static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1733{
1734 if (dev) {
1735 if (tb[IFLA_ADDRESS] &&
1736 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1737 return -EINVAL;
1738
1739 if (tb[IFLA_BROADCAST] &&
1740 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1741 return -EINVAL;
1742 }
1743
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001744 if (tb[IFLA_AF_SPEC]) {
1745 struct nlattr *af;
1746 int rem, err;
1747
1748 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1749 const struct rtnl_af_ops *af_ops;
1750
1751 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1752 return -EAFNOSUPPORT;
1753
1754 if (!af_ops->set_link_af)
1755 return -EOPNOTSUPP;
1756
1757 if (af_ops->validate_link_af) {
Kurt Van Dijck6d3a9a62011-01-26 04:55:24 +00001758 err = af_ops->validate_link_af(dev, af);
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001759 if (err < 0)
1760 return err;
1761 }
1762 }
1763 }
1764
Thomas Graf1840bb12008-02-23 19:54:36 -08001765 return 0;
1766}
1767
Eli Cohencc8e27c2016-03-11 22:58:34 +02001768static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
1769 int guid_type)
1770{
1771 const struct net_device_ops *ops = dev->netdev_ops;
1772
1773 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
1774}
1775
1776static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
1777{
1778 if (dev->type != ARPHRD_INFINIBAND)
1779 return -EOPNOTSUPP;
1780
1781 return handle_infiniband_guid(dev, ivt, guid_type);
1782}
1783
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001784static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
Chris Wrightc02db8c2010-05-16 01:05:45 -07001785{
Chris Wrightc02db8c2010-05-16 01:05:45 -07001786 const struct net_device_ops *ops = dev->netdev_ops;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001787 int err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001788
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001789 if (tb[IFLA_VF_MAC]) {
1790 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001791
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001792 err = -EOPNOTSUPP;
1793 if (ops->ndo_set_vf_mac)
1794 err = ops->ndo_set_vf_mac(dev, ivm->vf,
1795 ivm->mac);
1796 if (err < 0)
1797 return err;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001798 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001799
1800 if (tb[IFLA_VF_VLAN]) {
1801 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
1802
1803 err = -EOPNOTSUPP;
1804 if (ops->ndo_set_vf_vlan)
1805 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
Moshe Shemesh79aab092016-09-22 12:11:15 +03001806 ivv->qos,
1807 htons(ETH_P_8021Q));
1808 if (err < 0)
1809 return err;
1810 }
1811
1812 if (tb[IFLA_VF_VLAN_LIST]) {
1813 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
1814 struct nlattr *attr;
1815 int rem, len = 0;
1816
1817 err = -EOPNOTSUPP;
1818 if (!ops->ndo_set_vf_vlan)
1819 return err;
1820
1821 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
1822 if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
1823 nla_len(attr) < NLA_HDRLEN) {
1824 return -EINVAL;
1825 }
1826 if (len >= MAX_VLAN_LIST_LEN)
1827 return -EOPNOTSUPP;
1828 ivvl[len] = nla_data(attr);
1829
1830 len++;
1831 }
Arnd Bergmannfa34cd92016-09-30 18:13:49 +02001832 if (len == 0)
1833 return -EINVAL;
1834
Moshe Shemesh79aab092016-09-22 12:11:15 +03001835 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
1836 ivvl[0]->qos, ivvl[0]->vlan_proto);
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001837 if (err < 0)
1838 return err;
1839 }
1840
1841 if (tb[IFLA_VF_TX_RATE]) {
1842 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
1843 struct ifla_vf_info ivf;
1844
1845 err = -EOPNOTSUPP;
1846 if (ops->ndo_get_vf_config)
1847 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
1848 if (err < 0)
1849 return err;
1850
1851 err = -EOPNOTSUPP;
1852 if (ops->ndo_set_vf_rate)
1853 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1854 ivf.min_tx_rate,
1855 ivt->rate);
1856 if (err < 0)
1857 return err;
1858 }
1859
1860 if (tb[IFLA_VF_RATE]) {
1861 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
1862
1863 err = -EOPNOTSUPP;
1864 if (ops->ndo_set_vf_rate)
1865 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1866 ivt->min_tx_rate,
1867 ivt->max_tx_rate);
1868 if (err < 0)
1869 return err;
1870 }
1871
1872 if (tb[IFLA_VF_SPOOFCHK]) {
1873 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
1874
1875 err = -EOPNOTSUPP;
1876 if (ops->ndo_set_vf_spoofchk)
1877 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
1878 ivs->setting);
1879 if (err < 0)
1880 return err;
1881 }
1882
1883 if (tb[IFLA_VF_LINK_STATE]) {
1884 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
1885
1886 err = -EOPNOTSUPP;
1887 if (ops->ndo_set_vf_link_state)
1888 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
1889 ivl->link_state);
1890 if (err < 0)
1891 return err;
1892 }
1893
1894 if (tb[IFLA_VF_RSS_QUERY_EN]) {
1895 struct ifla_vf_rss_query_en *ivrssq_en;
1896
1897 err = -EOPNOTSUPP;
1898 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
1899 if (ops->ndo_set_vf_rss_query_en)
1900 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
1901 ivrssq_en->setting);
1902 if (err < 0)
1903 return err;
1904 }
1905
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00001906 if (tb[IFLA_VF_TRUST]) {
1907 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
1908
1909 err = -EOPNOTSUPP;
1910 if (ops->ndo_set_vf_trust)
1911 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
1912 if (err < 0)
1913 return err;
1914 }
1915
Eli Cohencc8e27c2016-03-11 22:58:34 +02001916 if (tb[IFLA_VF_IB_NODE_GUID]) {
1917 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
1918
1919 if (!ops->ndo_set_vf_guid)
1920 return -EOPNOTSUPP;
1921
1922 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
1923 }
1924
1925 if (tb[IFLA_VF_IB_PORT_GUID]) {
1926 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
1927
1928 if (!ops->ndo_set_vf_guid)
1929 return -EOPNOTSUPP;
1930
1931 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
1932 }
1933
Chris Wrightc02db8c2010-05-16 01:05:45 -07001934 return err;
1935}
1936
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001937static int do_set_master(struct net_device *dev, int ifindex)
1938{
Jiri Pirko898e5062013-01-03 22:48:52 +00001939 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001940 const struct net_device_ops *ops;
1941 int err;
1942
Jiri Pirko898e5062013-01-03 22:48:52 +00001943 if (upper_dev) {
1944 if (upper_dev->ifindex == ifindex)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001945 return 0;
Jiri Pirko898e5062013-01-03 22:48:52 +00001946 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001947 if (ops->ndo_del_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001948 err = ops->ndo_del_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001949 if (err)
1950 return err;
1951 } else {
1952 return -EOPNOTSUPP;
1953 }
1954 }
1955
1956 if (ifindex) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001957 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
1958 if (!upper_dev)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001959 return -EINVAL;
Jiri Pirko898e5062013-01-03 22:48:52 +00001960 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001961 if (ops->ndo_add_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001962 err = ops->ndo_add_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001963 if (err)
1964 return err;
1965 } else {
1966 return -EOPNOTSUPP;
1967 }
1968 }
1969 return 0;
1970}
1971
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001972#define DO_SETLINK_MODIFIED 0x01
Nicolas Dichtelba998902014-09-01 16:07:29 +02001973/* notify flag means notify + modified. */
1974#define DO_SETLINK_NOTIFY 0x03
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001975static int do_setlink(const struct sk_buff *skb,
1976 struct net_device *dev, struct ifinfomsg *ifm,
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07001977 struct netlink_ext_ack *extack,
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001978 struct nlattr **tb, char *ifname, int status)
Patrick McHardy0157f602007-06-13 12:03:36 -07001979{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001980 const struct net_device_ops *ops = dev->netdev_ops;
Patrick McHardy0157f602007-06-13 12:03:36 -07001981 int err;
1982
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001983 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
Eric W. Biederman81adee42009-11-08 00:53:51 -08001984 struct net *net = rtnl_link_get_net(dev_net(dev), tb);
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001985 if (IS_ERR(net)) {
1986 err = PTR_ERR(net);
1987 goto errout;
1988 }
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001989 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
Nicolas Dichtele0ebde02014-11-27 10:16:15 +01001990 put_net(net);
Eric W. Biedermanb51642f2012-11-16 03:03:11 +00001991 err = -EPERM;
1992 goto errout;
1993 }
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001994 err = dev_change_net_namespace(dev, net, ifname);
1995 put_net(net);
1996 if (err)
1997 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001998 status |= DO_SETLINK_MODIFIED;
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001999 }
2000
Patrick McHardy0157f602007-06-13 12:03:36 -07002001 if (tb[IFLA_MAP]) {
2002 struct rtnl_link_ifmap *u_map;
2003 struct ifmap k_map;
2004
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002005 if (!ops->ndo_set_config) {
Patrick McHardy0157f602007-06-13 12:03:36 -07002006 err = -EOPNOTSUPP;
2007 goto errout;
2008 }
2009
2010 if (!netif_device_present(dev)) {
2011 err = -ENODEV;
2012 goto errout;
2013 }
2014
2015 u_map = nla_data(tb[IFLA_MAP]);
2016 k_map.mem_start = (unsigned long) u_map->mem_start;
2017 k_map.mem_end = (unsigned long) u_map->mem_end;
2018 k_map.base_addr = (unsigned short) u_map->base_addr;
2019 k_map.irq = (unsigned char) u_map->irq;
2020 k_map.dma = (unsigned char) u_map->dma;
2021 k_map.port = (unsigned char) u_map->port;
2022
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002023 err = ops->ndo_set_config(dev, &k_map);
Patrick McHardy0157f602007-06-13 12:03:36 -07002024 if (err < 0)
2025 goto errout;
2026
Nicolas Dichtelba998902014-09-01 16:07:29 +02002027 status |= DO_SETLINK_NOTIFY;
Patrick McHardy0157f602007-06-13 12:03:36 -07002028 }
2029
2030 if (tb[IFLA_ADDRESS]) {
2031 struct sockaddr *sa;
2032 int len;
2033
Patrick McHardy0157f602007-06-13 12:03:36 -07002034 len = sizeof(sa_family_t) + dev->addr_len;
2035 sa = kmalloc(len, GFP_KERNEL);
2036 if (!sa) {
2037 err = -ENOMEM;
2038 goto errout;
2039 }
2040 sa->sa_family = dev->type;
2041 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
2042 dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00002043 err = dev_set_mac_address(dev, sa);
Patrick McHardy0157f602007-06-13 12:03:36 -07002044 kfree(sa);
2045 if (err)
2046 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002047 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002048 }
2049
2050 if (tb[IFLA_MTU]) {
2051 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
2052 if (err < 0)
2053 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002054 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002055 }
2056
Vlad Dogarucbda10f2011-01-13 23:38:30 +00002057 if (tb[IFLA_GROUP]) {
2058 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Nicolas Dichtelba998902014-09-01 16:07:29 +02002059 status |= DO_SETLINK_NOTIFY;
Vlad Dogarucbda10f2011-01-13 23:38:30 +00002060 }
2061
Patrick McHardy0157f602007-06-13 12:03:36 -07002062 /*
2063 * Interface selected by interface index but interface
2064 * name provided implies that a name change has been
2065 * requested.
2066 */
2067 if (ifm->ifi_index > 0 && ifname[0]) {
2068 err = dev_change_name(dev, ifname);
2069 if (err < 0)
2070 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002071 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002072 }
2073
Stephen Hemminger0b815a12008-09-22 21:28:11 -07002074 if (tb[IFLA_IFALIAS]) {
2075 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2076 nla_len(tb[IFLA_IFALIAS]));
2077 if (err < 0)
2078 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002079 status |= DO_SETLINK_NOTIFY;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07002080 }
2081
Patrick McHardy0157f602007-06-13 12:03:36 -07002082 if (tb[IFLA_BROADCAST]) {
2083 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00002084 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Patrick McHardy0157f602007-06-13 12:03:36 -07002085 }
2086
2087 if (ifm->ifi_flags || ifm->ifi_change) {
Patrick McHardy3729d502010-02-26 06:34:54 +00002088 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
Johannes Berg5f9021c2008-11-16 23:20:31 -08002089 if (err < 0)
2090 goto errout;
Patrick McHardy0157f602007-06-13 12:03:36 -07002091 }
2092
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002093 if (tb[IFLA_MASTER]) {
2094 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
2095 if (err)
2096 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002097 status |= DO_SETLINK_MODIFIED;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002098 }
2099
Jiri Pirko9a572472012-12-27 23:49:39 +00002100 if (tb[IFLA_CARRIER]) {
2101 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2102 if (err)
2103 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002104 status |= DO_SETLINK_MODIFIED;
Jiri Pirko9a572472012-12-27 23:49:39 +00002105 }
2106
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002107 if (tb[IFLA_TXQLEN]) {
Alexey Dobriyan0cd29502017-05-17 13:30:44 +03002108 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
2109 unsigned int orig_len = dev->tx_queue_len;
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002110
Jason Wang08294a22016-06-30 14:45:35 +08002111 if (dev->tx_queue_len ^ value) {
2112 dev->tx_queue_len = value;
2113 err = call_netdevice_notifiers(
2114 NETDEV_CHANGE_TX_QUEUE_LEN, dev);
2115 err = notifier_to_errno(err);
2116 if (err) {
2117 dev->tx_queue_len = orig_len;
2118 goto errout;
2119 }
Nicolas Dichtelba998902014-09-01 16:07:29 +02002120 status |= DO_SETLINK_NOTIFY;
Jason Wang08294a22016-06-30 14:45:35 +08002121 }
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002122 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002123
Patrick McHardy0157f602007-06-13 12:03:36 -07002124 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002125 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Patrick McHardy0157f602007-06-13 12:03:36 -07002126
2127 if (tb[IFLA_LINKMODE]) {
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002128 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2129
David S. Miller93b2d4a2008-02-17 18:35:07 -08002130 write_lock_bh(&dev_base_lock);
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002131 if (dev->link_mode ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02002132 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002133 dev->link_mode = value;
David S. Miller93b2d4a2008-02-17 18:35:07 -08002134 write_unlock_bh(&dev_base_lock);
Patrick McHardy0157f602007-06-13 12:03:36 -07002135 }
2136
Chris Wrightc02db8c2010-05-16 01:05:45 -07002137 if (tb[IFLA_VFINFO_LIST]) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002138 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
Chris Wrightc02db8c2010-05-16 01:05:45 -07002139 struct nlattr *attr;
2140 int rem;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002141
Chris Wrightc02db8c2010-05-16 01:05:45 -07002142 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002143 if (nla_type(attr) != IFLA_VF_INFO ||
2144 nla_len(attr) < NLA_HDRLEN) {
David Howells253683b2010-05-21 02:25:27 +00002145 err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002146 goto errout;
David Howells253683b2010-05-21 02:25:27 +00002147 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002148 err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr,
Johannes Bergfceb6432017-04-12 14:34:07 +02002149 ifla_vf_policy, NULL);
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002150 if (err < 0)
2151 goto errout;
2152 err = do_setvfinfo(dev, vfinfo);
Chris Wrightc02db8c2010-05-16 01:05:45 -07002153 if (err < 0)
2154 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002155 status |= DO_SETLINK_NOTIFY;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002156 }
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00002157 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002158 err = 0;
2159
Scott Feldman57b61082010-05-17 22:49:55 -07002160 if (tb[IFLA_VF_PORTS]) {
2161 struct nlattr *port[IFLA_PORT_MAX+1];
2162 struct nlattr *attr;
2163 int vf;
2164 int rem;
2165
2166 err = -EOPNOTSUPP;
2167 if (!ops->ndo_set_vf_port)
2168 goto errout;
2169
2170 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
Daniel Borkmann035d2102015-07-13 00:06:02 +02002171 if (nla_type(attr) != IFLA_VF_PORT ||
2172 nla_len(attr) < NLA_HDRLEN) {
2173 err = -EINVAL;
2174 goto errout;
2175 }
2176 err = nla_parse_nested(port, IFLA_PORT_MAX, attr,
Johannes Bergfceb6432017-04-12 14:34:07 +02002177 ifla_port_policy, NULL);
Scott Feldman57b61082010-05-17 22:49:55 -07002178 if (err < 0)
2179 goto errout;
2180 if (!port[IFLA_PORT_VF]) {
2181 err = -EOPNOTSUPP;
2182 goto errout;
2183 }
2184 vf = nla_get_u32(port[IFLA_PORT_VF]);
2185 err = ops->ndo_set_vf_port(dev, vf, port);
2186 if (err < 0)
2187 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002188 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07002189 }
2190 }
2191 err = 0;
2192
2193 if (tb[IFLA_PORT_SELF]) {
2194 struct nlattr *port[IFLA_PORT_MAX+1];
2195
2196 err = nla_parse_nested(port, IFLA_PORT_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +02002197 tb[IFLA_PORT_SELF], ifla_port_policy,
2198 NULL);
Scott Feldman57b61082010-05-17 22:49:55 -07002199 if (err < 0)
2200 goto errout;
2201
2202 err = -EOPNOTSUPP;
2203 if (ops->ndo_set_vf_port)
2204 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
2205 if (err < 0)
2206 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002207 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07002208 }
Thomas Graff8ff1822010-11-16 04:30:14 +00002209
2210 if (tb[IFLA_AF_SPEC]) {
2211 struct nlattr *af;
2212 int rem;
2213
2214 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2215 const struct rtnl_af_ops *af_ops;
2216
2217 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002218 BUG();
Thomas Graff8ff1822010-11-16 04:30:14 +00002219
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002220 err = af_ops->set_link_af(dev, af);
Thomas Graff8ff1822010-11-16 04:30:14 +00002221 if (err < 0)
2222 goto errout;
2223
Nicolas Dichtelba998902014-09-01 16:07:29 +02002224 status |= DO_SETLINK_NOTIFY;
Thomas Graff8ff1822010-11-16 04:30:14 +00002225 }
2226 }
Scott Feldman57b61082010-05-17 22:49:55 -07002227 err = 0;
2228
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07002229 if (tb[IFLA_PROTO_DOWN]) {
2230 err = dev_change_proto_down(dev,
2231 nla_get_u8(tb[IFLA_PROTO_DOWN]));
2232 if (err)
2233 goto errout;
2234 status |= DO_SETLINK_NOTIFY;
2235 }
2236
Brenden Blancod1fdd912016-07-19 12:16:49 -07002237 if (tb[IFLA_XDP]) {
2238 struct nlattr *xdp[IFLA_XDP_MAX + 1];
Daniel Borkmann85de8572016-11-28 23:16:54 +01002239 u32 xdp_flags = 0;
Brenden Blancod1fdd912016-07-19 12:16:49 -07002240
2241 err = nla_parse_nested(xdp, IFLA_XDP_MAX, tb[IFLA_XDP],
Johannes Bergfceb6432017-04-12 14:34:07 +02002242 ifla_xdp_policy, NULL);
Brenden Blancod1fdd912016-07-19 12:16:49 -07002243 if (err < 0)
2244 goto errout;
2245
Martin KaFai Lau58038692017-06-15 17:29:09 -07002246 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
Brenden Blanco262d8622016-07-20 17:22:34 -07002247 err = -EINVAL;
2248 goto errout;
2249 }
Daniel Borkmann85de8572016-11-28 23:16:54 +01002250
2251 if (xdp[IFLA_XDP_FLAGS]) {
2252 xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
2253 if (xdp_flags & ~XDP_FLAGS_MASK) {
2254 err = -EINVAL;
2255 goto errout;
2256 }
Jakub Kicinskiee5d0322017-06-21 18:25:04 -07002257 if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
Daniel Borkmann0489df92017-05-12 01:04:45 +02002258 err = -EINVAL;
2259 goto errout;
2260 }
Daniel Borkmann85de8572016-11-28 23:16:54 +01002261 }
2262
Brenden Blancod1fdd912016-07-19 12:16:49 -07002263 if (xdp[IFLA_XDP_FD]) {
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002264 err = dev_change_xdp_fd(dev, extack,
Daniel Borkmann85de8572016-11-28 23:16:54 +01002265 nla_get_s32(xdp[IFLA_XDP_FD]),
2266 xdp_flags);
Brenden Blancod1fdd912016-07-19 12:16:49 -07002267 if (err)
2268 goto errout;
2269 status |= DO_SETLINK_NOTIFY;
2270 }
2271 }
2272
Patrick McHardy0157f602007-06-13 12:03:36 -07002273errout:
Nicolas Dichtelba998902014-09-01 16:07:29 +02002274 if (status & DO_SETLINK_MODIFIED) {
2275 if (status & DO_SETLINK_NOTIFY)
2276 netdev_state_change(dev);
2277
2278 if (err < 0)
2279 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",
2280 dev->name);
2281 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002282
Patrick McHardy0157f602007-06-13 12:03:36 -07002283 return err;
2284}
2285
David Ahernc21ef3e2017-04-16 09:48:24 -07002286static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2287 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002289 struct net *net = sock_net(skb->sk);
Thomas Grafda5e0492006-08-10 21:17:37 -07002290 struct ifinfomsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 struct net_device *dev;
Patrick McHardy0157f602007-06-13 12:03:36 -07002292 int err;
Thomas Grafda5e0492006-08-10 21:17:37 -07002293 struct nlattr *tb[IFLA_MAX+1];
2294 char ifname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
David Ahernc21ef3e2017-04-16 09:48:24 -07002296 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy,
2297 extack);
Thomas Grafda5e0492006-08-10 21:17:37 -07002298 if (err < 0)
2299 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
Thomas Graf5176f912006-08-26 20:13:18 -07002301 if (tb[IFLA_IFNAME])
2302 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
Patrick McHardy78e5b8912006-09-13 20:35:36 -07002303 else
2304 ifname[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 err = -EINVAL;
Thomas Grafda5e0492006-08-10 21:17:37 -07002307 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -07002308 if (ifm->ifi_index > 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00002309 dev = __dev_get_by_index(net, ifm->ifi_index);
Thomas Grafda5e0492006-08-10 21:17:37 -07002310 else if (tb[IFLA_IFNAME])
Eric Dumazeta3d12892009-10-21 10:59:31 +00002311 dev = __dev_get_by_name(net, ifname);
Thomas Grafda5e0492006-08-10 21:17:37 -07002312 else
2313 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
Thomas Grafda5e0492006-08-10 21:17:37 -07002315 if (dev == NULL) {
2316 err = -ENODEV;
2317 goto errout;
2318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
Eric Dumazete0d087a2009-11-07 01:26:17 -08002320 err = validate_linkmsg(dev, tb);
2321 if (err < 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00002322 goto errout;
Thomas Grafda5e0492006-08-10 21:17:37 -07002323
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002324 err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
Thomas Grafda5e0492006-08-10 21:17:37 -07002325errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 return err;
2327}
2328
WANG Cong66400d52015-03-24 11:53:31 -07002329static int rtnl_group_dellink(const struct net *net, int group)
2330{
2331 struct net_device *dev, *aux;
2332 LIST_HEAD(list_kill);
2333 bool found = false;
2334
2335 if (!group)
2336 return -EPERM;
2337
2338 for_each_netdev(net, dev) {
2339 if (dev->group == group) {
2340 const struct rtnl_link_ops *ops;
2341
2342 found = true;
2343 ops = dev->rtnl_link_ops;
2344 if (!ops || !ops->dellink)
2345 return -EOPNOTSUPP;
2346 }
2347 }
2348
2349 if (!found)
2350 return -ENODEV;
2351
2352 for_each_netdev_safe(net, dev, aux) {
2353 if (dev->group == group) {
2354 const struct rtnl_link_ops *ops;
2355
2356 ops = dev->rtnl_link_ops;
2357 ops->dellink(dev, &list_kill);
2358 }
2359 }
2360 unregister_netdevice_many(&list_kill);
2361
2362 return 0;
2363}
2364
Thomas Graf614732e2015-07-21 10:44:06 +02002365int rtnl_delete_link(struct net_device *dev)
2366{
2367 const struct rtnl_link_ops *ops;
2368 LIST_HEAD(list_kill);
2369
2370 ops = dev->rtnl_link_ops;
2371 if (!ops || !ops->dellink)
2372 return -EOPNOTSUPP;
2373
2374 ops->dellink(dev, &list_kill);
2375 unregister_netdevice_many(&list_kill);
2376
2377 return 0;
2378}
2379EXPORT_SYMBOL_GPL(rtnl_delete_link);
2380
David Ahernc21ef3e2017-04-16 09:48:24 -07002381static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
2382 struct netlink_ext_ack *extack)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002383{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002384 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002385 struct net_device *dev;
2386 struct ifinfomsg *ifm;
2387 char ifname[IFNAMSIZ];
2388 struct nlattr *tb[IFLA_MAX+1];
2389 int err;
2390
David Ahernc21ef3e2017-04-16 09:48:24 -07002391 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002392 if (err < 0)
2393 return err;
2394
2395 if (tb[IFLA_IFNAME])
2396 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2397
2398 ifm = nlmsg_data(nlh);
2399 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07002400 dev = __dev_get_by_index(net, ifm->ifi_index);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002401 else if (tb[IFLA_IFNAME])
Eric W. Biederman881d9662007-09-17 11:56:21 -07002402 dev = __dev_get_by_name(net, ifname);
WANG Cong66400d52015-03-24 11:53:31 -07002403 else if (tb[IFLA_GROUP])
2404 return rtnl_group_dellink(net, nla_get_u32(tb[IFLA_GROUP]));
Patrick McHardy38f7b872007-06-13 12:03:51 -07002405 else
2406 return -EINVAL;
2407
2408 if (!dev)
2409 return -ENODEV;
2410
Thomas Graf614732e2015-07-21 10:44:06 +02002411 return rtnl_delete_link(dev);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002412}
2413
Patrick McHardy3729d502010-02-26 06:34:54 +00002414int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
2415{
2416 unsigned int old_flags;
2417 int err;
2418
2419 old_flags = dev->flags;
2420 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
2421 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
2422 if (err < 0)
2423 return err;
2424 }
2425
2426 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
Patrick McHardy3729d502010-02-26 06:34:54 +00002427
Nicolas Dichtela528c212013-09-25 12:02:44 +02002428 __dev_notify_flags(dev, old_flags, ~0U);
Patrick McHardy3729d502010-02-26 06:34:54 +00002429 return 0;
2430}
2431EXPORT_SYMBOL(rtnl_configure_link);
2432
Rami Rosenc0713562012-11-30 01:08:47 +00002433struct net_device *rtnl_create_link(struct net *net,
Thomas Graf78ebb0d2015-04-10 01:45:53 +02002434 const char *ifname, unsigned char name_assign_type,
Tom Gundersen55177502014-07-14 16:37:25 +02002435 const struct rtnl_link_ops *ops, struct nlattr *tb[])
Pavel Emelianove7199282007-08-08 22:16:38 -07002436{
Pavel Emelianove7199282007-08-08 22:16:38 -07002437 struct net_device *dev;
Jiri Pirkod40156a2012-07-20 02:28:47 +00002438 unsigned int num_tx_queues = 1;
2439 unsigned int num_rx_queues = 1;
Pavel Emelianove7199282007-08-08 22:16:38 -07002440
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002441 if (tb[IFLA_NUM_TX_QUEUES])
2442 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
2443 else if (ops->get_num_tx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002444 num_tx_queues = ops->get_num_tx_queues();
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002445
2446 if (tb[IFLA_NUM_RX_QUEUES])
2447 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
2448 else if (ops->get_num_rx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002449 num_rx_queues = ops->get_num_rx_queues();
stephen hemmingerefacb302012-04-10 18:34:43 +00002450
Tom Gundersen55177502014-07-14 16:37:25 +02002451 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
Tom Gundersenc835a672014-07-14 16:37:24 +02002452 ops->setup, num_tx_queues, num_rx_queues);
Pavel Emelianove7199282007-08-08 22:16:38 -07002453 if (!dev)
Tobias Klauserd1892e42017-02-20 16:32:06 +01002454 return ERR_PTR(-ENOMEM);
Pavel Emelianove7199282007-08-08 22:16:38 -07002455
Eric W. Biederman81adee42009-11-08 00:53:51 -08002456 dev_net_set(dev, net);
2457 dev->rtnl_link_ops = ops;
Patrick McHardy3729d502010-02-26 06:34:54 +00002458 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
Eric W. Biederman81adee42009-11-08 00:53:51 -08002459
Pavel Emelianove7199282007-08-08 22:16:38 -07002460 if (tb[IFLA_MTU])
2461 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002462 if (tb[IFLA_ADDRESS]) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002463 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
2464 nla_len(tb[IFLA_ADDRESS]));
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002465 dev->addr_assign_type = NET_ADDR_SET;
2466 }
Pavel Emelianove7199282007-08-08 22:16:38 -07002467 if (tb[IFLA_BROADCAST])
2468 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
2469 nla_len(tb[IFLA_BROADCAST]));
2470 if (tb[IFLA_TXQLEN])
2471 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
2472 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002473 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Pavel Emelianove7199282007-08-08 22:16:38 -07002474 if (tb[IFLA_LINKMODE])
2475 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
Patrick McHardyffa934f2011-01-20 03:00:42 +00002476 if (tb[IFLA_GROUP])
2477 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Pavel Emelianove7199282007-08-08 22:16:38 -07002478
2479 return dev;
Pavel Emelianove7199282007-08-08 22:16:38 -07002480}
Eric Dumazete0d087a2009-11-07 01:26:17 -08002481EXPORT_SYMBOL(rtnl_create_link);
Pavel Emelianove7199282007-08-08 22:16:38 -07002482
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002483static int rtnl_group_changelink(const struct sk_buff *skb,
2484 struct net *net, int group,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002485 struct ifinfomsg *ifm,
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002486 struct netlink_ext_ack *extack,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002487 struct nlattr **tb)
2488{
WANG Congd0795352015-03-23 16:31:09 -07002489 struct net_device *dev, *aux;
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002490 int err;
2491
WANG Congd0795352015-03-23 16:31:09 -07002492 for_each_netdev_safe(net, dev, aux) {
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002493 if (dev->group == group) {
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002494 err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002495 if (err < 0)
2496 return err;
2497 }
2498 }
2499
2500 return 0;
2501}
2502
David Ahernc21ef3e2017-04-16 09:48:24 -07002503static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2504 struct netlink_ext_ack *extack)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002505{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002506 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002507 const struct rtnl_link_ops *ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002508 const struct rtnl_link_ops *m_ops = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002509 struct net_device *dev;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002510 struct net_device *master_dev = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002511 struct ifinfomsg *ifm;
2512 char kind[MODULE_NAME_LEN];
2513 char ifname[IFNAMSIZ];
2514 struct nlattr *tb[IFLA_MAX+1];
2515 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
Tom Gundersen55177502014-07-14 16:37:25 +02002516 unsigned char name_assign_type = NET_NAME_USER;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002517 int err;
2518
Johannes Berg95a5afc2008-10-16 15:24:51 -07002519#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002520replay:
Thomas Graf8072f082007-07-31 14:13:50 -07002521#endif
David Ahernc21ef3e2017-04-16 09:48:24 -07002522 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002523 if (err < 0)
2524 return err;
2525
2526 if (tb[IFLA_IFNAME])
2527 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2528 else
2529 ifname[0] = '\0';
2530
2531 ifm = nlmsg_data(nlh);
2532 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07002533 dev = __dev_get_by_index(net, ifm->ifi_index);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002534 else {
2535 if (ifname[0])
2536 dev = __dev_get_by_name(net, ifname);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002537 else
2538 dev = NULL;
2539 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002540
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002541 if (dev) {
2542 master_dev = netdev_master_upper_dev_get(dev);
2543 if (master_dev)
2544 m_ops = master_dev->rtnl_link_ops;
2545 }
2546
Eric Dumazete0d087a2009-11-07 01:26:17 -08002547 err = validate_linkmsg(dev, tb);
2548 if (err < 0)
Thomas Graf1840bb12008-02-23 19:54:36 -08002549 return err;
2550
Patrick McHardy38f7b872007-06-13 12:03:51 -07002551 if (tb[IFLA_LINKINFO]) {
2552 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +02002553 tb[IFLA_LINKINFO], ifla_info_policy,
2554 NULL);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002555 if (err < 0)
2556 return err;
2557 } else
2558 memset(linkinfo, 0, sizeof(linkinfo));
2559
2560 if (linkinfo[IFLA_INFO_KIND]) {
2561 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
2562 ops = rtnl_link_ops_get(kind);
2563 } else {
2564 kind[0] = '\0';
2565 ops = NULL;
2566 }
2567
2568 if (1) {
Sasha Levin4e10fd52015-02-24 14:14:35 -05002569 struct nlattr *attr[ops ? ops->maxtype + 1 : 1];
2570 struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1];
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002571 struct nlattr **data = NULL;
2572 struct nlattr **slave_data = NULL;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002573 struct net *dest_net, *link_net = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002574
2575 if (ops) {
2576 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
2577 err = nla_parse_nested(attr, ops->maxtype,
2578 linkinfo[IFLA_INFO_DATA],
Johannes Bergfceb6432017-04-12 14:34:07 +02002579 ops->policy, NULL);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002580 if (err < 0)
2581 return err;
2582 data = attr;
2583 }
2584 if (ops->validate) {
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02002585 err = ops->validate(tb, data, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002586 if (err < 0)
2587 return err;
2588 }
2589 }
2590
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002591 if (m_ops) {
2592 if (m_ops->slave_maxtype &&
2593 linkinfo[IFLA_INFO_SLAVE_DATA]) {
2594 err = nla_parse_nested(slave_attr,
2595 m_ops->slave_maxtype,
2596 linkinfo[IFLA_INFO_SLAVE_DATA],
Johannes Bergfceb6432017-04-12 14:34:07 +02002597 m_ops->slave_policy,
2598 NULL);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002599 if (err < 0)
2600 return err;
2601 slave_data = slave_attr;
2602 }
2603 if (m_ops->slave_validate) {
Matthias Schifferd116ffc2017-06-25 23:56:03 +02002604 err = m_ops->slave_validate(tb, slave_data,
2605 extack);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002606 if (err < 0)
2607 return err;
2608 }
2609 }
2610
Patrick McHardy38f7b872007-06-13 12:03:51 -07002611 if (dev) {
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002612 int status = 0;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002613
2614 if (nlh->nlmsg_flags & NLM_F_EXCL)
2615 return -EEXIST;
2616 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2617 return -EOPNOTSUPP;
2618
2619 if (linkinfo[IFLA_INFO_DATA]) {
2620 if (!ops || ops != dev->rtnl_link_ops ||
2621 !ops->changelink)
2622 return -EOPNOTSUPP;
2623
Matthias Schifferad744b22017-06-25 23:56:00 +02002624 err = ops->changelink(dev, tb, data, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002625 if (err < 0)
2626 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002627 status |= DO_SETLINK_NOTIFY;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002628 }
2629
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002630 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2631 if (!m_ops || !m_ops->slave_changelink)
2632 return -EOPNOTSUPP;
2633
2634 err = m_ops->slave_changelink(master_dev, dev,
Matthias Schiffer17dd0ec2017-06-25 23:56:02 +02002635 tb, slave_data,
2636 extack);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002637 if (err < 0)
2638 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002639 status |= DO_SETLINK_NOTIFY;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002640 }
2641
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002642 return do_setlink(skb, dev, ifm, extack, tb, ifname,
2643 status);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002644 }
2645
Patrick McHardyffa934f2011-01-20 03:00:42 +00002646 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2647 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002648 return rtnl_group_changelink(skb, net,
Patrick McHardyffa934f2011-01-20 03:00:42 +00002649 nla_get_u32(tb[IFLA_GROUP]),
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002650 ifm, extack, tb);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002651 return -ENODEV;
Patrick McHardyffa934f2011-01-20 03:00:42 +00002652 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002653
Theuns Verwoerd160ca012017-01-31 12:23:46 +13002654 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
Patrick McHardy38f7b872007-06-13 12:03:51 -07002655 return -EOPNOTSUPP;
2656
2657 if (!ops) {
Johannes Berg95a5afc2008-10-16 15:24:51 -07002658#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002659 if (kind[0]) {
2660 __rtnl_unlock();
2661 request_module("rtnl-link-%s", kind);
2662 rtnl_lock();
2663 ops = rtnl_link_ops_get(kind);
2664 if (ops)
2665 goto replay;
2666 }
2667#endif
2668 return -EOPNOTSUPP;
2669 }
2670
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02002671 if (!ops->setup)
2672 return -EOPNOTSUPP;
2673
Tom Gundersen55177502014-07-14 16:37:25 +02002674 if (!ifname[0]) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07002675 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
Tom Gundersen55177502014-07-14 16:37:25 +02002676 name_assign_type = NET_NAME_ENUM;
2677 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002678
Eric W. Biederman81adee42009-11-08 00:53:51 -08002679 dest_net = rtnl_link_get_net(net, tb);
Eric W. Biederman13ad1772011-01-29 14:57:22 +00002680 if (IS_ERR(dest_net))
2681 return PTR_ERR(dest_net);
2682
Eric W. Biederman505ce412015-02-26 16:19:00 -06002683 err = -EPERM;
2684 if (!netlink_ns_capable(skb, dest_net->user_ns, CAP_NET_ADMIN))
2685 goto out;
2686
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002687 if (tb[IFLA_LINK_NETNSID]) {
2688 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
2689
2690 link_net = get_net_ns_by_id(dest_net, id);
2691 if (!link_net) {
2692 err = -EINVAL;
2693 goto out;
2694 }
Eric W. Biederman06615be2015-02-26 16:20:07 -06002695 err = -EPERM;
2696 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
2697 goto out;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002698 }
2699
2700 dev = rtnl_create_link(link_net ? : dest_net, ifname,
2701 name_assign_type, ops, tb);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002702 if (IS_ERR(dev)) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002703 err = PTR_ERR(dev);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002704 goto out;
2705 }
2706
2707 dev->ifindex = ifm->ifi_index;
2708
Cong Wang0e0eee22014-02-11 15:51:30 -08002709 if (ops->newlink) {
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02002710 err = ops->newlink(link_net ? : net, dev, tb, data,
2711 extack);
Cong Wang0e0eee22014-02-11 15:51:30 -08002712 /* Drivers should call free_netdev() in ->destructor
Cong Wange51fb152014-06-03 16:40:47 -07002713 * and unregister it on failure after registration
2714 * so that device could be finally freed in rtnl_unlock.
Cong Wang0e0eee22014-02-11 15:51:30 -08002715 */
Cong Wange51fb152014-06-03 16:40:47 -07002716 if (err < 0) {
2717 /* If device is not registered at all, free it now */
2718 if (dev->reg_state == NETREG_UNINITIALIZED)
2719 free_netdev(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002720 goto out;
Cong Wange51fb152014-06-03 16:40:47 -07002721 }
Cong Wang0e0eee22014-02-11 15:51:30 -08002722 } else {
Patrick McHardy2d85cba2007-07-11 19:42:13 -07002723 err = register_netdevice(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002724 if (err < 0) {
2725 free_netdev(dev);
2726 goto out;
2727 }
Dan Carpenterfce9b9b2013-08-14 12:35:42 +03002728 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002729 err = rtnl_configure_link(dev, ifm);
David S. Miller43638902015-03-10 21:58:32 -04002730 if (err < 0)
2731 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002732 if (link_net) {
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002733 err = dev_change_net_namespace(dev, dest_net, ifname);
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002734 if (err < 0)
David S. Miller43638902015-03-10 21:58:32 -04002735 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002736 }
Theuns Verwoerd160ca012017-01-31 12:23:46 +13002737 if (tb[IFLA_MASTER]) {
2738 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
2739 if (err)
2740 goto out_unregister;
2741 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002742out:
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002743 if (link_net)
2744 put_net(link_net);
Eric W. Biederman81adee42009-11-08 00:53:51 -08002745 put_net(dest_net);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002746 return err;
David S. Miller43638902015-03-10 21:58:32 -04002747out_unregister:
2748 if (ops->newlink) {
2749 LIST_HEAD(list_kill);
2750
2751 ops->dellink(dev, &list_kill);
2752 unregister_netdevice_many(&list_kill);
2753 } else {
2754 unregister_netdevice(dev);
2755 }
2756 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002757 }
2758}
2759
David Ahernc21ef3e2017-04-16 09:48:24 -07002760static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2761 struct netlink_ext_ack *extack)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002762{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002763 struct net *net = sock_net(skb->sk);
Thomas Grafb60c5112006-08-04 23:05:34 -07002764 struct ifinfomsg *ifm;
Eric Dumazeta3d12892009-10-21 10:59:31 +00002765 char ifname[IFNAMSIZ];
Thomas Grafb60c5112006-08-04 23:05:34 -07002766 struct nlattr *tb[IFLA_MAX+1];
2767 struct net_device *dev = NULL;
2768 struct sk_buff *nskb;
Thomas Graf339bf982006-11-10 14:10:15 -08002769 int err;
Greg Rose115c9b82012-02-21 16:54:48 -05002770 u32 ext_filter_mask = 0;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002771
David Ahernc21ef3e2017-04-16 09:48:24 -07002772 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Thomas Grafb60c5112006-08-04 23:05:34 -07002773 if (err < 0)
Eric Sesterhenn9918f232006-09-26 23:26:38 -07002774 return err;
Thomas Grafb60c5112006-08-04 23:05:34 -07002775
Eric Dumazeta3d12892009-10-21 10:59:31 +00002776 if (tb[IFLA_IFNAME])
2777 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2778
Greg Rose115c9b82012-02-21 16:54:48 -05002779 if (tb[IFLA_EXT_MASK])
2780 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2781
Thomas Grafb60c5112006-08-04 23:05:34 -07002782 ifm = nlmsg_data(nlh);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002783 if (ifm->ifi_index > 0)
2784 dev = __dev_get_by_index(net, ifm->ifi_index);
2785 else if (tb[IFLA_IFNAME])
2786 dev = __dev_get_by_name(net, ifname);
2787 else
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002788 return -EINVAL;
Thomas Grafb60c5112006-08-04 23:05:34 -07002789
Eric Dumazeta3d12892009-10-21 10:59:31 +00002790 if (dev == NULL)
2791 return -ENODEV;
2792
Greg Rose115c9b82012-02-21 16:54:48 -05002793 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002794 if (nskb == NULL)
2795 return -ENOBUFS;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002796
Eric W. Biederman15e47302012-09-07 20:12:54 +00002797 err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04002798 nlh->nlmsg_seq, 0, 0, ext_filter_mask, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08002799 if (err < 0) {
2800 /* -EMSGSIZE implies BUG in if_nlmsg_size */
2801 WARN_ON(err == -EMSGSIZE);
2802 kfree_skb(nskb);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002803 } else
Eric W. Biederman15e47302012-09-07 20:12:54 +00002804 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
Thomas Grafb60c5112006-08-04 23:05:34 -07002805
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002806 return err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002807}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002808
Greg Rose115c9b82012-02-21 16:54:48 -05002809static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
Greg Rosec7ac8672011-06-10 01:27:09 +00002810{
Greg Rose115c9b82012-02-21 16:54:48 -05002811 struct net *net = sock_net(skb->sk);
2812 struct net_device *dev;
2813 struct nlattr *tb[IFLA_MAX+1];
2814 u32 ext_filter_mask = 0;
2815 u16 min_ifinfo_dump_size = 0;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02002816 int hdrlen;
Greg Rose115c9b82012-02-21 16:54:48 -05002817
Michal Schmidte5eca6d2014-05-28 14:15:19 +02002818 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2819 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2820 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2821
Johannes Bergfceb6432017-04-12 14:34:07 +02002822 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00002823 if (tb[IFLA_EXT_MASK])
2824 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2825 }
Greg Rose115c9b82012-02-21 16:54:48 -05002826
2827 if (!ext_filter_mask)
2828 return NLMSG_GOODSIZE;
2829 /*
2830 * traverse the list of net devices and compute the minimum
2831 * buffer size based upon the filter mask.
2832 */
2833 list_for_each_entry(dev, &net->dev_base_head, dev_list) {
2834 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
2835 if_nlmsg_size(dev,
2836 ext_filter_mask));
2837 }
2838
Zhang Shengju93af2052016-11-22 14:14:28 +08002839 return nlmsg_total_size(min_ifinfo_dump_size);
Greg Rosec7ac8672011-06-10 01:27:09 +00002840}
2841
Adrian Bunk42bad1d2007-04-26 00:57:41 -07002842static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843{
2844 int idx;
2845 int s_idx = cb->family;
2846
2847 if (s_idx == 0)
2848 s_idx = 1;
Patrick McHardy25239ce2010-04-26 16:02:05 +02002849 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 int type = cb->nlh->nlmsg_type-RTM_BASE;
2851 if (idx < s_idx || idx == PF_PACKET)
2852 continue;
Thomas Grafe2849862007-03-22 11:48:11 -07002853 if (rtnl_msg_handlers[idx] == NULL ||
2854 rtnl_msg_handlers[idx][type].dumpit == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 continue;
Nicolas Dichtel04652772013-03-22 06:28:42 +00002856 if (idx > s_idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 memset(&cb->args[0], 0, sizeof(cb->args));
Nicolas Dichtel04652772013-03-22 06:28:42 +00002858 cb->prev_seq = 0;
2859 cb->seq = 0;
2860 }
Thomas Grafe2849862007-03-22 11:48:11 -07002861 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 break;
2863 }
2864 cb->family = idx;
2865
2866 return skb->len;
2867}
2868
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002869struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04002870 unsigned int change,
2871 u32 event, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002873 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 struct sk_buff *skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002875 int err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00002876 size_t if_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877
Alexei Starovoitov7f294052013-10-23 16:02:42 -07002878 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002879 if (skb == NULL)
2880 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04002882 err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0, event);
Patrick McHardy26932562007-01-31 23:16:40 -08002883 if (err < 0) {
2884 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
2885 WARN_ON(err == -EMSGSIZE);
2886 kfree_skb(skb);
2887 goto errout;
2888 }
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002889 return skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002890errout:
2891 if (err < 0)
Eric W. Biederman4b3da702007-11-19 22:27:40 -08002892 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002893 return NULL;
2894}
2895
2896void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
2897{
2898 struct net *net = dev_net(dev);
2899
2900 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
2901}
2902
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04002903static void rtmsg_ifinfo_event(int type, struct net_device *dev,
2904 unsigned int change, u32 event,
2905 gfp_t flags)
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002906{
2907 struct sk_buff *skb;
2908
Nicolas Dichteled2a80a2015-05-13 14:19:42 +02002909 if (dev->reg_state != NETREG_REGISTERED)
2910 return;
2911
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04002912 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002913 if (skb)
2914 rtmsg_ifinfo_send(skb, dev, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915}
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04002916
2917void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
2918 gfp_t flags)
2919{
Vlad Yasevich8c6c9182017-05-30 10:07:02 -04002920 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags);
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04002921}
Jiri Pirko471cb5a2013-01-03 22:49:01 +00002922EXPORT_SYMBOL(rtmsg_ifinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923
John Fastabendd83b0602012-04-15 06:44:08 +00002924static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
2925 struct net_device *dev,
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002926 u8 *addr, u16 vid, u32 pid, u32 seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002927 int type, unsigned int flags,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002928 int nlflags, u16 ndm_state)
John Fastabendd83b0602012-04-15 06:44:08 +00002929{
2930 struct nlmsghdr *nlh;
2931 struct ndmsg *ndm;
2932
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002933 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
John Fastabendd83b0602012-04-15 06:44:08 +00002934 if (!nlh)
2935 return -EMSGSIZE;
2936
2937 ndm = nlmsg_data(nlh);
2938 ndm->ndm_family = AF_BRIDGE;
2939 ndm->ndm_pad1 = 0;
2940 ndm->ndm_pad2 = 0;
2941 ndm->ndm_flags = flags;
2942 ndm->ndm_type = 0;
2943 ndm->ndm_ifindex = dev->ifindex;
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002944 ndm->ndm_state = ndm_state;
John Fastabendd83b0602012-04-15 06:44:08 +00002945
2946 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
2947 goto nla_put_failure;
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002948 if (vid)
2949 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
2950 goto nla_put_failure;
John Fastabendd83b0602012-04-15 06:44:08 +00002951
Johannes Berg053c0952015-01-16 22:09:00 +01002952 nlmsg_end(skb, nlh);
2953 return 0;
John Fastabendd83b0602012-04-15 06:44:08 +00002954
2955nla_put_failure:
2956 nlmsg_cancel(skb, nlh);
2957 return -EMSGSIZE;
2958}
2959
John Fastabend3ff661c2012-04-15 06:44:14 +00002960static inline size_t rtnl_fdb_nlmsg_size(void)
2961{
Sabrina Dubrocaf82ef3e2016-11-18 15:50:39 +01002962 return NLMSG_ALIGN(sizeof(struct ndmsg)) +
2963 nla_total_size(ETH_ALEN) + /* NDA_LLADDR */
2964 nla_total_size(sizeof(u16)) + /* NDA_VLAN */
2965 0;
John Fastabend3ff661c2012-04-15 06:44:14 +00002966}
2967
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002968static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
2969 u16 ndm_state)
John Fastabend3ff661c2012-04-15 06:44:14 +00002970{
2971 struct net *net = dev_net(dev);
2972 struct sk_buff *skb;
2973 int err = -ENOBUFS;
2974
2975 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
2976 if (!skb)
2977 goto errout;
2978
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002979 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002980 0, 0, type, NTF_SELF, 0, ndm_state);
John Fastabend3ff661c2012-04-15 06:44:14 +00002981 if (err < 0) {
2982 kfree_skb(skb);
2983 goto errout;
2984 }
2985
2986 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
2987 return;
2988errout:
2989 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
2990}
2991
Vlad Yasevich090096b2013-03-06 15:39:42 +00002992/**
2993 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2994 */
2995int ndo_dflt_fdb_add(struct ndmsg *ndm,
2996 struct nlattr *tb[],
2997 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01002998 const unsigned char *addr, u16 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00002999 u16 flags)
3000{
3001 int err = -EINVAL;
3002
3003 /* If aging addresses are supported device will need to
3004 * implement its own handler for this.
3005 */
3006 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
3007 pr_info("%s: FDB only supports static addresses\n", dev->name);
3008 return err;
3009 }
3010
Or Gerlitz65891fe2014-12-14 18:19:05 +02003011 if (vid) {
3012 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
3013 return err;
3014 }
3015
Vlad Yasevich090096b2013-03-06 15:39:42 +00003016 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3017 err = dev_uc_add_excl(dev, addr);
3018 else if (is_multicast_ether_addr(addr))
3019 err = dev_mc_add_excl(dev, addr);
3020
3021 /* Only return duplicate errors if NLM_F_EXCL is set */
3022 if (err == -EEXIST && !(flags & NLM_F_EXCL))
3023 err = 0;
3024
3025 return err;
3026}
3027EXPORT_SYMBOL(ndo_dflt_fdb_add);
3028
Jiri Pirkof6f64242014-11-28 14:34:15 +01003029static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid)
3030{
3031 u16 vid = 0;
3032
3033 if (vlan_attr) {
3034 if (nla_len(vlan_attr) != sizeof(u16)) {
3035 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n");
3036 return -EINVAL;
3037 }
3038
3039 vid = nla_get_u16(vlan_attr);
3040
3041 if (!vid || vid >= VLAN_VID_MASK) {
3042 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n",
3043 vid);
3044 return -EINVAL;
3045 }
3046 }
3047 *p_vid = vid;
3048 return 0;
3049}
3050
David Ahernc21ef3e2017-04-16 09:48:24 -07003051static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
3052 struct netlink_ext_ack *extack)
John Fastabend77162022012-04-15 06:43:56 +00003053{
3054 struct net *net = sock_net(skb->sk);
John Fastabend77162022012-04-15 06:43:56 +00003055 struct ndmsg *ndm;
3056 struct nlattr *tb[NDA_MAX+1];
3057 struct net_device *dev;
3058 u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01003059 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00003060 int err;
3061
David Ahernc21ef3e2017-04-16 09:48:24 -07003062 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
John Fastabend77162022012-04-15 06:43:56 +00003063 if (err < 0)
3064 return err;
3065
3066 ndm = nlmsg_data(nlh);
3067 if (ndm->ndm_ifindex == 0) {
3068 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
3069 return -EINVAL;
3070 }
3071
3072 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3073 if (dev == NULL) {
3074 pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
3075 return -ENODEV;
3076 }
3077
3078 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3079 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
3080 return -EINVAL;
3081 }
3082
3083 addr = nla_data(tb[NDA_LLADDR]);
John Fastabend77162022012-04-15 06:43:56 +00003084
Jiri Pirkof6f64242014-11-28 14:34:15 +01003085 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
3086 if (err)
3087 return err;
3088
John Fastabend77162022012-04-15 06:43:56 +00003089 err = -EOPNOTSUPP;
3090
3091 /* Support fdb on master device the net/bridge default case */
3092 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3093 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003094 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3095 const struct net_device_ops *ops = br_dev->netdev_ops;
3096
Jiri Pirkof6f64242014-11-28 14:34:15 +01003097 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
3098 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00003099 if (err)
3100 goto out;
3101 else
3102 ndm->ndm_flags &= ~NTF_MASTER;
3103 }
3104
3105 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00003106 if ((ndm->ndm_flags & NTF_SELF)) {
3107 if (dev->netdev_ops->ndo_fdb_add)
3108 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003109 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003110 nlh->nlmsg_flags);
3111 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01003112 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003113 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00003114
John Fastabend3ff661c2012-04-15 06:44:14 +00003115 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003116 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
3117 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00003118 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00003119 }
John Fastabend77162022012-04-15 06:43:56 +00003120 }
3121out:
3122 return err;
3123}
3124
Vlad Yasevich090096b2013-03-06 15:39:42 +00003125/**
3126 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3127 */
3128int ndo_dflt_fdb_del(struct ndmsg *ndm,
3129 struct nlattr *tb[],
3130 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003131 const unsigned char *addr, u16 vid)
Vlad Yasevich090096b2013-03-06 15:39:42 +00003132{
Alexander Duyckc8a89c42014-07-15 15:15:20 -07003133 int err = -EINVAL;
Vlad Yasevich090096b2013-03-06 15:39:42 +00003134
3135 /* If aging addresses are supported device will need to
3136 * implement its own handler for this.
3137 */
Sridhar Samudrala64535992013-08-08 15:19:48 -07003138 if (!(ndm->ndm_state & NUD_PERMANENT)) {
Vlad Yasevich090096b2013-03-06 15:39:42 +00003139 pr_info("%s: FDB only supports static addresses\n", dev->name);
Alexander Duyckc8a89c42014-07-15 15:15:20 -07003140 return err;
Vlad Yasevich090096b2013-03-06 15:39:42 +00003141 }
3142
3143 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3144 err = dev_uc_del(dev, addr);
3145 else if (is_multicast_ether_addr(addr))
3146 err = dev_mc_del(dev, addr);
Vlad Yasevich090096b2013-03-06 15:39:42 +00003147
3148 return err;
3149}
3150EXPORT_SYMBOL(ndo_dflt_fdb_del);
3151
David Ahernc21ef3e2017-04-16 09:48:24 -07003152static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
3153 struct netlink_ext_ack *extack)
John Fastabend77162022012-04-15 06:43:56 +00003154{
3155 struct net *net = sock_net(skb->sk);
3156 struct ndmsg *ndm;
Vlad Yasevich1690be62013-02-13 12:00:18 +00003157 struct nlattr *tb[NDA_MAX+1];
John Fastabend77162022012-04-15 06:43:56 +00003158 struct net_device *dev;
3159 int err = -EINVAL;
3160 __u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01003161 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00003162
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07003163 if (!netlink_capable(skb, CAP_NET_ADMIN))
Vlad Yasevich1690be62013-02-13 12:00:18 +00003164 return -EPERM;
3165
David Ahernc21ef3e2017-04-16 09:48:24 -07003166 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
Vlad Yasevich1690be62013-02-13 12:00:18 +00003167 if (err < 0)
3168 return err;
John Fastabend77162022012-04-15 06:43:56 +00003169
3170 ndm = nlmsg_data(nlh);
3171 if (ndm->ndm_ifindex == 0) {
3172 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
3173 return -EINVAL;
3174 }
3175
3176 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3177 if (dev == NULL) {
3178 pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
3179 return -ENODEV;
3180 }
3181
Vlad Yasevich1690be62013-02-13 12:00:18 +00003182 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3183 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
John Fastabend77162022012-04-15 06:43:56 +00003184 return -EINVAL;
3185 }
3186
Vlad Yasevich1690be62013-02-13 12:00:18 +00003187 addr = nla_data(tb[NDA_LLADDR]);
Vlad Yasevich1690be62013-02-13 12:00:18 +00003188
Jiri Pirkof6f64242014-11-28 14:34:15 +01003189 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
3190 if (err)
3191 return err;
3192
John Fastabend77162022012-04-15 06:43:56 +00003193 err = -EOPNOTSUPP;
3194
3195 /* Support fdb on master device the net/bridge default case */
3196 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3197 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003198 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3199 const struct net_device_ops *ops = br_dev->netdev_ops;
John Fastabend77162022012-04-15 06:43:56 +00003200
Jiri Pirko898e5062013-01-03 22:48:52 +00003201 if (ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003202 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00003203
3204 if (err)
3205 goto out;
3206 else
3207 ndm->ndm_flags &= ~NTF_MASTER;
3208 }
3209
3210 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00003211 if (ndm->ndm_flags & NTF_SELF) {
3212 if (dev->netdev_ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003213 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
3214 vid);
Vlad Yasevich090096b2013-03-06 15:39:42 +00003215 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01003216 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00003217
John Fastabend3ff661c2012-04-15 06:44:14 +00003218 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003219 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
3220 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00003221 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00003222 }
John Fastabend77162022012-04-15 06:43:56 +00003223 }
3224out:
3225 return err;
3226}
3227
John Fastabendd83b0602012-04-15 06:44:08 +00003228static int nlmsg_populate_fdb(struct sk_buff *skb,
3229 struct netlink_callback *cb,
3230 struct net_device *dev,
3231 int *idx,
3232 struct netdev_hw_addr_list *list)
3233{
3234 struct netdev_hw_addr *ha;
3235 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00003236 u32 portid, seq;
John Fastabendd83b0602012-04-15 06:44:08 +00003237
Eric W. Biederman15e47302012-09-07 20:12:54 +00003238 portid = NETLINK_CB(cb->skb).portid;
John Fastabendd83b0602012-04-15 06:44:08 +00003239 seq = cb->nlh->nlmsg_seq;
3240
3241 list_for_each_entry(ha, &list->list, list) {
Roopa Prabhud2976532016-08-30 21:56:45 -07003242 if (*idx < cb->args[2])
John Fastabendd83b0602012-04-15 06:44:08 +00003243 goto skip;
3244
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003245 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
John Fastabenda7a558f2012-11-01 16:23:10 +00003246 portid, seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003247 RTM_NEWNEIGH, NTF_SELF,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003248 NLM_F_MULTI, NUD_PERMANENT);
John Fastabendd83b0602012-04-15 06:44:08 +00003249 if (err < 0)
3250 return err;
3251skip:
3252 *idx += 1;
3253 }
3254 return 0;
3255}
3256
3257/**
Ben Hutchings2c530402012-07-10 10:55:09 +00003258 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
John Fastabendd83b0602012-04-15 06:44:08 +00003259 * @nlh: netlink message header
3260 * @dev: netdevice
3261 *
3262 * Default netdevice operation to dump the existing unicast address list.
John Fastabend91f3e7b2013-03-29 08:18:37 +00003263 * Returns number of addresses from list put in skb.
John Fastabendd83b0602012-04-15 06:44:08 +00003264 */
3265int ndo_dflt_fdb_dump(struct sk_buff *skb,
3266 struct netlink_callback *cb,
3267 struct net_device *dev,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04003268 struct net_device *filter_dev,
Roopa Prabhud2976532016-08-30 21:56:45 -07003269 int *idx)
John Fastabendd83b0602012-04-15 06:44:08 +00003270{
3271 int err;
3272
3273 netif_addr_lock_bh(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -07003274 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
John Fastabendd83b0602012-04-15 06:44:08 +00003275 if (err)
3276 goto out;
Zhang Shengju2934c9d2016-11-30 16:37:34 +08003277 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
John Fastabendd83b0602012-04-15 06:44:08 +00003278out:
3279 netif_addr_unlock_bh(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -07003280 return err;
John Fastabendd83b0602012-04-15 06:44:08 +00003281}
3282EXPORT_SYMBOL(ndo_dflt_fdb_dump);
3283
John Fastabend77162022012-04-15 06:43:56 +00003284static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
3285{
John Fastabend77162022012-04-15 06:43:56 +00003286 struct net_device *dev;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003287 struct nlattr *tb[IFLA_MAX+1];
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003288 struct net_device *br_dev = NULL;
3289 const struct net_device_ops *ops = NULL;
3290 const struct net_device_ops *cops = NULL;
3291 struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
3292 struct net *net = sock_net(skb->sk);
Roopa Prabhud2976532016-08-30 21:56:45 -07003293 struct hlist_head *head;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003294 int brport_idx = 0;
3295 int br_idx = 0;
Roopa Prabhud2976532016-08-30 21:56:45 -07003296 int h, s_h;
3297 int idx = 0, s_idx;
3298 int err = 0;
3299 int fidx = 0;
John Fastabend77162022012-04-15 06:43:56 +00003300
Alexander Potapenko0ff50e82017-05-23 13:20:28 +02003301 err = nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb,
3302 IFLA_MAX, ifla_policy, NULL);
3303 if (err < 0) {
3304 return -EINVAL;
3305 } else if (err == 0) {
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003306 if (tb[IFLA_MASTER])
3307 br_idx = nla_get_u32(tb[IFLA_MASTER]);
3308 }
John Fastabend77162022012-04-15 06:43:56 +00003309
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003310 brport_idx = ifm->ifi_index;
3311
3312 if (br_idx) {
3313 br_dev = __dev_get_by_index(net, br_idx);
3314 if (!br_dev)
3315 return -ENODEV;
3316
3317 ops = br_dev->netdev_ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003318 }
3319
Roopa Prabhud2976532016-08-30 21:56:45 -07003320 s_h = cb->args[0];
3321 s_idx = cb->args[1];
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003322
Roopa Prabhud2976532016-08-30 21:56:45 -07003323 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
3324 idx = 0;
3325 head = &net->dev_index_head[h];
3326 hlist_for_each_entry(dev, head, index_hlist) {
3327
3328 if (brport_idx && (dev->ifindex != brport_idx))
3329 continue;
3330
3331 if (!br_idx) { /* user did not specify a specific bridge */
3332 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3333 br_dev = netdev_master_upper_dev_get(dev);
3334 cops = br_dev->netdev_ops;
3335 }
3336 } else {
3337 if (dev != br_dev &&
3338 !(dev->priv_flags & IFF_BRIDGE_PORT))
3339 continue;
3340
3341 if (br_dev != netdev_master_upper_dev_get(dev) &&
3342 !(dev->priv_flags & IFF_EBRIDGE))
3343 continue;
3344 cops = ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003345 }
3346
Roopa Prabhud2976532016-08-30 21:56:45 -07003347 if (idx < s_idx)
3348 goto cont;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003349
Roopa Prabhud2976532016-08-30 21:56:45 -07003350 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3351 if (cops && cops->ndo_fdb_dump) {
3352 err = cops->ndo_fdb_dump(skb, cb,
3353 br_dev, dev,
3354 &fidx);
3355 if (err == -EMSGSIZE)
3356 goto out;
3357 }
3358 }
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003359
Roopa Prabhud2976532016-08-30 21:56:45 -07003360 if (dev->netdev_ops->ndo_fdb_dump)
3361 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
3362 dev, NULL,
3363 &fidx);
3364 else
3365 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
3366 &fidx);
3367 if (err == -EMSGSIZE)
3368 goto out;
3369
3370 cops = NULL;
3371
3372 /* reset fdb offset to 0 for rest of the interfaces */
3373 cb->args[2] = 0;
3374 fidx = 0;
3375cont:
3376 idx++;
John Fastabend77162022012-04-15 06:43:56 +00003377 }
John Fastabend77162022012-04-15 06:43:56 +00003378 }
John Fastabend77162022012-04-15 06:43:56 +00003379
Roopa Prabhud2976532016-08-30 21:56:45 -07003380out:
3381 cb->args[0] = h;
3382 cb->args[1] = idx;
3383 cb->args[2] = fidx;
3384
John Fastabend77162022012-04-15 06:43:56 +00003385 return skb->len;
3386}
3387
Scott Feldman2c3c0312014-11-28 14:34:25 +01003388static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
3389 unsigned int attrnum, unsigned int flag)
3390{
3391 if (mask & flag)
3392 return nla_put_u8(skb, attrnum, !!(flags & flag));
3393 return 0;
3394}
3395
John Fastabend815cccb2012-10-24 08:13:09 +00003396int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Scott Feldman2c3c0312014-11-28 14:34:25 +01003397 struct net_device *dev, u16 mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003398 u32 flags, u32 mask, int nlflags,
3399 u32 filter_mask,
3400 int (*vlan_fill)(struct sk_buff *skb,
3401 struct net_device *dev,
3402 u32 filter_mask))
John Fastabend815cccb2012-10-24 08:13:09 +00003403{
3404 struct nlmsghdr *nlh;
3405 struct ifinfomsg *ifm;
3406 struct nlattr *br_afspec;
Scott Feldman2c3c0312014-11-28 14:34:25 +01003407 struct nlattr *protinfo;
John Fastabend815cccb2012-10-24 08:13:09 +00003408 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Jiri Pirko898e5062013-01-03 22:48:52 +00003409 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003410 int err = 0;
John Fastabend815cccb2012-10-24 08:13:09 +00003411
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02003412 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
John Fastabend815cccb2012-10-24 08:13:09 +00003413 if (nlh == NULL)
3414 return -EMSGSIZE;
3415
3416 ifm = nlmsg_data(nlh);
3417 ifm->ifi_family = AF_BRIDGE;
3418 ifm->__ifi_pad = 0;
3419 ifm->ifi_type = dev->type;
3420 ifm->ifi_index = dev->ifindex;
3421 ifm->ifi_flags = dev_get_flags(dev);
3422 ifm->ifi_change = 0;
3423
3424
3425 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
3426 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
3427 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00003428 (br_dev &&
3429 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
John Fastabend815cccb2012-10-24 08:13:09 +00003430 (dev->addr_len &&
3431 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02003432 (dev->ifindex != dev_get_iflink(dev) &&
3433 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
John Fastabend815cccb2012-10-24 08:13:09 +00003434 goto nla_put_failure;
3435
3436 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
3437 if (!br_afspec)
3438 goto nla_put_failure;
3439
Roopa Prabhu1d460b92014-12-08 14:04:20 -08003440 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
John Fastabend815cccb2012-10-24 08:13:09 +00003441 nla_nest_cancel(skb, br_afspec);
3442 goto nla_put_failure;
3443 }
Roopa Prabhu1d460b92014-12-08 14:04:20 -08003444
3445 if (mode != BRIDGE_MODE_UNDEF) {
3446 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
3447 nla_nest_cancel(skb, br_afspec);
3448 goto nla_put_failure;
3449 }
3450 }
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003451 if (vlan_fill) {
3452 err = vlan_fill(skb, dev, filter_mask);
3453 if (err) {
3454 nla_nest_cancel(skb, br_afspec);
3455 goto nla_put_failure;
3456 }
3457 }
John Fastabend815cccb2012-10-24 08:13:09 +00003458 nla_nest_end(skb, br_afspec);
3459
Scott Feldman2c3c0312014-11-28 14:34:25 +01003460 protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
3461 if (!protinfo)
3462 goto nla_put_failure;
3463
3464 if (brport_nla_put_flag(skb, flags, mask,
3465 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
3466 brport_nla_put_flag(skb, flags, mask,
3467 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
3468 brport_nla_put_flag(skb, flags, mask,
3469 IFLA_BRPORT_FAST_LEAVE,
3470 BR_MULTICAST_FAST_LEAVE) ||
3471 brport_nla_put_flag(skb, flags, mask,
3472 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
3473 brport_nla_put_flag(skb, flags, mask,
3474 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
3475 brport_nla_put_flag(skb, flags, mask,
3476 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
3477 brport_nla_put_flag(skb, flags, mask,
3478 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
3479 brport_nla_put_flag(skb, flags, mask,
3480 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
3481 nla_nest_cancel(skb, protinfo);
3482 goto nla_put_failure;
3483 }
3484
3485 nla_nest_end(skb, protinfo);
3486
Johannes Berg053c0952015-01-16 22:09:00 +01003487 nlmsg_end(skb, nlh);
3488 return 0;
John Fastabend815cccb2012-10-24 08:13:09 +00003489nla_put_failure:
3490 nlmsg_cancel(skb, nlh);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003491 return err ? err : -EMSGSIZE;
John Fastabend815cccb2012-10-24 08:13:09 +00003492}
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003493EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
John Fastabend815cccb2012-10-24 08:13:09 +00003494
John Fastabende5a55a82012-10-24 08:12:57 +00003495static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
3496{
3497 struct net *net = sock_net(skb->sk);
3498 struct net_device *dev;
3499 int idx = 0;
3500 u32 portid = NETLINK_CB(cb->skb).portid;
3501 u32 seq = cb->nlh->nlmsg_seq;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00003502 u32 filter_mask = 0;
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003503 int err;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00003504
Thomas Grafaa68c202014-11-26 13:42:20 +01003505 if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
3506 struct nlattr *extfilt;
3507
3508 extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
3509 IFLA_EXT_MASK);
3510 if (extfilt) {
3511 if (nla_len(extfilt) < sizeof(filter_mask))
3512 return -EINVAL;
3513
3514 filter_mask = nla_get_u32(extfilt);
3515 }
3516 }
John Fastabende5a55a82012-10-24 08:12:57 +00003517
3518 rcu_read_lock();
3519 for_each_netdev_rcu(net, dev) {
3520 const struct net_device_ops *ops = dev->netdev_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00003521 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
John Fastabende5a55a82012-10-24 08:12:57 +00003522
Jiri Pirko898e5062013-01-03 22:48:52 +00003523 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003524 if (idx >= cb->args[0]) {
3525 err = br_dev->netdev_ops->ndo_bridge_getlink(
3526 skb, portid, seq, dev,
3527 filter_mask, NLM_F_MULTI);
David Ahernf6c57752017-05-15 23:19:17 -07003528 if (err < 0 && err != -EOPNOTSUPP) {
3529 if (likely(skb->len))
3530 break;
3531
3532 goto out_err;
3533 }
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003534 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00003535 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00003536 }
3537
3538 if (ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003539 if (idx >= cb->args[0]) {
3540 err = ops->ndo_bridge_getlink(skb, portid,
3541 seq, dev,
3542 filter_mask,
3543 NLM_F_MULTI);
David Ahernf6c57752017-05-15 23:19:17 -07003544 if (err < 0 && err != -EOPNOTSUPP) {
3545 if (likely(skb->len))
3546 break;
3547
3548 goto out_err;
3549 }
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003550 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00003551 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00003552 }
3553 }
David Ahernf6c57752017-05-15 23:19:17 -07003554 err = skb->len;
3555out_err:
John Fastabende5a55a82012-10-24 08:12:57 +00003556 rcu_read_unlock();
3557 cb->args[0] = idx;
3558
David Ahernf6c57752017-05-15 23:19:17 -07003559 return err;
John Fastabende5a55a82012-10-24 08:12:57 +00003560}
3561
John Fastabend2469ffd2012-10-24 08:13:03 +00003562static inline size_t bridge_nlmsg_size(void)
3563{
3564 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3565 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3566 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3567 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
3568 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
3569 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
3570 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
3571 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
3572 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
3573 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
3574 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
3575}
3576
Roopa Prabhu02dba432015-01-14 20:02:25 -08003577static int rtnl_bridge_notify(struct net_device *dev)
John Fastabend2469ffd2012-10-24 08:13:03 +00003578{
3579 struct net *net = dev_net(dev);
John Fastabend2469ffd2012-10-24 08:13:03 +00003580 struct sk_buff *skb;
3581 int err = -EOPNOTSUPP;
3582
Roopa Prabhu02dba432015-01-14 20:02:25 -08003583 if (!dev->netdev_ops->ndo_bridge_getlink)
3584 return 0;
3585
John Fastabend2469ffd2012-10-24 08:13:03 +00003586 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
3587 if (!skb) {
3588 err = -ENOMEM;
3589 goto errout;
3590 }
3591
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02003592 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
Roopa Prabhu02dba432015-01-14 20:02:25 -08003593 if (err < 0)
3594 goto errout;
John Fastabend2469ffd2012-10-24 08:13:03 +00003595
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08003596 if (!skb->len)
3597 goto errout;
3598
John Fastabend2469ffd2012-10-24 08:13:03 +00003599 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
3600 return 0;
3601errout:
3602 WARN_ON(err == -EMSGSIZE);
3603 kfree_skb(skb);
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08003604 if (err)
3605 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
John Fastabend2469ffd2012-10-24 08:13:03 +00003606 return err;
3607}
3608
David Ahernc21ef3e2017-04-16 09:48:24 -07003609static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3610 struct netlink_ext_ack *extack)
John Fastabende5a55a82012-10-24 08:12:57 +00003611{
3612 struct net *net = sock_net(skb->sk);
3613 struct ifinfomsg *ifm;
3614 struct net_device *dev;
John Fastabend2469ffd2012-10-24 08:13:03 +00003615 struct nlattr *br_spec, *attr = NULL;
3616 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02003617 u16 flags = 0;
John Fastabendc38e01b2012-11-02 16:32:36 +00003618 bool have_flags = false;
John Fastabende5a55a82012-10-24 08:12:57 +00003619
3620 if (nlmsg_len(nlh) < sizeof(*ifm))
3621 return -EINVAL;
3622
3623 ifm = nlmsg_data(nlh);
3624 if (ifm->ifi_family != AF_BRIDGE)
3625 return -EPFNOSUPPORT;
3626
3627 dev = __dev_get_by_index(net, ifm->ifi_index);
3628 if (!dev) {
3629 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3630 return -ENODEV;
3631 }
3632
John Fastabend2469ffd2012-10-24 08:13:03 +00003633 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3634 if (br_spec) {
3635 nla_for_each_nested(attr, br_spec, rem) {
3636 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01003637 if (nla_len(attr) < sizeof(flags))
3638 return -EINVAL;
3639
John Fastabendc38e01b2012-11-02 16:32:36 +00003640 have_flags = true;
John Fastabend2469ffd2012-10-24 08:13:03 +00003641 flags = nla_get_u16(attr);
3642 break;
3643 }
3644 }
3645 }
3646
3647 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003648 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3649
3650 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
John Fastabend2469ffd2012-10-24 08:13:03 +00003651 err = -EOPNOTSUPP;
3652 goto out;
3653 }
3654
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003655 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
John Fastabende5a55a82012-10-24 08:12:57 +00003656 if (err)
3657 goto out;
John Fastabend2469ffd2012-10-24 08:13:03 +00003658
3659 flags &= ~BRIDGE_FLAGS_MASTER;
John Fastabende5a55a82012-10-24 08:12:57 +00003660 }
3661
John Fastabend2469ffd2012-10-24 08:13:03 +00003662 if ((flags & BRIDGE_FLAGS_SELF)) {
3663 if (!dev->netdev_ops->ndo_bridge_setlink)
3664 err = -EOPNOTSUPP;
3665 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003666 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
3667 flags);
Roopa Prabhu02dba432015-01-14 20:02:25 -08003668 if (!err) {
John Fastabend2469ffd2012-10-24 08:13:03 +00003669 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08003670
3671 /* Generate event to notify upper layer of bridge
3672 * change
3673 */
3674 err = rtnl_bridge_notify(dev);
3675 }
John Fastabend2469ffd2012-10-24 08:13:03 +00003676 }
3677
John Fastabendc38e01b2012-11-02 16:32:36 +00003678 if (have_flags)
John Fastabend2469ffd2012-10-24 08:13:03 +00003679 memcpy(nla_data(attr), &flags, sizeof(flags));
John Fastabende5a55a82012-10-24 08:12:57 +00003680out:
3681 return err;
3682}
3683
David Ahernc21ef3e2017-04-16 09:48:24 -07003684static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
3685 struct netlink_ext_ack *extack)
Vlad Yasevich407af322013-02-13 12:00:12 +00003686{
3687 struct net *net = sock_net(skb->sk);
3688 struct ifinfomsg *ifm;
3689 struct net_device *dev;
3690 struct nlattr *br_spec, *attr = NULL;
3691 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02003692 u16 flags = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +00003693 bool have_flags = false;
3694
3695 if (nlmsg_len(nlh) < sizeof(*ifm))
3696 return -EINVAL;
3697
3698 ifm = nlmsg_data(nlh);
3699 if (ifm->ifi_family != AF_BRIDGE)
3700 return -EPFNOSUPPORT;
3701
3702 dev = __dev_get_by_index(net, ifm->ifi_index);
3703 if (!dev) {
3704 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3705 return -ENODEV;
3706 }
3707
3708 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3709 if (br_spec) {
3710 nla_for_each_nested(attr, br_spec, rem) {
3711 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01003712 if (nla_len(attr) < sizeof(flags))
3713 return -EINVAL;
3714
Vlad Yasevich407af322013-02-13 12:00:12 +00003715 have_flags = true;
3716 flags = nla_get_u16(attr);
3717 break;
3718 }
3719 }
3720 }
3721
Vlad Yasevich407af322013-02-13 12:00:12 +00003722 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3723 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3724
3725 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
3726 err = -EOPNOTSUPP;
3727 goto out;
3728 }
3729
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003730 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00003731 if (err)
3732 goto out;
3733
3734 flags &= ~BRIDGE_FLAGS_MASTER;
3735 }
3736
3737 if ((flags & BRIDGE_FLAGS_SELF)) {
3738 if (!dev->netdev_ops->ndo_bridge_dellink)
3739 err = -EOPNOTSUPP;
3740 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003741 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
3742 flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00003743
Roopa Prabhu02dba432015-01-14 20:02:25 -08003744 if (!err) {
Vlad Yasevich407af322013-02-13 12:00:12 +00003745 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08003746
3747 /* Generate event to notify upper layer of bridge
3748 * change
3749 */
3750 err = rtnl_bridge_notify(dev);
3751 }
Vlad Yasevich407af322013-02-13 12:00:12 +00003752 }
3753
3754 if (have_flags)
3755 memcpy(nla_data(attr), &flags, sizeof(flags));
Vlad Yasevich407af322013-02-13 12:00:12 +00003756out:
3757 return err;
3758}
3759
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003760static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
3761{
3762 return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
3763 (!idxattr || idxattr == attrid);
3764}
3765
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02003766#define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
3767static int rtnl_get_offload_stats_attr_size(int attr_id)
3768{
3769 switch (attr_id) {
3770 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
3771 return sizeof(struct rtnl_link_stats64);
3772 }
3773
3774 return 0;
3775}
3776
3777static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
3778 int *prividx)
3779{
3780 struct nlattr *attr = NULL;
3781 int attr_id, size;
3782 void *attr_data;
3783 int err;
3784
3785 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
3786 dev->netdev_ops->ndo_get_offload_stats))
3787 return -ENODATA;
3788
3789 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
3790 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
3791 if (attr_id < *prividx)
3792 continue;
3793
3794 size = rtnl_get_offload_stats_attr_size(attr_id);
3795 if (!size)
3796 continue;
3797
Or Gerlitz3df5b3c2016-11-22 23:09:54 +02003798 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02003799 continue;
3800
3801 attr = nla_reserve_64bit(skb, attr_id, size,
3802 IFLA_OFFLOAD_XSTATS_UNSPEC);
3803 if (!attr)
3804 goto nla_put_failure;
3805
3806 attr_data = nla_data(attr);
3807 memset(attr_data, 0, size);
3808 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
3809 attr_data);
3810 if (err)
3811 goto get_offload_stats_failure;
3812 }
3813
3814 if (!attr)
3815 return -ENODATA;
3816
3817 *prividx = 0;
3818 return 0;
3819
3820nla_put_failure:
3821 err = -EMSGSIZE;
3822get_offload_stats_failure:
3823 *prividx = attr_id;
3824 return err;
3825}
3826
3827static int rtnl_get_offload_stats_size(const struct net_device *dev)
3828{
3829 int nla_size = 0;
3830 int attr_id;
3831 int size;
3832
3833 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
3834 dev->netdev_ops->ndo_get_offload_stats))
3835 return 0;
3836
3837 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
3838 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
Or Gerlitz3df5b3c2016-11-22 23:09:54 +02003839 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02003840 continue;
3841 size = rtnl_get_offload_stats_attr_size(attr_id);
3842 nla_size += nla_total_size_64bit(size);
3843 }
3844
3845 if (nla_size != 0)
3846 nla_size += nla_total_size(0);
3847
3848 return nla_size;
3849}
3850
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003851static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
3852 int type, u32 pid, u32 seq, u32 change,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003853 unsigned int flags, unsigned int filter_mask,
3854 int *idxattr, int *prividx)
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003855{
3856 struct if_stats_msg *ifsm;
3857 struct nlmsghdr *nlh;
3858 struct nlattr *attr;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003859 int s_prividx = *prividx;
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02003860 int err;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003861
3862 ASSERT_RTNL();
3863
3864 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
3865 if (!nlh)
3866 return -EMSGSIZE;
3867
3868 ifsm = nlmsg_data(nlh);
3869 ifsm->ifindex = dev->ifindex;
3870 ifsm->filter_mask = filter_mask;
3871
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003872 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003873 struct rtnl_link_stats64 *sp;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003874
Nicolas Dichtel58414d32016-04-21 18:58:25 +02003875 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
3876 sizeof(struct rtnl_link_stats64),
3877 IFLA_STATS_UNSPEC);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003878 if (!attr)
3879 goto nla_put_failure;
3880
3881 sp = nla_data(attr);
3882 dev_get_stats(dev, sp);
3883 }
3884
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02003885 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
3886 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
3887
3888 if (ops && ops->fill_linkxstats) {
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02003889 *idxattr = IFLA_STATS_LINK_XSTATS;
3890 attr = nla_nest_start(skb,
3891 IFLA_STATS_LINK_XSTATS);
3892 if (!attr)
3893 goto nla_put_failure;
3894
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02003895 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
3896 nla_nest_end(skb, attr);
3897 if (err)
3898 goto nla_put_failure;
3899 *idxattr = 0;
3900 }
3901 }
3902
3903 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
3904 *idxattr)) {
3905 const struct rtnl_link_ops *ops = NULL;
3906 const struct net_device *master;
3907
3908 master = netdev_master_upper_dev_get(dev);
3909 if (master)
3910 ops = master->rtnl_link_ops;
3911 if (ops && ops->fill_linkxstats) {
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02003912 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
3913 attr = nla_nest_start(skb,
3914 IFLA_STATS_LINK_XSTATS_SLAVE);
3915 if (!attr)
3916 goto nla_put_failure;
3917
3918 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02003919 nla_nest_end(skb, attr);
3920 if (err)
3921 goto nla_put_failure;
3922 *idxattr = 0;
3923 }
3924 }
3925
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02003926 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
3927 *idxattr)) {
3928 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
3929 attr = nla_nest_start(skb, IFLA_STATS_LINK_OFFLOAD_XSTATS);
3930 if (!attr)
3931 goto nla_put_failure;
3932
3933 err = rtnl_get_offload_stats(skb, dev, prividx);
3934 if (err == -ENODATA)
3935 nla_nest_cancel(skb, attr);
3936 else
3937 nla_nest_end(skb, attr);
3938
3939 if (err && err != -ENODATA)
3940 goto nla_put_failure;
3941 *idxattr = 0;
3942 }
3943
Robert Shearmanaefb4d42017-01-16 14:16:36 +00003944 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
3945 struct rtnl_af_ops *af_ops;
3946
3947 *idxattr = IFLA_STATS_AF_SPEC;
3948 attr = nla_nest_start(skb, IFLA_STATS_AF_SPEC);
3949 if (!attr)
3950 goto nla_put_failure;
3951
3952 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
3953 if (af_ops->fill_stats_af) {
3954 struct nlattr *af;
3955 int err;
3956
3957 af = nla_nest_start(skb, af_ops->family);
3958 if (!af)
3959 goto nla_put_failure;
3960
3961 err = af_ops->fill_stats_af(skb, dev);
3962
3963 if (err == -ENODATA)
3964 nla_nest_cancel(skb, af);
3965 else if (err < 0)
3966 goto nla_put_failure;
3967
3968 nla_nest_end(skb, af);
3969 }
3970 }
3971
3972 nla_nest_end(skb, attr);
3973
3974 *idxattr = 0;
3975 }
3976
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003977 nlmsg_end(skb, nlh);
3978
3979 return 0;
3980
3981nla_put_failure:
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003982 /* not a multi message or no progress mean a real error */
3983 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
3984 nlmsg_cancel(skb, nlh);
3985 else
3986 nlmsg_end(skb, nlh);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003987
3988 return -EMSGSIZE;
3989}
3990
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003991static size_t if_nlmsg_stats_size(const struct net_device *dev,
3992 u32 filter_mask)
3993{
3994 size_t size = 0;
3995
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003996 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003997 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
3998
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02003999 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
4000 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004001 int attr = IFLA_STATS_LINK_XSTATS;
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004002
4003 if (ops && ops->get_linkxstats_size) {
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004004 size += nla_total_size(ops->get_linkxstats_size(dev,
4005 attr));
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004006 /* for IFLA_STATS_LINK_XSTATS */
4007 size += nla_total_size(0);
4008 }
4009 }
4010
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004011 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
4012 struct net_device *_dev = (struct net_device *)dev;
4013 const struct rtnl_link_ops *ops = NULL;
4014 const struct net_device *master;
4015
4016 /* netdev_master_upper_dev_get can't take const */
4017 master = netdev_master_upper_dev_get(_dev);
4018 if (master)
4019 ops = master->rtnl_link_ops;
4020 if (ops && ops->get_linkxstats_size) {
4021 int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
4022
4023 size += nla_total_size(ops->get_linkxstats_size(dev,
4024 attr));
4025 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
4026 size += nla_total_size(0);
4027 }
4028 }
4029
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004030 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
4031 size += rtnl_get_offload_stats_size(dev);
4032
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004033 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
4034 struct rtnl_af_ops *af_ops;
4035
4036 /* for IFLA_STATS_AF_SPEC */
4037 size += nla_total_size(0);
4038
4039 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
4040 if (af_ops->get_stats_af_size) {
4041 size += nla_total_size(
4042 af_ops->get_stats_af_size(dev));
4043
4044 /* for AF_* */
4045 size += nla_total_size(0);
4046 }
4047 }
4048 }
4049
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004050 return size;
4051}
4052
David Ahernc21ef3e2017-04-16 09:48:24 -07004053static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
4054 struct netlink_ext_ack *extack)
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004055{
4056 struct net *net = sock_net(skb->sk);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004057 struct net_device *dev = NULL;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004058 int idxattr = 0, prividx = 0;
4059 struct if_stats_msg *ifsm;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004060 struct sk_buff *nskb;
4061 u32 filter_mask;
4062 int err;
4063
Mathias Krause4775cc12016-12-28 17:52:15 +01004064 if (nlmsg_len(nlh) < sizeof(*ifsm))
4065 return -EINVAL;
4066
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004067 ifsm = nlmsg_data(nlh);
4068 if (ifsm->ifindex > 0)
4069 dev = __dev_get_by_index(net, ifsm->ifindex);
4070 else
4071 return -EINVAL;
4072
4073 if (!dev)
4074 return -ENODEV;
4075
4076 filter_mask = ifsm->filter_mask;
4077 if (!filter_mask)
4078 return -EINVAL;
4079
4080 nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
4081 if (!nskb)
4082 return -ENOBUFS;
4083
4084 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
4085 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004086 0, filter_mask, &idxattr, &prividx);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004087 if (err < 0) {
4088 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
4089 WARN_ON(err == -EMSGSIZE);
4090 kfree_skb(nskb);
4091 } else {
4092 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
4093 }
4094
4095 return err;
4096}
4097
4098static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
4099{
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004100 int h, s_h, err, s_idx, s_idxattr, s_prividx;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004101 struct net *net = sock_net(skb->sk);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004102 unsigned int flags = NLM_F_MULTI;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004103 struct if_stats_msg *ifsm;
4104 struct hlist_head *head;
4105 struct net_device *dev;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004106 u32 filter_mask = 0;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004107 int idx = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004108
4109 s_h = cb->args[0];
4110 s_idx = cb->args[1];
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004111 s_idxattr = cb->args[2];
4112 s_prividx = cb->args[3];
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004113
4114 cb->seq = net->dev_base_seq;
4115
Mathias Krause4775cc12016-12-28 17:52:15 +01004116 if (nlmsg_len(cb->nlh) < sizeof(*ifsm))
4117 return -EINVAL;
4118
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004119 ifsm = nlmsg_data(cb->nlh);
4120 filter_mask = ifsm->filter_mask;
4121 if (!filter_mask)
4122 return -EINVAL;
4123
4124 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4125 idx = 0;
4126 head = &net->dev_index_head[h];
4127 hlist_for_each_entry(dev, head, index_hlist) {
4128 if (idx < s_idx)
4129 goto cont;
4130 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
4131 NETLINK_CB(cb->skb).portid,
4132 cb->nlh->nlmsg_seq, 0,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004133 flags, filter_mask,
4134 &s_idxattr, &s_prividx);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004135 /* If we ran out of room on the first message,
4136 * we're in trouble
4137 */
4138 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
4139
4140 if (err < 0)
4141 goto out;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004142 s_prividx = 0;
4143 s_idxattr = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004144 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
4145cont:
4146 idx++;
4147 }
4148 }
4149out:
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004150 cb->args[3] = s_prividx;
4151 cb->args[2] = s_idxattr;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004152 cb->args[1] = idx;
4153 cb->args[0] = h;
4154
4155 return skb->len;
4156}
4157
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158/* Process one rtnetlink message. */
4159
Johannes Berg2d4bc932017-04-12 14:34:04 +02004160static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
4161 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004163 struct net *net = sock_net(skb->sk);
Thomas Grafe2849862007-03-22 11:48:11 -07004164 rtnl_doit_func doit;
Alexander Kuleshov617cfc72016-01-10 21:26:57 +06004165 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 int family;
4167 int type;
Eric Dumazet2907c352011-05-25 07:34:04 +00004168 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 if (type > RTM_MAX)
Thomas Graf038890f2007-04-05 14:35:52 -07004172 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173
4174 type -= RTM_BASE;
4175
4176 /* All the messages must have at least 1 byte length */
Hong zhi guo573ce262013-03-27 06:47:04 +00004177 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 return 0;
4179
Hong zhi guo573ce262013-03-27 06:47:04 +00004180 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 kind = type&3;
4182
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07004183 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07004184 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185
David S. Millerb8f3ab42011-01-18 12:40:38 -08004186 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004187 struct sock *rtnl;
Thomas Grafe2849862007-03-22 11:48:11 -07004188 rtnl_dumpit_func dumpit;
Greg Rosec7ac8672011-06-10 01:27:09 +00004189 rtnl_calcit_func calcit;
4190 u16 min_dump_alloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191
Thomas Grafe2849862007-03-22 11:48:11 -07004192 dumpit = rtnl_get_dumpit(family, type);
4193 if (dumpit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07004194 return -EOPNOTSUPP;
Greg Rosec7ac8672011-06-10 01:27:09 +00004195 calcit = rtnl_get_calcit(family, type);
4196 if (calcit)
Greg Rose115c9b82012-02-21 16:54:48 -05004197 min_dump_alloc = calcit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198
Eric Dumazet2907c352011-05-25 07:34:04 +00004199 __rtnl_unlock();
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004200 rtnl = net->rtnl;
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00004201 {
4202 struct netlink_dump_control c = {
4203 .dump = dumpit,
4204 .min_dump_alloc = min_dump_alloc,
4205 };
4206 err = netlink_dump_start(rtnl, skb, nlh, &c);
4207 }
Eric Dumazet2907c352011-05-25 07:34:04 +00004208 rtnl_lock();
4209 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 }
4211
Thomas Grafe2849862007-03-22 11:48:11 -07004212 doit = rtnl_get_doit(family, type);
4213 if (doit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07004214 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215
David Ahernc21ef3e2017-04-16 09:48:24 -07004216 return doit(skb, nlh, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217}
4218
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07004219static void rtnetlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07004221 rtnl_lock();
4222 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
4223 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224}
4225
Julien Gomes5f729ea2017-06-20 13:54:16 -07004226static int rtnetlink_bind(struct net *net, int group)
4227{
4228 switch (group) {
4229 case RTNLGRP_IPV4_MROUTE_R:
4230 case RTNLGRP_IPV6_MROUTE_R:
4231 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
4232 return -EPERM;
4233 break;
4234 }
4235 return 0;
4236}
4237
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
4239{
Jiri Pirko351638e2013-05-28 01:30:21 +00004240 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02004241
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 switch (event) {
Vlad Yasevich5138e862017-04-04 09:23:41 -04004243 case NETDEV_REBOOT:
David Ahern37536542017-07-19 10:22:40 -07004244 case NETDEV_CHANGEADDR:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004245 case NETDEV_CHANGENAME:
4246 case NETDEV_FEAT_CHANGE:
4247 case NETDEV_BONDING_FAILOVER:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004248 case NETDEV_NOTIFY_PEERS:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004249 case NETDEV_RESEND_IGMP:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004250 case NETDEV_CHANGEINFODATA:
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04004251 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
4252 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 break;
4254 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 break;
4256 }
4257 return NOTIFY_DONE;
4258}
4259
4260static struct notifier_block rtnetlink_dev_notifier = {
4261 .notifier_call = rtnetlink_event,
4262};
4263
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004264
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004265static int __net_init rtnetlink_net_init(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004266{
4267 struct sock *sk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00004268 struct netlink_kernel_cfg cfg = {
4269 .groups = RTNLGRP_MAX,
4270 .input = rtnetlink_rcv,
4271 .cb_mutex = &rtnl_mutex,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00004272 .flags = NL_CFG_F_NONROOT_RECV,
Julien Gomes5f729ea2017-06-20 13:54:16 -07004273 .bind = rtnetlink_bind,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00004274 };
4275
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00004276 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004277 if (!sk)
4278 return -ENOMEM;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004279 net->rtnl = sk;
4280 return 0;
4281}
4282
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004283static void __net_exit rtnetlink_net_exit(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004284{
Denis V. Lunev775516b2008-01-18 23:55:19 -08004285 netlink_kernel_release(net->rtnl);
4286 net->rtnl = NULL;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004287}
4288
4289static struct pernet_operations rtnetlink_net_ops = {
4290 .init = rtnetlink_net_init,
4291 .exit = rtnetlink_net_exit,
4292};
4293
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294void __init rtnetlink_init(void)
4295{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004296 if (register_pernet_subsys(&rtnetlink_net_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 panic("rtnetlink_init: cannot initialize rtnetlink\n");
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004298
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 register_netdevice_notifier(&rtnetlink_dev_notifier);
Thomas Graf340d17f2007-03-22 11:49:22 -07004300
Greg Rosec7ac8672011-06-10 01:27:09 +00004301 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
4302 rtnl_dump_ifinfo, rtnl_calcit);
4303 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
4304 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
4305 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
Thomas Graf687ad8c2007-03-22 11:59:42 -07004306
Greg Rosec7ac8672011-06-10 01:27:09 +00004307 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
4308 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
David Aherna7678c72017-03-21 12:22:26 -07004309 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, NULL);
John Fastabend77162022012-04-15 06:43:56 +00004310
4311 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL);
4312 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL);
4313 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL);
John Fastabende5a55a82012-10-24 08:12:57 +00004314
4315 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL);
Vlad Yasevich407af322013-02-13 12:00:12 +00004316 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL);
John Fastabende5a55a82012-10-24 08:12:57 +00004317 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004318
4319 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
4320 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321}