]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/core/rtnetlink.c
UBUNTU: SAUCE: Import aufs driver
[mirror_ubuntu-bionic-kernel.git] / net / core / rtnetlink.c
CommitLineData
1da177e4
LT
1/*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * Routing netlink socket interface: protocol independent part.
7 *
8 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
14 *
15 * Fixes:
16 * Vitaly E. Lavrov RTA_OK arithmetics was wrong.
17 */
18
ee5d032f 19#include <linux/bitops.h>
1da177e4
LT
20#include <linux/errno.h>
21#include <linux/module.h>
22#include <linux/types.h>
23#include <linux/socket.h>
24#include <linux/kernel.h>
1da177e4
LT
25#include <linux/timer.h>
26#include <linux/string.h>
27#include <linux/sockios.h>
28#include <linux/net.h>
29#include <linux/fcntl.h>
30#include <linux/mm.h>
31#include <linux/slab.h>
32#include <linux/interrupt.h>
33#include <linux/capability.h>
34#include <linux/skbuff.h>
35#include <linux/init.h>
36#include <linux/security.h>
6756ae4b 37#include <linux/mutex.h>
1823730f 38#include <linux/if_addr.h>
77162022 39#include <linux/if_bridge.h>
f6f6424b 40#include <linux/if_vlan.h>
ebc08a6f 41#include <linux/pci.h>
77162022 42#include <linux/etherdevice.h>
58038695 43#include <linux/bpf.h>
1da177e4 44
7c0f6ba6 45#include <linux/uaccess.h>
1da177e4
LT
46
47#include <linux/inet.h>
48#include <linux/netdevice.h>
82f28412 49#include <net/switchdev.h>
1da177e4
LT
50#include <net/ip.h>
51#include <net/protocol.h>
52#include <net/arp.h>
53#include <net/route.h>
54#include <net/udp.h>
ea697639 55#include <net/tcp.h>
1da177e4
LT
56#include <net/sock.h>
57#include <net/pkt_sched.h>
14c0b97d 58#include <net/fib_rules.h>
e2849863 59#include <net/rtnetlink.h>
30ffee84 60#include <net/net_namespace.h>
1da177e4 61
e0d087af 62struct rtnl_link {
e2849863
TG
63 rtnl_doit_func doit;
64 rtnl_dumpit_func dumpit;
62256f98 65 unsigned int flags;
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
1b5c5493
ED
76static struct sk_buff *defer_kfree_skb_list;
77void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail)
78{
79 if (head && tail) {
80 tail->next = defer_kfree_skb_list;
81 defer_kfree_skb_list = head;
82 }
83}
84EXPORT_SYMBOL(rtnl_kfree_skbs);
85
6756ae4b 86void __rtnl_unlock(void)
1da177e4 87{
1b5c5493
ED
88 struct sk_buff *head = defer_kfree_skb_list;
89
90 defer_kfree_skb_list = NULL;
91
6756ae4b 92 mutex_unlock(&rtnl_mutex);
1b5c5493
ED
93
94 while (head) {
95 struct sk_buff *next = head->next;
96
97 kfree_skb(head);
98 cond_resched();
99 head = next;
100 }
1da177e4 101}
6756ae4b 102
1da177e4
LT
103void rtnl_unlock(void)
104{
58ec3b4d 105 /* This fellow will unlock it for us. */
1da177e4
LT
106 netdev_run_todo();
107}
e0d087af 108EXPORT_SYMBOL(rtnl_unlock);
1da177e4 109
6756ae4b
SH
110int rtnl_trylock(void)
111{
112 return mutex_trylock(&rtnl_mutex);
113}
e0d087af 114EXPORT_SYMBOL(rtnl_trylock);
6756ae4b 115
c9c1014b
PM
116int rtnl_is_locked(void)
117{
118 return mutex_is_locked(&rtnl_mutex);
119}
e0d087af 120EXPORT_SYMBOL(rtnl_is_locked);
c9c1014b 121
a898def2 122#ifdef CONFIG_PROVE_LOCKING
0cbf3343 123bool lockdep_rtnl_is_held(void)
a898def2
PM
124{
125 return lockdep_is_held(&rtnl_mutex);
126}
127EXPORT_SYMBOL(lockdep_rtnl_is_held);
128#endif /* #ifdef CONFIG_PROVE_LOCKING */
129
6853dd48 130static struct rtnl_link __rcu *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
019a3169 131static refcount_t rtnl_msg_handlers_ref[RTNL_FAMILY_MAX + 1];
e2849863
TG
132
133static inline int rtm_msgindex(int msgtype)
134{
135 int msgindex = msgtype - RTM_BASE;
136
137 /*
138 * msgindex < 0 implies someone tried to register a netlink
139 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
140 * the message type has not been added to linux/rtnetlink.h
141 */
142 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
143
144 return msgindex;
145}
146
e2849863
TG
147/**
148 * __rtnl_register - Register a rtnetlink message type
149 * @protocol: Protocol family or PF_UNSPEC
150 * @msgtype: rtnetlink message type
151 * @doit: Function pointer called for each request message
152 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
b97bac64 153 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
e2849863
TG
154 *
155 * Registers the specified function pointers (at least one of them has
156 * to be non-NULL) to be called whenever a request message for the
157 * specified protocol family and message type is received.
158 *
159 * The special protocol family PF_UNSPEC may be used to define fallback
160 * function pointers for the case when no entry for the specific protocol
161 * family exists.
162 *
163 * Returns 0 on success or a negative error code.
164 */
165int __rtnl_register(int protocol, int msgtype,
c7ac8679 166 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
b97bac64 167 unsigned int flags)
e2849863
TG
168{
169 struct rtnl_link *tab;
170 int msgindex;
171
25239cee 172 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
e2849863
TG
173 msgindex = rtm_msgindex(msgtype);
174
377cb248 175 tab = rcu_dereference_raw(rtnl_msg_handlers[protocol]);
e2849863
TG
176 if (tab == NULL) {
177 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
178 if (tab == NULL)
179 return -ENOBUFS;
180
6853dd48 181 rcu_assign_pointer(rtnl_msg_handlers[protocol], tab);
e2849863
TG
182 }
183
184 if (doit)
185 tab[msgindex].doit = doit;
e2849863
TG
186 if (dumpit)
187 tab[msgindex].dumpit = dumpit;
62256f98 188 tab[msgindex].flags |= flags;
e2849863
TG
189
190 return 0;
191}
e2849863
TG
192EXPORT_SYMBOL_GPL(__rtnl_register);
193
194/**
195 * rtnl_register - Register a rtnetlink message type
196 *
197 * Identical to __rtnl_register() but panics on failure. This is useful
198 * as failure of this function is very unlikely, it can only happen due
199 * to lack of memory when allocating the chain to store all message
200 * handlers for a protocol. Meant for use in init functions where lack
25985edc 201 * of memory implies no sense in continuing.
e2849863
TG
202 */
203void rtnl_register(int protocol, int msgtype,
c7ac8679 204 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
b97bac64 205 unsigned int flags)
e2849863 206{
b97bac64 207 if (__rtnl_register(protocol, msgtype, doit, dumpit, flags) < 0)
e2849863
TG
208 panic("Unable to register rtnetlink message handler, "
209 "protocol = %d, message type = %d\n",
210 protocol, msgtype);
211}
e2849863
TG
212EXPORT_SYMBOL_GPL(rtnl_register);
213
214/**
215 * rtnl_unregister - Unregister a rtnetlink message type
216 * @protocol: Protocol family or PF_UNSPEC
217 * @msgtype: rtnetlink message type
218 *
219 * Returns 0 on success or a negative error code.
220 */
221int rtnl_unregister(int protocol, int msgtype)
222{
6853dd48 223 struct rtnl_link *handlers;
e2849863
TG
224 int msgindex;
225
25239cee 226 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
e2849863
TG
227 msgindex = rtm_msgindex(msgtype);
228
6853dd48
FW
229 rtnl_lock();
230 handlers = rtnl_dereference(rtnl_msg_handlers[protocol]);
231 if (!handlers) {
232 rtnl_unlock();
e2849863 233 return -ENOENT;
6853dd48 234 }
e2849863 235
6853dd48
FW
236 handlers[msgindex].doit = NULL;
237 handlers[msgindex].dumpit = NULL;
62256f98 238 handlers[msgindex].flags = 0;
6853dd48 239 rtnl_unlock();
e2849863
TG
240
241 return 0;
242}
e2849863
TG
243EXPORT_SYMBOL_GPL(rtnl_unregister);
244
245/**
246 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
247 * @protocol : Protocol family or PF_UNSPEC
248 *
249 * Identical to calling rtnl_unregster() for all registered message types
250 * of a certain protocol family.
251 */
252void rtnl_unregister_all(int protocol)
253{
019a3169
FW
254 struct rtnl_link *handlers;
255
25239cee 256 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
e2849863 257
019a3169 258 rtnl_lock();
6853dd48
FW
259 handlers = rtnl_dereference(rtnl_msg_handlers[protocol]);
260 RCU_INIT_POINTER(rtnl_msg_handlers[protocol], NULL);
019a3169
FW
261 rtnl_unlock();
262
6853dd48
FW
263 synchronize_net();
264
d38a6512 265 while (refcount_read(&rtnl_msg_handlers_ref[protocol]) > 1)
019a3169
FW
266 schedule();
267 kfree(handlers);
e2849863 268}
e2849863 269EXPORT_SYMBOL_GPL(rtnl_unregister_all);
1da177e4 270
38f7b870
PM
271static LIST_HEAD(link_ops);
272
c63044f0
ED
273static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
274{
275 const struct rtnl_link_ops *ops;
276
277 list_for_each_entry(ops, &link_ops, list) {
278 if (!strcmp(ops->kind, kind))
279 return ops;
280 }
281 return NULL;
282}
283
38f7b870
PM
284/**
285 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
286 * @ops: struct rtnl_link_ops * to register
287 *
288 * The caller must hold the rtnl_mutex. This function should be used
289 * by drivers that create devices during module initialization. It
290 * must be called before registering the devices.
291 *
292 * Returns 0 on success or a negative error code.
293 */
294int __rtnl_link_register(struct rtnl_link_ops *ops)
295{
c63044f0
ED
296 if (rtnl_link_ops_get(ops->kind))
297 return -EEXIST;
298
b0ab2fab
JP
299 /* The check for setup is here because if ops
300 * does not have that filled up, it is not possible
301 * to use the ops for creating device. So do not
302 * fill up dellink as well. That disables rtnl_dellink.
303 */
304 if (ops->setup && !ops->dellink)
23289a37 305 ops->dellink = unregister_netdevice_queue;
2d85cba2 306
38f7b870
PM
307 list_add_tail(&ops->list, &link_ops);
308 return 0;
309}
38f7b870
PM
310EXPORT_SYMBOL_GPL(__rtnl_link_register);
311
312/**
313 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
314 * @ops: struct rtnl_link_ops * to register
315 *
316 * Returns 0 on success or a negative error code.
317 */
318int rtnl_link_register(struct rtnl_link_ops *ops)
319{
320 int err;
321
322 rtnl_lock();
323 err = __rtnl_link_register(ops);
324 rtnl_unlock();
325 return err;
326}
38f7b870
PM
327EXPORT_SYMBOL_GPL(rtnl_link_register);
328
669f87ba
PE
329static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
330{
331 struct net_device *dev;
23289a37
ED
332 LIST_HEAD(list_kill);
333
669f87ba 334 for_each_netdev(net, dev) {
23289a37
ED
335 if (dev->rtnl_link_ops == ops)
336 ops->dellink(dev, &list_kill);
669f87ba 337 }
23289a37 338 unregister_netdevice_many(&list_kill);
669f87ba
PE
339}
340
38f7b870
PM
341/**
342 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
343 * @ops: struct rtnl_link_ops * to unregister
344 *
2d85cba2 345 * The caller must hold the rtnl_mutex.
38f7b870
PM
346 */
347void __rtnl_link_unregister(struct rtnl_link_ops *ops)
348{
881d966b 349 struct net *net;
2d85cba2 350
881d966b 351 for_each_net(net) {
669f87ba 352 __rtnl_kill_links(net, ops);
2d85cba2 353 }
38f7b870
PM
354 list_del(&ops->list);
355}
38f7b870
PM
356EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
357
200b916f
CW
358/* Return with the rtnl_lock held when there are no network
359 * devices unregistering in any network namespace.
360 */
361static void rtnl_lock_unregistering_all(void)
362{
363 struct net *net;
364 bool unregistering;
ff960a73 365 DEFINE_WAIT_FUNC(wait, woken_wake_function);
200b916f 366
ff960a73 367 add_wait_queue(&netdev_unregistering_wq, &wait);
200b916f 368 for (;;) {
200b916f
CW
369 unregistering = false;
370 rtnl_lock();
371 for_each_net(net) {
372 if (net->dev_unreg_count > 0) {
373 unregistering = true;
374 break;
375 }
376 }
377 if (!unregistering)
378 break;
379 __rtnl_unlock();
ff960a73
PZ
380
381 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
200b916f 382 }
ff960a73 383 remove_wait_queue(&netdev_unregistering_wq, &wait);
200b916f
CW
384}
385
38f7b870
PM
386/**
387 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
388 * @ops: struct rtnl_link_ops * to unregister
389 */
390void rtnl_link_unregister(struct rtnl_link_ops *ops)
391{
200b916f
CW
392 /* Close the race with cleanup_net() */
393 mutex_lock(&net_mutex);
394 rtnl_lock_unregistering_all();
38f7b870
PM
395 __rtnl_link_unregister(ops);
396 rtnl_unlock();
200b916f 397 mutex_unlock(&net_mutex);
38f7b870 398}
38f7b870
PM
399EXPORT_SYMBOL_GPL(rtnl_link_unregister);
400
ba7d49b1
JP
401static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
402{
403 struct net_device *master_dev;
404 const struct rtnl_link_ops *ops;
8515ae38 405 size_t size = 0;
ba7d49b1 406
8515ae38
FW
407 rcu_read_lock();
408
409 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
ba7d49b1 410 if (!master_dev)
8515ae38
FW
411 goto out;
412
ba7d49b1 413 ops = master_dev->rtnl_link_ops;
6049f253 414 if (!ops || !ops->get_slave_size)
8515ae38 415 goto out;
ba7d49b1 416 /* IFLA_INFO_SLAVE_DATA + nested data */
8515ae38 417 size = nla_total_size(sizeof(struct nlattr)) +
ba7d49b1 418 ops->get_slave_size(master_dev, dev);
8515ae38
FW
419
420out:
421 rcu_read_unlock();
422 return size;
ba7d49b1
JP
423}
424
38f7b870
PM
425static size_t rtnl_link_get_size(const struct net_device *dev)
426{
427 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
428 size_t size;
429
430 if (!ops)
431 return 0;
432
369cf77a
TG
433 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
434 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
38f7b870
PM
435
436 if (ops->get_size)
437 /* IFLA_INFO_DATA + nested data */
369cf77a 438 size += nla_total_size(sizeof(struct nlattr)) +
38f7b870
PM
439 ops->get_size(dev);
440
441 if (ops->get_xstats_size)
369cf77a
TG
442 /* IFLA_INFO_XSTATS */
443 size += nla_total_size(ops->get_xstats_size(dev));
38f7b870 444
ba7d49b1
JP
445 size += rtnl_link_get_slave_info_data_size(dev);
446
38f7b870
PM
447 return size;
448}
449
f8ff182c
TG
450static LIST_HEAD(rtnl_af_ops);
451
452static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
453{
454 const struct rtnl_af_ops *ops;
455
5fa85a09 456 list_for_each_entry_rcu(ops, &rtnl_af_ops, list) {
f8ff182c
TG
457 if (ops->family == family)
458 return ops;
459 }
460
461 return NULL;
462}
463
f8ff182c
TG
464/**
465 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
466 * @ops: struct rtnl_af_ops * to register
467 *
468 * Returns 0 on success or a negative error code.
469 */
3678a9d8 470void rtnl_af_register(struct rtnl_af_ops *ops)
f8ff182c 471{
f8ff182c 472 rtnl_lock();
5fa85a09 473 list_add_tail_rcu(&ops->list, &rtnl_af_ops);
f8ff182c 474 rtnl_unlock();
f8ff182c
TG
475}
476EXPORT_SYMBOL_GPL(rtnl_af_register);
477
f8ff182c
TG
478/**
479 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
480 * @ops: struct rtnl_af_ops * to unregister
481 */
482void rtnl_af_unregister(struct rtnl_af_ops *ops)
483{
484 rtnl_lock();
5fa85a09 485 list_del_rcu(&ops->list);
f8ff182c 486 rtnl_unlock();
5fa85a09
FW
487
488 synchronize_rcu();
f8ff182c
TG
489}
490EXPORT_SYMBOL_GPL(rtnl_af_unregister);
491
b1974ed0
AR
492static size_t rtnl_link_get_af_size(const struct net_device *dev,
493 u32 ext_filter_mask)
f8ff182c
TG
494{
495 struct rtnl_af_ops *af_ops;
496 size_t size;
497
498 /* IFLA_AF_SPEC */
499 size = nla_total_size(sizeof(struct nlattr));
500
5fa85a09
FW
501 rcu_read_lock();
502 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
f8ff182c
TG
503 if (af_ops->get_link_af_size) {
504 /* AF_* + nested data */
505 size += nla_total_size(sizeof(struct nlattr)) +
b1974ed0 506 af_ops->get_link_af_size(dev, ext_filter_mask);
f8ff182c
TG
507 }
508 }
5fa85a09 509 rcu_read_unlock();
f8ff182c
TG
510
511 return size;
512}
513
ba7d49b1 514static bool rtnl_have_link_slave_info(const struct net_device *dev)
38f7b870 515{
ba7d49b1 516 struct net_device *master_dev;
4c82a95e 517 bool ret = false;
38f7b870 518
4c82a95e
FW
519 rcu_read_lock();
520
521 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
813f020c 522 if (master_dev && master_dev->rtnl_link_ops)
4c82a95e
FW
523 ret = true;
524 rcu_read_unlock();
525 return ret;
ba7d49b1
JP
526}
527
528static int rtnl_link_slave_info_fill(struct sk_buff *skb,
529 const struct net_device *dev)
530{
531 struct net_device *master_dev;
532 const struct rtnl_link_ops *ops;
533 struct nlattr *slave_data;
534 int err;
38f7b870 535
ba7d49b1
JP
536 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
537 if (!master_dev)
538 return 0;
539 ops = master_dev->rtnl_link_ops;
540 if (!ops)
541 return 0;
542 if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
543 return -EMSGSIZE;
544 if (ops->fill_slave_info) {
545 slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA);
546 if (!slave_data)
547 return -EMSGSIZE;
548 err = ops->fill_slave_info(skb, master_dev, dev);
549 if (err < 0)
550 goto err_cancel_slave_data;
551 nla_nest_end(skb, slave_data);
552 }
553 return 0;
554
555err_cancel_slave_data:
556 nla_nest_cancel(skb, slave_data);
557 return err;
558}
559
560static int rtnl_link_info_fill(struct sk_buff *skb,
561 const struct net_device *dev)
562{
563 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
564 struct nlattr *data;
565 int err;
566
567 if (!ops)
568 return 0;
38f7b870 569 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
ba7d49b1 570 return -EMSGSIZE;
38f7b870
PM
571 if (ops->fill_xstats) {
572 err = ops->fill_xstats(skb, dev);
573 if (err < 0)
ba7d49b1 574 return err;
38f7b870
PM
575 }
576 if (ops->fill_info) {
577 data = nla_nest_start(skb, IFLA_INFO_DATA);
ba7d49b1
JP
578 if (data == NULL)
579 return -EMSGSIZE;
38f7b870
PM
580 err = ops->fill_info(skb, dev);
581 if (err < 0)
582 goto err_cancel_data;
583 nla_nest_end(skb, data);
584 }
38f7b870
PM
585 return 0;
586
587err_cancel_data:
588 nla_nest_cancel(skb, data);
ba7d49b1
JP
589 return err;
590}
591
592static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
593{
594 struct nlattr *linkinfo;
595 int err = -EMSGSIZE;
596
597 linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
598 if (linkinfo == NULL)
599 goto out;
600
601 err = rtnl_link_info_fill(skb, dev);
602 if (err < 0)
603 goto err_cancel_link;
604
605 err = rtnl_link_slave_info_fill(skb, dev);
606 if (err < 0)
607 goto err_cancel_link;
608
609 nla_nest_end(skb, linkinfo);
610 return 0;
611
38f7b870
PM
612err_cancel_link:
613 nla_nest_cancel(skb, linkinfo);
614out:
615 return err;
616}
617
95c96174 618int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
1da177e4 619{
97c53cac 620 struct sock *rtnl = net->rtnl;
1da177e4
LT
621 int err = 0;
622
ac6d439d 623 NETLINK_CB(skb).dst_group = group;
1da177e4 624 if (echo)
63354797 625 refcount_inc(&skb->users);
1da177e4
LT
626 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
627 if (echo)
628 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
629 return err;
630}
631
97c53cac 632int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
2942e900 633{
97c53cac
DL
634 struct sock *rtnl = net->rtnl;
635
2942e900
TG
636 return nlmsg_unicast(rtnl, skb, pid);
637}
e0d087af 638EXPORT_SYMBOL(rtnl_unicast);
2942e900 639
1ce85fe4
PNA
640void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
641 struct nlmsghdr *nlh, gfp_t flags)
97676b6b 642{
97c53cac 643 struct sock *rtnl = net->rtnl;
97676b6b
TG
644 int report = 0;
645
646 if (nlh)
647 report = nlmsg_report(nlh);
648
1ce85fe4 649 nlmsg_notify(rtnl, skb, pid, group, report, flags);
97676b6b 650}
e0d087af 651EXPORT_SYMBOL(rtnl_notify);
97676b6b 652
97c53cac 653void rtnl_set_sk_err(struct net *net, u32 group, int error)
97676b6b 654{
97c53cac
DL
655 struct sock *rtnl = net->rtnl;
656
97676b6b
TG
657 netlink_set_err(rtnl, 0, group, error);
658}
e0d087af 659EXPORT_SYMBOL(rtnl_set_sk_err);
97676b6b 660
1da177e4
LT
661int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
662{
2d7202bf
TG
663 struct nlattr *mx;
664 int i, valid = 0;
665
666 mx = nla_nest_start(skb, RTA_METRICS);
667 if (mx == NULL)
668 return -ENOBUFS;
669
670 for (i = 0; i < RTAX_MAX; i++) {
671 if (metrics[i]) {
ea697639
DB
672 if (i == RTAX_CC_ALGO - 1) {
673 char tmp[TCP_CA_NAME_MAX], *name;
674
675 name = tcp_ca_get_name_by_key(metrics[i], tmp);
676 if (!name)
677 continue;
678 if (nla_put_string(skb, i + 1, name))
679 goto nla_put_failure;
c3a8d947
DB
680 } else if (i == RTAX_FEATURES - 1) {
681 u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
682
f8edcd12
PS
683 if (!user_features)
684 continue;
c3a8d947
DB
685 BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
686 if (nla_put_u32(skb, i + 1, user_features))
687 goto nla_put_failure;
ea697639
DB
688 } else {
689 if (nla_put_u32(skb, i + 1, metrics[i]))
690 goto nla_put_failure;
691 }
2d7202bf 692 valid++;
2d7202bf 693 }
1da177e4 694 }
1da177e4 695
a57d27fc
DM
696 if (!valid) {
697 nla_nest_cancel(skb, mx);
698 return 0;
699 }
2d7202bf
TG
700
701 return nla_nest_end(skb, mx);
702
703nla_put_failure:
bc3ed28c
TG
704 nla_nest_cancel(skb, mx);
705 return -EMSGSIZE;
1da177e4 706}
e0d087af 707EXPORT_SYMBOL(rtnetlink_put_metrics);
1da177e4 708
e3703b3d 709int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
87a50699 710 long expires, u32 error)
e3703b3d
TG
711{
712 struct rta_cacheinfo ci = {
a399a805 713 .rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse),
e3703b3d
TG
714 .rta_used = dst->__use,
715 .rta_clntref = atomic_read(&(dst->__refcnt)),
716 .rta_error = error,
717 .rta_id = id,
e3703b3d
TG
718 };
719
8253947e
LW
720 if (expires) {
721 unsigned long clock;
e3703b3d 722
8253947e
LW
723 clock = jiffies_to_clock_t(abs(expires));
724 clock = min_t(unsigned long, clock, INT_MAX);
725 ci.rta_expires = (expires > 0) ? clock : -clock;
726 }
e3703b3d
TG
727 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
728}
e3703b3d 729EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
1da177e4 730
93b2d4a2 731static void set_operstate(struct net_device *dev, unsigned char transition)
b00055aa
SR
732{
733 unsigned char operstate = dev->operstate;
734
e0d087af 735 switch (transition) {
b00055aa
SR
736 case IF_OPER_UP:
737 if ((operstate == IF_OPER_DORMANT ||
738 operstate == IF_OPER_UNKNOWN) &&
739 !netif_dormant(dev))
740 operstate = IF_OPER_UP;
741 break;
742
743 case IF_OPER_DORMANT:
744 if (operstate == IF_OPER_UP ||
745 operstate == IF_OPER_UNKNOWN)
746 operstate = IF_OPER_DORMANT;
747 break;
3ff50b79 748 }
b00055aa
SR
749
750 if (dev->operstate != operstate) {
751 write_lock_bh(&dev_base_lock);
752 dev->operstate = operstate;
753 write_unlock_bh(&dev_base_lock);
93b2d4a2
DM
754 netdev_state_change(dev);
755 }
b00055aa
SR
756}
757
b1beb681
JB
758static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
759{
760 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
761 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
762}
763
3729d502
PM
764static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
765 const struct ifinfomsg *ifm)
766{
767 unsigned int flags = ifm->ifi_flags;
768
769 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
770 if (ifm->ifi_change)
771 flags = (flags & ifm->ifi_change) |
b1beb681 772 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
3729d502
PM
773
774 return flags;
775}
776
b60c5115 777static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
be1f3c2c 778 const struct rtnl_link_stats64 *b)
1da177e4 779{
b60c5115
TG
780 a->rx_packets = b->rx_packets;
781 a->tx_packets = b->tx_packets;
782 a->rx_bytes = b->rx_bytes;
783 a->tx_bytes = b->tx_bytes;
784 a->rx_errors = b->rx_errors;
785 a->tx_errors = b->tx_errors;
786 a->rx_dropped = b->rx_dropped;
787 a->tx_dropped = b->tx_dropped;
788
789 a->multicast = b->multicast;
790 a->collisions = b->collisions;
791
792 a->rx_length_errors = b->rx_length_errors;
793 a->rx_over_errors = b->rx_over_errors;
794 a->rx_crc_errors = b->rx_crc_errors;
795 a->rx_frame_errors = b->rx_frame_errors;
796 a->rx_fifo_errors = b->rx_fifo_errors;
797 a->rx_missed_errors = b->rx_missed_errors;
798
799 a->tx_aborted_errors = b->tx_aborted_errors;
800 a->tx_carrier_errors = b->tx_carrier_errors;
801 a->tx_fifo_errors = b->tx_fifo_errors;
802 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
803 a->tx_window_errors = b->tx_window_errors;
804
805 a->rx_compressed = b->rx_compressed;
806 a->tx_compressed = b->tx_compressed;
6e7333d3
JW
807
808 a->rx_nohandler = b->rx_nohandler;
10708f37
JE
809}
810
c02db8c6 811/* All VF info */
115c9b81
GR
812static inline int rtnl_vfinfo_size(const struct net_device *dev,
813 u32 ext_filter_mask)
ebc08a6f 814{
9af15c38 815 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
c02db8c6 816 int num_vfs = dev_num_vf(dev->dev.parent);
7e75f74a 817 size_t size = nla_total_size(0);
045de01a 818 size += num_vfs *
7e75f74a
SD
819 (nla_total_size(0) +
820 nla_total_size(sizeof(struct ifla_vf_mac)) +
821 nla_total_size(sizeof(struct ifla_vf_vlan)) +
822 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
79aab093
MS
823 nla_total_size(MAX_VLAN_LIST_LEN *
824 sizeof(struct ifla_vf_vlan_info)) +
ed616689 825 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
7e75f74a 826 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
945a3676 827 nla_total_size(sizeof(struct ifla_vf_rate)) +
01a3d796 828 nla_total_size(sizeof(struct ifla_vf_link_state)) +
3b766cd8 829 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
7e75f74a 830 nla_total_size(0) + /* nest IFLA_VF_STATS */
3b766cd8 831 /* IFLA_VF_STATS_RX_PACKETS */
343a6d8e 832 nla_total_size_64bit(sizeof(__u64)) +
3b766cd8 833 /* IFLA_VF_STATS_TX_PACKETS */
343a6d8e 834 nla_total_size_64bit(sizeof(__u64)) +
3b766cd8 835 /* IFLA_VF_STATS_RX_BYTES */
343a6d8e 836 nla_total_size_64bit(sizeof(__u64)) +
3b766cd8 837 /* IFLA_VF_STATS_TX_BYTES */
343a6d8e 838 nla_total_size_64bit(sizeof(__u64)) +
3b766cd8 839 /* IFLA_VF_STATS_BROADCAST */
343a6d8e 840 nla_total_size_64bit(sizeof(__u64)) +
3b766cd8 841 /* IFLA_VF_STATS_MULTICAST */
343a6d8e 842 nla_total_size_64bit(sizeof(__u64)) +
dd461d6a 843 nla_total_size(sizeof(struct ifla_vf_trust)));
c02db8c6
CW
844 return size;
845 } else
ebc08a6f
WM
846 return 0;
847}
848
c53864fd
DG
849static size_t rtnl_port_size(const struct net_device *dev,
850 u32 ext_filter_mask)
57b61080
SF
851{
852 size_t port_size = nla_total_size(4) /* PORT_VF */
853 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
57b61080
SF
854 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
855 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
856 + nla_total_size(1) /* PROT_VDP_REQUEST */
857 + nla_total_size(2); /* PORT_VDP_RESPONSE */
858 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
859 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
860 + port_size;
861 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
862 + port_size;
863
c53864fd
DG
864 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
865 !(ext_filter_mask & RTEXT_FILTER_VF))
57b61080
SF
866 return 0;
867 if (dev_num_vf(dev->dev.parent))
868 return port_self_size + vf_ports_size +
869 vf_port_size * dev_num_vf(dev->dev.parent);
870 else
871 return port_self_size;
872}
873
b5cdae32 874static size_t rtnl_xdp_size(void)
d1fdd913 875{
b3cfaa31 876 size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */
58038695
MKL
877 nla_total_size(1) + /* XDP_ATTACHED */
878 nla_total_size(4); /* XDP_PROG_ID */
d1fdd913 879
b5cdae32 880 return xdp_size;
d1fdd913
BB
881}
882
115c9b81
GR
883static noinline size_t if_nlmsg_size(const struct net_device *dev,
884 u32 ext_filter_mask)
339bf98f
TG
885{
886 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
887 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
0b815a1a 888 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
339bf98f 889 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
270cb4d0 890 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
339bf98f 891 + nla_total_size(sizeof(struct rtnl_link_stats))
35c58459 892 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
339bf98f
TG
893 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
894 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
895 + nla_total_size(4) /* IFLA_TXQLEN */
896 + nla_total_size(4) /* IFLA_WEIGHT */
897 + nla_total_size(4) /* IFLA_MTU */
898 + nla_total_size(4) /* IFLA_LINK */
899 + nla_total_size(4) /* IFLA_MASTER */
9a57247f 900 + nla_total_size(1) /* IFLA_CARRIER */
edbc0bb3 901 + nla_total_size(4) /* IFLA_PROMISCUITY */
76ff5cc9
JP
902 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
903 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
6919756c
TK
904 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
905 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
339bf98f 906 + nla_total_size(1) /* IFLA_OPERSTATE */
38f7b870 907 + nla_total_size(1) /* IFLA_LINKMODE */
2d3b479d 908 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
d37512a2 909 + nla_total_size(4) /* IFLA_LINK_NETNSID */
db833d40 910 + nla_total_size(4) /* IFLA_GROUP */
115c9b81
GR
911 + nla_total_size(ext_filter_mask
912 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
913 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
c53864fd 914 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
f8ff182c 915 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
b1974ed0 916 + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
82f28412 917 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
88d6378b 918 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
c57c7a95 919 + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
b5cdae32 920 + rtnl_xdp_size() /* IFLA_XDP */
3d3ea5af 921 + nla_total_size(4) /* IFLA_EVENT */
6621dd29 922 + nla_total_size(4) /* IFLA_NEW_NETNSID */
03ac738d 923 + nla_total_size(1) /* IFLA_PROTO_DOWN */
79e1ad14
JB
924 + nla_total_size(4) /* IFLA_IF_NETNSID */
925 + 0;
339bf98f
TG
926}
927
57b61080
SF
928static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
929{
930 struct nlattr *vf_ports;
931 struct nlattr *vf_port;
932 int vf;
933 int err;
934
935 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
936 if (!vf_ports)
937 return -EMSGSIZE;
938
939 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
940 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
8ca94183
SF
941 if (!vf_port)
942 goto nla_put_failure;
a6574349
DM
943 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
944 goto nla_put_failure;
57b61080 945 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
8ca94183
SF
946 if (err == -EMSGSIZE)
947 goto nla_put_failure;
57b61080 948 if (err) {
57b61080
SF
949 nla_nest_cancel(skb, vf_port);
950 continue;
951 }
952 nla_nest_end(skb, vf_port);
953 }
954
955 nla_nest_end(skb, vf_ports);
956
957 return 0;
8ca94183
SF
958
959nla_put_failure:
960 nla_nest_cancel(skb, vf_ports);
961 return -EMSGSIZE;
57b61080
SF
962}
963
964static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
965{
966 struct nlattr *port_self;
967 int err;
968
969 port_self = nla_nest_start(skb, IFLA_PORT_SELF);
970 if (!port_self)
971 return -EMSGSIZE;
972
973 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
974 if (err) {
975 nla_nest_cancel(skb, port_self);
8ca94183 976 return (err == -EMSGSIZE) ? err : 0;
57b61080
SF
977 }
978
979 nla_nest_end(skb, port_self);
980
981 return 0;
982}
983
c53864fd
DG
984static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
985 u32 ext_filter_mask)
57b61080
SF
986{
987 int err;
988
c53864fd
DG
989 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
990 !(ext_filter_mask & RTEXT_FILTER_VF))
57b61080
SF
991 return 0;
992
993 err = rtnl_port_self_fill(skb, dev);
994 if (err)
995 return err;
996
997 if (dev_num_vf(dev->dev.parent)) {
998 err = rtnl_vf_ports_fill(skb, dev);
999 if (err)
1000 return err;
1001 }
1002
1003 return 0;
1004}
1005
66cae9ed
JP
1006static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
1007{
1008 int err;
02637fce 1009 struct netdev_phys_item_id ppid;
66cae9ed
JP
1010
1011 err = dev_get_phys_port_id(dev, &ppid);
1012 if (err) {
1013 if (err == -EOPNOTSUPP)
1014 return 0;
1015 return err;
1016 }
1017
1018 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1019 return -EMSGSIZE;
1020
1021 return 0;
1022}
1023
db24a904
DA
1024static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1025{
1026 char name[IFNAMSIZ];
1027 int err;
1028
1029 err = dev_get_phys_port_name(dev, name, sizeof(name));
1030 if (err) {
1031 if (err == -EOPNOTSUPP)
1032 return 0;
1033 return err;
1034 }
1035
77ef033b 1036 if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
db24a904
DA
1037 return -EMSGSIZE;
1038
1039 return 0;
1040}
1041
82f28412
JP
1042static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1043{
1044 int err;
f8e20a9f 1045 struct switchdev_attr attr = {
6ff64f6f 1046 .orig_dev = dev,
1f868398 1047 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
f8e20a9f
SF
1048 .flags = SWITCHDEV_F_NO_RECURSE,
1049 };
82f28412 1050
f8e20a9f 1051 err = switchdev_port_attr_get(dev, &attr);
82f28412
JP
1052 if (err) {
1053 if (err == -EOPNOTSUPP)
1054 return 0;
1055 return err;
1056 }
1057
42275bd8
SF
1058 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len,
1059 attr.u.ppid.id))
82f28412
JP
1060 return -EMSGSIZE;
1061
1062 return 0;
1063}
1064
b22b941b
HFS
1065static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1066 struct net_device *dev)
1067{
550bce59 1068 struct rtnl_link_stats64 *sp;
b22b941b 1069 struct nlattr *attr;
18402843 1070
58414d32
ND
1071 attr = nla_reserve_64bit(skb, IFLA_STATS64,
1072 sizeof(struct rtnl_link_stats64), IFLA_PAD);
b22b941b
HFS
1073 if (!attr)
1074 return -EMSGSIZE;
1075
550bce59
RP
1076 sp = nla_data(attr);
1077 dev_get_stats(dev, sp);
b22b941b 1078
550bce59
RP
1079 attr = nla_reserve(skb, IFLA_STATS,
1080 sizeof(struct rtnl_link_stats));
b22b941b
HFS
1081 if (!attr)
1082 return -EMSGSIZE;
1083
550bce59 1084 copy_rtnl_link_stats(nla_data(attr), sp);
b22b941b
HFS
1085
1086 return 0;
1087}
1088
1089static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1090 struct net_device *dev,
1091 int vfs_num,
1092 struct nlattr *vfinfo)
1093{
1094 struct ifla_vf_rss_query_en vf_rss_query_en;
79aab093 1095 struct nlattr *vf, *vfstats, *vfvlanlist;
b22b941b 1096 struct ifla_vf_link_state vf_linkstate;
79aab093 1097 struct ifla_vf_vlan_info vf_vlan_info;
b22b941b
HFS
1098 struct ifla_vf_spoofchk vf_spoofchk;
1099 struct ifla_vf_tx_rate vf_tx_rate;
1100 struct ifla_vf_stats vf_stats;
1101 struct ifla_vf_trust vf_trust;
1102 struct ifla_vf_vlan vf_vlan;
1103 struct ifla_vf_rate vf_rate;
b22b941b
HFS
1104 struct ifla_vf_mac vf_mac;
1105 struct ifla_vf_info ivi;
1106
0eed9cf5
MY
1107 memset(&ivi, 0, sizeof(ivi));
1108
b22b941b
HFS
1109 /* Not all SR-IOV capable drivers support the
1110 * spoofcheck and "RSS query enable" query. Preset to
1111 * -1 so the user space tool can detect that the driver
1112 * didn't report anything.
1113 */
1114 ivi.spoofchk = -1;
1115 ivi.rss_query_en = -1;
1116 ivi.trusted = -1;
b22b941b
HFS
1117 /* The default value for VF link state is "auto"
1118 * IFLA_VF_LINK_STATE_AUTO which equals zero
1119 */
1120 ivi.linkstate = 0;
79aab093
MS
1121 /* VLAN Protocol by default is 802.1Q */
1122 ivi.vlan_proto = htons(ETH_P_8021Q);
b22b941b
HFS
1123 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1124 return 0;
1125
775f4f05
DC
1126 memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
1127
b22b941b
HFS
1128 vf_mac.vf =
1129 vf_vlan.vf =
79aab093 1130 vf_vlan_info.vf =
b22b941b
HFS
1131 vf_rate.vf =
1132 vf_tx_rate.vf =
1133 vf_spoofchk.vf =
1134 vf_linkstate.vf =
1135 vf_rss_query_en.vf =
1136 vf_trust.vf = ivi.vf;
1137
1138 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1139 vf_vlan.vlan = ivi.vlan;
1140 vf_vlan.qos = ivi.qos;
79aab093
MS
1141 vf_vlan_info.vlan = ivi.vlan;
1142 vf_vlan_info.qos = ivi.qos;
1143 vf_vlan_info.vlan_proto = ivi.vlan_proto;
b22b941b
HFS
1144 vf_tx_rate.rate = ivi.max_tx_rate;
1145 vf_rate.min_tx_rate = ivi.min_tx_rate;
1146 vf_rate.max_tx_rate = ivi.max_tx_rate;
1147 vf_spoofchk.setting = ivi.spoofchk;
1148 vf_linkstate.link_state = ivi.linkstate;
1149 vf_rss_query_en.setting = ivi.rss_query_en;
1150 vf_trust.setting = ivi.trusted;
1151 vf = nla_nest_start(skb, IFLA_VF_INFO);
79aab093
MS
1152 if (!vf)
1153 goto nla_put_vfinfo_failure;
b22b941b
HFS
1154 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1155 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1156 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1157 &vf_rate) ||
1158 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1159 &vf_tx_rate) ||
1160 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1161 &vf_spoofchk) ||
1162 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1163 &vf_linkstate) ||
1164 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1165 sizeof(vf_rss_query_en),
1166 &vf_rss_query_en) ||
1167 nla_put(skb, IFLA_VF_TRUST,
1168 sizeof(vf_trust), &vf_trust))
79aab093
MS
1169 goto nla_put_vf_failure;
1170 vfvlanlist = nla_nest_start(skb, IFLA_VF_VLAN_LIST);
1171 if (!vfvlanlist)
1172 goto nla_put_vf_failure;
1173 if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
1174 &vf_vlan_info)) {
1175 nla_nest_cancel(skb, vfvlanlist);
1176 goto nla_put_vf_failure;
1177 }
1178 nla_nest_end(skb, vfvlanlist);
b22b941b
HFS
1179 memset(&vf_stats, 0, sizeof(vf_stats));
1180 if (dev->netdev_ops->ndo_get_vf_stats)
1181 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1182 &vf_stats);
1183 vfstats = nla_nest_start(skb, IFLA_VF_STATS);
79aab093
MS
1184 if (!vfstats)
1185 goto nla_put_vf_failure;
343a6d8e
ND
1186 if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1187 vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1188 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1189 vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1190 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1191 vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1192 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1193 vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1194 nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1195 vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1196 nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
79aab093
MS
1197 vf_stats.multicast, IFLA_VF_STATS_PAD)) {
1198 nla_nest_cancel(skb, vfstats);
1199 goto nla_put_vf_failure;
1200 }
b22b941b
HFS
1201 nla_nest_end(skb, vfstats);
1202 nla_nest_end(skb, vf);
1203 return 0;
79aab093
MS
1204
1205nla_put_vf_failure:
1206 nla_nest_cancel(skb, vf);
1207nla_put_vfinfo_failure:
1208 nla_nest_cancel(skb, vfinfo);
1209 return -EMSGSIZE;
b22b941b
HFS
1210}
1211
250fc3df
FW
1212static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb,
1213 struct net_device *dev,
1214 u32 ext_filter_mask)
1215{
1216 struct nlattr *vfinfo;
1217 int i, num_vfs;
1218
1219 if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0))
1220 return 0;
1221
1222 num_vfs = dev_num_vf(dev->dev.parent);
1223 if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs))
1224 return -EMSGSIZE;
1225
1226 if (!dev->netdev_ops->ndo_get_vf_config)
1227 return 0;
1228
1229 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1230 if (!vfinfo)
1231 return -EMSGSIZE;
1232
1233 for (i = 0; i < num_vfs; i++) {
1234 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1235 return -EMSGSIZE;
1236 }
1237
1238 nla_nest_end(skb, vfinfo);
1239 return 0;
1240}
1241
b22b941b
HFS
1242static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1243{
5f8e4474
KL
1244 struct rtnl_link_ifmap map;
1245
1246 memset(&map, 0, sizeof(map));
1247 map.mem_start = dev->mem_start;
1248 map.mem_end = dev->mem_end;
1249 map.base_addr = dev->base_addr;
1250 map.irq = dev->irq;
1251 map.dma = dev->dma;
1252 map.port = dev->if_port;
1253
270cb4d0 1254 if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
b22b941b
HFS
1255 return -EMSGSIZE;
1256
1257 return 0;
1258}
1259
58038695 1260static u8 rtnl_xdp_attached_mode(struct net_device *dev, u32 *prog_id)
d67b9cd2
DB
1261{
1262 const struct net_device_ops *ops = dev->netdev_ops;
58038695 1263 const struct bpf_prog *generic_xdp_prog;
d67b9cd2
DB
1264
1265 ASSERT_RTNL();
1266
58038695
MKL
1267 *prog_id = 0;
1268 generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
1269 if (generic_xdp_prog) {
1270 *prog_id = generic_xdp_prog->aux->id;
d67b9cd2 1271 return XDP_ATTACHED_SKB;
58038695 1272 }
f4e63525 1273 if (!ops->ndo_bpf)
ce158e58 1274 return XDP_ATTACHED_NONE;
d67b9cd2 1275
f4e63525 1276 return __dev_xdp_attached(dev, ops->ndo_bpf, prog_id);
d67b9cd2
DB
1277}
1278
d1fdd913
BB
1279static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1280{
d1fdd913 1281 struct nlattr *xdp;
58038695 1282 u32 prog_id;
d1fdd913
BB
1283 int err;
1284
d1fdd913
BB
1285 xdp = nla_nest_start(skb, IFLA_XDP);
1286 if (!xdp)
1287 return -EMSGSIZE;
d67b9cd2
DB
1288
1289 err = nla_put_u8(skb, IFLA_XDP_ATTACHED,
58038695 1290 rtnl_xdp_attached_mode(dev, &prog_id));
d1fdd913
BB
1291 if (err)
1292 goto err_cancel;
1293
58038695
MKL
1294 if (prog_id) {
1295 err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
1296 if (err)
1297 goto err_cancel;
1298 }
1299
d1fdd913
BB
1300 nla_nest_end(skb, xdp);
1301 return 0;
1302
1303err_cancel:
1304 nla_nest_cancel(skb, xdp);
1305 return err;
1306}
1307
3d3ea5af
VY
1308static u32 rtnl_get_event(unsigned long event)
1309{
1310 u32 rtnl_event_type = IFLA_EVENT_NONE;
1311
1312 switch (event) {
1313 case NETDEV_REBOOT:
1314 rtnl_event_type = IFLA_EVENT_REBOOT;
1315 break;
1316 case NETDEV_FEAT_CHANGE:
1317 rtnl_event_type = IFLA_EVENT_FEATURES;
1318 break;
1319 case NETDEV_BONDING_FAILOVER:
1320 rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
1321 break;
1322 case NETDEV_NOTIFY_PEERS:
1323 rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
1324 break;
1325 case NETDEV_RESEND_IGMP:
1326 rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
1327 break;
1328 case NETDEV_CHANGEINFODATA:
1329 rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
1330 break;
1331 default:
1332 break;
1333 }
1334
1335 return rtnl_event_type;
1336}
1337
79110a04
FW
1338static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
1339{
1340 const struct net_device *upper_dev;
1341 int ret = 0;
1342
1343 rcu_read_lock();
1344
1345 upper_dev = netdev_master_upper_dev_get_rcu(dev);
1346 if (upper_dev)
1347 ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);
1348
1349 rcu_read_unlock();
1350 return ret;
1351}
1352
1353static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev)
1354{
1355 int ifindex = dev_get_iflink(dev);
1356
1357 if (dev->ifindex == ifindex)
1358 return 0;
1359
1360 return nla_put_u32(skb, IFLA_LINK, ifindex);
1361}
1362
6c557001
FW
1363static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
1364 struct net_device *dev)
1365{
1366 char buf[IFALIASZ];
1367 int ret;
1368
1369 ret = dev_get_alias(dev, buf, sizeof(buf));
1370 return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
1371}
1372
b1e66b9a 1373static int rtnl_fill_link_netnsid(struct sk_buff *skb,
79e1ad14
JB
1374 const struct net_device *dev,
1375 struct net *src_net)
b1e66b9a
FW
1376{
1377 if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
1378 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1379
1380 if (!net_eq(dev_net(dev), link_net)) {
79e1ad14 1381 int id = peernet2id_alloc(src_net, link_net);
b1e66b9a
FW
1382
1383 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1384 return -EMSGSIZE;
1385 }
1386 }
1387
1388 return 0;
1389}
1390
070cbf5b
FW
1391static int rtnl_fill_link_af(struct sk_buff *skb,
1392 const struct net_device *dev,
1393 u32 ext_filter_mask)
1394{
1395 const struct rtnl_af_ops *af_ops;
1396 struct nlattr *af_spec;
1397
1398 af_spec = nla_nest_start(skb, IFLA_AF_SPEC);
1399 if (!af_spec)
1400 return -EMSGSIZE;
1401
5fa85a09 1402 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
070cbf5b
FW
1403 struct nlattr *af;
1404 int err;
1405
1406 if (!af_ops->fill_link_af)
1407 continue;
1408
1409 af = nla_nest_start(skb, af_ops->family);
1410 if (!af)
1411 return -EMSGSIZE;
1412
1413 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1414 /*
1415 * Caller may return ENODATA to indicate that there
1416 * was no data to be dumped. This is not an error, it
1417 * means we should trim the attribute header and
1418 * continue.
1419 */
1420 if (err == -ENODATA)
1421 nla_nest_cancel(skb, af);
1422 else if (err < 0)
1423 return -EMSGSIZE;
1424
1425 nla_nest_end(skb, af);
1426 }
1427
1428 nla_nest_end(skb, af_spec);
1429 return 0;
1430}
1431
79e1ad14
JB
1432static int rtnl_fill_ifinfo(struct sk_buff *skb,
1433 struct net_device *dev, struct net *src_net,
575c3e2a 1434 int type, u32 pid, u32 seq, u32 change,
3d3ea5af 1435 unsigned int flags, u32 ext_filter_mask,
79e1ad14 1436 u32 event, int *new_nsid, int tgt_netnsid)
b60c5115
TG
1437{
1438 struct ifinfomsg *ifm;
1439 struct nlmsghdr *nlh;
1da177e4 1440
2907c35f 1441 ASSERT_RTNL();
b60c5115
TG
1442 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1443 if (nlh == NULL)
26932566 1444 return -EMSGSIZE;
1da177e4 1445
b60c5115
TG
1446 ifm = nlmsg_data(nlh);
1447 ifm->ifi_family = AF_UNSPEC;
1448 ifm->__ifi_pad = 0;
1449 ifm->ifi_type = dev->type;
1450 ifm->ifi_index = dev->ifindex;
1451 ifm->ifi_flags = dev_get_flags(dev);
1452 ifm->ifi_change = change;
1453
79e1ad14
JB
1454 if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_IF_NETNSID, tgt_netnsid))
1455 goto nla_put_failure;
1456
a6574349
DM
1457 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1458 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1459 nla_put_u8(skb, IFLA_OPERSTATE,
1460 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1461 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1462 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1463 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
edbc0bb3 1464 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
76ff5cc9 1465 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
c70ce028
ED
1466 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1467 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
1d69c2b3 1468#ifdef CONFIG_RPS
76ff5cc9 1469 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
1d69c2b3 1470#endif
79110a04
FW
1471 nla_put_iflink(skb, dev) ||
1472 put_master_ifindex(skb, dev) ||
9a57247f 1473 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
a6574349
DM
1474 (dev->qdisc &&
1475 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
6c557001 1476 nla_put_ifalias(skb, dev) ||
2d3b479d 1477 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
88d6378b
AK
1478 atomic_read(&dev->carrier_changes)) ||
1479 nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
a6574349 1480 goto nla_put_failure;
0b815a1a 1481
3d3ea5af
VY
1482 if (event != IFLA_EVENT_NONE) {
1483 if (nla_put_u32(skb, IFLA_EVENT, event))
1484 goto nla_put_failure;
1485 }
1486
b22b941b
HFS
1487 if (rtnl_fill_link_ifmap(skb, dev))
1488 goto nla_put_failure;
1da177e4
LT
1489
1490 if (dev->addr_len) {
a6574349
DM
1491 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1492 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1493 goto nla_put_failure;
1da177e4
LT
1494 }
1495
66cae9ed
JP
1496 if (rtnl_phys_port_id_fill(skb, dev))
1497 goto nla_put_failure;
1498
db24a904
DA
1499 if (rtnl_phys_port_name_fill(skb, dev))
1500 goto nla_put_failure;
1501
82f28412
JP
1502 if (rtnl_phys_switch_id_fill(skb, dev))
1503 goto nla_put_failure;
1504
b22b941b 1505 if (rtnl_fill_stats(skb, dev))
10708f37 1506 goto nla_put_failure;
10708f37 1507
250fc3df 1508 if (rtnl_fill_vf(skb, dev, ext_filter_mask))
a6574349 1509 goto nla_put_failure;
57b61080 1510
c53864fd 1511 if (rtnl_port_fill(skb, dev, ext_filter_mask))
57b61080
SF
1512 goto nla_put_failure;
1513
d1fdd913
BB
1514 if (rtnl_xdp_fill(skb, dev))
1515 goto nla_put_failure;
1516
ba7d49b1 1517 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
38f7b870
PM
1518 if (rtnl_link_fill(skb, dev) < 0)
1519 goto nla_put_failure;
1520 }
1521
79e1ad14 1522 if (rtnl_fill_link_netnsid(skb, dev, src_net))
b1e66b9a 1523 goto nla_put_failure;
d37512a2 1524
6621dd29
ND
1525 if (new_nsid &&
1526 nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0)
1527 goto nla_put_failure;
1528
5fa85a09 1529 rcu_read_lock();
070cbf5b 1530 if (rtnl_fill_link_af(skb, dev, ext_filter_mask))
5fa85a09
FW
1531 goto nla_put_failure_rcu;
1532 rcu_read_unlock();
f8ff182c 1533
053c095a
JB
1534 nlmsg_end(skb, nlh);
1535 return 0;
b60c5115 1536
5fa85a09
FW
1537nla_put_failure_rcu:
1538 rcu_read_unlock();
b60c5115 1539nla_put_failure:
26932566
PM
1540 nlmsg_cancel(skb, nlh);
1541 return -EMSGSIZE;
1da177e4
LT
1542}
1543
f7b12606 1544static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
5176f91e 1545 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
38f7b870
PM
1546 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1547 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
5176f91e 1548 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
da5e0494 1549 [IFLA_MTU] = { .type = NLA_U32 },
76e87306 1550 [IFLA_LINK] = { .type = NLA_U32 },
fbaec0ea 1551 [IFLA_MASTER] = { .type = NLA_U32 },
9a57247f 1552 [IFLA_CARRIER] = { .type = NLA_U8 },
da5e0494
TG
1553 [IFLA_TXQLEN] = { .type = NLA_U32 },
1554 [IFLA_WEIGHT] = { .type = NLA_U32 },
1555 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1556 [IFLA_LINKMODE] = { .type = NLA_U8 },
76e87306 1557 [IFLA_LINKINFO] = { .type = NLA_NESTED },
d8a5ec67 1558 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
f0630529 1559 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
2459b4c6
ND
1560 /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
1561 * allow 0-length string (needed to remove an alias).
1562 */
1563 [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
c02db8c6 1564 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
57b61080
SF
1565 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1566 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
f8ff182c 1567 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
115c9b81 1568 [IFLA_EXT_MASK] = { .type = NLA_U32 },
edbc0bb3 1569 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
76ff5cc9
JP
1570 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1571 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
02637fce 1572 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
2d3b479d 1573 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
82f28412 1574 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
317f4810 1575 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
88d6378b 1576 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
d1fdd913 1577 [IFLA_XDP] = { .type = NLA_NESTED },
3d3ea5af 1578 [IFLA_EVENT] = { .type = NLA_U32 },
db833d40 1579 [IFLA_GROUP] = { .type = NLA_U32 },
79e1ad14 1580 [IFLA_IF_NETNSID] = { .type = NLA_S32 },
da5e0494
TG
1581};
1582
38f7b870
PM
1583static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1584 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1585 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
ba7d49b1
JP
1586 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1587 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
38f7b870
PM
1588};
1589
c02db8c6 1590static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
364d5716
DB
1591 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1592 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
79aab093 1593 [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED },
364d5716
DB
1594 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1595 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1596 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1597 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
01a3d796 1598 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
3b766cd8 1599 [IFLA_VF_STATS] = { .type = NLA_NESTED },
dd461d6a 1600 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
cc8e27cc
EC
1601 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1602 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
3b766cd8
EBE
1603};
1604
57b61080
SF
1605static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1606 [IFLA_PORT_VF] = { .type = NLA_U32 },
1607 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1608 .len = PORT_PROFILE_MAX },
57b61080
SF
1609 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1610 .len = PORT_UUID_MAX },
1611 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1612 .len = PORT_UUID_MAX },
1613 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1614 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
025331df
DB
1615
1616 /* Unused, but we need to keep it here since user space could
1617 * fill it. It's also broken with regard to NLA_BINARY use in
1618 * combination with structs.
1619 */
1620 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1621 .len = sizeof(struct ifla_port_vsi) },
57b61080
SF
1622};
1623
d1fdd913
BB
1624static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
1625 [IFLA_XDP_FD] = { .type = NLA_S32 },
1626 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 },
85de8576 1627 [IFLA_XDP_FLAGS] = { .type = NLA_U32 },
58038695 1628 [IFLA_XDP_PROG_ID] = { .type = NLA_U32 },
d1fdd913
BB
1629};
1630
dc599f76
DA
1631static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1632{
1633 const struct rtnl_link_ops *ops = NULL;
1634 struct nlattr *linfo[IFLA_INFO_MAX + 1];
1635
fceb6435
JB
1636 if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla,
1637 ifla_info_policy, NULL) < 0)
dc599f76
DA
1638 return NULL;
1639
1640 if (linfo[IFLA_INFO_KIND]) {
1641 char kind[MODULE_NAME_LEN];
1642
1643 nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1644 ops = rtnl_link_ops_get(kind);
1645 }
1646
1647 return ops;
1648}
1649
1650static bool link_master_filtered(struct net_device *dev, int master_idx)
1651{
1652 struct net_device *master;
1653
1654 if (!master_idx)
1655 return false;
1656
1657 master = netdev_master_upper_dev_get(dev);
1658 if (!master || master->ifindex != master_idx)
1659 return true;
1660
1661 return false;
1662}
1663
1664static bool link_kind_filtered(const struct net_device *dev,
1665 const struct rtnl_link_ops *kind_ops)
1666{
1667 if (kind_ops && dev->rtnl_link_ops != kind_ops)
1668 return true;
1669
1670 return false;
1671}
1672
1673static bool link_dump_filtered(struct net_device *dev,
1674 int master_idx,
1675 const struct rtnl_link_ops *kind_ops)
1676{
1677 if (link_master_filtered(dev, master_idx) ||
1678 link_kind_filtered(dev, kind_ops))
1679 return true;
1680
1681 return false;
1682}
1683
f428fe4a 1684static struct net *get_target_net(struct sock *sk, int netnsid)
79e1ad14
JB
1685{
1686 struct net *net;
1687
f428fe4a 1688 net = get_net_ns_by_id(sock_net(sk), netnsid);
79e1ad14
JB
1689 if (!net)
1690 return ERR_PTR(-EINVAL);
1691
1692 /* For now, the caller is required to have CAP_NET_ADMIN in
1693 * the user namespace owning the target net ns.
1694 */
f428fe4a 1695 if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) {
79e1ad14
JB
1696 put_net(net);
1697 return ERR_PTR(-EACCES);
1698 }
1699 return net;
1700}
1701
f7b12606
JP
1702static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
1703{
1704 struct net *net = sock_net(skb->sk);
79e1ad14 1705 struct net *tgt_net = net;
f7b12606
JP
1706 int h, s_h;
1707 int idx = 0, s_idx;
1708 struct net_device *dev;
1709 struct hlist_head *head;
1710 struct nlattr *tb[IFLA_MAX+1];
1711 u32 ext_filter_mask = 0;
dc599f76
DA
1712 const struct rtnl_link_ops *kind_ops = NULL;
1713 unsigned int flags = NLM_F_MULTI;
1714 int master_idx = 0;
79e1ad14 1715 int netnsid = -1;
973462bb 1716 int err;
e5eca6d4 1717 int hdrlen;
f7b12606
JP
1718
1719 s_h = cb->args[0];
1720 s_idx = cb->args[1];
1721
e5eca6d4
MS
1722 /* A hack to preserve kernel<->userspace interface.
1723 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1724 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1725 * what iproute2 < v3.9.0 used.
1726 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1727 * attribute, its netlink message is shorter than struct ifinfomsg.
1728 */
1729 hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1730 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1731
fceb6435
JB
1732 if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX,
1733 ifla_policy, NULL) >= 0) {
79e1ad14
JB
1734 if (tb[IFLA_IF_NETNSID]) {
1735 netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]);
f428fe4a 1736 tgt_net = get_target_net(skb->sk, netnsid);
79e1ad14
JB
1737 if (IS_ERR(tgt_net)) {
1738 tgt_net = net;
1739 netnsid = -1;
1740 }
1741 }
1742
f7b12606
JP
1743 if (tb[IFLA_EXT_MASK])
1744 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
dc599f76
DA
1745
1746 if (tb[IFLA_MASTER])
1747 master_idx = nla_get_u32(tb[IFLA_MASTER]);
1748
1749 if (tb[IFLA_LINKINFO])
1750 kind_ops = linkinfo_to_kind_ops(tb[IFLA_LINKINFO]);
1751
1752 if (master_idx || kind_ops)
1753 flags |= NLM_F_DUMP_FILTERED;
f7b12606
JP
1754 }
1755
1756 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1757 idx = 0;
79e1ad14 1758 head = &tgt_net->dev_index_head[h];
cac5e65e 1759 hlist_for_each_entry(dev, head, index_hlist) {
dc599f76 1760 if (link_dump_filtered(dev, master_idx, kind_ops))
3f0ae05d 1761 goto cont;
f7b12606
JP
1762 if (idx < s_idx)
1763 goto cont;
79e1ad14
JB
1764 err = rtnl_fill_ifinfo(skb, dev, net,
1765 RTM_NEWLINK,
973462bb
DG
1766 NETLINK_CB(cb->skb).portid,
1767 cb->nlh->nlmsg_seq, 0,
dc599f76 1768 flags,
79e1ad14
JB
1769 ext_filter_mask, 0, NULL,
1770 netnsid);
973462bb 1771
f6c5775f
DA
1772 if (err < 0) {
1773 if (likely(skb->len))
1774 goto out;
1775
1776 goto out_err;
1777 }
f7b12606
JP
1778cont:
1779 idx++;
1780 }
1781 }
1782out:
f6c5775f
DA
1783 err = skb->len;
1784out_err:
f7b12606
JP
1785 cb->args[1] = idx;
1786 cb->args[0] = h;
d0225784
JS
1787 cb->seq = net->dev_base_seq;
1788 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
79e1ad14
JB
1789 if (netnsid >= 0)
1790 put_net(tgt_net);
f7b12606 1791
f6c5775f 1792 return err;
f7b12606
JP
1793}
1794
fceb6435
JB
1795int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
1796 struct netlink_ext_ack *exterr)
f7b12606 1797{
fceb6435 1798 return nla_parse(tb, IFLA_MAX, head, len, ifla_policy, exterr);
f7b12606
JP
1799}
1800EXPORT_SYMBOL(rtnl_nla_parse_ifla);
1801
81adee47
EB
1802struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1803{
1804 struct net *net;
1805 /* Examine the link attributes and figure out which
1806 * network namespace we are talking about.
1807 */
1808 if (tb[IFLA_NET_NS_PID])
1809 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
f0630529
EB
1810 else if (tb[IFLA_NET_NS_FD])
1811 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
81adee47
EB
1812 else
1813 net = get_net(src_net);
1814 return net;
1815}
1816EXPORT_SYMBOL(rtnl_link_get_net);
1817
1840bb13
TG
1818static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1819{
1820 if (dev) {
1821 if (tb[IFLA_ADDRESS] &&
1822 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1823 return -EINVAL;
1824
1825 if (tb[IFLA_BROADCAST] &&
1826 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1827 return -EINVAL;
1828 }
1829
cf7afbfe
TG
1830 if (tb[IFLA_AF_SPEC]) {
1831 struct nlattr *af;
1832 int rem, err;
1833
1834 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1835 const struct rtnl_af_ops *af_ops;
1836
5fa85a09
FW
1837 rcu_read_lock();
1838 af_ops = rtnl_af_lookup(nla_type(af));
1839 if (!af_ops) {
1840 rcu_read_unlock();
cf7afbfe 1841 return -EAFNOSUPPORT;
5fa85a09 1842 }
cf7afbfe 1843
5fa85a09
FW
1844 if (!af_ops->set_link_af) {
1845 rcu_read_unlock();
cf7afbfe 1846 return -EOPNOTSUPP;
5fa85a09 1847 }
cf7afbfe
TG
1848
1849 if (af_ops->validate_link_af) {
6d3a9a68 1850 err = af_ops->validate_link_af(dev, af);
5fa85a09
FW
1851 if (err < 0) {
1852 rcu_read_unlock();
cf7afbfe 1853 return err;
5fa85a09 1854 }
cf7afbfe 1855 }
5fa85a09
FW
1856
1857 rcu_read_unlock();
cf7afbfe
TG
1858 }
1859 }
1860
1840bb13
TG
1861 return 0;
1862}
1863
cc8e27cc
EC
1864static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
1865 int guid_type)
1866{
1867 const struct net_device_ops *ops = dev->netdev_ops;
1868
1869 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
1870}
1871
1872static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
1873{
1874 if (dev->type != ARPHRD_INFINIBAND)
1875 return -EOPNOTSUPP;
1876
1877 return handle_infiniband_guid(dev, ivt, guid_type);
1878}
1879
4f7d2cdf 1880static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
c02db8c6 1881{
c02db8c6 1882 const struct net_device_ops *ops = dev->netdev_ops;
4f7d2cdf 1883 int err = -EINVAL;
c02db8c6 1884
4f7d2cdf
DB
1885 if (tb[IFLA_VF_MAC]) {
1886 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
01a3d796 1887
4f7d2cdf
DB
1888 err = -EOPNOTSUPP;
1889 if (ops->ndo_set_vf_mac)
1890 err = ops->ndo_set_vf_mac(dev, ivm->vf,
1891 ivm->mac);
1892 if (err < 0)
1893 return err;
1894 }
1895
1896 if (tb[IFLA_VF_VLAN]) {
1897 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
1898
1899 err = -EOPNOTSUPP;
1900 if (ops->ndo_set_vf_vlan)
1901 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
79aab093
MS
1902 ivv->qos,
1903 htons(ETH_P_8021Q));
1904 if (err < 0)
1905 return err;
1906 }
1907
1908 if (tb[IFLA_VF_VLAN_LIST]) {
1909 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
1910 struct nlattr *attr;
1911 int rem, len = 0;
1912
1913 err = -EOPNOTSUPP;
1914 if (!ops->ndo_set_vf_vlan)
1915 return err;
1916
1917 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
1918 if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
1919 nla_len(attr) < NLA_HDRLEN) {
1920 return -EINVAL;
1921 }
1922 if (len >= MAX_VLAN_LIST_LEN)
1923 return -EOPNOTSUPP;
1924 ivvl[len] = nla_data(attr);
1925
1926 len++;
1927 }
fa34cd94
AB
1928 if (len == 0)
1929 return -EINVAL;
1930
79aab093
MS
1931 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
1932 ivvl[0]->qos, ivvl[0]->vlan_proto);
4f7d2cdf
DB
1933 if (err < 0)
1934 return err;
c02db8c6 1935 }
4f7d2cdf
DB
1936
1937 if (tb[IFLA_VF_TX_RATE]) {
1938 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
1939 struct ifla_vf_info ivf;
1940
1941 err = -EOPNOTSUPP;
1942 if (ops->ndo_get_vf_config)
1943 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
1944 if (err < 0)
1945 return err;
1946
1947 err = -EOPNOTSUPP;
1948 if (ops->ndo_set_vf_rate)
1949 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1950 ivf.min_tx_rate,
1951 ivt->rate);
1952 if (err < 0)
1953 return err;
1954 }
1955
1956 if (tb[IFLA_VF_RATE]) {
1957 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
1958
1959 err = -EOPNOTSUPP;
1960 if (ops->ndo_set_vf_rate)
1961 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1962 ivt->min_tx_rate,
1963 ivt->max_tx_rate);
1964 if (err < 0)
1965 return err;
1966 }
1967
1968 if (tb[IFLA_VF_SPOOFCHK]) {
1969 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
1970
1971 err = -EOPNOTSUPP;
1972 if (ops->ndo_set_vf_spoofchk)
1973 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
1974 ivs->setting);
1975 if (err < 0)
1976 return err;
1977 }
1978
1979 if (tb[IFLA_VF_LINK_STATE]) {
1980 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
1981
1982 err = -EOPNOTSUPP;
1983 if (ops->ndo_set_vf_link_state)
1984 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
1985 ivl->link_state);
1986 if (err < 0)
1987 return err;
1988 }
1989
1990 if (tb[IFLA_VF_RSS_QUERY_EN]) {
1991 struct ifla_vf_rss_query_en *ivrssq_en;
1992
1993 err = -EOPNOTSUPP;
1994 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
1995 if (ops->ndo_set_vf_rss_query_en)
1996 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
1997 ivrssq_en->setting);
1998 if (err < 0)
1999 return err;
2000 }
2001
dd461d6a
HS
2002 if (tb[IFLA_VF_TRUST]) {
2003 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
2004
2005 err = -EOPNOTSUPP;
2006 if (ops->ndo_set_vf_trust)
2007 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
2008 if (err < 0)
2009 return err;
2010 }
2011
cc8e27cc
EC
2012 if (tb[IFLA_VF_IB_NODE_GUID]) {
2013 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
2014
2015 if (!ops->ndo_set_vf_guid)
2016 return -EOPNOTSUPP;
2017
2018 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
2019 }
2020
2021 if (tb[IFLA_VF_IB_PORT_GUID]) {
2022 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
2023
2024 if (!ops->ndo_set_vf_guid)
2025 return -EOPNOTSUPP;
2026
2027 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
2028 }
2029
c02db8c6
CW
2030 return err;
2031}
2032
33eaf2a6
DA
2033static int do_set_master(struct net_device *dev, int ifindex,
2034 struct netlink_ext_ack *extack)
fbaec0ea 2035{
898e5061 2036 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
fbaec0ea
JP
2037 const struct net_device_ops *ops;
2038 int err;
2039
898e5061
JP
2040 if (upper_dev) {
2041 if (upper_dev->ifindex == ifindex)
fbaec0ea 2042 return 0;
898e5061 2043 ops = upper_dev->netdev_ops;
fbaec0ea 2044 if (ops->ndo_del_slave) {
898e5061 2045 err = ops->ndo_del_slave(upper_dev, dev);
fbaec0ea
JP
2046 if (err)
2047 return err;
2048 } else {
2049 return -EOPNOTSUPP;
2050 }
2051 }
2052
2053 if (ifindex) {
898e5061
JP
2054 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
2055 if (!upper_dev)
fbaec0ea 2056 return -EINVAL;
898e5061 2057 ops = upper_dev->netdev_ops;
fbaec0ea 2058 if (ops->ndo_add_slave) {
33eaf2a6 2059 err = ops->ndo_add_slave(upper_dev, dev, extack);
fbaec0ea
JP
2060 if (err)
2061 return err;
2062 } else {
2063 return -EOPNOTSUPP;
2064 }
2065 }
2066 return 0;
2067}
2068
90c325e3 2069#define DO_SETLINK_MODIFIED 0x01
ba998906
ND
2070/* notify flag means notify + modified. */
2071#define DO_SETLINK_NOTIFY 0x03
90f62cf3
EB
2072static int do_setlink(const struct sk_buff *skb,
2073 struct net_device *dev, struct ifinfomsg *ifm,
ddf9f970 2074 struct netlink_ext_ack *extack,
90c325e3 2075 struct nlattr **tb, char *ifname, int status)
1da177e4 2076{
d314774c 2077 const struct net_device_ops *ops = dev->netdev_ops;
0157f60c 2078 int err;
1da177e4 2079
f0630529 2080 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
81adee47 2081 struct net *net = rtnl_link_get_net(dev_net(dev), tb);
d8a5ec67
EB
2082 if (IS_ERR(net)) {
2083 err = PTR_ERR(net);
2084 goto errout;
2085 }
90f62cf3 2086 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
e0ebde0e 2087 put_net(net);
b51642f6
EB
2088 err = -EPERM;
2089 goto errout;
2090 }
d8a5ec67
EB
2091 err = dev_change_net_namespace(dev, net, ifname);
2092 put_net(net);
2093 if (err)
2094 goto errout;
90c325e3 2095 status |= DO_SETLINK_MODIFIED;
d8a5ec67
EB
2096 }
2097
da5e0494 2098 if (tb[IFLA_MAP]) {
1da177e4
LT
2099 struct rtnl_link_ifmap *u_map;
2100 struct ifmap k_map;
2101
d314774c 2102 if (!ops->ndo_set_config) {
1da177e4 2103 err = -EOPNOTSUPP;
0157f60c 2104 goto errout;
1da177e4
LT
2105 }
2106
2107 if (!netif_device_present(dev)) {
2108 err = -ENODEV;
0157f60c 2109 goto errout;
1da177e4 2110 }
1da177e4 2111
da5e0494 2112 u_map = nla_data(tb[IFLA_MAP]);
1da177e4
LT
2113 k_map.mem_start = (unsigned long) u_map->mem_start;
2114 k_map.mem_end = (unsigned long) u_map->mem_end;
2115 k_map.base_addr = (unsigned short) u_map->base_addr;
2116 k_map.irq = (unsigned char) u_map->irq;
2117 k_map.dma = (unsigned char) u_map->dma;
2118 k_map.port = (unsigned char) u_map->port;
2119
d314774c 2120 err = ops->ndo_set_config(dev, &k_map);
da5e0494 2121 if (err < 0)
0157f60c 2122 goto errout;
1da177e4 2123
ba998906 2124 status |= DO_SETLINK_NOTIFY;
1da177e4
LT
2125 }
2126
da5e0494 2127 if (tb[IFLA_ADDRESS]) {
70f8e78e
DM
2128 struct sockaddr *sa;
2129 int len;
2130
153711f9
WC
2131 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2132 sizeof(*sa));
70f8e78e
DM
2133 sa = kmalloc(len, GFP_KERNEL);
2134 if (!sa) {
2135 err = -ENOMEM;
0157f60c 2136 goto errout;
70f8e78e
DM
2137 }
2138 sa->sa_family = dev->type;
da5e0494 2139 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
70f8e78e 2140 dev->addr_len);
e7c3273e 2141 err = dev_set_mac_address(dev, sa);
70f8e78e 2142 kfree(sa);
1da177e4 2143 if (err)
0157f60c 2144 goto errout;
90c325e3 2145 status |= DO_SETLINK_MODIFIED;
1da177e4
LT
2146 }
2147
da5e0494
TG
2148 if (tb[IFLA_MTU]) {
2149 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
2150 if (err < 0)
0157f60c 2151 goto errout;
90c325e3 2152 status |= DO_SETLINK_MODIFIED;
1da177e4
LT
2153 }
2154
cbda10fa
VD
2155 if (tb[IFLA_GROUP]) {
2156 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
ba998906 2157 status |= DO_SETLINK_NOTIFY;
cbda10fa
VD
2158 }
2159
da5e0494
TG
2160 /*
2161 * Interface selected by interface index but interface
2162 * name provided implies that a name change has been
2163 * requested.
2164 */
51055be8 2165 if (ifm->ifi_index > 0 && ifname[0]) {
da5e0494
TG
2166 err = dev_change_name(dev, ifname);
2167 if (err < 0)
0157f60c 2168 goto errout;
90c325e3 2169 status |= DO_SETLINK_MODIFIED;
1da177e4
LT
2170 }
2171
0b815a1a
SH
2172 if (tb[IFLA_IFALIAS]) {
2173 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2174 nla_len(tb[IFLA_IFALIAS]));
2175 if (err < 0)
2176 goto errout;
ba998906 2177 status |= DO_SETLINK_NOTIFY;
0b815a1a
SH
2178 }
2179
da5e0494
TG
2180 if (tb[IFLA_BROADCAST]) {
2181 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
e7c3273e 2182 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
1da177e4
LT
2183 }
2184
83b496e9 2185 if (ifm->ifi_flags || ifm->ifi_change) {
3729d502 2186 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
5f9021cf
JB
2187 if (err < 0)
2188 goto errout;
83b496e9 2189 }
1da177e4 2190
fbaec0ea 2191 if (tb[IFLA_MASTER]) {
33eaf2a6 2192 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
fbaec0ea
JP
2193 if (err)
2194 goto errout;
90c325e3 2195 status |= DO_SETLINK_MODIFIED;
fbaec0ea
JP
2196 }
2197
9a57247f
JP
2198 if (tb[IFLA_CARRIER]) {
2199 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2200 if (err)
2201 goto errout;
90c325e3 2202 status |= DO_SETLINK_MODIFIED;
9a57247f
JP
2203 }
2204
5d1180fc 2205 if (tb[IFLA_TXQLEN]) {
0cd29503
AD
2206 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
2207 unsigned int orig_len = dev->tx_queue_len;
08294a26
JW
2208
2209 if (dev->tx_queue_len ^ value) {
2210 dev->tx_queue_len = value;
2211 err = call_netdevice_notifiers(
2212 NETDEV_CHANGE_TX_QUEUE_LEN, dev);
2213 err = notifier_to_errno(err);
2214 if (err) {
2215 dev->tx_queue_len = orig_len;
2216 goto errout;
2217 }
2d7f669b 2218 status |= DO_SETLINK_MODIFIED;
08294a26 2219 }
5d1180fc 2220 }
b00055aa 2221
da5e0494 2222 if (tb[IFLA_OPERSTATE])
93b2d4a2 2223 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
b00055aa 2224
da5e0494 2225 if (tb[IFLA_LINKMODE]) {
1889b0e7
ND
2226 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2227
93b2d4a2 2228 write_lock_bh(&dev_base_lock);
1889b0e7 2229 if (dev->link_mode ^ value)
ba998906 2230 status |= DO_SETLINK_NOTIFY;
1889b0e7 2231 dev->link_mode = value;
93b2d4a2 2232 write_unlock_bh(&dev_base_lock);
b00055aa
SR
2233 }
2234
c02db8c6 2235 if (tb[IFLA_VFINFO_LIST]) {
4f7d2cdf 2236 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
c02db8c6
CW
2237 struct nlattr *attr;
2238 int rem;
4f7d2cdf 2239
c02db8c6 2240 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
4f7d2cdf
DB
2241 if (nla_type(attr) != IFLA_VF_INFO ||
2242 nla_len(attr) < NLA_HDRLEN) {
253683bb 2243 err = -EINVAL;
c02db8c6 2244 goto errout;
253683bb 2245 }
4f7d2cdf 2246 err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr,
fceb6435 2247 ifla_vf_policy, NULL);
4f7d2cdf
DB
2248 if (err < 0)
2249 goto errout;
2250 err = do_setvfinfo(dev, vfinfo);
c02db8c6
CW
2251 if (err < 0)
2252 goto errout;
ba998906 2253 status |= DO_SETLINK_NOTIFY;
c02db8c6 2254 }
ebc08a6f 2255 }
1da177e4
LT
2256 err = 0;
2257
57b61080
SF
2258 if (tb[IFLA_VF_PORTS]) {
2259 struct nlattr *port[IFLA_PORT_MAX+1];
2260 struct nlattr *attr;
2261 int vf;
2262 int rem;
2263
2264 err = -EOPNOTSUPP;
2265 if (!ops->ndo_set_vf_port)
2266 goto errout;
2267
2268 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
035d210f
DB
2269 if (nla_type(attr) != IFLA_VF_PORT ||
2270 nla_len(attr) < NLA_HDRLEN) {
2271 err = -EINVAL;
2272 goto errout;
2273 }
2274 err = nla_parse_nested(port, IFLA_PORT_MAX, attr,
fceb6435 2275 ifla_port_policy, NULL);
57b61080
SF
2276 if (err < 0)
2277 goto errout;
2278 if (!port[IFLA_PORT_VF]) {
2279 err = -EOPNOTSUPP;
2280 goto errout;
2281 }
2282 vf = nla_get_u32(port[IFLA_PORT_VF]);
2283 err = ops->ndo_set_vf_port(dev, vf, port);
2284 if (err < 0)
2285 goto errout;
ba998906 2286 status |= DO_SETLINK_NOTIFY;
57b61080
SF
2287 }
2288 }
2289 err = 0;
2290
2291 if (tb[IFLA_PORT_SELF]) {
2292 struct nlattr *port[IFLA_PORT_MAX+1];
2293
2294 err = nla_parse_nested(port, IFLA_PORT_MAX,
fceb6435
JB
2295 tb[IFLA_PORT_SELF], ifla_port_policy,
2296 NULL);
57b61080
SF
2297 if (err < 0)
2298 goto errout;
2299
2300 err = -EOPNOTSUPP;
2301 if (ops->ndo_set_vf_port)
2302 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
2303 if (err < 0)
2304 goto errout;
ba998906 2305 status |= DO_SETLINK_NOTIFY;
57b61080 2306 }
f8ff182c
TG
2307
2308 if (tb[IFLA_AF_SPEC]) {
2309 struct nlattr *af;
2310 int rem;
2311
2312 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2313 const struct rtnl_af_ops *af_ops;
2314
5fa85a09
FW
2315 rcu_read_lock();
2316
058c8d59 2317 BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
f8ff182c 2318
cf7afbfe 2319 err = af_ops->set_link_af(dev, af);
5fa85a09
FW
2320 if (err < 0) {
2321 rcu_read_unlock();
f8ff182c 2322 goto errout;
5fa85a09 2323 }
f8ff182c 2324
5fa85a09 2325 rcu_read_unlock();
ba998906 2326 status |= DO_SETLINK_NOTIFY;
f8ff182c
TG
2327 }
2328 }
57b61080
SF
2329 err = 0;
2330
88d6378b
AK
2331 if (tb[IFLA_PROTO_DOWN]) {
2332 err = dev_change_proto_down(dev,
2333 nla_get_u8(tb[IFLA_PROTO_DOWN]));
2334 if (err)
2335 goto errout;
2336 status |= DO_SETLINK_NOTIFY;
2337 }
2338
d1fdd913
BB
2339 if (tb[IFLA_XDP]) {
2340 struct nlattr *xdp[IFLA_XDP_MAX + 1];
85de8576 2341 u32 xdp_flags = 0;
d1fdd913
BB
2342
2343 err = nla_parse_nested(xdp, IFLA_XDP_MAX, tb[IFLA_XDP],
fceb6435 2344 ifla_xdp_policy, NULL);
d1fdd913
BB
2345 if (err < 0)
2346 goto errout;
2347
58038695 2348 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
262d8625
BB
2349 err = -EINVAL;
2350 goto errout;
2351 }
85de8576
DB
2352
2353 if (xdp[IFLA_XDP_FLAGS]) {
2354 xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
2355 if (xdp_flags & ~XDP_FLAGS_MASK) {
2356 err = -EINVAL;
2357 goto errout;
2358 }
ee5d032f 2359 if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
0489df9a
DB
2360 err = -EINVAL;
2361 goto errout;
2362 }
85de8576
DB
2363 }
2364
d1fdd913 2365 if (xdp[IFLA_XDP_FD]) {
ddf9f970 2366 err = dev_change_xdp_fd(dev, extack,
85de8576
DB
2367 nla_get_s32(xdp[IFLA_XDP_FD]),
2368 xdp_flags);
d1fdd913
BB
2369 if (err)
2370 goto errout;
2371 status |= DO_SETLINK_NOTIFY;
2372 }
2373 }
2374
0157f60c 2375errout:
ba998906 2376 if (status & DO_SETLINK_MODIFIED) {
64ff90cc 2377 if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
ba998906
ND
2378 netdev_state_change(dev);
2379
2380 if (err < 0)
2381 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",
2382 dev->name);
2383 }
da5e0494 2384
0157f60c
PM
2385 return err;
2386}
1da177e4 2387
c21ef3e3
DA
2388static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2389 struct netlink_ext_ack *extack)
0157f60c 2390{
3b1e0a65 2391 struct net *net = sock_net(skb->sk);
0157f60c
PM
2392 struct ifinfomsg *ifm;
2393 struct net_device *dev;
2394 int err;
2395 struct nlattr *tb[IFLA_MAX+1];
2396 char ifname[IFNAMSIZ];
2397
c21ef3e3
DA
2398 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy,
2399 extack);
0157f60c
PM
2400 if (err < 0)
2401 goto errout;
2402
79e1ad14
JB
2403 if (tb[IFLA_IF_NETNSID])
2404 return -EOPNOTSUPP;
2405
0157f60c
PM
2406 if (tb[IFLA_IFNAME])
2407 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2408 else
2409 ifname[0] = '\0';
2410
2411 err = -EINVAL;
2412 ifm = nlmsg_data(nlh);
2413 if (ifm->ifi_index > 0)
a3d12891 2414 dev = __dev_get_by_index(net, ifm->ifi_index);
0157f60c 2415 else if (tb[IFLA_IFNAME])
a3d12891 2416 dev = __dev_get_by_name(net, ifname);
0157f60c
PM
2417 else
2418 goto errout;
2419
2420 if (dev == NULL) {
2421 err = -ENODEV;
2422 goto errout;
2423 }
2424
e0d087af
ED
2425 err = validate_linkmsg(dev, tb);
2426 if (err < 0)
a3d12891 2427 goto errout;
0157f60c 2428
ddf9f970 2429 err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
da5e0494 2430errout:
1da177e4
LT
2431 return err;
2432}
2433
66400d54
WC
2434static int rtnl_group_dellink(const struct net *net, int group)
2435{
2436 struct net_device *dev, *aux;
2437 LIST_HEAD(list_kill);
2438 bool found = false;
2439
2440 if (!group)
2441 return -EPERM;
2442
2443 for_each_netdev(net, dev) {
2444 if (dev->group == group) {
2445 const struct rtnl_link_ops *ops;
2446
2447 found = true;
2448 ops = dev->rtnl_link_ops;
2449 if (!ops || !ops->dellink)
2450 return -EOPNOTSUPP;
2451 }
2452 }
2453
2454 if (!found)
2455 return -ENODEV;
2456
2457 for_each_netdev_safe(net, dev, aux) {
2458 if (dev->group == group) {
2459 const struct rtnl_link_ops *ops;
2460
2461 ops = dev->rtnl_link_ops;
2462 ops->dellink(dev, &list_kill);
2463 }
2464 }
2465 unregister_netdevice_many(&list_kill);
2466
2467 return 0;
2468}
2469
614732ea
TG
2470int rtnl_delete_link(struct net_device *dev)
2471{
2472 const struct rtnl_link_ops *ops;
2473 LIST_HEAD(list_kill);
2474
2475 ops = dev->rtnl_link_ops;
2476 if (!ops || !ops->dellink)
2477 return -EOPNOTSUPP;
2478
2479 ops->dellink(dev, &list_kill);
2480 unregister_netdevice_many(&list_kill);
2481
2482 return 0;
2483}
2484EXPORT_SYMBOL_GPL(rtnl_delete_link);
2485
c21ef3e3
DA
2486static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
2487 struct netlink_ext_ack *extack)
38f7b870 2488{
3b1e0a65 2489 struct net *net = sock_net(skb->sk);
38f7b870
PM
2490 struct net_device *dev;
2491 struct ifinfomsg *ifm;
2492 char ifname[IFNAMSIZ];
2493 struct nlattr *tb[IFLA_MAX+1];
2494 int err;
2495
c21ef3e3 2496 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
38f7b870
PM
2497 if (err < 0)
2498 return err;
2499
79e1ad14
JB
2500 if (tb[IFLA_IF_NETNSID])
2501 return -EOPNOTSUPP;
2502
38f7b870
PM
2503 if (tb[IFLA_IFNAME])
2504 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2505
2506 ifm = nlmsg_data(nlh);
2507 if (ifm->ifi_index > 0)
881d966b 2508 dev = __dev_get_by_index(net, ifm->ifi_index);
38f7b870 2509 else if (tb[IFLA_IFNAME])
881d966b 2510 dev = __dev_get_by_name(net, ifname);
66400d54
WC
2511 else if (tb[IFLA_GROUP])
2512 return rtnl_group_dellink(net, nla_get_u32(tb[IFLA_GROUP]));
38f7b870
PM
2513 else
2514 return -EINVAL;
2515
2516 if (!dev)
2517 return -ENODEV;
2518
614732ea 2519 return rtnl_delete_link(dev);
38f7b870
PM
2520}
2521
3729d502
PM
2522int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
2523{
2524 unsigned int old_flags;
2525 int err;
2526
2527 old_flags = dev->flags;
2528 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
2529 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
2530 if (err < 0)
2531 return err;
2532 }
2533
2534 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
3729d502 2535
a528c219 2536 __dev_notify_flags(dev, old_flags, ~0U);
3729d502
PM
2537 return 0;
2538}
2539EXPORT_SYMBOL(rtnl_configure_link);
2540
c0713563 2541struct net_device *rtnl_create_link(struct net *net,
78ebb0d0 2542 const char *ifname, unsigned char name_assign_type,
5517750f 2543 const struct rtnl_link_ops *ops, struct nlattr *tb[])
e7199288 2544{
e7199288 2545 struct net_device *dev;
d40156aa
JP
2546 unsigned int num_tx_queues = 1;
2547 unsigned int num_rx_queues = 1;
e7199288 2548
76ff5cc9
JP
2549 if (tb[IFLA_NUM_TX_QUEUES])
2550 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
2551 else if (ops->get_num_tx_queues)
d40156aa 2552 num_tx_queues = ops->get_num_tx_queues();
76ff5cc9
JP
2553
2554 if (tb[IFLA_NUM_RX_QUEUES])
2555 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
2556 else if (ops->get_num_rx_queues)
d40156aa 2557 num_rx_queues = ops->get_num_rx_queues();
efacb309 2558
5517750f 2559 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
c835a677 2560 ops->setup, num_tx_queues, num_rx_queues);
e7199288 2561 if (!dev)
d1892e4e 2562 return ERR_PTR(-ENOMEM);
e7199288 2563
81adee47
EB
2564 dev_net_set(dev, net);
2565 dev->rtnl_link_ops = ops;
3729d502 2566 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
81adee47 2567
e7199288
PE
2568 if (tb[IFLA_MTU])
2569 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
2afb9b53 2570 if (tb[IFLA_ADDRESS]) {
e7199288
PE
2571 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
2572 nla_len(tb[IFLA_ADDRESS]));
2afb9b53
JP
2573 dev->addr_assign_type = NET_ADDR_SET;
2574 }
e7199288
PE
2575 if (tb[IFLA_BROADCAST])
2576 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
2577 nla_len(tb[IFLA_BROADCAST]));
2578 if (tb[IFLA_TXQLEN])
2579 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
2580 if (tb[IFLA_OPERSTATE])
93b2d4a2 2581 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
e7199288
PE
2582 if (tb[IFLA_LINKMODE])
2583 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
ffa934f1
PM
2584 if (tb[IFLA_GROUP])
2585 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
e7199288
PE
2586
2587 return dev;
e7199288 2588}
e0d087af 2589EXPORT_SYMBOL(rtnl_create_link);
e7199288 2590
90f62cf3
EB
2591static int rtnl_group_changelink(const struct sk_buff *skb,
2592 struct net *net, int group,
e7ed828f 2593 struct ifinfomsg *ifm,
ddf9f970 2594 struct netlink_ext_ack *extack,
e7ed828f
VD
2595 struct nlattr **tb)
2596{
d079535d 2597 struct net_device *dev, *aux;
e7ed828f
VD
2598 int err;
2599
d079535d 2600 for_each_netdev_safe(net, dev, aux) {
e7ed828f 2601 if (dev->group == group) {
ddf9f970 2602 err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
e7ed828f
VD
2603 if (err < 0)
2604 return err;
2605 }
2606 }
2607
2608 return 0;
2609}
2610
c21ef3e3
DA
2611static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2612 struct netlink_ext_ack *extack)
38f7b870 2613{
3b1e0a65 2614 struct net *net = sock_net(skb->sk);
38f7b870 2615 const struct rtnl_link_ops *ops;
ba7d49b1 2616 const struct rtnl_link_ops *m_ops = NULL;
38f7b870 2617 struct net_device *dev;
ba7d49b1 2618 struct net_device *master_dev = NULL;
38f7b870
PM
2619 struct ifinfomsg *ifm;
2620 char kind[MODULE_NAME_LEN];
2621 char ifname[IFNAMSIZ];
2622 struct nlattr *tb[IFLA_MAX+1];
2623 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
5517750f 2624 unsigned char name_assign_type = NET_NAME_USER;
38f7b870
PM
2625 int err;
2626
95a5afca 2627#ifdef CONFIG_MODULES
38f7b870 2628replay:
8072f085 2629#endif
c21ef3e3 2630 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
38f7b870
PM
2631 if (err < 0)
2632 return err;
2633
79e1ad14
JB
2634 if (tb[IFLA_IF_NETNSID])
2635 return -EOPNOTSUPP;
2636
38f7b870
PM
2637 if (tb[IFLA_IFNAME])
2638 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2639 else
2640 ifname[0] = '\0';
2641
2642 ifm = nlmsg_data(nlh);
2643 if (ifm->ifi_index > 0)
881d966b 2644 dev = __dev_get_by_index(net, ifm->ifi_index);
e7ed828f
VD
2645 else {
2646 if (ifname[0])
2647 dev = __dev_get_by_name(net, ifname);
e7ed828f
VD
2648 else
2649 dev = NULL;
2650 }
38f7b870 2651
ba7d49b1
JP
2652 if (dev) {
2653 master_dev = netdev_master_upper_dev_get(dev);
2654 if (master_dev)
2655 m_ops = master_dev->rtnl_link_ops;
2656 }
2657
e0d087af
ED
2658 err = validate_linkmsg(dev, tb);
2659 if (err < 0)
1840bb13
TG
2660 return err;
2661
38f7b870
PM
2662 if (tb[IFLA_LINKINFO]) {
2663 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
fceb6435
JB
2664 tb[IFLA_LINKINFO], ifla_info_policy,
2665 NULL);
38f7b870
PM
2666 if (err < 0)
2667 return err;
2668 } else
2669 memset(linkinfo, 0, sizeof(linkinfo));
2670
2671 if (linkinfo[IFLA_INFO_KIND]) {
2672 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
2673 ops = rtnl_link_ops_get(kind);
2674 } else {
2675 kind[0] = '\0';
2676 ops = NULL;
2677 }
2678
2679 if (1) {
4e10fd5b
SL
2680 struct nlattr *attr[ops ? ops->maxtype + 1 : 1];
2681 struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1];
ba7d49b1
JP
2682 struct nlattr **data = NULL;
2683 struct nlattr **slave_data = NULL;
317f4810 2684 struct net *dest_net, *link_net = NULL;
38f7b870
PM
2685
2686 if (ops) {
2687 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
2688 err = nla_parse_nested(attr, ops->maxtype,
2689 linkinfo[IFLA_INFO_DATA],
fceb6435 2690 ops->policy, NULL);
38f7b870
PM
2691 if (err < 0)
2692 return err;
2693 data = attr;
2694 }
2695 if (ops->validate) {
a8b8a889 2696 err = ops->validate(tb, data, extack);
38f7b870
PM
2697 if (err < 0)
2698 return err;
2699 }
2700 }
2701
ba7d49b1
JP
2702 if (m_ops) {
2703 if (m_ops->slave_maxtype &&
2704 linkinfo[IFLA_INFO_SLAVE_DATA]) {
2705 err = nla_parse_nested(slave_attr,
2706 m_ops->slave_maxtype,
2707 linkinfo[IFLA_INFO_SLAVE_DATA],
fceb6435
JB
2708 m_ops->slave_policy,
2709 NULL);
ba7d49b1
JP
2710 if (err < 0)
2711 return err;
2712 slave_data = slave_attr;
2713 }
ba7d49b1
JP
2714 }
2715
38f7b870 2716 if (dev) {
90c325e3 2717 int status = 0;
38f7b870
PM
2718
2719 if (nlh->nlmsg_flags & NLM_F_EXCL)
2720 return -EEXIST;
2721 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2722 return -EOPNOTSUPP;
2723
2724 if (linkinfo[IFLA_INFO_DATA]) {
2725 if (!ops || ops != dev->rtnl_link_ops ||
2726 !ops->changelink)
2727 return -EOPNOTSUPP;
2728
ad744b22 2729 err = ops->changelink(dev, tb, data, extack);
38f7b870
PM
2730 if (err < 0)
2731 return err;
ba998906 2732 status |= DO_SETLINK_NOTIFY;
38f7b870
PM
2733 }
2734
ba7d49b1
JP
2735 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2736 if (!m_ops || !m_ops->slave_changelink)
2737 return -EOPNOTSUPP;
2738
2739 err = m_ops->slave_changelink(master_dev, dev,
17dd0ec4
MS
2740 tb, slave_data,
2741 extack);
ba7d49b1
JP
2742 if (err < 0)
2743 return err;
ba998906 2744 status |= DO_SETLINK_NOTIFY;
ba7d49b1
JP
2745 }
2746
ddf9f970
JK
2747 return do_setlink(skb, dev, ifm, extack, tb, ifname,
2748 status);
38f7b870
PM
2749 }
2750
ffa934f1
PM
2751 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2752 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
90f62cf3 2753 return rtnl_group_changelink(skb, net,
ffa934f1 2754 nla_get_u32(tb[IFLA_GROUP]),
ddf9f970 2755 ifm, extack, tb);
38f7b870 2756 return -ENODEV;
ffa934f1 2757 }
38f7b870 2758
160ca014 2759 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
38f7b870
PM
2760 return -EOPNOTSUPP;
2761
2762 if (!ops) {
95a5afca 2763#ifdef CONFIG_MODULES
38f7b870
PM
2764 if (kind[0]) {
2765 __rtnl_unlock();
2766 request_module("rtnl-link-%s", kind);
2767 rtnl_lock();
2768 ops = rtnl_link_ops_get(kind);
2769 if (ops)
2770 goto replay;
2771 }
2772#endif
2773 return -EOPNOTSUPP;
2774 }
2775
b0ab2fab
JP
2776 if (!ops->setup)
2777 return -EOPNOTSUPP;
2778
5517750f 2779 if (!ifname[0]) {
38f7b870 2780 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
5517750f
TG
2781 name_assign_type = NET_NAME_ENUM;
2782 }
e7199288 2783
81adee47 2784 dest_net = rtnl_link_get_net(net, tb);
13ad1774
EB
2785 if (IS_ERR(dest_net))
2786 return PTR_ERR(dest_net);
2787
505ce415
EB
2788 err = -EPERM;
2789 if (!netlink_ns_capable(skb, dest_net->user_ns, CAP_NET_ADMIN))
2790 goto out;
2791
317f4810
ND
2792 if (tb[IFLA_LINK_NETNSID]) {
2793 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
2794
2795 link_net = get_net_ns_by_id(dest_net, id);
2796 if (!link_net) {
2797 err = -EINVAL;
2798 goto out;
2799 }
06615bed
EB
2800 err = -EPERM;
2801 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
2802 goto out;
317f4810
ND
2803 }
2804
2805 dev = rtnl_create_link(link_net ? : dest_net, ifname,
2806 name_assign_type, ops, tb);
9c7dafbf 2807 if (IS_ERR(dev)) {
e7199288 2808 err = PTR_ERR(dev);
9c7dafbf
PE
2809 goto out;
2810 }
2811
2812 dev->ifindex = ifm->ifi_index;
2813
0e0eee24 2814 if (ops->newlink) {
7a3f4a18
MS
2815 err = ops->newlink(link_net ? : net, dev, tb, data,
2816 extack);
0e0eee24 2817 /* Drivers should call free_netdev() in ->destructor
e51fb152
CW
2818 * and unregister it on failure after registration
2819 * so that device could be finally freed in rtnl_unlock.
0e0eee24 2820 */
e51fb152
CW
2821 if (err < 0) {
2822 /* If device is not registered at all, free it now */
2823 if (dev->reg_state == NETREG_UNINITIALIZED)
2824 free_netdev(dev);
0e0eee24 2825 goto out;
e51fb152 2826 }
0e0eee24 2827 } else {
2d85cba2 2828 err = register_netdevice(dev);
0e0eee24
CW
2829 if (err < 0) {
2830 free_netdev(dev);
2831 goto out;
2832 }
fce9b9be 2833 }
3729d502 2834 err = rtnl_configure_link(dev, ifm);
43638900
DM
2835 if (err < 0)
2836 goto out_unregister;
bdef279b 2837 if (link_net) {
317f4810 2838 err = dev_change_net_namespace(dev, dest_net, ifname);
bdef279b 2839 if (err < 0)
43638900 2840 goto out_unregister;
bdef279b 2841 }
160ca014 2842 if (tb[IFLA_MASTER]) {
33eaf2a6
DA
2843 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]),
2844 extack);
160ca014
TV
2845 if (err)
2846 goto out_unregister;
2847 }
3729d502 2848out:
317f4810
ND
2849 if (link_net)
2850 put_net(link_net);
81adee47 2851 put_net(dest_net);
38f7b870 2852 return err;
43638900
DM
2853out_unregister:
2854 if (ops->newlink) {
2855 LIST_HEAD(list_kill);
2856
2857 ops->dellink(dev, &list_kill);
2858 unregister_netdevice_many(&list_kill);
2859 } else {
2860 unregister_netdevice(dev);
2861 }
2862 goto out;
38f7b870
PM
2863 }
2864}
2865
c21ef3e3
DA
2866static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2867 struct netlink_ext_ack *extack)
711e2c33 2868{
3b1e0a65 2869 struct net *net = sock_net(skb->sk);
79e1ad14 2870 struct net *tgt_net = net;
b60c5115 2871 struct ifinfomsg *ifm;
a3d12891 2872 char ifname[IFNAMSIZ];
b60c5115
TG
2873 struct nlattr *tb[IFLA_MAX+1];
2874 struct net_device *dev = NULL;
2875 struct sk_buff *nskb;
79e1ad14 2876 int netnsid = -1;
339bf98f 2877 int err;
115c9b81 2878 u32 ext_filter_mask = 0;
711e2c33 2879
c21ef3e3 2880 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
b60c5115 2881 if (err < 0)
9918f230 2882 return err;
b60c5115 2883
79e1ad14
JB
2884 if (tb[IFLA_IF_NETNSID]) {
2885 netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]);
f428fe4a 2886 tgt_net = get_target_net(NETLINK_CB(skb).sk, netnsid);
79e1ad14
JB
2887 if (IS_ERR(tgt_net))
2888 return PTR_ERR(tgt_net);
2889 }
2890
a3d12891
ED
2891 if (tb[IFLA_IFNAME])
2892 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2893
115c9b81
GR
2894 if (tb[IFLA_EXT_MASK])
2895 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2896
79e1ad14 2897 err = -EINVAL;
b60c5115 2898 ifm = nlmsg_data(nlh);
a3d12891 2899 if (ifm->ifi_index > 0)
79e1ad14 2900 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
a3d12891 2901 else if (tb[IFLA_IFNAME])
79e1ad14 2902 dev = __dev_get_by_name(tgt_net, ifname);
a3d12891 2903 else
79e1ad14 2904 goto out;
711e2c33 2905
79e1ad14 2906 err = -ENODEV;
a3d12891 2907 if (dev == NULL)
79e1ad14 2908 goto out;
a3d12891 2909
79e1ad14 2910 err = -ENOBUFS;
115c9b81 2911 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
a3d12891 2912 if (nskb == NULL)
79e1ad14 2913 goto out;
b60c5115 2914
79e1ad14
JB
2915 err = rtnl_fill_ifinfo(nskb, dev, net,
2916 RTM_NEWLINK, NETLINK_CB(skb).portid,
2917 nlh->nlmsg_seq, 0, 0, ext_filter_mask,
2918 0, NULL, netnsid);
26932566
PM
2919 if (err < 0) {
2920 /* -EMSGSIZE implies BUG in if_nlmsg_size */
2921 WARN_ON(err == -EMSGSIZE);
2922 kfree_skb(nskb);
a3d12891 2923 } else
15e47304 2924 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
79e1ad14
JB
2925out:
2926 if (netnsid >= 0)
2927 put_net(tgt_net);
711e2c33 2928
b60c5115 2929 return err;
711e2c33 2930}
711e2c33 2931
115c9b81 2932static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
c7ac8679 2933{
115c9b81
GR
2934 struct net *net = sock_net(skb->sk);
2935 struct net_device *dev;
2936 struct nlattr *tb[IFLA_MAX+1];
2937 u32 ext_filter_mask = 0;
2938 u16 min_ifinfo_dump_size = 0;
e5eca6d4
MS
2939 int hdrlen;
2940
2941 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2942 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2943 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
115c9b81 2944
fceb6435 2945 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
a4b64fbe
ED
2946 if (tb[IFLA_EXT_MASK])
2947 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2948 }
115c9b81
GR
2949
2950 if (!ext_filter_mask)
2951 return NLMSG_GOODSIZE;
2952 /*
2953 * traverse the list of net devices and compute the minimum
2954 * buffer size based upon the filter mask.
2955 */
6853dd48
FW
2956 rcu_read_lock();
2957 for_each_netdev_rcu(net, dev) {
115c9b81
GR
2958 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
2959 if_nlmsg_size(dev,
2960 ext_filter_mask));
2961 }
6853dd48 2962 rcu_read_unlock();
115c9b81 2963
93af2056 2964 return nlmsg_total_size(min_ifinfo_dump_size);
c7ac8679
GR
2965}
2966
42bad1da 2967static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
1da177e4
LT
2968{
2969 int idx;
2970 int s_idx = cb->family;
2971
2972 if (s_idx == 0)
2973 s_idx = 1;
6853dd48 2974
25239cee 2975 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
1da177e4 2976 int type = cb->nlh->nlmsg_type-RTM_BASE;
6853dd48
FW
2977 struct rtnl_link *handlers;
2978 rtnl_dumpit_func dumpit;
2979
1da177e4
LT
2980 if (idx < s_idx || idx == PF_PACKET)
2981 continue;
6853dd48
FW
2982
2983 handlers = rtnl_dereference(rtnl_msg_handlers[idx]);
2984 if (!handlers)
1da177e4 2985 continue;
6853dd48
FW
2986
2987 dumpit = READ_ONCE(handlers[type].dumpit);
2988 if (!dumpit)
2989 continue;
2990
0465277f 2991 if (idx > s_idx) {
1da177e4 2992 memset(&cb->args[0], 0, sizeof(cb->args));
0465277f
ND
2993 cb->prev_seq = 0;
2994 cb->seq = 0;
2995 }
6853dd48 2996 if (dumpit(skb, cb))
1da177e4
LT
2997 break;
2998 }
2999 cb->family = idx;
3000
3001 return skb->len;
3002}
3003
395eea6c 3004struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
3d3ea5af 3005 unsigned int change,
6621dd29 3006 u32 event, gfp_t flags, int *new_nsid)
1da177e4 3007{
c346dca1 3008 struct net *net = dev_net(dev);
1da177e4 3009 struct sk_buff *skb;
0ec6d3f4 3010 int err = -ENOBUFS;
c7ac8679 3011 size_t if_info_size;
1da177e4 3012
7f294054 3013 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
0ec6d3f4
TG
3014 if (skb == NULL)
3015 goto errout;
1da177e4 3016
79e1ad14
JB
3017 err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
3018 type, 0, 0, change, 0, 0, event,
3019 new_nsid, -1);
26932566
PM
3020 if (err < 0) {
3021 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
3022 WARN_ON(err == -EMSGSIZE);
3023 kfree_skb(skb);
3024 goto errout;
3025 }
395eea6c 3026 return skb;
0ec6d3f4
TG
3027errout:
3028 if (err < 0)
4b3da706 3029 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
395eea6c
MB
3030 return NULL;
3031}
3032
3033void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
3034{
3035 struct net *net = dev_net(dev);
3036
3037 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
3038}
3039
3d3ea5af
VY
3040static void rtmsg_ifinfo_event(int type, struct net_device *dev,
3041 unsigned int change, u32 event,
6621dd29 3042 gfp_t flags, int *new_nsid)
395eea6c
MB
3043{
3044 struct sk_buff *skb;
3045
ed2a80ab
ND
3046 if (dev->reg_state != NETREG_REGISTERED)
3047 return;
3048
6621dd29 3049 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid);
395eea6c
MB
3050 if (skb)
3051 rtmsg_ifinfo_send(skb, dev, flags);
1da177e4 3052}
3d3ea5af
VY
3053
3054void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
3055 gfp_t flags)
3056{
6621dd29 3057 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, NULL);
3d3ea5af 3058}
1da177e4 3059
6621dd29
ND
3060void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
3061 gfp_t flags, int *new_nsid)
3062{
3063 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3064 new_nsid);
3065}
3066
d83b0603
JF
3067static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
3068 struct net_device *dev,
1e53d5bb 3069 u8 *addr, u16 vid, u32 pid, u32 seq,
1c104a6b 3070 int type, unsigned int flags,
b3379041 3071 int nlflags, u16 ndm_state)
d83b0603
JF
3072{
3073 struct nlmsghdr *nlh;
3074 struct ndmsg *ndm;
3075
1c104a6b 3076 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
d83b0603
JF
3077 if (!nlh)
3078 return -EMSGSIZE;
3079
3080 ndm = nlmsg_data(nlh);
3081 ndm->ndm_family = AF_BRIDGE;
3082 ndm->ndm_pad1 = 0;
3083 ndm->ndm_pad2 = 0;
3084 ndm->ndm_flags = flags;
3085 ndm->ndm_type = 0;
3086 ndm->ndm_ifindex = dev->ifindex;
b3379041 3087 ndm->ndm_state = ndm_state;
d83b0603
JF
3088
3089 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
3090 goto nla_put_failure;
1e53d5bb
HS
3091 if (vid)
3092 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
3093 goto nla_put_failure;
d83b0603 3094
053c095a
JB
3095 nlmsg_end(skb, nlh);
3096 return 0;
d83b0603
JF
3097
3098nla_put_failure:
3099 nlmsg_cancel(skb, nlh);
3100 return -EMSGSIZE;
3101}
3102
3ff661c3
JF
3103static inline size_t rtnl_fdb_nlmsg_size(void)
3104{
f82ef3e1
SD
3105 return NLMSG_ALIGN(sizeof(struct ndmsg)) +
3106 nla_total_size(ETH_ALEN) + /* NDA_LLADDR */
3107 nla_total_size(sizeof(u16)) + /* NDA_VLAN */
3108 0;
3ff661c3
JF
3109}
3110
b3379041
HS
3111static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
3112 u16 ndm_state)
3ff661c3
JF
3113{
3114 struct net *net = dev_net(dev);
3115 struct sk_buff *skb;
3116 int err = -ENOBUFS;
3117
3118 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
3119 if (!skb)
3120 goto errout;
3121
1e53d5bb 3122 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
b3379041 3123 0, 0, type, NTF_SELF, 0, ndm_state);
3ff661c3
JF
3124 if (err < 0) {
3125 kfree_skb(skb);
3126 goto errout;
3127 }
3128
3129 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
3130 return;
3131errout:
3132 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
3133}
3134
090096bf
VY
3135/**
3136 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3137 */
3138int ndo_dflt_fdb_add(struct ndmsg *ndm,
3139 struct nlattr *tb[],
3140 struct net_device *dev,
f6f6424b 3141 const unsigned char *addr, u16 vid,
090096bf
VY
3142 u16 flags)
3143{
3144 int err = -EINVAL;
3145
3146 /* If aging addresses are supported device will need to
3147 * implement its own handler for this.
3148 */
3149 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
3150 pr_info("%s: FDB only supports static addresses\n", dev->name);
3151 return err;
3152 }
3153
65891fea
OG
3154 if (vid) {
3155 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
3156 return err;
3157 }
3158
090096bf
VY
3159 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3160 err = dev_uc_add_excl(dev, addr);
3161 else if (is_multicast_ether_addr(addr))
3162 err = dev_mc_add_excl(dev, addr);
3163
3164 /* Only return duplicate errors if NLM_F_EXCL is set */
3165 if (err == -EEXIST && !(flags & NLM_F_EXCL))
3166 err = 0;
3167
3168 return err;
3169}
3170EXPORT_SYMBOL(ndo_dflt_fdb_add);
3171
b88d12e4
FW
3172static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
3173 struct netlink_ext_ack *extack)
f6f6424b
JP
3174{
3175 u16 vid = 0;
3176
3177 if (vlan_attr) {
3178 if (nla_len(vlan_attr) != sizeof(u16)) {
b88d12e4 3179 NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
f6f6424b
JP
3180 return -EINVAL;
3181 }
3182
3183 vid = nla_get_u16(vlan_attr);
3184
3185 if (!vid || vid >= VLAN_VID_MASK) {
b88d12e4 3186 NL_SET_ERR_MSG(extack, "invalid vlan id");
f6f6424b
JP
3187 return -EINVAL;
3188 }
3189 }
3190 *p_vid = vid;
3191 return 0;
3192}
3193
c21ef3e3
DA
3194static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
3195 struct netlink_ext_ack *extack)
77162022
JF
3196{
3197 struct net *net = sock_net(skb->sk);
77162022
JF
3198 struct ndmsg *ndm;
3199 struct nlattr *tb[NDA_MAX+1];
3200 struct net_device *dev;
3201 u8 *addr;
f6f6424b 3202 u16 vid;
77162022
JF
3203 int err;
3204
c21ef3e3 3205 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
77162022
JF
3206 if (err < 0)
3207 return err;
3208
3209 ndm = nlmsg_data(nlh);
3210 if (ndm->ndm_ifindex == 0) {
b88d12e4 3211 NL_SET_ERR_MSG(extack, "invalid ifindex");
77162022
JF
3212 return -EINVAL;
3213 }
3214
3215 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3216 if (dev == NULL) {
b88d12e4 3217 NL_SET_ERR_MSG(extack, "unknown ifindex");
77162022
JF
3218 return -ENODEV;
3219 }
3220
3221 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
b88d12e4 3222 NL_SET_ERR_MSG(extack, "invalid address");
77162022
JF
3223 return -EINVAL;
3224 }
3225
3226 addr = nla_data(tb[NDA_LLADDR]);
77162022 3227
b88d12e4 3228 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
f6f6424b
JP
3229 if (err)
3230 return err;
3231
77162022
JF
3232 err = -EOPNOTSUPP;
3233
3234 /* Support fdb on master device the net/bridge default case */
3235 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3236 (dev->priv_flags & IFF_BRIDGE_PORT)) {
898e5061
JP
3237 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3238 const struct net_device_ops *ops = br_dev->netdev_ops;
3239
f6f6424b
JP
3240 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
3241 nlh->nlmsg_flags);
77162022
JF
3242 if (err)
3243 goto out;
3244 else
3245 ndm->ndm_flags &= ~NTF_MASTER;
3246 }
3247
3248 /* Embedded bridge, macvlan, and any other device support */
090096bf
VY
3249 if ((ndm->ndm_flags & NTF_SELF)) {
3250 if (dev->netdev_ops->ndo_fdb_add)
3251 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
f6f6424b 3252 vid,
090096bf
VY
3253 nlh->nlmsg_flags);
3254 else
f6f6424b 3255 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
090096bf 3256 nlh->nlmsg_flags);
77162022 3257
3ff661c3 3258 if (!err) {
b3379041
HS
3259 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
3260 ndm->ndm_state);
77162022 3261 ndm->ndm_flags &= ~NTF_SELF;
3ff661c3 3262 }
77162022
JF
3263 }
3264out:
3265 return err;
3266}
3267
090096bf
VY
3268/**
3269 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3270 */
3271int ndo_dflt_fdb_del(struct ndmsg *ndm,
3272 struct nlattr *tb[],
3273 struct net_device *dev,
f6f6424b 3274 const unsigned char *addr, u16 vid)
090096bf 3275{
c8a89c4a 3276 int err = -EINVAL;
090096bf
VY
3277
3278 /* If aging addresses are supported device will need to
3279 * implement its own handler for this.
3280 */
64535993 3281 if (!(ndm->ndm_state & NUD_PERMANENT)) {
090096bf 3282 pr_info("%s: FDB only supports static addresses\n", dev->name);
c8a89c4a 3283 return err;
090096bf
VY
3284 }
3285
3286 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3287 err = dev_uc_del(dev, addr);
3288 else if (is_multicast_ether_addr(addr))
3289 err = dev_mc_del(dev, addr);
090096bf
VY
3290
3291 return err;
3292}
3293EXPORT_SYMBOL(ndo_dflt_fdb_del);
3294
c21ef3e3
DA
3295static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
3296 struct netlink_ext_ack *extack)
77162022
JF
3297{
3298 struct net *net = sock_net(skb->sk);
3299 struct ndmsg *ndm;
1690be63 3300 struct nlattr *tb[NDA_MAX+1];
77162022
JF
3301 struct net_device *dev;
3302 int err = -EINVAL;
3303 __u8 *addr;
f6f6424b 3304 u16 vid;
77162022 3305
90f62cf3 3306 if (!netlink_capable(skb, CAP_NET_ADMIN))
1690be63
VY
3307 return -EPERM;
3308
c21ef3e3 3309 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
1690be63
VY
3310 if (err < 0)
3311 return err;
77162022
JF
3312
3313 ndm = nlmsg_data(nlh);
3314 if (ndm->ndm_ifindex == 0) {
b88d12e4 3315 NL_SET_ERR_MSG(extack, "invalid ifindex");
77162022
JF
3316 return -EINVAL;
3317 }
3318
3319 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3320 if (dev == NULL) {
b88d12e4 3321 NL_SET_ERR_MSG(extack, "unknown ifindex");
77162022
JF
3322 return -ENODEV;
3323 }
3324
1690be63 3325 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
b88d12e4 3326 NL_SET_ERR_MSG(extack, "invalid address");
1690be63
VY
3327 return -EINVAL;
3328 }
3329
3330 addr = nla_data(tb[NDA_LLADDR]);
77162022 3331
b88d12e4 3332 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
f6f6424b
JP
3333 if (err)
3334 return err;
3335
77162022
JF
3336 err = -EOPNOTSUPP;
3337
3338 /* Support fdb on master device the net/bridge default case */
3339 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3340 (dev->priv_flags & IFF_BRIDGE_PORT)) {
898e5061
JP
3341 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3342 const struct net_device_ops *ops = br_dev->netdev_ops;
77162022 3343
898e5061 3344 if (ops->ndo_fdb_del)
f6f6424b 3345 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
77162022
JF
3346
3347 if (err)
3348 goto out;
3349 else
3350 ndm->ndm_flags &= ~NTF_MASTER;
3351 }
3352
3353 /* Embedded bridge, macvlan, and any other device support */
090096bf
VY
3354 if (ndm->ndm_flags & NTF_SELF) {
3355 if (dev->netdev_ops->ndo_fdb_del)
f6f6424b
JP
3356 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
3357 vid);
090096bf 3358 else
f6f6424b 3359 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
77162022 3360
3ff661c3 3361 if (!err) {
b3379041
HS
3362 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
3363 ndm->ndm_state);
77162022 3364 ndm->ndm_flags &= ~NTF_SELF;
3ff661c3 3365 }
77162022
JF
3366 }
3367out:
3368 return err;
3369}
3370
d83b0603
JF
3371static int nlmsg_populate_fdb(struct sk_buff *skb,
3372 struct netlink_callback *cb,
3373 struct net_device *dev,
3374 int *idx,
3375 struct netdev_hw_addr_list *list)
3376{
3377 struct netdev_hw_addr *ha;
3378 int err;
15e47304 3379 u32 portid, seq;
d83b0603 3380
15e47304 3381 portid = NETLINK_CB(cb->skb).portid;
d83b0603
JF
3382 seq = cb->nlh->nlmsg_seq;
3383
3384 list_for_each_entry(ha, &list->list, list) {
d297653d 3385 if (*idx < cb->args[2])
d83b0603
JF
3386 goto skip;
3387
1e53d5bb 3388 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
a7a558fe 3389 portid, seq,
1c104a6b 3390 RTM_NEWNEIGH, NTF_SELF,
b3379041 3391 NLM_F_MULTI, NUD_PERMANENT);
d83b0603
JF
3392 if (err < 0)
3393 return err;
3394skip:
3395 *idx += 1;
3396 }
3397 return 0;
3398}
3399
3400/**
2c53040f 3401 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
d83b0603
JF
3402 * @nlh: netlink message header
3403 * @dev: netdevice
3404 *
3405 * Default netdevice operation to dump the existing unicast address list.
91f3e7b1 3406 * Returns number of addresses from list put in skb.
d83b0603
JF
3407 */
3408int ndo_dflt_fdb_dump(struct sk_buff *skb,
3409 struct netlink_callback *cb,
3410 struct net_device *dev,
5d5eacb3 3411 struct net_device *filter_dev,
d297653d 3412 int *idx)
d83b0603
JF
3413{
3414 int err;
3415
3416 netif_addr_lock_bh(dev);
d297653d 3417 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
d83b0603
JF
3418 if (err)
3419 goto out;
2934c9db 3420 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
d83b0603
JF
3421out:
3422 netif_addr_unlock_bh(dev);
d297653d 3423 return err;
d83b0603
JF
3424}
3425EXPORT_SYMBOL(ndo_dflt_fdb_dump);
3426
77162022
JF
3427static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
3428{
77162022 3429 struct net_device *dev;
5e6d2435 3430 struct nlattr *tb[IFLA_MAX+1];
5e6d2435
JHS
3431 struct net_device *br_dev = NULL;
3432 const struct net_device_ops *ops = NULL;
3433 const struct net_device_ops *cops = NULL;
3434 struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
3435 struct net *net = sock_net(skb->sk);
d297653d 3436 struct hlist_head *head;
5e6d2435
JHS
3437 int brport_idx = 0;
3438 int br_idx = 0;
d297653d
RP
3439 int h, s_h;
3440 int idx = 0, s_idx;
3441 int err = 0;
3442 int fidx = 0;
5e6d2435 3443
0ff50e83
AP
3444 err = nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb,
3445 IFLA_MAX, ifla_policy, NULL);
3446 if (err < 0) {
3447 return -EINVAL;
3448 } else if (err == 0) {
5e6d2435
JHS
3449 if (tb[IFLA_MASTER])
3450 br_idx = nla_get_u32(tb[IFLA_MASTER]);
3451 }
3452
3453 brport_idx = ifm->ifi_index;
3454
3455 if (br_idx) {
3456 br_dev = __dev_get_by_index(net, br_idx);
3457 if (!br_dev)
3458 return -ENODEV;
3459
3460 ops = br_dev->netdev_ops;
5e6d2435
JHS
3461 }
3462
d297653d
RP
3463 s_h = cb->args[0];
3464 s_idx = cb->args[1];
5e6d2435 3465
d297653d
RP
3466 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
3467 idx = 0;
3468 head = &net->dev_index_head[h];
3469 hlist_for_each_entry(dev, head, index_hlist) {
5e6d2435 3470
d297653d 3471 if (brport_idx && (dev->ifindex != brport_idx))
5e6d2435
JHS
3472 continue;
3473
d297653d
RP
3474 if (!br_idx) { /* user did not specify a specific bridge */
3475 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3476 br_dev = netdev_master_upper_dev_get(dev);
3477 cops = br_dev->netdev_ops;
3478 }
3479 } else {
3480 if (dev != br_dev &&
3481 !(dev->priv_flags & IFF_BRIDGE_PORT))
3482 continue;
5e6d2435 3483
d297653d
RP
3484 if (br_dev != netdev_master_upper_dev_get(dev) &&
3485 !(dev->priv_flags & IFF_EBRIDGE))
3486 continue;
3487 cops = ops;
3488 }
77162022 3489
d297653d
RP
3490 if (idx < s_idx)
3491 goto cont;
77162022 3492
d297653d
RP
3493 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3494 if (cops && cops->ndo_fdb_dump) {
3495 err = cops->ndo_fdb_dump(skb, cb,
3496 br_dev, dev,
3497 &fidx);
3498 if (err == -EMSGSIZE)
3499 goto out;
3500 }
3501 }
5e6d2435 3502
d297653d
RP
3503 if (dev->netdev_ops->ndo_fdb_dump)
3504 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
3505 dev, NULL,
3506 &fidx);
3507 else
3508 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
3509 &fidx);
3510 if (err == -EMSGSIZE)
3511 goto out;
3512
3513 cops = NULL;
3514
3515 /* reset fdb offset to 0 for rest of the interfaces */
3516 cb->args[2] = 0;
3517 fidx = 0;
3518cont:
3519 idx++;
3520 }
77162022 3521 }
77162022 3522
d297653d
RP
3523out:
3524 cb->args[0] = h;
3525 cb->args[1] = idx;
3526 cb->args[2] = fidx;
3527
77162022
JF
3528 return skb->len;
3529}
3530
2c3c031c
SF
3531static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
3532 unsigned int attrnum, unsigned int flag)
3533{
3534 if (mask & flag)
3535 return nla_put_u8(skb, attrnum, !!(flags & flag));
3536 return 0;
3537}
3538
815cccbf 3539int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
2c3c031c 3540 struct net_device *dev, u16 mode,
7d4f8d87
SF
3541 u32 flags, u32 mask, int nlflags,
3542 u32 filter_mask,
3543 int (*vlan_fill)(struct sk_buff *skb,
3544 struct net_device *dev,
3545 u32 filter_mask))
815cccbf
JF
3546{
3547 struct nlmsghdr *nlh;
3548 struct ifinfomsg *ifm;
3549 struct nlattr *br_afspec;
2c3c031c 3550 struct nlattr *protinfo;
815cccbf 3551 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
898e5061 3552 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
7d4f8d87 3553 int err = 0;
815cccbf 3554
46c264da 3555 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
815cccbf
JF
3556 if (nlh == NULL)
3557 return -EMSGSIZE;
3558
3559 ifm = nlmsg_data(nlh);
3560 ifm->ifi_family = AF_BRIDGE;
3561 ifm->__ifi_pad = 0;
3562 ifm->ifi_type = dev->type;
3563 ifm->ifi_index = dev->ifindex;
3564 ifm->ifi_flags = dev_get_flags(dev);
3565 ifm->ifi_change = 0;
3566
3567
3568 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
3569 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
3570 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
898e5061
JP
3571 (br_dev &&
3572 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
815cccbf
JF
3573 (dev->addr_len &&
3574 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
a54acb3a
ND
3575 (dev->ifindex != dev_get_iflink(dev) &&
3576 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
815cccbf
JF
3577 goto nla_put_failure;
3578
3579 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
3580 if (!br_afspec)
3581 goto nla_put_failure;
3582
1d460b98 3583 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
815cccbf
JF
3584 nla_nest_cancel(skb, br_afspec);
3585 goto nla_put_failure;
3586 }
1d460b98
RP
3587
3588 if (mode != BRIDGE_MODE_UNDEF) {
3589 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
3590 nla_nest_cancel(skb, br_afspec);
3591 goto nla_put_failure;
3592 }
3593 }
7d4f8d87
SF
3594 if (vlan_fill) {
3595 err = vlan_fill(skb, dev, filter_mask);
3596 if (err) {
3597 nla_nest_cancel(skb, br_afspec);
3598 goto nla_put_failure;
3599 }
3600 }
815cccbf
JF
3601 nla_nest_end(skb, br_afspec);
3602
2c3c031c
SF
3603 protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
3604 if (!protinfo)
3605 goto nla_put_failure;
3606
3607 if (brport_nla_put_flag(skb, flags, mask,
3608 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
3609 brport_nla_put_flag(skb, flags, mask,
3610 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
3611 brport_nla_put_flag(skb, flags, mask,
3612 IFLA_BRPORT_FAST_LEAVE,
3613 BR_MULTICAST_FAST_LEAVE) ||
3614 brport_nla_put_flag(skb, flags, mask,
3615 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
3616 brport_nla_put_flag(skb, flags, mask,
3617 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
3618 brport_nla_put_flag(skb, flags, mask,
3619 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
3620 brport_nla_put_flag(skb, flags, mask,
3621 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
3622 brport_nla_put_flag(skb, flags, mask,
3623 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
3624 nla_nest_cancel(skb, protinfo);
3625 goto nla_put_failure;
3626 }
3627
3628 nla_nest_end(skb, protinfo);
3629
053c095a
JB
3630 nlmsg_end(skb, nlh);
3631 return 0;
815cccbf
JF
3632nla_put_failure:
3633 nlmsg_cancel(skb, nlh);
7d4f8d87 3634 return err ? err : -EMSGSIZE;
815cccbf 3635}
7d4f8d87 3636EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
815cccbf 3637
e5a55a89
JF
3638static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
3639{
3640 struct net *net = sock_net(skb->sk);
3641 struct net_device *dev;
3642 int idx = 0;
3643 u32 portid = NETLINK_CB(cb->skb).portid;
3644 u32 seq = cb->nlh->nlmsg_seq;
6cbdceeb 3645 u32 filter_mask = 0;
d64f69b0 3646 int err;
6cbdceeb 3647
aa68c20f
TG
3648 if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
3649 struct nlattr *extfilt;
3650
3651 extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
3652 IFLA_EXT_MASK);
3653 if (extfilt) {
3654 if (nla_len(extfilt) < sizeof(filter_mask))
3655 return -EINVAL;
3656
3657 filter_mask = nla_get_u32(extfilt);
3658 }
3659 }
e5a55a89
JF
3660
3661 rcu_read_lock();
3662 for_each_netdev_rcu(net, dev) {
3663 const struct net_device_ops *ops = dev->netdev_ops;
898e5061 3664 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
e5a55a89 3665
898e5061 3666 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
d64f69b0
RP
3667 if (idx >= cb->args[0]) {
3668 err = br_dev->netdev_ops->ndo_bridge_getlink(
3669 skb, portid, seq, dev,
3670 filter_mask, NLM_F_MULTI);
f6c5775f
DA
3671 if (err < 0 && err != -EOPNOTSUPP) {
3672 if (likely(skb->len))
3673 break;
3674
3675 goto out_err;
3676 }
d64f69b0 3677 }
25b1e679 3678 idx++;
e5a55a89
JF
3679 }
3680
3681 if (ops->ndo_bridge_getlink) {
d64f69b0
RP
3682 if (idx >= cb->args[0]) {
3683 err = ops->ndo_bridge_getlink(skb, portid,
3684 seq, dev,
3685 filter_mask,
3686 NLM_F_MULTI);
f6c5775f
DA
3687 if (err < 0 && err != -EOPNOTSUPP) {
3688 if (likely(skb->len))
3689 break;
3690
3691 goto out_err;
3692 }
d64f69b0 3693 }
25b1e679 3694 idx++;
e5a55a89
JF
3695 }
3696 }
f6c5775f
DA
3697 err = skb->len;
3698out_err:
e5a55a89
JF
3699 rcu_read_unlock();
3700 cb->args[0] = idx;
3701
f6c5775f 3702 return err;
e5a55a89
JF
3703}
3704
2469ffd7
JF
3705static inline size_t bridge_nlmsg_size(void)
3706{
3707 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3708 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3709 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3710 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
3711 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
3712 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
3713 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
3714 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
3715 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
3716 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
3717 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
3718}
3719
02dba438 3720static int rtnl_bridge_notify(struct net_device *dev)
2469ffd7
JF
3721{
3722 struct net *net = dev_net(dev);
2469ffd7
JF
3723 struct sk_buff *skb;
3724 int err = -EOPNOTSUPP;
3725
02dba438
RP
3726 if (!dev->netdev_ops->ndo_bridge_getlink)
3727 return 0;
3728
2469ffd7
JF
3729 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
3730 if (!skb) {
3731 err = -ENOMEM;
3732 goto errout;
3733 }
3734
46c264da 3735 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
02dba438
RP
3736 if (err < 0)
3737 goto errout;
2469ffd7 3738
59ccaaaa
RP
3739 if (!skb->len)
3740 goto errout;
3741
2469ffd7
JF
3742 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
3743 return 0;
3744errout:
3745 WARN_ON(err == -EMSGSIZE);
3746 kfree_skb(skb);
59ccaaaa
RP
3747 if (err)
3748 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
2469ffd7
JF
3749 return err;
3750}
3751
c21ef3e3
DA
3752static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3753 struct netlink_ext_ack *extack)
e5a55a89
JF
3754{
3755 struct net *net = sock_net(skb->sk);
3756 struct ifinfomsg *ifm;
3757 struct net_device *dev;
2469ffd7
JF
3758 struct nlattr *br_spec, *attr = NULL;
3759 int rem, err = -EOPNOTSUPP;
4de8b413 3760 u16 flags = 0;
c38e01b8 3761 bool have_flags = false;
e5a55a89
JF
3762
3763 if (nlmsg_len(nlh) < sizeof(*ifm))
3764 return -EINVAL;
3765
3766 ifm = nlmsg_data(nlh);
3767 if (ifm->ifi_family != AF_BRIDGE)
3768 return -EPFNOSUPPORT;
3769
3770 dev = __dev_get_by_index(net, ifm->ifi_index);
3771 if (!dev) {
b88d12e4 3772 NL_SET_ERR_MSG(extack, "unknown ifindex");
e5a55a89
JF
3773 return -ENODEV;
3774 }
3775
2469ffd7
JF
3776 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3777 if (br_spec) {
3778 nla_for_each_nested(attr, br_spec, rem) {
3779 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
6e8d1c55
TG
3780 if (nla_len(attr) < sizeof(flags))
3781 return -EINVAL;
3782
c38e01b8 3783 have_flags = true;
2469ffd7
JF
3784 flags = nla_get_u16(attr);
3785 break;
3786 }
3787 }
3788 }
3789
3790 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
898e5061
JP
3791 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3792
3793 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
2469ffd7
JF
3794 err = -EOPNOTSUPP;
3795 goto out;
3796 }
3797
add511b3 3798 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
e5a55a89
JF
3799 if (err)
3800 goto out;
2469ffd7
JF
3801
3802 flags &= ~BRIDGE_FLAGS_MASTER;
e5a55a89
JF
3803 }
3804
2469ffd7
JF
3805 if ((flags & BRIDGE_FLAGS_SELF)) {
3806 if (!dev->netdev_ops->ndo_bridge_setlink)
3807 err = -EOPNOTSUPP;
3808 else
add511b3
RP
3809 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
3810 flags);
02dba438 3811 if (!err) {
2469ffd7 3812 flags &= ~BRIDGE_FLAGS_SELF;
02dba438
RP
3813
3814 /* Generate event to notify upper layer of bridge
3815 * change
3816 */
3817 err = rtnl_bridge_notify(dev);
3818 }
2469ffd7 3819 }
e5a55a89 3820
c38e01b8 3821 if (have_flags)
2469ffd7 3822 memcpy(nla_data(attr), &flags, sizeof(flags));
e5a55a89
JF
3823out:
3824 return err;
3825}
3826
c21ef3e3
DA
3827static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
3828 struct netlink_ext_ack *extack)
407af329
VY
3829{
3830 struct net *net = sock_net(skb->sk);
3831 struct ifinfomsg *ifm;
3832 struct net_device *dev;
3833 struct nlattr *br_spec, *attr = NULL;
3834 int rem, err = -EOPNOTSUPP;
4de8b413 3835 u16 flags = 0;
407af329
VY
3836 bool have_flags = false;
3837
3838 if (nlmsg_len(nlh) < sizeof(*ifm))
3839 return -EINVAL;
3840
3841 ifm = nlmsg_data(nlh);
3842 if (ifm->ifi_family != AF_BRIDGE)
3843 return -EPFNOSUPPORT;
3844
3845 dev = __dev_get_by_index(net, ifm->ifi_index);
3846 if (!dev) {
b88d12e4 3847 NL_SET_ERR_MSG(extack, "unknown ifindex");
407af329
VY
3848 return -ENODEV;
3849 }
3850
3851 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3852 if (br_spec) {
3853 nla_for_each_nested(attr, br_spec, rem) {
3854 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
6e8d1c55
TG
3855 if (nla_len(attr) < sizeof(flags))
3856 return -EINVAL;
3857
407af329
VY
3858 have_flags = true;
3859 flags = nla_get_u16(attr);
3860 break;
3861 }
3862 }
3863 }
3864
407af329
VY
3865 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3866 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3867
3868 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
3869 err = -EOPNOTSUPP;
3870 goto out;
3871 }
3872
add511b3 3873 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
407af329
VY
3874 if (err)
3875 goto out;
3876
3877 flags &= ~BRIDGE_FLAGS_MASTER;
3878 }
3879
3880 if ((flags & BRIDGE_FLAGS_SELF)) {
3881 if (!dev->netdev_ops->ndo_bridge_dellink)
3882 err = -EOPNOTSUPP;
3883 else
add511b3
RP
3884 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
3885 flags);
407af329 3886
02dba438 3887 if (!err) {
407af329 3888 flags &= ~BRIDGE_FLAGS_SELF;
02dba438
RP
3889
3890 /* Generate event to notify upper layer of bridge
3891 * change
3892 */
3893 err = rtnl_bridge_notify(dev);
3894 }
407af329
VY
3895 }
3896
3897 if (have_flags)
3898 memcpy(nla_data(attr), &flags, sizeof(flags));
407af329
VY
3899out:
3900 return err;
3901}
3902
e8872a25
NA
3903static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
3904{
3905 return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
3906 (!idxattr || idxattr == attrid);
3907}
3908
69ae6ad2
NF
3909#define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
3910static int rtnl_get_offload_stats_attr_size(int attr_id)
3911{
3912 switch (attr_id) {
3913 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
3914 return sizeof(struct rtnl_link_stats64);
3915 }
3916
3917 return 0;
3918}
3919
3920static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
3921 int *prividx)
3922{
3923 struct nlattr *attr = NULL;
3924 int attr_id, size;
3925 void *attr_data;
3926 int err;
3927
3928 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
3929 dev->netdev_ops->ndo_get_offload_stats))
3930 return -ENODATA;
3931
3932 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
3933 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
3934 if (attr_id < *prividx)
3935 continue;
3936
3937 size = rtnl_get_offload_stats_attr_size(attr_id);
3938 if (!size)
3939 continue;
3940
3df5b3c6 3941 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
69ae6ad2
NF
3942 continue;
3943
3944 attr = nla_reserve_64bit(skb, attr_id, size,
3945 IFLA_OFFLOAD_XSTATS_UNSPEC);
3946 if (!attr)
3947 goto nla_put_failure;
3948
3949 attr_data = nla_data(attr);
3950 memset(attr_data, 0, size);
3951 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
3952 attr_data);
3953 if (err)
3954 goto get_offload_stats_failure;
3955 }
3956
3957 if (!attr)
3958 return -ENODATA;
3959
3960 *prividx = 0;
3961 return 0;
3962
3963nla_put_failure:
3964 err = -EMSGSIZE;
3965get_offload_stats_failure:
3966 *prividx = attr_id;
3967 return err;
3968}
3969
3970static int rtnl_get_offload_stats_size(const struct net_device *dev)
3971{
3972 int nla_size = 0;
3973 int attr_id;
3974 int size;
3975
3976 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
3977 dev->netdev_ops->ndo_get_offload_stats))
3978 return 0;
3979
3980 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
3981 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
3df5b3c6 3982 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
69ae6ad2
NF
3983 continue;
3984 size = rtnl_get_offload_stats_attr_size(attr_id);
3985 nla_size += nla_total_size_64bit(size);
3986 }
3987
3988 if (nla_size != 0)
3989 nla_size += nla_total_size(0);
3990
3991 return nla_size;
3992}
3993
10c9ead9
RP
3994static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
3995 int type, u32 pid, u32 seq, u32 change,
e8872a25
NA
3996 unsigned int flags, unsigned int filter_mask,
3997 int *idxattr, int *prividx)
10c9ead9
RP
3998{
3999 struct if_stats_msg *ifsm;
4000 struct nlmsghdr *nlh;
4001 struct nlattr *attr;
e8872a25 4002 int s_prividx = *prividx;
69ae6ad2 4003 int err;
10c9ead9
RP
4004
4005 ASSERT_RTNL();
4006
4007 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
4008 if (!nlh)
4009 return -EMSGSIZE;
4010
4011 ifsm = nlmsg_data(nlh);
ce024f42
NA
4012 ifsm->family = PF_UNSPEC;
4013 ifsm->pad1 = 0;
4014 ifsm->pad2 = 0;
10c9ead9
RP
4015 ifsm->ifindex = dev->ifindex;
4016 ifsm->filter_mask = filter_mask;
4017
e8872a25 4018 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
10c9ead9 4019 struct rtnl_link_stats64 *sp;
10c9ead9 4020
58414d32
ND
4021 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
4022 sizeof(struct rtnl_link_stats64),
4023 IFLA_STATS_UNSPEC);
10c9ead9
RP
4024 if (!attr)
4025 goto nla_put_failure;
4026
4027 sp = nla_data(attr);
4028 dev_get_stats(dev, sp);
4029 }
4030
97a47fac
NA
4031 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
4032 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
4033
4034 if (ops && ops->fill_linkxstats) {
97a47fac
NA
4035 *idxattr = IFLA_STATS_LINK_XSTATS;
4036 attr = nla_nest_start(skb,
4037 IFLA_STATS_LINK_XSTATS);
4038 if (!attr)
4039 goto nla_put_failure;
4040
80e73cc5
NA
4041 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
4042 nla_nest_end(skb, attr);
4043 if (err)
4044 goto nla_put_failure;
4045 *idxattr = 0;
4046 }
4047 }
4048
4049 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
4050 *idxattr)) {
4051 const struct rtnl_link_ops *ops = NULL;
4052 const struct net_device *master;
4053
4054 master = netdev_master_upper_dev_get(dev);
4055 if (master)
4056 ops = master->rtnl_link_ops;
4057 if (ops && ops->fill_linkxstats) {
80e73cc5
NA
4058 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
4059 attr = nla_nest_start(skb,
4060 IFLA_STATS_LINK_XSTATS_SLAVE);
4061 if (!attr)
4062 goto nla_put_failure;
4063
4064 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
97a47fac
NA
4065 nla_nest_end(skb, attr);
4066 if (err)
4067 goto nla_put_failure;
4068 *idxattr = 0;
4069 }
4070 }
4071
69ae6ad2
NF
4072 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
4073 *idxattr)) {
4074 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
4075 attr = nla_nest_start(skb, IFLA_STATS_LINK_OFFLOAD_XSTATS);
4076 if (!attr)
4077 goto nla_put_failure;
4078
4079 err = rtnl_get_offload_stats(skb, dev, prividx);
4080 if (err == -ENODATA)
4081 nla_nest_cancel(skb, attr);
4082 else
4083 nla_nest_end(skb, attr);
4084
4085 if (err && err != -ENODATA)
4086 goto nla_put_failure;
4087 *idxattr = 0;
4088 }
4089
aefb4d4a
RS
4090 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
4091 struct rtnl_af_ops *af_ops;
4092
4093 *idxattr = IFLA_STATS_AF_SPEC;
4094 attr = nla_nest_start(skb, IFLA_STATS_AF_SPEC);
4095 if (!attr)
4096 goto nla_put_failure;
4097
5fa85a09
FW
4098 rcu_read_lock();
4099 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
aefb4d4a
RS
4100 if (af_ops->fill_stats_af) {
4101 struct nlattr *af;
4102 int err;
4103
4104 af = nla_nest_start(skb, af_ops->family);
5fa85a09
FW
4105 if (!af) {
4106 rcu_read_unlock();
aefb4d4a 4107 goto nla_put_failure;
5fa85a09 4108 }
aefb4d4a
RS
4109 err = af_ops->fill_stats_af(skb, dev);
4110
5fa85a09 4111 if (err == -ENODATA) {
aefb4d4a 4112 nla_nest_cancel(skb, af);
5fa85a09
FW
4113 } else if (err < 0) {
4114 rcu_read_unlock();
aefb4d4a 4115 goto nla_put_failure;
5fa85a09 4116 }
aefb4d4a
RS
4117
4118 nla_nest_end(skb, af);
4119 }
4120 }
5fa85a09 4121 rcu_read_unlock();
aefb4d4a
RS
4122
4123 nla_nest_end(skb, attr);
4124
4125 *idxattr = 0;
4126 }
4127
10c9ead9
RP
4128 nlmsg_end(skb, nlh);
4129
4130 return 0;
4131
4132nla_put_failure:
e8872a25
NA
4133 /* not a multi message or no progress mean a real error */
4134 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
4135 nlmsg_cancel(skb, nlh);
4136 else
4137 nlmsg_end(skb, nlh);
10c9ead9
RP
4138
4139 return -EMSGSIZE;
4140}
4141
10c9ead9
RP
4142static size_t if_nlmsg_stats_size(const struct net_device *dev,
4143 u32 filter_mask)
4144{
4145 size_t size = 0;
4146
e8872a25 4147 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
10c9ead9
RP
4148 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
4149
97a47fac
NA
4150 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
4151 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
80e73cc5 4152 int attr = IFLA_STATS_LINK_XSTATS;
97a47fac
NA
4153
4154 if (ops && ops->get_linkxstats_size) {
80e73cc5
NA
4155 size += nla_total_size(ops->get_linkxstats_size(dev,
4156 attr));
97a47fac
NA
4157 /* for IFLA_STATS_LINK_XSTATS */
4158 size += nla_total_size(0);
4159 }
4160 }
4161
80e73cc5
NA
4162 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
4163 struct net_device *_dev = (struct net_device *)dev;
4164 const struct rtnl_link_ops *ops = NULL;
4165 const struct net_device *master;
4166
4167 /* netdev_master_upper_dev_get can't take const */
4168 master = netdev_master_upper_dev_get(_dev);
4169 if (master)
4170 ops = master->rtnl_link_ops;
4171 if (ops && ops->get_linkxstats_size) {
4172 int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
4173
4174 size += nla_total_size(ops->get_linkxstats_size(dev,
4175 attr));
4176 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
4177 size += nla_total_size(0);
4178 }
4179 }
4180
69ae6ad2
NF
4181 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
4182 size += rtnl_get_offload_stats_size(dev);
4183
aefb4d4a
RS
4184 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
4185 struct rtnl_af_ops *af_ops;
4186
4187 /* for IFLA_STATS_AF_SPEC */
4188 size += nla_total_size(0);
4189
5fa85a09
FW
4190 rcu_read_lock();
4191 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
aefb4d4a
RS
4192 if (af_ops->get_stats_af_size) {
4193 size += nla_total_size(
4194 af_ops->get_stats_af_size(dev));
4195
4196 /* for AF_* */
4197 size += nla_total_size(0);
4198 }
4199 }
5fa85a09 4200 rcu_read_unlock();
aefb4d4a
RS
4201 }
4202
10c9ead9
RP
4203 return size;
4204}
4205
c21ef3e3
DA
4206static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
4207 struct netlink_ext_ack *extack)
10c9ead9
RP
4208{
4209 struct net *net = sock_net(skb->sk);
10c9ead9 4210 struct net_device *dev = NULL;
e8872a25
NA
4211 int idxattr = 0, prividx = 0;
4212 struct if_stats_msg *ifsm;
10c9ead9
RP
4213 struct sk_buff *nskb;
4214 u32 filter_mask;
4215 int err;
4216
4775cc1f
MK
4217 if (nlmsg_len(nlh) < sizeof(*ifsm))
4218 return -EINVAL;
4219
10c9ead9
RP
4220 ifsm = nlmsg_data(nlh);
4221 if (ifsm->ifindex > 0)
4222 dev = __dev_get_by_index(net, ifsm->ifindex);
4223 else
4224 return -EINVAL;
4225
4226 if (!dev)
4227 return -ENODEV;
4228
4229 filter_mask = ifsm->filter_mask;
4230 if (!filter_mask)
4231 return -EINVAL;
4232
4233 nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
4234 if (!nskb)
4235 return -ENOBUFS;
4236
4237 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
4238 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
e8872a25 4239 0, filter_mask, &idxattr, &prividx);
10c9ead9
RP
4240 if (err < 0) {
4241 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
4242 WARN_ON(err == -EMSGSIZE);
4243 kfree_skb(nskb);
4244 } else {
4245 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
4246 }
4247
4248 return err;
4249}
4250
4251static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
4252{
e8872a25 4253 int h, s_h, err, s_idx, s_idxattr, s_prividx;
10c9ead9 4254 struct net *net = sock_net(skb->sk);
e8872a25 4255 unsigned int flags = NLM_F_MULTI;
10c9ead9 4256 struct if_stats_msg *ifsm;
10c9ead9 4257 struct hlist_head *head;
e8872a25 4258 struct net_device *dev;
10c9ead9 4259 u32 filter_mask = 0;
e8872a25 4260 int idx = 0;
10c9ead9
RP
4261
4262 s_h = cb->args[0];
4263 s_idx = cb->args[1];
e8872a25
NA
4264 s_idxattr = cb->args[2];
4265 s_prividx = cb->args[3];
10c9ead9
RP
4266
4267 cb->seq = net->dev_base_seq;
4268
4775cc1f
MK
4269 if (nlmsg_len(cb->nlh) < sizeof(*ifsm))
4270 return -EINVAL;
4271
10c9ead9
RP
4272 ifsm = nlmsg_data(cb->nlh);
4273 filter_mask = ifsm->filter_mask;
4274 if (!filter_mask)
4275 return -EINVAL;
4276
4277 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4278 idx = 0;
4279 head = &net->dev_index_head[h];
4280 hlist_for_each_entry(dev, head, index_hlist) {
4281 if (idx < s_idx)
4282 goto cont;
4283 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
4284 NETLINK_CB(cb->skb).portid,
4285 cb->nlh->nlmsg_seq, 0,
e8872a25
NA
4286 flags, filter_mask,
4287 &s_idxattr, &s_prividx);
10c9ead9
RP
4288 /* If we ran out of room on the first message,
4289 * we're in trouble
4290 */
4291 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
4292
4293 if (err < 0)
4294 goto out;
e8872a25
NA
4295 s_prividx = 0;
4296 s_idxattr = 0;
10c9ead9
RP
4297 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
4298cont:
4299 idx++;
4300 }
4301 }
4302out:
e8872a25
NA
4303 cb->args[3] = s_prividx;
4304 cb->args[2] = s_idxattr;
10c9ead9
RP
4305 cb->args[1] = idx;
4306 cb->args[0] = h;
4307
4308 return skb->len;
4309}
4310
1da177e4
LT
4311/* Process one rtnetlink message. */
4312
2d4bc933
JB
4313static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
4314 struct netlink_ext_ack *extack)
1da177e4 4315{
3b1e0a65 4316 struct net *net = sock_net(skb->sk);
6853dd48
FW
4317 struct rtnl_link *handlers;
4318 int err = -EOPNOTSUPP;
e2849863 4319 rtnl_doit_func doit;
62256f98 4320 unsigned int flags;
617cfc75 4321 int kind;
1da177e4
LT
4322 int family;
4323 int type;
1da177e4 4324
1da177e4 4325 type = nlh->nlmsg_type;
1da177e4 4326 if (type > RTM_MAX)
038890fe 4327 return -EOPNOTSUPP;
1da177e4
LT
4328
4329 type -= RTM_BASE;
4330
4331 /* All the messages must have at least 1 byte length */
573ce260 4332 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
1da177e4
LT
4333 return 0;
4334
573ce260 4335 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
1da177e4
LT
4336 kind = type&3;
4337
90f62cf3 4338 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
1d00a4eb 4339 return -EPERM;
1da177e4 4340
5c2bb9b6 4341 if (family >= ARRAY_SIZE(rtnl_msg_handlers))
6853dd48
FW
4342 family = PF_UNSPEC;
4343
4344 rcu_read_lock();
4345 handlers = rcu_dereference(rtnl_msg_handlers[family]);
4346 if (!handlers) {
4347 family = PF_UNSPEC;
4348 handlers = rcu_dereference(rtnl_msg_handlers[family]);
4349 }
4350
b8f3ab42 4351 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
97c53cac 4352 struct sock *rtnl;
e2849863 4353 rtnl_dumpit_func dumpit;
c7ac8679 4354 u16 min_dump_alloc = 0;
1da177e4 4355
6853dd48
FW
4356 dumpit = READ_ONCE(handlers[type].dumpit);
4357 if (!dumpit) {
4358 family = PF_UNSPEC;
4359 handlers = rcu_dereference(rtnl_msg_handlers[PF_UNSPEC]);
4360 if (!handlers)
4361 goto err_unlock;
0cc09020 4362
6853dd48
FW
4363 dumpit = READ_ONCE(handlers[type].dumpit);
4364 if (!dumpit)
4365 goto err_unlock;
4366 }
e1fa6d21 4367
019a3169
FW
4368 refcount_inc(&rtnl_msg_handlers_ref[family]);
4369
5c2bb9b6 4370 if (type == RTM_GETLINK - RTM_BASE)
e1fa6d21 4371 min_dump_alloc = rtnl_calcit(skb, nlh);
9ac4a169 4372
6853dd48
FW
4373 rcu_read_unlock();
4374
97c53cac 4375 rtnl = net->rtnl;
80d326fa
PNA
4376 {
4377 struct netlink_dump_control c = {
4378 .dump = dumpit,
4379 .min_dump_alloc = min_dump_alloc,
4380 };
4381 err = netlink_dump_start(rtnl, skb, nlh, &c);
4382 }
019a3169 4383 refcount_dec(&rtnl_msg_handlers_ref[family]);
2907c35f 4384 return err;
1da177e4
LT
4385 }
4386
8caa38b5
FW
4387 doit = READ_ONCE(handlers[type].doit);
4388 if (!doit) {
4389 family = PF_UNSPEC;
4390 handlers = rcu_dereference(rtnl_msg_handlers[family]);
4391 }
4392
62256f98
FW
4393 flags = READ_ONCE(handlers[type].flags);
4394 if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
4395 refcount_inc(&rtnl_msg_handlers_ref[family]);
4396 doit = READ_ONCE(handlers[type].doit);
4397 rcu_read_unlock();
4398 if (doit)
4399 err = doit(skb, nlh, extack);
4400 refcount_dec(&rtnl_msg_handlers_ref[family]);
4401 return err;
4402 }
4403
6853dd48 4404 rcu_read_unlock();
1da177e4 4405
6853dd48
FW
4406 rtnl_lock();
4407 handlers = rtnl_dereference(rtnl_msg_handlers[family]);
4408 if (handlers) {
4409 doit = READ_ONCE(handlers[type].doit);
4410 if (doit)
4411 err = doit(skb, nlh, extack);
4412 }
0cc09020 4413 rtnl_unlock();
0cc09020
FW
4414 return err;
4415
4416err_unlock:
6853dd48 4417 rcu_read_unlock();
0cc09020 4418 return -EOPNOTSUPP;
1da177e4
LT
4419}
4420
cd40b7d3 4421static void rtnetlink_rcv(struct sk_buff *skb)
1da177e4 4422{
cd40b7d3 4423 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
1da177e4
LT
4424}
4425
5f729eaa
JG
4426static int rtnetlink_bind(struct net *net, int group)
4427{
4428 switch (group) {
4429 case RTNLGRP_IPV4_MROUTE_R:
4430 case RTNLGRP_IPV6_MROUTE_R:
4431 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
4432 return -EPERM;
4433 break;
4434 }
4435 return 0;
4436}
4437
1da177e4
LT
4438static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
4439{
351638e7 4440 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
e9dc8653 4441
1da177e4 4442 switch (event) {
5138e86f 4443 case NETDEV_REBOOT:
8a212589 4444 case NETDEV_CHANGEMTU:
3753654e 4445 case NETDEV_CHANGEADDR:
5138e86f
VY
4446 case NETDEV_CHANGENAME:
4447 case NETDEV_FEAT_CHANGE:
4448 case NETDEV_BONDING_FAILOVER:
e6e66594 4449 case NETDEV_POST_TYPE_CHANGE:
5138e86f 4450 case NETDEV_NOTIFY_PEERS:
dc709f37 4451 case NETDEV_CHANGEUPPER:
5138e86f 4452 case NETDEV_RESEND_IGMP:
5138e86f 4453 case NETDEV_CHANGEINFODATA:
eeda3fb9 4454 case NETDEV_CHANGELOWERSTATE:
ebdcf045 4455 case NETDEV_CHANGE_TX_QUEUE_LEN:
3d3ea5af 4456 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
6621dd29 4457 GFP_KERNEL, NULL);
1da177e4
LT
4458 break;
4459 default:
1da177e4
LT
4460 break;
4461 }
4462 return NOTIFY_DONE;
4463}
4464
4465static struct notifier_block rtnetlink_dev_notifier = {
4466 .notifier_call = rtnetlink_event,
4467};
4468
97c53cac 4469
2c8c1e72 4470static int __net_init rtnetlink_net_init(struct net *net)
97c53cac
DL
4471{
4472 struct sock *sk;
a31f2d17
PNA
4473 struct netlink_kernel_cfg cfg = {
4474 .groups = RTNLGRP_MAX,
4475 .input = rtnetlink_rcv,
4476 .cb_mutex = &rtnl_mutex,
9785e10a 4477 .flags = NL_CFG_F_NONROOT_RECV,
5f729eaa 4478 .bind = rtnetlink_bind,
a31f2d17
PNA
4479 };
4480
9f00d977 4481 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
97c53cac
DL
4482 if (!sk)
4483 return -ENOMEM;
97c53cac
DL
4484 net->rtnl = sk;
4485 return 0;
4486}
4487
2c8c1e72 4488static void __net_exit rtnetlink_net_exit(struct net *net)
97c53cac 4489{
775516bf
DL
4490 netlink_kernel_release(net->rtnl);
4491 net->rtnl = NULL;
97c53cac
DL
4492}
4493
4494static struct pernet_operations rtnetlink_net_ops = {
4495 .init = rtnetlink_net_init,
4496 .exit = rtnetlink_net_exit,
4497};
4498
1da177e4
LT
4499void __init rtnetlink_init(void)
4500{
d38a6512
FW
4501 int i;
4502
4503 for (i = 0; i < ARRAY_SIZE(rtnl_msg_handlers_ref); i++)
4504 refcount_set(&rtnl_msg_handlers_ref[i], 1);
4505
97c53cac 4506 if (register_pernet_subsys(&rtnetlink_net_ops))
1da177e4 4507 panic("rtnetlink_init: cannot initialize rtnetlink\n");
97c53cac 4508
1da177e4 4509 register_netdevice_notifier(&rtnetlink_dev_notifier);
340d17fc 4510
c7ac8679 4511 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
b97bac64
FW
4512 rtnl_dump_ifinfo, 0);
4513 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
4514 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
4515 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
687ad8cc 4516
b97bac64
FW
4517 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
4518 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
4519 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
77162022 4520
b97bac64
FW
4521 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
4522 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
4523 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, 0);
e5a55a89 4524
b97bac64
FW
4525 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
4526 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
4527 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
10c9ead9
RP
4528
4529 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
b97bac64 4530 0);
1da177e4 4531}