blob: 8c9d34deea7d0f8fff7202e9ffcffad54b04361f [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;
65};
66
Stephen Hemminger6756ae42006-03-20 22:23:58 -080067static DEFINE_MUTEX(rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69void rtnl_lock(void)
70{
Stephen Hemminger6756ae42006-03-20 22:23:58 -080071 mutex_lock(&rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072}
Eric Dumazete0d087a2009-11-07 01:26:17 -080073EXPORT_SYMBOL(rtnl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Eric Dumazet1b5c5492016-06-13 20:21:50 -070075static struct sk_buff *defer_kfree_skb_list;
76void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail)
77{
78 if (head && tail) {
79 tail->next = defer_kfree_skb_list;
80 defer_kfree_skb_list = head;
81 }
82}
83EXPORT_SYMBOL(rtnl_kfree_skbs);
84
Stephen Hemminger6756ae42006-03-20 22:23:58 -080085void __rtnl_unlock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Eric Dumazet1b5c5492016-06-13 20:21:50 -070087 struct sk_buff *head = defer_kfree_skb_list;
88
89 defer_kfree_skb_list = NULL;
90
Stephen Hemminger6756ae42006-03-20 22:23:58 -080091 mutex_unlock(&rtnl_mutex);
Eric Dumazet1b5c5492016-06-13 20:21:50 -070092
93 while (head) {
94 struct sk_buff *next = head->next;
95
96 kfree_skb(head);
97 cond_resched();
98 head = next;
99 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102void rtnl_unlock(void)
103{
Herbert Xu58ec3b42008-10-07 15:50:03 -0700104 /* This fellow will unlock it for us. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 netdev_run_todo();
106}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800107EXPORT_SYMBOL(rtnl_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800109int rtnl_trylock(void)
110{
111 return mutex_trylock(&rtnl_mutex);
112}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800113EXPORT_SYMBOL(rtnl_trylock);
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800114
Patrick McHardyc9c10142008-04-23 22:10:48 -0700115int rtnl_is_locked(void)
116{
117 return mutex_is_locked(&rtnl_mutex);
118}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800119EXPORT_SYMBOL(rtnl_is_locked);
Patrick McHardyc9c10142008-04-23 22:10:48 -0700120
Paul E. McKenneya898def2010-02-22 17:04:49 -0800121#ifdef CONFIG_PROVE_LOCKING
Yaowei Bai0cbf3342015-10-08 21:29:02 +0800122bool lockdep_rtnl_is_held(void)
Paul E. McKenneya898def2010-02-22 17:04:49 -0800123{
124 return lockdep_is_held(&rtnl_mutex);
125}
126EXPORT_SYMBOL(lockdep_rtnl_is_held);
127#endif /* #ifdef CONFIG_PROVE_LOCKING */
128
Patrick McHardy25239ce2010-04-26 16:02:05 +0200129static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
Thomas Grafe2849862007-03-22 11:48:11 -0700130
131static inline int rtm_msgindex(int msgtype)
132{
133 int msgindex = msgtype - RTM_BASE;
134
135 /*
136 * msgindex < 0 implies someone tried to register a netlink
137 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
138 * the message type has not been added to linux/rtnetlink.h
139 */
140 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
141
142 return msgindex;
143}
144
145static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
146{
147 struct rtnl_link *tab;
148
Patrick McHardy25239ce2010-04-26 16:02:05 +0200149 if (protocol <= RTNL_FAMILY_MAX)
Patrick McHardy0f87b1d2010-04-13 05:03:17 +0000150 tab = rtnl_msg_handlers[protocol];
151 else
152 tab = NULL;
153
Thomas Graf51057f22007-03-22 21:41:06 -0700154 if (tab == NULL || tab[msgindex].doit == NULL)
Thomas Grafe2849862007-03-22 11:48:11 -0700155 tab = rtnl_msg_handlers[PF_UNSPEC];
156
Hans Zhangc80bbea2012-10-22 22:21:23 +0000157 return tab[msgindex].doit;
Thomas Grafe2849862007-03-22 11:48:11 -0700158}
159
160static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
161{
162 struct rtnl_link *tab;
163
Patrick McHardy25239ce2010-04-26 16:02:05 +0200164 if (protocol <= RTNL_FAMILY_MAX)
Patrick McHardy0f87b1d2010-04-13 05:03:17 +0000165 tab = rtnl_msg_handlers[protocol];
166 else
167 tab = NULL;
168
Thomas Graf51057f22007-03-22 21:41:06 -0700169 if (tab == NULL || tab[msgindex].dumpit == NULL)
Thomas Grafe2849862007-03-22 11:48:11 -0700170 tab = rtnl_msg_handlers[PF_UNSPEC];
171
Hans Zhangc80bbea2012-10-22 22:21:23 +0000172 return tab[msgindex].dumpit;
Thomas Grafe2849862007-03-22 11:48:11 -0700173}
174
175/**
176 * __rtnl_register - Register a rtnetlink message type
177 * @protocol: Protocol family or PF_UNSPEC
178 * @msgtype: rtnetlink message type
179 * @doit: Function pointer called for each request message
180 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
Greg Rosec7ac8672011-06-10 01:27:09 +0000181 * @calcit: Function pointer to calc size of dump message
Thomas Grafe2849862007-03-22 11:48:11 -0700182 *
183 * Registers the specified function pointers (at least one of them has
184 * to be non-NULL) to be called whenever a request message for the
185 * specified protocol family and message type is received.
186 *
187 * The special protocol family PF_UNSPEC may be used to define fallback
188 * function pointers for the case when no entry for the specific protocol
189 * family exists.
190 *
191 * Returns 0 on success or a negative error code.
192 */
193int __rtnl_register(int protocol, int msgtype,
Greg Rosec7ac8672011-06-10 01:27:09 +0000194 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
195 rtnl_calcit_func calcit)
Thomas Grafe2849862007-03-22 11:48:11 -0700196{
197 struct rtnl_link *tab;
198 int msgindex;
199
Patrick McHardy25239ce2010-04-26 16:02:05 +0200200 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700201 msgindex = rtm_msgindex(msgtype);
202
203 tab = rtnl_msg_handlers[protocol];
204 if (tab == NULL) {
205 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
206 if (tab == NULL)
207 return -ENOBUFS;
208
209 rtnl_msg_handlers[protocol] = tab;
210 }
211
212 if (doit)
213 tab[msgindex].doit = doit;
214
215 if (dumpit)
216 tab[msgindex].dumpit = dumpit;
217
218 return 0;
219}
Thomas Grafe2849862007-03-22 11:48:11 -0700220EXPORT_SYMBOL_GPL(__rtnl_register);
221
222/**
223 * rtnl_register - Register a rtnetlink message type
224 *
225 * Identical to __rtnl_register() but panics on failure. This is useful
226 * as failure of this function is very unlikely, it can only happen due
227 * to lack of memory when allocating the chain to store all message
228 * handlers for a protocol. Meant for use in init functions where lack
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300229 * of memory implies no sense in continuing.
Thomas Grafe2849862007-03-22 11:48:11 -0700230 */
231void rtnl_register(int protocol, int msgtype,
Greg Rosec7ac8672011-06-10 01:27:09 +0000232 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
233 rtnl_calcit_func calcit)
Thomas Grafe2849862007-03-22 11:48:11 -0700234{
Greg Rosec7ac8672011-06-10 01:27:09 +0000235 if (__rtnl_register(protocol, msgtype, doit, dumpit, calcit) < 0)
Thomas Grafe2849862007-03-22 11:48:11 -0700236 panic("Unable to register rtnetlink message handler, "
237 "protocol = %d, message type = %d\n",
238 protocol, msgtype);
239}
Thomas Grafe2849862007-03-22 11:48:11 -0700240EXPORT_SYMBOL_GPL(rtnl_register);
241
242/**
243 * rtnl_unregister - Unregister a rtnetlink message type
244 * @protocol: Protocol family or PF_UNSPEC
245 * @msgtype: rtnetlink message type
246 *
247 * Returns 0 on success or a negative error code.
248 */
249int rtnl_unregister(int protocol, int msgtype)
250{
251 int msgindex;
252
Patrick McHardy25239ce2010-04-26 16:02:05 +0200253 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700254 msgindex = rtm_msgindex(msgtype);
255
256 if (rtnl_msg_handlers[protocol] == NULL)
257 return -ENOENT;
258
259 rtnl_msg_handlers[protocol][msgindex].doit = NULL;
260 rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
261
262 return 0;
263}
Thomas Grafe2849862007-03-22 11:48:11 -0700264EXPORT_SYMBOL_GPL(rtnl_unregister);
265
266/**
267 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
268 * @protocol : Protocol family or PF_UNSPEC
269 *
270 * Identical to calling rtnl_unregster() for all registered message types
271 * of a certain protocol family.
272 */
273void rtnl_unregister_all(int protocol)
274{
Patrick McHardy25239ce2010-04-26 16:02:05 +0200275 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700276
277 kfree(rtnl_msg_handlers[protocol]);
278 rtnl_msg_handlers[protocol] = NULL;
279}
Thomas Grafe2849862007-03-22 11:48:11 -0700280EXPORT_SYMBOL_GPL(rtnl_unregister_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Patrick McHardy38f7b872007-06-13 12:03:51 -0700282static LIST_HEAD(link_ops);
283
Eric Dumazetc63044f2011-12-13 11:38:00 +0000284static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
285{
286 const struct rtnl_link_ops *ops;
287
288 list_for_each_entry(ops, &link_ops, list) {
289 if (!strcmp(ops->kind, kind))
290 return ops;
291 }
292 return NULL;
293}
294
Patrick McHardy38f7b872007-06-13 12:03:51 -0700295/**
296 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
297 * @ops: struct rtnl_link_ops * to register
298 *
299 * The caller must hold the rtnl_mutex. This function should be used
300 * by drivers that create devices during module initialization. It
301 * must be called before registering the devices.
302 *
303 * Returns 0 on success or a negative error code.
304 */
305int __rtnl_link_register(struct rtnl_link_ops *ops)
306{
Eric Dumazetc63044f2011-12-13 11:38:00 +0000307 if (rtnl_link_ops_get(ops->kind))
308 return -EEXIST;
309
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +0200310 /* The check for setup is here because if ops
311 * does not have that filled up, it is not possible
312 * to use the ops for creating device. So do not
313 * fill up dellink as well. That disables rtnl_dellink.
314 */
315 if (ops->setup && !ops->dellink)
Eric Dumazet23289a32009-10-27 07:06:36 +0000316 ops->dellink = unregister_netdevice_queue;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700317
Patrick McHardy38f7b872007-06-13 12:03:51 -0700318 list_add_tail(&ops->list, &link_ops);
319 return 0;
320}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700321EXPORT_SYMBOL_GPL(__rtnl_link_register);
322
323/**
324 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
325 * @ops: struct rtnl_link_ops * to register
326 *
327 * Returns 0 on success or a negative error code.
328 */
329int rtnl_link_register(struct rtnl_link_ops *ops)
330{
331 int err;
332
333 rtnl_lock();
334 err = __rtnl_link_register(ops);
335 rtnl_unlock();
336 return err;
337}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700338EXPORT_SYMBOL_GPL(rtnl_link_register);
339
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700340static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
341{
342 struct net_device *dev;
Eric Dumazet23289a32009-10-27 07:06:36 +0000343 LIST_HEAD(list_kill);
344
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700345 for_each_netdev(net, dev) {
Eric Dumazet23289a32009-10-27 07:06:36 +0000346 if (dev->rtnl_link_ops == ops)
347 ops->dellink(dev, &list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700348 }
Eric Dumazet23289a32009-10-27 07:06:36 +0000349 unregister_netdevice_many(&list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700350}
351
Patrick McHardy38f7b872007-06-13 12:03:51 -0700352/**
353 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
354 * @ops: struct rtnl_link_ops * to unregister
355 *
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700356 * The caller must hold the rtnl_mutex.
Patrick McHardy38f7b872007-06-13 12:03:51 -0700357 */
358void __rtnl_link_unregister(struct rtnl_link_ops *ops)
359{
Eric W. Biederman881d9662007-09-17 11:56:21 -0700360 struct net *net;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700361
Eric W. Biederman881d9662007-09-17 11:56:21 -0700362 for_each_net(net) {
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700363 __rtnl_kill_links(net, ops);
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700364 }
Patrick McHardy38f7b872007-06-13 12:03:51 -0700365 list_del(&ops->list);
366}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700367EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
368
Cong Wang200b9162014-05-12 15:11:20 -0700369/* Return with the rtnl_lock held when there are no network
370 * devices unregistering in any network namespace.
371 */
372static void rtnl_lock_unregistering_all(void)
373{
374 struct net *net;
375 bool unregistering;
Peter Zijlstraff960a72014-10-29 17:04:56 +0100376 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Cong Wang200b9162014-05-12 15:11:20 -0700377
Peter Zijlstraff960a72014-10-29 17:04:56 +0100378 add_wait_queue(&netdev_unregistering_wq, &wait);
Cong Wang200b9162014-05-12 15:11:20 -0700379 for (;;) {
Cong Wang200b9162014-05-12 15:11:20 -0700380 unregistering = false;
381 rtnl_lock();
382 for_each_net(net) {
383 if (net->dev_unreg_count > 0) {
384 unregistering = true;
385 break;
386 }
387 }
388 if (!unregistering)
389 break;
390 __rtnl_unlock();
Peter Zijlstraff960a72014-10-29 17:04:56 +0100391
392 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Cong Wang200b9162014-05-12 15:11:20 -0700393 }
Peter Zijlstraff960a72014-10-29 17:04:56 +0100394 remove_wait_queue(&netdev_unregistering_wq, &wait);
Cong Wang200b9162014-05-12 15:11:20 -0700395}
396
Patrick McHardy38f7b872007-06-13 12:03:51 -0700397/**
398 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
399 * @ops: struct rtnl_link_ops * to unregister
400 */
401void rtnl_link_unregister(struct rtnl_link_ops *ops)
402{
Cong Wang200b9162014-05-12 15:11:20 -0700403 /* Close the race with cleanup_net() */
404 mutex_lock(&net_mutex);
405 rtnl_lock_unregistering_all();
Patrick McHardy38f7b872007-06-13 12:03:51 -0700406 __rtnl_link_unregister(ops);
407 rtnl_unlock();
Cong Wang200b9162014-05-12 15:11:20 -0700408 mutex_unlock(&net_mutex);
Patrick McHardy38f7b872007-06-13 12:03:51 -0700409}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700410EXPORT_SYMBOL_GPL(rtnl_link_unregister);
411
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100412static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
413{
414 struct net_device *master_dev;
415 const struct rtnl_link_ops *ops;
416
417 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
418 if (!master_dev)
419 return 0;
420 ops = master_dev->rtnl_link_ops;
Fernando Luis Vazquez Cao6049f252014-02-04 19:35:02 +0900421 if (!ops || !ops->get_slave_size)
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100422 return 0;
423 /* IFLA_INFO_SLAVE_DATA + nested data */
424 return nla_total_size(sizeof(struct nlattr)) +
425 ops->get_slave_size(master_dev, dev);
426}
427
Patrick McHardy38f7b872007-06-13 12:03:51 -0700428static size_t rtnl_link_get_size(const struct net_device *dev)
429{
430 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
431 size_t size;
432
433 if (!ops)
434 return 0;
435
Thomas Graf369cf772010-11-11 15:47:59 +0000436 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
437 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700438
439 if (ops->get_size)
440 /* IFLA_INFO_DATA + nested data */
Thomas Graf369cf772010-11-11 15:47:59 +0000441 size += nla_total_size(sizeof(struct nlattr)) +
Patrick McHardy38f7b872007-06-13 12:03:51 -0700442 ops->get_size(dev);
443
444 if (ops->get_xstats_size)
Thomas Graf369cf772010-11-11 15:47:59 +0000445 /* IFLA_INFO_XSTATS */
446 size += nla_total_size(ops->get_xstats_size(dev));
Patrick McHardy38f7b872007-06-13 12:03:51 -0700447
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100448 size += rtnl_link_get_slave_info_data_size(dev);
449
Patrick McHardy38f7b872007-06-13 12:03:51 -0700450 return size;
451}
452
Thomas Graff8ff1822010-11-16 04:30:14 +0000453static LIST_HEAD(rtnl_af_ops);
454
455static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
456{
457 const struct rtnl_af_ops *ops;
458
459 list_for_each_entry(ops, &rtnl_af_ops, list) {
460 if (ops->family == family)
461 return ops;
462 }
463
464 return NULL;
465}
466
467/**
Thomas Graff8ff1822010-11-16 04:30:14 +0000468 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
469 * @ops: struct rtnl_af_ops * to register
470 *
471 * Returns 0 on success or a negative error code.
472 */
stephen hemminger3678a9d2013-12-30 10:41:32 -0800473void rtnl_af_register(struct rtnl_af_ops *ops)
Thomas Graff8ff1822010-11-16 04:30:14 +0000474{
Thomas Graff8ff1822010-11-16 04:30:14 +0000475 rtnl_lock();
stephen hemminger3678a9d2013-12-30 10:41:32 -0800476 list_add_tail(&ops->list, &rtnl_af_ops);
Thomas Graff8ff1822010-11-16 04:30:14 +0000477 rtnl_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +0000478}
479EXPORT_SYMBOL_GPL(rtnl_af_register);
480
481/**
482 * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
483 * @ops: struct rtnl_af_ops * to unregister
484 *
485 * The caller must hold the rtnl_mutex.
486 */
487void __rtnl_af_unregister(struct rtnl_af_ops *ops)
488{
489 list_del(&ops->list);
490}
491EXPORT_SYMBOL_GPL(__rtnl_af_unregister);
492
493/**
494 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
495 * @ops: struct rtnl_af_ops * to unregister
496 */
497void rtnl_af_unregister(struct rtnl_af_ops *ops)
498{
499 rtnl_lock();
500 __rtnl_af_unregister(ops);
501 rtnl_unlock();
502}
503EXPORT_SYMBOL_GPL(rtnl_af_unregister);
504
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700505static size_t rtnl_link_get_af_size(const struct net_device *dev,
506 u32 ext_filter_mask)
Thomas Graff8ff1822010-11-16 04:30:14 +0000507{
508 struct rtnl_af_ops *af_ops;
509 size_t size;
510
511 /* IFLA_AF_SPEC */
512 size = nla_total_size(sizeof(struct nlattr));
513
514 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
515 if (af_ops->get_link_af_size) {
516 /* AF_* + nested data */
517 size += nla_total_size(sizeof(struct nlattr)) +
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700518 af_ops->get_link_af_size(dev, ext_filter_mask);
Thomas Graff8ff1822010-11-16 04:30:14 +0000519 }
520 }
521
522 return size;
523}
524
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100525static bool rtnl_have_link_slave_info(const struct net_device *dev)
526{
527 struct net_device *master_dev;
528
529 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
Jiri Pirko813f0202014-01-23 19:19:21 +0100530 if (master_dev && master_dev->rtnl_link_ops)
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100531 return true;
532 return false;
533}
534
535static int rtnl_link_slave_info_fill(struct sk_buff *skb,
536 const struct net_device *dev)
537{
538 struct net_device *master_dev;
539 const struct rtnl_link_ops *ops;
540 struct nlattr *slave_data;
541 int err;
542
543 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
544 if (!master_dev)
545 return 0;
546 ops = master_dev->rtnl_link_ops;
547 if (!ops)
548 return 0;
549 if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
550 return -EMSGSIZE;
551 if (ops->fill_slave_info) {
552 slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA);
553 if (!slave_data)
554 return -EMSGSIZE;
555 err = ops->fill_slave_info(skb, master_dev, dev);
556 if (err < 0)
557 goto err_cancel_slave_data;
558 nla_nest_end(skb, slave_data);
559 }
560 return 0;
561
562err_cancel_slave_data:
563 nla_nest_cancel(skb, slave_data);
564 return err;
565}
566
567static int rtnl_link_info_fill(struct sk_buff *skb,
568 const struct net_device *dev)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700569{
570 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100571 struct nlattr *data;
572 int err;
573
574 if (!ops)
575 return 0;
576 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
577 return -EMSGSIZE;
578 if (ops->fill_xstats) {
579 err = ops->fill_xstats(skb, dev);
580 if (err < 0)
581 return err;
582 }
583 if (ops->fill_info) {
584 data = nla_nest_start(skb, IFLA_INFO_DATA);
585 if (data == NULL)
586 return -EMSGSIZE;
587 err = ops->fill_info(skb, dev);
588 if (err < 0)
589 goto err_cancel_data;
590 nla_nest_end(skb, data);
591 }
592 return 0;
593
594err_cancel_data:
595 nla_nest_cancel(skb, data);
596 return err;
597}
598
599static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
600{
601 struct nlattr *linkinfo;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700602 int err = -EMSGSIZE;
603
604 linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
605 if (linkinfo == NULL)
606 goto out;
607
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100608 err = rtnl_link_info_fill(skb, dev);
609 if (err < 0)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700610 goto err_cancel_link;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100611
612 err = rtnl_link_slave_info_fill(skb, dev);
613 if (err < 0)
614 goto err_cancel_link;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700615
616 nla_nest_end(skb, linkinfo);
617 return 0;
618
Patrick McHardy38f7b872007-06-13 12:03:51 -0700619err_cancel_link:
620 nla_nest_cancel(skb, linkinfo);
621out:
622 return err;
623}
624
Eric Dumazet95c96172012-04-15 05:58:06 +0000625int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800627 struct sock *rtnl = net->rtnl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 int err = 0;
629
Patrick McHardyac6d4392005-08-14 19:29:52 -0700630 NETLINK_CB(skb).dst_group = group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 if (echo)
Reshetova, Elena63354792017-06-30 13:07:58 +0300632 refcount_inc(&skb->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
634 if (echo)
635 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
636 return err;
637}
638
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800639int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
Thomas Graf2942e902006-08-15 00:30:25 -0700640{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800641 struct sock *rtnl = net->rtnl;
642
Thomas Graf2942e902006-08-15 00:30:25 -0700643 return nlmsg_unicast(rtnl, skb, pid);
644}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800645EXPORT_SYMBOL(rtnl_unicast);
Thomas Graf2942e902006-08-15 00:30:25 -0700646
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800647void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
648 struct nlmsghdr *nlh, gfp_t flags)
Thomas Graf97676b62006-08-15 00:31:41 -0700649{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800650 struct sock *rtnl = net->rtnl;
Thomas Graf97676b62006-08-15 00:31:41 -0700651 int report = 0;
652
653 if (nlh)
654 report = nlmsg_report(nlh);
655
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800656 nlmsg_notify(rtnl, skb, pid, group, report, flags);
Thomas Graf97676b62006-08-15 00:31:41 -0700657}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800658EXPORT_SYMBOL(rtnl_notify);
Thomas Graf97676b62006-08-15 00:31:41 -0700659
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800660void rtnl_set_sk_err(struct net *net, u32 group, int error)
Thomas Graf97676b62006-08-15 00:31:41 -0700661{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800662 struct sock *rtnl = net->rtnl;
663
Thomas Graf97676b62006-08-15 00:31:41 -0700664 netlink_set_err(rtnl, 0, group, error);
665}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800666EXPORT_SYMBOL(rtnl_set_sk_err);
Thomas Graf97676b62006-08-15 00:31:41 -0700667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
669{
Thomas Graf2d7202b2006-08-22 00:01:27 -0700670 struct nlattr *mx;
671 int i, valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Thomas Graf2d7202b2006-08-22 00:01:27 -0700673 mx = nla_nest_start(skb, RTA_METRICS);
674 if (mx == NULL)
675 return -ENOBUFS;
676
677 for (i = 0; i < RTAX_MAX; i++) {
678 if (metrics[i]) {
Daniel Borkmannea697632015-01-05 23:57:47 +0100679 if (i == RTAX_CC_ALGO - 1) {
680 char tmp[TCP_CA_NAME_MAX], *name;
681
682 name = tcp_ca_get_name_by_key(metrics[i], tmp);
683 if (!name)
684 continue;
685 if (nla_put_string(skb, i + 1, name))
686 goto nla_put_failure;
Daniel Borkmannc3a8d942015-08-31 15:58:47 +0200687 } else if (i == RTAX_FEATURES - 1) {
688 u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
689
Phil Sutterf8edcd12016-08-23 13:14:31 +0200690 if (!user_features)
691 continue;
Daniel Borkmannc3a8d942015-08-31 15:58:47 +0200692 BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
693 if (nla_put_u32(skb, i + 1, user_features))
694 goto nla_put_failure;
Daniel Borkmannea697632015-01-05 23:57:47 +0100695 } else {
696 if (nla_put_u32(skb, i + 1, metrics[i]))
697 goto nla_put_failure;
698 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700699 valid++;
Thomas Graf2d7202b2006-08-22 00:01:27 -0700700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
David S. Millera57d27f2006-08-22 22:20:14 -0700703 if (!valid) {
704 nla_nest_cancel(skb, mx);
705 return 0;
706 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700707
708 return nla_nest_end(skb, mx);
709
710nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700711 nla_nest_cancel(skb, mx);
712 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800714EXPORT_SYMBOL(rtnetlink_put_metrics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Thomas Grafe3703b32006-11-27 09:27:07 -0800716int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
David S. Miller87a50692012-07-10 05:06:14 -0700717 long expires, u32 error)
Thomas Grafe3703b32006-11-27 09:27:07 -0800718{
719 struct rta_cacheinfo ci = {
Eric Dumazeta399a802012-08-08 21:13:53 +0000720 .rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse),
Thomas Grafe3703b32006-11-27 09:27:07 -0800721 .rta_used = dst->__use,
722 .rta_clntref = atomic_read(&(dst->__refcnt)),
723 .rta_error = error,
724 .rta_id = id,
Thomas Grafe3703b32006-11-27 09:27:07 -0800725 };
726
Li Wei82539472012-07-29 16:01:30 +0000727 if (expires) {
728 unsigned long clock;
Thomas Grafe3703b32006-11-27 09:27:07 -0800729
Li Wei82539472012-07-29 16:01:30 +0000730 clock = jiffies_to_clock_t(abs(expires));
731 clock = min_t(unsigned long, clock, INT_MAX);
732 ci.rta_expires = (expires > 0) ? clock : -clock;
733 }
Thomas Grafe3703b32006-11-27 09:27:07 -0800734 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
735}
Thomas Grafe3703b32006-11-27 09:27:07 -0800736EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
David S. Miller93b2d4a2008-02-17 18:35:07 -0800738static void set_operstate(struct net_device *dev, unsigned char transition)
Stefan Rompfb00055a2006-03-20 17:09:11 -0800739{
740 unsigned char operstate = dev->operstate;
741
Eric Dumazete0d087a2009-11-07 01:26:17 -0800742 switch (transition) {
Stefan Rompfb00055a2006-03-20 17:09:11 -0800743 case IF_OPER_UP:
744 if ((operstate == IF_OPER_DORMANT ||
745 operstate == IF_OPER_UNKNOWN) &&
746 !netif_dormant(dev))
747 operstate = IF_OPER_UP;
748 break;
749
750 case IF_OPER_DORMANT:
751 if (operstate == IF_OPER_UP ||
752 operstate == IF_OPER_UNKNOWN)
753 operstate = IF_OPER_DORMANT;
754 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700755 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800756
757 if (dev->operstate != operstate) {
758 write_lock_bh(&dev_base_lock);
759 dev->operstate = operstate;
760 write_unlock_bh(&dev_base_lock);
David S. Miller93b2d4a2008-02-17 18:35:07 -0800761 netdev_state_change(dev);
762 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800763}
764
Jiri Bencb1beb682012-07-27 02:58:22 +0000765static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
766{
767 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
768 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
769}
770
Patrick McHardy3729d502010-02-26 06:34:54 +0000771static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
772 const struct ifinfomsg *ifm)
773{
774 unsigned int flags = ifm->ifi_flags;
775
776 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
777 if (ifm->ifi_change)
778 flags = (flags & ifm->ifi_change) |
Jiri Bencb1beb682012-07-27 02:58:22 +0000779 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
Patrick McHardy3729d502010-02-26 06:34:54 +0000780
781 return flags;
782}
783
Thomas Grafb60c5112006-08-04 23:05:34 -0700784static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000785 const struct rtnl_link_stats64 *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Thomas Grafb60c5112006-08-04 23:05:34 -0700787 a->rx_packets = b->rx_packets;
788 a->tx_packets = b->tx_packets;
789 a->rx_bytes = b->rx_bytes;
790 a->tx_bytes = b->tx_bytes;
791 a->rx_errors = b->rx_errors;
792 a->tx_errors = b->tx_errors;
793 a->rx_dropped = b->rx_dropped;
794 a->tx_dropped = b->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Thomas Grafb60c5112006-08-04 23:05:34 -0700796 a->multicast = b->multicast;
797 a->collisions = b->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Thomas Grafb60c5112006-08-04 23:05:34 -0700799 a->rx_length_errors = b->rx_length_errors;
800 a->rx_over_errors = b->rx_over_errors;
801 a->rx_crc_errors = b->rx_crc_errors;
802 a->rx_frame_errors = b->rx_frame_errors;
803 a->rx_fifo_errors = b->rx_fifo_errors;
804 a->rx_missed_errors = b->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Thomas Grafb60c5112006-08-04 23:05:34 -0700806 a->tx_aborted_errors = b->tx_aborted_errors;
807 a->tx_carrier_errors = b->tx_carrier_errors;
808 a->tx_fifo_errors = b->tx_fifo_errors;
809 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
810 a->tx_window_errors = b->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Thomas Grafb60c5112006-08-04 23:05:34 -0700812 a->rx_compressed = b->rx_compressed;
813 a->tx_compressed = b->tx_compressed;
Jarod Wilson6e7333d2016-02-01 18:51:05 -0500814
815 a->rx_nohandler = b->rx_nohandler;
Jan Engelhardt10708f32010-03-11 09:57:29 +0000816}
817
Chris Wrightc02db8c2010-05-16 01:05:45 -0700818/* All VF info */
Greg Rose115c9b82012-02-21 16:54:48 -0500819static inline int rtnl_vfinfo_size(const struct net_device *dev,
820 u32 ext_filter_mask)
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000821{
Phil Sutter9af15c32017-01-18 14:04:39 +0100822 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
Chris Wrightc02db8c2010-05-16 01:05:45 -0700823 int num_vfs = dev_num_vf(dev->dev.parent);
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100824 size_t size = nla_total_size(0);
Scott Feldman045de012010-05-28 03:42:43 -0700825 size += num_vfs *
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100826 (nla_total_size(0) +
827 nla_total_size(sizeof(struct ifla_vf_mac)) +
828 nla_total_size(sizeof(struct ifla_vf_vlan)) +
829 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
Moshe Shemesh79aab092016-09-22 12:11:15 +0300830 nla_total_size(MAX_VLAN_LIST_LEN *
831 sizeof(struct ifla_vf_vlan_info)) +
Sucheta Chakrabortyed616682014-05-22 09:59:05 -0400832 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100833 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
Jiri Benc945a3672014-08-08 16:44:32 +0200834 nla_total_size(sizeof(struct ifla_vf_rate)) +
Vlad Zolotarov01a3d792015-03-30 21:35:23 +0300835 nla_total_size(sizeof(struct ifla_vf_link_state)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300836 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100837 nla_total_size(0) + /* nest IFLA_VF_STATS */
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300838 /* IFLA_VF_STATS_RX_PACKETS */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200839 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300840 /* IFLA_VF_STATS_TX_PACKETS */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200841 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300842 /* IFLA_VF_STATS_RX_BYTES */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200843 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300844 /* IFLA_VF_STATS_TX_BYTES */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200845 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300846 /* IFLA_VF_STATS_BROADCAST */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200847 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300848 /* IFLA_VF_STATS_MULTICAST */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200849 nla_total_size_64bit(sizeof(__u64)) +
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +0000850 nla_total_size(sizeof(struct ifla_vf_trust)));
Chris Wrightc02db8c2010-05-16 01:05:45 -0700851 return size;
852 } else
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000853 return 0;
854}
855
David Gibsonc53864f2014-04-24 10:22:36 +1000856static size_t rtnl_port_size(const struct net_device *dev,
857 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -0700858{
859 size_t port_size = nla_total_size(4) /* PORT_VF */
860 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
Scott Feldman57b61082010-05-17 22:49:55 -0700861 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
862 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
863 + nla_total_size(1) /* PROT_VDP_REQUEST */
864 + nla_total_size(2); /* PORT_VDP_RESPONSE */
865 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
866 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
867 + port_size;
868 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
869 + port_size;
870
David Gibsonc53864f2014-04-24 10:22:36 +1000871 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
872 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -0700873 return 0;
874 if (dev_num_vf(dev->dev.parent))
875 return port_self_size + vf_ports_size +
876 vf_port_size * dev_num_vf(dev->dev.parent);
877 else
878 return port_self_size;
879}
880
David S. Millerb5cdae32017-04-18 15:36:58 -0400881static size_t rtnl_xdp_size(void)
Brenden Blancod1fdd912016-07-19 12:16:49 -0700882{
Sabrina Dubrocab3cfaa32016-11-15 11:16:35 +0100883 size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */
Martin KaFai Lau58038692017-06-15 17:29:09 -0700884 nla_total_size(1) + /* XDP_ATTACHED */
885 nla_total_size(4); /* XDP_PROG_ID */
Brenden Blancod1fdd912016-07-19 12:16:49 -0700886
David S. Millerb5cdae32017-04-18 15:36:58 -0400887 return xdp_size;
Brenden Blancod1fdd912016-07-19 12:16:49 -0700888}
889
Greg Rose115c9b82012-02-21 16:54:48 -0500890static noinline size_t if_nlmsg_size(const struct net_device *dev,
891 u32 ext_filter_mask)
Thomas Graf339bf982006-11-10 14:10:15 -0800892{
893 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
894 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700895 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
Thomas Graf339bf982006-11-10 14:10:15 -0800896 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
Nicolas Dichtel270cb4d2016-04-26 10:06:16 +0200897 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
Thomas Graf339bf982006-11-10 14:10:15 -0800898 + nla_total_size(sizeof(struct rtnl_link_stats))
David S. Miller35c58452016-04-19 19:49:29 -0400899 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
Thomas Graf339bf982006-11-10 14:10:15 -0800900 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
901 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
902 + nla_total_size(4) /* IFLA_TXQLEN */
903 + nla_total_size(4) /* IFLA_WEIGHT */
904 + nla_total_size(4) /* IFLA_MTU */
905 + nla_total_size(4) /* IFLA_LINK */
906 + nla_total_size(4) /* IFLA_MASTER */
Jiri Pirko9a572472012-12-27 23:49:39 +0000907 + nla_total_size(1) /* IFLA_CARRIER */
Ben Greearedbc0bb2012-03-29 12:51:30 +0000908 + nla_total_size(4) /* IFLA_PROMISCUITY */
Jiri Pirko76ff5cc2012-07-20 02:28:48 +0000909 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
910 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
Tobias Klauser69197562016-11-30 14:30:37 +0100911 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
912 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
Thomas Graf339bf982006-11-10 14:10:15 -0800913 + nla_total_size(1) /* IFLA_OPERSTATE */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700914 + nla_total_size(1) /* IFLA_LINKMODE */
david decotigny2d3b4792014-03-29 09:48:35 -0700915 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
Nicolas Dichteld37512a2015-01-15 15:11:16 +0100916 + nla_total_size(4) /* IFLA_LINK_NETNSID */
Serhey Popovychdb833d42017-06-20 14:35:23 +0300917 + nla_total_size(4) /* IFLA_GROUP */
Greg Rose115c9b82012-02-21 16:54:48 -0500918 + nla_total_size(ext_filter_mask
919 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
920 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
David Gibsonc53864f2014-04-24 10:22:36 +1000921 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
Thomas Graff8ff1822010-11-16 04:30:14 +0000922 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700923 + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
Jiri Pirko82f28412014-11-28 14:34:18 +0100924 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
Anuradha Karuppiah88d63782015-07-14 13:43:20 -0700925 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
Nicolas Dichtelc57c7a92016-03-31 18:10:31 +0200926 + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
David S. Millerb5cdae32017-04-18 15:36:58 -0400927 + rtnl_xdp_size() /* IFLA_XDP */
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -0400928 + nla_total_size(4) /* IFLA_EVENT */
Anuradha Karuppiah88d63782015-07-14 13:43:20 -0700929 + nla_total_size(1); /* IFLA_PROTO_DOWN */
930
Thomas Graf339bf982006-11-10 14:10:15 -0800931}
932
Scott Feldman57b61082010-05-17 22:49:55 -0700933static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
934{
935 struct nlattr *vf_ports;
936 struct nlattr *vf_port;
937 int vf;
938 int err;
939
940 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
941 if (!vf_ports)
942 return -EMSGSIZE;
943
944 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
945 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
Scott Feldman8ca94182010-05-28 03:42:18 -0700946 if (!vf_port)
947 goto nla_put_failure;
David S. Millera6574342012-04-01 20:12:00 -0400948 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
949 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -0700950 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
Scott Feldman8ca94182010-05-28 03:42:18 -0700951 if (err == -EMSGSIZE)
952 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -0700953 if (err) {
Scott Feldman57b61082010-05-17 22:49:55 -0700954 nla_nest_cancel(skb, vf_port);
955 continue;
956 }
957 nla_nest_end(skb, vf_port);
958 }
959
960 nla_nest_end(skb, vf_ports);
961
962 return 0;
Scott Feldman8ca94182010-05-28 03:42:18 -0700963
964nla_put_failure:
965 nla_nest_cancel(skb, vf_ports);
966 return -EMSGSIZE;
Scott Feldman57b61082010-05-17 22:49:55 -0700967}
968
969static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
970{
971 struct nlattr *port_self;
972 int err;
973
974 port_self = nla_nest_start(skb, IFLA_PORT_SELF);
975 if (!port_self)
976 return -EMSGSIZE;
977
978 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
979 if (err) {
980 nla_nest_cancel(skb, port_self);
Scott Feldman8ca94182010-05-28 03:42:18 -0700981 return (err == -EMSGSIZE) ? err : 0;
Scott Feldman57b61082010-05-17 22:49:55 -0700982 }
983
984 nla_nest_end(skb, port_self);
985
986 return 0;
987}
988
David Gibsonc53864f2014-04-24 10:22:36 +1000989static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
990 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -0700991{
992 int err;
993
David Gibsonc53864f2014-04-24 10:22:36 +1000994 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
995 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -0700996 return 0;
997
998 err = rtnl_port_self_fill(skb, dev);
999 if (err)
1000 return err;
1001
1002 if (dev_num_vf(dev->dev.parent)) {
1003 err = rtnl_vf_ports_fill(skb, dev);
1004 if (err)
1005 return err;
1006 }
1007
1008 return 0;
1009}
1010
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001011static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
1012{
1013 int err;
Jiri Pirko02637fc2014-11-28 14:34:16 +01001014 struct netdev_phys_item_id ppid;
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001015
1016 err = dev_get_phys_port_id(dev, &ppid);
1017 if (err) {
1018 if (err == -EOPNOTSUPP)
1019 return 0;
1020 return err;
1021 }
1022
1023 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1024 return -EMSGSIZE;
1025
1026 return 0;
1027}
1028
David Aherndb24a902015-03-17 20:23:15 -06001029static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1030{
1031 char name[IFNAMSIZ];
1032 int err;
1033
1034 err = dev_get_phys_port_name(dev, name, sizeof(name));
1035 if (err) {
1036 if (err == -EOPNOTSUPP)
1037 return 0;
1038 return err;
1039 }
1040
Michal Schmidt77ef0332017-05-04 16:48:58 +02001041 if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
David Aherndb24a902015-03-17 20:23:15 -06001042 return -EMSGSIZE;
1043
1044 return 0;
1045}
1046
Jiri Pirko82f28412014-11-28 14:34:18 +01001047static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1048{
1049 int err;
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001050 struct switchdev_attr attr = {
Ido Schimmel6ff64f62015-12-15 16:03:35 +01001051 .orig_dev = dev,
Jiri Pirko1f868392015-10-01 11:03:42 +02001052 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001053 .flags = SWITCHDEV_F_NO_RECURSE,
1054 };
Jiri Pirko82f28412014-11-28 14:34:18 +01001055
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001056 err = switchdev_port_attr_get(dev, &attr);
Jiri Pirko82f28412014-11-28 14:34:18 +01001057 if (err) {
1058 if (err == -EOPNOTSUPP)
1059 return 0;
1060 return err;
1061 }
1062
Scott Feldman42275bd2015-05-13 11:16:50 -07001063 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len,
1064 attr.u.ppid.id))
Jiri Pirko82f28412014-11-28 14:34:18 +01001065 return -EMSGSIZE;
1066
1067 return 0;
1068}
1069
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001070static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1071 struct net_device *dev)
1072{
Roopa Prabhu550bce52016-04-15 20:36:25 -07001073 struct rtnl_link_stats64 *sp;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001074 struct nlattr *attr;
1075
Nicolas Dichtel58414d32016-04-21 18:58:25 +02001076 attr = nla_reserve_64bit(skb, IFLA_STATS64,
1077 sizeof(struct rtnl_link_stats64), IFLA_PAD);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001078 if (!attr)
1079 return -EMSGSIZE;
1080
Roopa Prabhu550bce52016-04-15 20:36:25 -07001081 sp = nla_data(attr);
1082 dev_get_stats(dev, sp);
1083
1084 attr = nla_reserve(skb, IFLA_STATS,
1085 sizeof(struct rtnl_link_stats));
1086 if (!attr)
1087 return -EMSGSIZE;
1088
1089 copy_rtnl_link_stats(nla_data(attr), sp);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001090
1091 return 0;
1092}
1093
1094static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1095 struct net_device *dev,
1096 int vfs_num,
1097 struct nlattr *vfinfo)
1098{
1099 struct ifla_vf_rss_query_en vf_rss_query_en;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001100 struct nlattr *vf, *vfstats, *vfvlanlist;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001101 struct ifla_vf_link_state vf_linkstate;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001102 struct ifla_vf_vlan_info vf_vlan_info;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001103 struct ifla_vf_spoofchk vf_spoofchk;
1104 struct ifla_vf_tx_rate vf_tx_rate;
1105 struct ifla_vf_stats vf_stats;
1106 struct ifla_vf_trust vf_trust;
1107 struct ifla_vf_vlan vf_vlan;
1108 struct ifla_vf_rate vf_rate;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001109 struct ifla_vf_mac vf_mac;
1110 struct ifla_vf_info ivi;
1111
Mintz, Yuval0eed9cf2017-06-07 21:00:33 +03001112 memset(&ivi, 0, sizeof(ivi));
1113
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001114 /* Not all SR-IOV capable drivers support the
1115 * spoofcheck and "RSS query enable" query. Preset to
1116 * -1 so the user space tool can detect that the driver
1117 * didn't report anything.
1118 */
1119 ivi.spoofchk = -1;
1120 ivi.rss_query_en = -1;
1121 ivi.trusted = -1;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001122 /* The default value for VF link state is "auto"
1123 * IFLA_VF_LINK_STATE_AUTO which equals zero
1124 */
1125 ivi.linkstate = 0;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001126 /* VLAN Protocol by default is 802.1Q */
1127 ivi.vlan_proto = htons(ETH_P_8021Q);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001128 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1129 return 0;
1130
Dan Carpenter775f4f02016-10-13 11:45:28 +03001131 memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
1132
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001133 vf_mac.vf =
1134 vf_vlan.vf =
Moshe Shemesh79aab092016-09-22 12:11:15 +03001135 vf_vlan_info.vf =
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001136 vf_rate.vf =
1137 vf_tx_rate.vf =
1138 vf_spoofchk.vf =
1139 vf_linkstate.vf =
1140 vf_rss_query_en.vf =
1141 vf_trust.vf = ivi.vf;
1142
1143 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1144 vf_vlan.vlan = ivi.vlan;
1145 vf_vlan.qos = ivi.qos;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001146 vf_vlan_info.vlan = ivi.vlan;
1147 vf_vlan_info.qos = ivi.qos;
1148 vf_vlan_info.vlan_proto = ivi.vlan_proto;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001149 vf_tx_rate.rate = ivi.max_tx_rate;
1150 vf_rate.min_tx_rate = ivi.min_tx_rate;
1151 vf_rate.max_tx_rate = ivi.max_tx_rate;
1152 vf_spoofchk.setting = ivi.spoofchk;
1153 vf_linkstate.link_state = ivi.linkstate;
1154 vf_rss_query_en.setting = ivi.rss_query_en;
1155 vf_trust.setting = ivi.trusted;
1156 vf = nla_nest_start(skb, IFLA_VF_INFO);
Moshe Shemesh79aab092016-09-22 12:11:15 +03001157 if (!vf)
1158 goto nla_put_vfinfo_failure;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001159 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1160 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1161 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1162 &vf_rate) ||
1163 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1164 &vf_tx_rate) ||
1165 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1166 &vf_spoofchk) ||
1167 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1168 &vf_linkstate) ||
1169 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1170 sizeof(vf_rss_query_en),
1171 &vf_rss_query_en) ||
1172 nla_put(skb, IFLA_VF_TRUST,
1173 sizeof(vf_trust), &vf_trust))
Moshe Shemesh79aab092016-09-22 12:11:15 +03001174 goto nla_put_vf_failure;
1175 vfvlanlist = nla_nest_start(skb, IFLA_VF_VLAN_LIST);
1176 if (!vfvlanlist)
1177 goto nla_put_vf_failure;
1178 if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
1179 &vf_vlan_info)) {
1180 nla_nest_cancel(skb, vfvlanlist);
1181 goto nla_put_vf_failure;
1182 }
1183 nla_nest_end(skb, vfvlanlist);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001184 memset(&vf_stats, 0, sizeof(vf_stats));
1185 if (dev->netdev_ops->ndo_get_vf_stats)
1186 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1187 &vf_stats);
1188 vfstats = nla_nest_start(skb, IFLA_VF_STATS);
Moshe Shemesh79aab092016-09-22 12:11:15 +03001189 if (!vfstats)
1190 goto nla_put_vf_failure;
Nicolas Dichtel343a6d82016-04-25 10:25:14 +02001191 if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1192 vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1193 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1194 vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1195 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1196 vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1197 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1198 vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1199 nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1200 vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1201 nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
Moshe Shemesh79aab092016-09-22 12:11:15 +03001202 vf_stats.multicast, IFLA_VF_STATS_PAD)) {
1203 nla_nest_cancel(skb, vfstats);
1204 goto nla_put_vf_failure;
1205 }
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001206 nla_nest_end(skb, vfstats);
1207 nla_nest_end(skb, vf);
1208 return 0;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001209
1210nla_put_vf_failure:
1211 nla_nest_cancel(skb, vf);
1212nla_put_vfinfo_failure:
1213 nla_nest_cancel(skb, vfinfo);
1214 return -EMSGSIZE;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001215}
1216
1217static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1218{
Kangjie Lu5f8e4472016-05-03 16:46:24 -04001219 struct rtnl_link_ifmap map;
1220
1221 memset(&map, 0, sizeof(map));
1222 map.mem_start = dev->mem_start;
1223 map.mem_end = dev->mem_end;
1224 map.base_addr = dev->base_addr;
1225 map.irq = dev->irq;
1226 map.dma = dev->dma;
1227 map.port = dev->if_port;
1228
Nicolas Dichtel270cb4d2016-04-26 10:06:16 +02001229 if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001230 return -EMSGSIZE;
1231
1232 return 0;
1233}
1234
Martin KaFai Lau58038692017-06-15 17:29:09 -07001235static u8 rtnl_xdp_attached_mode(struct net_device *dev, u32 *prog_id)
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001236{
1237 const struct net_device_ops *ops = dev->netdev_ops;
Martin KaFai Lau58038692017-06-15 17:29:09 -07001238 const struct bpf_prog *generic_xdp_prog;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001239
1240 ASSERT_RTNL();
1241
Martin KaFai Lau58038692017-06-15 17:29:09 -07001242 *prog_id = 0;
1243 generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
1244 if (generic_xdp_prog) {
1245 *prog_id = generic_xdp_prog->aux->id;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001246 return XDP_ATTACHED_SKB;
Martin KaFai Lau58038692017-06-15 17:29:09 -07001247 }
Jakub Kicinskice158e52017-06-21 18:25:09 -07001248 if (!ops->ndo_xdp)
1249 return XDP_ATTACHED_NONE;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001250
Jakub Kicinskice158e52017-06-21 18:25:09 -07001251 return __dev_xdp_attached(dev, ops->ndo_xdp, prog_id);
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001252}
1253
Brenden Blancod1fdd912016-07-19 12:16:49 -07001254static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1255{
Brenden Blancod1fdd912016-07-19 12:16:49 -07001256 struct nlattr *xdp;
Martin KaFai Lau58038692017-06-15 17:29:09 -07001257 u32 prog_id;
Brenden Blancod1fdd912016-07-19 12:16:49 -07001258 int err;
1259
Brenden Blancod1fdd912016-07-19 12:16:49 -07001260 xdp = nla_nest_start(skb, IFLA_XDP);
1261 if (!xdp)
1262 return -EMSGSIZE;
David S. Millerb5cdae32017-04-18 15:36:58 -04001263
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001264 err = nla_put_u8(skb, IFLA_XDP_ATTACHED,
Martin KaFai Lau58038692017-06-15 17:29:09 -07001265 rtnl_xdp_attached_mode(dev, &prog_id));
Brenden Blancod1fdd912016-07-19 12:16:49 -07001266 if (err)
1267 goto err_cancel;
1268
Martin KaFai Lau58038692017-06-15 17:29:09 -07001269 if (prog_id) {
1270 err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
1271 if (err)
1272 goto err_cancel;
1273 }
1274
Brenden Blancod1fdd912016-07-19 12:16:49 -07001275 nla_nest_end(skb, xdp);
1276 return 0;
1277
1278err_cancel:
1279 nla_nest_cancel(skb, xdp);
1280 return err;
1281}
1282
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001283static u32 rtnl_get_event(unsigned long event)
1284{
1285 u32 rtnl_event_type = IFLA_EVENT_NONE;
1286
1287 switch (event) {
1288 case NETDEV_REBOOT:
1289 rtnl_event_type = IFLA_EVENT_REBOOT;
1290 break;
1291 case NETDEV_FEAT_CHANGE:
1292 rtnl_event_type = IFLA_EVENT_FEATURES;
1293 break;
1294 case NETDEV_BONDING_FAILOVER:
1295 rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
1296 break;
1297 case NETDEV_NOTIFY_PEERS:
1298 rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
1299 break;
1300 case NETDEV_RESEND_IGMP:
1301 rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
1302 break;
1303 case NETDEV_CHANGEINFODATA:
1304 rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
1305 break;
1306 default:
1307 break;
1308 }
1309
1310 return rtnl_event_type;
1311}
1312
Thomas Grafb60c5112006-08-04 23:05:34 -07001313static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
Patrick McHardy575c3e22007-05-22 17:00:49 -07001314 int type, u32 pid, u32 seq, u32 change,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001315 unsigned int flags, u32 ext_filter_mask,
1316 u32 event)
Thomas Grafb60c5112006-08-04 23:05:34 -07001317{
1318 struct ifinfomsg *ifm;
1319 struct nlmsghdr *nlh;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001320 struct nlattr *af_spec;
Thomas Graff8ff1822010-11-16 04:30:14 +00001321 struct rtnl_af_ops *af_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00001322 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Thomas Grafb60c5112006-08-04 23:05:34 -07001323
Eric Dumazet2907c352011-05-25 07:34:04 +00001324 ASSERT_RTNL();
Thomas Grafb60c5112006-08-04 23:05:34 -07001325 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1326 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08001327 return -EMSGSIZE;
Thomas Grafb60c5112006-08-04 23:05:34 -07001328
1329 ifm = nlmsg_data(nlh);
1330 ifm->ifi_family = AF_UNSPEC;
1331 ifm->__ifi_pad = 0;
1332 ifm->ifi_type = dev->type;
1333 ifm->ifi_index = dev->ifindex;
1334 ifm->ifi_flags = dev_get_flags(dev);
1335 ifm->ifi_change = change;
1336
David S. Millera6574342012-04-01 20:12:00 -04001337 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1338 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1339 nla_put_u8(skb, IFLA_OPERSTATE,
1340 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1341 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1342 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1343 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
Ben Greearedbc0bb2012-03-29 12:51:30 +00001344 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001345 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
Eric Dumazetc70ce022016-03-21 09:55:10 -07001346 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1347 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001348#ifdef CONFIG_RPS
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001349 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001350#endif
Nicolas Dichtela54acb32015-04-02 17:07:00 +02001351 (dev->ifindex != dev_get_iflink(dev) &&
1352 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00001353 (upper_dev &&
1354 nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
Jiri Pirko9a572472012-12-27 23:49:39 +00001355 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
David S. Millera6574342012-04-01 20:12:00 -04001356 (dev->qdisc &&
1357 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1358 (dev->ifalias &&
david decotigny2d3b4792014-03-29 09:48:35 -07001359 nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) ||
1360 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001361 atomic_read(&dev->carrier_changes)) ||
1362 nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
David S. Millera6574342012-04-01 20:12:00 -04001363 goto nla_put_failure;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001364
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001365 if (event != IFLA_EVENT_NONE) {
1366 if (nla_put_u32(skb, IFLA_EVENT, event))
1367 goto nla_put_failure;
1368 }
1369
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001370 if (rtnl_fill_link_ifmap(skb, dev))
1371 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
1373 if (dev->addr_len) {
David S. Millera6574342012-04-01 20:12:00 -04001374 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1375 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1376 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 }
1378
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001379 if (rtnl_phys_port_id_fill(skb, dev))
1380 goto nla_put_failure;
1381
David Aherndb24a902015-03-17 20:23:15 -06001382 if (rtnl_phys_port_name_fill(skb, dev))
1383 goto nla_put_failure;
1384
Jiri Pirko82f28412014-11-28 14:34:18 +01001385 if (rtnl_phys_switch_id_fill(skb, dev))
1386 goto nla_put_failure;
1387
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001388 if (rtnl_fill_stats(skb, dev))
Pavel Emelyanov96e74082008-05-21 14:12:46 -07001389 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
David S. Millera6574342012-04-01 20:12:00 -04001391 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
1392 nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
1393 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001394
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001395 if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent &&
1396 ext_filter_mask & RTEXT_FILTER_VF) {
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001397 int i;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001398 struct nlattr *vfinfo;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001399 int num_vfs = dev_num_vf(dev->dev.parent);
1400
Chris Wrightc02db8c2010-05-16 01:05:45 -07001401 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1402 if (!vfinfo)
1403 goto nla_put_failure;
1404 for (i = 0; i < num_vfs; i++) {
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001405 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
Chris Wrightc02db8c2010-05-16 01:05:45 -07001406 goto nla_put_failure;
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001407 }
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001408
Chris Wrightc02db8c2010-05-16 01:05:45 -07001409 nla_nest_end(skb, vfinfo);
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001410 }
Scott Feldman57b61082010-05-17 22:49:55 -07001411
David Gibsonc53864f2014-04-24 10:22:36 +10001412 if (rtnl_port_fill(skb, dev, ext_filter_mask))
Scott Feldman57b61082010-05-17 22:49:55 -07001413 goto nla_put_failure;
1414
Brenden Blancod1fdd912016-07-19 12:16:49 -07001415 if (rtnl_xdp_fill(skb, dev))
1416 goto nla_put_failure;
1417
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001418 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07001419 if (rtnl_link_fill(skb, dev) < 0)
1420 goto nla_put_failure;
1421 }
1422
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001423 if (dev->rtnl_link_ops &&
1424 dev->rtnl_link_ops->get_link_net) {
1425 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1426
1427 if (!net_eq(dev_net(dev), link_net)) {
Nicolas Dichtel7a0877d2015-05-07 11:02:49 +02001428 int id = peernet2id_alloc(dev_net(dev), link_net);
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001429
1430 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1431 goto nla_put_failure;
1432 }
1433 }
1434
Thomas Graff8ff1822010-11-16 04:30:14 +00001435 if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1436 goto nla_put_failure;
1437
1438 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1439 if (af_ops->fill_link_af) {
1440 struct nlattr *af;
1441 int err;
1442
1443 if (!(af = nla_nest_start(skb, af_ops->family)))
1444 goto nla_put_failure;
1445
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04001446 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
Thomas Graff8ff1822010-11-16 04:30:14 +00001447
1448 /*
1449 * Caller may return ENODATA to indicate that there
1450 * was no data to be dumped. This is not an error, it
1451 * means we should trim the attribute header and
1452 * continue.
1453 */
1454 if (err == -ENODATA)
1455 nla_nest_cancel(skb, af);
1456 else if (err < 0)
1457 goto nla_put_failure;
1458
1459 nla_nest_end(skb, af);
1460 }
1461 }
1462
1463 nla_nest_end(skb, af_spec);
1464
Johannes Berg053c0952015-01-16 22:09:00 +01001465 nlmsg_end(skb, nlh);
1466 return 0;
Thomas Grafb60c5112006-08-04 23:05:34 -07001467
1468nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001469 nlmsg_cancel(skb, nlh);
1470 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471}
1472
Jiri Pirkof7b12602014-02-18 20:53:18 +01001473static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1474 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1475 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1476 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1477 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1478 [IFLA_MTU] = { .type = NLA_U32 },
1479 [IFLA_LINK] = { .type = NLA_U32 },
1480 [IFLA_MASTER] = { .type = NLA_U32 },
1481 [IFLA_CARRIER] = { .type = NLA_U8 },
1482 [IFLA_TXQLEN] = { .type = NLA_U32 },
1483 [IFLA_WEIGHT] = { .type = NLA_U32 },
1484 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1485 [IFLA_LINKMODE] = { .type = NLA_U8 },
1486 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1487 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1488 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
1489 [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 },
1490 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1491 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1492 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1493 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1494 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1495 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1496 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1497 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
Jiri Pirko02637fc2014-11-28 14:34:16 +01001498 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
david decotigny2d3b4792014-03-29 09:48:35 -07001499 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
Jiri Pirko82f28412014-11-28 14:34:18 +01001500 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
Nicolas Dichtel317f4812015-01-15 15:11:18 +01001501 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001502 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001503 [IFLA_XDP] = { .type = NLA_NESTED },
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001504 [IFLA_EVENT] = { .type = NLA_U32 },
Serhey Popovychdb833d42017-06-20 14:35:23 +03001505 [IFLA_GROUP] = { .type = NLA_U32 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001506};
1507
1508static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1509 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1510 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1511 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1512 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1513};
1514
Jiri Pirkof7b12602014-02-18 20:53:18 +01001515static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
Daniel Borkmann364d5712015-02-05 18:44:04 +01001516 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1517 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
Moshe Shemesh79aab092016-09-22 12:11:15 +03001518 [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED },
Daniel Borkmann364d5712015-02-05 18:44:04 +01001519 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1520 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1521 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1522 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001523 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001524 [IFLA_VF_STATS] = { .type = NLA_NESTED },
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00001525 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
Eli Cohencc8e27c2016-03-11 22:58:34 +02001526 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1527 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001528};
1529
Jiri Pirkof7b12602014-02-18 20:53:18 +01001530static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1531 [IFLA_PORT_VF] = { .type = NLA_U32 },
1532 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1533 .len = PORT_PROFILE_MAX },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001534 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1535 .len = PORT_UUID_MAX },
1536 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1537 .len = PORT_UUID_MAX },
1538 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1539 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
Daniel Borkmann025331d2017-02-17 01:56:11 +01001540
1541 /* Unused, but we need to keep it here since user space could
1542 * fill it. It's also broken with regard to NLA_BINARY use in
1543 * combination with structs.
1544 */
1545 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1546 .len = sizeof(struct ifla_port_vsi) },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001547};
1548
Brenden Blancod1fdd912016-07-19 12:16:49 -07001549static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
1550 [IFLA_XDP_FD] = { .type = NLA_S32 },
1551 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 },
Daniel Borkmann85de8572016-11-28 23:16:54 +01001552 [IFLA_XDP_FLAGS] = { .type = NLA_U32 },
Martin KaFai Lau58038692017-06-15 17:29:09 -07001553 [IFLA_XDP_PROG_ID] = { .type = NLA_U32 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001554};
1555
David Aherndc599f72016-02-02 08:17:07 -08001556static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1557{
1558 const struct rtnl_link_ops *ops = NULL;
1559 struct nlattr *linfo[IFLA_INFO_MAX + 1];
1560
Johannes Bergfceb6432017-04-12 14:34:07 +02001561 if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla,
1562 ifla_info_policy, NULL) < 0)
David Aherndc599f72016-02-02 08:17:07 -08001563 return NULL;
1564
1565 if (linfo[IFLA_INFO_KIND]) {
1566 char kind[MODULE_NAME_LEN];
1567
1568 nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1569 ops = rtnl_link_ops_get(kind);
1570 }
1571
1572 return ops;
1573}
1574
1575static bool link_master_filtered(struct net_device *dev, int master_idx)
1576{
1577 struct net_device *master;
1578
1579 if (!master_idx)
1580 return false;
1581
1582 master = netdev_master_upper_dev_get(dev);
1583 if (!master || master->ifindex != master_idx)
1584 return true;
1585
1586 return false;
1587}
1588
1589static bool link_kind_filtered(const struct net_device *dev,
1590 const struct rtnl_link_ops *kind_ops)
1591{
1592 if (kind_ops && dev->rtnl_link_ops != kind_ops)
1593 return true;
1594
1595 return false;
1596}
1597
1598static bool link_dump_filtered(struct net_device *dev,
1599 int master_idx,
1600 const struct rtnl_link_ops *kind_ops)
1601{
1602 if (link_master_filtered(dev, master_idx) ||
1603 link_kind_filtered(dev, kind_ops))
1604 return true;
1605
1606 return false;
1607}
1608
Thomas Grafb60c5112006-08-04 23:05:34 -07001609static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001611 struct net *net = sock_net(skb->sk);
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001612 int h, s_h;
1613 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 struct net_device *dev;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001615 struct hlist_head *head;
Greg Rose115c9b82012-02-21 16:54:48 -05001616 struct nlattr *tb[IFLA_MAX+1];
1617 u32 ext_filter_mask = 0;
David Aherndc599f72016-02-02 08:17:07 -08001618 const struct rtnl_link_ops *kind_ops = NULL;
1619 unsigned int flags = NLM_F_MULTI;
1620 int master_idx = 0;
David Gibson973462b2014-04-24 10:22:35 +10001621 int err;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001622 int hdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001624 s_h = cb->args[0];
1625 s_idx = cb->args[1];
1626
Thomas Graf4e985ad2011-06-21 03:11:20 +00001627 cb->seq = net->dev_base_seq;
1628
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001629 /* A hack to preserve kernel<->userspace interface.
1630 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1631 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1632 * what iproute2 < v3.9.0 used.
1633 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1634 * attribute, its netlink message is shorter than struct ifinfomsg.
1635 */
1636 hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1637 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1638
Johannes Bergfceb6432017-04-12 14:34:07 +02001639 if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX,
1640 ifla_policy, NULL) >= 0) {
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001641 if (tb[IFLA_EXT_MASK])
1642 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
David Aherndc599f72016-02-02 08:17:07 -08001643
1644 if (tb[IFLA_MASTER])
1645 master_idx = nla_get_u32(tb[IFLA_MASTER]);
1646
1647 if (tb[IFLA_LINKINFO])
1648 kind_ops = linkinfo_to_kind_ops(tb[IFLA_LINKINFO]);
1649
1650 if (master_idx || kind_ops)
1651 flags |= NLM_F_DUMP_FILTERED;
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001652 }
Greg Rose115c9b82012-02-21 16:54:48 -05001653
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001654 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1655 idx = 0;
1656 head = &net->dev_index_head[h];
Eric Dumazetcac5e652015-02-27 09:42:50 -08001657 hlist_for_each_entry(dev, head, index_hlist) {
David Aherndc599f72016-02-02 08:17:07 -08001658 if (link_dump_filtered(dev, master_idx, kind_ops))
Zhang Shengju3f0ae052016-11-19 23:28:32 +08001659 goto cont;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001660 if (idx < s_idx)
1661 goto cont;
David Gibson973462b2014-04-24 10:22:35 +10001662 err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
1663 NETLINK_CB(cb->skb).portid,
1664 cb->nlh->nlmsg_seq, 0,
David Aherndc599f72016-02-02 08:17:07 -08001665 flags,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001666 ext_filter_mask, 0);
David Gibson973462b2014-04-24 10:22:35 +10001667
David Ahernf6c57752017-05-15 23:19:17 -07001668 if (err < 0) {
1669 if (likely(skb->len))
1670 goto out;
1671
1672 goto out_err;
1673 }
Thomas Graf4e985ad2011-06-21 03:11:20 +00001674
1675 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Pavel Emelianov7562f872007-05-03 15:13:45 -07001676cont:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001677 idx++;
1678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 }
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001680out:
David Ahernf6c57752017-05-15 23:19:17 -07001681 err = skb->len;
1682out_err:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001683 cb->args[1] = idx;
1684 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
David Ahernf6c57752017-05-15 23:19:17 -07001686 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687}
1688
Johannes Bergfceb6432017-04-12 14:34:07 +02001689int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
1690 struct netlink_ext_ack *exterr)
Jiri Pirkof7b12602014-02-18 20:53:18 +01001691{
Johannes Bergfceb6432017-04-12 14:34:07 +02001692 return nla_parse(tb, IFLA_MAX, head, len, ifla_policy, exterr);
Jiri Pirkof7b12602014-02-18 20:53:18 +01001693}
1694EXPORT_SYMBOL(rtnl_nla_parse_ifla);
Scott Feldman57b61082010-05-17 22:49:55 -07001695
Eric W. Biederman81adee42009-11-08 00:53:51 -08001696struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1697{
1698 struct net *net;
1699 /* Examine the link attributes and figure out which
1700 * network namespace we are talking about.
1701 */
1702 if (tb[IFLA_NET_NS_PID])
1703 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001704 else if (tb[IFLA_NET_NS_FD])
1705 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
Eric W. Biederman81adee42009-11-08 00:53:51 -08001706 else
1707 net = get_net(src_net);
1708 return net;
1709}
1710EXPORT_SYMBOL(rtnl_link_get_net);
1711
Thomas Graf1840bb12008-02-23 19:54:36 -08001712static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1713{
1714 if (dev) {
1715 if (tb[IFLA_ADDRESS] &&
1716 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1717 return -EINVAL;
1718
1719 if (tb[IFLA_BROADCAST] &&
1720 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1721 return -EINVAL;
1722 }
1723
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001724 if (tb[IFLA_AF_SPEC]) {
1725 struct nlattr *af;
1726 int rem, err;
1727
1728 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1729 const struct rtnl_af_ops *af_ops;
1730
1731 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1732 return -EAFNOSUPPORT;
1733
1734 if (!af_ops->set_link_af)
1735 return -EOPNOTSUPP;
1736
1737 if (af_ops->validate_link_af) {
Kurt Van Dijck6d3a9a62011-01-26 04:55:24 +00001738 err = af_ops->validate_link_af(dev, af);
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001739 if (err < 0)
1740 return err;
1741 }
1742 }
1743 }
1744
Thomas Graf1840bb12008-02-23 19:54:36 -08001745 return 0;
1746}
1747
Eli Cohencc8e27c2016-03-11 22:58:34 +02001748static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
1749 int guid_type)
1750{
1751 const struct net_device_ops *ops = dev->netdev_ops;
1752
1753 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
1754}
1755
1756static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
1757{
1758 if (dev->type != ARPHRD_INFINIBAND)
1759 return -EOPNOTSUPP;
1760
1761 return handle_infiniband_guid(dev, ivt, guid_type);
1762}
1763
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001764static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
Chris Wrightc02db8c2010-05-16 01:05:45 -07001765{
Chris Wrightc02db8c2010-05-16 01:05:45 -07001766 const struct net_device_ops *ops = dev->netdev_ops;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001767 int err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001768
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001769 if (tb[IFLA_VF_MAC]) {
1770 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001771
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001772 err = -EOPNOTSUPP;
1773 if (ops->ndo_set_vf_mac)
1774 err = ops->ndo_set_vf_mac(dev, ivm->vf,
1775 ivm->mac);
1776 if (err < 0)
1777 return err;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001778 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001779
1780 if (tb[IFLA_VF_VLAN]) {
1781 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
1782
1783 err = -EOPNOTSUPP;
1784 if (ops->ndo_set_vf_vlan)
1785 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
Moshe Shemesh79aab092016-09-22 12:11:15 +03001786 ivv->qos,
1787 htons(ETH_P_8021Q));
1788 if (err < 0)
1789 return err;
1790 }
1791
1792 if (tb[IFLA_VF_VLAN_LIST]) {
1793 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
1794 struct nlattr *attr;
1795 int rem, len = 0;
1796
1797 err = -EOPNOTSUPP;
1798 if (!ops->ndo_set_vf_vlan)
1799 return err;
1800
1801 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
1802 if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
1803 nla_len(attr) < NLA_HDRLEN) {
1804 return -EINVAL;
1805 }
1806 if (len >= MAX_VLAN_LIST_LEN)
1807 return -EOPNOTSUPP;
1808 ivvl[len] = nla_data(attr);
1809
1810 len++;
1811 }
Arnd Bergmannfa34cd92016-09-30 18:13:49 +02001812 if (len == 0)
1813 return -EINVAL;
1814
Moshe Shemesh79aab092016-09-22 12:11:15 +03001815 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
1816 ivvl[0]->qos, ivvl[0]->vlan_proto);
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001817 if (err < 0)
1818 return err;
1819 }
1820
1821 if (tb[IFLA_VF_TX_RATE]) {
1822 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
1823 struct ifla_vf_info ivf;
1824
1825 err = -EOPNOTSUPP;
1826 if (ops->ndo_get_vf_config)
1827 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
1828 if (err < 0)
1829 return err;
1830
1831 err = -EOPNOTSUPP;
1832 if (ops->ndo_set_vf_rate)
1833 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1834 ivf.min_tx_rate,
1835 ivt->rate);
1836 if (err < 0)
1837 return err;
1838 }
1839
1840 if (tb[IFLA_VF_RATE]) {
1841 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
1842
1843 err = -EOPNOTSUPP;
1844 if (ops->ndo_set_vf_rate)
1845 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1846 ivt->min_tx_rate,
1847 ivt->max_tx_rate);
1848 if (err < 0)
1849 return err;
1850 }
1851
1852 if (tb[IFLA_VF_SPOOFCHK]) {
1853 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
1854
1855 err = -EOPNOTSUPP;
1856 if (ops->ndo_set_vf_spoofchk)
1857 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
1858 ivs->setting);
1859 if (err < 0)
1860 return err;
1861 }
1862
1863 if (tb[IFLA_VF_LINK_STATE]) {
1864 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
1865
1866 err = -EOPNOTSUPP;
1867 if (ops->ndo_set_vf_link_state)
1868 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
1869 ivl->link_state);
1870 if (err < 0)
1871 return err;
1872 }
1873
1874 if (tb[IFLA_VF_RSS_QUERY_EN]) {
1875 struct ifla_vf_rss_query_en *ivrssq_en;
1876
1877 err = -EOPNOTSUPP;
1878 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
1879 if (ops->ndo_set_vf_rss_query_en)
1880 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
1881 ivrssq_en->setting);
1882 if (err < 0)
1883 return err;
1884 }
1885
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00001886 if (tb[IFLA_VF_TRUST]) {
1887 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
1888
1889 err = -EOPNOTSUPP;
1890 if (ops->ndo_set_vf_trust)
1891 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
1892 if (err < 0)
1893 return err;
1894 }
1895
Eli Cohencc8e27c2016-03-11 22:58:34 +02001896 if (tb[IFLA_VF_IB_NODE_GUID]) {
1897 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
1898
1899 if (!ops->ndo_set_vf_guid)
1900 return -EOPNOTSUPP;
1901
1902 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
1903 }
1904
1905 if (tb[IFLA_VF_IB_PORT_GUID]) {
1906 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
1907
1908 if (!ops->ndo_set_vf_guid)
1909 return -EOPNOTSUPP;
1910
1911 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
1912 }
1913
Chris Wrightc02db8c2010-05-16 01:05:45 -07001914 return err;
1915}
1916
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001917static int do_set_master(struct net_device *dev, int ifindex)
1918{
Jiri Pirko898e5062013-01-03 22:48:52 +00001919 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001920 const struct net_device_ops *ops;
1921 int err;
1922
Jiri Pirko898e5062013-01-03 22:48:52 +00001923 if (upper_dev) {
1924 if (upper_dev->ifindex == ifindex)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001925 return 0;
Jiri Pirko898e5062013-01-03 22:48:52 +00001926 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001927 if (ops->ndo_del_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001928 err = ops->ndo_del_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001929 if (err)
1930 return err;
1931 } else {
1932 return -EOPNOTSUPP;
1933 }
1934 }
1935
1936 if (ifindex) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001937 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
1938 if (!upper_dev)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001939 return -EINVAL;
Jiri Pirko898e5062013-01-03 22:48:52 +00001940 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001941 if (ops->ndo_add_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001942 err = ops->ndo_add_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001943 if (err)
1944 return err;
1945 } else {
1946 return -EOPNOTSUPP;
1947 }
1948 }
1949 return 0;
1950}
1951
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001952#define DO_SETLINK_MODIFIED 0x01
Nicolas Dichtelba998902014-09-01 16:07:29 +02001953/* notify flag means notify + modified. */
1954#define DO_SETLINK_NOTIFY 0x03
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001955static int do_setlink(const struct sk_buff *skb,
1956 struct net_device *dev, struct ifinfomsg *ifm,
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07001957 struct netlink_ext_ack *extack,
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001958 struct nlattr **tb, char *ifname, int status)
Patrick McHardy0157f602007-06-13 12:03:36 -07001959{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001960 const struct net_device_ops *ops = dev->netdev_ops;
Patrick McHardy0157f602007-06-13 12:03:36 -07001961 int err;
1962
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001963 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
Eric W. Biederman81adee42009-11-08 00:53:51 -08001964 struct net *net = rtnl_link_get_net(dev_net(dev), tb);
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001965 if (IS_ERR(net)) {
1966 err = PTR_ERR(net);
1967 goto errout;
1968 }
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001969 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
Nicolas Dichtele0ebde02014-11-27 10:16:15 +01001970 put_net(net);
Eric W. Biedermanb51642f2012-11-16 03:03:11 +00001971 err = -EPERM;
1972 goto errout;
1973 }
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001974 err = dev_change_net_namespace(dev, net, ifname);
1975 put_net(net);
1976 if (err)
1977 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001978 status |= DO_SETLINK_MODIFIED;
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001979 }
1980
Patrick McHardy0157f602007-06-13 12:03:36 -07001981 if (tb[IFLA_MAP]) {
1982 struct rtnl_link_ifmap *u_map;
1983 struct ifmap k_map;
1984
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001985 if (!ops->ndo_set_config) {
Patrick McHardy0157f602007-06-13 12:03:36 -07001986 err = -EOPNOTSUPP;
1987 goto errout;
1988 }
1989
1990 if (!netif_device_present(dev)) {
1991 err = -ENODEV;
1992 goto errout;
1993 }
1994
1995 u_map = nla_data(tb[IFLA_MAP]);
1996 k_map.mem_start = (unsigned long) u_map->mem_start;
1997 k_map.mem_end = (unsigned long) u_map->mem_end;
1998 k_map.base_addr = (unsigned short) u_map->base_addr;
1999 k_map.irq = (unsigned char) u_map->irq;
2000 k_map.dma = (unsigned char) u_map->dma;
2001 k_map.port = (unsigned char) u_map->port;
2002
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002003 err = ops->ndo_set_config(dev, &k_map);
Patrick McHardy0157f602007-06-13 12:03:36 -07002004 if (err < 0)
2005 goto errout;
2006
Nicolas Dichtelba998902014-09-01 16:07:29 +02002007 status |= DO_SETLINK_NOTIFY;
Patrick McHardy0157f602007-06-13 12:03:36 -07002008 }
2009
2010 if (tb[IFLA_ADDRESS]) {
2011 struct sockaddr *sa;
2012 int len;
2013
WANG Cong153711f2017-07-20 11:27:57 -07002014 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2015 sizeof(*sa));
Patrick McHardy0157f602007-06-13 12:03:36 -07002016 sa = kmalloc(len, GFP_KERNEL);
2017 if (!sa) {
2018 err = -ENOMEM;
2019 goto errout;
2020 }
2021 sa->sa_family = dev->type;
2022 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
2023 dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00002024 err = dev_set_mac_address(dev, sa);
Patrick McHardy0157f602007-06-13 12:03:36 -07002025 kfree(sa);
2026 if (err)
2027 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002028 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002029 }
2030
2031 if (tb[IFLA_MTU]) {
2032 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
2033 if (err < 0)
2034 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002035 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002036 }
2037
Vlad Dogarucbda10f2011-01-13 23:38:30 +00002038 if (tb[IFLA_GROUP]) {
2039 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Nicolas Dichtelba998902014-09-01 16:07:29 +02002040 status |= DO_SETLINK_NOTIFY;
Vlad Dogarucbda10f2011-01-13 23:38:30 +00002041 }
2042
Patrick McHardy0157f602007-06-13 12:03:36 -07002043 /*
2044 * Interface selected by interface index but interface
2045 * name provided implies that a name change has been
2046 * requested.
2047 */
2048 if (ifm->ifi_index > 0 && ifname[0]) {
2049 err = dev_change_name(dev, ifname);
2050 if (err < 0)
2051 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002052 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002053 }
2054
Stephen Hemminger0b815a12008-09-22 21:28:11 -07002055 if (tb[IFLA_IFALIAS]) {
2056 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2057 nla_len(tb[IFLA_IFALIAS]));
2058 if (err < 0)
2059 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002060 status |= DO_SETLINK_NOTIFY;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07002061 }
2062
Patrick McHardy0157f602007-06-13 12:03:36 -07002063 if (tb[IFLA_BROADCAST]) {
2064 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00002065 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Patrick McHardy0157f602007-06-13 12:03:36 -07002066 }
2067
2068 if (ifm->ifi_flags || ifm->ifi_change) {
Patrick McHardy3729d502010-02-26 06:34:54 +00002069 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
Johannes Berg5f9021c2008-11-16 23:20:31 -08002070 if (err < 0)
2071 goto errout;
Patrick McHardy0157f602007-06-13 12:03:36 -07002072 }
2073
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002074 if (tb[IFLA_MASTER]) {
2075 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
2076 if (err)
2077 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002078 status |= DO_SETLINK_MODIFIED;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002079 }
2080
Jiri Pirko9a572472012-12-27 23:49:39 +00002081 if (tb[IFLA_CARRIER]) {
2082 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2083 if (err)
2084 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002085 status |= DO_SETLINK_MODIFIED;
Jiri Pirko9a572472012-12-27 23:49:39 +00002086 }
2087
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002088 if (tb[IFLA_TXQLEN]) {
Alexey Dobriyan0cd29502017-05-17 13:30:44 +03002089 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
2090 unsigned int orig_len = dev->tx_queue_len;
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002091
Jason Wang08294a22016-06-30 14:45:35 +08002092 if (dev->tx_queue_len ^ value) {
2093 dev->tx_queue_len = value;
2094 err = call_netdevice_notifiers(
2095 NETDEV_CHANGE_TX_QUEUE_LEN, dev);
2096 err = notifier_to_errno(err);
2097 if (err) {
2098 dev->tx_queue_len = orig_len;
2099 goto errout;
2100 }
Nicolas Dichtelba998902014-09-01 16:07:29 +02002101 status |= DO_SETLINK_NOTIFY;
Jason Wang08294a22016-06-30 14:45:35 +08002102 }
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002103 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002104
Patrick McHardy0157f602007-06-13 12:03:36 -07002105 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002106 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Patrick McHardy0157f602007-06-13 12:03:36 -07002107
2108 if (tb[IFLA_LINKMODE]) {
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002109 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2110
David S. Miller93b2d4a2008-02-17 18:35:07 -08002111 write_lock_bh(&dev_base_lock);
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002112 if (dev->link_mode ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02002113 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002114 dev->link_mode = value;
David S. Miller93b2d4a2008-02-17 18:35:07 -08002115 write_unlock_bh(&dev_base_lock);
Patrick McHardy0157f602007-06-13 12:03:36 -07002116 }
2117
Chris Wrightc02db8c2010-05-16 01:05:45 -07002118 if (tb[IFLA_VFINFO_LIST]) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002119 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
Chris Wrightc02db8c2010-05-16 01:05:45 -07002120 struct nlattr *attr;
2121 int rem;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002122
Chris Wrightc02db8c2010-05-16 01:05:45 -07002123 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002124 if (nla_type(attr) != IFLA_VF_INFO ||
2125 nla_len(attr) < NLA_HDRLEN) {
David Howells253683b2010-05-21 02:25:27 +00002126 err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002127 goto errout;
David Howells253683b2010-05-21 02:25:27 +00002128 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002129 err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr,
Johannes Bergfceb6432017-04-12 14:34:07 +02002130 ifla_vf_policy, NULL);
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002131 if (err < 0)
2132 goto errout;
2133 err = do_setvfinfo(dev, vfinfo);
Chris Wrightc02db8c2010-05-16 01:05:45 -07002134 if (err < 0)
2135 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002136 status |= DO_SETLINK_NOTIFY;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002137 }
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00002138 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002139 err = 0;
2140
Scott Feldman57b61082010-05-17 22:49:55 -07002141 if (tb[IFLA_VF_PORTS]) {
2142 struct nlattr *port[IFLA_PORT_MAX+1];
2143 struct nlattr *attr;
2144 int vf;
2145 int rem;
2146
2147 err = -EOPNOTSUPP;
2148 if (!ops->ndo_set_vf_port)
2149 goto errout;
2150
2151 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
Daniel Borkmann035d2102015-07-13 00:06:02 +02002152 if (nla_type(attr) != IFLA_VF_PORT ||
2153 nla_len(attr) < NLA_HDRLEN) {
2154 err = -EINVAL;
2155 goto errout;
2156 }
2157 err = nla_parse_nested(port, IFLA_PORT_MAX, attr,
Johannes Bergfceb6432017-04-12 14:34:07 +02002158 ifla_port_policy, NULL);
Scott Feldman57b61082010-05-17 22:49:55 -07002159 if (err < 0)
2160 goto errout;
2161 if (!port[IFLA_PORT_VF]) {
2162 err = -EOPNOTSUPP;
2163 goto errout;
2164 }
2165 vf = nla_get_u32(port[IFLA_PORT_VF]);
2166 err = ops->ndo_set_vf_port(dev, vf, port);
2167 if (err < 0)
2168 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002169 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07002170 }
2171 }
2172 err = 0;
2173
2174 if (tb[IFLA_PORT_SELF]) {
2175 struct nlattr *port[IFLA_PORT_MAX+1];
2176
2177 err = nla_parse_nested(port, IFLA_PORT_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +02002178 tb[IFLA_PORT_SELF], ifla_port_policy,
2179 NULL);
Scott Feldman57b61082010-05-17 22:49:55 -07002180 if (err < 0)
2181 goto errout;
2182
2183 err = -EOPNOTSUPP;
2184 if (ops->ndo_set_vf_port)
2185 err = ops->ndo_set_vf_port(dev, PORT_SELF_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 }
Thomas Graff8ff1822010-11-16 04:30:14 +00002190
2191 if (tb[IFLA_AF_SPEC]) {
2192 struct nlattr *af;
2193 int rem;
2194
2195 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2196 const struct rtnl_af_ops *af_ops;
2197
2198 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002199 BUG();
Thomas Graff8ff1822010-11-16 04:30:14 +00002200
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002201 err = af_ops->set_link_af(dev, af);
Thomas Graff8ff1822010-11-16 04:30:14 +00002202 if (err < 0)
2203 goto errout;
2204
Nicolas Dichtelba998902014-09-01 16:07:29 +02002205 status |= DO_SETLINK_NOTIFY;
Thomas Graff8ff1822010-11-16 04:30:14 +00002206 }
2207 }
Scott Feldman57b61082010-05-17 22:49:55 -07002208 err = 0;
2209
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07002210 if (tb[IFLA_PROTO_DOWN]) {
2211 err = dev_change_proto_down(dev,
2212 nla_get_u8(tb[IFLA_PROTO_DOWN]));
2213 if (err)
2214 goto errout;
2215 status |= DO_SETLINK_NOTIFY;
2216 }
2217
Brenden Blancod1fdd912016-07-19 12:16:49 -07002218 if (tb[IFLA_XDP]) {
2219 struct nlattr *xdp[IFLA_XDP_MAX + 1];
Daniel Borkmann85de8572016-11-28 23:16:54 +01002220 u32 xdp_flags = 0;
Brenden Blancod1fdd912016-07-19 12:16:49 -07002221
2222 err = nla_parse_nested(xdp, IFLA_XDP_MAX, tb[IFLA_XDP],
Johannes Bergfceb6432017-04-12 14:34:07 +02002223 ifla_xdp_policy, NULL);
Brenden Blancod1fdd912016-07-19 12:16:49 -07002224 if (err < 0)
2225 goto errout;
2226
Martin KaFai Lau58038692017-06-15 17:29:09 -07002227 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
Brenden Blanco262d8622016-07-20 17:22:34 -07002228 err = -EINVAL;
2229 goto errout;
2230 }
Daniel Borkmann85de8572016-11-28 23:16:54 +01002231
2232 if (xdp[IFLA_XDP_FLAGS]) {
2233 xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
2234 if (xdp_flags & ~XDP_FLAGS_MASK) {
2235 err = -EINVAL;
2236 goto errout;
2237 }
Jakub Kicinskiee5d0322017-06-21 18:25:04 -07002238 if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
Daniel Borkmann0489df92017-05-12 01:04:45 +02002239 err = -EINVAL;
2240 goto errout;
2241 }
Daniel Borkmann85de8572016-11-28 23:16:54 +01002242 }
2243
Brenden Blancod1fdd912016-07-19 12:16:49 -07002244 if (xdp[IFLA_XDP_FD]) {
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002245 err = dev_change_xdp_fd(dev, extack,
Daniel Borkmann85de8572016-11-28 23:16:54 +01002246 nla_get_s32(xdp[IFLA_XDP_FD]),
2247 xdp_flags);
Brenden Blancod1fdd912016-07-19 12:16:49 -07002248 if (err)
2249 goto errout;
2250 status |= DO_SETLINK_NOTIFY;
2251 }
2252 }
2253
Patrick McHardy0157f602007-06-13 12:03:36 -07002254errout:
Nicolas Dichtelba998902014-09-01 16:07:29 +02002255 if (status & DO_SETLINK_MODIFIED) {
2256 if (status & DO_SETLINK_NOTIFY)
2257 netdev_state_change(dev);
2258
2259 if (err < 0)
2260 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",
2261 dev->name);
2262 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002263
Patrick McHardy0157f602007-06-13 12:03:36 -07002264 return err;
2265}
2266
David Ahernc21ef3e2017-04-16 09:48:24 -07002267static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2268 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002270 struct net *net = sock_net(skb->sk);
Thomas Grafda5e0492006-08-10 21:17:37 -07002271 struct ifinfomsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 struct net_device *dev;
Patrick McHardy0157f602007-06-13 12:03:36 -07002273 int err;
Thomas Grafda5e0492006-08-10 21:17:37 -07002274 struct nlattr *tb[IFLA_MAX+1];
2275 char ifname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
David Ahernc21ef3e2017-04-16 09:48:24 -07002277 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy,
2278 extack);
Thomas Grafda5e0492006-08-10 21:17:37 -07002279 if (err < 0)
2280 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
Thomas Graf5176f912006-08-26 20:13:18 -07002282 if (tb[IFLA_IFNAME])
2283 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
Patrick McHardy78e5b8912006-09-13 20:35:36 -07002284 else
2285 ifname[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 err = -EINVAL;
Thomas Grafda5e0492006-08-10 21:17:37 -07002288 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -07002289 if (ifm->ifi_index > 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00002290 dev = __dev_get_by_index(net, ifm->ifi_index);
Thomas Grafda5e0492006-08-10 21:17:37 -07002291 else if (tb[IFLA_IFNAME])
Eric Dumazeta3d12892009-10-21 10:59:31 +00002292 dev = __dev_get_by_name(net, ifname);
Thomas Grafda5e0492006-08-10 21:17:37 -07002293 else
2294 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
Thomas Grafda5e0492006-08-10 21:17:37 -07002296 if (dev == NULL) {
2297 err = -ENODEV;
2298 goto errout;
2299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
Eric Dumazete0d087a2009-11-07 01:26:17 -08002301 err = validate_linkmsg(dev, tb);
2302 if (err < 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00002303 goto errout;
Thomas Grafda5e0492006-08-10 21:17:37 -07002304
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002305 err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
Thomas Grafda5e0492006-08-10 21:17:37 -07002306errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 return err;
2308}
2309
WANG Cong66400d52015-03-24 11:53:31 -07002310static int rtnl_group_dellink(const struct net *net, int group)
2311{
2312 struct net_device *dev, *aux;
2313 LIST_HEAD(list_kill);
2314 bool found = false;
2315
2316 if (!group)
2317 return -EPERM;
2318
2319 for_each_netdev(net, dev) {
2320 if (dev->group == group) {
2321 const struct rtnl_link_ops *ops;
2322
2323 found = true;
2324 ops = dev->rtnl_link_ops;
2325 if (!ops || !ops->dellink)
2326 return -EOPNOTSUPP;
2327 }
2328 }
2329
2330 if (!found)
2331 return -ENODEV;
2332
2333 for_each_netdev_safe(net, dev, aux) {
2334 if (dev->group == group) {
2335 const struct rtnl_link_ops *ops;
2336
2337 ops = dev->rtnl_link_ops;
2338 ops->dellink(dev, &list_kill);
2339 }
2340 }
2341 unregister_netdevice_many(&list_kill);
2342
2343 return 0;
2344}
2345
Thomas Graf614732e2015-07-21 10:44:06 +02002346int rtnl_delete_link(struct net_device *dev)
2347{
2348 const struct rtnl_link_ops *ops;
2349 LIST_HEAD(list_kill);
2350
2351 ops = dev->rtnl_link_ops;
2352 if (!ops || !ops->dellink)
2353 return -EOPNOTSUPP;
2354
2355 ops->dellink(dev, &list_kill);
2356 unregister_netdevice_many(&list_kill);
2357
2358 return 0;
2359}
2360EXPORT_SYMBOL_GPL(rtnl_delete_link);
2361
David Ahernc21ef3e2017-04-16 09:48:24 -07002362static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
2363 struct netlink_ext_ack *extack)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002364{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002365 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002366 struct net_device *dev;
2367 struct ifinfomsg *ifm;
2368 char ifname[IFNAMSIZ];
2369 struct nlattr *tb[IFLA_MAX+1];
2370 int err;
2371
David Ahernc21ef3e2017-04-16 09:48:24 -07002372 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002373 if (err < 0)
2374 return err;
2375
2376 if (tb[IFLA_IFNAME])
2377 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2378
2379 ifm = nlmsg_data(nlh);
2380 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07002381 dev = __dev_get_by_index(net, ifm->ifi_index);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002382 else if (tb[IFLA_IFNAME])
Eric W. Biederman881d9662007-09-17 11:56:21 -07002383 dev = __dev_get_by_name(net, ifname);
WANG Cong66400d52015-03-24 11:53:31 -07002384 else if (tb[IFLA_GROUP])
2385 return rtnl_group_dellink(net, nla_get_u32(tb[IFLA_GROUP]));
Patrick McHardy38f7b872007-06-13 12:03:51 -07002386 else
2387 return -EINVAL;
2388
2389 if (!dev)
2390 return -ENODEV;
2391
Thomas Graf614732e2015-07-21 10:44:06 +02002392 return rtnl_delete_link(dev);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002393}
2394
Patrick McHardy3729d502010-02-26 06:34:54 +00002395int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
2396{
2397 unsigned int old_flags;
2398 int err;
2399
2400 old_flags = dev->flags;
2401 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
2402 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
2403 if (err < 0)
2404 return err;
2405 }
2406
2407 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
Patrick McHardy3729d502010-02-26 06:34:54 +00002408
Nicolas Dichtela528c212013-09-25 12:02:44 +02002409 __dev_notify_flags(dev, old_flags, ~0U);
Patrick McHardy3729d502010-02-26 06:34:54 +00002410 return 0;
2411}
2412EXPORT_SYMBOL(rtnl_configure_link);
2413
Rami Rosenc0713562012-11-30 01:08:47 +00002414struct net_device *rtnl_create_link(struct net *net,
Thomas Graf78ebb0d2015-04-10 01:45:53 +02002415 const char *ifname, unsigned char name_assign_type,
Tom Gundersen55177502014-07-14 16:37:25 +02002416 const struct rtnl_link_ops *ops, struct nlattr *tb[])
Pavel Emelianove7199282007-08-08 22:16:38 -07002417{
Pavel Emelianove7199282007-08-08 22:16:38 -07002418 struct net_device *dev;
Jiri Pirkod40156a2012-07-20 02:28:47 +00002419 unsigned int num_tx_queues = 1;
2420 unsigned int num_rx_queues = 1;
Pavel Emelianove7199282007-08-08 22:16:38 -07002421
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002422 if (tb[IFLA_NUM_TX_QUEUES])
2423 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
2424 else if (ops->get_num_tx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002425 num_tx_queues = ops->get_num_tx_queues();
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002426
2427 if (tb[IFLA_NUM_RX_QUEUES])
2428 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
2429 else if (ops->get_num_rx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002430 num_rx_queues = ops->get_num_rx_queues();
stephen hemmingerefacb302012-04-10 18:34:43 +00002431
Tom Gundersen55177502014-07-14 16:37:25 +02002432 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
Tom Gundersenc835a672014-07-14 16:37:24 +02002433 ops->setup, num_tx_queues, num_rx_queues);
Pavel Emelianove7199282007-08-08 22:16:38 -07002434 if (!dev)
Tobias Klauserd1892e42017-02-20 16:32:06 +01002435 return ERR_PTR(-ENOMEM);
Pavel Emelianove7199282007-08-08 22:16:38 -07002436
Eric W. Biederman81adee42009-11-08 00:53:51 -08002437 dev_net_set(dev, net);
2438 dev->rtnl_link_ops = ops;
Patrick McHardy3729d502010-02-26 06:34:54 +00002439 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
Eric W. Biederman81adee42009-11-08 00:53:51 -08002440
Pavel Emelianove7199282007-08-08 22:16:38 -07002441 if (tb[IFLA_MTU])
2442 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002443 if (tb[IFLA_ADDRESS]) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002444 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
2445 nla_len(tb[IFLA_ADDRESS]));
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002446 dev->addr_assign_type = NET_ADDR_SET;
2447 }
Pavel Emelianove7199282007-08-08 22:16:38 -07002448 if (tb[IFLA_BROADCAST])
2449 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
2450 nla_len(tb[IFLA_BROADCAST]));
2451 if (tb[IFLA_TXQLEN])
2452 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
2453 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002454 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Pavel Emelianove7199282007-08-08 22:16:38 -07002455 if (tb[IFLA_LINKMODE])
2456 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
Patrick McHardyffa934f2011-01-20 03:00:42 +00002457 if (tb[IFLA_GROUP])
2458 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Pavel Emelianove7199282007-08-08 22:16:38 -07002459
2460 return dev;
Pavel Emelianove7199282007-08-08 22:16:38 -07002461}
Eric Dumazete0d087a2009-11-07 01:26:17 -08002462EXPORT_SYMBOL(rtnl_create_link);
Pavel Emelianove7199282007-08-08 22:16:38 -07002463
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002464static int rtnl_group_changelink(const struct sk_buff *skb,
2465 struct net *net, int group,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002466 struct ifinfomsg *ifm,
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002467 struct netlink_ext_ack *extack,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002468 struct nlattr **tb)
2469{
WANG Congd0795352015-03-23 16:31:09 -07002470 struct net_device *dev, *aux;
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002471 int err;
2472
WANG Congd0795352015-03-23 16:31:09 -07002473 for_each_netdev_safe(net, dev, aux) {
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002474 if (dev->group == group) {
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002475 err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002476 if (err < 0)
2477 return err;
2478 }
2479 }
2480
2481 return 0;
2482}
2483
David Ahernc21ef3e2017-04-16 09:48:24 -07002484static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2485 struct netlink_ext_ack *extack)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002486{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002487 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002488 const struct rtnl_link_ops *ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002489 const struct rtnl_link_ops *m_ops = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002490 struct net_device *dev;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002491 struct net_device *master_dev = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002492 struct ifinfomsg *ifm;
2493 char kind[MODULE_NAME_LEN];
2494 char ifname[IFNAMSIZ];
2495 struct nlattr *tb[IFLA_MAX+1];
2496 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
Tom Gundersen55177502014-07-14 16:37:25 +02002497 unsigned char name_assign_type = NET_NAME_USER;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002498 int err;
2499
Johannes Berg95a5afc2008-10-16 15:24:51 -07002500#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002501replay:
Thomas Graf8072f082007-07-31 14:13:50 -07002502#endif
David Ahernc21ef3e2017-04-16 09:48:24 -07002503 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002504 if (err < 0)
2505 return err;
2506
2507 if (tb[IFLA_IFNAME])
2508 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2509 else
2510 ifname[0] = '\0';
2511
2512 ifm = nlmsg_data(nlh);
2513 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07002514 dev = __dev_get_by_index(net, ifm->ifi_index);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002515 else {
2516 if (ifname[0])
2517 dev = __dev_get_by_name(net, ifname);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002518 else
2519 dev = NULL;
2520 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002521
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002522 if (dev) {
2523 master_dev = netdev_master_upper_dev_get(dev);
2524 if (master_dev)
2525 m_ops = master_dev->rtnl_link_ops;
2526 }
2527
Eric Dumazete0d087a2009-11-07 01:26:17 -08002528 err = validate_linkmsg(dev, tb);
2529 if (err < 0)
Thomas Graf1840bb12008-02-23 19:54:36 -08002530 return err;
2531
Patrick McHardy38f7b872007-06-13 12:03:51 -07002532 if (tb[IFLA_LINKINFO]) {
2533 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +02002534 tb[IFLA_LINKINFO], ifla_info_policy,
2535 NULL);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002536 if (err < 0)
2537 return err;
2538 } else
2539 memset(linkinfo, 0, sizeof(linkinfo));
2540
2541 if (linkinfo[IFLA_INFO_KIND]) {
2542 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
2543 ops = rtnl_link_ops_get(kind);
2544 } else {
2545 kind[0] = '\0';
2546 ops = NULL;
2547 }
2548
2549 if (1) {
Sasha Levin4e10fd52015-02-24 14:14:35 -05002550 struct nlattr *attr[ops ? ops->maxtype + 1 : 1];
2551 struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1];
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002552 struct nlattr **data = NULL;
2553 struct nlattr **slave_data = NULL;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002554 struct net *dest_net, *link_net = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002555
2556 if (ops) {
2557 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
2558 err = nla_parse_nested(attr, ops->maxtype,
2559 linkinfo[IFLA_INFO_DATA],
Johannes Bergfceb6432017-04-12 14:34:07 +02002560 ops->policy, NULL);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002561 if (err < 0)
2562 return err;
2563 data = attr;
2564 }
2565 if (ops->validate) {
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02002566 err = ops->validate(tb, data, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002567 if (err < 0)
2568 return err;
2569 }
2570 }
2571
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002572 if (m_ops) {
2573 if (m_ops->slave_maxtype &&
2574 linkinfo[IFLA_INFO_SLAVE_DATA]) {
2575 err = nla_parse_nested(slave_attr,
2576 m_ops->slave_maxtype,
2577 linkinfo[IFLA_INFO_SLAVE_DATA],
Johannes Bergfceb6432017-04-12 14:34:07 +02002578 m_ops->slave_policy,
2579 NULL);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002580 if (err < 0)
2581 return err;
2582 slave_data = slave_attr;
2583 }
2584 if (m_ops->slave_validate) {
Matthias Schifferd116ffc2017-06-25 23:56:03 +02002585 err = m_ops->slave_validate(tb, slave_data,
2586 extack);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002587 if (err < 0)
2588 return err;
2589 }
2590 }
2591
Patrick McHardy38f7b872007-06-13 12:03:51 -07002592 if (dev) {
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002593 int status = 0;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002594
2595 if (nlh->nlmsg_flags & NLM_F_EXCL)
2596 return -EEXIST;
2597 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2598 return -EOPNOTSUPP;
2599
2600 if (linkinfo[IFLA_INFO_DATA]) {
2601 if (!ops || ops != dev->rtnl_link_ops ||
2602 !ops->changelink)
2603 return -EOPNOTSUPP;
2604
Matthias Schifferad744b22017-06-25 23:56:00 +02002605 err = ops->changelink(dev, tb, data, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002606 if (err < 0)
2607 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002608 status |= DO_SETLINK_NOTIFY;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002609 }
2610
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002611 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2612 if (!m_ops || !m_ops->slave_changelink)
2613 return -EOPNOTSUPP;
2614
2615 err = m_ops->slave_changelink(master_dev, dev,
Matthias Schiffer17dd0ec2017-06-25 23:56:02 +02002616 tb, slave_data,
2617 extack);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002618 if (err < 0)
2619 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002620 status |= DO_SETLINK_NOTIFY;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002621 }
2622
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002623 return do_setlink(skb, dev, ifm, extack, tb, ifname,
2624 status);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002625 }
2626
Patrick McHardyffa934f2011-01-20 03:00:42 +00002627 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2628 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002629 return rtnl_group_changelink(skb, net,
Patrick McHardyffa934f2011-01-20 03:00:42 +00002630 nla_get_u32(tb[IFLA_GROUP]),
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002631 ifm, extack, tb);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002632 return -ENODEV;
Patrick McHardyffa934f2011-01-20 03:00:42 +00002633 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002634
Theuns Verwoerd160ca012017-01-31 12:23:46 +13002635 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
Patrick McHardy38f7b872007-06-13 12:03:51 -07002636 return -EOPNOTSUPP;
2637
2638 if (!ops) {
Johannes Berg95a5afc2008-10-16 15:24:51 -07002639#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002640 if (kind[0]) {
2641 __rtnl_unlock();
2642 request_module("rtnl-link-%s", kind);
2643 rtnl_lock();
2644 ops = rtnl_link_ops_get(kind);
2645 if (ops)
2646 goto replay;
2647 }
2648#endif
2649 return -EOPNOTSUPP;
2650 }
2651
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02002652 if (!ops->setup)
2653 return -EOPNOTSUPP;
2654
Tom Gundersen55177502014-07-14 16:37:25 +02002655 if (!ifname[0]) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07002656 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
Tom Gundersen55177502014-07-14 16:37:25 +02002657 name_assign_type = NET_NAME_ENUM;
2658 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002659
Eric W. Biederman81adee42009-11-08 00:53:51 -08002660 dest_net = rtnl_link_get_net(net, tb);
Eric W. Biederman13ad1772011-01-29 14:57:22 +00002661 if (IS_ERR(dest_net))
2662 return PTR_ERR(dest_net);
2663
Eric W. Biederman505ce412015-02-26 16:19:00 -06002664 err = -EPERM;
2665 if (!netlink_ns_capable(skb, dest_net->user_ns, CAP_NET_ADMIN))
2666 goto out;
2667
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002668 if (tb[IFLA_LINK_NETNSID]) {
2669 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
2670
2671 link_net = get_net_ns_by_id(dest_net, id);
2672 if (!link_net) {
2673 err = -EINVAL;
2674 goto out;
2675 }
Eric W. Biederman06615be2015-02-26 16:20:07 -06002676 err = -EPERM;
2677 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
2678 goto out;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002679 }
2680
2681 dev = rtnl_create_link(link_net ? : dest_net, ifname,
2682 name_assign_type, ops, tb);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002683 if (IS_ERR(dev)) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002684 err = PTR_ERR(dev);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002685 goto out;
2686 }
2687
2688 dev->ifindex = ifm->ifi_index;
2689
Cong Wang0e0eee22014-02-11 15:51:30 -08002690 if (ops->newlink) {
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02002691 err = ops->newlink(link_net ? : net, dev, tb, data,
2692 extack);
Cong Wang0e0eee22014-02-11 15:51:30 -08002693 /* Drivers should call free_netdev() in ->destructor
Cong Wange51fb152014-06-03 16:40:47 -07002694 * and unregister it on failure after registration
2695 * so that device could be finally freed in rtnl_unlock.
Cong Wang0e0eee22014-02-11 15:51:30 -08002696 */
Cong Wange51fb152014-06-03 16:40:47 -07002697 if (err < 0) {
2698 /* If device is not registered at all, free it now */
2699 if (dev->reg_state == NETREG_UNINITIALIZED)
2700 free_netdev(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002701 goto out;
Cong Wange51fb152014-06-03 16:40:47 -07002702 }
Cong Wang0e0eee22014-02-11 15:51:30 -08002703 } else {
Patrick McHardy2d85cba2007-07-11 19:42:13 -07002704 err = register_netdevice(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002705 if (err < 0) {
2706 free_netdev(dev);
2707 goto out;
2708 }
Dan Carpenterfce9b9b2013-08-14 12:35:42 +03002709 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002710 err = rtnl_configure_link(dev, ifm);
David S. Miller43638902015-03-10 21:58:32 -04002711 if (err < 0)
2712 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002713 if (link_net) {
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002714 err = dev_change_net_namespace(dev, dest_net, ifname);
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002715 if (err < 0)
David S. Miller43638902015-03-10 21:58:32 -04002716 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002717 }
Theuns Verwoerd160ca012017-01-31 12:23:46 +13002718 if (tb[IFLA_MASTER]) {
2719 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
2720 if (err)
2721 goto out_unregister;
2722 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002723out:
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002724 if (link_net)
2725 put_net(link_net);
Eric W. Biederman81adee42009-11-08 00:53:51 -08002726 put_net(dest_net);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002727 return err;
David S. Miller43638902015-03-10 21:58:32 -04002728out_unregister:
2729 if (ops->newlink) {
2730 LIST_HEAD(list_kill);
2731
2732 ops->dellink(dev, &list_kill);
2733 unregister_netdevice_many(&list_kill);
2734 } else {
2735 unregister_netdevice(dev);
2736 }
2737 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002738 }
2739}
2740
David Ahernc21ef3e2017-04-16 09:48:24 -07002741static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2742 struct netlink_ext_ack *extack)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002743{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002744 struct net *net = sock_net(skb->sk);
Thomas Grafb60c5112006-08-04 23:05:34 -07002745 struct ifinfomsg *ifm;
Eric Dumazeta3d12892009-10-21 10:59:31 +00002746 char ifname[IFNAMSIZ];
Thomas Grafb60c5112006-08-04 23:05:34 -07002747 struct nlattr *tb[IFLA_MAX+1];
2748 struct net_device *dev = NULL;
2749 struct sk_buff *nskb;
Thomas Graf339bf982006-11-10 14:10:15 -08002750 int err;
Greg Rose115c9b82012-02-21 16:54:48 -05002751 u32 ext_filter_mask = 0;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002752
David Ahernc21ef3e2017-04-16 09:48:24 -07002753 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Thomas Grafb60c5112006-08-04 23:05:34 -07002754 if (err < 0)
Eric Sesterhenn9918f232006-09-26 23:26:38 -07002755 return err;
Thomas Grafb60c5112006-08-04 23:05:34 -07002756
Eric Dumazeta3d12892009-10-21 10:59:31 +00002757 if (tb[IFLA_IFNAME])
2758 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2759
Greg Rose115c9b82012-02-21 16:54:48 -05002760 if (tb[IFLA_EXT_MASK])
2761 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2762
Thomas Grafb60c5112006-08-04 23:05:34 -07002763 ifm = nlmsg_data(nlh);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002764 if (ifm->ifi_index > 0)
2765 dev = __dev_get_by_index(net, ifm->ifi_index);
2766 else if (tb[IFLA_IFNAME])
2767 dev = __dev_get_by_name(net, ifname);
2768 else
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002769 return -EINVAL;
Thomas Grafb60c5112006-08-04 23:05:34 -07002770
Eric Dumazeta3d12892009-10-21 10:59:31 +00002771 if (dev == NULL)
2772 return -ENODEV;
2773
Greg Rose115c9b82012-02-21 16:54:48 -05002774 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002775 if (nskb == NULL)
2776 return -ENOBUFS;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002777
Eric W. Biederman15e47302012-09-07 20:12:54 +00002778 err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04002779 nlh->nlmsg_seq, 0, 0, ext_filter_mask, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08002780 if (err < 0) {
2781 /* -EMSGSIZE implies BUG in if_nlmsg_size */
2782 WARN_ON(err == -EMSGSIZE);
2783 kfree_skb(nskb);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002784 } else
Eric W. Biederman15e47302012-09-07 20:12:54 +00002785 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
Thomas Grafb60c5112006-08-04 23:05:34 -07002786
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002787 return err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002788}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002789
Greg Rose115c9b82012-02-21 16:54:48 -05002790static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
Greg Rosec7ac8672011-06-10 01:27:09 +00002791{
Greg Rose115c9b82012-02-21 16:54:48 -05002792 struct net *net = sock_net(skb->sk);
2793 struct net_device *dev;
2794 struct nlattr *tb[IFLA_MAX+1];
2795 u32 ext_filter_mask = 0;
2796 u16 min_ifinfo_dump_size = 0;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02002797 int hdrlen;
Greg Rose115c9b82012-02-21 16:54:48 -05002798
Michal Schmidte5eca6d2014-05-28 14:15:19 +02002799 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2800 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2801 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2802
Johannes Bergfceb6432017-04-12 14:34:07 +02002803 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00002804 if (tb[IFLA_EXT_MASK])
2805 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2806 }
Greg Rose115c9b82012-02-21 16:54:48 -05002807
2808 if (!ext_filter_mask)
2809 return NLMSG_GOODSIZE;
2810 /*
2811 * traverse the list of net devices and compute the minimum
2812 * buffer size based upon the filter mask.
2813 */
2814 list_for_each_entry(dev, &net->dev_base_head, dev_list) {
2815 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
2816 if_nlmsg_size(dev,
2817 ext_filter_mask));
2818 }
2819
Zhang Shengju93af2052016-11-22 14:14:28 +08002820 return nlmsg_total_size(min_ifinfo_dump_size);
Greg Rosec7ac8672011-06-10 01:27:09 +00002821}
2822
Adrian Bunk42bad1d2007-04-26 00:57:41 -07002823static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824{
2825 int idx;
2826 int s_idx = cb->family;
2827
2828 if (s_idx == 0)
2829 s_idx = 1;
Patrick McHardy25239ce2010-04-26 16:02:05 +02002830 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 int type = cb->nlh->nlmsg_type-RTM_BASE;
2832 if (idx < s_idx || idx == PF_PACKET)
2833 continue;
Thomas Grafe2849862007-03-22 11:48:11 -07002834 if (rtnl_msg_handlers[idx] == NULL ||
2835 rtnl_msg_handlers[idx][type].dumpit == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 continue;
Nicolas Dichtel04652772013-03-22 06:28:42 +00002837 if (idx > s_idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 memset(&cb->args[0], 0, sizeof(cb->args));
Nicolas Dichtel04652772013-03-22 06:28:42 +00002839 cb->prev_seq = 0;
2840 cb->seq = 0;
2841 }
Thomas Grafe2849862007-03-22 11:48:11 -07002842 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 break;
2844 }
2845 cb->family = idx;
2846
2847 return skb->len;
2848}
2849
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002850struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04002851 unsigned int change,
2852 u32 event, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002854 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 struct sk_buff *skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002856 int err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00002857 size_t if_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858
Alexei Starovoitov7f294052013-10-23 16:02:42 -07002859 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002860 if (skb == NULL)
2861 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04002863 err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0, event);
Patrick McHardy26932562007-01-31 23:16:40 -08002864 if (err < 0) {
2865 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
2866 WARN_ON(err == -EMSGSIZE);
2867 kfree_skb(skb);
2868 goto errout;
2869 }
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002870 return skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002871errout:
2872 if (err < 0)
Eric W. Biederman4b3da702007-11-19 22:27:40 -08002873 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002874 return NULL;
2875}
2876
2877void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
2878{
2879 struct net *net = dev_net(dev);
2880
2881 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
2882}
2883
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04002884static void rtmsg_ifinfo_event(int type, struct net_device *dev,
2885 unsigned int change, u32 event,
2886 gfp_t flags)
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002887{
2888 struct sk_buff *skb;
2889
Nicolas Dichteled2a80a2015-05-13 14:19:42 +02002890 if (dev->reg_state != NETREG_REGISTERED)
2891 return;
2892
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04002893 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002894 if (skb)
2895 rtmsg_ifinfo_send(skb, dev, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896}
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04002897
2898void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
2899 gfp_t flags)
2900{
Vlad Yasevich8c6c9182017-05-30 10:07:02 -04002901 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags);
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04002902}
Jiri Pirko471cb5a2013-01-03 22:49:01 +00002903EXPORT_SYMBOL(rtmsg_ifinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904
John Fastabendd83b0602012-04-15 06:44:08 +00002905static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
2906 struct net_device *dev,
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002907 u8 *addr, u16 vid, u32 pid, u32 seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002908 int type, unsigned int flags,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002909 int nlflags, u16 ndm_state)
John Fastabendd83b0602012-04-15 06:44:08 +00002910{
2911 struct nlmsghdr *nlh;
2912 struct ndmsg *ndm;
2913
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002914 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
John Fastabendd83b0602012-04-15 06:44:08 +00002915 if (!nlh)
2916 return -EMSGSIZE;
2917
2918 ndm = nlmsg_data(nlh);
2919 ndm->ndm_family = AF_BRIDGE;
2920 ndm->ndm_pad1 = 0;
2921 ndm->ndm_pad2 = 0;
2922 ndm->ndm_flags = flags;
2923 ndm->ndm_type = 0;
2924 ndm->ndm_ifindex = dev->ifindex;
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002925 ndm->ndm_state = ndm_state;
John Fastabendd83b0602012-04-15 06:44:08 +00002926
2927 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
2928 goto nla_put_failure;
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002929 if (vid)
2930 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
2931 goto nla_put_failure;
John Fastabendd83b0602012-04-15 06:44:08 +00002932
Johannes Berg053c0952015-01-16 22:09:00 +01002933 nlmsg_end(skb, nlh);
2934 return 0;
John Fastabendd83b0602012-04-15 06:44:08 +00002935
2936nla_put_failure:
2937 nlmsg_cancel(skb, nlh);
2938 return -EMSGSIZE;
2939}
2940
John Fastabend3ff661c2012-04-15 06:44:14 +00002941static inline size_t rtnl_fdb_nlmsg_size(void)
2942{
Sabrina Dubrocaf82ef3e2016-11-18 15:50:39 +01002943 return NLMSG_ALIGN(sizeof(struct ndmsg)) +
2944 nla_total_size(ETH_ALEN) + /* NDA_LLADDR */
2945 nla_total_size(sizeof(u16)) + /* NDA_VLAN */
2946 0;
John Fastabend3ff661c2012-04-15 06:44:14 +00002947}
2948
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002949static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
2950 u16 ndm_state)
John Fastabend3ff661c2012-04-15 06:44:14 +00002951{
2952 struct net *net = dev_net(dev);
2953 struct sk_buff *skb;
2954 int err = -ENOBUFS;
2955
2956 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
2957 if (!skb)
2958 goto errout;
2959
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002960 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002961 0, 0, type, NTF_SELF, 0, ndm_state);
John Fastabend3ff661c2012-04-15 06:44:14 +00002962 if (err < 0) {
2963 kfree_skb(skb);
2964 goto errout;
2965 }
2966
2967 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
2968 return;
2969errout:
2970 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
2971}
2972
Vlad Yasevich090096b2013-03-06 15:39:42 +00002973/**
2974 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2975 */
2976int ndo_dflt_fdb_add(struct ndmsg *ndm,
2977 struct nlattr *tb[],
2978 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01002979 const unsigned char *addr, u16 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00002980 u16 flags)
2981{
2982 int err = -EINVAL;
2983
2984 /* If aging addresses are supported device will need to
2985 * implement its own handler for this.
2986 */
2987 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2988 pr_info("%s: FDB only supports static addresses\n", dev->name);
2989 return err;
2990 }
2991
Or Gerlitz65891fe2014-12-14 18:19:05 +02002992 if (vid) {
2993 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
2994 return err;
2995 }
2996
Vlad Yasevich090096b2013-03-06 15:39:42 +00002997 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2998 err = dev_uc_add_excl(dev, addr);
2999 else if (is_multicast_ether_addr(addr))
3000 err = dev_mc_add_excl(dev, addr);
3001
3002 /* Only return duplicate errors if NLM_F_EXCL is set */
3003 if (err == -EEXIST && !(flags & NLM_F_EXCL))
3004 err = 0;
3005
3006 return err;
3007}
3008EXPORT_SYMBOL(ndo_dflt_fdb_add);
3009
Jiri Pirkof6f64242014-11-28 14:34:15 +01003010static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid)
3011{
3012 u16 vid = 0;
3013
3014 if (vlan_attr) {
3015 if (nla_len(vlan_attr) != sizeof(u16)) {
3016 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n");
3017 return -EINVAL;
3018 }
3019
3020 vid = nla_get_u16(vlan_attr);
3021
3022 if (!vid || vid >= VLAN_VID_MASK) {
3023 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n",
3024 vid);
3025 return -EINVAL;
3026 }
3027 }
3028 *p_vid = vid;
3029 return 0;
3030}
3031
David Ahernc21ef3e2017-04-16 09:48:24 -07003032static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
3033 struct netlink_ext_ack *extack)
John Fastabend77162022012-04-15 06:43:56 +00003034{
3035 struct net *net = sock_net(skb->sk);
John Fastabend77162022012-04-15 06:43:56 +00003036 struct ndmsg *ndm;
3037 struct nlattr *tb[NDA_MAX+1];
3038 struct net_device *dev;
3039 u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01003040 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00003041 int err;
3042
David Ahernc21ef3e2017-04-16 09:48:24 -07003043 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
John Fastabend77162022012-04-15 06:43:56 +00003044 if (err < 0)
3045 return err;
3046
3047 ndm = nlmsg_data(nlh);
3048 if (ndm->ndm_ifindex == 0) {
3049 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
3050 return -EINVAL;
3051 }
3052
3053 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3054 if (dev == NULL) {
3055 pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
3056 return -ENODEV;
3057 }
3058
3059 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3060 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
3061 return -EINVAL;
3062 }
3063
3064 addr = nla_data(tb[NDA_LLADDR]);
John Fastabend77162022012-04-15 06:43:56 +00003065
Jiri Pirkof6f64242014-11-28 14:34:15 +01003066 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
3067 if (err)
3068 return err;
3069
John Fastabend77162022012-04-15 06:43:56 +00003070 err = -EOPNOTSUPP;
3071
3072 /* Support fdb on master device the net/bridge default case */
3073 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3074 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003075 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3076 const struct net_device_ops *ops = br_dev->netdev_ops;
3077
Jiri Pirkof6f64242014-11-28 14:34:15 +01003078 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
3079 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00003080 if (err)
3081 goto out;
3082 else
3083 ndm->ndm_flags &= ~NTF_MASTER;
3084 }
3085
3086 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00003087 if ((ndm->ndm_flags & NTF_SELF)) {
3088 if (dev->netdev_ops->ndo_fdb_add)
3089 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003090 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003091 nlh->nlmsg_flags);
3092 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01003093 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003094 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00003095
John Fastabend3ff661c2012-04-15 06:44:14 +00003096 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003097 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
3098 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00003099 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00003100 }
John Fastabend77162022012-04-15 06:43:56 +00003101 }
3102out:
3103 return err;
3104}
3105
Vlad Yasevich090096b2013-03-06 15:39:42 +00003106/**
3107 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3108 */
3109int ndo_dflt_fdb_del(struct ndmsg *ndm,
3110 struct nlattr *tb[],
3111 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003112 const unsigned char *addr, u16 vid)
Vlad Yasevich090096b2013-03-06 15:39:42 +00003113{
Alexander Duyckc8a89c42014-07-15 15:15:20 -07003114 int err = -EINVAL;
Vlad Yasevich090096b2013-03-06 15:39:42 +00003115
3116 /* If aging addresses are supported device will need to
3117 * implement its own handler for this.
3118 */
Sridhar Samudrala64535992013-08-08 15:19:48 -07003119 if (!(ndm->ndm_state & NUD_PERMANENT)) {
Vlad Yasevich090096b2013-03-06 15:39:42 +00003120 pr_info("%s: FDB only supports static addresses\n", dev->name);
Alexander Duyckc8a89c42014-07-15 15:15:20 -07003121 return err;
Vlad Yasevich090096b2013-03-06 15:39:42 +00003122 }
3123
3124 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3125 err = dev_uc_del(dev, addr);
3126 else if (is_multicast_ether_addr(addr))
3127 err = dev_mc_del(dev, addr);
Vlad Yasevich090096b2013-03-06 15:39:42 +00003128
3129 return err;
3130}
3131EXPORT_SYMBOL(ndo_dflt_fdb_del);
3132
David Ahernc21ef3e2017-04-16 09:48:24 -07003133static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
3134 struct netlink_ext_ack *extack)
John Fastabend77162022012-04-15 06:43:56 +00003135{
3136 struct net *net = sock_net(skb->sk);
3137 struct ndmsg *ndm;
Vlad Yasevich1690be62013-02-13 12:00:18 +00003138 struct nlattr *tb[NDA_MAX+1];
John Fastabend77162022012-04-15 06:43:56 +00003139 struct net_device *dev;
3140 int err = -EINVAL;
3141 __u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01003142 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00003143
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07003144 if (!netlink_capable(skb, CAP_NET_ADMIN))
Vlad Yasevich1690be62013-02-13 12:00:18 +00003145 return -EPERM;
3146
David Ahernc21ef3e2017-04-16 09:48:24 -07003147 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
Vlad Yasevich1690be62013-02-13 12:00:18 +00003148 if (err < 0)
3149 return err;
John Fastabend77162022012-04-15 06:43:56 +00003150
3151 ndm = nlmsg_data(nlh);
3152 if (ndm->ndm_ifindex == 0) {
3153 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
3154 return -EINVAL;
3155 }
3156
3157 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3158 if (dev == NULL) {
3159 pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
3160 return -ENODEV;
3161 }
3162
Vlad Yasevich1690be62013-02-13 12:00:18 +00003163 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
3164 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
John Fastabend77162022012-04-15 06:43:56 +00003165 return -EINVAL;
3166 }
3167
Vlad Yasevich1690be62013-02-13 12:00:18 +00003168 addr = nla_data(tb[NDA_LLADDR]);
Vlad Yasevich1690be62013-02-13 12:00:18 +00003169
Jiri Pirkof6f64242014-11-28 14:34:15 +01003170 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
3171 if (err)
3172 return err;
3173
John Fastabend77162022012-04-15 06:43:56 +00003174 err = -EOPNOTSUPP;
3175
3176 /* Support fdb on master device the net/bridge default case */
3177 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3178 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003179 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3180 const struct net_device_ops *ops = br_dev->netdev_ops;
John Fastabend77162022012-04-15 06:43:56 +00003181
Jiri Pirko898e5062013-01-03 22:48:52 +00003182 if (ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003183 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00003184
3185 if (err)
3186 goto out;
3187 else
3188 ndm->ndm_flags &= ~NTF_MASTER;
3189 }
3190
3191 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00003192 if (ndm->ndm_flags & NTF_SELF) {
3193 if (dev->netdev_ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003194 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
3195 vid);
Vlad Yasevich090096b2013-03-06 15:39:42 +00003196 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01003197 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00003198
John Fastabend3ff661c2012-04-15 06:44:14 +00003199 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003200 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
3201 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00003202 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00003203 }
John Fastabend77162022012-04-15 06:43:56 +00003204 }
3205out:
3206 return err;
3207}
3208
John Fastabendd83b0602012-04-15 06:44:08 +00003209static int nlmsg_populate_fdb(struct sk_buff *skb,
3210 struct netlink_callback *cb,
3211 struct net_device *dev,
3212 int *idx,
3213 struct netdev_hw_addr_list *list)
3214{
3215 struct netdev_hw_addr *ha;
3216 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00003217 u32 portid, seq;
John Fastabendd83b0602012-04-15 06:44:08 +00003218
Eric W. Biederman15e47302012-09-07 20:12:54 +00003219 portid = NETLINK_CB(cb->skb).portid;
John Fastabendd83b0602012-04-15 06:44:08 +00003220 seq = cb->nlh->nlmsg_seq;
3221
3222 list_for_each_entry(ha, &list->list, list) {
Roopa Prabhud2976532016-08-30 21:56:45 -07003223 if (*idx < cb->args[2])
John Fastabendd83b0602012-04-15 06:44:08 +00003224 goto skip;
3225
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003226 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
John Fastabenda7a558f2012-11-01 16:23:10 +00003227 portid, seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003228 RTM_NEWNEIGH, NTF_SELF,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003229 NLM_F_MULTI, NUD_PERMANENT);
John Fastabendd83b0602012-04-15 06:44:08 +00003230 if (err < 0)
3231 return err;
3232skip:
3233 *idx += 1;
3234 }
3235 return 0;
3236}
3237
3238/**
Ben Hutchings2c530402012-07-10 10:55:09 +00003239 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
John Fastabendd83b0602012-04-15 06:44:08 +00003240 * @nlh: netlink message header
3241 * @dev: netdevice
3242 *
3243 * Default netdevice operation to dump the existing unicast address list.
John Fastabend91f3e7b2013-03-29 08:18:37 +00003244 * Returns number of addresses from list put in skb.
John Fastabendd83b0602012-04-15 06:44:08 +00003245 */
3246int ndo_dflt_fdb_dump(struct sk_buff *skb,
3247 struct netlink_callback *cb,
3248 struct net_device *dev,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04003249 struct net_device *filter_dev,
Roopa Prabhud2976532016-08-30 21:56:45 -07003250 int *idx)
John Fastabendd83b0602012-04-15 06:44:08 +00003251{
3252 int err;
3253
3254 netif_addr_lock_bh(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -07003255 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
John Fastabendd83b0602012-04-15 06:44:08 +00003256 if (err)
3257 goto out;
Zhang Shengju2934c9d2016-11-30 16:37:34 +08003258 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
John Fastabendd83b0602012-04-15 06:44:08 +00003259out:
3260 netif_addr_unlock_bh(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -07003261 return err;
John Fastabendd83b0602012-04-15 06:44:08 +00003262}
3263EXPORT_SYMBOL(ndo_dflt_fdb_dump);
3264
John Fastabend77162022012-04-15 06:43:56 +00003265static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
3266{
John Fastabend77162022012-04-15 06:43:56 +00003267 struct net_device *dev;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003268 struct nlattr *tb[IFLA_MAX+1];
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003269 struct net_device *br_dev = NULL;
3270 const struct net_device_ops *ops = NULL;
3271 const struct net_device_ops *cops = NULL;
3272 struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
3273 struct net *net = sock_net(skb->sk);
Roopa Prabhud2976532016-08-30 21:56:45 -07003274 struct hlist_head *head;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003275 int brport_idx = 0;
3276 int br_idx = 0;
Roopa Prabhud2976532016-08-30 21:56:45 -07003277 int h, s_h;
3278 int idx = 0, s_idx;
3279 int err = 0;
3280 int fidx = 0;
John Fastabend77162022012-04-15 06:43:56 +00003281
Alexander Potapenko0ff50e82017-05-23 13:20:28 +02003282 err = nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb,
3283 IFLA_MAX, ifla_policy, NULL);
3284 if (err < 0) {
3285 return -EINVAL;
3286 } else if (err == 0) {
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003287 if (tb[IFLA_MASTER])
3288 br_idx = nla_get_u32(tb[IFLA_MASTER]);
3289 }
John Fastabend77162022012-04-15 06:43:56 +00003290
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003291 brport_idx = ifm->ifi_index;
3292
3293 if (br_idx) {
3294 br_dev = __dev_get_by_index(net, br_idx);
3295 if (!br_dev)
3296 return -ENODEV;
3297
3298 ops = br_dev->netdev_ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003299 }
3300
Roopa Prabhud2976532016-08-30 21:56:45 -07003301 s_h = cb->args[0];
3302 s_idx = cb->args[1];
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003303
Roopa Prabhud2976532016-08-30 21:56:45 -07003304 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
3305 idx = 0;
3306 head = &net->dev_index_head[h];
3307 hlist_for_each_entry(dev, head, index_hlist) {
3308
3309 if (brport_idx && (dev->ifindex != brport_idx))
3310 continue;
3311
3312 if (!br_idx) { /* user did not specify a specific bridge */
3313 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3314 br_dev = netdev_master_upper_dev_get(dev);
3315 cops = br_dev->netdev_ops;
3316 }
3317 } else {
3318 if (dev != br_dev &&
3319 !(dev->priv_flags & IFF_BRIDGE_PORT))
3320 continue;
3321
3322 if (br_dev != netdev_master_upper_dev_get(dev) &&
3323 !(dev->priv_flags & IFF_EBRIDGE))
3324 continue;
3325 cops = ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003326 }
3327
Roopa Prabhud2976532016-08-30 21:56:45 -07003328 if (idx < s_idx)
3329 goto cont;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003330
Roopa Prabhud2976532016-08-30 21:56:45 -07003331 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3332 if (cops && cops->ndo_fdb_dump) {
3333 err = cops->ndo_fdb_dump(skb, cb,
3334 br_dev, dev,
3335 &fidx);
3336 if (err == -EMSGSIZE)
3337 goto out;
3338 }
3339 }
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003340
Roopa Prabhud2976532016-08-30 21:56:45 -07003341 if (dev->netdev_ops->ndo_fdb_dump)
3342 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
3343 dev, NULL,
3344 &fidx);
3345 else
3346 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
3347 &fidx);
3348 if (err == -EMSGSIZE)
3349 goto out;
3350
3351 cops = NULL;
3352
3353 /* reset fdb offset to 0 for rest of the interfaces */
3354 cb->args[2] = 0;
3355 fidx = 0;
3356cont:
3357 idx++;
John Fastabend77162022012-04-15 06:43:56 +00003358 }
John Fastabend77162022012-04-15 06:43:56 +00003359 }
John Fastabend77162022012-04-15 06:43:56 +00003360
Roopa Prabhud2976532016-08-30 21:56:45 -07003361out:
3362 cb->args[0] = h;
3363 cb->args[1] = idx;
3364 cb->args[2] = fidx;
3365
John Fastabend77162022012-04-15 06:43:56 +00003366 return skb->len;
3367}
3368
Scott Feldman2c3c0312014-11-28 14:34:25 +01003369static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
3370 unsigned int attrnum, unsigned int flag)
3371{
3372 if (mask & flag)
3373 return nla_put_u8(skb, attrnum, !!(flags & flag));
3374 return 0;
3375}
3376
John Fastabend815cccb2012-10-24 08:13:09 +00003377int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Scott Feldman2c3c0312014-11-28 14:34:25 +01003378 struct net_device *dev, u16 mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003379 u32 flags, u32 mask, int nlflags,
3380 u32 filter_mask,
3381 int (*vlan_fill)(struct sk_buff *skb,
3382 struct net_device *dev,
3383 u32 filter_mask))
John Fastabend815cccb2012-10-24 08:13:09 +00003384{
3385 struct nlmsghdr *nlh;
3386 struct ifinfomsg *ifm;
3387 struct nlattr *br_afspec;
Scott Feldman2c3c0312014-11-28 14:34:25 +01003388 struct nlattr *protinfo;
John Fastabend815cccb2012-10-24 08:13:09 +00003389 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Jiri Pirko898e5062013-01-03 22:48:52 +00003390 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003391 int err = 0;
John Fastabend815cccb2012-10-24 08:13:09 +00003392
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02003393 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
John Fastabend815cccb2012-10-24 08:13:09 +00003394 if (nlh == NULL)
3395 return -EMSGSIZE;
3396
3397 ifm = nlmsg_data(nlh);
3398 ifm->ifi_family = AF_BRIDGE;
3399 ifm->__ifi_pad = 0;
3400 ifm->ifi_type = dev->type;
3401 ifm->ifi_index = dev->ifindex;
3402 ifm->ifi_flags = dev_get_flags(dev);
3403 ifm->ifi_change = 0;
3404
3405
3406 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
3407 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
3408 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00003409 (br_dev &&
3410 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
John Fastabend815cccb2012-10-24 08:13:09 +00003411 (dev->addr_len &&
3412 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02003413 (dev->ifindex != dev_get_iflink(dev) &&
3414 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
John Fastabend815cccb2012-10-24 08:13:09 +00003415 goto nla_put_failure;
3416
3417 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
3418 if (!br_afspec)
3419 goto nla_put_failure;
3420
Roopa Prabhu1d460b92014-12-08 14:04:20 -08003421 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
John Fastabend815cccb2012-10-24 08:13:09 +00003422 nla_nest_cancel(skb, br_afspec);
3423 goto nla_put_failure;
3424 }
Roopa Prabhu1d460b92014-12-08 14:04:20 -08003425
3426 if (mode != BRIDGE_MODE_UNDEF) {
3427 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
3428 nla_nest_cancel(skb, br_afspec);
3429 goto nla_put_failure;
3430 }
3431 }
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003432 if (vlan_fill) {
3433 err = vlan_fill(skb, dev, filter_mask);
3434 if (err) {
3435 nla_nest_cancel(skb, br_afspec);
3436 goto nla_put_failure;
3437 }
3438 }
John Fastabend815cccb2012-10-24 08:13:09 +00003439 nla_nest_end(skb, br_afspec);
3440
Scott Feldman2c3c0312014-11-28 14:34:25 +01003441 protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
3442 if (!protinfo)
3443 goto nla_put_failure;
3444
3445 if (brport_nla_put_flag(skb, flags, mask,
3446 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
3447 brport_nla_put_flag(skb, flags, mask,
3448 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
3449 brport_nla_put_flag(skb, flags, mask,
3450 IFLA_BRPORT_FAST_LEAVE,
3451 BR_MULTICAST_FAST_LEAVE) ||
3452 brport_nla_put_flag(skb, flags, mask,
3453 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
3454 brport_nla_put_flag(skb, flags, mask,
3455 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
3456 brport_nla_put_flag(skb, flags, mask,
3457 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
3458 brport_nla_put_flag(skb, flags, mask,
3459 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
3460 brport_nla_put_flag(skb, flags, mask,
3461 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
3462 nla_nest_cancel(skb, protinfo);
3463 goto nla_put_failure;
3464 }
3465
3466 nla_nest_end(skb, protinfo);
3467
Johannes Berg053c0952015-01-16 22:09:00 +01003468 nlmsg_end(skb, nlh);
3469 return 0;
John Fastabend815cccb2012-10-24 08:13:09 +00003470nla_put_failure:
3471 nlmsg_cancel(skb, nlh);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003472 return err ? err : -EMSGSIZE;
John Fastabend815cccb2012-10-24 08:13:09 +00003473}
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003474EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
John Fastabend815cccb2012-10-24 08:13:09 +00003475
John Fastabende5a55a82012-10-24 08:12:57 +00003476static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
3477{
3478 struct net *net = sock_net(skb->sk);
3479 struct net_device *dev;
3480 int idx = 0;
3481 u32 portid = NETLINK_CB(cb->skb).portid;
3482 u32 seq = cb->nlh->nlmsg_seq;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00003483 u32 filter_mask = 0;
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003484 int err;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00003485
Thomas Grafaa68c202014-11-26 13:42:20 +01003486 if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
3487 struct nlattr *extfilt;
3488
3489 extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
3490 IFLA_EXT_MASK);
3491 if (extfilt) {
3492 if (nla_len(extfilt) < sizeof(filter_mask))
3493 return -EINVAL;
3494
3495 filter_mask = nla_get_u32(extfilt);
3496 }
3497 }
John Fastabende5a55a82012-10-24 08:12:57 +00003498
3499 rcu_read_lock();
3500 for_each_netdev_rcu(net, dev) {
3501 const struct net_device_ops *ops = dev->netdev_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00003502 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
John Fastabende5a55a82012-10-24 08:12:57 +00003503
Jiri Pirko898e5062013-01-03 22:48:52 +00003504 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003505 if (idx >= cb->args[0]) {
3506 err = br_dev->netdev_ops->ndo_bridge_getlink(
3507 skb, portid, seq, dev,
3508 filter_mask, NLM_F_MULTI);
David Ahernf6c57752017-05-15 23:19:17 -07003509 if (err < 0 && err != -EOPNOTSUPP) {
3510 if (likely(skb->len))
3511 break;
3512
3513 goto out_err;
3514 }
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003515 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00003516 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00003517 }
3518
3519 if (ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003520 if (idx >= cb->args[0]) {
3521 err = ops->ndo_bridge_getlink(skb, portid,
3522 seq, dev,
3523 filter_mask,
3524 NLM_F_MULTI);
David Ahernf6c57752017-05-15 23:19:17 -07003525 if (err < 0 && err != -EOPNOTSUPP) {
3526 if (likely(skb->len))
3527 break;
3528
3529 goto out_err;
3530 }
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003531 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00003532 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00003533 }
3534 }
David Ahernf6c57752017-05-15 23:19:17 -07003535 err = skb->len;
3536out_err:
John Fastabende5a55a82012-10-24 08:12:57 +00003537 rcu_read_unlock();
3538 cb->args[0] = idx;
3539
David Ahernf6c57752017-05-15 23:19:17 -07003540 return err;
John Fastabende5a55a82012-10-24 08:12:57 +00003541}
3542
John Fastabend2469ffd2012-10-24 08:13:03 +00003543static inline size_t bridge_nlmsg_size(void)
3544{
3545 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3546 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3547 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3548 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
3549 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
3550 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
3551 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
3552 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
3553 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
3554 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
3555 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
3556}
3557
Roopa Prabhu02dba432015-01-14 20:02:25 -08003558static int rtnl_bridge_notify(struct net_device *dev)
John Fastabend2469ffd2012-10-24 08:13:03 +00003559{
3560 struct net *net = dev_net(dev);
John Fastabend2469ffd2012-10-24 08:13:03 +00003561 struct sk_buff *skb;
3562 int err = -EOPNOTSUPP;
3563
Roopa Prabhu02dba432015-01-14 20:02:25 -08003564 if (!dev->netdev_ops->ndo_bridge_getlink)
3565 return 0;
3566
John Fastabend2469ffd2012-10-24 08:13:03 +00003567 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
3568 if (!skb) {
3569 err = -ENOMEM;
3570 goto errout;
3571 }
3572
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02003573 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
Roopa Prabhu02dba432015-01-14 20:02:25 -08003574 if (err < 0)
3575 goto errout;
John Fastabend2469ffd2012-10-24 08:13:03 +00003576
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08003577 if (!skb->len)
3578 goto errout;
3579
John Fastabend2469ffd2012-10-24 08:13:03 +00003580 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
3581 return 0;
3582errout:
3583 WARN_ON(err == -EMSGSIZE);
3584 kfree_skb(skb);
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08003585 if (err)
3586 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
John Fastabend2469ffd2012-10-24 08:13:03 +00003587 return err;
3588}
3589
David Ahernc21ef3e2017-04-16 09:48:24 -07003590static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3591 struct netlink_ext_ack *extack)
John Fastabende5a55a82012-10-24 08:12:57 +00003592{
3593 struct net *net = sock_net(skb->sk);
3594 struct ifinfomsg *ifm;
3595 struct net_device *dev;
John Fastabend2469ffd2012-10-24 08:13:03 +00003596 struct nlattr *br_spec, *attr = NULL;
3597 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02003598 u16 flags = 0;
John Fastabendc38e01b2012-11-02 16:32:36 +00003599 bool have_flags = false;
John Fastabende5a55a82012-10-24 08:12:57 +00003600
3601 if (nlmsg_len(nlh) < sizeof(*ifm))
3602 return -EINVAL;
3603
3604 ifm = nlmsg_data(nlh);
3605 if (ifm->ifi_family != AF_BRIDGE)
3606 return -EPFNOSUPPORT;
3607
3608 dev = __dev_get_by_index(net, ifm->ifi_index);
3609 if (!dev) {
3610 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3611 return -ENODEV;
3612 }
3613
John Fastabend2469ffd2012-10-24 08:13:03 +00003614 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3615 if (br_spec) {
3616 nla_for_each_nested(attr, br_spec, rem) {
3617 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01003618 if (nla_len(attr) < sizeof(flags))
3619 return -EINVAL;
3620
John Fastabendc38e01b2012-11-02 16:32:36 +00003621 have_flags = true;
John Fastabend2469ffd2012-10-24 08:13:03 +00003622 flags = nla_get_u16(attr);
3623 break;
3624 }
3625 }
3626 }
3627
3628 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003629 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3630
3631 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
John Fastabend2469ffd2012-10-24 08:13:03 +00003632 err = -EOPNOTSUPP;
3633 goto out;
3634 }
3635
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003636 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
John Fastabende5a55a82012-10-24 08:12:57 +00003637 if (err)
3638 goto out;
John Fastabend2469ffd2012-10-24 08:13:03 +00003639
3640 flags &= ~BRIDGE_FLAGS_MASTER;
John Fastabende5a55a82012-10-24 08:12:57 +00003641 }
3642
John Fastabend2469ffd2012-10-24 08:13:03 +00003643 if ((flags & BRIDGE_FLAGS_SELF)) {
3644 if (!dev->netdev_ops->ndo_bridge_setlink)
3645 err = -EOPNOTSUPP;
3646 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003647 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
3648 flags);
Roopa Prabhu02dba432015-01-14 20:02:25 -08003649 if (!err) {
John Fastabend2469ffd2012-10-24 08:13:03 +00003650 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08003651
3652 /* Generate event to notify upper layer of bridge
3653 * change
3654 */
3655 err = rtnl_bridge_notify(dev);
3656 }
John Fastabend2469ffd2012-10-24 08:13:03 +00003657 }
3658
John Fastabendc38e01b2012-11-02 16:32:36 +00003659 if (have_flags)
John Fastabend2469ffd2012-10-24 08:13:03 +00003660 memcpy(nla_data(attr), &flags, sizeof(flags));
John Fastabende5a55a82012-10-24 08:12:57 +00003661out:
3662 return err;
3663}
3664
David Ahernc21ef3e2017-04-16 09:48:24 -07003665static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
3666 struct netlink_ext_ack *extack)
Vlad Yasevich407af322013-02-13 12:00:12 +00003667{
3668 struct net *net = sock_net(skb->sk);
3669 struct ifinfomsg *ifm;
3670 struct net_device *dev;
3671 struct nlattr *br_spec, *attr = NULL;
3672 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02003673 u16 flags = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +00003674 bool have_flags = false;
3675
3676 if (nlmsg_len(nlh) < sizeof(*ifm))
3677 return -EINVAL;
3678
3679 ifm = nlmsg_data(nlh);
3680 if (ifm->ifi_family != AF_BRIDGE)
3681 return -EPFNOSUPPORT;
3682
3683 dev = __dev_get_by_index(net, ifm->ifi_index);
3684 if (!dev) {
3685 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3686 return -ENODEV;
3687 }
3688
3689 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3690 if (br_spec) {
3691 nla_for_each_nested(attr, br_spec, rem) {
3692 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01003693 if (nla_len(attr) < sizeof(flags))
3694 return -EINVAL;
3695
Vlad Yasevich407af322013-02-13 12:00:12 +00003696 have_flags = true;
3697 flags = nla_get_u16(attr);
3698 break;
3699 }
3700 }
3701 }
3702
Vlad Yasevich407af322013-02-13 12:00:12 +00003703 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3704 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3705
3706 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
3707 err = -EOPNOTSUPP;
3708 goto out;
3709 }
3710
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003711 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00003712 if (err)
3713 goto out;
3714
3715 flags &= ~BRIDGE_FLAGS_MASTER;
3716 }
3717
3718 if ((flags & BRIDGE_FLAGS_SELF)) {
3719 if (!dev->netdev_ops->ndo_bridge_dellink)
3720 err = -EOPNOTSUPP;
3721 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003722 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
3723 flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00003724
Roopa Prabhu02dba432015-01-14 20:02:25 -08003725 if (!err) {
Vlad Yasevich407af322013-02-13 12:00:12 +00003726 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08003727
3728 /* Generate event to notify upper layer of bridge
3729 * change
3730 */
3731 err = rtnl_bridge_notify(dev);
3732 }
Vlad Yasevich407af322013-02-13 12:00:12 +00003733 }
3734
3735 if (have_flags)
3736 memcpy(nla_data(attr), &flags, sizeof(flags));
Vlad Yasevich407af322013-02-13 12:00:12 +00003737out:
3738 return err;
3739}
3740
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003741static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
3742{
3743 return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
3744 (!idxattr || idxattr == attrid);
3745}
3746
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02003747#define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
3748static int rtnl_get_offload_stats_attr_size(int attr_id)
3749{
3750 switch (attr_id) {
3751 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
3752 return sizeof(struct rtnl_link_stats64);
3753 }
3754
3755 return 0;
3756}
3757
3758static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
3759 int *prividx)
3760{
3761 struct nlattr *attr = NULL;
3762 int attr_id, size;
3763 void *attr_data;
3764 int err;
3765
3766 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
3767 dev->netdev_ops->ndo_get_offload_stats))
3768 return -ENODATA;
3769
3770 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
3771 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
3772 if (attr_id < *prividx)
3773 continue;
3774
3775 size = rtnl_get_offload_stats_attr_size(attr_id);
3776 if (!size)
3777 continue;
3778
Or Gerlitz3df5b3c2016-11-22 23:09:54 +02003779 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02003780 continue;
3781
3782 attr = nla_reserve_64bit(skb, attr_id, size,
3783 IFLA_OFFLOAD_XSTATS_UNSPEC);
3784 if (!attr)
3785 goto nla_put_failure;
3786
3787 attr_data = nla_data(attr);
3788 memset(attr_data, 0, size);
3789 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
3790 attr_data);
3791 if (err)
3792 goto get_offload_stats_failure;
3793 }
3794
3795 if (!attr)
3796 return -ENODATA;
3797
3798 *prividx = 0;
3799 return 0;
3800
3801nla_put_failure:
3802 err = -EMSGSIZE;
3803get_offload_stats_failure:
3804 *prividx = attr_id;
3805 return err;
3806}
3807
3808static int rtnl_get_offload_stats_size(const struct net_device *dev)
3809{
3810 int nla_size = 0;
3811 int attr_id;
3812 int size;
3813
3814 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
3815 dev->netdev_ops->ndo_get_offload_stats))
3816 return 0;
3817
3818 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
3819 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
Or Gerlitz3df5b3c2016-11-22 23:09:54 +02003820 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02003821 continue;
3822 size = rtnl_get_offload_stats_attr_size(attr_id);
3823 nla_size += nla_total_size_64bit(size);
3824 }
3825
3826 if (nla_size != 0)
3827 nla_size += nla_total_size(0);
3828
3829 return nla_size;
3830}
3831
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003832static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
3833 int type, u32 pid, u32 seq, u32 change,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003834 unsigned int flags, unsigned int filter_mask,
3835 int *idxattr, int *prividx)
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003836{
3837 struct if_stats_msg *ifsm;
3838 struct nlmsghdr *nlh;
3839 struct nlattr *attr;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003840 int s_prividx = *prividx;
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02003841 int err;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003842
3843 ASSERT_RTNL();
3844
3845 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
3846 if (!nlh)
3847 return -EMSGSIZE;
3848
3849 ifsm = nlmsg_data(nlh);
3850 ifsm->ifindex = dev->ifindex;
3851 ifsm->filter_mask = filter_mask;
3852
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003853 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003854 struct rtnl_link_stats64 *sp;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003855
Nicolas Dichtel58414d32016-04-21 18:58:25 +02003856 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
3857 sizeof(struct rtnl_link_stats64),
3858 IFLA_STATS_UNSPEC);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003859 if (!attr)
3860 goto nla_put_failure;
3861
3862 sp = nla_data(attr);
3863 dev_get_stats(dev, sp);
3864 }
3865
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02003866 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
3867 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
3868
3869 if (ops && ops->fill_linkxstats) {
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02003870 *idxattr = IFLA_STATS_LINK_XSTATS;
3871 attr = nla_nest_start(skb,
3872 IFLA_STATS_LINK_XSTATS);
3873 if (!attr)
3874 goto nla_put_failure;
3875
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02003876 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
3877 nla_nest_end(skb, attr);
3878 if (err)
3879 goto nla_put_failure;
3880 *idxattr = 0;
3881 }
3882 }
3883
3884 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
3885 *idxattr)) {
3886 const struct rtnl_link_ops *ops = NULL;
3887 const struct net_device *master;
3888
3889 master = netdev_master_upper_dev_get(dev);
3890 if (master)
3891 ops = master->rtnl_link_ops;
3892 if (ops && ops->fill_linkxstats) {
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02003893 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
3894 attr = nla_nest_start(skb,
3895 IFLA_STATS_LINK_XSTATS_SLAVE);
3896 if (!attr)
3897 goto nla_put_failure;
3898
3899 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02003900 nla_nest_end(skb, attr);
3901 if (err)
3902 goto nla_put_failure;
3903 *idxattr = 0;
3904 }
3905 }
3906
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02003907 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
3908 *idxattr)) {
3909 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
3910 attr = nla_nest_start(skb, IFLA_STATS_LINK_OFFLOAD_XSTATS);
3911 if (!attr)
3912 goto nla_put_failure;
3913
3914 err = rtnl_get_offload_stats(skb, dev, prividx);
3915 if (err == -ENODATA)
3916 nla_nest_cancel(skb, attr);
3917 else
3918 nla_nest_end(skb, attr);
3919
3920 if (err && err != -ENODATA)
3921 goto nla_put_failure;
3922 *idxattr = 0;
3923 }
3924
Robert Shearmanaefb4d42017-01-16 14:16:36 +00003925 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
3926 struct rtnl_af_ops *af_ops;
3927
3928 *idxattr = IFLA_STATS_AF_SPEC;
3929 attr = nla_nest_start(skb, IFLA_STATS_AF_SPEC);
3930 if (!attr)
3931 goto nla_put_failure;
3932
3933 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
3934 if (af_ops->fill_stats_af) {
3935 struct nlattr *af;
3936 int err;
3937
3938 af = nla_nest_start(skb, af_ops->family);
3939 if (!af)
3940 goto nla_put_failure;
3941
3942 err = af_ops->fill_stats_af(skb, dev);
3943
3944 if (err == -ENODATA)
3945 nla_nest_cancel(skb, af);
3946 else if (err < 0)
3947 goto nla_put_failure;
3948
3949 nla_nest_end(skb, af);
3950 }
3951 }
3952
3953 nla_nest_end(skb, attr);
3954
3955 *idxattr = 0;
3956 }
3957
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003958 nlmsg_end(skb, nlh);
3959
3960 return 0;
3961
3962nla_put_failure:
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003963 /* not a multi message or no progress mean a real error */
3964 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
3965 nlmsg_cancel(skb, nlh);
3966 else
3967 nlmsg_end(skb, nlh);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003968
3969 return -EMSGSIZE;
3970}
3971
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003972static size_t if_nlmsg_stats_size(const struct net_device *dev,
3973 u32 filter_mask)
3974{
3975 size_t size = 0;
3976
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003977 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003978 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
3979
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02003980 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
3981 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02003982 int attr = IFLA_STATS_LINK_XSTATS;
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02003983
3984 if (ops && ops->get_linkxstats_size) {
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02003985 size += nla_total_size(ops->get_linkxstats_size(dev,
3986 attr));
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02003987 /* for IFLA_STATS_LINK_XSTATS */
3988 size += nla_total_size(0);
3989 }
3990 }
3991
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02003992 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
3993 struct net_device *_dev = (struct net_device *)dev;
3994 const struct rtnl_link_ops *ops = NULL;
3995 const struct net_device *master;
3996
3997 /* netdev_master_upper_dev_get can't take const */
3998 master = netdev_master_upper_dev_get(_dev);
3999 if (master)
4000 ops = master->rtnl_link_ops;
4001 if (ops && ops->get_linkxstats_size) {
4002 int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
4003
4004 size += nla_total_size(ops->get_linkxstats_size(dev,
4005 attr));
4006 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
4007 size += nla_total_size(0);
4008 }
4009 }
4010
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004011 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
4012 size += rtnl_get_offload_stats_size(dev);
4013
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004014 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
4015 struct rtnl_af_ops *af_ops;
4016
4017 /* for IFLA_STATS_AF_SPEC */
4018 size += nla_total_size(0);
4019
4020 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
4021 if (af_ops->get_stats_af_size) {
4022 size += nla_total_size(
4023 af_ops->get_stats_af_size(dev));
4024
4025 /* for AF_* */
4026 size += nla_total_size(0);
4027 }
4028 }
4029 }
4030
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004031 return size;
4032}
4033
David Ahernc21ef3e2017-04-16 09:48:24 -07004034static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
4035 struct netlink_ext_ack *extack)
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004036{
4037 struct net *net = sock_net(skb->sk);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004038 struct net_device *dev = NULL;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004039 int idxattr = 0, prividx = 0;
4040 struct if_stats_msg *ifsm;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004041 struct sk_buff *nskb;
4042 u32 filter_mask;
4043 int err;
4044
Mathias Krause4775cc12016-12-28 17:52:15 +01004045 if (nlmsg_len(nlh) < sizeof(*ifsm))
4046 return -EINVAL;
4047
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004048 ifsm = nlmsg_data(nlh);
4049 if (ifsm->ifindex > 0)
4050 dev = __dev_get_by_index(net, ifsm->ifindex);
4051 else
4052 return -EINVAL;
4053
4054 if (!dev)
4055 return -ENODEV;
4056
4057 filter_mask = ifsm->filter_mask;
4058 if (!filter_mask)
4059 return -EINVAL;
4060
4061 nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
4062 if (!nskb)
4063 return -ENOBUFS;
4064
4065 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
4066 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004067 0, filter_mask, &idxattr, &prividx);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004068 if (err < 0) {
4069 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
4070 WARN_ON(err == -EMSGSIZE);
4071 kfree_skb(nskb);
4072 } else {
4073 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
4074 }
4075
4076 return err;
4077}
4078
4079static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
4080{
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004081 int h, s_h, err, s_idx, s_idxattr, s_prividx;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004082 struct net *net = sock_net(skb->sk);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004083 unsigned int flags = NLM_F_MULTI;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004084 struct if_stats_msg *ifsm;
4085 struct hlist_head *head;
4086 struct net_device *dev;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004087 u32 filter_mask = 0;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004088 int idx = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004089
4090 s_h = cb->args[0];
4091 s_idx = cb->args[1];
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004092 s_idxattr = cb->args[2];
4093 s_prividx = cb->args[3];
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004094
4095 cb->seq = net->dev_base_seq;
4096
Mathias Krause4775cc12016-12-28 17:52:15 +01004097 if (nlmsg_len(cb->nlh) < sizeof(*ifsm))
4098 return -EINVAL;
4099
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004100 ifsm = nlmsg_data(cb->nlh);
4101 filter_mask = ifsm->filter_mask;
4102 if (!filter_mask)
4103 return -EINVAL;
4104
4105 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4106 idx = 0;
4107 head = &net->dev_index_head[h];
4108 hlist_for_each_entry(dev, head, index_hlist) {
4109 if (idx < s_idx)
4110 goto cont;
4111 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
4112 NETLINK_CB(cb->skb).portid,
4113 cb->nlh->nlmsg_seq, 0,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004114 flags, filter_mask,
4115 &s_idxattr, &s_prividx);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004116 /* If we ran out of room on the first message,
4117 * we're in trouble
4118 */
4119 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
4120
4121 if (err < 0)
4122 goto out;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004123 s_prividx = 0;
4124 s_idxattr = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004125 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
4126cont:
4127 idx++;
4128 }
4129 }
4130out:
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004131 cb->args[3] = s_prividx;
4132 cb->args[2] = s_idxattr;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004133 cb->args[1] = idx;
4134 cb->args[0] = h;
4135
4136 return skb->len;
4137}
4138
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139/* Process one rtnetlink message. */
4140
Johannes Berg2d4bc932017-04-12 14:34:04 +02004141static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
4142 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004144 struct net *net = sock_net(skb->sk);
Thomas Grafe2849862007-03-22 11:48:11 -07004145 rtnl_doit_func doit;
Alexander Kuleshov617cfc72016-01-10 21:26:57 +06004146 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 int family;
4148 int type;
Eric Dumazet2907c352011-05-25 07:34:04 +00004149 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 if (type > RTM_MAX)
Thomas Graf038890f2007-04-05 14:35:52 -07004153 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154
4155 type -= RTM_BASE;
4156
4157 /* All the messages must have at least 1 byte length */
Hong zhi guo573ce262013-03-27 06:47:04 +00004158 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 return 0;
4160
Hong zhi guo573ce262013-03-27 06:47:04 +00004161 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 kind = type&3;
4163
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07004164 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07004165 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166
David S. Millerb8f3ab42011-01-18 12:40:38 -08004167 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004168 struct sock *rtnl;
Thomas Grafe2849862007-03-22 11:48:11 -07004169 rtnl_dumpit_func dumpit;
Greg Rosec7ac8672011-06-10 01:27:09 +00004170 u16 min_dump_alloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171
Thomas Grafe2849862007-03-22 11:48:11 -07004172 dumpit = rtnl_get_dumpit(family, type);
4173 if (dumpit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07004174 return -EOPNOTSUPP;
Florian Westphale1fa6d22017-08-09 20:41:47 +02004175
4176 if (type == RTM_GETLINK)
4177 min_dump_alloc = rtnl_calcit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178
Eric Dumazet2907c352011-05-25 07:34:04 +00004179 __rtnl_unlock();
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004180 rtnl = net->rtnl;
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00004181 {
4182 struct netlink_dump_control c = {
4183 .dump = dumpit,
4184 .min_dump_alloc = min_dump_alloc,
4185 };
4186 err = netlink_dump_start(rtnl, skb, nlh, &c);
4187 }
Eric Dumazet2907c352011-05-25 07:34:04 +00004188 rtnl_lock();
4189 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 }
4191
Thomas Grafe2849862007-03-22 11:48:11 -07004192 doit = rtnl_get_doit(family, type);
4193 if (doit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07004194 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195
David Ahernc21ef3e2017-04-16 09:48:24 -07004196 return doit(skb, nlh, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197}
4198
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07004199static void rtnetlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07004201 rtnl_lock();
4202 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
4203 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204}
4205
Julien Gomes5f729ea2017-06-20 13:54:16 -07004206static int rtnetlink_bind(struct net *net, int group)
4207{
4208 switch (group) {
4209 case RTNLGRP_IPV4_MROUTE_R:
4210 case RTNLGRP_IPV6_MROUTE_R:
4211 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
4212 return -EPERM;
4213 break;
4214 }
4215 return 0;
4216}
4217
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
4219{
Jiri Pirko351638e2013-05-28 01:30:21 +00004220 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02004221
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 switch (event) {
Vlad Yasevich5138e862017-04-04 09:23:41 -04004223 case NETDEV_REBOOT:
David Ahern37536542017-07-19 10:22:40 -07004224 case NETDEV_CHANGEADDR:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004225 case NETDEV_CHANGENAME:
4226 case NETDEV_FEAT_CHANGE:
4227 case NETDEV_BONDING_FAILOVER:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004228 case NETDEV_NOTIFY_PEERS:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004229 case NETDEV_RESEND_IGMP:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004230 case NETDEV_CHANGEINFODATA:
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04004231 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
4232 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 break;
4234 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 break;
4236 }
4237 return NOTIFY_DONE;
4238}
4239
4240static struct notifier_block rtnetlink_dev_notifier = {
4241 .notifier_call = rtnetlink_event,
4242};
4243
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004244
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004245static int __net_init rtnetlink_net_init(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004246{
4247 struct sock *sk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00004248 struct netlink_kernel_cfg cfg = {
4249 .groups = RTNLGRP_MAX,
4250 .input = rtnetlink_rcv,
4251 .cb_mutex = &rtnl_mutex,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00004252 .flags = NL_CFG_F_NONROOT_RECV,
Julien Gomes5f729ea2017-06-20 13:54:16 -07004253 .bind = rtnetlink_bind,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00004254 };
4255
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00004256 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004257 if (!sk)
4258 return -ENOMEM;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004259 net->rtnl = sk;
4260 return 0;
4261}
4262
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004263static void __net_exit rtnetlink_net_exit(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004264{
Denis V. Lunev775516b2008-01-18 23:55:19 -08004265 netlink_kernel_release(net->rtnl);
4266 net->rtnl = NULL;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004267}
4268
4269static struct pernet_operations rtnetlink_net_ops = {
4270 .init = rtnetlink_net_init,
4271 .exit = rtnetlink_net_exit,
4272};
4273
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274void __init rtnetlink_init(void)
4275{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004276 if (register_pernet_subsys(&rtnetlink_net_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 panic("rtnetlink_init: cannot initialize rtnetlink\n");
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004278
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 register_netdevice_notifier(&rtnetlink_dev_notifier);
Thomas Graf340d17f2007-03-22 11:49:22 -07004280
Greg Rosec7ac8672011-06-10 01:27:09 +00004281 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
Florian Westphale1fa6d22017-08-09 20:41:47 +02004282 rtnl_dump_ifinfo, NULL);
Greg Rosec7ac8672011-06-10 01:27:09 +00004283 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
4284 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
4285 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
Thomas Graf687ad8c2007-03-22 11:59:42 -07004286
Greg Rosec7ac8672011-06-10 01:27:09 +00004287 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
4288 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
David Aherna7678c72017-03-21 12:22:26 -07004289 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, NULL);
John Fastabend77162022012-04-15 06:43:56 +00004290
4291 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL);
4292 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL);
4293 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL);
John Fastabende5a55a82012-10-24 08:12:57 +00004294
4295 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL);
Vlad Yasevich407af322013-02-13 12:00:12 +00004296 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL);
John Fastabende5a55a82012-10-24 08:12:57 +00004297 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004298
4299 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
4300 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301}