Merge tag 'mlx5-updates-2021-04-21' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-block.git] / net / core / rtnetlink.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4
LT
2/*
3 * INET An implementation of the TCP/IP protocol suite for the LINUX
4 * operating system. INET is implemented using the BSD Socket
5 * interface as the means of communication with the user level.
6 *
7 * Routing netlink socket interface: protocol independent part.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 *
1da177e4
LT
11 * Fixes:
12 * Vitaly E. Lavrov RTA_OK arithmetics was wrong.
13 */
14
ee5d032f 15#include <linux/bitops.h>
1da177e4
LT
16#include <linux/errno.h>
17#include <linux/module.h>
18#include <linux/types.h>
19#include <linux/socket.h>
20#include <linux/kernel.h>
1da177e4
LT
21#include <linux/timer.h>
22#include <linux/string.h>
23#include <linux/sockios.h>
24#include <linux/net.h>
25#include <linux/fcntl.h>
26#include <linux/mm.h>
27#include <linux/slab.h>
28#include <linux/interrupt.h>
29#include <linux/capability.h>
30#include <linux/skbuff.h>
31#include <linux/init.h>
32#include <linux/security.h>
6756ae4b 33#include <linux/mutex.h>
1823730f 34#include <linux/if_addr.h>
77162022 35#include <linux/if_bridge.h>
f6f6424b 36#include <linux/if_vlan.h>
ebc08a6f 37#include <linux/pci.h>
77162022 38#include <linux/etherdevice.h>
58038695 39#include <linux/bpf.h>
1da177e4 40
7c0f6ba6 41#include <linux/uaccess.h>
1da177e4
LT
42
43#include <linux/inet.h>
44#include <linux/netdevice.h>
45#include <net/ip.h>
46#include <net/protocol.h>
47#include <net/arp.h>
48#include <net/route.h>
49#include <net/udp.h>
ea697639 50#include <net/tcp.h>
1da177e4
LT
51#include <net/sock.h>
52#include <net/pkt_sched.h>
14c0b97d 53#include <net/fib_rules.h>
e2849863 54#include <net/rtnetlink.h>
30ffee84 55#include <net/net_namespace.h>
1da177e4 56
a428afe8 57#define RTNL_MAX_TYPE 50
2dba407f 58#define RTNL_SLAVE_MAX_TYPE 40
ccf8dbcd 59
e0d087af 60struct rtnl_link {
e2849863
TG
61 rtnl_doit_func doit;
62 rtnl_dumpit_func dumpit;
e4202511 63 struct module *owner;
62256f98 64 unsigned int flags;
addf9b90 65 struct rcu_head rcu;
e2849863
TG
66};
67
6756ae4b 68static DEFINE_MUTEX(rtnl_mutex);
1da177e4
LT
69
70void rtnl_lock(void)
71{
6756ae4b 72 mutex_lock(&rtnl_mutex);
1da177e4 73}
e0d087af 74EXPORT_SYMBOL(rtnl_lock);
1da177e4 75
79ffdfc6
KT
76int rtnl_lock_killable(void)
77{
78 return mutex_lock_killable(&rtnl_mutex);
79}
80EXPORT_SYMBOL(rtnl_lock_killable);
81
1b5c5493
ED
82static struct sk_buff *defer_kfree_skb_list;
83void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail)
84{
85 if (head && tail) {
86 tail->next = defer_kfree_skb_list;
87 defer_kfree_skb_list = head;
88 }
89}
90EXPORT_SYMBOL(rtnl_kfree_skbs);
91
6756ae4b 92void __rtnl_unlock(void)
1da177e4 93{
1b5c5493
ED
94 struct sk_buff *head = defer_kfree_skb_list;
95
96 defer_kfree_skb_list = NULL;
97
6756ae4b 98 mutex_unlock(&rtnl_mutex);
1b5c5493
ED
99
100 while (head) {
101 struct sk_buff *next = head->next;
102
103 kfree_skb(head);
104 cond_resched();
105 head = next;
106 }
1da177e4 107}
6756ae4b 108
1da177e4
LT
109void rtnl_unlock(void)
110{
58ec3b4d 111 /* This fellow will unlock it for us. */
1da177e4
LT
112 netdev_run_todo();
113}
e0d087af 114EXPORT_SYMBOL(rtnl_unlock);
1da177e4 115
6756ae4b
SH
116int rtnl_trylock(void)
117{
118 return mutex_trylock(&rtnl_mutex);
119}
e0d087af 120EXPORT_SYMBOL(rtnl_trylock);
6756ae4b 121
c9c1014b
PM
122int rtnl_is_locked(void)
123{
124 return mutex_is_locked(&rtnl_mutex);
125}
e0d087af 126EXPORT_SYMBOL(rtnl_is_locked);
c9c1014b 127
6f99528e
VB
128bool refcount_dec_and_rtnl_lock(refcount_t *r)
129{
130 return refcount_dec_and_mutex_lock(r, &rtnl_mutex);
131}
132EXPORT_SYMBOL(refcount_dec_and_rtnl_lock);
133
a898def2 134#ifdef CONFIG_PROVE_LOCKING
0cbf3343 135bool lockdep_rtnl_is_held(void)
a898def2
PM
136{
137 return lockdep_is_held(&rtnl_mutex);
138}
139EXPORT_SYMBOL(lockdep_rtnl_is_held);
140#endif /* #ifdef CONFIG_PROVE_LOCKING */
141
51e13685 142static struct rtnl_link __rcu *__rcu *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
e2849863
TG
143
144static inline int rtm_msgindex(int msgtype)
145{
146 int msgindex = msgtype - RTM_BASE;
147
148 /*
149 * msgindex < 0 implies someone tried to register a netlink
150 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
151 * the message type has not been added to linux/rtnetlink.h
152 */
153 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
154
155 return msgindex;
156}
157
addf9b90
FW
158static struct rtnl_link *rtnl_get_link(int protocol, int msgtype)
159{
51e13685 160 struct rtnl_link __rcu **tab;
addf9b90
FW
161
162 if (protocol >= ARRAY_SIZE(rtnl_msg_handlers))
163 protocol = PF_UNSPEC;
164
165 tab = rcu_dereference_rtnl(rtnl_msg_handlers[protocol]);
166 if (!tab)
167 tab = rcu_dereference_rtnl(rtnl_msg_handlers[PF_UNSPEC]);
168
51e13685 169 return rcu_dereference_rtnl(tab[msgtype]);
addf9b90
FW
170}
171
e4202511
FW
172static int rtnl_register_internal(struct module *owner,
173 int protocol, int msgtype,
174 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
175 unsigned int flags)
e2849863 176{
b0e9fe1b
FW
177 struct rtnl_link *link, *old;
178 struct rtnl_link __rcu **tab;
e2849863 179 int msgindex;
addf9b90 180 int ret = -ENOBUFS;
e2849863 181
25239cee 182 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
e2849863
TG
183 msgindex = rtm_msgindex(msgtype);
184
addf9b90 185 rtnl_lock();
51e13685 186 tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
e2849863 187 if (tab == NULL) {
addf9b90
FW
188 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(void *), GFP_KERNEL);
189 if (!tab)
190 goto unlock;
e2849863 191
addf9b90 192 /* ensures we see the 0 stores */
6853dd48 193 rcu_assign_pointer(rtnl_msg_handlers[protocol], tab);
e2849863
TG
194 }
195
addf9b90
FW
196 old = rtnl_dereference(tab[msgindex]);
197 if (old) {
198 link = kmemdup(old, sizeof(*old), GFP_KERNEL);
199 if (!link)
200 goto unlock;
201 } else {
202 link = kzalloc(sizeof(*link), GFP_KERNEL);
203 if (!link)
204 goto unlock;
205 }
206
e4202511
FW
207 WARN_ON(link->owner && link->owner != owner);
208 link->owner = owner;
209
addf9b90 210 WARN_ON(doit && link->doit && link->doit != doit);
e2849863 211 if (doit)
addf9b90
FW
212 link->doit = doit;
213 WARN_ON(dumpit && link->dumpit && link->dumpit != dumpit);
e2849863 214 if (dumpit)
addf9b90 215 link->dumpit = dumpit;
e2849863 216
addf9b90
FW
217 link->flags |= flags;
218
219 /* publish protocol:msgtype */
220 rcu_assign_pointer(tab[msgindex], link);
221 ret = 0;
222 if (old)
223 kfree_rcu(old, rcu);
224unlock:
225 rtnl_unlock();
226 return ret;
e2849863 227}
e4202511
FW
228
229/**
230 * rtnl_register_module - Register a rtnetlink message type
231 *
232 * @owner: module registering the hook (THIS_MODULE)
233 * @protocol: Protocol family or PF_UNSPEC
234 * @msgtype: rtnetlink message type
235 * @doit: Function pointer called for each request message
236 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
237 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
238 *
239 * Like rtnl_register, but for use by removable modules.
240 */
241int rtnl_register_module(struct module *owner,
242 int protocol, int msgtype,
243 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
244 unsigned int flags)
245{
246 return rtnl_register_internal(owner, protocol, msgtype,
247 doit, dumpit, flags);
248}
249EXPORT_SYMBOL_GPL(rtnl_register_module);
250
251/**
16feebcf 252 * rtnl_register - Register a rtnetlink message type
e4202511
FW
253 * @protocol: Protocol family or PF_UNSPEC
254 * @msgtype: rtnetlink message type
255 * @doit: Function pointer called for each request message
256 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
257 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
258 *
259 * Registers the specified function pointers (at least one of them has
260 * to be non-NULL) to be called whenever a request message for the
261 * specified protocol family and message type is received.
262 *
263 * The special protocol family PF_UNSPEC may be used to define fallback
264 * function pointers for the case when no entry for the specific protocol
265 * family exists.
e2849863
TG
266 */
267void rtnl_register(int protocol, int msgtype,
c7ac8679 268 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
b97bac64 269 unsigned int flags)
e2849863 270{
16feebcf
FW
271 int err;
272
273 err = rtnl_register_internal(NULL, protocol, msgtype, doit, dumpit,
274 flags);
275 if (err)
276 pr_err("Unable to register rtnetlink message handler, "
277 "protocol = %d, message type = %d\n", protocol, msgtype);
e2849863 278}
e2849863
TG
279
280/**
281 * rtnl_unregister - Unregister a rtnetlink message type
282 * @protocol: Protocol family or PF_UNSPEC
283 * @msgtype: rtnetlink message type
284 *
285 * Returns 0 on success or a negative error code.
286 */
287int rtnl_unregister(int protocol, int msgtype)
288{
51e13685
JK
289 struct rtnl_link __rcu **tab;
290 struct rtnl_link *link;
e2849863
TG
291 int msgindex;
292
25239cee 293 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
e2849863
TG
294 msgindex = rtm_msgindex(msgtype);
295
6853dd48 296 rtnl_lock();
addf9b90
FW
297 tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
298 if (!tab) {
6853dd48 299 rtnl_unlock();
e2849863 300 return -ENOENT;
6853dd48 301 }
e2849863 302
51e13685 303 link = rtnl_dereference(tab[msgindex]);
addf9b90 304 rcu_assign_pointer(tab[msgindex], NULL);
6853dd48 305 rtnl_unlock();
e2849863 306
addf9b90
FW
307 kfree_rcu(link, rcu);
308
e2849863
TG
309 return 0;
310}
e2849863
TG
311EXPORT_SYMBOL_GPL(rtnl_unregister);
312
313/**
314 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
315 * @protocol : Protocol family or PF_UNSPEC
316 *
317 * Identical to calling rtnl_unregster() for all registered message types
318 * of a certain protocol family.
319 */
320void rtnl_unregister_all(int protocol)
321{
51e13685
JK
322 struct rtnl_link __rcu **tab;
323 struct rtnl_link *link;
addf9b90 324 int msgindex;
019a3169 325
25239cee 326 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
e2849863 327
019a3169 328 rtnl_lock();
51e13685 329 tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
f707ef61
SD
330 if (!tab) {
331 rtnl_unlock();
332 return;
333 }
6853dd48 334 RCU_INIT_POINTER(rtnl_msg_handlers[protocol], NULL);
addf9b90 335 for (msgindex = 0; msgindex < RTM_NR_MSGTYPES; msgindex++) {
51e13685 336 link = rtnl_dereference(tab[msgindex]);
addf9b90
FW
337 if (!link)
338 continue;
339
340 rcu_assign_pointer(tab[msgindex], NULL);
341 kfree_rcu(link, rcu);
342 }
019a3169
FW
343 rtnl_unlock();
344
6853dd48
FW
345 synchronize_net();
346
addf9b90 347 kfree(tab);
e2849863 348}
e2849863 349EXPORT_SYMBOL_GPL(rtnl_unregister_all);
1da177e4 350
38f7b870
PM
351static LIST_HEAD(link_ops);
352
c63044f0
ED
353static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
354{
355 const struct rtnl_link_ops *ops;
356
357 list_for_each_entry(ops, &link_ops, list) {
358 if (!strcmp(ops->kind, kind))
359 return ops;
360 }
361 return NULL;
362}
363
38f7b870
PM
364/**
365 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
366 * @ops: struct rtnl_link_ops * to register
367 *
368 * The caller must hold the rtnl_mutex. This function should be used
369 * by drivers that create devices during module initialization. It
370 * must be called before registering the devices.
371 *
372 * Returns 0 on success or a negative error code.
373 */
374int __rtnl_link_register(struct rtnl_link_ops *ops)
375{
c63044f0
ED
376 if (rtnl_link_ops_get(ops->kind))
377 return -EEXIST;
378
b0ab2fab
JP
379 /* The check for setup is here because if ops
380 * does not have that filled up, it is not possible
381 * to use the ops for creating device. So do not
382 * fill up dellink as well. That disables rtnl_dellink.
383 */
384 if (ops->setup && !ops->dellink)
23289a37 385 ops->dellink = unregister_netdevice_queue;
2d85cba2 386
38f7b870
PM
387 list_add_tail(&ops->list, &link_ops);
388 return 0;
389}
38f7b870
PM
390EXPORT_SYMBOL_GPL(__rtnl_link_register);
391
392/**
393 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
394 * @ops: struct rtnl_link_ops * to register
395 *
396 * Returns 0 on success or a negative error code.
397 */
398int rtnl_link_register(struct rtnl_link_ops *ops)
399{
400 int err;
401
ccf8dbcd
KC
402 /* Sanity-check max sizes to avoid stack buffer overflow. */
403 if (WARN_ON(ops->maxtype > RTNL_MAX_TYPE ||
404 ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE))
405 return -EINVAL;
406
38f7b870
PM
407 rtnl_lock();
408 err = __rtnl_link_register(ops);
409 rtnl_unlock();
410 return err;
411}
38f7b870
PM
412EXPORT_SYMBOL_GPL(rtnl_link_register);
413
669f87ba
PE
414static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
415{
416 struct net_device *dev;
23289a37
ED
417 LIST_HEAD(list_kill);
418
669f87ba 419 for_each_netdev(net, dev) {
23289a37
ED
420 if (dev->rtnl_link_ops == ops)
421 ops->dellink(dev, &list_kill);
669f87ba 422 }
23289a37 423 unregister_netdevice_many(&list_kill);
669f87ba
PE
424}
425
38f7b870
PM
426/**
427 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
428 * @ops: struct rtnl_link_ops * to unregister
429 *
554873e5
KT
430 * The caller must hold the rtnl_mutex and guarantee net_namespace_list
431 * integrity (hold pernet_ops_rwsem for writing to close the race
432 * with setup_net() and cleanup_net()).
38f7b870
PM
433 */
434void __rtnl_link_unregister(struct rtnl_link_ops *ops)
435{
881d966b 436 struct net *net;
2d85cba2 437
881d966b 438 for_each_net(net) {
669f87ba 439 __rtnl_kill_links(net, ops);
2d85cba2 440 }
38f7b870
PM
441 list_del(&ops->list);
442}
38f7b870
PM
443EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
444
200b916f
CW
445/* Return with the rtnl_lock held when there are no network
446 * devices unregistering in any network namespace.
447 */
448static void rtnl_lock_unregistering_all(void)
449{
450 struct net *net;
451 bool unregistering;
ff960a73 452 DEFINE_WAIT_FUNC(wait, woken_wake_function);
200b916f 453
ff960a73 454 add_wait_queue(&netdev_unregistering_wq, &wait);
200b916f 455 for (;;) {
200b916f
CW
456 unregistering = false;
457 rtnl_lock();
f0b07bb1
KT
458 /* We held write locked pernet_ops_rwsem, and parallel
459 * setup_net() and cleanup_net() are not possible.
460 */
200b916f
CW
461 for_each_net(net) {
462 if (net->dev_unreg_count > 0) {
463 unregistering = true;
464 break;
465 }
466 }
467 if (!unregistering)
468 break;
469 __rtnl_unlock();
ff960a73
PZ
470
471 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
200b916f 472 }
ff960a73 473 remove_wait_queue(&netdev_unregistering_wq, &wait);
200b916f
CW
474}
475
38f7b870
PM
476/**
477 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
478 * @ops: struct rtnl_link_ops * to unregister
479 */
480void rtnl_link_unregister(struct rtnl_link_ops *ops)
481{
8518e9bb 482 /* Close the race with setup_net() and cleanup_net() */
4420bf21 483 down_write(&pernet_ops_rwsem);
200b916f 484 rtnl_lock_unregistering_all();
38f7b870
PM
485 __rtnl_link_unregister(ops);
486 rtnl_unlock();
4420bf21 487 up_write(&pernet_ops_rwsem);
38f7b870 488}
38f7b870
PM
489EXPORT_SYMBOL_GPL(rtnl_link_unregister);
490
ba7d49b1
JP
491static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
492{
493 struct net_device *master_dev;
494 const struct rtnl_link_ops *ops;
8515ae38 495 size_t size = 0;
ba7d49b1 496
8515ae38
FW
497 rcu_read_lock();
498
499 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
ba7d49b1 500 if (!master_dev)
8515ae38
FW
501 goto out;
502
ba7d49b1 503 ops = master_dev->rtnl_link_ops;
6049f253 504 if (!ops || !ops->get_slave_size)
8515ae38 505 goto out;
ba7d49b1 506 /* IFLA_INFO_SLAVE_DATA + nested data */
8515ae38 507 size = nla_total_size(sizeof(struct nlattr)) +
ba7d49b1 508 ops->get_slave_size(master_dev, dev);
8515ae38
FW
509
510out:
511 rcu_read_unlock();
512 return size;
ba7d49b1
JP
513}
514
38f7b870
PM
515static size_t rtnl_link_get_size(const struct net_device *dev)
516{
517 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
518 size_t size;
519
520 if (!ops)
521 return 0;
522
369cf77a
TG
523 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
524 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
38f7b870
PM
525
526 if (ops->get_size)
527 /* IFLA_INFO_DATA + nested data */
369cf77a 528 size += nla_total_size(sizeof(struct nlattr)) +
38f7b870
PM
529 ops->get_size(dev);
530
531 if (ops->get_xstats_size)
369cf77a
TG
532 /* IFLA_INFO_XSTATS */
533 size += nla_total_size(ops->get_xstats_size(dev));
38f7b870 534
ba7d49b1
JP
535 size += rtnl_link_get_slave_info_data_size(dev);
536
38f7b870
PM
537 return size;
538}
539
f8ff182c
TG
540static LIST_HEAD(rtnl_af_ops);
541
542static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
543{
544 const struct rtnl_af_ops *ops;
545
5fa85a09 546 list_for_each_entry_rcu(ops, &rtnl_af_ops, list) {
f8ff182c
TG
547 if (ops->family == family)
548 return ops;
549 }
550
551 return NULL;
552}
553
f8ff182c
TG
554/**
555 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
556 * @ops: struct rtnl_af_ops * to register
557 *
558 * Returns 0 on success or a negative error code.
559 */
3678a9d8 560void rtnl_af_register(struct rtnl_af_ops *ops)
f8ff182c 561{
f8ff182c 562 rtnl_lock();
5fa85a09 563 list_add_tail_rcu(&ops->list, &rtnl_af_ops);
f8ff182c 564 rtnl_unlock();
f8ff182c
TG
565}
566EXPORT_SYMBOL_GPL(rtnl_af_register);
567
f8ff182c
TG
568/**
569 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
570 * @ops: struct rtnl_af_ops * to unregister
571 */
572void rtnl_af_unregister(struct rtnl_af_ops *ops)
573{
574 rtnl_lock();
5fa85a09 575 list_del_rcu(&ops->list);
f8ff182c 576 rtnl_unlock();
5fa85a09
FW
577
578 synchronize_rcu();
f8ff182c
TG
579}
580EXPORT_SYMBOL_GPL(rtnl_af_unregister);
581
b1974ed0
AR
582static size_t rtnl_link_get_af_size(const struct net_device *dev,
583 u32 ext_filter_mask)
f8ff182c
TG
584{
585 struct rtnl_af_ops *af_ops;
586 size_t size;
587
588 /* IFLA_AF_SPEC */
589 size = nla_total_size(sizeof(struct nlattr));
590
5fa85a09
FW
591 rcu_read_lock();
592 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
f8ff182c
TG
593 if (af_ops->get_link_af_size) {
594 /* AF_* + nested data */
595 size += nla_total_size(sizeof(struct nlattr)) +
b1974ed0 596 af_ops->get_link_af_size(dev, ext_filter_mask);
f8ff182c
TG
597 }
598 }
5fa85a09 599 rcu_read_unlock();
f8ff182c
TG
600
601 return size;
602}
603
ba7d49b1 604static bool rtnl_have_link_slave_info(const struct net_device *dev)
38f7b870 605{
ba7d49b1 606 struct net_device *master_dev;
4c82a95e 607 bool ret = false;
38f7b870 608
4c82a95e
FW
609 rcu_read_lock();
610
611 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
813f020c 612 if (master_dev && master_dev->rtnl_link_ops)
4c82a95e
FW
613 ret = true;
614 rcu_read_unlock();
615 return ret;
ba7d49b1
JP
616}
617
618static int rtnl_link_slave_info_fill(struct sk_buff *skb,
619 const struct net_device *dev)
620{
621 struct net_device *master_dev;
622 const struct rtnl_link_ops *ops;
623 struct nlattr *slave_data;
624 int err;
38f7b870 625
ba7d49b1
JP
626 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
627 if (!master_dev)
628 return 0;
629 ops = master_dev->rtnl_link_ops;
630 if (!ops)
631 return 0;
632 if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
633 return -EMSGSIZE;
634 if (ops->fill_slave_info) {
ae0be8de 635 slave_data = nla_nest_start_noflag(skb, IFLA_INFO_SLAVE_DATA);
ba7d49b1
JP
636 if (!slave_data)
637 return -EMSGSIZE;
638 err = ops->fill_slave_info(skb, master_dev, dev);
639 if (err < 0)
640 goto err_cancel_slave_data;
641 nla_nest_end(skb, slave_data);
642 }
643 return 0;
644
645err_cancel_slave_data:
646 nla_nest_cancel(skb, slave_data);
647 return err;
648}
649
650static int rtnl_link_info_fill(struct sk_buff *skb,
651 const struct net_device *dev)
652{
653 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
654 struct nlattr *data;
655 int err;
656
657 if (!ops)
658 return 0;
38f7b870 659 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
ba7d49b1 660 return -EMSGSIZE;
38f7b870
PM
661 if (ops->fill_xstats) {
662 err = ops->fill_xstats(skb, dev);
663 if (err < 0)
ba7d49b1 664 return err;
38f7b870
PM
665 }
666 if (ops->fill_info) {
ae0be8de 667 data = nla_nest_start_noflag(skb, IFLA_INFO_DATA);
ba7d49b1
JP
668 if (data == NULL)
669 return -EMSGSIZE;
38f7b870
PM
670 err = ops->fill_info(skb, dev);
671 if (err < 0)
672 goto err_cancel_data;
673 nla_nest_end(skb, data);
674 }
38f7b870
PM
675 return 0;
676
677err_cancel_data:
678 nla_nest_cancel(skb, data);
ba7d49b1
JP
679 return err;
680}
681
682static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
683{
684 struct nlattr *linkinfo;
685 int err = -EMSGSIZE;
686
ae0be8de 687 linkinfo = nla_nest_start_noflag(skb, IFLA_LINKINFO);
ba7d49b1
JP
688 if (linkinfo == NULL)
689 goto out;
690
691 err = rtnl_link_info_fill(skb, dev);
692 if (err < 0)
693 goto err_cancel_link;
694
695 err = rtnl_link_slave_info_fill(skb, dev);
696 if (err < 0)
697 goto err_cancel_link;
698
699 nla_nest_end(skb, linkinfo);
700 return 0;
701
38f7b870
PM
702err_cancel_link:
703 nla_nest_cancel(skb, linkinfo);
704out:
705 return err;
706}
707
95c96174 708int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
1da177e4 709{
97c53cac 710 struct sock *rtnl = net->rtnl;
1da177e4
LT
711 int err = 0;
712
ac6d439d 713 NETLINK_CB(skb).dst_group = group;
1da177e4 714 if (echo)
63354797 715 refcount_inc(&skb->users);
1da177e4
LT
716 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
717 if (echo)
718 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
719 return err;
720}
721
97c53cac 722int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
2942e900 723{
97c53cac
DL
724 struct sock *rtnl = net->rtnl;
725
2942e900
TG
726 return nlmsg_unicast(rtnl, skb, pid);
727}
e0d087af 728EXPORT_SYMBOL(rtnl_unicast);
2942e900 729
1ce85fe4
PNA
730void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
731 struct nlmsghdr *nlh, gfp_t flags)
97676b6b 732{
97c53cac 733 struct sock *rtnl = net->rtnl;
97676b6b
TG
734 int report = 0;
735
736 if (nlh)
737 report = nlmsg_report(nlh);
738
1ce85fe4 739 nlmsg_notify(rtnl, skb, pid, group, report, flags);
97676b6b 740}
e0d087af 741EXPORT_SYMBOL(rtnl_notify);
97676b6b 742
97c53cac 743void rtnl_set_sk_err(struct net *net, u32 group, int error)
97676b6b 744{
97c53cac
DL
745 struct sock *rtnl = net->rtnl;
746
97676b6b
TG
747 netlink_set_err(rtnl, 0, group, error);
748}
e0d087af 749EXPORT_SYMBOL(rtnl_set_sk_err);
97676b6b 750
1da177e4
LT
751int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
752{
2d7202bf
TG
753 struct nlattr *mx;
754 int i, valid = 0;
755
c22a133a
DA
756 /* nothing is dumped for dst_default_metrics, so just skip the loop */
757 if (metrics == dst_default_metrics.metrics)
758 return 0;
759
ae0be8de 760 mx = nla_nest_start_noflag(skb, RTA_METRICS);
2d7202bf
TG
761 if (mx == NULL)
762 return -ENOBUFS;
763
764 for (i = 0; i < RTAX_MAX; i++) {
765 if (metrics[i]) {
ea697639
DB
766 if (i == RTAX_CC_ALGO - 1) {
767 char tmp[TCP_CA_NAME_MAX], *name;
768
769 name = tcp_ca_get_name_by_key(metrics[i], tmp);
770 if (!name)
771 continue;
772 if (nla_put_string(skb, i + 1, name))
773 goto nla_put_failure;
c3a8d947
DB
774 } else if (i == RTAX_FEATURES - 1) {
775 u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
776
f8edcd12
PS
777 if (!user_features)
778 continue;
c3a8d947
DB
779 BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
780 if (nla_put_u32(skb, i + 1, user_features))
781 goto nla_put_failure;
ea697639
DB
782 } else {
783 if (nla_put_u32(skb, i + 1, metrics[i]))
784 goto nla_put_failure;
785 }
2d7202bf 786 valid++;
2d7202bf 787 }
1da177e4 788 }
1da177e4 789
a57d27fc
DM
790 if (!valid) {
791 nla_nest_cancel(skb, mx);
792 return 0;
793 }
2d7202bf
TG
794
795 return nla_nest_end(skb, mx);
796
797nla_put_failure:
bc3ed28c
TG
798 nla_nest_cancel(skb, mx);
799 return -EMSGSIZE;
1da177e4 800}
e0d087af 801EXPORT_SYMBOL(rtnetlink_put_metrics);
1da177e4 802
e3703b3d 803int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
87a50699 804 long expires, u32 error)
e3703b3d
TG
805{
806 struct rta_cacheinfo ci = {
e3703b3d
TG
807 .rta_error = error,
808 .rta_id = id,
e3703b3d
TG
809 };
810
3940746d
DA
811 if (dst) {
812 ci.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse);
813 ci.rta_used = dst->__use;
814 ci.rta_clntref = atomic_read(&dst->__refcnt);
815 }
8253947e
LW
816 if (expires) {
817 unsigned long clock;
e3703b3d 818
8253947e
LW
819 clock = jiffies_to_clock_t(abs(expires));
820 clock = min_t(unsigned long, clock, INT_MAX);
821 ci.rta_expires = (expires > 0) ? clock : -clock;
822 }
e3703b3d
TG
823 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
824}
e3703b3d 825EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
1da177e4 826
93b2d4a2 827static void set_operstate(struct net_device *dev, unsigned char transition)
b00055aa
SR
828{
829 unsigned char operstate = dev->operstate;
830
e0d087af 831 switch (transition) {
b00055aa
SR
832 case IF_OPER_UP:
833 if ((operstate == IF_OPER_DORMANT ||
eec517cd 834 operstate == IF_OPER_TESTING ||
b00055aa 835 operstate == IF_OPER_UNKNOWN) &&
eec517cd 836 !netif_dormant(dev) && !netif_testing(dev))
b00055aa
SR
837 operstate = IF_OPER_UP;
838 break;
839
eec517cd
AL
840 case IF_OPER_TESTING:
841 if (operstate == IF_OPER_UP ||
842 operstate == IF_OPER_UNKNOWN)
843 operstate = IF_OPER_TESTING;
844 break;
845
b00055aa
SR
846 case IF_OPER_DORMANT:
847 if (operstate == IF_OPER_UP ||
848 operstate == IF_OPER_UNKNOWN)
849 operstate = IF_OPER_DORMANT;
850 break;
3ff50b79 851 }
b00055aa
SR
852
853 if (dev->operstate != operstate) {
854 write_lock_bh(&dev_base_lock);
855 dev->operstate = operstate;
856 write_unlock_bh(&dev_base_lock);
93b2d4a2
DM
857 netdev_state_change(dev);
858 }
b00055aa
SR
859}
860
b1beb681
JB
861static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
862{
863 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
864 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
865}
866
3729d502
PM
867static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
868 const struct ifinfomsg *ifm)
869{
870 unsigned int flags = ifm->ifi_flags;
871
872 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
873 if (ifm->ifi_change)
874 flags = (flags & ifm->ifi_change) |
b1beb681 875 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
3729d502
PM
876
877 return flags;
878}
879
b60c5115 880static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
be1f3c2c 881 const struct rtnl_link_stats64 *b)
1da177e4 882{
b60c5115
TG
883 a->rx_packets = b->rx_packets;
884 a->tx_packets = b->tx_packets;
885 a->rx_bytes = b->rx_bytes;
886 a->tx_bytes = b->tx_bytes;
887 a->rx_errors = b->rx_errors;
888 a->tx_errors = b->tx_errors;
889 a->rx_dropped = b->rx_dropped;
890 a->tx_dropped = b->tx_dropped;
891
892 a->multicast = b->multicast;
893 a->collisions = b->collisions;
894
895 a->rx_length_errors = b->rx_length_errors;
896 a->rx_over_errors = b->rx_over_errors;
897 a->rx_crc_errors = b->rx_crc_errors;
898 a->rx_frame_errors = b->rx_frame_errors;
899 a->rx_fifo_errors = b->rx_fifo_errors;
900 a->rx_missed_errors = b->rx_missed_errors;
901
902 a->tx_aborted_errors = b->tx_aborted_errors;
903 a->tx_carrier_errors = b->tx_carrier_errors;
904 a->tx_fifo_errors = b->tx_fifo_errors;
905 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
906 a->tx_window_errors = b->tx_window_errors;
907
908 a->rx_compressed = b->rx_compressed;
909 a->tx_compressed = b->tx_compressed;
6e7333d3
JW
910
911 a->rx_nohandler = b->rx_nohandler;
10708f37
JE
912}
913
c02db8c6 914/* All VF info */
115c9b81
GR
915static inline int rtnl_vfinfo_size(const struct net_device *dev,
916 u32 ext_filter_mask)
ebc08a6f 917{
9af15c38 918 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
c02db8c6 919 int num_vfs = dev_num_vf(dev->dev.parent);
7e75f74a 920 size_t size = nla_total_size(0);
045de01a 921 size += num_vfs *
7e75f74a
SD
922 (nla_total_size(0) +
923 nla_total_size(sizeof(struct ifla_vf_mac)) +
75345f88 924 nla_total_size(sizeof(struct ifla_vf_broadcast)) +
7e75f74a
SD
925 nla_total_size(sizeof(struct ifla_vf_vlan)) +
926 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
79aab093
MS
927 nla_total_size(MAX_VLAN_LIST_LEN *
928 sizeof(struct ifla_vf_vlan_info)) +
ed616689 929 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
7e75f74a 930 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
945a3676 931 nla_total_size(sizeof(struct ifla_vf_rate)) +
01a3d796 932 nla_total_size(sizeof(struct ifla_vf_link_state)) +
3b766cd8 933 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
7e75f74a 934 nla_total_size(0) + /* nest IFLA_VF_STATS */
3b766cd8 935 /* IFLA_VF_STATS_RX_PACKETS */
343a6d8e 936 nla_total_size_64bit(sizeof(__u64)) +
3b766cd8 937 /* IFLA_VF_STATS_TX_PACKETS */
343a6d8e 938 nla_total_size_64bit(sizeof(__u64)) +
3b766cd8 939 /* IFLA_VF_STATS_RX_BYTES */
343a6d8e 940 nla_total_size_64bit(sizeof(__u64)) +
3b766cd8 941 /* IFLA_VF_STATS_TX_BYTES */
343a6d8e 942 nla_total_size_64bit(sizeof(__u64)) +
3b766cd8 943 /* IFLA_VF_STATS_BROADCAST */
343a6d8e 944 nla_total_size_64bit(sizeof(__u64)) +
3b766cd8 945 /* IFLA_VF_STATS_MULTICAST */
343a6d8e 946 nla_total_size_64bit(sizeof(__u64)) +
c5a9f6f0
EE
947 /* IFLA_VF_STATS_RX_DROPPED */
948 nla_total_size_64bit(sizeof(__u64)) +
949 /* IFLA_VF_STATS_TX_DROPPED */
950 nla_total_size_64bit(sizeof(__u64)) +
dd461d6a 951 nla_total_size(sizeof(struct ifla_vf_trust)));
c02db8c6
CW
952 return size;
953 } else
ebc08a6f
WM
954 return 0;
955}
956
c53864fd
DG
957static size_t rtnl_port_size(const struct net_device *dev,
958 u32 ext_filter_mask)
57b61080
SF
959{
960 size_t port_size = nla_total_size(4) /* PORT_VF */
961 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
57b61080
SF
962 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
963 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
964 + nla_total_size(1) /* PROT_VDP_REQUEST */
965 + nla_total_size(2); /* PORT_VDP_RESPONSE */
966 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
967 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
968 + port_size;
969 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
970 + port_size;
971
c53864fd
DG
972 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
973 !(ext_filter_mask & RTEXT_FILTER_VF))
57b61080
SF
974 return 0;
975 if (dev_num_vf(dev->dev.parent))
976 return port_self_size + vf_ports_size +
977 vf_port_size * dev_num_vf(dev->dev.parent);
978 else
979 return port_self_size;
980}
981
b5cdae32 982static size_t rtnl_xdp_size(void)
d1fdd913 983{
b3cfaa31 984 size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */
58038695 985 nla_total_size(1) + /* XDP_ATTACHED */
a25717d2 986 nla_total_size(4) + /* XDP_PROG_ID (or 1st mode) */
4f91da26 987 nla_total_size(4); /* XDP_<mode>_PROG_ID */
d1fdd913 988
b5cdae32 989 return xdp_size;
d1fdd913
BB
990}
991
88f4fb0c
JP
992static size_t rtnl_prop_list_size(const struct net_device *dev)
993{
994 struct netdev_name_node *name_node;
995 size_t size;
996
997 if (list_empty(&dev->name_node->list))
998 return 0;
999 size = nla_total_size(0);
1000 list_for_each_entry(name_node, &dev->name_node->list, list)
1001 size += nla_total_size(ALTIFNAMSIZ);
1002 return size;
1003}
1004
829eb208
RP
1005static size_t rtnl_proto_down_size(const struct net_device *dev)
1006{
1007 size_t size = nla_total_size(1);
1008
1009 if (dev->proto_down_reason)
1010 size += nla_total_size(0) + nla_total_size(4);
1011
1012 return size;
1013}
1014
115c9b81
GR
1015static noinline size_t if_nlmsg_size(const struct net_device *dev,
1016 u32 ext_filter_mask)
339bf98f
TG
1017{
1018 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
1019 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
0b815a1a 1020 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
339bf98f 1021 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
270cb4d0 1022 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
339bf98f 1023 + nla_total_size(sizeof(struct rtnl_link_stats))
35c58459 1024 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
339bf98f
TG
1025 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
1026 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
1027 + nla_total_size(4) /* IFLA_TXQLEN */
1028 + nla_total_size(4) /* IFLA_WEIGHT */
1029 + nla_total_size(4) /* IFLA_MTU */
1030 + nla_total_size(4) /* IFLA_LINK */
1031 + nla_total_size(4) /* IFLA_MASTER */
9a57247f 1032 + nla_total_size(1) /* IFLA_CARRIER */
edbc0bb3 1033 + nla_total_size(4) /* IFLA_PROMISCUITY */
76ff5cc9
JP
1034 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
1035 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
6919756c
TK
1036 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
1037 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
339bf98f 1038 + nla_total_size(1) /* IFLA_OPERSTATE */
38f7b870 1039 + nla_total_size(1) /* IFLA_LINKMODE */
2d3b479d 1040 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
d37512a2 1041 + nla_total_size(4) /* IFLA_LINK_NETNSID */
db833d40 1042 + nla_total_size(4) /* IFLA_GROUP */
115c9b81
GR
1043 + nla_total_size(ext_filter_mask
1044 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
1045 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
c53864fd 1046 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
f8ff182c 1047 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
b1974ed0 1048 + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
82f28412 1049 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
88d6378b 1050 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
c57c7a95 1051 + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
b5cdae32 1052 + rtnl_xdp_size() /* IFLA_XDP */
3d3ea5af 1053 + nla_total_size(4) /* IFLA_EVENT */
6621dd29 1054 + nla_total_size(4) /* IFLA_NEW_NETNSID */
38e01b30 1055 + nla_total_size(4) /* IFLA_NEW_IFINDEX */
829eb208 1056 + rtnl_proto_down_size(dev) /* proto down */
7e4a8d5a 1057 + nla_total_size(4) /* IFLA_TARGET_NETNSID */
b2d3bcfa
DD
1058 + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */
1059 + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */
3e7a50ce
SH
1060 + nla_total_size(4) /* IFLA_MIN_MTU */
1061 + nla_total_size(4) /* IFLA_MAX_MTU */
88f4fb0c 1062 + rtnl_prop_list_size(dev)
f74877a5 1063 + nla_total_size(MAX_ADDR_LEN) /* IFLA_PERM_ADDRESS */
79e1ad14 1064 + 0;
339bf98f
TG
1065}
1066
57b61080
SF
1067static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
1068{
1069 struct nlattr *vf_ports;
1070 struct nlattr *vf_port;
1071 int vf;
1072 int err;
1073
ae0be8de 1074 vf_ports = nla_nest_start_noflag(skb, IFLA_VF_PORTS);
57b61080
SF
1075 if (!vf_ports)
1076 return -EMSGSIZE;
1077
1078 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
ae0be8de 1079 vf_port = nla_nest_start_noflag(skb, IFLA_VF_PORT);
8ca94183
SF
1080 if (!vf_port)
1081 goto nla_put_failure;
a6574349
DM
1082 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
1083 goto nla_put_failure;
57b61080 1084 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
8ca94183
SF
1085 if (err == -EMSGSIZE)
1086 goto nla_put_failure;
57b61080 1087 if (err) {
57b61080
SF
1088 nla_nest_cancel(skb, vf_port);
1089 continue;
1090 }
1091 nla_nest_end(skb, vf_port);
1092 }
1093
1094 nla_nest_end(skb, vf_ports);
1095
1096 return 0;
8ca94183
SF
1097
1098nla_put_failure:
1099 nla_nest_cancel(skb, vf_ports);
1100 return -EMSGSIZE;
57b61080
SF
1101}
1102
1103static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
1104{
1105 struct nlattr *port_self;
1106 int err;
1107
ae0be8de 1108 port_self = nla_nest_start_noflag(skb, IFLA_PORT_SELF);
57b61080
SF
1109 if (!port_self)
1110 return -EMSGSIZE;
1111
1112 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
1113 if (err) {
1114 nla_nest_cancel(skb, port_self);
8ca94183 1115 return (err == -EMSGSIZE) ? err : 0;
57b61080
SF
1116 }
1117
1118 nla_nest_end(skb, port_self);
1119
1120 return 0;
1121}
1122
c53864fd
DG
1123static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1124 u32 ext_filter_mask)
57b61080
SF
1125{
1126 int err;
1127
c53864fd
DG
1128 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1129 !(ext_filter_mask & RTEXT_FILTER_VF))
57b61080
SF
1130 return 0;
1131
1132 err = rtnl_port_self_fill(skb, dev);
1133 if (err)
1134 return err;
1135
1136 if (dev_num_vf(dev->dev.parent)) {
1137 err = rtnl_vf_ports_fill(skb, dev);
1138 if (err)
1139 return err;
1140 }
1141
1142 return 0;
1143}
1144
66cae9ed
JP
1145static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
1146{
1147 int err;
02637fce 1148 struct netdev_phys_item_id ppid;
66cae9ed
JP
1149
1150 err = dev_get_phys_port_id(dev, &ppid);
1151 if (err) {
1152 if (err == -EOPNOTSUPP)
1153 return 0;
1154 return err;
1155 }
1156
1157 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1158 return -EMSGSIZE;
1159
1160 return 0;
1161}
1162
db24a904
DA
1163static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1164{
1165 char name[IFNAMSIZ];
1166 int err;
1167
1168 err = dev_get_phys_port_name(dev, name, sizeof(name));
1169 if (err) {
1170 if (err == -EOPNOTSUPP)
1171 return 0;
1172 return err;
1173 }
1174
77ef033b 1175 if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
db24a904
DA
1176 return -EMSGSIZE;
1177
1178 return 0;
1179}
1180
82f28412
JP
1181static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1182{
bccb3025 1183 struct netdev_phys_item_id ppid = { };
82f28412 1184 int err;
82f28412 1185
bccb3025 1186 err = dev_get_port_parent_id(dev, &ppid, false);
82f28412
JP
1187 if (err) {
1188 if (err == -EOPNOTSUPP)
1189 return 0;
1190 return err;
1191 }
1192
bccb3025 1193 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, ppid.id_len, ppid.id))
82f28412
JP
1194 return -EMSGSIZE;
1195
1196 return 0;
1197}
1198
b22b941b
HFS
1199static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1200 struct net_device *dev)
1201{
550bce59 1202 struct rtnl_link_stats64 *sp;
b22b941b 1203 struct nlattr *attr;
18402843 1204
58414d32
ND
1205 attr = nla_reserve_64bit(skb, IFLA_STATS64,
1206 sizeof(struct rtnl_link_stats64), IFLA_PAD);
b22b941b
HFS
1207 if (!attr)
1208 return -EMSGSIZE;
1209
550bce59
RP
1210 sp = nla_data(attr);
1211 dev_get_stats(dev, sp);
b22b941b 1212
550bce59
RP
1213 attr = nla_reserve(skb, IFLA_STATS,
1214 sizeof(struct rtnl_link_stats));
b22b941b
HFS
1215 if (!attr)
1216 return -EMSGSIZE;
1217
550bce59 1218 copy_rtnl_link_stats(nla_data(attr), sp);
b22b941b
HFS
1219
1220 return 0;
1221}
1222
1223static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1224 struct net_device *dev,
1225 int vfs_num,
1226 struct nlattr *vfinfo)
1227{
1228 struct ifla_vf_rss_query_en vf_rss_query_en;
79aab093 1229 struct nlattr *vf, *vfstats, *vfvlanlist;
b22b941b 1230 struct ifla_vf_link_state vf_linkstate;
79aab093 1231 struct ifla_vf_vlan_info vf_vlan_info;
b22b941b
HFS
1232 struct ifla_vf_spoofchk vf_spoofchk;
1233 struct ifla_vf_tx_rate vf_tx_rate;
1234 struct ifla_vf_stats vf_stats;
1235 struct ifla_vf_trust vf_trust;
1236 struct ifla_vf_vlan vf_vlan;
1237 struct ifla_vf_rate vf_rate;
b22b941b 1238 struct ifla_vf_mac vf_mac;
75345f88 1239 struct ifla_vf_broadcast vf_broadcast;
b22b941b 1240 struct ifla_vf_info ivi;
30aad417
DG
1241 struct ifla_vf_guid node_guid;
1242 struct ifla_vf_guid port_guid;
b22b941b 1243
0eed9cf5
MY
1244 memset(&ivi, 0, sizeof(ivi));
1245
b22b941b
HFS
1246 /* Not all SR-IOV capable drivers support the
1247 * spoofcheck and "RSS query enable" query. Preset to
1248 * -1 so the user space tool can detect that the driver
1249 * didn't report anything.
1250 */
1251 ivi.spoofchk = -1;
1252 ivi.rss_query_en = -1;
1253 ivi.trusted = -1;
b22b941b
HFS
1254 /* The default value for VF link state is "auto"
1255 * IFLA_VF_LINK_STATE_AUTO which equals zero
1256 */
1257 ivi.linkstate = 0;
79aab093
MS
1258 /* VLAN Protocol by default is 802.1Q */
1259 ivi.vlan_proto = htons(ETH_P_8021Q);
b22b941b
HFS
1260 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1261 return 0;
1262
775f4f05 1263 memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
9fbf082f
LR
1264 memset(&node_guid, 0, sizeof(node_guid));
1265 memset(&port_guid, 0, sizeof(port_guid));
775f4f05 1266
b22b941b
HFS
1267 vf_mac.vf =
1268 vf_vlan.vf =
79aab093 1269 vf_vlan_info.vf =
b22b941b
HFS
1270 vf_rate.vf =
1271 vf_tx_rate.vf =
1272 vf_spoofchk.vf =
1273 vf_linkstate.vf =
1274 vf_rss_query_en.vf =
9aed6ae0
DG
1275 vf_trust.vf =
1276 node_guid.vf =
1277 port_guid.vf = ivi.vf;
b22b941b
HFS
1278
1279 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
75345f88 1280 memcpy(vf_broadcast.broadcast, dev->broadcast, dev->addr_len);
b22b941b
HFS
1281 vf_vlan.vlan = ivi.vlan;
1282 vf_vlan.qos = ivi.qos;
79aab093
MS
1283 vf_vlan_info.vlan = ivi.vlan;
1284 vf_vlan_info.qos = ivi.qos;
1285 vf_vlan_info.vlan_proto = ivi.vlan_proto;
b22b941b
HFS
1286 vf_tx_rate.rate = ivi.max_tx_rate;
1287 vf_rate.min_tx_rate = ivi.min_tx_rate;
1288 vf_rate.max_tx_rate = ivi.max_tx_rate;
1289 vf_spoofchk.setting = ivi.spoofchk;
1290 vf_linkstate.link_state = ivi.linkstate;
1291 vf_rss_query_en.setting = ivi.rss_query_en;
1292 vf_trust.setting = ivi.trusted;
ae0be8de 1293 vf = nla_nest_start_noflag(skb, IFLA_VF_INFO);
79aab093
MS
1294 if (!vf)
1295 goto nla_put_vfinfo_failure;
b22b941b 1296 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
75345f88 1297 nla_put(skb, IFLA_VF_BROADCAST, sizeof(vf_broadcast), &vf_broadcast) ||
b22b941b
HFS
1298 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1299 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1300 &vf_rate) ||
1301 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1302 &vf_tx_rate) ||
1303 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1304 &vf_spoofchk) ||
1305 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1306 &vf_linkstate) ||
1307 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1308 sizeof(vf_rss_query_en),
1309 &vf_rss_query_en) ||
1310 nla_put(skb, IFLA_VF_TRUST,
1311 sizeof(vf_trust), &vf_trust))
79aab093 1312 goto nla_put_vf_failure;
30aad417 1313
30aad417
DG
1314 if (dev->netdev_ops->ndo_get_vf_guid &&
1315 !dev->netdev_ops->ndo_get_vf_guid(dev, vfs_num, &node_guid,
1316 &port_guid)) {
1317 if (nla_put(skb, IFLA_VF_IB_NODE_GUID, sizeof(node_guid),
1318 &node_guid) ||
1319 nla_put(skb, IFLA_VF_IB_PORT_GUID, sizeof(port_guid),
1320 &port_guid))
1321 goto nla_put_vf_failure;
1322 }
ae0be8de 1323 vfvlanlist = nla_nest_start_noflag(skb, IFLA_VF_VLAN_LIST);
79aab093
MS
1324 if (!vfvlanlist)
1325 goto nla_put_vf_failure;
1326 if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
1327 &vf_vlan_info)) {
1328 nla_nest_cancel(skb, vfvlanlist);
1329 goto nla_put_vf_failure;
1330 }
1331 nla_nest_end(skb, vfvlanlist);
b22b941b
HFS
1332 memset(&vf_stats, 0, sizeof(vf_stats));
1333 if (dev->netdev_ops->ndo_get_vf_stats)
1334 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1335 &vf_stats);
ae0be8de 1336 vfstats = nla_nest_start_noflag(skb, IFLA_VF_STATS);
79aab093
MS
1337 if (!vfstats)
1338 goto nla_put_vf_failure;
343a6d8e
ND
1339 if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1340 vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1341 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1342 vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1343 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1344 vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1345 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1346 vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1347 nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1348 vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1349 nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
c5a9f6f0
EE
1350 vf_stats.multicast, IFLA_VF_STATS_PAD) ||
1351 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED,
1352 vf_stats.rx_dropped, IFLA_VF_STATS_PAD) ||
1353 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED,
1354 vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) {
79aab093
MS
1355 nla_nest_cancel(skb, vfstats);
1356 goto nla_put_vf_failure;
1357 }
b22b941b
HFS
1358 nla_nest_end(skb, vfstats);
1359 nla_nest_end(skb, vf);
1360 return 0;
79aab093
MS
1361
1362nla_put_vf_failure:
1363 nla_nest_cancel(skb, vf);
1364nla_put_vfinfo_failure:
1365 nla_nest_cancel(skb, vfinfo);
1366 return -EMSGSIZE;
b22b941b
HFS
1367}
1368
250fc3df
FW
1369static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb,
1370 struct net_device *dev,
1371 u32 ext_filter_mask)
1372{
1373 struct nlattr *vfinfo;
1374 int i, num_vfs;
1375
1376 if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0))
1377 return 0;
1378
1379 num_vfs = dev_num_vf(dev->dev.parent);
1380 if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs))
1381 return -EMSGSIZE;
1382
1383 if (!dev->netdev_ops->ndo_get_vf_config)
1384 return 0;
1385
ae0be8de 1386 vfinfo = nla_nest_start_noflag(skb, IFLA_VFINFO_LIST);
250fc3df
FW
1387 if (!vfinfo)
1388 return -EMSGSIZE;
1389
1390 for (i = 0; i < num_vfs; i++) {
1391 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1392 return -EMSGSIZE;
1393 }
1394
1395 nla_nest_end(skb, vfinfo);
1396 return 0;
1397}
1398
b22b941b
HFS
1399static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1400{
5f8e4474
KL
1401 struct rtnl_link_ifmap map;
1402
1403 memset(&map, 0, sizeof(map));
1404 map.mem_start = dev->mem_start;
1405 map.mem_end = dev->mem_end;
1406 map.base_addr = dev->base_addr;
1407 map.irq = dev->irq;
1408 map.dma = dev->dma;
1409 map.port = dev->if_port;
1410
270cb4d0 1411 if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
b22b941b
HFS
1412 return -EMSGSIZE;
1413
1414 return 0;
1415}
1416
a25717d2 1417static u32 rtnl_xdp_prog_skb(struct net_device *dev)
d67b9cd2 1418{
58038695 1419 const struct bpf_prog *generic_xdp_prog;
d67b9cd2
DB
1420
1421 ASSERT_RTNL();
1422
58038695 1423 generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
a25717d2
JK
1424 if (!generic_xdp_prog)
1425 return 0;
1426 return generic_xdp_prog->aux->id;
1427}
d67b9cd2 1428
a25717d2
JK
1429static u32 rtnl_xdp_prog_drv(struct net_device *dev)
1430{
7f0a8382 1431 return dev_xdp_prog_id(dev, XDP_MODE_DRV);
a25717d2 1432}
118b4aa2 1433
a25717d2
JK
1434static u32 rtnl_xdp_prog_hw(struct net_device *dev)
1435{
7f0a8382 1436 return dev_xdp_prog_id(dev, XDP_MODE_HW);
a25717d2
JK
1437}
1438
1439static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev,
1440 u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr,
1441 u32 (*get_prog_id)(struct net_device *dev))
1442{
1443 u32 curr_id;
1444 int err;
1445
1446 curr_id = get_prog_id(dev);
1447 if (!curr_id)
1448 return 0;
1449
1450 *prog_id = curr_id;
1451 err = nla_put_u32(skb, attr, curr_id);
1452 if (err)
1453 return err;
d67b9cd2 1454
a25717d2
JK
1455 if (*mode != XDP_ATTACHED_NONE)
1456 *mode = XDP_ATTACHED_MULTI;
1457 else
1458 *mode = tgt_mode;
118b4aa2 1459
a25717d2 1460 return 0;
d67b9cd2
DB
1461}
1462
d1fdd913
BB
1463static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1464{
d1fdd913 1465 struct nlattr *xdp;
58038695 1466 u32 prog_id;
d1fdd913 1467 int err;
4f91da26 1468 u8 mode;
d1fdd913 1469
ae0be8de 1470 xdp = nla_nest_start_noflag(skb, IFLA_XDP);
d1fdd913
BB
1471 if (!xdp)
1472 return -EMSGSIZE;
d67b9cd2 1473
a25717d2
JK
1474 prog_id = 0;
1475 mode = XDP_ATTACHED_NONE;
202aabe8
JK
1476 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB,
1477 IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb);
1478 if (err)
a25717d2 1479 goto err_cancel;
202aabe8
JK
1480 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV,
1481 IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv);
1482 if (err)
a25717d2 1483 goto err_cancel;
202aabe8
JK
1484 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW,
1485 IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw);
1486 if (err)
a25717d2
JK
1487 goto err_cancel;
1488
4f91da26 1489 err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode);
d1fdd913
BB
1490 if (err)
1491 goto err_cancel;
1492
a25717d2 1493 if (prog_id && mode != XDP_ATTACHED_MULTI) {
58038695
MKL
1494 err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
1495 if (err)
1496 goto err_cancel;
1497 }
1498
d1fdd913
BB
1499 nla_nest_end(skb, xdp);
1500 return 0;
1501
1502err_cancel:
1503 nla_nest_cancel(skb, xdp);
1504 return err;
1505}
1506
3d3ea5af
VY
1507static u32 rtnl_get_event(unsigned long event)
1508{
1509 u32 rtnl_event_type = IFLA_EVENT_NONE;
1510
1511 switch (event) {
1512 case NETDEV_REBOOT:
1513 rtnl_event_type = IFLA_EVENT_REBOOT;
1514 break;
1515 case NETDEV_FEAT_CHANGE:
1516 rtnl_event_type = IFLA_EVENT_FEATURES;
1517 break;
1518 case NETDEV_BONDING_FAILOVER:
1519 rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
1520 break;
1521 case NETDEV_NOTIFY_PEERS:
1522 rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
1523 break;
1524 case NETDEV_RESEND_IGMP:
1525 rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
1526 break;
1527 case NETDEV_CHANGEINFODATA:
1528 rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
1529 break;
1530 default:
1531 break;
1532 }
1533
1534 return rtnl_event_type;
1535}
1536
79110a04
FW
1537static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
1538{
1539 const struct net_device *upper_dev;
1540 int ret = 0;
1541
1542 rcu_read_lock();
1543
1544 upper_dev = netdev_master_upper_dev_get_rcu(dev);
1545 if (upper_dev)
1546 ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);
1547
1548 rcu_read_unlock();
1549 return ret;
1550}
1551
feadc4b6
SD
1552static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev,
1553 bool force)
79110a04
FW
1554{
1555 int ifindex = dev_get_iflink(dev);
1556
feadc4b6
SD
1557 if (force || dev->ifindex != ifindex)
1558 return nla_put_u32(skb, IFLA_LINK, ifindex);
79110a04 1559
feadc4b6 1560 return 0;
79110a04
FW
1561}
1562
6c557001
FW
1563static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
1564 struct net_device *dev)
1565{
1566 char buf[IFALIASZ];
1567 int ret;
1568
1569 ret = dev_get_alias(dev, buf, sizeof(buf));
1570 return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
1571}
1572
b1e66b9a 1573static int rtnl_fill_link_netnsid(struct sk_buff *skb,
79e1ad14 1574 const struct net_device *dev,
d4e4fdf9 1575 struct net *src_net, gfp_t gfp)
b1e66b9a 1576{
feadc4b6
SD
1577 bool put_iflink = false;
1578
b1e66b9a
FW
1579 if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
1580 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1581
1582 if (!net_eq(dev_net(dev), link_net)) {
d4e4fdf9 1583 int id = peernet2id_alloc(src_net, link_net, gfp);
b1e66b9a
FW
1584
1585 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1586 return -EMSGSIZE;
feadc4b6
SD
1587
1588 put_iflink = true;
b1e66b9a
FW
1589 }
1590 }
1591
feadc4b6 1592 return nla_put_iflink(skb, dev, put_iflink);
b1e66b9a
FW
1593}
1594
070cbf5b
FW
1595static int rtnl_fill_link_af(struct sk_buff *skb,
1596 const struct net_device *dev,
1597 u32 ext_filter_mask)
1598{
1599 const struct rtnl_af_ops *af_ops;
1600 struct nlattr *af_spec;
1601
ae0be8de 1602 af_spec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
070cbf5b
FW
1603 if (!af_spec)
1604 return -EMSGSIZE;
1605
5fa85a09 1606 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
070cbf5b
FW
1607 struct nlattr *af;
1608 int err;
1609
1610 if (!af_ops->fill_link_af)
1611 continue;
1612
ae0be8de 1613 af = nla_nest_start_noflag(skb, af_ops->family);
070cbf5b
FW
1614 if (!af)
1615 return -EMSGSIZE;
1616
1617 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1618 /*
1619 * Caller may return ENODATA to indicate that there
1620 * was no data to be dumped. This is not an error, it
1621 * means we should trim the attribute header and
1622 * continue.
1623 */
1624 if (err == -ENODATA)
1625 nla_nest_cancel(skb, af);
1626 else if (err < 0)
1627 return -EMSGSIZE;
1628
1629 nla_nest_end(skb, af);
1630 }
1631
1632 nla_nest_end(skb, af_spec);
1633 return 0;
1634}
1635
88f4fb0c
JP
1636static int rtnl_fill_alt_ifnames(struct sk_buff *skb,
1637 const struct net_device *dev)
1638{
1639 struct netdev_name_node *name_node;
1640 int count = 0;
1641
1642 list_for_each_entry(name_node, &dev->name_node->list, list) {
1643 if (nla_put_string(skb, IFLA_ALT_IFNAME, name_node->name))
1644 return -EMSGSIZE;
1645 count++;
1646 }
1647 return count;
1648}
1649
1650static int rtnl_fill_prop_list(struct sk_buff *skb,
1651 const struct net_device *dev)
1652{
1653 struct nlattr *prop_list;
1654 int ret;
1655
1656 prop_list = nla_nest_start(skb, IFLA_PROP_LIST);
1657 if (!prop_list)
1658 return -EMSGSIZE;
1659
1660 ret = rtnl_fill_alt_ifnames(skb, dev);
1661 if (ret <= 0)
1662 goto nest_cancel;
1663
1664 nla_nest_end(skb, prop_list);
1665 return 0;
1666
1667nest_cancel:
1668 nla_nest_cancel(skb, prop_list);
1669 return ret;
1670}
1671
829eb208
RP
1672static int rtnl_fill_proto_down(struct sk_buff *skb,
1673 const struct net_device *dev)
1674{
1675 struct nlattr *pr;
1676 u32 preason;
1677
1678 if (nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
1679 goto nla_put_failure;
1680
1681 preason = dev->proto_down_reason;
1682 if (!preason)
1683 return 0;
1684
1685 pr = nla_nest_start(skb, IFLA_PROTO_DOWN_REASON);
1686 if (!pr)
1687 return -EMSGSIZE;
1688
1689 if (nla_put_u32(skb, IFLA_PROTO_DOWN_REASON_VALUE, preason)) {
1690 nla_nest_cancel(skb, pr);
1691 goto nla_put_failure;
1692 }
1693
1694 nla_nest_end(skb, pr);
1695 return 0;
1696
1697nla_put_failure:
1698 return -EMSGSIZE;
1699}
1700
79e1ad14
JB
1701static int rtnl_fill_ifinfo(struct sk_buff *skb,
1702 struct net_device *dev, struct net *src_net,
575c3e2a 1703 int type, u32 pid, u32 seq, u32 change,
3d3ea5af 1704 unsigned int flags, u32 ext_filter_mask,
38e01b30 1705 u32 event, int *new_nsid, int new_ifindex,
d4e4fdf9 1706 int tgt_netnsid, gfp_t gfp)
b60c5115
TG
1707{
1708 struct ifinfomsg *ifm;
1709 struct nlmsghdr *nlh;
1da177e4 1710
2907c35f 1711 ASSERT_RTNL();
b60c5115
TG
1712 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1713 if (nlh == NULL)
26932566 1714 return -EMSGSIZE;
1da177e4 1715
b60c5115
TG
1716 ifm = nlmsg_data(nlh);
1717 ifm->ifi_family = AF_UNSPEC;
1718 ifm->__ifi_pad = 0;
1719 ifm->ifi_type = dev->type;
1720 ifm->ifi_index = dev->ifindex;
1721 ifm->ifi_flags = dev_get_flags(dev);
1722 ifm->ifi_change = change;
1723
7e4a8d5a 1724 if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid))
79e1ad14
JB
1725 goto nla_put_failure;
1726
a6574349
DM
1727 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1728 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1729 nla_put_u8(skb, IFLA_OPERSTATE,
1730 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1731 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1732 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
3e7a50ce
SH
1733 nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) ||
1734 nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) ||
a6574349 1735 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
edbc0bb3 1736 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
76ff5cc9 1737 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
c70ce028
ED
1738 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1739 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
1d69c2b3 1740#ifdef CONFIG_RPS
76ff5cc9 1741 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
1d69c2b3 1742#endif
79110a04 1743 put_master_ifindex(skb, dev) ||
9a57247f 1744 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
a6574349
DM
1745 (dev->qdisc &&
1746 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
6c557001 1747 nla_put_ifalias(skb, dev) ||
2d3b479d 1748 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
b2d3bcfa
DD
1749 atomic_read(&dev->carrier_up_count) +
1750 atomic_read(&dev->carrier_down_count)) ||
b2d3bcfa
DD
1751 nla_put_u32(skb, IFLA_CARRIER_UP_COUNT,
1752 atomic_read(&dev->carrier_up_count)) ||
1753 nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT,
1754 atomic_read(&dev->carrier_down_count)))
a6574349 1755 goto nla_put_failure;
0b815a1a 1756
829eb208
RP
1757 if (rtnl_fill_proto_down(skb, dev))
1758 goto nla_put_failure;
1759
3d3ea5af
VY
1760 if (event != IFLA_EVENT_NONE) {
1761 if (nla_put_u32(skb, IFLA_EVENT, event))
1762 goto nla_put_failure;
1763 }
1764
b22b941b
HFS
1765 if (rtnl_fill_link_ifmap(skb, dev))
1766 goto nla_put_failure;
1da177e4
LT
1767
1768 if (dev->addr_len) {
a6574349
DM
1769 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1770 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1771 goto nla_put_failure;
1da177e4
LT
1772 }
1773
66cae9ed
JP
1774 if (rtnl_phys_port_id_fill(skb, dev))
1775 goto nla_put_failure;
1776
db24a904
DA
1777 if (rtnl_phys_port_name_fill(skb, dev))
1778 goto nla_put_failure;
1779
82f28412
JP
1780 if (rtnl_phys_switch_id_fill(skb, dev))
1781 goto nla_put_failure;
1782
b22b941b 1783 if (rtnl_fill_stats(skb, dev))
10708f37 1784 goto nla_put_failure;
10708f37 1785
250fc3df 1786 if (rtnl_fill_vf(skb, dev, ext_filter_mask))
a6574349 1787 goto nla_put_failure;
57b61080 1788
c53864fd 1789 if (rtnl_port_fill(skb, dev, ext_filter_mask))
57b61080
SF
1790 goto nla_put_failure;
1791
d1fdd913
BB
1792 if (rtnl_xdp_fill(skb, dev))
1793 goto nla_put_failure;
1794
ba7d49b1 1795 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
38f7b870
PM
1796 if (rtnl_link_fill(skb, dev) < 0)
1797 goto nla_put_failure;
1798 }
1799
d4e4fdf9 1800 if (rtnl_fill_link_netnsid(skb, dev, src_net, gfp))
b1e66b9a 1801 goto nla_put_failure;
d37512a2 1802
6621dd29
ND
1803 if (new_nsid &&
1804 nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0)
1805 goto nla_put_failure;
38e01b30
ND
1806 if (new_ifindex &&
1807 nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0)
1808 goto nla_put_failure;
1809
f74877a5
MK
1810 if (memchr_inv(dev->perm_addr, '\0', dev->addr_len) &&
1811 nla_put(skb, IFLA_PERM_ADDRESS, dev->addr_len, dev->perm_addr))
1812 goto nla_put_failure;
6621dd29 1813
5fa85a09 1814 rcu_read_lock();
070cbf5b 1815 if (rtnl_fill_link_af(skb, dev, ext_filter_mask))
5fa85a09
FW
1816 goto nla_put_failure_rcu;
1817 rcu_read_unlock();
f8ff182c 1818
88f4fb0c
JP
1819 if (rtnl_fill_prop_list(skb, dev))
1820 goto nla_put_failure;
1821
053c095a
JB
1822 nlmsg_end(skb, nlh);
1823 return 0;
b60c5115 1824
5fa85a09
FW
1825nla_put_failure_rcu:
1826 rcu_read_unlock();
b60c5115 1827nla_put_failure:
26932566
PM
1828 nlmsg_cancel(skb, nlh);
1829 return -EMSGSIZE;
1da177e4
LT
1830}
1831
f7b12606 1832static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
5176f91e 1833 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
38f7b870
PM
1834 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1835 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
5176f91e 1836 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
da5e0494 1837 [IFLA_MTU] = { .type = NLA_U32 },
76e87306 1838 [IFLA_LINK] = { .type = NLA_U32 },
fbaec0ea 1839 [IFLA_MASTER] = { .type = NLA_U32 },
9a57247f 1840 [IFLA_CARRIER] = { .type = NLA_U8 },
da5e0494
TG
1841 [IFLA_TXQLEN] = { .type = NLA_U32 },
1842 [IFLA_WEIGHT] = { .type = NLA_U32 },
1843 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1844 [IFLA_LINKMODE] = { .type = NLA_U8 },
76e87306 1845 [IFLA_LINKINFO] = { .type = NLA_NESTED },
d8a5ec67 1846 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
f0630529 1847 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
2459b4c6
ND
1848 /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
1849 * allow 0-length string (needed to remove an alias).
1850 */
1851 [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
c02db8c6 1852 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
57b61080
SF
1853 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1854 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
f8ff182c 1855 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
115c9b81 1856 [IFLA_EXT_MASK] = { .type = NLA_U32 },
edbc0bb3 1857 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
76ff5cc9
JP
1858 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1859 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
46e6b992
SH
1860 [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 },
1861 [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 },
02637fce 1862 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
2d3b479d 1863 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
82f28412 1864 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
317f4810 1865 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
88d6378b 1866 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
d1fdd913 1867 [IFLA_XDP] = { .type = NLA_NESTED },
3d3ea5af 1868 [IFLA_EVENT] = { .type = NLA_U32 },
db833d40 1869 [IFLA_GROUP] = { .type = NLA_U32 },
7e4a8d5a 1870 [IFLA_TARGET_NETNSID] = { .type = NLA_S32 },
b2d3bcfa
DD
1871 [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 },
1872 [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 },
3e7a50ce
SH
1873 [IFLA_MIN_MTU] = { .type = NLA_U32 },
1874 [IFLA_MAX_MTU] = { .type = NLA_U32 },
36fbf1e5
JP
1875 [IFLA_PROP_LIST] = { .type = NLA_NESTED },
1876 [IFLA_ALT_IFNAME] = { .type = NLA_STRING,
1877 .len = ALTIFNAMSIZ - 1 },
f74877a5 1878 [IFLA_PERM_ADDRESS] = { .type = NLA_REJECT },
829eb208 1879 [IFLA_PROTO_DOWN_REASON] = { .type = NLA_NESTED },
7e4a5131 1880 [IFLA_NEW_IFINDEX] = NLA_POLICY_MIN(NLA_S32, 1),
da5e0494
TG
1881};
1882
38f7b870
PM
1883static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1884 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1885 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
ba7d49b1
JP
1886 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1887 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
38f7b870
PM
1888};
1889
c02db8c6 1890static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
364d5716 1891 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
75345f88 1892 [IFLA_VF_BROADCAST] = { .type = NLA_REJECT },
364d5716 1893 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
79aab093 1894 [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED },
364d5716
DB
1895 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1896 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1897 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1898 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
01a3d796 1899 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
3b766cd8 1900 [IFLA_VF_STATS] = { .type = NLA_NESTED },
dd461d6a 1901 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
cc8e27cc
EC
1902 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1903 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
3b766cd8
EBE
1904};
1905
57b61080
SF
1906static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1907 [IFLA_PORT_VF] = { .type = NLA_U32 },
1908 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1909 .len = PORT_PROFILE_MAX },
57b61080
SF
1910 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1911 .len = PORT_UUID_MAX },
1912 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1913 .len = PORT_UUID_MAX },
1914 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1915 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
025331df
DB
1916
1917 /* Unused, but we need to keep it here since user space could
1918 * fill it. It's also broken with regard to NLA_BINARY use in
1919 * combination with structs.
1920 */
1921 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1922 .len = sizeof(struct ifla_port_vsi) },
57b61080
SF
1923};
1924
d1fdd913 1925static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
92234c8f 1926 [IFLA_XDP_UNSPEC] = { .strict_start_type = IFLA_XDP_EXPECTED_FD },
d1fdd913 1927 [IFLA_XDP_FD] = { .type = NLA_S32 },
92234c8f 1928 [IFLA_XDP_EXPECTED_FD] = { .type = NLA_S32 },
d1fdd913 1929 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 },
85de8576 1930 [IFLA_XDP_FLAGS] = { .type = NLA_U32 },
58038695 1931 [IFLA_XDP_PROG_ID] = { .type = NLA_U32 },
d1fdd913
BB
1932};
1933
dc599f76
DA
1934static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1935{
1936 const struct rtnl_link_ops *ops = NULL;
1937 struct nlattr *linfo[IFLA_INFO_MAX + 1];
1938
8cb08174 1939 if (nla_parse_nested_deprecated(linfo, IFLA_INFO_MAX, nla, ifla_info_policy, NULL) < 0)
dc599f76
DA
1940 return NULL;
1941
1942 if (linfo[IFLA_INFO_KIND]) {
1943 char kind[MODULE_NAME_LEN];
1944
872f6903 1945 nla_strscpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
dc599f76
DA
1946 ops = rtnl_link_ops_get(kind);
1947 }
1948
1949 return ops;
1950}
1951
1952static bool link_master_filtered(struct net_device *dev, int master_idx)
1953{
1954 struct net_device *master;
1955
1956 if (!master_idx)
1957 return false;
1958
1959 master = netdev_master_upper_dev_get(dev);
1960 if (!master || master->ifindex != master_idx)
1961 return true;
1962
1963 return false;
1964}
1965
1966static bool link_kind_filtered(const struct net_device *dev,
1967 const struct rtnl_link_ops *kind_ops)
1968{
1969 if (kind_ops && dev->rtnl_link_ops != kind_ops)
1970 return true;
1971
1972 return false;
1973}
1974
1975static bool link_dump_filtered(struct net_device *dev,
1976 int master_idx,
1977 const struct rtnl_link_ops *kind_ops)
1978{
1979 if (link_master_filtered(dev, master_idx) ||
1980 link_kind_filtered(dev, kind_ops))
1981 return true;
1982
1983 return false;
1984}
1985
c383edc4
CB
1986/**
1987 * rtnl_get_net_ns_capable - Get netns if sufficiently privileged.
1988 * @sk: netlink socket
1989 * @netnsid: network namespace identifier
1990 *
1991 * Returns the network namespace identified by netnsid on success or an error
1992 * pointer on failure.
1993 */
1994struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid)
79e1ad14
JB
1995{
1996 struct net *net;
1997
f428fe4a 1998 net = get_net_ns_by_id(sock_net(sk), netnsid);
79e1ad14
JB
1999 if (!net)
2000 return ERR_PTR(-EINVAL);
2001
2002 /* For now, the caller is required to have CAP_NET_ADMIN in
2003 * the user namespace owning the target net ns.
2004 */
f428fe4a 2005 if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) {
79e1ad14
JB
2006 put_net(net);
2007 return ERR_PTR(-EACCES);
2008 }
2009 return net;
2010}
c383edc4 2011EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable);
79e1ad14 2012
905cf0ab
DA
2013static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh,
2014 bool strict_check, struct nlattr **tb,
2015 struct netlink_ext_ack *extack)
2016{
2017 int hdrlen;
2018
2019 if (strict_check) {
2020 struct ifinfomsg *ifm;
2021
2022 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
2023 NL_SET_ERR_MSG(extack, "Invalid header for link dump");
2024 return -EINVAL;
2025 }
2026
2027 ifm = nlmsg_data(nlh);
2028 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
2029 ifm->ifi_change) {
2030 NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request");
2031 return -EINVAL;
2032 }
2033 if (ifm->ifi_index) {
2034 NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps");
2035 return -EINVAL;
2036 }
2037
8cb08174
JB
2038 return nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb,
2039 IFLA_MAX, ifla_policy,
2040 extack);
905cf0ab
DA
2041 }
2042
2043 /* A hack to preserve kernel<->userspace interface.
2044 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
2045 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
2046 * what iproute2 < v3.9.0 used.
2047 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
2048 * attribute, its netlink message is shorter than struct ifinfomsg.
2049 */
2050 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2051 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2052
8cb08174
JB
2053 return nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy,
2054 extack);
905cf0ab
DA
2055}
2056
f7b12606
JP
2057static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
2058{
905cf0ab
DA
2059 struct netlink_ext_ack *extack = cb->extack;
2060 const struct nlmsghdr *nlh = cb->nlh;
f7b12606 2061 struct net *net = sock_net(skb->sk);
79e1ad14 2062 struct net *tgt_net = net;
f7b12606
JP
2063 int h, s_h;
2064 int idx = 0, s_idx;
2065 struct net_device *dev;
2066 struct hlist_head *head;
2067 struct nlattr *tb[IFLA_MAX+1];
2068 u32 ext_filter_mask = 0;
dc599f76
DA
2069 const struct rtnl_link_ops *kind_ops = NULL;
2070 unsigned int flags = NLM_F_MULTI;
2071 int master_idx = 0;
79e1ad14 2072 int netnsid = -1;
905cf0ab 2073 int err, i;
f7b12606
JP
2074
2075 s_h = cb->args[0];
2076 s_idx = cb->args[1];
2077
905cf0ab
DA
2078 err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack);
2079 if (err < 0) {
2080 if (cb->strict_check)
2081 return err;
2082
2083 goto walk_entries;
2084 }
2085
2086 for (i = 0; i <= IFLA_MAX; ++i) {
2087 if (!tb[i])
2088 continue;
e5eca6d4 2089
905cf0ab
DA
2090 /* new attributes should only be added with strict checking */
2091 switch (i) {
2092 case IFLA_TARGET_NETNSID:
2093 netnsid = nla_get_s32(tb[i]);
c383edc4 2094 tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid);
905cf0ab
DA
2095 if (IS_ERR(tgt_net)) {
2096 NL_SET_ERR_MSG(extack, "Invalid target network namespace id");
893626d6 2097 return PTR_ERR(tgt_net);
905cf0ab
DA
2098 }
2099 break;
2100 case IFLA_EXT_MASK:
2101 ext_filter_mask = nla_get_u32(tb[i]);
2102 break;
2103 case IFLA_MASTER:
2104 master_idx = nla_get_u32(tb[i]);
2105 break;
2106 case IFLA_LINKINFO:
2107 kind_ops = linkinfo_to_kind_ops(tb[i]);
2108 break;
2109 default:
2110 if (cb->strict_check) {
2111 NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request");
2112 return -EINVAL;
2113 }
79e1ad14 2114 }
f7b12606
JP
2115 }
2116
905cf0ab
DA
2117 if (master_idx || kind_ops)
2118 flags |= NLM_F_DUMP_FILTERED;
2119
2120walk_entries:
f7b12606
JP
2121 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
2122 idx = 0;
79e1ad14 2123 head = &tgt_net->dev_index_head[h];
cac5e65e 2124 hlist_for_each_entry(dev, head, index_hlist) {
dc599f76 2125 if (link_dump_filtered(dev, master_idx, kind_ops))
3f0ae05d 2126 goto cont;
f7b12606
JP
2127 if (idx < s_idx)
2128 goto cont;
79e1ad14
JB
2129 err = rtnl_fill_ifinfo(skb, dev, net,
2130 RTM_NEWLINK,
973462bb 2131 NETLINK_CB(cb->skb).portid,
905cf0ab 2132 nlh->nlmsg_seq, 0, flags,
38e01b30 2133 ext_filter_mask, 0, NULL, 0,
d4e4fdf9 2134 netnsid, GFP_KERNEL);
973462bb 2135
f6c5775f
DA
2136 if (err < 0) {
2137 if (likely(skb->len))
2138 goto out;
2139
2140 goto out_err;
2141 }
f7b12606
JP
2142cont:
2143 idx++;
2144 }
2145 }
2146out:
f6c5775f
DA
2147 err = skb->len;
2148out_err:
f7b12606
JP
2149 cb->args[1] = idx;
2150 cb->args[0] = h;
a9ecb0cb 2151 cb->seq = tgt_net->dev_base_seq;
d0225784 2152 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
79e1ad14
JB
2153 if (netnsid >= 0)
2154 put_net(tgt_net);
f7b12606 2155
f6c5775f 2156 return err;
f7b12606
JP
2157}
2158
fceb6435
JB
2159int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
2160 struct netlink_ext_ack *exterr)
f7b12606 2161{
8cb08174
JB
2162 return nla_parse_deprecated(tb, IFLA_MAX, head, len, ifla_policy,
2163 exterr);
f7b12606
JP
2164}
2165EXPORT_SYMBOL(rtnl_nla_parse_ifla);
2166
81adee47
EB
2167struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
2168{
2169 struct net *net;
2170 /* Examine the link attributes and figure out which
2171 * network namespace we are talking about.
2172 */
2173 if (tb[IFLA_NET_NS_PID])
2174 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
f0630529
EB
2175 else if (tb[IFLA_NET_NS_FD])
2176 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
81adee47
EB
2177 else
2178 net = get_net(src_net);
2179 return net;
2180}
2181EXPORT_SYMBOL(rtnl_link_get_net);
2182
7c4f63ba
CB
2183/* Figure out which network namespace we are talking about by
2184 * examining the link attributes in the following order:
2185 *
2186 * 1. IFLA_NET_NS_PID
2187 * 2. IFLA_NET_NS_FD
7e4a8d5a 2188 * 3. IFLA_TARGET_NETNSID
7c4f63ba
CB
2189 */
2190static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net,
2191 struct nlattr *tb[])
2192{
2193 struct net *net;
2194
2195 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])
2196 return rtnl_link_get_net(src_net, tb);
2197
7e4a8d5a 2198 if (!tb[IFLA_TARGET_NETNSID])
7c4f63ba
CB
2199 return get_net(src_net);
2200
7e4a8d5a 2201 net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID]));
7c4f63ba
CB
2202 if (!net)
2203 return ERR_PTR(-EINVAL);
2204
2205 return net;
2206}
2207
2208static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb,
2209 struct net *src_net,
2210 struct nlattr *tb[], int cap)
2211{
2212 struct net *net;
2213
2214 net = rtnl_link_get_net_by_nlattr(src_net, tb);
2215 if (IS_ERR(net))
2216 return net;
2217
2218 if (!netlink_ns_capable(skb, net->user_ns, cap)) {
2219 put_net(net);
2220 return ERR_PTR(-EPERM);
2221 }
2222
2223 return net;
2224}
2225
4ff66cae
CB
2226/* Verify that rtnetlink requests do not pass additional properties
2227 * potentially referring to different network namespaces.
2228 */
2229static int rtnl_ensure_unique_netns(struct nlattr *tb[],
2230 struct netlink_ext_ack *extack,
2231 bool netns_id_only)
2232{
2233
2234 if (netns_id_only) {
2235 if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD])
2236 return 0;
2237
2238 NL_SET_ERR_MSG(extack, "specified netns attribute not supported");
2239 return -EOPNOTSUPP;
2240 }
2241
7e4a8d5a 2242 if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]))
4ff66cae
CB
2243 goto invalid_attr;
2244
7e4a8d5a 2245 if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD]))
4ff66cae
CB
2246 goto invalid_attr;
2247
7e4a8d5a 2248 if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID]))
4ff66cae
CB
2249 goto invalid_attr;
2250
2251 return 0;
2252
2253invalid_attr:
2254 NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified");
2255 return -EINVAL;
2256}
2257
1840bb13
TG
2258static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
2259{
2260 if (dev) {
2261 if (tb[IFLA_ADDRESS] &&
2262 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
2263 return -EINVAL;
2264
2265 if (tb[IFLA_BROADCAST] &&
2266 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
2267 return -EINVAL;
2268 }
2269
cf7afbfe
TG
2270 if (tb[IFLA_AF_SPEC]) {
2271 struct nlattr *af;
2272 int rem, err;
2273
2274 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2275 const struct rtnl_af_ops *af_ops;
2276
5fa85a09
FW
2277 rcu_read_lock();
2278 af_ops = rtnl_af_lookup(nla_type(af));
2279 if (!af_ops) {
2280 rcu_read_unlock();
cf7afbfe 2281 return -EAFNOSUPPORT;
5fa85a09 2282 }
cf7afbfe 2283
5fa85a09
FW
2284 if (!af_ops->set_link_af) {
2285 rcu_read_unlock();
cf7afbfe 2286 return -EOPNOTSUPP;
5fa85a09 2287 }
cf7afbfe
TG
2288
2289 if (af_ops->validate_link_af) {
6d3a9a68 2290 err = af_ops->validate_link_af(dev, af);
5fa85a09
FW
2291 if (err < 0) {
2292 rcu_read_unlock();
cf7afbfe 2293 return err;
5fa85a09 2294 }
cf7afbfe 2295 }
5fa85a09
FW
2296
2297 rcu_read_unlock();
cf7afbfe
TG
2298 }
2299 }
2300
1840bb13
TG
2301 return 0;
2302}
2303
cc8e27cc
EC
2304static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
2305 int guid_type)
2306{
2307 const struct net_device_ops *ops = dev->netdev_ops;
2308
2309 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
2310}
2311
2312static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
2313{
2314 if (dev->type != ARPHRD_INFINIBAND)
2315 return -EOPNOTSUPP;
2316
2317 return handle_infiniband_guid(dev, ivt, guid_type);
2318}
2319
4f7d2cdf 2320static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
c02db8c6 2321{
c02db8c6 2322 const struct net_device_ops *ops = dev->netdev_ops;
4f7d2cdf 2323 int err = -EINVAL;
c02db8c6 2324
4f7d2cdf
DB
2325 if (tb[IFLA_VF_MAC]) {
2326 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
01a3d796 2327
ff08ddba
DC
2328 if (ivm->vf >= INT_MAX)
2329 return -EINVAL;
4f7d2cdf
DB
2330 err = -EOPNOTSUPP;
2331 if (ops->ndo_set_vf_mac)
2332 err = ops->ndo_set_vf_mac(dev, ivm->vf,
2333 ivm->mac);
2334 if (err < 0)
2335 return err;
2336 }
2337
2338 if (tb[IFLA_VF_VLAN]) {
2339 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
2340
ff08ddba
DC
2341 if (ivv->vf >= INT_MAX)
2342 return -EINVAL;
4f7d2cdf
DB
2343 err = -EOPNOTSUPP;
2344 if (ops->ndo_set_vf_vlan)
2345 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
79aab093
MS
2346 ivv->qos,
2347 htons(ETH_P_8021Q));
2348 if (err < 0)
2349 return err;
2350 }
2351
2352 if (tb[IFLA_VF_VLAN_LIST]) {
2353 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
2354 struct nlattr *attr;
2355 int rem, len = 0;
2356
2357 err = -EOPNOTSUPP;
2358 if (!ops->ndo_set_vf_vlan)
2359 return err;
2360
2361 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
2362 if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
2363 nla_len(attr) < NLA_HDRLEN) {
2364 return -EINVAL;
2365 }
2366 if (len >= MAX_VLAN_LIST_LEN)
2367 return -EOPNOTSUPP;
2368 ivvl[len] = nla_data(attr);
2369
2370 len++;
2371 }
fa34cd94
AB
2372 if (len == 0)
2373 return -EINVAL;
2374
ff08ddba
DC
2375 if (ivvl[0]->vf >= INT_MAX)
2376 return -EINVAL;
79aab093
MS
2377 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
2378 ivvl[0]->qos, ivvl[0]->vlan_proto);
4f7d2cdf
DB
2379 if (err < 0)
2380 return err;
c02db8c6 2381 }
4f7d2cdf
DB
2382
2383 if (tb[IFLA_VF_TX_RATE]) {
2384 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
2385 struct ifla_vf_info ivf;
2386
ff08ddba
DC
2387 if (ivt->vf >= INT_MAX)
2388 return -EINVAL;
4f7d2cdf
DB
2389 err = -EOPNOTSUPP;
2390 if (ops->ndo_get_vf_config)
2391 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
2392 if (err < 0)
2393 return err;
2394
2395 err = -EOPNOTSUPP;
2396 if (ops->ndo_set_vf_rate)
2397 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2398 ivf.min_tx_rate,
2399 ivt->rate);
2400 if (err < 0)
2401 return err;
2402 }
2403
2404 if (tb[IFLA_VF_RATE]) {
2405 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
2406
ff08ddba
DC
2407 if (ivt->vf >= INT_MAX)
2408 return -EINVAL;
4f7d2cdf
DB
2409 err = -EOPNOTSUPP;
2410 if (ops->ndo_set_vf_rate)
2411 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2412 ivt->min_tx_rate,
2413 ivt->max_tx_rate);
2414 if (err < 0)
2415 return err;
2416 }
2417
2418 if (tb[IFLA_VF_SPOOFCHK]) {
2419 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
2420
ff08ddba
DC
2421 if (ivs->vf >= INT_MAX)
2422 return -EINVAL;
4f7d2cdf
DB
2423 err = -EOPNOTSUPP;
2424 if (ops->ndo_set_vf_spoofchk)
2425 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
2426 ivs->setting);
2427 if (err < 0)
2428 return err;
2429 }
2430
2431 if (tb[IFLA_VF_LINK_STATE]) {
2432 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
2433
ff08ddba
DC
2434 if (ivl->vf >= INT_MAX)
2435 return -EINVAL;
4f7d2cdf
DB
2436 err = -EOPNOTSUPP;
2437 if (ops->ndo_set_vf_link_state)
2438 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
2439 ivl->link_state);
2440 if (err < 0)
2441 return err;
2442 }
2443
2444 if (tb[IFLA_VF_RSS_QUERY_EN]) {
2445 struct ifla_vf_rss_query_en *ivrssq_en;
2446
2447 err = -EOPNOTSUPP;
2448 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
ff08ddba
DC
2449 if (ivrssq_en->vf >= INT_MAX)
2450 return -EINVAL;
4f7d2cdf
DB
2451 if (ops->ndo_set_vf_rss_query_en)
2452 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
2453 ivrssq_en->setting);
2454 if (err < 0)
2455 return err;
2456 }
2457
dd461d6a
HS
2458 if (tb[IFLA_VF_TRUST]) {
2459 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
2460
ff08ddba
DC
2461 if (ivt->vf >= INT_MAX)
2462 return -EINVAL;
dd461d6a
HS
2463 err = -EOPNOTSUPP;
2464 if (ops->ndo_set_vf_trust)
2465 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
2466 if (err < 0)
2467 return err;
2468 }
2469
cc8e27cc
EC
2470 if (tb[IFLA_VF_IB_NODE_GUID]) {
2471 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
2472
ff08ddba
DC
2473 if (ivt->vf >= INT_MAX)
2474 return -EINVAL;
cc8e27cc
EC
2475 if (!ops->ndo_set_vf_guid)
2476 return -EOPNOTSUPP;
cc8e27cc
EC
2477 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
2478 }
2479
2480 if (tb[IFLA_VF_IB_PORT_GUID]) {
2481 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
2482
ff08ddba
DC
2483 if (ivt->vf >= INT_MAX)
2484 return -EINVAL;
cc8e27cc
EC
2485 if (!ops->ndo_set_vf_guid)
2486 return -EOPNOTSUPP;
2487
2488 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
2489 }
2490
c02db8c6
CW
2491 return err;
2492}
2493
33eaf2a6
DA
2494static int do_set_master(struct net_device *dev, int ifindex,
2495 struct netlink_ext_ack *extack)
fbaec0ea 2496{
898e5061 2497 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
fbaec0ea
JP
2498 const struct net_device_ops *ops;
2499 int err;
2500
898e5061
JP
2501 if (upper_dev) {
2502 if (upper_dev->ifindex == ifindex)
fbaec0ea 2503 return 0;
898e5061 2504 ops = upper_dev->netdev_ops;
fbaec0ea 2505 if (ops->ndo_del_slave) {
898e5061 2506 err = ops->ndo_del_slave(upper_dev, dev);
fbaec0ea
JP
2507 if (err)
2508 return err;
2509 } else {
2510 return -EOPNOTSUPP;
2511 }
2512 }
2513
2514 if (ifindex) {
898e5061
JP
2515 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
2516 if (!upper_dev)
fbaec0ea 2517 return -EINVAL;
898e5061 2518 ops = upper_dev->netdev_ops;
fbaec0ea 2519 if (ops->ndo_add_slave) {
33eaf2a6 2520 err = ops->ndo_add_slave(upper_dev, dev, extack);
fbaec0ea
JP
2521 if (err)
2522 return err;
2523 } else {
2524 return -EOPNOTSUPP;
2525 }
2526 }
2527 return 0;
2528}
2529
829eb208
RP
2530static const struct nla_policy ifla_proto_down_reason_policy[IFLA_PROTO_DOWN_REASON_VALUE + 1] = {
2531 [IFLA_PROTO_DOWN_REASON_MASK] = { .type = NLA_U32 },
2532 [IFLA_PROTO_DOWN_REASON_VALUE] = { .type = NLA_U32 },
2533};
2534
2535static int do_set_proto_down(struct net_device *dev,
2536 struct nlattr *nl_proto_down,
2537 struct nlattr *nl_proto_down_reason,
2538 struct netlink_ext_ack *extack)
2539{
2540 struct nlattr *pdreason[IFLA_PROTO_DOWN_REASON_MAX + 1];
2541 const struct net_device_ops *ops = dev->netdev_ops;
2542 unsigned long mask = 0;
2543 u32 value;
2544 bool proto_down;
2545 int err;
2546
2547 if (!ops->ndo_change_proto_down) {
2548 NL_SET_ERR_MSG(extack, "Protodown not supported by device");
2549 return -EOPNOTSUPP;
2550 }
2551
2552 if (nl_proto_down_reason) {
2553 err = nla_parse_nested_deprecated(pdreason,
2554 IFLA_PROTO_DOWN_REASON_MAX,
2555 nl_proto_down_reason,
2556 ifla_proto_down_reason_policy,
2557 NULL);
2558 if (err < 0)
2559 return err;
2560
2561 if (!pdreason[IFLA_PROTO_DOWN_REASON_VALUE]) {
2562 NL_SET_ERR_MSG(extack, "Invalid protodown reason value");
2563 return -EINVAL;
2564 }
2565
2566 value = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_VALUE]);
2567
2568 if (pdreason[IFLA_PROTO_DOWN_REASON_MASK])
2569 mask = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_MASK]);
2570
2571 dev_change_proto_down_reason(dev, mask, value);
2572 }
2573
2574 if (nl_proto_down) {
2575 proto_down = nla_get_u8(nl_proto_down);
2576
2577 /* Dont turn off protodown if there are active reasons */
2578 if (!proto_down && dev->proto_down_reason) {
2579 NL_SET_ERR_MSG(extack, "Cannot clear protodown, active reasons");
2580 return -EBUSY;
2581 }
2582 err = dev_change_proto_down(dev,
2583 proto_down);
2584 if (err)
2585 return err;
2586 }
2587
2588 return 0;
2589}
2590
90c325e3 2591#define DO_SETLINK_MODIFIED 0x01
ba998906
ND
2592/* notify flag means notify + modified. */
2593#define DO_SETLINK_NOTIFY 0x03
90f62cf3
EB
2594static int do_setlink(const struct sk_buff *skb,
2595 struct net_device *dev, struct ifinfomsg *ifm,
ddf9f970 2596 struct netlink_ext_ack *extack,
90c325e3 2597 struct nlattr **tb, char *ifname, int status)
1da177e4 2598{
d314774c 2599 const struct net_device_ops *ops = dev->netdev_ops;
0157f60c 2600 int err;
1da177e4 2601
644c7eeb
ED
2602 err = validate_linkmsg(dev, tb);
2603 if (err < 0)
2604 return err;
2605
7e4a8d5a 2606 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) {
eeb85a14
AV
2607 struct net *net;
2608 int new_ifindex;
2609
2610 net = rtnl_link_get_net_capable(skb, dev_net(dev),
2611 tb, CAP_NET_ADMIN);
d8a5ec67
EB
2612 if (IS_ERR(net)) {
2613 err = PTR_ERR(net);
2614 goto errout;
2615 }
7c4f63ba 2616
eeb85a14
AV
2617 if (tb[IFLA_NEW_IFINDEX])
2618 new_ifindex = nla_get_s32(tb[IFLA_NEW_IFINDEX]);
2619 else
2620 new_ifindex = 0;
2621
0854fa82 2622 err = __dev_change_net_namespace(dev, net, ifname, new_ifindex);
d8a5ec67
EB
2623 put_net(net);
2624 if (err)
2625 goto errout;
90c325e3 2626 status |= DO_SETLINK_MODIFIED;
d8a5ec67
EB
2627 }
2628
da5e0494 2629 if (tb[IFLA_MAP]) {
1da177e4
LT
2630 struct rtnl_link_ifmap *u_map;
2631 struct ifmap k_map;
2632
d314774c 2633 if (!ops->ndo_set_config) {
1da177e4 2634 err = -EOPNOTSUPP;
0157f60c 2635 goto errout;
1da177e4
LT
2636 }
2637
2638 if (!netif_device_present(dev)) {
2639 err = -ENODEV;
0157f60c 2640 goto errout;
1da177e4 2641 }
1da177e4 2642
da5e0494 2643 u_map = nla_data(tb[IFLA_MAP]);
1da177e4
LT
2644 k_map.mem_start = (unsigned long) u_map->mem_start;
2645 k_map.mem_end = (unsigned long) u_map->mem_end;
2646 k_map.base_addr = (unsigned short) u_map->base_addr;
2647 k_map.irq = (unsigned char) u_map->irq;
2648 k_map.dma = (unsigned char) u_map->dma;
2649 k_map.port = (unsigned char) u_map->port;
2650
d314774c 2651 err = ops->ndo_set_config(dev, &k_map);
da5e0494 2652 if (err < 0)
0157f60c 2653 goto errout;
1da177e4 2654
ba998906 2655 status |= DO_SETLINK_NOTIFY;
1da177e4
LT
2656 }
2657
da5e0494 2658 if (tb[IFLA_ADDRESS]) {
70f8e78e
DM
2659 struct sockaddr *sa;
2660 int len;
2661
153711f9
WC
2662 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2663 sizeof(*sa));
70f8e78e
DM
2664 sa = kmalloc(len, GFP_KERNEL);
2665 if (!sa) {
2666 err = -ENOMEM;
0157f60c 2667 goto errout;
70f8e78e
DM
2668 }
2669 sa->sa_family = dev->type;
da5e0494 2670 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
70f8e78e 2671 dev->addr_len);
3b23a32a 2672 err = dev_set_mac_address_user(dev, sa, extack);
70f8e78e 2673 kfree(sa);
1da177e4 2674 if (err)
0157f60c 2675 goto errout;
90c325e3 2676 status |= DO_SETLINK_MODIFIED;
1da177e4
LT
2677 }
2678
da5e0494 2679 if (tb[IFLA_MTU]) {
7a4c53be 2680 err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack);
da5e0494 2681 if (err < 0)
0157f60c 2682 goto errout;
90c325e3 2683 status |= DO_SETLINK_MODIFIED;
1da177e4
LT
2684 }
2685
cbda10fa
VD
2686 if (tb[IFLA_GROUP]) {
2687 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
ba998906 2688 status |= DO_SETLINK_NOTIFY;
cbda10fa
VD
2689 }
2690
da5e0494
TG
2691 /*
2692 * Interface selected by interface index but interface
2693 * name provided implies that a name change has been
2694 * requested.
2695 */
51055be8 2696 if (ifm->ifi_index > 0 && ifname[0]) {
da5e0494
TG
2697 err = dev_change_name(dev, ifname);
2698 if (err < 0)
0157f60c 2699 goto errout;
90c325e3 2700 status |= DO_SETLINK_MODIFIED;
1da177e4
LT
2701 }
2702
0b815a1a
SH
2703 if (tb[IFLA_IFALIAS]) {
2704 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2705 nla_len(tb[IFLA_IFALIAS]));
2706 if (err < 0)
2707 goto errout;
ba998906 2708 status |= DO_SETLINK_NOTIFY;
0b815a1a
SH
2709 }
2710
da5e0494
TG
2711 if (tb[IFLA_BROADCAST]) {
2712 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
e7c3273e 2713 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
1da177e4
LT
2714 }
2715
83b496e9 2716 if (ifm->ifi_flags || ifm->ifi_change) {
567c5e13
PM
2717 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
2718 extack);
5f9021cf
JB
2719 if (err < 0)
2720 goto errout;
83b496e9 2721 }
1da177e4 2722
fbaec0ea 2723 if (tb[IFLA_MASTER]) {
33eaf2a6 2724 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
fbaec0ea
JP
2725 if (err)
2726 goto errout;
90c325e3 2727 status |= DO_SETLINK_MODIFIED;
fbaec0ea
JP
2728 }
2729
9a57247f
JP
2730 if (tb[IFLA_CARRIER]) {
2731 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2732 if (err)
2733 goto errout;
90c325e3 2734 status |= DO_SETLINK_MODIFIED;
9a57247f
JP
2735 }
2736
5d1180fc 2737 if (tb[IFLA_TXQLEN]) {
0cd29503 2738 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
6a643ddb
CW
2739
2740 err = dev_change_tx_queue_len(dev, value);
2741 if (err)
2742 goto errout;
2743 status |= DO_SETLINK_MODIFIED;
5d1180fc 2744 }
b00055aa 2745
46e6b992
SH
2746 if (tb[IFLA_GSO_MAX_SIZE]) {
2747 u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]);
2748
2749 if (max_size > GSO_MAX_SIZE) {
2750 err = -EINVAL;
2751 goto errout;
2752 }
2753
2754 if (dev->gso_max_size ^ max_size) {
2755 netif_set_gso_max_size(dev, max_size);
2756 status |= DO_SETLINK_MODIFIED;
2757 }
2758 }
2759
2760 if (tb[IFLA_GSO_MAX_SEGS]) {
2761 u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
2762
2763 if (max_segs > GSO_MAX_SEGS) {
2764 err = -EINVAL;
2765 goto errout;
2766 }
2767
2768 if (dev->gso_max_segs ^ max_segs) {
2769 dev->gso_max_segs = max_segs;
2770 status |= DO_SETLINK_MODIFIED;
2771 }
2772 }
2773
da5e0494 2774 if (tb[IFLA_OPERSTATE])
93b2d4a2 2775 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
b00055aa 2776
da5e0494 2777 if (tb[IFLA_LINKMODE]) {
1889b0e7
ND
2778 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2779
93b2d4a2 2780 write_lock_bh(&dev_base_lock);
1889b0e7 2781 if (dev->link_mode ^ value)
ba998906 2782 status |= DO_SETLINK_NOTIFY;
1889b0e7 2783 dev->link_mode = value;
93b2d4a2 2784 write_unlock_bh(&dev_base_lock);
b00055aa
SR
2785 }
2786
c02db8c6 2787 if (tb[IFLA_VFINFO_LIST]) {
4f7d2cdf 2788 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
c02db8c6
CW
2789 struct nlattr *attr;
2790 int rem;
4f7d2cdf 2791
c02db8c6 2792 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
4f7d2cdf
DB
2793 if (nla_type(attr) != IFLA_VF_INFO ||
2794 nla_len(attr) < NLA_HDRLEN) {
253683bb 2795 err = -EINVAL;
c02db8c6 2796 goto errout;
253683bb 2797 }
8cb08174
JB
2798 err = nla_parse_nested_deprecated(vfinfo, IFLA_VF_MAX,
2799 attr,
2800 ifla_vf_policy,
2801 NULL);
4f7d2cdf
DB
2802 if (err < 0)
2803 goto errout;
2804 err = do_setvfinfo(dev, vfinfo);
c02db8c6
CW
2805 if (err < 0)
2806 goto errout;
ba998906 2807 status |= DO_SETLINK_NOTIFY;
c02db8c6 2808 }
ebc08a6f 2809 }
1da177e4
LT
2810 err = 0;
2811
57b61080
SF
2812 if (tb[IFLA_VF_PORTS]) {
2813 struct nlattr *port[IFLA_PORT_MAX+1];
2814 struct nlattr *attr;
2815 int vf;
2816 int rem;
2817
2818 err = -EOPNOTSUPP;
2819 if (!ops->ndo_set_vf_port)
2820 goto errout;
2821
2822 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
035d210f
DB
2823 if (nla_type(attr) != IFLA_VF_PORT ||
2824 nla_len(attr) < NLA_HDRLEN) {
2825 err = -EINVAL;
2826 goto errout;
2827 }
8cb08174
JB
2828 err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
2829 attr,
2830 ifla_port_policy,
2831 NULL);
57b61080
SF
2832 if (err < 0)
2833 goto errout;
2834 if (!port[IFLA_PORT_VF]) {
2835 err = -EOPNOTSUPP;
2836 goto errout;
2837 }
2838 vf = nla_get_u32(port[IFLA_PORT_VF]);
2839 err = ops->ndo_set_vf_port(dev, vf, port);
2840 if (err < 0)
2841 goto errout;
ba998906 2842 status |= DO_SETLINK_NOTIFY;
57b61080
SF
2843 }
2844 }
2845 err = 0;
2846
2847 if (tb[IFLA_PORT_SELF]) {
2848 struct nlattr *port[IFLA_PORT_MAX+1];
2849
8cb08174
JB
2850 err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
2851 tb[IFLA_PORT_SELF],
2852 ifla_port_policy, NULL);
57b61080
SF
2853 if (err < 0)
2854 goto errout;
2855
2856 err = -EOPNOTSUPP;
2857 if (ops->ndo_set_vf_port)
2858 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
2859 if (err < 0)
2860 goto errout;
ba998906 2861 status |= DO_SETLINK_NOTIFY;
57b61080 2862 }
f8ff182c
TG
2863
2864 if (tb[IFLA_AF_SPEC]) {
2865 struct nlattr *af;
2866 int rem;
2867
2868 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2869 const struct rtnl_af_ops *af_ops;
2870
5fa85a09
FW
2871 rcu_read_lock();
2872
058c8d59 2873 BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
f8ff182c 2874
3583a4e8 2875 err = af_ops->set_link_af(dev, af, extack);
5fa85a09
FW
2876 if (err < 0) {
2877 rcu_read_unlock();
f8ff182c 2878 goto errout;
5fa85a09 2879 }
f8ff182c 2880
5fa85a09 2881 rcu_read_unlock();
ba998906 2882 status |= DO_SETLINK_NOTIFY;
f8ff182c
TG
2883 }
2884 }
57b61080
SF
2885 err = 0;
2886
829eb208
RP
2887 if (tb[IFLA_PROTO_DOWN] || tb[IFLA_PROTO_DOWN_REASON]) {
2888 err = do_set_proto_down(dev, tb[IFLA_PROTO_DOWN],
2889 tb[IFLA_PROTO_DOWN_REASON], extack);
88d6378b
AK
2890 if (err)
2891 goto errout;
2892 status |= DO_SETLINK_NOTIFY;
2893 }
2894
d1fdd913
BB
2895 if (tb[IFLA_XDP]) {
2896 struct nlattr *xdp[IFLA_XDP_MAX + 1];
85de8576 2897 u32 xdp_flags = 0;
d1fdd913 2898
8cb08174
JB
2899 err = nla_parse_nested_deprecated(xdp, IFLA_XDP_MAX,
2900 tb[IFLA_XDP],
2901 ifla_xdp_policy, NULL);
d1fdd913
BB
2902 if (err < 0)
2903 goto errout;
2904
58038695 2905 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
262d8625
BB
2906 err = -EINVAL;
2907 goto errout;
2908 }
85de8576
DB
2909
2910 if (xdp[IFLA_XDP_FLAGS]) {
2911 xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
2912 if (xdp_flags & ~XDP_FLAGS_MASK) {
2913 err = -EINVAL;
2914 goto errout;
2915 }
ee5d032f 2916 if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
0489df9a
DB
2917 err = -EINVAL;
2918 goto errout;
2919 }
85de8576
DB
2920 }
2921
d1fdd913 2922 if (xdp[IFLA_XDP_FD]) {
92234c8f
THJ
2923 int expected_fd = -1;
2924
2925 if (xdp_flags & XDP_FLAGS_REPLACE) {
2926 if (!xdp[IFLA_XDP_EXPECTED_FD]) {
2927 err = -EINVAL;
2928 goto errout;
2929 }
2930 expected_fd =
2931 nla_get_s32(xdp[IFLA_XDP_EXPECTED_FD]);
2932 }
2933
ddf9f970 2934 err = dev_change_xdp_fd(dev, extack,
85de8576 2935 nla_get_s32(xdp[IFLA_XDP_FD]),
92234c8f 2936 expected_fd,
85de8576 2937 xdp_flags);
d1fdd913
BB
2938 if (err)
2939 goto errout;
2940 status |= DO_SETLINK_NOTIFY;
2941 }
2942 }
2943
0157f60c 2944errout:
ba998906 2945 if (status & DO_SETLINK_MODIFIED) {
64ff90cc 2946 if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
ba998906
ND
2947 netdev_state_change(dev);
2948
2949 if (err < 0)
2950 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",
2951 dev->name);
2952 }
da5e0494 2953
0157f60c
PM
2954 return err;
2955}
1da177e4 2956
cc6090e9
JP
2957static struct net_device *rtnl_dev_get(struct net *net,
2958 struct nlattr *ifname_attr,
76c9ac0e 2959 struct nlattr *altifname_attr,
cc6090e9
JP
2960 char *ifname)
2961{
76c9ac0e 2962 char buffer[ALTIFNAMSIZ];
cc6090e9
JP
2963
2964 if (!ifname) {
2965 ifname = buffer;
2966 if (ifname_attr)
872f6903 2967 nla_strscpy(ifname, ifname_attr, IFNAMSIZ);
76c9ac0e 2968 else if (altifname_attr)
872f6903 2969 nla_strscpy(ifname, altifname_attr, ALTIFNAMSIZ);
cc6090e9
JP
2970 else
2971 return NULL;
2972 }
2973
2974 return __dev_get_by_name(net, ifname);
2975}
2976
c21ef3e3
DA
2977static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2978 struct netlink_ext_ack *extack)
0157f60c 2979{
3b1e0a65 2980 struct net *net = sock_net(skb->sk);
0157f60c
PM
2981 struct ifinfomsg *ifm;
2982 struct net_device *dev;
2983 int err;
2984 struct nlattr *tb[IFLA_MAX+1];
2985 char ifname[IFNAMSIZ];
2986
8cb08174
JB
2987 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
2988 ifla_policy, extack);
0157f60c
PM
2989 if (err < 0)
2990 goto errout;
2991
4ff66cae
CB
2992 err = rtnl_ensure_unique_netns(tb, extack, false);
2993 if (err < 0)
2994 goto errout;
2995
0157f60c 2996 if (tb[IFLA_IFNAME])
872f6903 2997 nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
0157f60c
PM
2998 else
2999 ifname[0] = '\0';
3000
3001 err = -EINVAL;
3002 ifm = nlmsg_data(nlh);
3003 if (ifm->ifi_index > 0)
a3d12891 3004 dev = __dev_get_by_index(net, ifm->ifi_index);
76c9ac0e
JP
3005 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3006 dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname);
0157f60c
PM
3007 else
3008 goto errout;
3009
3010 if (dev == NULL) {
3011 err = -ENODEV;
3012 goto errout;
3013 }
3014
ddf9f970 3015 err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
da5e0494 3016errout:
1da177e4
LT
3017 return err;
3018}
3019
66400d54
WC
3020static int rtnl_group_dellink(const struct net *net, int group)
3021{
3022 struct net_device *dev, *aux;
3023 LIST_HEAD(list_kill);
3024 bool found = false;
3025
3026 if (!group)
3027 return -EPERM;
3028
3029 for_each_netdev(net, dev) {
3030 if (dev->group == group) {
3031 const struct rtnl_link_ops *ops;
3032
3033 found = true;
3034 ops = dev->rtnl_link_ops;
3035 if (!ops || !ops->dellink)
3036 return -EOPNOTSUPP;
3037 }
3038 }
3039
3040 if (!found)
3041 return -ENODEV;
3042
3043 for_each_netdev_safe(net, dev, aux) {
3044 if (dev->group == group) {
3045 const struct rtnl_link_ops *ops;
3046
3047 ops = dev->rtnl_link_ops;
3048 ops->dellink(dev, &list_kill);
3049 }
3050 }
3051 unregister_netdevice_many(&list_kill);
3052
3053 return 0;
3054}
3055
614732ea
TG
3056int rtnl_delete_link(struct net_device *dev)
3057{
3058 const struct rtnl_link_ops *ops;
3059 LIST_HEAD(list_kill);
3060
3061 ops = dev->rtnl_link_ops;
3062 if (!ops || !ops->dellink)
3063 return -EOPNOTSUPP;
3064
3065 ops->dellink(dev, &list_kill);
3066 unregister_netdevice_many(&list_kill);
3067
3068 return 0;
3069}
3070EXPORT_SYMBOL_GPL(rtnl_delete_link);
3071
c21ef3e3
DA
3072static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
3073 struct netlink_ext_ack *extack)
38f7b870 3074{
3b1e0a65 3075 struct net *net = sock_net(skb->sk);
b61ad68a
CB
3076 struct net *tgt_net = net;
3077 struct net_device *dev = NULL;
38f7b870 3078 struct ifinfomsg *ifm;
38f7b870
PM
3079 struct nlattr *tb[IFLA_MAX+1];
3080 int err;
b61ad68a 3081 int netnsid = -1;
38f7b870 3082
8cb08174
JB
3083 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3084 ifla_policy, extack);
38f7b870
PM
3085 if (err < 0)
3086 return err;
3087
4ff66cae
CB
3088 err = rtnl_ensure_unique_netns(tb, extack, true);
3089 if (err < 0)
3090 return err;
3091
7e4a8d5a
CB
3092 if (tb[IFLA_TARGET_NETNSID]) {
3093 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
c383edc4 3094 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
b61ad68a
CB
3095 if (IS_ERR(tgt_net))
3096 return PTR_ERR(tgt_net);
3097 }
3098
3099 err = -EINVAL;
38f7b870
PM
3100 ifm = nlmsg_data(nlh);
3101 if (ifm->ifi_index > 0)
b61ad68a 3102 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
76c9ac0e
JP
3103 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3104 dev = rtnl_dev_get(net, tb[IFLA_IFNAME],
3105 tb[IFLA_ALT_IFNAME], NULL);
66400d54 3106 else if (tb[IFLA_GROUP])
b61ad68a 3107 err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP]));
38f7b870 3108 else
b61ad68a 3109 goto out;
38f7b870 3110
b61ad68a
CB
3111 if (!dev) {
3112 if (tb[IFLA_IFNAME] || ifm->ifi_index > 0)
3113 err = -ENODEV;
3114
3115 goto out;
3116 }
3117
3118 err = rtnl_delete_link(dev);
38f7b870 3119
b61ad68a
CB
3120out:
3121 if (netnsid >= 0)
3122 put_net(tgt_net);
3123
3124 return err;
38f7b870
PM
3125}
3126
3729d502
PM
3127int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
3128{
3129 unsigned int old_flags;
3130 int err;
3131
3132 old_flags = dev->flags;
3133 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
6d040321
PM
3134 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
3135 NULL);
3729d502
PM
3136 if (err < 0)
3137 return err;
3138 }
3139
8d356b89 3140 if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) {
56a49d70 3141 __dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags));
8d356b89
RP
3142 } else {
3143 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
3144 __dev_notify_flags(dev, old_flags, ~0U);
3145 }
3729d502
PM
3146 return 0;
3147}
3148EXPORT_SYMBOL(rtnl_configure_link);
3149
d0522f1c
DA
3150struct net_device *rtnl_create_link(struct net *net, const char *ifname,
3151 unsigned char name_assign_type,
3152 const struct rtnl_link_ops *ops,
3153 struct nlattr *tb[],
3154 struct netlink_ext_ack *extack)
e7199288 3155{
e7199288 3156 struct net_device *dev;
d40156aa
JP
3157 unsigned int num_tx_queues = 1;
3158 unsigned int num_rx_queues = 1;
e7199288 3159
76ff5cc9
JP
3160 if (tb[IFLA_NUM_TX_QUEUES])
3161 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
3162 else if (ops->get_num_tx_queues)
d40156aa 3163 num_tx_queues = ops->get_num_tx_queues();
76ff5cc9
JP
3164
3165 if (tb[IFLA_NUM_RX_QUEUES])
3166 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
3167 else if (ops->get_num_rx_queues)
d40156aa 3168 num_rx_queues = ops->get_num_rx_queues();
efacb309 3169
d0522f1c
DA
3170 if (num_tx_queues < 1 || num_tx_queues > 4096) {
3171 NL_SET_ERR_MSG(extack, "Invalid number of transmit queues");
0e1d6eca 3172 return ERR_PTR(-EINVAL);
d0522f1c 3173 }
0e1d6eca 3174
d0522f1c
DA
3175 if (num_rx_queues < 1 || num_rx_queues > 4096) {
3176 NL_SET_ERR_MSG(extack, "Invalid number of receive queues");
0e1d6eca 3177 return ERR_PTR(-EINVAL);
d0522f1c 3178 }
0e1d6eca 3179
5517750f 3180 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
c835a677 3181 ops->setup, num_tx_queues, num_rx_queues);
e7199288 3182 if (!dev)
d1892e4e 3183 return ERR_PTR(-ENOMEM);
e7199288 3184
81adee47
EB
3185 dev_net_set(dev, net);
3186 dev->rtnl_link_ops = ops;
3729d502 3187 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
81adee47 3188
d836f5c6
ED
3189 if (tb[IFLA_MTU]) {
3190 u32 mtu = nla_get_u32(tb[IFLA_MTU]);
3191 int err;
3192
3193 err = dev_validate_mtu(dev, mtu, extack);
3194 if (err) {
3195 free_netdev(dev);
3196 return ERR_PTR(err);
3197 }
3198 dev->mtu = mtu;
3199 }
2afb9b53 3200 if (tb[IFLA_ADDRESS]) {
e7199288
PE
3201 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
3202 nla_len(tb[IFLA_ADDRESS]));
2afb9b53
JP
3203 dev->addr_assign_type = NET_ADDR_SET;
3204 }
e7199288
PE
3205 if (tb[IFLA_BROADCAST])
3206 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
3207 nla_len(tb[IFLA_BROADCAST]));
3208 if (tb[IFLA_TXQLEN])
3209 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
3210 if (tb[IFLA_OPERSTATE])
93b2d4a2 3211 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
e7199288
PE
3212 if (tb[IFLA_LINKMODE])
3213 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
ffa934f1
PM
3214 if (tb[IFLA_GROUP])
3215 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
46e6b992
SH
3216 if (tb[IFLA_GSO_MAX_SIZE])
3217 netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE]));
3218 if (tb[IFLA_GSO_MAX_SEGS])
a0b586fa 3219 dev->gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
e7199288
PE
3220
3221 return dev;
e7199288 3222}
e0d087af 3223EXPORT_SYMBOL(rtnl_create_link);
e7199288 3224
90f62cf3
EB
3225static int rtnl_group_changelink(const struct sk_buff *skb,
3226 struct net *net, int group,
e7ed828f 3227 struct ifinfomsg *ifm,
ddf9f970 3228 struct netlink_ext_ack *extack,
e7ed828f
VD
3229 struct nlattr **tb)
3230{
d079535d 3231 struct net_device *dev, *aux;
e7ed828f
VD
3232 int err;
3233
d079535d 3234 for_each_netdev_safe(net, dev, aux) {
e7ed828f 3235 if (dev->group == group) {
ddf9f970 3236 err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
e7ed828f
VD
3237 if (err < 0)
3238 return err;
3239 }
3240 }
3241
3242 return 0;
3243}
3244
a2939745
JK
3245static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3246 struct nlattr **attr, struct netlink_ext_ack *extack)
38f7b870 3247{
420d0318
JK
3248 struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1];
3249 unsigned char name_assign_type = NET_NAME_USER;
3250 struct nlattr *linkinfo[IFLA_INFO_MAX + 1];
3251 const struct rtnl_link_ops *m_ops = NULL;
420d0318 3252 struct net_device *master_dev = NULL;
3b1e0a65 3253 struct net *net = sock_net(skb->sk);
38f7b870 3254 const struct rtnl_link_ops *ops;
420d0318
JK
3255 struct nlattr *tb[IFLA_MAX + 1];
3256 struct net *dest_net, *link_net;
3257 struct nlattr **slave_data;
3258 char kind[MODULE_NAME_LEN];
38f7b870
PM
3259 struct net_device *dev;
3260 struct ifinfomsg *ifm;
38f7b870 3261 char ifname[IFNAMSIZ];
420d0318 3262 struct nlattr **data;
38f7b870
PM
3263 int err;
3264
95a5afca 3265#ifdef CONFIG_MODULES
38f7b870 3266replay:
8072f085 3267#endif
8cb08174
JB
3268 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3269 ifla_policy, extack);
38f7b870
PM
3270 if (err < 0)
3271 return err;
3272
4ff66cae
CB
3273 err = rtnl_ensure_unique_netns(tb, extack, false);
3274 if (err < 0)
3275 return err;
3276
38f7b870 3277 if (tb[IFLA_IFNAME])
872f6903 3278 nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
38f7b870
PM
3279 else
3280 ifname[0] = '\0';
3281
3282 ifm = nlmsg_data(nlh);
3283 if (ifm->ifi_index > 0)
881d966b 3284 dev = __dev_get_by_index(net, ifm->ifi_index);
76c9ac0e
JP
3285 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3286 dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname);
7af12cba
JP
3287 else
3288 dev = NULL;
38f7b870 3289
ba7d49b1
JP
3290 if (dev) {
3291 master_dev = netdev_master_upper_dev_get(dev);
3292 if (master_dev)
3293 m_ops = master_dev->rtnl_link_ops;
3294 }
3295
e0d087af
ED
3296 err = validate_linkmsg(dev, tb);
3297 if (err < 0)
1840bb13
TG
3298 return err;
3299
38f7b870 3300 if (tb[IFLA_LINKINFO]) {
8cb08174
JB
3301 err = nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX,
3302 tb[IFLA_LINKINFO],
3303 ifla_info_policy, NULL);
38f7b870
PM
3304 if (err < 0)
3305 return err;
3306 } else
3307 memset(linkinfo, 0, sizeof(linkinfo));
3308
3309 if (linkinfo[IFLA_INFO_KIND]) {
872f6903 3310 nla_strscpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
38f7b870
PM
3311 ops = rtnl_link_ops_get(kind);
3312 } else {
3313 kind[0] = '\0';
3314 ops = NULL;
3315 }
3316
420d0318
JK
3317 data = NULL;
3318 if (ops) {
3319 if (ops->maxtype > RTNL_MAX_TYPE)
3320 return -EINVAL;
ccf8dbcd 3321
420d0318 3322 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
8cb08174
JB
3323 err = nla_parse_nested_deprecated(attr, ops->maxtype,
3324 linkinfo[IFLA_INFO_DATA],
3325 ops->policy, extack);
420d0318
JK
3326 if (err < 0)
3327 return err;
3328 data = attr;
38f7b870 3329 }
420d0318
JK
3330 if (ops->validate) {
3331 err = ops->validate(tb, data, extack);
3332 if (err < 0)
3333 return err;
3334 }
3335 }
38f7b870 3336
420d0318
JK
3337 slave_data = NULL;
3338 if (m_ops) {
3339 if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)
3340 return -EINVAL;
ccf8dbcd 3341
420d0318
JK
3342 if (m_ops->slave_maxtype &&
3343 linkinfo[IFLA_INFO_SLAVE_DATA]) {
8cb08174
JB
3344 err = nla_parse_nested_deprecated(slave_attr,
3345 m_ops->slave_maxtype,
3346 linkinfo[IFLA_INFO_SLAVE_DATA],
3347 m_ops->slave_policy,
3348 extack);
420d0318
JK
3349 if (err < 0)
3350 return err;
3351 slave_data = slave_attr;
ba7d49b1 3352 }
420d0318 3353 }
ba7d49b1 3354
420d0318
JK
3355 if (dev) {
3356 int status = 0;
38f7b870 3357
420d0318
JK
3358 if (nlh->nlmsg_flags & NLM_F_EXCL)
3359 return -EEXIST;
3360 if (nlh->nlmsg_flags & NLM_F_REPLACE)
3361 return -EOPNOTSUPP;
38f7b870 3362
420d0318
JK
3363 if (linkinfo[IFLA_INFO_DATA]) {
3364 if (!ops || ops != dev->rtnl_link_ops ||
3365 !ops->changelink)
3366 return -EOPNOTSUPP;
38f7b870 3367
420d0318
JK
3368 err = ops->changelink(dev, tb, data, extack);
3369 if (err < 0)
3370 return err;
3371 status |= DO_SETLINK_NOTIFY;
3372 }
ba7d49b1 3373
420d0318
JK
3374 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
3375 if (!m_ops || !m_ops->slave_changelink)
3376 return -EOPNOTSUPP;
ba7d49b1 3377
420d0318
JK
3378 err = m_ops->slave_changelink(master_dev, dev, tb,
3379 slave_data, extack);
3380 if (err < 0)
3381 return err;
3382 status |= DO_SETLINK_NOTIFY;
38f7b870
PM
3383 }
3384
420d0318
JK
3385 return do_setlink(skb, dev, ifm, extack, tb, ifname, status);
3386 }
3387
3388 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
3389 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
3390 return rtnl_group_changelink(skb, net,
ffa934f1 3391 nla_get_u32(tb[IFLA_GROUP]),
ddf9f970 3392 ifm, extack, tb);
420d0318
JK
3393 return -ENODEV;
3394 }
38f7b870 3395
420d0318
JK
3396 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
3397 return -EOPNOTSUPP;
38f7b870 3398
420d0318 3399 if (!ops) {
95a5afca 3400#ifdef CONFIG_MODULES
420d0318
JK
3401 if (kind[0]) {
3402 __rtnl_unlock();
3403 request_module("rtnl-link-%s", kind);
3404 rtnl_lock();
3405 ops = rtnl_link_ops_get(kind);
3406 if (ops)
3407 goto replay;
38f7b870 3408 }
420d0318
JK
3409#endif
3410 NL_SET_ERR_MSG(extack, "Unknown device type");
3411 return -EOPNOTSUPP;
3412 }
38f7b870 3413
420d0318
JK
3414 if (!ops->setup)
3415 return -EOPNOTSUPP;
b0ab2fab 3416
420d0318
JK
3417 if (!ifname[0]) {
3418 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
3419 name_assign_type = NET_NAME_ENUM;
3420 }
e7199288 3421
420d0318
JK
3422 dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN);
3423 if (IS_ERR(dest_net))
3424 return PTR_ERR(dest_net);
13ad1774 3425
420d0318
JK
3426 if (tb[IFLA_LINK_NETNSID]) {
3427 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
317f4810 3428
420d0318
JK
3429 link_net = get_net_ns_by_id(dest_net, id);
3430 if (!link_net) {
3431 NL_SET_ERR_MSG(extack, "Unknown network namespace id");
3432 err = -EINVAL;
9c7dafbf
PE
3433 goto out;
3434 }
420d0318
JK
3435 err = -EPERM;
3436 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
3437 goto out;
3438 } else {
3439 link_net = NULL;
3440 }
3441
3442 dev = rtnl_create_link(link_net ? : dest_net, ifname,
3443 name_assign_type, ops, tb, extack);
3444 if (IS_ERR(dev)) {
3445 err = PTR_ERR(dev);
3446 goto out;
3447 }
9c7dafbf 3448
420d0318 3449 dev->ifindex = ifm->ifi_index;
9c7dafbf 3450
c269a24c 3451 if (ops->newlink)
420d0318 3452 err = ops->newlink(link_net ? : net, dev, tb, data, extack);
c269a24c 3453 else
420d0318 3454 err = register_netdevice(dev);
c269a24c
JK
3455 if (err < 0) {
3456 free_netdev(dev);
3457 goto out;
420d0318 3458 }
c269a24c 3459
420d0318
JK
3460 err = rtnl_configure_link(dev, ifm);
3461 if (err < 0)
3462 goto out_unregister;
3463 if (link_net) {
0854fa82 3464 err = dev_change_net_namespace(dev, dest_net, ifname);
43638900
DM
3465 if (err < 0)
3466 goto out_unregister;
420d0318
JK
3467 }
3468 if (tb[IFLA_MASTER]) {
3469 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
3470 if (err)
3471 goto out_unregister;
3472 }
3729d502 3473out:
420d0318
JK
3474 if (link_net)
3475 put_net(link_net);
3476 put_net(dest_net);
3477 return err;
43638900 3478out_unregister:
420d0318
JK
3479 if (ops->newlink) {
3480 LIST_HEAD(list_kill);
43638900 3481
420d0318
JK
3482 ops->dellink(dev, &list_kill);
3483 unregister_netdevice_many(&list_kill);
3484 } else {
3485 unregister_netdevice(dev);
38f7b870 3486 }
420d0318 3487 goto out;
38f7b870
PM
3488}
3489
a2939745
JK
3490static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3491 struct netlink_ext_ack *extack)
3492{
3493 struct nlattr **attr;
3494 int ret;
3495
3496 attr = kmalloc_array(RTNL_MAX_TYPE + 1, sizeof(*attr), GFP_KERNEL);
3497 if (!attr)
3498 return -ENOMEM;
3499
3500 ret = __rtnl_newlink(skb, nlh, attr, extack);
3501 kfree(attr);
3502 return ret;
3503}
3504
9b3757b0
JK
3505static int rtnl_valid_getlink_req(struct sk_buff *skb,
3506 const struct nlmsghdr *nlh,
3507 struct nlattr **tb,
3508 struct netlink_ext_ack *extack)
3509{
3510 struct ifinfomsg *ifm;
3511 int i, err;
3512
3513 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
3514 NL_SET_ERR_MSG(extack, "Invalid header for get link");
3515 return -EINVAL;
3516 }
3517
3518 if (!netlink_strict_get_check(skb))
8cb08174
JB
3519 return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3520 ifla_policy, extack);
9b3757b0
JK
3521
3522 ifm = nlmsg_data(nlh);
3523 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
3524 ifm->ifi_change) {
3525 NL_SET_ERR_MSG(extack, "Invalid values in header for get link request");
3526 return -EINVAL;
3527 }
3528
8cb08174
JB
3529 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFLA_MAX,
3530 ifla_policy, extack);
9b3757b0
JK
3531 if (err)
3532 return err;
3533
3534 for (i = 0; i <= IFLA_MAX; i++) {
3535 if (!tb[i])
3536 continue;
3537
3538 switch (i) {
3539 case IFLA_IFNAME:
76c9ac0e 3540 case IFLA_ALT_IFNAME:
9b3757b0
JK
3541 case IFLA_EXT_MASK:
3542 case IFLA_TARGET_NETNSID:
3543 break;
3544 default:
3545 NL_SET_ERR_MSG(extack, "Unsupported attribute in get link request");
3546 return -EINVAL;
3547 }
3548 }
3549
3550 return 0;
3551}
3552
c21ef3e3
DA
3553static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3554 struct netlink_ext_ack *extack)
711e2c33 3555{
3b1e0a65 3556 struct net *net = sock_net(skb->sk);
79e1ad14 3557 struct net *tgt_net = net;
b60c5115
TG
3558 struct ifinfomsg *ifm;
3559 struct nlattr *tb[IFLA_MAX+1];
3560 struct net_device *dev = NULL;
3561 struct sk_buff *nskb;
79e1ad14 3562 int netnsid = -1;
339bf98f 3563 int err;
115c9b81 3564 u32 ext_filter_mask = 0;
711e2c33 3565
9b3757b0 3566 err = rtnl_valid_getlink_req(skb, nlh, tb, extack);
b60c5115 3567 if (err < 0)
9918f230 3568 return err;
b60c5115 3569
4ff66cae
CB
3570 err = rtnl_ensure_unique_netns(tb, extack, true);
3571 if (err < 0)
3572 return err;
3573
7e4a8d5a
CB
3574 if (tb[IFLA_TARGET_NETNSID]) {
3575 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
c383edc4 3576 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
79e1ad14
JB
3577 if (IS_ERR(tgt_net))
3578 return PTR_ERR(tgt_net);
3579 }
3580
115c9b81
GR
3581 if (tb[IFLA_EXT_MASK])
3582 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3583
79e1ad14 3584 err = -EINVAL;
b60c5115 3585 ifm = nlmsg_data(nlh);
a3d12891 3586 if (ifm->ifi_index > 0)
79e1ad14 3587 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
76c9ac0e
JP
3588 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3589 dev = rtnl_dev_get(tgt_net, tb[IFLA_IFNAME],
3590 tb[IFLA_ALT_IFNAME], NULL);
a3d12891 3591 else
79e1ad14 3592 goto out;
711e2c33 3593
79e1ad14 3594 err = -ENODEV;
a3d12891 3595 if (dev == NULL)
79e1ad14 3596 goto out;
a3d12891 3597
79e1ad14 3598 err = -ENOBUFS;
115c9b81 3599 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
a3d12891 3600 if (nskb == NULL)
79e1ad14 3601 goto out;
b60c5115 3602
79e1ad14
JB
3603 err = rtnl_fill_ifinfo(nskb, dev, net,
3604 RTM_NEWLINK, NETLINK_CB(skb).portid,
3605 nlh->nlmsg_seq, 0, 0, ext_filter_mask,
d4e4fdf9 3606 0, NULL, 0, netnsid, GFP_KERNEL);
26932566
PM
3607 if (err < 0) {
3608 /* -EMSGSIZE implies BUG in if_nlmsg_size */
3609 WARN_ON(err == -EMSGSIZE);
3610 kfree_skb(nskb);
a3d12891 3611 } else
15e47304 3612 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
79e1ad14
JB
3613out:
3614 if (netnsid >= 0)
3615 put_net(tgt_net);
711e2c33 3616
b60c5115 3617 return err;
711e2c33 3618}
711e2c33 3619
36fbf1e5
JP
3620static int rtnl_alt_ifname(int cmd, struct net_device *dev, struct nlattr *attr,
3621 bool *changed, struct netlink_ext_ack *extack)
3622{
3623 char *alt_ifname;
3624 int err;
3625
3626 err = nla_validate(attr, attr->nla_len, IFLA_MAX, ifla_policy, extack);
3627 if (err)
3628 return err;
3629
44bfa9c5
ED
3630 alt_ifname = nla_strdup(attr, GFP_KERNEL);
3631 if (!alt_ifname)
3632 return -ENOMEM;
3633
36fbf1e5 3634 if (cmd == RTM_NEWLINKPROP) {
36fbf1e5 3635 err = netdev_name_node_alt_create(dev, alt_ifname);
44bfa9c5
ED
3636 if (!err)
3637 alt_ifname = NULL;
36fbf1e5
JP
3638 } else if (cmd == RTM_DELLINKPROP) {
3639 err = netdev_name_node_alt_destroy(dev, alt_ifname);
36fbf1e5 3640 } else {
44bfa9c5
ED
3641 WARN_ON_ONCE(1);
3642 err = -EINVAL;
36fbf1e5
JP
3643 }
3644
44bfa9c5
ED
3645 kfree(alt_ifname);
3646 if (!err)
3647 *changed = true;
3648 return err;
36fbf1e5
JP
3649}
3650
3651static int rtnl_linkprop(int cmd, struct sk_buff *skb, struct nlmsghdr *nlh,
3652 struct netlink_ext_ack *extack)
3653{
3654 struct net *net = sock_net(skb->sk);
3655 struct nlattr *tb[IFLA_MAX + 1];
3656 struct net_device *dev;
3657 struct ifinfomsg *ifm;
3658 bool changed = false;
3659 struct nlattr *attr;
3660 int err, rem;
3661
3662 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
3663 if (err)
3664 return err;
3665
3666 err = rtnl_ensure_unique_netns(tb, extack, true);
3667 if (err)
3668 return err;
3669
3670 ifm = nlmsg_data(nlh);
cc6090e9 3671 if (ifm->ifi_index > 0)
36fbf1e5 3672 dev = __dev_get_by_index(net, ifm->ifi_index);
76c9ac0e
JP
3673 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3674 dev = rtnl_dev_get(net, tb[IFLA_IFNAME],
3675 tb[IFLA_ALT_IFNAME], NULL);
cc6090e9 3676 else
36fbf1e5 3677 return -EINVAL;
36fbf1e5
JP
3678
3679 if (!dev)
3680 return -ENODEV;
3681
3682 if (!tb[IFLA_PROP_LIST])
3683 return 0;
3684
3685 nla_for_each_nested(attr, tb[IFLA_PROP_LIST], rem) {
3686 switch (nla_type(attr)) {
3687 case IFLA_ALT_IFNAME:
3688 err = rtnl_alt_ifname(cmd, dev, attr, &changed, extack);
3689 if (err)
3690 return err;
3691 break;
3692 }
3693 }
3694
3695 if (changed)
3696 netdev_state_change(dev);
3697 return 0;
3698}
3699
3700static int rtnl_newlinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
3701 struct netlink_ext_ack *extack)
3702{
3703 return rtnl_linkprop(RTM_NEWLINKPROP, skb, nlh, extack);
3704}
3705
3706static int rtnl_dellinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
3707 struct netlink_ext_ack *extack)
3708{
3709 return rtnl_linkprop(RTM_DELLINKPROP, skb, nlh, extack);
3710}
3711
ebfe3c51 3712static u32 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
c7ac8679 3713{
115c9b81 3714 struct net *net = sock_net(skb->sk);
ebfe3c51 3715 size_t min_ifinfo_dump_size = 0;
115c9b81
GR
3716 struct nlattr *tb[IFLA_MAX+1];
3717 u32 ext_filter_mask = 0;
ebfe3c51 3718 struct net_device *dev;
e5eca6d4
MS
3719 int hdrlen;
3720
3721 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3722 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
3723 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
115c9b81 3724
8cb08174 3725 if (nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
a4b64fbe
ED
3726 if (tb[IFLA_EXT_MASK])
3727 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3728 }
115c9b81
GR
3729
3730 if (!ext_filter_mask)
3731 return NLMSG_GOODSIZE;
3732 /*
3733 * traverse the list of net devices and compute the minimum
3734 * buffer size based upon the filter mask.
3735 */
6853dd48
FW
3736 rcu_read_lock();
3737 for_each_netdev_rcu(net, dev) {
ebfe3c51
DZ
3738 min_ifinfo_dump_size = max(min_ifinfo_dump_size,
3739 if_nlmsg_size(dev, ext_filter_mask));
115c9b81 3740 }
6853dd48 3741 rcu_read_unlock();
115c9b81 3742
93af2056 3743 return nlmsg_total_size(min_ifinfo_dump_size);
c7ac8679
GR
3744}
3745
42bad1da 3746static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
1da177e4
LT
3747{
3748 int idx;
3749 int s_idx = cb->family;
87ccbb1f 3750 int type = cb->nlh->nlmsg_type - RTM_BASE;
c63586dc 3751 int ret = 0;
1da177e4
LT
3752
3753 if (s_idx == 0)
3754 s_idx = 1;
6853dd48 3755
25239cee 3756 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
51e13685 3757 struct rtnl_link __rcu **tab;
addf9b90 3758 struct rtnl_link *link;
6853dd48
FW
3759 rtnl_dumpit_func dumpit;
3760
1da177e4
LT
3761 if (idx < s_idx || idx == PF_PACKET)
3762 continue;
6853dd48 3763
addf9b90 3764 if (type < 0 || type >= RTM_NR_MSGTYPES)
1da177e4 3765 continue;
6853dd48 3766
addf9b90
FW
3767 tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]);
3768 if (!tab)
3769 continue;
3770
51e13685 3771 link = rcu_dereference_rtnl(tab[type]);
addf9b90
FW
3772 if (!link)
3773 continue;
3774
3775 dumpit = link->dumpit;
6853dd48
FW
3776 if (!dumpit)
3777 continue;
3778
0465277f 3779 if (idx > s_idx) {
1da177e4 3780 memset(&cb->args[0], 0, sizeof(cb->args));
0465277f
ND
3781 cb->prev_seq = 0;
3782 cb->seq = 0;
3783 }
c63586dc 3784 ret = dumpit(skb, cb);
5e1acb4a 3785 if (ret)
1da177e4
LT
3786 break;
3787 }
3788 cb->family = idx;
3789
c63586dc 3790 return skb->len ? : ret;
1da177e4
LT
3791}
3792
395eea6c 3793struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
3d3ea5af 3794 unsigned int change,
38e01b30
ND
3795 u32 event, gfp_t flags, int *new_nsid,
3796 int new_ifindex)
1da177e4 3797{
c346dca1 3798 struct net *net = dev_net(dev);
1da177e4 3799 struct sk_buff *skb;
0ec6d3f4 3800 int err = -ENOBUFS;
c7ac8679 3801 size_t if_info_size;
1da177e4 3802
7f294054 3803 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
0ec6d3f4
TG
3804 if (skb == NULL)
3805 goto errout;
1da177e4 3806
79e1ad14
JB
3807 err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
3808 type, 0, 0, change, 0, 0, event,
d4e4fdf9 3809 new_nsid, new_ifindex, -1, flags);
26932566
PM
3810 if (err < 0) {
3811 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
3812 WARN_ON(err == -EMSGSIZE);
3813 kfree_skb(skb);
3814 goto errout;
3815 }
395eea6c 3816 return skb;
0ec6d3f4
TG
3817errout:
3818 if (err < 0)
4b3da706 3819 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
395eea6c
MB
3820 return NULL;
3821}
3822
3823void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
3824{
3825 struct net *net = dev_net(dev);
3826
3827 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
3828}
3829
3d3ea5af
VY
3830static void rtmsg_ifinfo_event(int type, struct net_device *dev,
3831 unsigned int change, u32 event,
38e01b30 3832 gfp_t flags, int *new_nsid, int new_ifindex)
395eea6c
MB
3833{
3834 struct sk_buff *skb;
3835
ed2a80ab
ND
3836 if (dev->reg_state != NETREG_REGISTERED)
3837 return;
3838
38e01b30
ND
3839 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid,
3840 new_ifindex);
395eea6c
MB
3841 if (skb)
3842 rtmsg_ifinfo_send(skb, dev, flags);
1da177e4 3843}
3d3ea5af
VY
3844
3845void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
3846 gfp_t flags)
3847{
38e01b30
ND
3848 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3849 NULL, 0);
3d3ea5af 3850}
1da177e4 3851
6621dd29 3852void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
38e01b30 3853 gfp_t flags, int *new_nsid, int new_ifindex)
6621dd29
ND
3854{
3855 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
38e01b30 3856 new_nsid, new_ifindex);
6621dd29
ND
3857}
3858
d83b0603
JF
3859static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
3860 struct net_device *dev,
1e53d5bb 3861 u8 *addr, u16 vid, u32 pid, u32 seq,
1c104a6b 3862 int type, unsigned int flags,
b3379041 3863 int nlflags, u16 ndm_state)
d83b0603
JF
3864{
3865 struct nlmsghdr *nlh;
3866 struct ndmsg *ndm;
3867
1c104a6b 3868 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
d83b0603
JF
3869 if (!nlh)
3870 return -EMSGSIZE;
3871
3872 ndm = nlmsg_data(nlh);
3873 ndm->ndm_family = AF_BRIDGE;
3874 ndm->ndm_pad1 = 0;
3875 ndm->ndm_pad2 = 0;
3876 ndm->ndm_flags = flags;
3877 ndm->ndm_type = 0;
3878 ndm->ndm_ifindex = dev->ifindex;
b3379041 3879 ndm->ndm_state = ndm_state;
d83b0603
JF
3880
3881 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
3882 goto nla_put_failure;
1e53d5bb
HS
3883 if (vid)
3884 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
3885 goto nla_put_failure;
d83b0603 3886
053c095a
JB
3887 nlmsg_end(skb, nlh);
3888 return 0;
d83b0603
JF
3889
3890nla_put_failure:
3891 nlmsg_cancel(skb, nlh);
3892 return -EMSGSIZE;
3893}
3894
3ff661c3
JF
3895static inline size_t rtnl_fdb_nlmsg_size(void)
3896{
f82ef3e1
SD
3897 return NLMSG_ALIGN(sizeof(struct ndmsg)) +
3898 nla_total_size(ETH_ALEN) + /* NDA_LLADDR */
3899 nla_total_size(sizeof(u16)) + /* NDA_VLAN */
3900 0;
3ff661c3
JF
3901}
3902
b3379041
HS
3903static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
3904 u16 ndm_state)
3ff661c3
JF
3905{
3906 struct net *net = dev_net(dev);
3907 struct sk_buff *skb;
3908 int err = -ENOBUFS;
3909
3910 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
3911 if (!skb)
3912 goto errout;
3913
1e53d5bb 3914 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
b3379041 3915 0, 0, type, NTF_SELF, 0, ndm_state);
3ff661c3
JF
3916 if (err < 0) {
3917 kfree_skb(skb);
3918 goto errout;
3919 }
3920
3921 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
3922 return;
3923errout:
3924 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
3925}
3926
a986967e 3927/*
090096bf
VY
3928 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3929 */
3930int ndo_dflt_fdb_add(struct ndmsg *ndm,
3931 struct nlattr *tb[],
3932 struct net_device *dev,
f6f6424b 3933 const unsigned char *addr, u16 vid,
090096bf
VY
3934 u16 flags)
3935{
3936 int err = -EINVAL;
3937
3938 /* If aging addresses are supported device will need to
3939 * implement its own handler for this.
3940 */
3941 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
3942 pr_info("%s: FDB only supports static addresses\n", dev->name);
3943 return err;
3944 }
3945
65891fea
OG
3946 if (vid) {
3947 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
3948 return err;
3949 }
3950
090096bf
VY
3951 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3952 err = dev_uc_add_excl(dev, addr);
3953 else if (is_multicast_ether_addr(addr))
3954 err = dev_mc_add_excl(dev, addr);
3955
3956 /* Only return duplicate errors if NLM_F_EXCL is set */
3957 if (err == -EEXIST && !(flags & NLM_F_EXCL))
3958 err = 0;
3959
3960 return err;
3961}
3962EXPORT_SYMBOL(ndo_dflt_fdb_add);
3963
b88d12e4
FW
3964static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
3965 struct netlink_ext_ack *extack)
f6f6424b
JP
3966{
3967 u16 vid = 0;
3968
3969 if (vlan_attr) {
3970 if (nla_len(vlan_attr) != sizeof(u16)) {
b88d12e4 3971 NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
f6f6424b
JP
3972 return -EINVAL;
3973 }
3974
3975 vid = nla_get_u16(vlan_attr);
3976
3977 if (!vid || vid >= VLAN_VID_MASK) {
b88d12e4 3978 NL_SET_ERR_MSG(extack, "invalid vlan id");
f6f6424b
JP
3979 return -EINVAL;
3980 }
3981 }
3982 *p_vid = vid;
3983 return 0;
3984}
3985
c21ef3e3
DA
3986static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
3987 struct netlink_ext_ack *extack)
77162022
JF
3988{
3989 struct net *net = sock_net(skb->sk);
77162022
JF
3990 struct ndmsg *ndm;
3991 struct nlattr *tb[NDA_MAX+1];
3992 struct net_device *dev;
3993 u8 *addr;
f6f6424b 3994 u16 vid;
77162022
JF
3995 int err;
3996
8cb08174
JB
3997 err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
3998 extack);
77162022
JF
3999 if (err < 0)
4000 return err;
4001
4002 ndm = nlmsg_data(nlh);
4003 if (ndm->ndm_ifindex == 0) {
b88d12e4 4004 NL_SET_ERR_MSG(extack, "invalid ifindex");
77162022
JF
4005 return -EINVAL;
4006 }
4007
4008 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
4009 if (dev == NULL) {
b88d12e4 4010 NL_SET_ERR_MSG(extack, "unknown ifindex");
77162022
JF
4011 return -ENODEV;
4012 }
4013
4014 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
b88d12e4 4015 NL_SET_ERR_MSG(extack, "invalid address");
77162022
JF
4016 return -EINVAL;
4017 }
4018
da715775
IS
4019 if (dev->type != ARPHRD_ETHER) {
4020 NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices");
4021 return -EINVAL;
4022 }
4023
77162022 4024 addr = nla_data(tb[NDA_LLADDR]);
77162022 4025
b88d12e4 4026 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
f6f6424b
JP
4027 if (err)
4028 return err;
4029
77162022
JF
4030 err = -EOPNOTSUPP;
4031
4032 /* Support fdb on master device the net/bridge default case */
4033 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2e92a2d0 4034 netif_is_bridge_port(dev)) {
898e5061
JP
4035 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4036 const struct net_device_ops *ops = br_dev->netdev_ops;
4037
f6f6424b 4038 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
87b0984e 4039 nlh->nlmsg_flags, extack);
77162022
JF
4040 if (err)
4041 goto out;
4042 else
4043 ndm->ndm_flags &= ~NTF_MASTER;
4044 }
4045
4046 /* Embedded bridge, macvlan, and any other device support */
090096bf
VY
4047 if ((ndm->ndm_flags & NTF_SELF)) {
4048 if (dev->netdev_ops->ndo_fdb_add)
4049 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
f6f6424b 4050 vid,
87b0984e
PM
4051 nlh->nlmsg_flags,
4052 extack);
090096bf 4053 else
f6f6424b 4054 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
090096bf 4055 nlh->nlmsg_flags);
77162022 4056
3ff661c3 4057 if (!err) {
b3379041
HS
4058 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
4059 ndm->ndm_state);
77162022 4060 ndm->ndm_flags &= ~NTF_SELF;
3ff661c3 4061 }
77162022
JF
4062 }
4063out:
4064 return err;
4065}
4066
a986967e 4067/*
090096bf
VY
4068 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
4069 */
4070int ndo_dflt_fdb_del(struct ndmsg *ndm,
4071 struct nlattr *tb[],
4072 struct net_device *dev,
f6f6424b 4073 const unsigned char *addr, u16 vid)
090096bf 4074{
c8a89c4a 4075 int err = -EINVAL;
090096bf
VY
4076
4077 /* If aging addresses are supported device will need to
4078 * implement its own handler for this.
4079 */
64535993 4080 if (!(ndm->ndm_state & NUD_PERMANENT)) {
090096bf 4081 pr_info("%s: FDB only supports static addresses\n", dev->name);
c8a89c4a 4082 return err;
090096bf
VY
4083 }
4084
4085 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
4086 err = dev_uc_del(dev, addr);
4087 else if (is_multicast_ether_addr(addr))
4088 err = dev_mc_del(dev, addr);
090096bf
VY
4089
4090 return err;
4091}
4092EXPORT_SYMBOL(ndo_dflt_fdb_del);
4093
c21ef3e3
DA
4094static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
4095 struct netlink_ext_ack *extack)
77162022
JF
4096{
4097 struct net *net = sock_net(skb->sk);
4098 struct ndmsg *ndm;
1690be63 4099 struct nlattr *tb[NDA_MAX+1];
77162022 4100 struct net_device *dev;
77162022 4101 __u8 *addr;
7d311801 4102 int err;
f6f6424b 4103 u16 vid;
77162022 4104
90f62cf3 4105 if (!netlink_capable(skb, CAP_NET_ADMIN))
1690be63
VY
4106 return -EPERM;
4107
8cb08174
JB
4108 err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
4109 extack);
1690be63
VY
4110 if (err < 0)
4111 return err;
77162022
JF
4112
4113 ndm = nlmsg_data(nlh);
4114 if (ndm->ndm_ifindex == 0) {
b88d12e4 4115 NL_SET_ERR_MSG(extack, "invalid ifindex");
77162022
JF
4116 return -EINVAL;
4117 }
4118
4119 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
4120 if (dev == NULL) {
b88d12e4 4121 NL_SET_ERR_MSG(extack, "unknown ifindex");
77162022
JF
4122 return -ENODEV;
4123 }
4124
1690be63 4125 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
b88d12e4 4126 NL_SET_ERR_MSG(extack, "invalid address");
1690be63
VY
4127 return -EINVAL;
4128 }
4129
da715775
IS
4130 if (dev->type != ARPHRD_ETHER) {
4131 NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices");
4132 return -EINVAL;
4133 }
4134
1690be63 4135 addr = nla_data(tb[NDA_LLADDR]);
77162022 4136
b88d12e4 4137 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
f6f6424b
JP
4138 if (err)
4139 return err;
4140
77162022
JF
4141 err = -EOPNOTSUPP;
4142
4143 /* Support fdb on master device the net/bridge default case */
4144 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2e92a2d0 4145 netif_is_bridge_port(dev)) {
898e5061
JP
4146 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4147 const struct net_device_ops *ops = br_dev->netdev_ops;
77162022 4148
898e5061 4149 if (ops->ndo_fdb_del)
f6f6424b 4150 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
77162022
JF
4151
4152 if (err)
4153 goto out;
4154 else
4155 ndm->ndm_flags &= ~NTF_MASTER;
4156 }
4157
4158 /* Embedded bridge, macvlan, and any other device support */
090096bf
VY
4159 if (ndm->ndm_flags & NTF_SELF) {
4160 if (dev->netdev_ops->ndo_fdb_del)
f6f6424b
JP
4161 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
4162 vid);
090096bf 4163 else
f6f6424b 4164 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
77162022 4165
3ff661c3 4166 if (!err) {
b3379041
HS
4167 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
4168 ndm->ndm_state);
77162022 4169 ndm->ndm_flags &= ~NTF_SELF;
3ff661c3 4170 }
77162022
JF
4171 }
4172out:
4173 return err;
4174}
4175
d83b0603
JF
4176static int nlmsg_populate_fdb(struct sk_buff *skb,
4177 struct netlink_callback *cb,
4178 struct net_device *dev,
4179 int *idx,
4180 struct netdev_hw_addr_list *list)
4181{
4182 struct netdev_hw_addr *ha;
4183 int err;
15e47304 4184 u32 portid, seq;
d83b0603 4185
15e47304 4186 portid = NETLINK_CB(cb->skb).portid;
d83b0603
JF
4187 seq = cb->nlh->nlmsg_seq;
4188
4189 list_for_each_entry(ha, &list->list, list) {
d297653d 4190 if (*idx < cb->args[2])
d83b0603
JF
4191 goto skip;
4192
1e53d5bb 4193 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
a7a558fe 4194 portid, seq,
1c104a6b 4195 RTM_NEWNEIGH, NTF_SELF,
b3379041 4196 NLM_F_MULTI, NUD_PERMANENT);
d83b0603
JF
4197 if (err < 0)
4198 return err;
4199skip:
4200 *idx += 1;
4201 }
4202 return 0;
4203}
4204
4205/**
2c53040f 4206 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
a986967e
BVA
4207 * @skb: socket buffer to store message in
4208 * @cb: netlink callback
d83b0603 4209 * @dev: netdevice
a986967e
BVA
4210 * @filter_dev: ignored
4211 * @idx: the number of FDB table entries dumped is added to *@idx
d83b0603
JF
4212 *
4213 * Default netdevice operation to dump the existing unicast address list.
91f3e7b1 4214 * Returns number of addresses from list put in skb.
d83b0603
JF
4215 */
4216int ndo_dflt_fdb_dump(struct sk_buff *skb,
4217 struct netlink_callback *cb,
4218 struct net_device *dev,
5d5eacb3 4219 struct net_device *filter_dev,
d297653d 4220 int *idx)
d83b0603
JF
4221{
4222 int err;
4223
68883893
ED
4224 if (dev->type != ARPHRD_ETHER)
4225 return -EINVAL;
4226
d83b0603 4227 netif_addr_lock_bh(dev);
d297653d 4228 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
d83b0603
JF
4229 if (err)
4230 goto out;
2934c9db 4231 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
d83b0603
JF
4232out:
4233 netif_addr_unlock_bh(dev);
d297653d 4234 return err;
d83b0603
JF
4235}
4236EXPORT_SYMBOL(ndo_dflt_fdb_dump);
4237
8c6e137f
DA
4238static int valid_fdb_dump_strict(const struct nlmsghdr *nlh,
4239 int *br_idx, int *brport_idx,
4240 struct netlink_ext_ack *extack)
4241{
4242 struct nlattr *tb[NDA_MAX + 1];
4243 struct ndmsg *ndm;
4244 int err, i;
4245
4246 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
4247 NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request");
4248 return -EINVAL;
4249 }
4250
4251 ndm = nlmsg_data(nlh);
4252 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
4253 ndm->ndm_flags || ndm->ndm_type) {
8b73018f 4254 NL_SET_ERR_MSG(extack, "Invalid values in header for fdb dump request");
8c6e137f
DA
4255 return -EINVAL;
4256 }
4257
8cb08174
JB
4258 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
4259 NDA_MAX, NULL, extack);
8c6e137f
DA
4260 if (err < 0)
4261 return err;
4262
4263 *brport_idx = ndm->ndm_ifindex;
4264 for (i = 0; i <= NDA_MAX; ++i) {
4265 if (!tb[i])
4266 continue;
4267
4268 switch (i) {
4269 case NDA_IFINDEX:
4270 if (nla_len(tb[i]) != sizeof(u32)) {
4271 NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request");
4272 return -EINVAL;
4273 }
4274 *brport_idx = nla_get_u32(tb[NDA_IFINDEX]);
4275 break;
4276 case NDA_MASTER:
4277 if (nla_len(tb[i]) != sizeof(u32)) {
4278 NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request");
4279 return -EINVAL;
4280 }
4281 *br_idx = nla_get_u32(tb[NDA_MASTER]);
4282 break;
4283 default:
4284 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request");
4285 return -EINVAL;
4286 }
4287 }
4288
4289 return 0;
4290}
4291
8dfbda19
DA
4292static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh,
4293 int *br_idx, int *brport_idx,
4294 struct netlink_ext_ack *extack)
77162022 4295{
5e6d2435 4296 struct nlattr *tb[IFLA_MAX+1];
8dfbda19 4297 int err;
5e6d2435 4298
bd961c9b
MFO
4299 /* A hack to preserve kernel<->userspace interface.
4300 * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0.
4301 * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails.
4302 * So, check for ndmsg with an optional u32 attribute (not used here).
4303 * Fortunately these sizes don't conflict with the size of ifinfomsg
4304 * with an optional attribute.
4305 */
8dfbda19
DA
4306 if (nlmsg_len(nlh) != sizeof(struct ndmsg) &&
4307 (nlmsg_len(nlh) != sizeof(struct ndmsg) +
bd961c9b 4308 nla_attr_size(sizeof(u32)))) {
4565d7e5
DA
4309 struct ifinfomsg *ifm;
4310
8cb08174
JB
4311 err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
4312 tb, IFLA_MAX, ifla_policy,
4313 extack);
bd961c9b
MFO
4314 if (err < 0) {
4315 return -EINVAL;
4316 } else if (err == 0) {
4317 if (tb[IFLA_MASTER])
8dfbda19 4318 *br_idx = nla_get_u32(tb[IFLA_MASTER]);
bd961c9b 4319 }
5e6d2435 4320
4565d7e5 4321 ifm = nlmsg_data(nlh);
8dfbda19 4322 *brport_idx = ifm->ifi_index;
bd961c9b 4323 }
8dfbda19
DA
4324 return 0;
4325}
4326
4327static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
4328{
4329 struct net_device *dev;
4330 struct net_device *br_dev = NULL;
4331 const struct net_device_ops *ops = NULL;
4332 const struct net_device_ops *cops = NULL;
4333 struct net *net = sock_net(skb->sk);
4334 struct hlist_head *head;
4335 int brport_idx = 0;
4336 int br_idx = 0;
4337 int h, s_h;
4338 int idx = 0, s_idx;
4339 int err = 0;
4340 int fidx = 0;
4341
8c6e137f
DA
4342 if (cb->strict_check)
4343 err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx,
4344 cb->extack);
4345 else
4346 err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx,
4347 cb->extack);
8dfbda19
DA
4348 if (err < 0)
4349 return err;
5e6d2435
JHS
4350
4351 if (br_idx) {
4352 br_dev = __dev_get_by_index(net, br_idx);
4353 if (!br_dev)
4354 return -ENODEV;
4355
4356 ops = br_dev->netdev_ops;
5e6d2435
JHS
4357 }
4358
d297653d
RP
4359 s_h = cb->args[0];
4360 s_idx = cb->args[1];
5e6d2435 4361
d297653d
RP
4362 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4363 idx = 0;
4364 head = &net->dev_index_head[h];
4365 hlist_for_each_entry(dev, head, index_hlist) {
5e6d2435 4366
d297653d 4367 if (brport_idx && (dev->ifindex != brport_idx))
5e6d2435
JHS
4368 continue;
4369
d297653d 4370 if (!br_idx) { /* user did not specify a specific bridge */
2e92a2d0 4371 if (netif_is_bridge_port(dev)) {
d297653d
RP
4372 br_dev = netdev_master_upper_dev_get(dev);
4373 cops = br_dev->netdev_ops;
4374 }
4375 } else {
4376 if (dev != br_dev &&
2e92a2d0 4377 !netif_is_bridge_port(dev))
d297653d 4378 continue;
5e6d2435 4379
d297653d
RP
4380 if (br_dev != netdev_master_upper_dev_get(dev) &&
4381 !(dev->priv_flags & IFF_EBRIDGE))
4382 continue;
4383 cops = ops;
4384 }
77162022 4385
d297653d
RP
4386 if (idx < s_idx)
4387 goto cont;
77162022 4388
2e92a2d0 4389 if (netif_is_bridge_port(dev)) {
d297653d
RP
4390 if (cops && cops->ndo_fdb_dump) {
4391 err = cops->ndo_fdb_dump(skb, cb,
4392 br_dev, dev,
4393 &fidx);
4394 if (err == -EMSGSIZE)
4395 goto out;
4396 }
4397 }
5e6d2435 4398
d297653d
RP
4399 if (dev->netdev_ops->ndo_fdb_dump)
4400 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
4401 dev, NULL,
4402 &fidx);
4403 else
4404 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
4405 &fidx);
4406 if (err == -EMSGSIZE)
4407 goto out;
4408
4409 cops = NULL;
4410
4411 /* reset fdb offset to 0 for rest of the interfaces */
4412 cb->args[2] = 0;
4413 fidx = 0;
4414cont:
4415 idx++;
4416 }
77162022 4417 }
77162022 4418
d297653d
RP
4419out:
4420 cb->args[0] = h;
4421 cb->args[1] = idx;
4422 cb->args[2] = fidx;
4423
77162022
JF
4424 return skb->len;
4425}
4426
5b2f94b2
RP
4427static int valid_fdb_get_strict(const struct nlmsghdr *nlh,
4428 struct nlattr **tb, u8 *ndm_flags,
4429 int *br_idx, int *brport_idx, u8 **addr,
4430 u16 *vid, struct netlink_ext_ack *extack)
4431{
4432 struct ndmsg *ndm;
4433 int err, i;
4434
4435 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
4436 NL_SET_ERR_MSG(extack, "Invalid header for fdb get request");
4437 return -EINVAL;
4438 }
4439
4440 ndm = nlmsg_data(nlh);
4441 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
4442 ndm->ndm_type) {
4443 NL_SET_ERR_MSG(extack, "Invalid values in header for fdb get request");
4444 return -EINVAL;
4445 }
4446
4447 if (ndm->ndm_flags & ~(NTF_MASTER | NTF_SELF)) {
4448 NL_SET_ERR_MSG(extack, "Invalid flags in header for fdb get request");
4449 return -EINVAL;
4450 }
4451
8cb08174
JB
4452 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
4453 NDA_MAX, nda_policy, extack);
5b2f94b2
RP
4454 if (err < 0)
4455 return err;
4456
4457 *ndm_flags = ndm->ndm_flags;
4458 *brport_idx = ndm->ndm_ifindex;
4459 for (i = 0; i <= NDA_MAX; ++i) {
4460 if (!tb[i])
4461 continue;
4462
4463 switch (i) {
4464 case NDA_MASTER:
4465 *br_idx = nla_get_u32(tb[i]);
4466 break;
4467 case NDA_LLADDR:
4468 if (nla_len(tb[i]) != ETH_ALEN) {
4469 NL_SET_ERR_MSG(extack, "Invalid address in fdb get request");
4470 return -EINVAL;
4471 }
4472 *addr = nla_data(tb[i]);
4473 break;
4474 case NDA_VLAN:
4475 err = fdb_vid_parse(tb[i], vid, extack);
4476 if (err)
4477 return err;
4478 break;
4479 case NDA_VNI:
4480 break;
4481 default:
4482 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb get request");
4483 return -EINVAL;
4484 }
4485 }
4486
4487 return 0;
4488}
4489
4490static int rtnl_fdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh,
4491 struct netlink_ext_ack *extack)
4492{
4493 struct net_device *dev = NULL, *br_dev = NULL;
4494 const struct net_device_ops *ops = NULL;
4495 struct net *net = sock_net(in_skb->sk);
4496 struct nlattr *tb[NDA_MAX + 1];
4497 struct sk_buff *skb;
4498 int brport_idx = 0;
4499 u8 ndm_flags = 0;
4500 int br_idx = 0;
4501 u8 *addr = NULL;
4502 u16 vid = 0;
4503 int err;
4504
4505 err = valid_fdb_get_strict(nlh, tb, &ndm_flags, &br_idx,
4506 &brport_idx, &addr, &vid, extack);
4507 if (err < 0)
4508 return err;
4509
f989d03e
NA
4510 if (!addr) {
4511 NL_SET_ERR_MSG(extack, "Missing lookup address for fdb get request");
4512 return -EINVAL;
4513 }
4514
5b2f94b2
RP
4515 if (brport_idx) {
4516 dev = __dev_get_by_index(net, brport_idx);
4517 if (!dev) {
4518 NL_SET_ERR_MSG(extack, "Unknown device ifindex");
4519 return -ENODEV;
4520 }
4521 }
4522
4523 if (br_idx) {
4524 if (dev) {
4525 NL_SET_ERR_MSG(extack, "Master and device are mutually exclusive");
4526 return -EINVAL;
4527 }
4528
4529 br_dev = __dev_get_by_index(net, br_idx);
4530 if (!br_dev) {
4531 NL_SET_ERR_MSG(extack, "Invalid master ifindex");
4532 return -EINVAL;
4533 }
4534 ops = br_dev->netdev_ops;
4535 }
4536
4537 if (dev) {
4538 if (!ndm_flags || (ndm_flags & NTF_MASTER)) {
2e92a2d0 4539 if (!netif_is_bridge_port(dev)) {
5b2f94b2
RP
4540 NL_SET_ERR_MSG(extack, "Device is not a bridge port");
4541 return -EINVAL;
4542 }
4543 br_dev = netdev_master_upper_dev_get(dev);
4544 if (!br_dev) {
4545 NL_SET_ERR_MSG(extack, "Master of device not found");
4546 return -EINVAL;
4547 }
4548 ops = br_dev->netdev_ops;
4549 } else {
4550 if (!(ndm_flags & NTF_SELF)) {
4551 NL_SET_ERR_MSG(extack, "Missing NTF_SELF");
4552 return -EINVAL;
4553 }
4554 ops = dev->netdev_ops;
4555 }
4556 }
4557
4558 if (!br_dev && !dev) {
4559 NL_SET_ERR_MSG(extack, "No device specified");
4560 return -ENODEV;
4561 }
4562
4563 if (!ops || !ops->ndo_fdb_get) {
4564 NL_SET_ERR_MSG(extack, "Fdb get operation not supported by device");
4565 return -EOPNOTSUPP;
4566 }
4567
4568 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
4569 if (!skb)
4570 return -ENOBUFS;
4571
4572 if (br_dev)
4573 dev = br_dev;
4574 err = ops->ndo_fdb_get(skb, tb, dev, addr, vid,
4575 NETLINK_CB(in_skb).portid,
4576 nlh->nlmsg_seq, extack);
4577 if (err)
4578 goto out;
4579
4580 return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
4581out:
4582 kfree_skb(skb);
4583 return err;
4584}
4585
2c3c031c
SF
4586static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
4587 unsigned int attrnum, unsigned int flag)
4588{
4589 if (mask & flag)
4590 return nla_put_u8(skb, attrnum, !!(flags & flag));
4591 return 0;
4592}
4593
815cccbf 4594int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
2c3c031c 4595 struct net_device *dev, u16 mode,
7d4f8d87
SF
4596 u32 flags, u32 mask, int nlflags,
4597 u32 filter_mask,
4598 int (*vlan_fill)(struct sk_buff *skb,
4599 struct net_device *dev,
4600 u32 filter_mask))
815cccbf
JF
4601{
4602 struct nlmsghdr *nlh;
4603 struct ifinfomsg *ifm;
4604 struct nlattr *br_afspec;
2c3c031c 4605 struct nlattr *protinfo;
815cccbf 4606 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
898e5061 4607 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
7d4f8d87 4608 int err = 0;
815cccbf 4609
46c264da 4610 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
815cccbf
JF
4611 if (nlh == NULL)
4612 return -EMSGSIZE;
4613
4614 ifm = nlmsg_data(nlh);
4615 ifm->ifi_family = AF_BRIDGE;
4616 ifm->__ifi_pad = 0;
4617 ifm->ifi_type = dev->type;
4618 ifm->ifi_index = dev->ifindex;
4619 ifm->ifi_flags = dev_get_flags(dev);
4620 ifm->ifi_change = 0;
4621
4622
4623 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4624 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4625 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
898e5061
JP
4626 (br_dev &&
4627 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
815cccbf
JF
4628 (dev->addr_len &&
4629 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
a54acb3a
ND
4630 (dev->ifindex != dev_get_iflink(dev) &&
4631 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
815cccbf
JF
4632 goto nla_put_failure;
4633
ae0be8de 4634 br_afspec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
815cccbf
JF
4635 if (!br_afspec)
4636 goto nla_put_failure;
4637
1d460b98 4638 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
815cccbf
JF
4639 nla_nest_cancel(skb, br_afspec);
4640 goto nla_put_failure;
4641 }
1d460b98
RP
4642
4643 if (mode != BRIDGE_MODE_UNDEF) {
4644 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
4645 nla_nest_cancel(skb, br_afspec);
4646 goto nla_put_failure;
4647 }
4648 }
7d4f8d87
SF
4649 if (vlan_fill) {
4650 err = vlan_fill(skb, dev, filter_mask);
4651 if (err) {
4652 nla_nest_cancel(skb, br_afspec);
4653 goto nla_put_failure;
4654 }
4655 }
815cccbf
JF
4656 nla_nest_end(skb, br_afspec);
4657
ae0be8de 4658 protinfo = nla_nest_start(skb, IFLA_PROTINFO);
2c3c031c
SF
4659 if (!protinfo)
4660 goto nla_put_failure;
4661
4662 if (brport_nla_put_flag(skb, flags, mask,
4663 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
4664 brport_nla_put_flag(skb, flags, mask,
4665 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
4666 brport_nla_put_flag(skb, flags, mask,
4667 IFLA_BRPORT_FAST_LEAVE,
4668 BR_MULTICAST_FAST_LEAVE) ||
4669 brport_nla_put_flag(skb, flags, mask,
4670 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
4671 brport_nla_put_flag(skb, flags, mask,
4672 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
4673 brport_nla_put_flag(skb, flags, mask,
4674 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
4675 brport_nla_put_flag(skb, flags, mask,
4676 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
4677 brport_nla_put_flag(skb, flags, mask,
583cb0b4
JW
4678 IFLA_BRPORT_PROXYARP, BR_PROXYARP) ||
4679 brport_nla_put_flag(skb, flags, mask,
4680 IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD) ||
4681 brport_nla_put_flag(skb, flags, mask,
4682 IFLA_BRPORT_BCAST_FLOOD, BR_BCAST_FLOOD)) {
2c3c031c
SF
4683 nla_nest_cancel(skb, protinfo);
4684 goto nla_put_failure;
4685 }
4686
4687 nla_nest_end(skb, protinfo);
4688
053c095a
JB
4689 nlmsg_end(skb, nlh);
4690 return 0;
815cccbf
JF
4691nla_put_failure:
4692 nlmsg_cancel(skb, nlh);
7d4f8d87 4693 return err ? err : -EMSGSIZE;
815cccbf 4694}
7d4f8d87 4695EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
815cccbf 4696
2d011be8
DA
4697static int valid_bridge_getlink_req(const struct nlmsghdr *nlh,
4698 bool strict_check, u32 *filter_mask,
4699 struct netlink_ext_ack *extack)
4700{
4701 struct nlattr *tb[IFLA_MAX+1];
4702 int err, i;
4703
4704 if (strict_check) {
4705 struct ifinfomsg *ifm;
4706
4707 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
4708 NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump");
4709 return -EINVAL;
4710 }
4711
4712 ifm = nlmsg_data(nlh);
4713 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
4714 ifm->ifi_change || ifm->ifi_index) {
4715 NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request");
4716 return -EINVAL;
4717 }
4718
8cb08174
JB
4719 err = nlmsg_parse_deprecated_strict(nlh,
4720 sizeof(struct ifinfomsg),
4721 tb, IFLA_MAX, ifla_policy,
4722 extack);
2d011be8 4723 } else {
8cb08174
JB
4724 err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
4725 tb, IFLA_MAX, ifla_policy,
4726 extack);
2d011be8
DA
4727 }
4728 if (err < 0)
4729 return err;
4730
4731 /* new attributes should only be added with strict checking */
4732 for (i = 0; i <= IFLA_MAX; ++i) {
4733 if (!tb[i])
4734 continue;
4735
4736 switch (i) {
4737 case IFLA_EXT_MASK:
4738 *filter_mask = nla_get_u32(tb[i]);
4739 break;
4740 default:
4741 if (strict_check) {
4742 NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request");
4743 return -EINVAL;
4744 }
4745 }
4746 }
4747
4748 return 0;
4749}
4750
e5a55a89
JF
4751static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
4752{
2d011be8 4753 const struct nlmsghdr *nlh = cb->nlh;
e5a55a89
JF
4754 struct net *net = sock_net(skb->sk);
4755 struct net_device *dev;
4756 int idx = 0;
4757 u32 portid = NETLINK_CB(cb->skb).portid;
2d011be8 4758 u32 seq = nlh->nlmsg_seq;
6cbdceeb 4759 u32 filter_mask = 0;
d64f69b0 4760 int err;
6cbdceeb 4761
2d011be8
DA
4762 err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask,
4763 cb->extack);
4764 if (err < 0 && cb->strict_check)
4765 return err;
e5a55a89
JF
4766
4767 rcu_read_lock();
4768 for_each_netdev_rcu(net, dev) {
4769 const struct net_device_ops *ops = dev->netdev_ops;
898e5061 4770 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
e5a55a89 4771
898e5061 4772 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
d64f69b0
RP
4773 if (idx >= cb->args[0]) {
4774 err = br_dev->netdev_ops->ndo_bridge_getlink(
4775 skb, portid, seq, dev,
4776 filter_mask, NLM_F_MULTI);
f6c5775f
DA
4777 if (err < 0 && err != -EOPNOTSUPP) {
4778 if (likely(skb->len))
4779 break;
4780
4781 goto out_err;
4782 }
d64f69b0 4783 }
25b1e679 4784 idx++;
e5a55a89
JF
4785 }
4786
4787 if (ops->ndo_bridge_getlink) {
d64f69b0
RP
4788 if (idx >= cb->args[0]) {
4789 err = ops->ndo_bridge_getlink(skb, portid,
4790 seq, dev,
4791 filter_mask,
4792 NLM_F_MULTI);
f6c5775f
DA
4793 if (err < 0 && err != -EOPNOTSUPP) {
4794 if (likely(skb->len))
4795 break;
4796
4797 goto out_err;
4798 }
d64f69b0 4799 }
25b1e679 4800 idx++;
e5a55a89
JF
4801 }
4802 }
f6c5775f
DA
4803 err = skb->len;
4804out_err:
e5a55a89
JF
4805 rcu_read_unlock();
4806 cb->args[0] = idx;
4807
f6c5775f 4808 return err;
e5a55a89
JF
4809}
4810
2469ffd7
JF
4811static inline size_t bridge_nlmsg_size(void)
4812{
4813 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4814 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4815 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4816 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
4817 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
4818 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
4819 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
4820 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
4821 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
4822 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
4823 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
4824}
4825
02dba438 4826static int rtnl_bridge_notify(struct net_device *dev)
2469ffd7
JF
4827{
4828 struct net *net = dev_net(dev);
2469ffd7
JF
4829 struct sk_buff *skb;
4830 int err = -EOPNOTSUPP;
4831
02dba438
RP
4832 if (!dev->netdev_ops->ndo_bridge_getlink)
4833 return 0;
4834
2469ffd7
JF
4835 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
4836 if (!skb) {
4837 err = -ENOMEM;
4838 goto errout;
4839 }
4840
46c264da 4841 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
02dba438
RP
4842 if (err < 0)
4843 goto errout;
2469ffd7 4844
59ccaaaa
RP
4845 if (!skb->len)
4846 goto errout;
4847
2469ffd7
JF
4848 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
4849 return 0;
4850errout:
4851 WARN_ON(err == -EMSGSIZE);
4852 kfree_skb(skb);
59ccaaaa
RP
4853 if (err)
4854 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
2469ffd7
JF
4855 return err;
4856}
4857
c21ef3e3
DA
4858static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
4859 struct netlink_ext_ack *extack)
e5a55a89
JF
4860{
4861 struct net *net = sock_net(skb->sk);
4862 struct ifinfomsg *ifm;
4863 struct net_device *dev;
2469ffd7
JF
4864 struct nlattr *br_spec, *attr = NULL;
4865 int rem, err = -EOPNOTSUPP;
4de8b413 4866 u16 flags = 0;
c38e01b8 4867 bool have_flags = false;
e5a55a89
JF
4868
4869 if (nlmsg_len(nlh) < sizeof(*ifm))
4870 return -EINVAL;
4871
4872 ifm = nlmsg_data(nlh);
4873 if (ifm->ifi_family != AF_BRIDGE)
4874 return -EPFNOSUPPORT;
4875
4876 dev = __dev_get_by_index(net, ifm->ifi_index);
4877 if (!dev) {
b88d12e4 4878 NL_SET_ERR_MSG(extack, "unknown ifindex");
e5a55a89
JF
4879 return -ENODEV;
4880 }
4881
2469ffd7
JF
4882 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4883 if (br_spec) {
4884 nla_for_each_nested(attr, br_spec, rem) {
4885 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
6e8d1c55
TG
4886 if (nla_len(attr) < sizeof(flags))
4887 return -EINVAL;
4888
c38e01b8 4889 have_flags = true;
2469ffd7
JF
4890 flags = nla_get_u16(attr);
4891 break;
4892 }
4893 }
4894 }
4895
4896 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
898e5061
JP
4897 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4898
4899 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
2469ffd7
JF
4900 err = -EOPNOTSUPP;
4901 goto out;
4902 }
4903
2fd527b7
PM
4904 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags,
4905 extack);
e5a55a89
JF
4906 if (err)
4907 goto out;
2469ffd7
JF
4908
4909 flags &= ~BRIDGE_FLAGS_MASTER;
e5a55a89
JF
4910 }
4911
2469ffd7
JF
4912 if ((flags & BRIDGE_FLAGS_SELF)) {
4913 if (!dev->netdev_ops->ndo_bridge_setlink)
4914 err = -EOPNOTSUPP;
4915 else
add511b3 4916 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
2fd527b7
PM
4917 flags,
4918 extack);
02dba438 4919 if (!err) {
2469ffd7 4920 flags &= ~BRIDGE_FLAGS_SELF;
02dba438
RP
4921
4922 /* Generate event to notify upper layer of bridge
4923 * change
4924 */
4925 err = rtnl_bridge_notify(dev);
4926 }
2469ffd7 4927 }
e5a55a89 4928
c38e01b8 4929 if (have_flags)
2469ffd7 4930 memcpy(nla_data(attr), &flags, sizeof(flags));
e5a55a89
JF
4931out:
4932 return err;
4933}
4934
c21ef3e3
DA
4935static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
4936 struct netlink_ext_ack *extack)
407af329
VY
4937{
4938 struct net *net = sock_net(skb->sk);
4939 struct ifinfomsg *ifm;
4940 struct net_device *dev;
4941 struct nlattr *br_spec, *attr = NULL;
4942 int rem, err = -EOPNOTSUPP;
4de8b413 4943 u16 flags = 0;
407af329
VY
4944 bool have_flags = false;
4945
4946 if (nlmsg_len(nlh) < sizeof(*ifm))
4947 return -EINVAL;
4948
4949 ifm = nlmsg_data(nlh);
4950 if (ifm->ifi_family != AF_BRIDGE)
4951 return -EPFNOSUPPORT;
4952
4953 dev = __dev_get_by_index(net, ifm->ifi_index);
4954 if (!dev) {
b88d12e4 4955 NL_SET_ERR_MSG(extack, "unknown ifindex");
407af329
VY
4956 return -ENODEV;
4957 }
4958
4959 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4960 if (br_spec) {
4961 nla_for_each_nested(attr, br_spec, rem) {
4962 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
6e8d1c55
TG
4963 if (nla_len(attr) < sizeof(flags))
4964 return -EINVAL;
4965
407af329
VY
4966 have_flags = true;
4967 flags = nla_get_u16(attr);
4968 break;
4969 }
4970 }
4971 }
4972
407af329
VY
4973 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
4974 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4975
4976 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
4977 err = -EOPNOTSUPP;
4978 goto out;
4979 }
4980
add511b3 4981 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
407af329
VY
4982 if (err)
4983 goto out;
4984
4985 flags &= ~BRIDGE_FLAGS_MASTER;
4986 }
4987
4988 if ((flags & BRIDGE_FLAGS_SELF)) {
4989 if (!dev->netdev_ops->ndo_bridge_dellink)
4990 err = -EOPNOTSUPP;
4991 else
add511b3
RP
4992 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
4993 flags);
407af329 4994
02dba438 4995 if (!err) {
407af329 4996 flags &= ~BRIDGE_FLAGS_SELF;
02dba438
RP
4997
4998 /* Generate event to notify upper layer of bridge
4999 * change
5000 */
5001 err = rtnl_bridge_notify(dev);
5002 }
407af329
VY
5003 }
5004
5005 if (have_flags)
5006 memcpy(nla_data(attr), &flags, sizeof(flags));
407af329
VY
5007out:
5008 return err;
5009}
5010
e8872a25
NA
5011static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
5012{
5013 return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
5014 (!idxattr || idxattr == attrid);
5015}
5016
69ae6ad2
NF
5017#define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
5018static int rtnl_get_offload_stats_attr_size(int attr_id)
5019{
5020 switch (attr_id) {
5021 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
5022 return sizeof(struct rtnl_link_stats64);
5023 }
5024
5025 return 0;
5026}
5027
5028static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
5029 int *prividx)
5030{
5031 struct nlattr *attr = NULL;
5032 int attr_id, size;
5033 void *attr_data;
5034 int err;
5035
5036 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
5037 dev->netdev_ops->ndo_get_offload_stats))
5038 return -ENODATA;
5039
5040 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
5041 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
5042 if (attr_id < *prividx)
5043 continue;
5044
5045 size = rtnl_get_offload_stats_attr_size(attr_id);
5046 if (!size)
5047 continue;
5048
3df5b3c6 5049 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
69ae6ad2
NF
5050 continue;
5051
5052 attr = nla_reserve_64bit(skb, attr_id, size,
5053 IFLA_OFFLOAD_XSTATS_UNSPEC);
5054 if (!attr)
5055 goto nla_put_failure;
5056
5057 attr_data = nla_data(attr);
5058 memset(attr_data, 0, size);
5059 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
5060 attr_data);
5061 if (err)
5062 goto get_offload_stats_failure;
5063 }
5064
5065 if (!attr)
5066 return -ENODATA;
5067
5068 *prividx = 0;
5069 return 0;
5070
5071nla_put_failure:
5072 err = -EMSGSIZE;
5073get_offload_stats_failure:
5074 *prividx = attr_id;
5075 return err;
5076}
5077
5078static int rtnl_get_offload_stats_size(const struct net_device *dev)
5079{
5080 int nla_size = 0;
5081 int attr_id;
5082 int size;
5083
5084 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
5085 dev->netdev_ops->ndo_get_offload_stats))
5086 return 0;
5087
5088 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
5089 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
3df5b3c6 5090 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
69ae6ad2
NF
5091 continue;
5092 size = rtnl_get_offload_stats_attr_size(attr_id);
5093 nla_size += nla_total_size_64bit(size);
5094 }
5095
5096 if (nla_size != 0)
5097 nla_size += nla_total_size(0);
5098
5099 return nla_size;
5100}
5101
10c9ead9
RP
5102static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
5103 int type, u32 pid, u32 seq, u32 change,
e8872a25
NA
5104 unsigned int flags, unsigned int filter_mask,
5105 int *idxattr, int *prividx)
10c9ead9
RP
5106{
5107 struct if_stats_msg *ifsm;
5108 struct nlmsghdr *nlh;
5109 struct nlattr *attr;
e8872a25 5110 int s_prividx = *prividx;
69ae6ad2 5111 int err;
10c9ead9
RP
5112
5113 ASSERT_RTNL();
5114
5115 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
5116 if (!nlh)
5117 return -EMSGSIZE;
5118
5119 ifsm = nlmsg_data(nlh);
ce024f42
NA
5120 ifsm->family = PF_UNSPEC;
5121 ifsm->pad1 = 0;
5122 ifsm->pad2 = 0;
10c9ead9
RP
5123 ifsm->ifindex = dev->ifindex;
5124 ifsm->filter_mask = filter_mask;
5125
e8872a25 5126 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
10c9ead9 5127 struct rtnl_link_stats64 *sp;
10c9ead9 5128
58414d32
ND
5129 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
5130 sizeof(struct rtnl_link_stats64),
5131 IFLA_STATS_UNSPEC);
10c9ead9
RP
5132 if (!attr)
5133 goto nla_put_failure;
5134
5135 sp = nla_data(attr);
5136 dev_get_stats(dev, sp);
5137 }
5138
97a47fac
NA
5139 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
5140 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
5141
5142 if (ops && ops->fill_linkxstats) {
97a47fac 5143 *idxattr = IFLA_STATS_LINK_XSTATS;
ae0be8de
MK
5144 attr = nla_nest_start_noflag(skb,
5145 IFLA_STATS_LINK_XSTATS);
97a47fac
NA
5146 if (!attr)
5147 goto nla_put_failure;
5148
80e73cc5
NA
5149 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
5150 nla_nest_end(skb, attr);
5151 if (err)
5152 goto nla_put_failure;
5153 *idxattr = 0;
5154 }
5155 }
5156
5157 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
5158 *idxattr)) {
5159 const struct rtnl_link_ops *ops = NULL;
5160 const struct net_device *master;
5161
5162 master = netdev_master_upper_dev_get(dev);
5163 if (master)
5164 ops = master->rtnl_link_ops;
5165 if (ops && ops->fill_linkxstats) {
80e73cc5 5166 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
ae0be8de
MK
5167 attr = nla_nest_start_noflag(skb,
5168 IFLA_STATS_LINK_XSTATS_SLAVE);
80e73cc5
NA
5169 if (!attr)
5170 goto nla_put_failure;
5171
5172 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
97a47fac
NA
5173 nla_nest_end(skb, attr);
5174 if (err)
5175 goto nla_put_failure;
5176 *idxattr = 0;
5177 }
5178 }
5179
69ae6ad2
NF
5180 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
5181 *idxattr)) {
5182 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
ae0be8de
MK
5183 attr = nla_nest_start_noflag(skb,
5184 IFLA_STATS_LINK_OFFLOAD_XSTATS);
69ae6ad2
NF
5185 if (!attr)
5186 goto nla_put_failure;
5187
5188 err = rtnl_get_offload_stats(skb, dev, prividx);
5189 if (err == -ENODATA)
5190 nla_nest_cancel(skb, attr);
5191 else
5192 nla_nest_end(skb, attr);
5193
5194 if (err && err != -ENODATA)
5195 goto nla_put_failure;
5196 *idxattr = 0;
5197 }
5198
aefb4d4a
RS
5199 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
5200 struct rtnl_af_ops *af_ops;
5201
5202 *idxattr = IFLA_STATS_AF_SPEC;
ae0be8de 5203 attr = nla_nest_start_noflag(skb, IFLA_STATS_AF_SPEC);
aefb4d4a
RS
5204 if (!attr)
5205 goto nla_put_failure;
5206
5fa85a09
FW
5207 rcu_read_lock();
5208 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
aefb4d4a
RS
5209 if (af_ops->fill_stats_af) {
5210 struct nlattr *af;
5211 int err;
5212
ae0be8de
MK
5213 af = nla_nest_start_noflag(skb,
5214 af_ops->family);
5fa85a09
FW
5215 if (!af) {
5216 rcu_read_unlock();
aefb4d4a 5217 goto nla_put_failure;
5fa85a09 5218 }
aefb4d4a
RS
5219 err = af_ops->fill_stats_af(skb, dev);
5220
5fa85a09 5221 if (err == -ENODATA) {
aefb4d4a 5222 nla_nest_cancel(skb, af);
5fa85a09
FW
5223 } else if (err < 0) {
5224 rcu_read_unlock();
aefb4d4a 5225 goto nla_put_failure;
5fa85a09 5226 }
aefb4d4a
RS
5227
5228 nla_nest_end(skb, af);
5229 }
5230 }
5fa85a09 5231 rcu_read_unlock();
aefb4d4a
RS
5232
5233 nla_nest_end(skb, attr);
5234
5235 *idxattr = 0;
5236 }
5237
10c9ead9
RP
5238 nlmsg_end(skb, nlh);
5239
5240 return 0;
5241
5242nla_put_failure:
e8872a25
NA
5243 /* not a multi message or no progress mean a real error */
5244 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
5245 nlmsg_cancel(skb, nlh);
5246 else
5247 nlmsg_end(skb, nlh);
10c9ead9
RP
5248
5249 return -EMSGSIZE;
5250}
5251
10c9ead9
RP
5252static size_t if_nlmsg_stats_size(const struct net_device *dev,
5253 u32 filter_mask)
5254{
5255 size_t size = 0;
5256
e8872a25 5257 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
10c9ead9
RP
5258 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
5259
97a47fac
NA
5260 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
5261 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
80e73cc5 5262 int attr = IFLA_STATS_LINK_XSTATS;
97a47fac
NA
5263
5264 if (ops && ops->get_linkxstats_size) {
80e73cc5
NA
5265 size += nla_total_size(ops->get_linkxstats_size(dev,
5266 attr));
97a47fac
NA
5267 /* for IFLA_STATS_LINK_XSTATS */
5268 size += nla_total_size(0);
5269 }
5270 }
5271
80e73cc5
NA
5272 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
5273 struct net_device *_dev = (struct net_device *)dev;
5274 const struct rtnl_link_ops *ops = NULL;
5275 const struct net_device *master;
5276
5277 /* netdev_master_upper_dev_get can't take const */
5278 master = netdev_master_upper_dev_get(_dev);
5279 if (master)
5280 ops = master->rtnl_link_ops;
5281 if (ops && ops->get_linkxstats_size) {
5282 int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
5283
5284 size += nla_total_size(ops->get_linkxstats_size(dev,
5285 attr));
5286 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
5287 size += nla_total_size(0);
5288 }
5289 }
5290
69ae6ad2
NF
5291 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
5292 size += rtnl_get_offload_stats_size(dev);
5293
aefb4d4a
RS
5294 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
5295 struct rtnl_af_ops *af_ops;
5296
5297 /* for IFLA_STATS_AF_SPEC */
5298 size += nla_total_size(0);
5299
5fa85a09
FW
5300 rcu_read_lock();
5301 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
aefb4d4a
RS
5302 if (af_ops->get_stats_af_size) {
5303 size += nla_total_size(
5304 af_ops->get_stats_af_size(dev));
5305
5306 /* for AF_* */
5307 size += nla_total_size(0);
5308 }
5309 }
5fa85a09 5310 rcu_read_unlock();
aefb4d4a
RS
5311 }
5312
10c9ead9
RP
5313 return size;
5314}
5315
51bc860d
JK
5316static int rtnl_valid_stats_req(const struct nlmsghdr *nlh, bool strict_check,
5317 bool is_dump, struct netlink_ext_ack *extack)
5318{
5319 struct if_stats_msg *ifsm;
5320
69f23a09 5321 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifsm))) {
51bc860d
JK
5322 NL_SET_ERR_MSG(extack, "Invalid header for stats dump");
5323 return -EINVAL;
5324 }
5325
5326 if (!strict_check)
5327 return 0;
5328
5329 ifsm = nlmsg_data(nlh);
5330
5331 /* only requests using strict checks can pass data to influence
5332 * the dump. The legacy exception is filter_mask.
5333 */
5334 if (ifsm->pad1 || ifsm->pad2 || (is_dump && ifsm->ifindex)) {
5335 NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request");
5336 return -EINVAL;
5337 }
5338 if (nlmsg_attrlen(nlh, sizeof(*ifsm))) {
5339 NL_SET_ERR_MSG(extack, "Invalid attributes after stats header");
5340 return -EINVAL;
5341 }
6300acb2
JK
5342 if (ifsm->filter_mask >= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX + 1)) {
5343 NL_SET_ERR_MSG(extack, "Invalid stats requested through filter mask");
5344 return -EINVAL;
5345 }
51bc860d
JK
5346
5347 return 0;
5348}
5349
c21ef3e3
DA
5350static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
5351 struct netlink_ext_ack *extack)
10c9ead9
RP
5352{
5353 struct net *net = sock_net(skb->sk);
10c9ead9 5354 struct net_device *dev = NULL;
e8872a25
NA
5355 int idxattr = 0, prividx = 0;
5356 struct if_stats_msg *ifsm;
10c9ead9
RP
5357 struct sk_buff *nskb;
5358 u32 filter_mask;
5359 int err;
5360
51bc860d
JK
5361 err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb),
5362 false, extack);
5363 if (err)
5364 return err;
4775cc1f 5365
10c9ead9
RP
5366 ifsm = nlmsg_data(nlh);
5367 if (ifsm->ifindex > 0)
5368 dev = __dev_get_by_index(net, ifsm->ifindex);
5369 else
5370 return -EINVAL;
5371
5372 if (!dev)
5373 return -ENODEV;
5374
5375 filter_mask = ifsm->filter_mask;
5376 if (!filter_mask)
5377 return -EINVAL;
5378
5379 nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
5380 if (!nskb)
5381 return -ENOBUFS;
5382
5383 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
5384 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
e8872a25 5385 0, filter_mask, &idxattr, &prividx);
10c9ead9
RP
5386 if (err < 0) {
5387 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
5388 WARN_ON(err == -EMSGSIZE);
5389 kfree_skb(nskb);
5390 } else {
5391 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
5392 }
5393
5394 return err;
5395}
5396
5397static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
5398{
841891ec 5399 struct netlink_ext_ack *extack = cb->extack;
e8872a25 5400 int h, s_h, err, s_idx, s_idxattr, s_prividx;
10c9ead9 5401 struct net *net = sock_net(skb->sk);
e8872a25 5402 unsigned int flags = NLM_F_MULTI;
10c9ead9 5403 struct if_stats_msg *ifsm;
10c9ead9 5404 struct hlist_head *head;
e8872a25 5405 struct net_device *dev;
10c9ead9 5406 u32 filter_mask = 0;
e8872a25 5407 int idx = 0;
10c9ead9
RP
5408
5409 s_h = cb->args[0];
5410 s_idx = cb->args[1];
e8872a25
NA
5411 s_idxattr = cb->args[2];
5412 s_prividx = cb->args[3];
10c9ead9
RP
5413
5414 cb->seq = net->dev_base_seq;
5415
51bc860d
JK
5416 err = rtnl_valid_stats_req(cb->nlh, cb->strict_check, true, extack);
5417 if (err)
5418 return err;
4775cc1f 5419
10c9ead9
RP
5420 ifsm = nlmsg_data(cb->nlh);
5421 filter_mask = ifsm->filter_mask;
841891ec
DA
5422 if (!filter_mask) {
5423 NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump");
10c9ead9 5424 return -EINVAL;
841891ec 5425 }
10c9ead9
RP
5426
5427 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5428 idx = 0;
5429 head = &net->dev_index_head[h];
5430 hlist_for_each_entry(dev, head, index_hlist) {
5431 if (idx < s_idx)
5432 goto cont;
5433 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
5434 NETLINK_CB(cb->skb).portid,
5435 cb->nlh->nlmsg_seq, 0,
e8872a25
NA
5436 flags, filter_mask,
5437 &s_idxattr, &s_prividx);
10c9ead9
RP
5438 /* If we ran out of room on the first message,
5439 * we're in trouble
5440 */
5441 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
5442
5443 if (err < 0)
5444 goto out;
e8872a25
NA
5445 s_prividx = 0;
5446 s_idxattr = 0;
10c9ead9
RP
5447 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
5448cont:
5449 idx++;
5450 }
5451 }
5452out:
e8872a25
NA
5453 cb->args[3] = s_prividx;
5454 cb->args[2] = s_idxattr;
10c9ead9
RP
5455 cb->args[1] = idx;
5456 cb->args[0] = h;
5457
5458 return skb->len;
5459}
5460
1da177e4
LT
5461/* Process one rtnetlink message. */
5462
2d4bc933
JB
5463static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
5464 struct netlink_ext_ack *extack)
1da177e4 5465{
3b1e0a65 5466 struct net *net = sock_net(skb->sk);
addf9b90 5467 struct rtnl_link *link;
e4202511 5468 struct module *owner;
6853dd48 5469 int err = -EOPNOTSUPP;
e2849863 5470 rtnl_doit_func doit;
62256f98 5471 unsigned int flags;
617cfc75 5472 int kind;
1da177e4
LT
5473 int family;
5474 int type;
1da177e4 5475
1da177e4 5476 type = nlh->nlmsg_type;
1da177e4 5477 if (type > RTM_MAX)
038890fe 5478 return -EOPNOTSUPP;
1da177e4
LT
5479
5480 type -= RTM_BASE;
5481
5482 /* All the messages must have at least 1 byte length */
573ce260 5483 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
1da177e4
LT
5484 return 0;
5485
573ce260 5486 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
1da177e4
LT
5487 kind = type&3;
5488
90f62cf3 5489 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
1d00a4eb 5490 return -EPERM;
1da177e4 5491
6853dd48 5492 rcu_read_lock();
b8f3ab42 5493 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
97c53cac 5494 struct sock *rtnl;
e2849863 5495 rtnl_dumpit_func dumpit;
ebfe3c51 5496 u32 min_dump_alloc = 0;
1da177e4 5497
addf9b90
FW
5498 link = rtnl_get_link(family, type);
5499 if (!link || !link->dumpit) {
6853dd48 5500 family = PF_UNSPEC;
addf9b90
FW
5501 link = rtnl_get_link(family, type);
5502 if (!link || !link->dumpit)
6853dd48
FW
5503 goto err_unlock;
5504 }
e4202511 5505 owner = link->owner;
addf9b90 5506 dumpit = link->dumpit;
e1fa6d21 5507
5c2bb9b6 5508 if (type == RTM_GETLINK - RTM_BASE)
e1fa6d21 5509 min_dump_alloc = rtnl_calcit(skb, nlh);
9ac4a169 5510
e4202511
FW
5511 err = 0;
5512 /* need to do this before rcu_read_unlock() */
5513 if (!try_module_get(owner))
5514 err = -EPROTONOSUPPORT;
5515
6853dd48
FW
5516 rcu_read_unlock();
5517
97c53cac 5518 rtnl = net->rtnl;
e4202511 5519 if (err == 0) {
80d326fa
PNA
5520 struct netlink_dump_control c = {
5521 .dump = dumpit,
5522 .min_dump_alloc = min_dump_alloc,
e4202511 5523 .module = owner,
80d326fa
PNA
5524 };
5525 err = netlink_dump_start(rtnl, skb, nlh, &c);
e4202511
FW
5526 /* netlink_dump_start() will keep a reference on
5527 * module if dump is still in progress.
5528 */
5529 module_put(owner);
80d326fa 5530 }
2907c35f 5531 return err;
1da177e4
LT
5532 }
5533
addf9b90
FW
5534 link = rtnl_get_link(family, type);
5535 if (!link || !link->doit) {
8caa38b5 5536 family = PF_UNSPEC;
addf9b90
FW
5537 link = rtnl_get_link(PF_UNSPEC, type);
5538 if (!link || !link->doit)
5539 goto out_unlock;
8caa38b5
FW
5540 }
5541
e4202511
FW
5542 owner = link->owner;
5543 if (!try_module_get(owner)) {
5544 err = -EPROTONOSUPPORT;
5545 goto out_unlock;
5546 }
5547
addf9b90 5548 flags = link->flags;
62256f98 5549 if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
addf9b90 5550 doit = link->doit;
62256f98
FW
5551 rcu_read_unlock();
5552 if (doit)
5553 err = doit(skb, nlh, extack);
e4202511 5554 module_put(owner);
62256f98
FW
5555 return err;
5556 }
6853dd48 5557 rcu_read_unlock();
1da177e4 5558
6853dd48 5559 rtnl_lock();
addf9b90
FW
5560 link = rtnl_get_link(family, type);
5561 if (link && link->doit)
5562 err = link->doit(skb, nlh, extack);
0cc09020 5563 rtnl_unlock();
addf9b90 5564
e4202511
FW
5565 module_put(owner);
5566
addf9b90
FW
5567 return err;
5568
5569out_unlock:
5570 rcu_read_unlock();
0cc09020
FW
5571 return err;
5572
5573err_unlock:
6853dd48 5574 rcu_read_unlock();
0cc09020 5575 return -EOPNOTSUPP;
1da177e4
LT
5576}
5577
cd40b7d3 5578static void rtnetlink_rcv(struct sk_buff *skb)
1da177e4 5579{
cd40b7d3 5580 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
1da177e4
LT
5581}
5582
5f729eaa
JG
5583static int rtnetlink_bind(struct net *net, int group)
5584{
5585 switch (group) {
5586 case RTNLGRP_IPV4_MROUTE_R:
5587 case RTNLGRP_IPV6_MROUTE_R:
5588 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
5589 return -EPERM;
5590 break;
5591 }
5592 return 0;
5593}
5594
1da177e4
LT
5595static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
5596{
351638e7 5597 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
e9dc8653 5598
1da177e4 5599 switch (event) {
5138e86f 5600 case NETDEV_REBOOT:
8a212589 5601 case NETDEV_CHANGEMTU:
3753654e 5602 case NETDEV_CHANGEADDR:
5138e86f
VY
5603 case NETDEV_CHANGENAME:
5604 case NETDEV_FEAT_CHANGE:
5605 case NETDEV_BONDING_FAILOVER:
e6e66594 5606 case NETDEV_POST_TYPE_CHANGE:
5138e86f 5607 case NETDEV_NOTIFY_PEERS:
dc709f37 5608 case NETDEV_CHANGEUPPER:
5138e86f 5609 case NETDEV_RESEND_IGMP:
5138e86f 5610 case NETDEV_CHANGEINFODATA:
eeda3fb9 5611 case NETDEV_CHANGELOWERSTATE:
ebdcf045 5612 case NETDEV_CHANGE_TX_QUEUE_LEN:
3d3ea5af 5613 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
38e01b30 5614 GFP_KERNEL, NULL, 0);
1da177e4
LT
5615 break;
5616 default:
1da177e4
LT
5617 break;
5618 }
5619 return NOTIFY_DONE;
5620}
5621
5622static struct notifier_block rtnetlink_dev_notifier = {
5623 .notifier_call = rtnetlink_event,
5624};
5625
97c53cac 5626
2c8c1e72 5627static int __net_init rtnetlink_net_init(struct net *net)
97c53cac
DL
5628{
5629 struct sock *sk;
a31f2d17
PNA
5630 struct netlink_kernel_cfg cfg = {
5631 .groups = RTNLGRP_MAX,
5632 .input = rtnetlink_rcv,
5633 .cb_mutex = &rtnl_mutex,
9785e10a 5634 .flags = NL_CFG_F_NONROOT_RECV,
5f729eaa 5635 .bind = rtnetlink_bind,
a31f2d17
PNA
5636 };
5637
9f00d977 5638 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
97c53cac
DL
5639 if (!sk)
5640 return -ENOMEM;
97c53cac
DL
5641 net->rtnl = sk;
5642 return 0;
5643}
5644
2c8c1e72 5645static void __net_exit rtnetlink_net_exit(struct net *net)
97c53cac 5646{
775516bf
DL
5647 netlink_kernel_release(net->rtnl);
5648 net->rtnl = NULL;
97c53cac
DL
5649}
5650
5651static struct pernet_operations rtnetlink_net_ops = {
5652 .init = rtnetlink_net_init,
5653 .exit = rtnetlink_net_exit,
5654};
5655
1da177e4
LT
5656void __init rtnetlink_init(void)
5657{
97c53cac 5658 if (register_pernet_subsys(&rtnetlink_net_ops))
1da177e4 5659 panic("rtnetlink_init: cannot initialize rtnetlink\n");
97c53cac 5660
1da177e4 5661 register_netdevice_notifier(&rtnetlink_dev_notifier);
340d17fc 5662
c7ac8679 5663 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
b97bac64
FW
5664 rtnl_dump_ifinfo, 0);
5665 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
5666 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
5667 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
687ad8cc 5668
b97bac64
FW
5669 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
5670 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
5671 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
77162022 5672
36fbf1e5
JP
5673 rtnl_register(PF_UNSPEC, RTM_NEWLINKPROP, rtnl_newlinkprop, NULL, 0);
5674 rtnl_register(PF_UNSPEC, RTM_DELLINKPROP, rtnl_dellinkprop, NULL, 0);
5675
b97bac64
FW
5676 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
5677 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
5b2f94b2 5678 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0);
e5a55a89 5679
b97bac64
FW
5680 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
5681 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
5682 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
10c9ead9
RP
5683
5684 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
b97bac64 5685 0);
1da177e4 5686}