]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/core/rtnetlink.c
net: systemport: Guard against unmapped TX ring
[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 */
88d6378b
AK
923 + nla_total_size(1); /* IFLA_PROTO_DOWN */
924
339bf98f
TG
925}
926
57b61080
SF
927static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
928{
929 struct nlattr *vf_ports;
930 struct nlattr *vf_port;
931 int vf;
932 int err;
933
934 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
935 if (!vf_ports)
936 return -EMSGSIZE;
937
938 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
939 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
8ca94183
SF
940 if (!vf_port)
941 goto nla_put_failure;
a6574349
DM
942 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
943 goto nla_put_failure;
57b61080 944 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
8ca94183
SF
945 if (err == -EMSGSIZE)
946 goto nla_put_failure;
57b61080 947 if (err) {
57b61080
SF
948 nla_nest_cancel(skb, vf_port);
949 continue;
950 }
951 nla_nest_end(skb, vf_port);
952 }
953
954 nla_nest_end(skb, vf_ports);
955
956 return 0;
8ca94183
SF
957
958nla_put_failure:
959 nla_nest_cancel(skb, vf_ports);
960 return -EMSGSIZE;
57b61080
SF
961}
962
963static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
964{
965 struct nlattr *port_self;
966 int err;
967
968 port_self = nla_nest_start(skb, IFLA_PORT_SELF);
969 if (!port_self)
970 return -EMSGSIZE;
971
972 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
973 if (err) {
974 nla_nest_cancel(skb, port_self);
8ca94183 975 return (err == -EMSGSIZE) ? err : 0;
57b61080
SF
976 }
977
978 nla_nest_end(skb, port_self);
979
980 return 0;
981}
982
c53864fd
DG
983static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
984 u32 ext_filter_mask)
57b61080
SF
985{
986 int err;
987
c53864fd
DG
988 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
989 !(ext_filter_mask & RTEXT_FILTER_VF))
57b61080
SF
990 return 0;
991
992 err = rtnl_port_self_fill(skb, dev);
993 if (err)
994 return err;
995
996 if (dev_num_vf(dev->dev.parent)) {
997 err = rtnl_vf_ports_fill(skb, dev);
998 if (err)
999 return err;
1000 }
1001
1002 return 0;
1003}
1004
66cae9ed
JP
1005static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
1006{
1007 int err;
02637fce 1008 struct netdev_phys_item_id ppid;
66cae9ed
JP
1009
1010 err = dev_get_phys_port_id(dev, &ppid);
1011 if (err) {
1012 if (err == -EOPNOTSUPP)
1013 return 0;
1014 return err;
1015 }
1016
1017 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1018 return -EMSGSIZE;
1019
1020 return 0;
1021}
1022
db24a904
DA
1023static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1024{
1025 char name[IFNAMSIZ];
1026 int err;
1027
1028 err = dev_get_phys_port_name(dev, name, sizeof(name));
1029 if (err) {
1030 if (err == -EOPNOTSUPP)
1031 return 0;
1032 return err;
1033 }
1034
77ef033b 1035 if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
db24a904
DA
1036 return -EMSGSIZE;
1037
1038 return 0;
1039}
1040
82f28412
JP
1041static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1042{
1043 int err;
f8e20a9f 1044 struct switchdev_attr attr = {
6ff64f6f 1045 .orig_dev = dev,
1f868398 1046 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
f8e20a9f
SF
1047 .flags = SWITCHDEV_F_NO_RECURSE,
1048 };
82f28412 1049
f8e20a9f 1050 err = switchdev_port_attr_get(dev, &attr);
82f28412
JP
1051 if (err) {
1052 if (err == -EOPNOTSUPP)
1053 return 0;
1054 return err;
1055 }
1056
42275bd8
SF
1057 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len,
1058 attr.u.ppid.id))
82f28412
JP
1059 return -EMSGSIZE;
1060
1061 return 0;
1062}
1063
b22b941b
HFS
1064static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1065 struct net_device *dev)
1066{
550bce59 1067 struct rtnl_link_stats64 *sp;
b22b941b 1068 struct nlattr *attr;
18402843 1069
58414d32
ND
1070 attr = nla_reserve_64bit(skb, IFLA_STATS64,
1071 sizeof(struct rtnl_link_stats64), IFLA_PAD);
b22b941b
HFS
1072 if (!attr)
1073 return -EMSGSIZE;
1074
550bce59
RP
1075 sp = nla_data(attr);
1076 dev_get_stats(dev, sp);
b22b941b 1077
550bce59
RP
1078 attr = nla_reserve(skb, IFLA_STATS,
1079 sizeof(struct rtnl_link_stats));
b22b941b
HFS
1080 if (!attr)
1081 return -EMSGSIZE;
1082
550bce59 1083 copy_rtnl_link_stats(nla_data(attr), sp);
b22b941b
HFS
1084
1085 return 0;
1086}
1087
1088static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1089 struct net_device *dev,
1090 int vfs_num,
1091 struct nlattr *vfinfo)
1092{
1093 struct ifla_vf_rss_query_en vf_rss_query_en;
79aab093 1094 struct nlattr *vf, *vfstats, *vfvlanlist;
b22b941b 1095 struct ifla_vf_link_state vf_linkstate;
79aab093 1096 struct ifla_vf_vlan_info vf_vlan_info;
b22b941b
HFS
1097 struct ifla_vf_spoofchk vf_spoofchk;
1098 struct ifla_vf_tx_rate vf_tx_rate;
1099 struct ifla_vf_stats vf_stats;
1100 struct ifla_vf_trust vf_trust;
1101 struct ifla_vf_vlan vf_vlan;
1102 struct ifla_vf_rate vf_rate;
b22b941b
HFS
1103 struct ifla_vf_mac vf_mac;
1104 struct ifla_vf_info ivi;
1105
0eed9cf5
MY
1106 memset(&ivi, 0, sizeof(ivi));
1107
b22b941b
HFS
1108 /* Not all SR-IOV capable drivers support the
1109 * spoofcheck and "RSS query enable" query. Preset to
1110 * -1 so the user space tool can detect that the driver
1111 * didn't report anything.
1112 */
1113 ivi.spoofchk = -1;
1114 ivi.rss_query_en = -1;
1115 ivi.trusted = -1;
b22b941b
HFS
1116 /* The default value for VF link state is "auto"
1117 * IFLA_VF_LINK_STATE_AUTO which equals zero
1118 */
1119 ivi.linkstate = 0;
79aab093
MS
1120 /* VLAN Protocol by default is 802.1Q */
1121 ivi.vlan_proto = htons(ETH_P_8021Q);
b22b941b
HFS
1122 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1123 return 0;
1124
775f4f05
DC
1125 memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
1126
b22b941b
HFS
1127 vf_mac.vf =
1128 vf_vlan.vf =
79aab093 1129 vf_vlan_info.vf =
b22b941b
HFS
1130 vf_rate.vf =
1131 vf_tx_rate.vf =
1132 vf_spoofchk.vf =
1133 vf_linkstate.vf =
1134 vf_rss_query_en.vf =
1135 vf_trust.vf = ivi.vf;
1136
1137 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1138 vf_vlan.vlan = ivi.vlan;
1139 vf_vlan.qos = ivi.qos;
79aab093
MS
1140 vf_vlan_info.vlan = ivi.vlan;
1141 vf_vlan_info.qos = ivi.qos;
1142 vf_vlan_info.vlan_proto = ivi.vlan_proto;
b22b941b
HFS
1143 vf_tx_rate.rate = ivi.max_tx_rate;
1144 vf_rate.min_tx_rate = ivi.min_tx_rate;
1145 vf_rate.max_tx_rate = ivi.max_tx_rate;
1146 vf_spoofchk.setting = ivi.spoofchk;
1147 vf_linkstate.link_state = ivi.linkstate;
1148 vf_rss_query_en.setting = ivi.rss_query_en;
1149 vf_trust.setting = ivi.trusted;
1150 vf = nla_nest_start(skb, IFLA_VF_INFO);
79aab093
MS
1151 if (!vf)
1152 goto nla_put_vfinfo_failure;
b22b941b
HFS
1153 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1154 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1155 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1156 &vf_rate) ||
1157 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1158 &vf_tx_rate) ||
1159 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1160 &vf_spoofchk) ||
1161 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1162 &vf_linkstate) ||
1163 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1164 sizeof(vf_rss_query_en),
1165 &vf_rss_query_en) ||
1166 nla_put(skb, IFLA_VF_TRUST,
1167 sizeof(vf_trust), &vf_trust))
79aab093
MS
1168 goto nla_put_vf_failure;
1169 vfvlanlist = nla_nest_start(skb, IFLA_VF_VLAN_LIST);
1170 if (!vfvlanlist)
1171 goto nla_put_vf_failure;
1172 if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
1173 &vf_vlan_info)) {
1174 nla_nest_cancel(skb, vfvlanlist);
1175 goto nla_put_vf_failure;
1176 }
1177 nla_nest_end(skb, vfvlanlist);
b22b941b
HFS
1178 memset(&vf_stats, 0, sizeof(vf_stats));
1179 if (dev->netdev_ops->ndo_get_vf_stats)
1180 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1181 &vf_stats);
1182 vfstats = nla_nest_start(skb, IFLA_VF_STATS);
79aab093
MS
1183 if (!vfstats)
1184 goto nla_put_vf_failure;
343a6d8e
ND
1185 if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1186 vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1187 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1188 vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1189 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1190 vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1191 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1192 vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1193 nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1194 vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1195 nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
79aab093
MS
1196 vf_stats.multicast, IFLA_VF_STATS_PAD)) {
1197 nla_nest_cancel(skb, vfstats);
1198 goto nla_put_vf_failure;
1199 }
b22b941b
HFS
1200 nla_nest_end(skb, vfstats);
1201 nla_nest_end(skb, vf);
1202 return 0;
79aab093
MS
1203
1204nla_put_vf_failure:
1205 nla_nest_cancel(skb, vf);
1206nla_put_vfinfo_failure:
1207 nla_nest_cancel(skb, vfinfo);
1208 return -EMSGSIZE;
b22b941b
HFS
1209}
1210
250fc3df
FW
1211static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb,
1212 struct net_device *dev,
1213 u32 ext_filter_mask)
1214{
1215 struct nlattr *vfinfo;
1216 int i, num_vfs;
1217
1218 if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0))
1219 return 0;
1220
1221 num_vfs = dev_num_vf(dev->dev.parent);
1222 if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs))
1223 return -EMSGSIZE;
1224
1225 if (!dev->netdev_ops->ndo_get_vf_config)
1226 return 0;
1227
1228 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1229 if (!vfinfo)
1230 return -EMSGSIZE;
1231
1232 for (i = 0; i < num_vfs; i++) {
1233 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1234 return -EMSGSIZE;
1235 }
1236
1237 nla_nest_end(skb, vfinfo);
1238 return 0;
1239}
1240
b22b941b
HFS
1241static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1242{
5f8e4474
KL
1243 struct rtnl_link_ifmap map;
1244
1245 memset(&map, 0, sizeof(map));
1246 map.mem_start = dev->mem_start;
1247 map.mem_end = dev->mem_end;
1248 map.base_addr = dev->base_addr;
1249 map.irq = dev->irq;
1250 map.dma = dev->dma;
1251 map.port = dev->if_port;
1252
270cb4d0 1253 if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
b22b941b
HFS
1254 return -EMSGSIZE;
1255
1256 return 0;
1257}
1258
58038695 1259static u8 rtnl_xdp_attached_mode(struct net_device *dev, u32 *prog_id)
d67b9cd2
DB
1260{
1261 const struct net_device_ops *ops = dev->netdev_ops;
58038695 1262 const struct bpf_prog *generic_xdp_prog;
d67b9cd2
DB
1263
1264 ASSERT_RTNL();
1265
58038695
MKL
1266 *prog_id = 0;
1267 generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
1268 if (generic_xdp_prog) {
1269 *prog_id = generic_xdp_prog->aux->id;
d67b9cd2 1270 return XDP_ATTACHED_SKB;
58038695 1271 }
ce158e58
JK
1272 if (!ops->ndo_xdp)
1273 return XDP_ATTACHED_NONE;
d67b9cd2 1274
ce158e58 1275 return __dev_xdp_attached(dev, ops->ndo_xdp, prog_id);
d67b9cd2
DB
1276}
1277
d1fdd913
BB
1278static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1279{
d1fdd913 1280 struct nlattr *xdp;
58038695 1281 u32 prog_id;
d1fdd913
BB
1282 int err;
1283
d1fdd913
BB
1284 xdp = nla_nest_start(skb, IFLA_XDP);
1285 if (!xdp)
1286 return -EMSGSIZE;
d67b9cd2
DB
1287
1288 err = nla_put_u8(skb, IFLA_XDP_ATTACHED,
58038695 1289 rtnl_xdp_attached_mode(dev, &prog_id));
d1fdd913
BB
1290 if (err)
1291 goto err_cancel;
1292
58038695
MKL
1293 if (prog_id) {
1294 err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
1295 if (err)
1296 goto err_cancel;
1297 }
1298
d1fdd913
BB
1299 nla_nest_end(skb, xdp);
1300 return 0;
1301
1302err_cancel:
1303 nla_nest_cancel(skb, xdp);
1304 return err;
1305}
1306
3d3ea5af
VY
1307static u32 rtnl_get_event(unsigned long event)
1308{
1309 u32 rtnl_event_type = IFLA_EVENT_NONE;
1310
1311 switch (event) {
1312 case NETDEV_REBOOT:
1313 rtnl_event_type = IFLA_EVENT_REBOOT;
1314 break;
1315 case NETDEV_FEAT_CHANGE:
1316 rtnl_event_type = IFLA_EVENT_FEATURES;
1317 break;
1318 case NETDEV_BONDING_FAILOVER:
1319 rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
1320 break;
1321 case NETDEV_NOTIFY_PEERS:
1322 rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
1323 break;
1324 case NETDEV_RESEND_IGMP:
1325 rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
1326 break;
1327 case NETDEV_CHANGEINFODATA:
1328 rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
1329 break;
1330 default:
1331 break;
1332 }
1333
1334 return rtnl_event_type;
1335}
1336
79110a04
FW
1337static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
1338{
1339 const struct net_device *upper_dev;
1340 int ret = 0;
1341
1342 rcu_read_lock();
1343
1344 upper_dev = netdev_master_upper_dev_get_rcu(dev);
1345 if (upper_dev)
1346 ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);
1347
1348 rcu_read_unlock();
1349 return ret;
1350}
1351
1352static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev)
1353{
1354 int ifindex = dev_get_iflink(dev);
1355
1356 if (dev->ifindex == ifindex)
1357 return 0;
1358
1359 return nla_put_u32(skb, IFLA_LINK, ifindex);
1360}
1361
6c557001
FW
1362static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
1363 struct net_device *dev)
1364{
1365 char buf[IFALIASZ];
1366 int ret;
1367
1368 ret = dev_get_alias(dev, buf, sizeof(buf));
1369 return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
1370}
1371
b1e66b9a
FW
1372static int rtnl_fill_link_netnsid(struct sk_buff *skb,
1373 const struct net_device *dev)
1374{
1375 if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
1376 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1377
1378 if (!net_eq(dev_net(dev), link_net)) {
1379 int id = peernet2id_alloc(dev_net(dev), link_net);
1380
1381 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1382 return -EMSGSIZE;
1383 }
1384 }
1385
1386 return 0;
1387}
1388
070cbf5b
FW
1389static int rtnl_fill_link_af(struct sk_buff *skb,
1390 const struct net_device *dev,
1391 u32 ext_filter_mask)
1392{
1393 const struct rtnl_af_ops *af_ops;
1394 struct nlattr *af_spec;
1395
1396 af_spec = nla_nest_start(skb, IFLA_AF_SPEC);
1397 if (!af_spec)
1398 return -EMSGSIZE;
1399
5fa85a09 1400 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
070cbf5b
FW
1401 struct nlattr *af;
1402 int err;
1403
1404 if (!af_ops->fill_link_af)
1405 continue;
1406
1407 af = nla_nest_start(skb, af_ops->family);
1408 if (!af)
1409 return -EMSGSIZE;
1410
1411 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1412 /*
1413 * Caller may return ENODATA to indicate that there
1414 * was no data to be dumped. This is not an error, it
1415 * means we should trim the attribute header and
1416 * continue.
1417 */
1418 if (err == -ENODATA)
1419 nla_nest_cancel(skb, af);
1420 else if (err < 0)
1421 return -EMSGSIZE;
1422
1423 nla_nest_end(skb, af);
1424 }
1425
1426 nla_nest_end(skb, af_spec);
1427 return 0;
1428}
1429
b60c5115 1430static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
575c3e2a 1431 int type, u32 pid, u32 seq, u32 change,
3d3ea5af 1432 unsigned int flags, u32 ext_filter_mask,
6621dd29 1433 u32 event, int *new_nsid)
b60c5115
TG
1434{
1435 struct ifinfomsg *ifm;
1436 struct nlmsghdr *nlh;
1da177e4 1437
2907c35f 1438 ASSERT_RTNL();
b60c5115
TG
1439 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1440 if (nlh == NULL)
26932566 1441 return -EMSGSIZE;
1da177e4 1442
b60c5115
TG
1443 ifm = nlmsg_data(nlh);
1444 ifm->ifi_family = AF_UNSPEC;
1445 ifm->__ifi_pad = 0;
1446 ifm->ifi_type = dev->type;
1447 ifm->ifi_index = dev->ifindex;
1448 ifm->ifi_flags = dev_get_flags(dev);
1449 ifm->ifi_change = change;
1450
a6574349
DM
1451 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1452 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1453 nla_put_u8(skb, IFLA_OPERSTATE,
1454 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1455 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1456 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1457 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
edbc0bb3 1458 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
76ff5cc9 1459 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
c70ce028
ED
1460 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1461 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
1d69c2b3 1462#ifdef CONFIG_RPS
76ff5cc9 1463 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
1d69c2b3 1464#endif
79110a04
FW
1465 nla_put_iflink(skb, dev) ||
1466 put_master_ifindex(skb, dev) ||
9a57247f 1467 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
a6574349
DM
1468 (dev->qdisc &&
1469 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
6c557001 1470 nla_put_ifalias(skb, dev) ||
2d3b479d 1471 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
88d6378b
AK
1472 atomic_read(&dev->carrier_changes)) ||
1473 nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
a6574349 1474 goto nla_put_failure;
0b815a1a 1475
3d3ea5af
VY
1476 if (event != IFLA_EVENT_NONE) {
1477 if (nla_put_u32(skb, IFLA_EVENT, event))
1478 goto nla_put_failure;
1479 }
1480
b22b941b
HFS
1481 if (rtnl_fill_link_ifmap(skb, dev))
1482 goto nla_put_failure;
1da177e4
LT
1483
1484 if (dev->addr_len) {
a6574349
DM
1485 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1486 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1487 goto nla_put_failure;
1da177e4
LT
1488 }
1489
66cae9ed
JP
1490 if (rtnl_phys_port_id_fill(skb, dev))
1491 goto nla_put_failure;
1492
db24a904
DA
1493 if (rtnl_phys_port_name_fill(skb, dev))
1494 goto nla_put_failure;
1495
82f28412
JP
1496 if (rtnl_phys_switch_id_fill(skb, dev))
1497 goto nla_put_failure;
1498
b22b941b 1499 if (rtnl_fill_stats(skb, dev))
10708f37 1500 goto nla_put_failure;
10708f37 1501
250fc3df 1502 if (rtnl_fill_vf(skb, dev, ext_filter_mask))
a6574349 1503 goto nla_put_failure;
57b61080 1504
c53864fd 1505 if (rtnl_port_fill(skb, dev, ext_filter_mask))
57b61080
SF
1506 goto nla_put_failure;
1507
d1fdd913
BB
1508 if (rtnl_xdp_fill(skb, dev))
1509 goto nla_put_failure;
1510
ba7d49b1 1511 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
38f7b870
PM
1512 if (rtnl_link_fill(skb, dev) < 0)
1513 goto nla_put_failure;
1514 }
1515
b1e66b9a
FW
1516 if (rtnl_fill_link_netnsid(skb, dev))
1517 goto nla_put_failure;
d37512a2 1518
6621dd29
ND
1519 if (new_nsid &&
1520 nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0)
1521 goto nla_put_failure;
1522
5fa85a09 1523 rcu_read_lock();
070cbf5b 1524 if (rtnl_fill_link_af(skb, dev, ext_filter_mask))
5fa85a09
FW
1525 goto nla_put_failure_rcu;
1526 rcu_read_unlock();
f8ff182c 1527
053c095a
JB
1528 nlmsg_end(skb, nlh);
1529 return 0;
b60c5115 1530
5fa85a09
FW
1531nla_put_failure_rcu:
1532 rcu_read_unlock();
b60c5115 1533nla_put_failure:
26932566
PM
1534 nlmsg_cancel(skb, nlh);
1535 return -EMSGSIZE;
1da177e4
LT
1536}
1537
f7b12606 1538static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
5176f91e 1539 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
38f7b870
PM
1540 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1541 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
5176f91e 1542 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
da5e0494 1543 [IFLA_MTU] = { .type = NLA_U32 },
76e87306 1544 [IFLA_LINK] = { .type = NLA_U32 },
fbaec0ea 1545 [IFLA_MASTER] = { .type = NLA_U32 },
9a57247f 1546 [IFLA_CARRIER] = { .type = NLA_U8 },
da5e0494
TG
1547 [IFLA_TXQLEN] = { .type = NLA_U32 },
1548 [IFLA_WEIGHT] = { .type = NLA_U32 },
1549 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1550 [IFLA_LINKMODE] = { .type = NLA_U8 },
76e87306 1551 [IFLA_LINKINFO] = { .type = NLA_NESTED },
d8a5ec67 1552 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
f0630529 1553 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
2459b4c6
ND
1554 /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
1555 * allow 0-length string (needed to remove an alias).
1556 */
1557 [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
c02db8c6 1558 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
57b61080
SF
1559 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1560 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
f8ff182c 1561 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
115c9b81 1562 [IFLA_EXT_MASK] = { .type = NLA_U32 },
edbc0bb3 1563 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
76ff5cc9
JP
1564 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1565 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
02637fce 1566 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
2d3b479d 1567 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
82f28412 1568 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
317f4810 1569 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
88d6378b 1570 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
d1fdd913 1571 [IFLA_XDP] = { .type = NLA_NESTED },
3d3ea5af 1572 [IFLA_EVENT] = { .type = NLA_U32 },
db833d40 1573 [IFLA_GROUP] = { .type = NLA_U32 },
da5e0494
TG
1574};
1575
38f7b870
PM
1576static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1577 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1578 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
ba7d49b1
JP
1579 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1580 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
38f7b870
PM
1581};
1582
c02db8c6 1583static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
364d5716
DB
1584 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1585 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
79aab093 1586 [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED },
364d5716
DB
1587 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1588 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1589 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1590 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
01a3d796 1591 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
3b766cd8 1592 [IFLA_VF_STATS] = { .type = NLA_NESTED },
dd461d6a 1593 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
cc8e27cc
EC
1594 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1595 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
3b766cd8
EBE
1596};
1597
57b61080
SF
1598static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1599 [IFLA_PORT_VF] = { .type = NLA_U32 },
1600 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1601 .len = PORT_PROFILE_MAX },
57b61080
SF
1602 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1603 .len = PORT_UUID_MAX },
1604 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1605 .len = PORT_UUID_MAX },
1606 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1607 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
025331df
DB
1608
1609 /* Unused, but we need to keep it here since user space could
1610 * fill it. It's also broken with regard to NLA_BINARY use in
1611 * combination with structs.
1612 */
1613 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1614 .len = sizeof(struct ifla_port_vsi) },
57b61080
SF
1615};
1616
d1fdd913
BB
1617static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
1618 [IFLA_XDP_FD] = { .type = NLA_S32 },
1619 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 },
85de8576 1620 [IFLA_XDP_FLAGS] = { .type = NLA_U32 },
58038695 1621 [IFLA_XDP_PROG_ID] = { .type = NLA_U32 },
d1fdd913
BB
1622};
1623
dc599f76
DA
1624static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1625{
1626 const struct rtnl_link_ops *ops = NULL;
1627 struct nlattr *linfo[IFLA_INFO_MAX + 1];
1628
fceb6435
JB
1629 if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla,
1630 ifla_info_policy, NULL) < 0)
dc599f76
DA
1631 return NULL;
1632
1633 if (linfo[IFLA_INFO_KIND]) {
1634 char kind[MODULE_NAME_LEN];
1635
1636 nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1637 ops = rtnl_link_ops_get(kind);
1638 }
1639
1640 return ops;
1641}
1642
1643static bool link_master_filtered(struct net_device *dev, int master_idx)
1644{
1645 struct net_device *master;
1646
1647 if (!master_idx)
1648 return false;
1649
1650 master = netdev_master_upper_dev_get(dev);
1651 if (!master || master->ifindex != master_idx)
1652 return true;
1653
1654 return false;
1655}
1656
1657static bool link_kind_filtered(const struct net_device *dev,
1658 const struct rtnl_link_ops *kind_ops)
1659{
1660 if (kind_ops && dev->rtnl_link_ops != kind_ops)
1661 return true;
1662
1663 return false;
1664}
1665
1666static bool link_dump_filtered(struct net_device *dev,
1667 int master_idx,
1668 const struct rtnl_link_ops *kind_ops)
1669{
1670 if (link_master_filtered(dev, master_idx) ||
1671 link_kind_filtered(dev, kind_ops))
1672 return true;
1673
1674 return false;
1675}
1676
f7b12606
JP
1677static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
1678{
1679 struct net *net = sock_net(skb->sk);
1680 int h, s_h;
1681 int idx = 0, s_idx;
1682 struct net_device *dev;
1683 struct hlist_head *head;
1684 struct nlattr *tb[IFLA_MAX+1];
1685 u32 ext_filter_mask = 0;
dc599f76
DA
1686 const struct rtnl_link_ops *kind_ops = NULL;
1687 unsigned int flags = NLM_F_MULTI;
1688 int master_idx = 0;
973462bb 1689 int err;
e5eca6d4 1690 int hdrlen;
f7b12606
JP
1691
1692 s_h = cb->args[0];
1693 s_idx = cb->args[1];
1694
e5eca6d4
MS
1695 /* A hack to preserve kernel<->userspace interface.
1696 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1697 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1698 * what iproute2 < v3.9.0 used.
1699 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1700 * attribute, its netlink message is shorter than struct ifinfomsg.
1701 */
1702 hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1703 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1704
fceb6435
JB
1705 if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX,
1706 ifla_policy, NULL) >= 0) {
f7b12606
JP
1707 if (tb[IFLA_EXT_MASK])
1708 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
dc599f76
DA
1709
1710 if (tb[IFLA_MASTER])
1711 master_idx = nla_get_u32(tb[IFLA_MASTER]);
1712
1713 if (tb[IFLA_LINKINFO])
1714 kind_ops = linkinfo_to_kind_ops(tb[IFLA_LINKINFO]);
1715
1716 if (master_idx || kind_ops)
1717 flags |= NLM_F_DUMP_FILTERED;
f7b12606
JP
1718 }
1719
1720 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1721 idx = 0;
1722 head = &net->dev_index_head[h];
cac5e65e 1723 hlist_for_each_entry(dev, head, index_hlist) {
dc599f76 1724 if (link_dump_filtered(dev, master_idx, kind_ops))
3f0ae05d 1725 goto cont;
f7b12606
JP
1726 if (idx < s_idx)
1727 goto cont;
973462bb
DG
1728 err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
1729 NETLINK_CB(cb->skb).portid,
1730 cb->nlh->nlmsg_seq, 0,
dc599f76 1731 flags,
6621dd29 1732 ext_filter_mask, 0, NULL);
973462bb 1733
f6c5775f
DA
1734 if (err < 0) {
1735 if (likely(skb->len))
1736 goto out;
1737
1738 goto out_err;
1739 }
f7b12606
JP
1740cont:
1741 idx++;
1742 }
1743 }
1744out:
f6c5775f
DA
1745 err = skb->len;
1746out_err:
f7b12606
JP
1747 cb->args[1] = idx;
1748 cb->args[0] = h;
d0225784
JS
1749 cb->seq = net->dev_base_seq;
1750 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
f7b12606 1751
f6c5775f 1752 return err;
f7b12606
JP
1753}
1754
fceb6435
JB
1755int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
1756 struct netlink_ext_ack *exterr)
f7b12606 1757{
fceb6435 1758 return nla_parse(tb, IFLA_MAX, head, len, ifla_policy, exterr);
f7b12606
JP
1759}
1760EXPORT_SYMBOL(rtnl_nla_parse_ifla);
1761
81adee47
EB
1762struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1763{
1764 struct net *net;
1765 /* Examine the link attributes and figure out which
1766 * network namespace we are talking about.
1767 */
1768 if (tb[IFLA_NET_NS_PID])
1769 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
f0630529
EB
1770 else if (tb[IFLA_NET_NS_FD])
1771 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
81adee47
EB
1772 else
1773 net = get_net(src_net);
1774 return net;
1775}
1776EXPORT_SYMBOL(rtnl_link_get_net);
1777
1840bb13
TG
1778static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1779{
1780 if (dev) {
1781 if (tb[IFLA_ADDRESS] &&
1782 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1783 return -EINVAL;
1784
1785 if (tb[IFLA_BROADCAST] &&
1786 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1787 return -EINVAL;
1788 }
1789
cf7afbfe
TG
1790 if (tb[IFLA_AF_SPEC]) {
1791 struct nlattr *af;
1792 int rem, err;
1793
1794 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1795 const struct rtnl_af_ops *af_ops;
1796
5fa85a09
FW
1797 rcu_read_lock();
1798 af_ops = rtnl_af_lookup(nla_type(af));
1799 if (!af_ops) {
1800 rcu_read_unlock();
cf7afbfe 1801 return -EAFNOSUPPORT;
5fa85a09 1802 }
cf7afbfe 1803
5fa85a09
FW
1804 if (!af_ops->set_link_af) {
1805 rcu_read_unlock();
cf7afbfe 1806 return -EOPNOTSUPP;
5fa85a09 1807 }
cf7afbfe
TG
1808
1809 if (af_ops->validate_link_af) {
6d3a9a68 1810 err = af_ops->validate_link_af(dev, af);
5fa85a09
FW
1811 if (err < 0) {
1812 rcu_read_unlock();
cf7afbfe 1813 return err;
5fa85a09 1814 }
cf7afbfe 1815 }
5fa85a09
FW
1816
1817 rcu_read_unlock();
cf7afbfe
TG
1818 }
1819 }
1820
1840bb13
TG
1821 return 0;
1822}
1823
cc8e27cc
EC
1824static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
1825 int guid_type)
1826{
1827 const struct net_device_ops *ops = dev->netdev_ops;
1828
1829 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
1830}
1831
1832static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
1833{
1834 if (dev->type != ARPHRD_INFINIBAND)
1835 return -EOPNOTSUPP;
1836
1837 return handle_infiniband_guid(dev, ivt, guid_type);
1838}
1839
4f7d2cdf 1840static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
c02db8c6 1841{
c02db8c6 1842 const struct net_device_ops *ops = dev->netdev_ops;
4f7d2cdf 1843 int err = -EINVAL;
c02db8c6 1844
4f7d2cdf
DB
1845 if (tb[IFLA_VF_MAC]) {
1846 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
01a3d796 1847
4f7d2cdf
DB
1848 err = -EOPNOTSUPP;
1849 if (ops->ndo_set_vf_mac)
1850 err = ops->ndo_set_vf_mac(dev, ivm->vf,
1851 ivm->mac);
1852 if (err < 0)
1853 return err;
1854 }
1855
1856 if (tb[IFLA_VF_VLAN]) {
1857 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
1858
1859 err = -EOPNOTSUPP;
1860 if (ops->ndo_set_vf_vlan)
1861 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
79aab093
MS
1862 ivv->qos,
1863 htons(ETH_P_8021Q));
1864 if (err < 0)
1865 return err;
1866 }
1867
1868 if (tb[IFLA_VF_VLAN_LIST]) {
1869 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
1870 struct nlattr *attr;
1871 int rem, len = 0;
1872
1873 err = -EOPNOTSUPP;
1874 if (!ops->ndo_set_vf_vlan)
1875 return err;
1876
1877 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
1878 if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
1879 nla_len(attr) < NLA_HDRLEN) {
1880 return -EINVAL;
1881 }
1882 if (len >= MAX_VLAN_LIST_LEN)
1883 return -EOPNOTSUPP;
1884 ivvl[len] = nla_data(attr);
1885
1886 len++;
1887 }
fa34cd94
AB
1888 if (len == 0)
1889 return -EINVAL;
1890
79aab093
MS
1891 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
1892 ivvl[0]->qos, ivvl[0]->vlan_proto);
4f7d2cdf
DB
1893 if (err < 0)
1894 return err;
c02db8c6 1895 }
4f7d2cdf
DB
1896
1897 if (tb[IFLA_VF_TX_RATE]) {
1898 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
1899 struct ifla_vf_info ivf;
1900
1901 err = -EOPNOTSUPP;
1902 if (ops->ndo_get_vf_config)
1903 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
1904 if (err < 0)
1905 return err;
1906
1907 err = -EOPNOTSUPP;
1908 if (ops->ndo_set_vf_rate)
1909 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1910 ivf.min_tx_rate,
1911 ivt->rate);
1912 if (err < 0)
1913 return err;
1914 }
1915
1916 if (tb[IFLA_VF_RATE]) {
1917 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
1918
1919 err = -EOPNOTSUPP;
1920 if (ops->ndo_set_vf_rate)
1921 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1922 ivt->min_tx_rate,
1923 ivt->max_tx_rate);
1924 if (err < 0)
1925 return err;
1926 }
1927
1928 if (tb[IFLA_VF_SPOOFCHK]) {
1929 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
1930
1931 err = -EOPNOTSUPP;
1932 if (ops->ndo_set_vf_spoofchk)
1933 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
1934 ivs->setting);
1935 if (err < 0)
1936 return err;
1937 }
1938
1939 if (tb[IFLA_VF_LINK_STATE]) {
1940 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
1941
1942 err = -EOPNOTSUPP;
1943 if (ops->ndo_set_vf_link_state)
1944 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
1945 ivl->link_state);
1946 if (err < 0)
1947 return err;
1948 }
1949
1950 if (tb[IFLA_VF_RSS_QUERY_EN]) {
1951 struct ifla_vf_rss_query_en *ivrssq_en;
1952
1953 err = -EOPNOTSUPP;
1954 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
1955 if (ops->ndo_set_vf_rss_query_en)
1956 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
1957 ivrssq_en->setting);
1958 if (err < 0)
1959 return err;
1960 }
1961
dd461d6a
HS
1962 if (tb[IFLA_VF_TRUST]) {
1963 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
1964
1965 err = -EOPNOTSUPP;
1966 if (ops->ndo_set_vf_trust)
1967 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
1968 if (err < 0)
1969 return err;
1970 }
1971
cc8e27cc
EC
1972 if (tb[IFLA_VF_IB_NODE_GUID]) {
1973 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
1974
1975 if (!ops->ndo_set_vf_guid)
1976 return -EOPNOTSUPP;
1977
1978 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
1979 }
1980
1981 if (tb[IFLA_VF_IB_PORT_GUID]) {
1982 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
1983
1984 if (!ops->ndo_set_vf_guid)
1985 return -EOPNOTSUPP;
1986
1987 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
1988 }
1989
c02db8c6
CW
1990 return err;
1991}
1992
33eaf2a6
DA
1993static int do_set_master(struct net_device *dev, int ifindex,
1994 struct netlink_ext_ack *extack)
fbaec0ea 1995{
898e5061 1996 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
fbaec0ea
JP
1997 const struct net_device_ops *ops;
1998 int err;
1999
898e5061
JP
2000 if (upper_dev) {
2001 if (upper_dev->ifindex == ifindex)
fbaec0ea 2002 return 0;
898e5061 2003 ops = upper_dev->netdev_ops;
fbaec0ea 2004 if (ops->ndo_del_slave) {
898e5061 2005 err = ops->ndo_del_slave(upper_dev, dev);
fbaec0ea
JP
2006 if (err)
2007 return err;
2008 } else {
2009 return -EOPNOTSUPP;
2010 }
2011 }
2012
2013 if (ifindex) {
898e5061
JP
2014 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
2015 if (!upper_dev)
fbaec0ea 2016 return -EINVAL;
898e5061 2017 ops = upper_dev->netdev_ops;
fbaec0ea 2018 if (ops->ndo_add_slave) {
33eaf2a6 2019 err = ops->ndo_add_slave(upper_dev, dev, extack);
fbaec0ea
JP
2020 if (err)
2021 return err;
2022 } else {
2023 return -EOPNOTSUPP;
2024 }
2025 }
2026 return 0;
2027}
2028
90c325e3 2029#define DO_SETLINK_MODIFIED 0x01
ba998906
ND
2030/* notify flag means notify + modified. */
2031#define DO_SETLINK_NOTIFY 0x03
90f62cf3
EB
2032static int do_setlink(const struct sk_buff *skb,
2033 struct net_device *dev, struct ifinfomsg *ifm,
ddf9f970 2034 struct netlink_ext_ack *extack,
90c325e3 2035 struct nlattr **tb, char *ifname, int status)
1da177e4 2036{
d314774c 2037 const struct net_device_ops *ops = dev->netdev_ops;
0157f60c 2038 int err;
1da177e4 2039
f0630529 2040 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
81adee47 2041 struct net *net = rtnl_link_get_net(dev_net(dev), tb);
d8a5ec67
EB
2042 if (IS_ERR(net)) {
2043 err = PTR_ERR(net);
2044 goto errout;
2045 }
90f62cf3 2046 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
e0ebde0e 2047 put_net(net);
b51642f6
EB
2048 err = -EPERM;
2049 goto errout;
2050 }
d8a5ec67
EB
2051 err = dev_change_net_namespace(dev, net, ifname);
2052 put_net(net);
2053 if (err)
2054 goto errout;
90c325e3 2055 status |= DO_SETLINK_MODIFIED;
d8a5ec67
EB
2056 }
2057
da5e0494 2058 if (tb[IFLA_MAP]) {
1da177e4
LT
2059 struct rtnl_link_ifmap *u_map;
2060 struct ifmap k_map;
2061
d314774c 2062 if (!ops->ndo_set_config) {
1da177e4 2063 err = -EOPNOTSUPP;
0157f60c 2064 goto errout;
1da177e4
LT
2065 }
2066
2067 if (!netif_device_present(dev)) {
2068 err = -ENODEV;
0157f60c 2069 goto errout;
1da177e4 2070 }
1da177e4 2071
da5e0494 2072 u_map = nla_data(tb[IFLA_MAP]);
1da177e4
LT
2073 k_map.mem_start = (unsigned long) u_map->mem_start;
2074 k_map.mem_end = (unsigned long) u_map->mem_end;
2075 k_map.base_addr = (unsigned short) u_map->base_addr;
2076 k_map.irq = (unsigned char) u_map->irq;
2077 k_map.dma = (unsigned char) u_map->dma;
2078 k_map.port = (unsigned char) u_map->port;
2079
d314774c 2080 err = ops->ndo_set_config(dev, &k_map);
da5e0494 2081 if (err < 0)
0157f60c 2082 goto errout;
1da177e4 2083
ba998906 2084 status |= DO_SETLINK_NOTIFY;
1da177e4
LT
2085 }
2086
da5e0494 2087 if (tb[IFLA_ADDRESS]) {
70f8e78e
DM
2088 struct sockaddr *sa;
2089 int len;
2090
153711f9
WC
2091 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2092 sizeof(*sa));
70f8e78e
DM
2093 sa = kmalloc(len, GFP_KERNEL);
2094 if (!sa) {
2095 err = -ENOMEM;
0157f60c 2096 goto errout;
70f8e78e
DM
2097 }
2098 sa->sa_family = dev->type;
da5e0494 2099 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
70f8e78e 2100 dev->addr_len);
e7c3273e 2101 err = dev_set_mac_address(dev, sa);
70f8e78e 2102 kfree(sa);
1da177e4 2103 if (err)
0157f60c 2104 goto errout;
90c325e3 2105 status |= DO_SETLINK_MODIFIED;
1da177e4
LT
2106 }
2107
da5e0494
TG
2108 if (tb[IFLA_MTU]) {
2109 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
2110 if (err < 0)
0157f60c 2111 goto errout;
90c325e3 2112 status |= DO_SETLINK_MODIFIED;
1da177e4
LT
2113 }
2114
cbda10fa
VD
2115 if (tb[IFLA_GROUP]) {
2116 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
ba998906 2117 status |= DO_SETLINK_NOTIFY;
cbda10fa
VD
2118 }
2119
da5e0494
TG
2120 /*
2121 * Interface selected by interface index but interface
2122 * name provided implies that a name change has been
2123 * requested.
2124 */
51055be8 2125 if (ifm->ifi_index > 0 && ifname[0]) {
da5e0494
TG
2126 err = dev_change_name(dev, ifname);
2127 if (err < 0)
0157f60c 2128 goto errout;
90c325e3 2129 status |= DO_SETLINK_MODIFIED;
1da177e4
LT
2130 }
2131
0b815a1a
SH
2132 if (tb[IFLA_IFALIAS]) {
2133 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2134 nla_len(tb[IFLA_IFALIAS]));
2135 if (err < 0)
2136 goto errout;
ba998906 2137 status |= DO_SETLINK_NOTIFY;
0b815a1a
SH
2138 }
2139
da5e0494
TG
2140 if (tb[IFLA_BROADCAST]) {
2141 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
e7c3273e 2142 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
1da177e4
LT
2143 }
2144
83b496e9 2145 if (ifm->ifi_flags || ifm->ifi_change) {
3729d502 2146 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
5f9021cf
JB
2147 if (err < 0)
2148 goto errout;
83b496e9 2149 }
1da177e4 2150
fbaec0ea 2151 if (tb[IFLA_MASTER]) {
33eaf2a6 2152 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
fbaec0ea
JP
2153 if (err)
2154 goto errout;
90c325e3 2155 status |= DO_SETLINK_MODIFIED;
fbaec0ea
JP
2156 }
2157
9a57247f
JP
2158 if (tb[IFLA_CARRIER]) {
2159 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2160 if (err)
2161 goto errout;
90c325e3 2162 status |= DO_SETLINK_MODIFIED;
9a57247f
JP
2163 }
2164
5d1180fc 2165 if (tb[IFLA_TXQLEN]) {
0cd29503
AD
2166 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
2167 unsigned int orig_len = dev->tx_queue_len;
08294a26
JW
2168
2169 if (dev->tx_queue_len ^ value) {
2170 dev->tx_queue_len = value;
2171 err = call_netdevice_notifiers(
2172 NETDEV_CHANGE_TX_QUEUE_LEN, dev);
2173 err = notifier_to_errno(err);
2174 if (err) {
2175 dev->tx_queue_len = orig_len;
2176 goto errout;
2177 }
2d7f669b 2178 status |= DO_SETLINK_MODIFIED;
08294a26 2179 }
5d1180fc 2180 }
b00055aa 2181
da5e0494 2182 if (tb[IFLA_OPERSTATE])
93b2d4a2 2183 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
b00055aa 2184
da5e0494 2185 if (tb[IFLA_LINKMODE]) {
1889b0e7
ND
2186 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2187
93b2d4a2 2188 write_lock_bh(&dev_base_lock);
1889b0e7 2189 if (dev->link_mode ^ value)
ba998906 2190 status |= DO_SETLINK_NOTIFY;
1889b0e7 2191 dev->link_mode = value;
93b2d4a2 2192 write_unlock_bh(&dev_base_lock);
b00055aa
SR
2193 }
2194
c02db8c6 2195 if (tb[IFLA_VFINFO_LIST]) {
4f7d2cdf 2196 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
c02db8c6
CW
2197 struct nlattr *attr;
2198 int rem;
4f7d2cdf 2199
c02db8c6 2200 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
4f7d2cdf
DB
2201 if (nla_type(attr) != IFLA_VF_INFO ||
2202 nla_len(attr) < NLA_HDRLEN) {
253683bb 2203 err = -EINVAL;
c02db8c6 2204 goto errout;
253683bb 2205 }
4f7d2cdf 2206 err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr,
fceb6435 2207 ifla_vf_policy, NULL);
4f7d2cdf
DB
2208 if (err < 0)
2209 goto errout;
2210 err = do_setvfinfo(dev, vfinfo);
c02db8c6
CW
2211 if (err < 0)
2212 goto errout;
ba998906 2213 status |= DO_SETLINK_NOTIFY;
c02db8c6 2214 }
ebc08a6f 2215 }
1da177e4
LT
2216 err = 0;
2217
57b61080
SF
2218 if (tb[IFLA_VF_PORTS]) {
2219 struct nlattr *port[IFLA_PORT_MAX+1];
2220 struct nlattr *attr;
2221 int vf;
2222 int rem;
2223
2224 err = -EOPNOTSUPP;
2225 if (!ops->ndo_set_vf_port)
2226 goto errout;
2227
2228 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
035d210f
DB
2229 if (nla_type(attr) != IFLA_VF_PORT ||
2230 nla_len(attr) < NLA_HDRLEN) {
2231 err = -EINVAL;
2232 goto errout;
2233 }
2234 err = nla_parse_nested(port, IFLA_PORT_MAX, attr,
fceb6435 2235 ifla_port_policy, NULL);
57b61080
SF
2236 if (err < 0)
2237 goto errout;
2238 if (!port[IFLA_PORT_VF]) {
2239 err = -EOPNOTSUPP;
2240 goto errout;
2241 }
2242 vf = nla_get_u32(port[IFLA_PORT_VF]);
2243 err = ops->ndo_set_vf_port(dev, vf, port);
2244 if (err < 0)
2245 goto errout;
ba998906 2246 status |= DO_SETLINK_NOTIFY;
57b61080
SF
2247 }
2248 }
2249 err = 0;
2250
2251 if (tb[IFLA_PORT_SELF]) {
2252 struct nlattr *port[IFLA_PORT_MAX+1];
2253
2254 err = nla_parse_nested(port, IFLA_PORT_MAX,
fceb6435
JB
2255 tb[IFLA_PORT_SELF], ifla_port_policy,
2256 NULL);
57b61080
SF
2257 if (err < 0)
2258 goto errout;
2259
2260 err = -EOPNOTSUPP;
2261 if (ops->ndo_set_vf_port)
2262 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
2263 if (err < 0)
2264 goto errout;
ba998906 2265 status |= DO_SETLINK_NOTIFY;
57b61080 2266 }
f8ff182c
TG
2267
2268 if (tb[IFLA_AF_SPEC]) {
2269 struct nlattr *af;
2270 int rem;
2271
2272 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2273 const struct rtnl_af_ops *af_ops;
2274
5fa85a09
FW
2275 rcu_read_lock();
2276
f8ff182c 2277 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
cf7afbfe 2278 BUG();
f8ff182c 2279
cf7afbfe 2280 err = af_ops->set_link_af(dev, af);
5fa85a09
FW
2281 if (err < 0) {
2282 rcu_read_unlock();
f8ff182c 2283 goto errout;
5fa85a09 2284 }
f8ff182c 2285
5fa85a09 2286 rcu_read_unlock();
ba998906 2287 status |= DO_SETLINK_NOTIFY;
f8ff182c
TG
2288 }
2289 }
57b61080
SF
2290 err = 0;
2291
88d6378b
AK
2292 if (tb[IFLA_PROTO_DOWN]) {
2293 err = dev_change_proto_down(dev,
2294 nla_get_u8(tb[IFLA_PROTO_DOWN]));
2295 if (err)
2296 goto errout;
2297 status |= DO_SETLINK_NOTIFY;
2298 }
2299
d1fdd913
BB
2300 if (tb[IFLA_XDP]) {
2301 struct nlattr *xdp[IFLA_XDP_MAX + 1];
85de8576 2302 u32 xdp_flags = 0;
d1fdd913
BB
2303
2304 err = nla_parse_nested(xdp, IFLA_XDP_MAX, tb[IFLA_XDP],
fceb6435 2305 ifla_xdp_policy, NULL);
d1fdd913
BB
2306 if (err < 0)
2307 goto errout;
2308
58038695 2309 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
262d8625
BB
2310 err = -EINVAL;
2311 goto errout;
2312 }
85de8576
DB
2313
2314 if (xdp[IFLA_XDP_FLAGS]) {
2315 xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
2316 if (xdp_flags & ~XDP_FLAGS_MASK) {
2317 err = -EINVAL;
2318 goto errout;
2319 }
ee5d032f 2320 if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
0489df9a
DB
2321 err = -EINVAL;
2322 goto errout;
2323 }
85de8576
DB
2324 }
2325
d1fdd913 2326 if (xdp[IFLA_XDP_FD]) {
ddf9f970 2327 err = dev_change_xdp_fd(dev, extack,
85de8576
DB
2328 nla_get_s32(xdp[IFLA_XDP_FD]),
2329 xdp_flags);
d1fdd913
BB
2330 if (err)
2331 goto errout;
2332 status |= DO_SETLINK_NOTIFY;
2333 }
2334 }
2335
0157f60c 2336errout:
ba998906 2337 if (status & DO_SETLINK_MODIFIED) {
64ff90cc 2338 if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
ba998906
ND
2339 netdev_state_change(dev);
2340
2341 if (err < 0)
2342 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",
2343 dev->name);
2344 }
da5e0494 2345
0157f60c
PM
2346 return err;
2347}
1da177e4 2348
c21ef3e3
DA
2349static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2350 struct netlink_ext_ack *extack)
0157f60c 2351{
3b1e0a65 2352 struct net *net = sock_net(skb->sk);
0157f60c
PM
2353 struct ifinfomsg *ifm;
2354 struct net_device *dev;
2355 int err;
2356 struct nlattr *tb[IFLA_MAX+1];
2357 char ifname[IFNAMSIZ];
2358
c21ef3e3
DA
2359 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy,
2360 extack);
0157f60c
PM
2361 if (err < 0)
2362 goto errout;
2363
2364 if (tb[IFLA_IFNAME])
2365 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2366 else
2367 ifname[0] = '\0';
2368
2369 err = -EINVAL;
2370 ifm = nlmsg_data(nlh);
2371 if (ifm->ifi_index > 0)
a3d12891 2372 dev = __dev_get_by_index(net, ifm->ifi_index);
0157f60c 2373 else if (tb[IFLA_IFNAME])
a3d12891 2374 dev = __dev_get_by_name(net, ifname);
0157f60c
PM
2375 else
2376 goto errout;
2377
2378 if (dev == NULL) {
2379 err = -ENODEV;
2380 goto errout;
2381 }
2382
e0d087af
ED
2383 err = validate_linkmsg(dev, tb);
2384 if (err < 0)
a3d12891 2385 goto errout;
0157f60c 2386
ddf9f970 2387 err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
da5e0494 2388errout:
1da177e4
LT
2389 return err;
2390}
2391
66400d54
WC
2392static int rtnl_group_dellink(const struct net *net, int group)
2393{
2394 struct net_device *dev, *aux;
2395 LIST_HEAD(list_kill);
2396 bool found = false;
2397
2398 if (!group)
2399 return -EPERM;
2400
2401 for_each_netdev(net, dev) {
2402 if (dev->group == group) {
2403 const struct rtnl_link_ops *ops;
2404
2405 found = true;
2406 ops = dev->rtnl_link_ops;
2407 if (!ops || !ops->dellink)
2408 return -EOPNOTSUPP;
2409 }
2410 }
2411
2412 if (!found)
2413 return -ENODEV;
2414
2415 for_each_netdev_safe(net, dev, aux) {
2416 if (dev->group == group) {
2417 const struct rtnl_link_ops *ops;
2418
2419 ops = dev->rtnl_link_ops;
2420 ops->dellink(dev, &list_kill);
2421 }
2422 }
2423 unregister_netdevice_many(&list_kill);
2424
2425 return 0;
2426}
2427
614732ea
TG
2428int rtnl_delete_link(struct net_device *dev)
2429{
2430 const struct rtnl_link_ops *ops;
2431 LIST_HEAD(list_kill);
2432
2433 ops = dev->rtnl_link_ops;
2434 if (!ops || !ops->dellink)
2435 return -EOPNOTSUPP;
2436
2437 ops->dellink(dev, &list_kill);
2438 unregister_netdevice_many(&list_kill);
2439
2440 return 0;
2441}
2442EXPORT_SYMBOL_GPL(rtnl_delete_link);
2443
c21ef3e3
DA
2444static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
2445 struct netlink_ext_ack *extack)
38f7b870 2446{
3b1e0a65 2447 struct net *net = sock_net(skb->sk);
38f7b870
PM
2448 struct net_device *dev;
2449 struct ifinfomsg *ifm;
2450 char ifname[IFNAMSIZ];
2451 struct nlattr *tb[IFLA_MAX+1];
2452 int err;
2453
c21ef3e3 2454 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
38f7b870
PM
2455 if (err < 0)
2456 return err;
2457
2458 if (tb[IFLA_IFNAME])
2459 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2460
2461 ifm = nlmsg_data(nlh);
2462 if (ifm->ifi_index > 0)
881d966b 2463 dev = __dev_get_by_index(net, ifm->ifi_index);
38f7b870 2464 else if (tb[IFLA_IFNAME])
881d966b 2465 dev = __dev_get_by_name(net, ifname);
66400d54
WC
2466 else if (tb[IFLA_GROUP])
2467 return rtnl_group_dellink(net, nla_get_u32(tb[IFLA_GROUP]));
38f7b870
PM
2468 else
2469 return -EINVAL;
2470
2471 if (!dev)
2472 return -ENODEV;
2473
614732ea 2474 return rtnl_delete_link(dev);
38f7b870
PM
2475}
2476
3729d502
PM
2477int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
2478{
2479 unsigned int old_flags;
2480 int err;
2481
2482 old_flags = dev->flags;
2483 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
2484 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
2485 if (err < 0)
2486 return err;
2487 }
2488
2489 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
3729d502 2490
a528c219 2491 __dev_notify_flags(dev, old_flags, ~0U);
3729d502
PM
2492 return 0;
2493}
2494EXPORT_SYMBOL(rtnl_configure_link);
2495
c0713563 2496struct net_device *rtnl_create_link(struct net *net,
78ebb0d0 2497 const char *ifname, unsigned char name_assign_type,
5517750f 2498 const struct rtnl_link_ops *ops, struct nlattr *tb[])
e7199288 2499{
e7199288 2500 struct net_device *dev;
d40156aa
JP
2501 unsigned int num_tx_queues = 1;
2502 unsigned int num_rx_queues = 1;
e7199288 2503
76ff5cc9
JP
2504 if (tb[IFLA_NUM_TX_QUEUES])
2505 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
2506 else if (ops->get_num_tx_queues)
d40156aa 2507 num_tx_queues = ops->get_num_tx_queues();
76ff5cc9
JP
2508
2509 if (tb[IFLA_NUM_RX_QUEUES])
2510 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
2511 else if (ops->get_num_rx_queues)
d40156aa 2512 num_rx_queues = ops->get_num_rx_queues();
efacb309 2513
5517750f 2514 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
c835a677 2515 ops->setup, num_tx_queues, num_rx_queues);
e7199288 2516 if (!dev)
d1892e4e 2517 return ERR_PTR(-ENOMEM);
e7199288 2518
81adee47
EB
2519 dev_net_set(dev, net);
2520 dev->rtnl_link_ops = ops;
3729d502 2521 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
81adee47 2522
e7199288
PE
2523 if (tb[IFLA_MTU])
2524 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
2afb9b53 2525 if (tb[IFLA_ADDRESS]) {
e7199288
PE
2526 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
2527 nla_len(tb[IFLA_ADDRESS]));
2afb9b53
JP
2528 dev->addr_assign_type = NET_ADDR_SET;
2529 }
e7199288
PE
2530 if (tb[IFLA_BROADCAST])
2531 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
2532 nla_len(tb[IFLA_BROADCAST]));
2533 if (tb[IFLA_TXQLEN])
2534 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
2535 if (tb[IFLA_OPERSTATE])
93b2d4a2 2536 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
e7199288
PE
2537 if (tb[IFLA_LINKMODE])
2538 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
ffa934f1
PM
2539 if (tb[IFLA_GROUP])
2540 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
e7199288
PE
2541
2542 return dev;
e7199288 2543}
e0d087af 2544EXPORT_SYMBOL(rtnl_create_link);
e7199288 2545
90f62cf3
EB
2546static int rtnl_group_changelink(const struct sk_buff *skb,
2547 struct net *net, int group,
e7ed828f 2548 struct ifinfomsg *ifm,
ddf9f970 2549 struct netlink_ext_ack *extack,
e7ed828f
VD
2550 struct nlattr **tb)
2551{
d079535d 2552 struct net_device *dev, *aux;
e7ed828f
VD
2553 int err;
2554
d079535d 2555 for_each_netdev_safe(net, dev, aux) {
e7ed828f 2556 if (dev->group == group) {
ddf9f970 2557 err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
e7ed828f
VD
2558 if (err < 0)
2559 return err;
2560 }
2561 }
2562
2563 return 0;
2564}
2565
c21ef3e3
DA
2566static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2567 struct netlink_ext_ack *extack)
38f7b870 2568{
3b1e0a65 2569 struct net *net = sock_net(skb->sk);
38f7b870 2570 const struct rtnl_link_ops *ops;
ba7d49b1 2571 const struct rtnl_link_ops *m_ops = NULL;
38f7b870 2572 struct net_device *dev;
ba7d49b1 2573 struct net_device *master_dev = NULL;
38f7b870
PM
2574 struct ifinfomsg *ifm;
2575 char kind[MODULE_NAME_LEN];
2576 char ifname[IFNAMSIZ];
2577 struct nlattr *tb[IFLA_MAX+1];
2578 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
5517750f 2579 unsigned char name_assign_type = NET_NAME_USER;
38f7b870
PM
2580 int err;
2581
95a5afca 2582#ifdef CONFIG_MODULES
38f7b870 2583replay:
8072f085 2584#endif
c21ef3e3 2585 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
38f7b870
PM
2586 if (err < 0)
2587 return err;
2588
2589 if (tb[IFLA_IFNAME])
2590 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2591 else
2592 ifname[0] = '\0';
2593
2594 ifm = nlmsg_data(nlh);
2595 if (ifm->ifi_index > 0)
881d966b 2596 dev = __dev_get_by_index(net, ifm->ifi_index);
e7ed828f
VD
2597 else {
2598 if (ifname[0])
2599 dev = __dev_get_by_name(net, ifname);
e7ed828f
VD
2600 else
2601 dev = NULL;
2602 }
38f7b870 2603
ba7d49b1
JP
2604 if (dev) {
2605 master_dev = netdev_master_upper_dev_get(dev);
2606 if (master_dev)
2607 m_ops = master_dev->rtnl_link_ops;
2608 }
2609
e0d087af
ED
2610 err = validate_linkmsg(dev, tb);
2611 if (err < 0)
1840bb13
TG
2612 return err;
2613
38f7b870
PM
2614 if (tb[IFLA_LINKINFO]) {
2615 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
fceb6435
JB
2616 tb[IFLA_LINKINFO], ifla_info_policy,
2617 NULL);
38f7b870
PM
2618 if (err < 0)
2619 return err;
2620 } else
2621 memset(linkinfo, 0, sizeof(linkinfo));
2622
2623 if (linkinfo[IFLA_INFO_KIND]) {
2624 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
2625 ops = rtnl_link_ops_get(kind);
2626 } else {
2627 kind[0] = '\0';
2628 ops = NULL;
2629 }
2630
2631 if (1) {
4e10fd5b
SL
2632 struct nlattr *attr[ops ? ops->maxtype + 1 : 1];
2633 struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1];
ba7d49b1
JP
2634 struct nlattr **data = NULL;
2635 struct nlattr **slave_data = NULL;
317f4810 2636 struct net *dest_net, *link_net = NULL;
38f7b870
PM
2637
2638 if (ops) {
2639 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
2640 err = nla_parse_nested(attr, ops->maxtype,
2641 linkinfo[IFLA_INFO_DATA],
fceb6435 2642 ops->policy, NULL);
38f7b870
PM
2643 if (err < 0)
2644 return err;
2645 data = attr;
2646 }
2647 if (ops->validate) {
a8b8a889 2648 err = ops->validate(tb, data, extack);
38f7b870
PM
2649 if (err < 0)
2650 return err;
2651 }
2652 }
2653
ba7d49b1
JP
2654 if (m_ops) {
2655 if (m_ops->slave_maxtype &&
2656 linkinfo[IFLA_INFO_SLAVE_DATA]) {
2657 err = nla_parse_nested(slave_attr,
2658 m_ops->slave_maxtype,
2659 linkinfo[IFLA_INFO_SLAVE_DATA],
fceb6435
JB
2660 m_ops->slave_policy,
2661 NULL);
ba7d49b1
JP
2662 if (err < 0)
2663 return err;
2664 slave_data = slave_attr;
2665 }
ba7d49b1
JP
2666 }
2667
38f7b870 2668 if (dev) {
90c325e3 2669 int status = 0;
38f7b870
PM
2670
2671 if (nlh->nlmsg_flags & NLM_F_EXCL)
2672 return -EEXIST;
2673 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2674 return -EOPNOTSUPP;
2675
2676 if (linkinfo[IFLA_INFO_DATA]) {
2677 if (!ops || ops != dev->rtnl_link_ops ||
2678 !ops->changelink)
2679 return -EOPNOTSUPP;
2680
ad744b22 2681 err = ops->changelink(dev, tb, data, extack);
38f7b870
PM
2682 if (err < 0)
2683 return err;
ba998906 2684 status |= DO_SETLINK_NOTIFY;
38f7b870
PM
2685 }
2686
ba7d49b1
JP
2687 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2688 if (!m_ops || !m_ops->slave_changelink)
2689 return -EOPNOTSUPP;
2690
2691 err = m_ops->slave_changelink(master_dev, dev,
17dd0ec4
MS
2692 tb, slave_data,
2693 extack);
ba7d49b1
JP
2694 if (err < 0)
2695 return err;
ba998906 2696 status |= DO_SETLINK_NOTIFY;
ba7d49b1
JP
2697 }
2698
ddf9f970
JK
2699 return do_setlink(skb, dev, ifm, extack, tb, ifname,
2700 status);
38f7b870
PM
2701 }
2702
ffa934f1
PM
2703 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2704 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
90f62cf3 2705 return rtnl_group_changelink(skb, net,
ffa934f1 2706 nla_get_u32(tb[IFLA_GROUP]),
ddf9f970 2707 ifm, extack, tb);
38f7b870 2708 return -ENODEV;
ffa934f1 2709 }
38f7b870 2710
160ca014 2711 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
38f7b870
PM
2712 return -EOPNOTSUPP;
2713
2714 if (!ops) {
95a5afca 2715#ifdef CONFIG_MODULES
38f7b870
PM
2716 if (kind[0]) {
2717 __rtnl_unlock();
2718 request_module("rtnl-link-%s", kind);
2719 rtnl_lock();
2720 ops = rtnl_link_ops_get(kind);
2721 if (ops)
2722 goto replay;
2723 }
2724#endif
2725 return -EOPNOTSUPP;
2726 }
2727
b0ab2fab
JP
2728 if (!ops->setup)
2729 return -EOPNOTSUPP;
2730
5517750f 2731 if (!ifname[0]) {
38f7b870 2732 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
5517750f
TG
2733 name_assign_type = NET_NAME_ENUM;
2734 }
e7199288 2735
81adee47 2736 dest_net = rtnl_link_get_net(net, tb);
13ad1774
EB
2737 if (IS_ERR(dest_net))
2738 return PTR_ERR(dest_net);
2739
505ce415
EB
2740 err = -EPERM;
2741 if (!netlink_ns_capable(skb, dest_net->user_ns, CAP_NET_ADMIN))
2742 goto out;
2743
317f4810
ND
2744 if (tb[IFLA_LINK_NETNSID]) {
2745 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
2746
2747 link_net = get_net_ns_by_id(dest_net, id);
2748 if (!link_net) {
2749 err = -EINVAL;
2750 goto out;
2751 }
06615bed
EB
2752 err = -EPERM;
2753 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
2754 goto out;
317f4810
ND
2755 }
2756
2757 dev = rtnl_create_link(link_net ? : dest_net, ifname,
2758 name_assign_type, ops, tb);
9c7dafbf 2759 if (IS_ERR(dev)) {
e7199288 2760 err = PTR_ERR(dev);
9c7dafbf
PE
2761 goto out;
2762 }
2763
2764 dev->ifindex = ifm->ifi_index;
2765
0e0eee24 2766 if (ops->newlink) {
7a3f4a18
MS
2767 err = ops->newlink(link_net ? : net, dev, tb, data,
2768 extack);
0e0eee24 2769 /* Drivers should call free_netdev() in ->destructor
e51fb152
CW
2770 * and unregister it on failure after registration
2771 * so that device could be finally freed in rtnl_unlock.
0e0eee24 2772 */
e51fb152
CW
2773 if (err < 0) {
2774 /* If device is not registered at all, free it now */
2775 if (dev->reg_state == NETREG_UNINITIALIZED)
2776 free_netdev(dev);
0e0eee24 2777 goto out;
e51fb152 2778 }
0e0eee24 2779 } else {
2d85cba2 2780 err = register_netdevice(dev);
0e0eee24
CW
2781 if (err < 0) {
2782 free_netdev(dev);
2783 goto out;
2784 }
fce9b9be 2785 }
3729d502 2786 err = rtnl_configure_link(dev, ifm);
43638900
DM
2787 if (err < 0)
2788 goto out_unregister;
bdef279b 2789 if (link_net) {
317f4810 2790 err = dev_change_net_namespace(dev, dest_net, ifname);
bdef279b 2791 if (err < 0)
43638900 2792 goto out_unregister;
bdef279b 2793 }
160ca014 2794 if (tb[IFLA_MASTER]) {
33eaf2a6
DA
2795 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]),
2796 extack);
160ca014
TV
2797 if (err)
2798 goto out_unregister;
2799 }
3729d502 2800out:
317f4810
ND
2801 if (link_net)
2802 put_net(link_net);
81adee47 2803 put_net(dest_net);
38f7b870 2804 return err;
43638900
DM
2805out_unregister:
2806 if (ops->newlink) {
2807 LIST_HEAD(list_kill);
2808
2809 ops->dellink(dev, &list_kill);
2810 unregister_netdevice_many(&list_kill);
2811 } else {
2812 unregister_netdevice(dev);
2813 }
2814 goto out;
38f7b870
PM
2815 }
2816}
2817
c21ef3e3
DA
2818static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2819 struct netlink_ext_ack *extack)
711e2c33 2820{
3b1e0a65 2821 struct net *net = sock_net(skb->sk);
b60c5115 2822 struct ifinfomsg *ifm;
a3d12891 2823 char ifname[IFNAMSIZ];
b60c5115
TG
2824 struct nlattr *tb[IFLA_MAX+1];
2825 struct net_device *dev = NULL;
2826 struct sk_buff *nskb;
339bf98f 2827 int err;
115c9b81 2828 u32 ext_filter_mask = 0;
711e2c33 2829
c21ef3e3 2830 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
b60c5115 2831 if (err < 0)
9918f230 2832 return err;
b60c5115 2833
a3d12891
ED
2834 if (tb[IFLA_IFNAME])
2835 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2836
115c9b81
GR
2837 if (tb[IFLA_EXT_MASK])
2838 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2839
b60c5115 2840 ifm = nlmsg_data(nlh);
a3d12891
ED
2841 if (ifm->ifi_index > 0)
2842 dev = __dev_get_by_index(net, ifm->ifi_index);
2843 else if (tb[IFLA_IFNAME])
2844 dev = __dev_get_by_name(net, ifname);
2845 else
711e2c33 2846 return -EINVAL;
711e2c33 2847
a3d12891
ED
2848 if (dev == NULL)
2849 return -ENODEV;
2850
115c9b81 2851 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
a3d12891
ED
2852 if (nskb == NULL)
2853 return -ENOBUFS;
b60c5115 2854
15e47304 2855 err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
6621dd29 2856 nlh->nlmsg_seq, 0, 0, ext_filter_mask, 0, NULL);
26932566
PM
2857 if (err < 0) {
2858 /* -EMSGSIZE implies BUG in if_nlmsg_size */
2859 WARN_ON(err == -EMSGSIZE);
2860 kfree_skb(nskb);
a3d12891 2861 } else
15e47304 2862 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
711e2c33 2863
b60c5115 2864 return err;
711e2c33 2865}
711e2c33 2866
115c9b81 2867static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
c7ac8679 2868{
115c9b81
GR
2869 struct net *net = sock_net(skb->sk);
2870 struct net_device *dev;
2871 struct nlattr *tb[IFLA_MAX+1];
2872 u32 ext_filter_mask = 0;
2873 u16 min_ifinfo_dump_size = 0;
e5eca6d4
MS
2874 int hdrlen;
2875
2876 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2877 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2878 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
115c9b81 2879
fceb6435 2880 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
a4b64fbe
ED
2881 if (tb[IFLA_EXT_MASK])
2882 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2883 }
115c9b81
GR
2884
2885 if (!ext_filter_mask)
2886 return NLMSG_GOODSIZE;
2887 /*
2888 * traverse the list of net devices and compute the minimum
2889 * buffer size based upon the filter mask.
2890 */
6853dd48
FW
2891 rcu_read_lock();
2892 for_each_netdev_rcu(net, dev) {
115c9b81
GR
2893 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
2894 if_nlmsg_size(dev,
2895 ext_filter_mask));
2896 }
6853dd48 2897 rcu_read_unlock();
115c9b81 2898
93af2056 2899 return nlmsg_total_size(min_ifinfo_dump_size);
c7ac8679
GR
2900}
2901
42bad1da 2902static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
1da177e4
LT
2903{
2904 int idx;
2905 int s_idx = cb->family;
2906
2907 if (s_idx == 0)
2908 s_idx = 1;
6853dd48 2909
25239cee 2910 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
1da177e4 2911 int type = cb->nlh->nlmsg_type-RTM_BASE;
6853dd48
FW
2912 struct rtnl_link *handlers;
2913 rtnl_dumpit_func dumpit;
2914
1da177e4
LT
2915 if (idx < s_idx || idx == PF_PACKET)
2916 continue;
6853dd48
FW
2917
2918 handlers = rtnl_dereference(rtnl_msg_handlers[idx]);
2919 if (!handlers)
1da177e4 2920 continue;
6853dd48
FW
2921
2922 dumpit = READ_ONCE(handlers[type].dumpit);
2923 if (!dumpit)
2924 continue;
2925
0465277f 2926 if (idx > s_idx) {
1da177e4 2927 memset(&cb->args[0], 0, sizeof(cb->args));
0465277f
ND
2928 cb->prev_seq = 0;
2929 cb->seq = 0;
2930 }
6853dd48 2931 if (dumpit(skb, cb))
1da177e4
LT
2932 break;
2933 }
2934 cb->family = idx;
2935
2936 return skb->len;
2937}
2938
395eea6c 2939struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
3d3ea5af 2940 unsigned int change,
6621dd29 2941 u32 event, gfp_t flags, int *new_nsid)
1da177e4 2942{
c346dca1 2943 struct net *net = dev_net(dev);
1da177e4 2944 struct sk_buff *skb;
0ec6d3f4 2945 int err = -ENOBUFS;
c7ac8679 2946 size_t if_info_size;
1da177e4 2947
7f294054 2948 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
0ec6d3f4
TG
2949 if (skb == NULL)
2950 goto errout;
1da177e4 2951
6621dd29
ND
2952 err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0, event,
2953 new_nsid);
26932566
PM
2954 if (err < 0) {
2955 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
2956 WARN_ON(err == -EMSGSIZE);
2957 kfree_skb(skb);
2958 goto errout;
2959 }
395eea6c 2960 return skb;
0ec6d3f4
TG
2961errout:
2962 if (err < 0)
4b3da706 2963 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
395eea6c
MB
2964 return NULL;
2965}
2966
2967void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
2968{
2969 struct net *net = dev_net(dev);
2970
2971 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
2972}
2973
3d3ea5af
VY
2974static void rtmsg_ifinfo_event(int type, struct net_device *dev,
2975 unsigned int change, u32 event,
6621dd29 2976 gfp_t flags, int *new_nsid)
395eea6c
MB
2977{
2978 struct sk_buff *skb;
2979
ed2a80ab
ND
2980 if (dev->reg_state != NETREG_REGISTERED)
2981 return;
2982
6621dd29 2983 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid);
395eea6c
MB
2984 if (skb)
2985 rtmsg_ifinfo_send(skb, dev, flags);
1da177e4 2986}
3d3ea5af
VY
2987
2988void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
2989 gfp_t flags)
2990{
6621dd29 2991 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, NULL);
3d3ea5af 2992}
471cb5a3 2993EXPORT_SYMBOL(rtmsg_ifinfo);
1da177e4 2994
6621dd29
ND
2995void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
2996 gfp_t flags, int *new_nsid)
2997{
2998 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
2999 new_nsid);
3000}
3001
d83b0603
JF
3002static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
3003 struct net_device *dev,
1e53d5bb 3004 u8 *addr, u16 vid, u32 pid, u32 seq,
1c104a6b 3005 int type, unsigned int flags,
b3379041 3006 int nlflags, u16 ndm_state)
d83b0603
JF
3007{
3008 struct nlmsghdr *nlh;
3009 struct ndmsg *ndm;
3010
1c104a6b 3011 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
d83b0603
JF
3012 if (!nlh)
3013 return -EMSGSIZE;
3014
3015 ndm = nlmsg_data(nlh);
3016 ndm->ndm_family = AF_BRIDGE;
3017 ndm->ndm_pad1 = 0;
3018 ndm->ndm_pad2 = 0;
3019 ndm->ndm_flags = flags;
3020 ndm->ndm_type = 0;
3021 ndm->ndm_ifindex = dev->ifindex;
b3379041 3022 ndm->ndm_state = ndm_state;
d83b0603
JF
3023
3024 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
3025 goto nla_put_failure;
1e53d5bb
HS
3026 if (vid)
3027 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
3028 goto nla_put_failure;
d83b0603 3029
053c095a
JB
3030 nlmsg_end(skb, nlh);
3031 return 0;
d83b0603
JF
3032
3033nla_put_failure:
3034 nlmsg_cancel(skb, nlh);
3035 return -EMSGSIZE;
3036}
3037
3ff661c3
JF
3038static inline size_t rtnl_fdb_nlmsg_size(void)
3039{
f82ef3e1
SD
3040 return NLMSG_ALIGN(sizeof(struct ndmsg)) +
3041 nla_total_size(ETH_ALEN) + /* NDA_LLADDR */
3042 nla_total_size(sizeof(u16)) + /* NDA_VLAN */
3043 0;
3ff661c3
JF
3044}
3045
b3379041
HS
3046static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
3047 u16 ndm_state)
3ff661c3
JF
3048{
3049 struct net *net = dev_net(dev);
3050 struct sk_buff *skb;
3051 int err = -ENOBUFS;
3052
3053 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
3054 if (!skb)
3055 goto errout;
3056
1e53d5bb 3057 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
b3379041 3058 0, 0, type, NTF_SELF, 0, ndm_state);
3ff661c3
JF
3059 if (err < 0) {
3060 kfree_skb(skb);
3061 goto errout;
3062 }
3063
3064 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
3065 return;
3066errout:
3067 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
3068}
3069
090096bf
VY
3070/**
3071 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3072 */
3073int ndo_dflt_fdb_add(struct ndmsg *ndm,
3074 struct nlattr *tb[],
3075 struct net_device *dev,
f6f6424b 3076 const unsigned char *addr, u16 vid,
090096bf
VY
3077 u16 flags)
3078{
3079 int err = -EINVAL;
3080
3081 /* If aging addresses are supported device will need to
3082 * implement its own handler for this.
3083 */
3084 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
3085 pr_info("%s: FDB only supports static addresses\n", dev->name);
3086 return err;
3087 }
3088
65891fea
OG
3089 if (vid) {
3090 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
3091 return err;
3092 }
3093
090096bf
VY
3094 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3095 err = dev_uc_add_excl(dev, addr);
3096 else if (is_multicast_ether_addr(addr))
3097 err = dev_mc_add_excl(dev, addr);
3098
3099 /* Only return duplicate errors if NLM_F_EXCL is set */
3100 if (err == -EEXIST && !(flags & NLM_F_EXCL))
3101 err = 0;
3102
3103 return err;
3104}
3105EXPORT_SYMBOL(ndo_dflt_fdb_add);
3106
b88d12e4
FW
3107static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
3108 struct netlink_ext_ack *extack)
f6f6424b
JP
3109{
3110 u16 vid = 0;
3111
3112 if (vlan_attr) {
3113 if (nla_len(vlan_attr) != sizeof(u16)) {
b88d12e4 3114 NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
f6f6424b
JP
3115 return -EINVAL;
3116 }
3117
3118 vid = nla_get_u16(vlan_attr);
3119
3120 if (!vid || vid >= VLAN_VID_MASK) {
b88d12e4 3121 NL_SET_ERR_MSG(extack, "invalid vlan id");
f6f6424b
JP
3122 return -EINVAL;
3123 }
3124 }
3125 *p_vid = vid;
3126 return 0;
3127}
3128
c21ef3e3
DA
3129static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
3130 struct netlink_ext_ack *extack)
77162022
JF
3131{
3132 struct net *net = sock_net(skb->sk);
77162022
JF
3133 struct ndmsg *ndm;
3134 struct nlattr *tb[NDA_MAX+1];
3135 struct net_device *dev;
3136 u8 *addr;
f6f6424b 3137 u16 vid;
77162022
JF
3138 int err;
3139
c21ef3e3 3140 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
77162022
JF
3141 if (err < 0)
3142 return err;
3143
3144 ndm = nlmsg_data(nlh);
3145 if (ndm->ndm_ifindex == 0) {
b88d12e4 3146 NL_SET_ERR_MSG(extack, "invalid ifindex");
77162022
JF
3147 return -EINVAL;
3148 }
3149
3150 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3151 if (dev == NULL) {
b88d12e4 3152 NL_SET_ERR_MSG(extack, "unknown ifindex");
77162022
JF
3153 return -ENODEV;
3154 }
3155
3156 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
b88d12e4 3157 NL_SET_ERR_MSG(extack, "invalid address");
77162022
JF
3158 return -EINVAL;
3159 }
3160
3161 addr = nla_data(tb[NDA_LLADDR]);
77162022 3162
b88d12e4 3163 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
f6f6424b
JP
3164 if (err)
3165 return err;
3166
77162022
JF
3167 err = -EOPNOTSUPP;
3168
3169 /* Support fdb on master device the net/bridge default case */
3170 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3171 (dev->priv_flags & IFF_BRIDGE_PORT)) {
898e5061
JP
3172 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3173 const struct net_device_ops *ops = br_dev->netdev_ops;
3174
f6f6424b
JP
3175 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
3176 nlh->nlmsg_flags);
77162022
JF
3177 if (err)
3178 goto out;
3179 else
3180 ndm->ndm_flags &= ~NTF_MASTER;
3181 }
3182
3183 /* Embedded bridge, macvlan, and any other device support */
090096bf
VY
3184 if ((ndm->ndm_flags & NTF_SELF)) {
3185 if (dev->netdev_ops->ndo_fdb_add)
3186 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
f6f6424b 3187 vid,
090096bf
VY
3188 nlh->nlmsg_flags);
3189 else
f6f6424b 3190 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
090096bf 3191 nlh->nlmsg_flags);
77162022 3192
3ff661c3 3193 if (!err) {
b3379041
HS
3194 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
3195 ndm->ndm_state);
77162022 3196 ndm->ndm_flags &= ~NTF_SELF;
3ff661c3 3197 }
77162022
JF
3198 }
3199out:
3200 return err;
3201}
3202
090096bf
VY
3203/**
3204 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3205 */
3206int ndo_dflt_fdb_del(struct ndmsg *ndm,
3207 struct nlattr *tb[],
3208 struct net_device *dev,
f6f6424b 3209 const unsigned char *addr, u16 vid)
090096bf 3210{
c8a89c4a 3211 int err = -EINVAL;
090096bf
VY
3212
3213 /* If aging addresses are supported device will need to
3214 * implement its own handler for this.
3215 */
64535993 3216 if (!(ndm->ndm_state & NUD_PERMANENT)) {
090096bf 3217 pr_info("%s: FDB only supports static addresses\n", dev->name);
c8a89c4a 3218 return err;
090096bf
VY
3219 }
3220
3221 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3222 err = dev_uc_del(dev, addr);
3223 else if (is_multicast_ether_addr(addr))
3224 err = dev_mc_del(dev, addr);
090096bf
VY
3225
3226 return err;
3227}
3228EXPORT_SYMBOL(ndo_dflt_fdb_del);
3229
c21ef3e3
DA
3230static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
3231 struct netlink_ext_ack *extack)
77162022
JF
3232{
3233 struct net *net = sock_net(skb->sk);
3234 struct ndmsg *ndm;
1690be63 3235 struct nlattr *tb[NDA_MAX+1];
77162022
JF
3236 struct net_device *dev;
3237 int err = -EINVAL;
3238 __u8 *addr;
f6f6424b 3239 u16 vid;
77162022 3240
90f62cf3 3241 if (!netlink_capable(skb, CAP_NET_ADMIN))
1690be63
VY
3242 return -EPERM;
3243
c21ef3e3 3244 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
1690be63
VY
3245 if (err < 0)
3246 return err;
77162022
JF
3247
3248 ndm = nlmsg_data(nlh);
3249 if (ndm->ndm_ifindex == 0) {
b88d12e4 3250 NL_SET_ERR_MSG(extack, "invalid ifindex");
77162022
JF
3251 return -EINVAL;
3252 }
3253
3254 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3255 if (dev == NULL) {
b88d12e4 3256 NL_SET_ERR_MSG(extack, "unknown ifindex");
77162022
JF
3257 return -ENODEV;
3258 }
3259
1690be63 3260 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
b88d12e4 3261 NL_SET_ERR_MSG(extack, "invalid address");
1690be63
VY
3262 return -EINVAL;
3263 }
3264
3265 addr = nla_data(tb[NDA_LLADDR]);
77162022 3266
b88d12e4 3267 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
f6f6424b
JP
3268 if (err)
3269 return err;
3270
77162022
JF
3271 err = -EOPNOTSUPP;
3272
3273 /* Support fdb on master device the net/bridge default case */
3274 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3275 (dev->priv_flags & IFF_BRIDGE_PORT)) {
898e5061
JP
3276 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3277 const struct net_device_ops *ops = br_dev->netdev_ops;
77162022 3278
898e5061 3279 if (ops->ndo_fdb_del)
f6f6424b 3280 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
77162022
JF
3281
3282 if (err)
3283 goto out;
3284 else
3285 ndm->ndm_flags &= ~NTF_MASTER;
3286 }
3287
3288 /* Embedded bridge, macvlan, and any other device support */
090096bf
VY
3289 if (ndm->ndm_flags & NTF_SELF) {
3290 if (dev->netdev_ops->ndo_fdb_del)
f6f6424b
JP
3291 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
3292 vid);
090096bf 3293 else
f6f6424b 3294 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
77162022 3295
3ff661c3 3296 if (!err) {
b3379041
HS
3297 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
3298 ndm->ndm_state);
77162022 3299 ndm->ndm_flags &= ~NTF_SELF;
3ff661c3 3300 }
77162022
JF
3301 }
3302out:
3303 return err;
3304}
3305
d83b0603
JF
3306static int nlmsg_populate_fdb(struct sk_buff *skb,
3307 struct netlink_callback *cb,
3308 struct net_device *dev,
3309 int *idx,
3310 struct netdev_hw_addr_list *list)
3311{
3312 struct netdev_hw_addr *ha;
3313 int err;
15e47304 3314 u32 portid, seq;
d83b0603 3315
15e47304 3316 portid = NETLINK_CB(cb->skb).portid;
d83b0603
JF
3317 seq = cb->nlh->nlmsg_seq;
3318
3319 list_for_each_entry(ha, &list->list, list) {
d297653d 3320 if (*idx < cb->args[2])
d83b0603
JF
3321 goto skip;
3322
1e53d5bb 3323 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
a7a558fe 3324 portid, seq,
1c104a6b 3325 RTM_NEWNEIGH, NTF_SELF,
b3379041 3326 NLM_F_MULTI, NUD_PERMANENT);
d83b0603
JF
3327 if (err < 0)
3328 return err;
3329skip:
3330 *idx += 1;
3331 }
3332 return 0;
3333}
3334
3335/**
2c53040f 3336 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
d83b0603
JF
3337 * @nlh: netlink message header
3338 * @dev: netdevice
3339 *
3340 * Default netdevice operation to dump the existing unicast address list.
91f3e7b1 3341 * Returns number of addresses from list put in skb.
d83b0603
JF
3342 */
3343int ndo_dflt_fdb_dump(struct sk_buff *skb,
3344 struct netlink_callback *cb,
3345 struct net_device *dev,
5d5eacb3 3346 struct net_device *filter_dev,
d297653d 3347 int *idx)
d83b0603
JF
3348{
3349 int err;
3350
3351 netif_addr_lock_bh(dev);
d297653d 3352 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
d83b0603
JF
3353 if (err)
3354 goto out;
2934c9db 3355 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
d83b0603
JF
3356out:
3357 netif_addr_unlock_bh(dev);
d297653d 3358 return err;
d83b0603
JF
3359}
3360EXPORT_SYMBOL(ndo_dflt_fdb_dump);
3361
77162022
JF
3362static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
3363{
77162022 3364 struct net_device *dev;
5e6d2435 3365 struct nlattr *tb[IFLA_MAX+1];
5e6d2435
JHS
3366 struct net_device *br_dev = NULL;
3367 const struct net_device_ops *ops = NULL;
3368 const struct net_device_ops *cops = NULL;
3369 struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
3370 struct net *net = sock_net(skb->sk);
d297653d 3371 struct hlist_head *head;
5e6d2435
JHS
3372 int brport_idx = 0;
3373 int br_idx = 0;
d297653d
RP
3374 int h, s_h;
3375 int idx = 0, s_idx;
3376 int err = 0;
3377 int fidx = 0;
5e6d2435 3378
0ff50e83
AP
3379 err = nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb,
3380 IFLA_MAX, ifla_policy, NULL);
3381 if (err < 0) {
3382 return -EINVAL;
3383 } else if (err == 0) {
5e6d2435
JHS
3384 if (tb[IFLA_MASTER])
3385 br_idx = nla_get_u32(tb[IFLA_MASTER]);
3386 }
3387
3388 brport_idx = ifm->ifi_index;
3389
3390 if (br_idx) {
3391 br_dev = __dev_get_by_index(net, br_idx);
3392 if (!br_dev)
3393 return -ENODEV;
3394
3395 ops = br_dev->netdev_ops;
5e6d2435
JHS
3396 }
3397
d297653d
RP
3398 s_h = cb->args[0];
3399 s_idx = cb->args[1];
5e6d2435 3400
d297653d
RP
3401 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
3402 idx = 0;
3403 head = &net->dev_index_head[h];
3404 hlist_for_each_entry(dev, head, index_hlist) {
5e6d2435 3405
d297653d 3406 if (brport_idx && (dev->ifindex != brport_idx))
5e6d2435
JHS
3407 continue;
3408
d297653d
RP
3409 if (!br_idx) { /* user did not specify a specific bridge */
3410 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3411 br_dev = netdev_master_upper_dev_get(dev);
3412 cops = br_dev->netdev_ops;
3413 }
3414 } else {
3415 if (dev != br_dev &&
3416 !(dev->priv_flags & IFF_BRIDGE_PORT))
3417 continue;
5e6d2435 3418
d297653d
RP
3419 if (br_dev != netdev_master_upper_dev_get(dev) &&
3420 !(dev->priv_flags & IFF_EBRIDGE))
3421 continue;
3422 cops = ops;
3423 }
77162022 3424
d297653d
RP
3425 if (idx < s_idx)
3426 goto cont;
77162022 3427
d297653d
RP
3428 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3429 if (cops && cops->ndo_fdb_dump) {
3430 err = cops->ndo_fdb_dump(skb, cb,
3431 br_dev, dev,
3432 &fidx);
3433 if (err == -EMSGSIZE)
3434 goto out;
3435 }
3436 }
5e6d2435 3437
d297653d
RP
3438 if (dev->netdev_ops->ndo_fdb_dump)
3439 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
3440 dev, NULL,
3441 &fidx);
3442 else
3443 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
3444 &fidx);
3445 if (err == -EMSGSIZE)
3446 goto out;
3447
3448 cops = NULL;
3449
3450 /* reset fdb offset to 0 for rest of the interfaces */
3451 cb->args[2] = 0;
3452 fidx = 0;
3453cont:
3454 idx++;
3455 }
77162022 3456 }
77162022 3457
d297653d
RP
3458out:
3459 cb->args[0] = h;
3460 cb->args[1] = idx;
3461 cb->args[2] = fidx;
3462
77162022
JF
3463 return skb->len;
3464}
3465
2c3c031c
SF
3466static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
3467 unsigned int attrnum, unsigned int flag)
3468{
3469 if (mask & flag)
3470 return nla_put_u8(skb, attrnum, !!(flags & flag));
3471 return 0;
3472}
3473
815cccbf 3474int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
2c3c031c 3475 struct net_device *dev, u16 mode,
7d4f8d87
SF
3476 u32 flags, u32 mask, int nlflags,
3477 u32 filter_mask,
3478 int (*vlan_fill)(struct sk_buff *skb,
3479 struct net_device *dev,
3480 u32 filter_mask))
815cccbf
JF
3481{
3482 struct nlmsghdr *nlh;
3483 struct ifinfomsg *ifm;
3484 struct nlattr *br_afspec;
2c3c031c 3485 struct nlattr *protinfo;
815cccbf 3486 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
898e5061 3487 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
7d4f8d87 3488 int err = 0;
815cccbf 3489
46c264da 3490 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
815cccbf
JF
3491 if (nlh == NULL)
3492 return -EMSGSIZE;
3493
3494 ifm = nlmsg_data(nlh);
3495 ifm->ifi_family = AF_BRIDGE;
3496 ifm->__ifi_pad = 0;
3497 ifm->ifi_type = dev->type;
3498 ifm->ifi_index = dev->ifindex;
3499 ifm->ifi_flags = dev_get_flags(dev);
3500 ifm->ifi_change = 0;
3501
3502
3503 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
3504 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
3505 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
898e5061
JP
3506 (br_dev &&
3507 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
815cccbf
JF
3508 (dev->addr_len &&
3509 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
a54acb3a
ND
3510 (dev->ifindex != dev_get_iflink(dev) &&
3511 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
815cccbf
JF
3512 goto nla_put_failure;
3513
3514 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
3515 if (!br_afspec)
3516 goto nla_put_failure;
3517
1d460b98 3518 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
815cccbf
JF
3519 nla_nest_cancel(skb, br_afspec);
3520 goto nla_put_failure;
3521 }
1d460b98
RP
3522
3523 if (mode != BRIDGE_MODE_UNDEF) {
3524 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
3525 nla_nest_cancel(skb, br_afspec);
3526 goto nla_put_failure;
3527 }
3528 }
7d4f8d87
SF
3529 if (vlan_fill) {
3530 err = vlan_fill(skb, dev, filter_mask);
3531 if (err) {
3532 nla_nest_cancel(skb, br_afspec);
3533 goto nla_put_failure;
3534 }
3535 }
815cccbf
JF
3536 nla_nest_end(skb, br_afspec);
3537
2c3c031c
SF
3538 protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
3539 if (!protinfo)
3540 goto nla_put_failure;
3541
3542 if (brport_nla_put_flag(skb, flags, mask,
3543 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
3544 brport_nla_put_flag(skb, flags, mask,
3545 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
3546 brport_nla_put_flag(skb, flags, mask,
3547 IFLA_BRPORT_FAST_LEAVE,
3548 BR_MULTICAST_FAST_LEAVE) ||
3549 brport_nla_put_flag(skb, flags, mask,
3550 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
3551 brport_nla_put_flag(skb, flags, mask,
3552 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
3553 brport_nla_put_flag(skb, flags, mask,
3554 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
3555 brport_nla_put_flag(skb, flags, mask,
3556 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
3557 brport_nla_put_flag(skb, flags, mask,
3558 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
3559 nla_nest_cancel(skb, protinfo);
3560 goto nla_put_failure;
3561 }
3562
3563 nla_nest_end(skb, protinfo);
3564
053c095a
JB
3565 nlmsg_end(skb, nlh);
3566 return 0;
815cccbf
JF
3567nla_put_failure:
3568 nlmsg_cancel(skb, nlh);
7d4f8d87 3569 return err ? err : -EMSGSIZE;
815cccbf 3570}
7d4f8d87 3571EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
815cccbf 3572
e5a55a89
JF
3573static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
3574{
3575 struct net *net = sock_net(skb->sk);
3576 struct net_device *dev;
3577 int idx = 0;
3578 u32 portid = NETLINK_CB(cb->skb).portid;
3579 u32 seq = cb->nlh->nlmsg_seq;
6cbdceeb 3580 u32 filter_mask = 0;
d64f69b0 3581 int err;
6cbdceeb 3582
aa68c20f
TG
3583 if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
3584 struct nlattr *extfilt;
3585
3586 extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
3587 IFLA_EXT_MASK);
3588 if (extfilt) {
3589 if (nla_len(extfilt) < sizeof(filter_mask))
3590 return -EINVAL;
3591
3592 filter_mask = nla_get_u32(extfilt);
3593 }
3594 }
e5a55a89
JF
3595
3596 rcu_read_lock();
3597 for_each_netdev_rcu(net, dev) {
3598 const struct net_device_ops *ops = dev->netdev_ops;
898e5061 3599 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
e5a55a89 3600
898e5061 3601 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
d64f69b0
RP
3602 if (idx >= cb->args[0]) {
3603 err = br_dev->netdev_ops->ndo_bridge_getlink(
3604 skb, portid, seq, dev,
3605 filter_mask, NLM_F_MULTI);
f6c5775f
DA
3606 if (err < 0 && err != -EOPNOTSUPP) {
3607 if (likely(skb->len))
3608 break;
3609
3610 goto out_err;
3611 }
d64f69b0 3612 }
25b1e679 3613 idx++;
e5a55a89
JF
3614 }
3615
3616 if (ops->ndo_bridge_getlink) {
d64f69b0
RP
3617 if (idx >= cb->args[0]) {
3618 err = ops->ndo_bridge_getlink(skb, portid,
3619 seq, dev,
3620 filter_mask,
3621 NLM_F_MULTI);
f6c5775f
DA
3622 if (err < 0 && err != -EOPNOTSUPP) {
3623 if (likely(skb->len))
3624 break;
3625
3626 goto out_err;
3627 }
d64f69b0 3628 }
25b1e679 3629 idx++;
e5a55a89
JF
3630 }
3631 }
f6c5775f
DA
3632 err = skb->len;
3633out_err:
e5a55a89
JF
3634 rcu_read_unlock();
3635 cb->args[0] = idx;
3636
f6c5775f 3637 return err;
e5a55a89
JF
3638}
3639
2469ffd7
JF
3640static inline size_t bridge_nlmsg_size(void)
3641{
3642 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3643 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3644 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3645 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
3646 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
3647 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
3648 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
3649 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
3650 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
3651 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
3652 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
3653}
3654
02dba438 3655static int rtnl_bridge_notify(struct net_device *dev)
2469ffd7
JF
3656{
3657 struct net *net = dev_net(dev);
2469ffd7
JF
3658 struct sk_buff *skb;
3659 int err = -EOPNOTSUPP;
3660
02dba438
RP
3661 if (!dev->netdev_ops->ndo_bridge_getlink)
3662 return 0;
3663
2469ffd7
JF
3664 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
3665 if (!skb) {
3666 err = -ENOMEM;
3667 goto errout;
3668 }
3669
46c264da 3670 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
02dba438
RP
3671 if (err < 0)
3672 goto errout;
2469ffd7 3673
59ccaaaa
RP
3674 if (!skb->len)
3675 goto errout;
3676
2469ffd7
JF
3677 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
3678 return 0;
3679errout:
3680 WARN_ON(err == -EMSGSIZE);
3681 kfree_skb(skb);
59ccaaaa
RP
3682 if (err)
3683 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
2469ffd7
JF
3684 return err;
3685}
3686
c21ef3e3
DA
3687static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3688 struct netlink_ext_ack *extack)
e5a55a89
JF
3689{
3690 struct net *net = sock_net(skb->sk);
3691 struct ifinfomsg *ifm;
3692 struct net_device *dev;
2469ffd7
JF
3693 struct nlattr *br_spec, *attr = NULL;
3694 int rem, err = -EOPNOTSUPP;
4de8b413 3695 u16 flags = 0;
c38e01b8 3696 bool have_flags = false;
e5a55a89
JF
3697
3698 if (nlmsg_len(nlh) < sizeof(*ifm))
3699 return -EINVAL;
3700
3701 ifm = nlmsg_data(nlh);
3702 if (ifm->ifi_family != AF_BRIDGE)
3703 return -EPFNOSUPPORT;
3704
3705 dev = __dev_get_by_index(net, ifm->ifi_index);
3706 if (!dev) {
b88d12e4 3707 NL_SET_ERR_MSG(extack, "unknown ifindex");
e5a55a89
JF
3708 return -ENODEV;
3709 }
3710
2469ffd7
JF
3711 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3712 if (br_spec) {
3713 nla_for_each_nested(attr, br_spec, rem) {
3714 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
6e8d1c55
TG
3715 if (nla_len(attr) < sizeof(flags))
3716 return -EINVAL;
3717
c38e01b8 3718 have_flags = true;
2469ffd7
JF
3719 flags = nla_get_u16(attr);
3720 break;
3721 }
3722 }
3723 }
3724
3725 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
898e5061
JP
3726 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3727
3728 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
2469ffd7
JF
3729 err = -EOPNOTSUPP;
3730 goto out;
3731 }
3732
add511b3 3733 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
e5a55a89
JF
3734 if (err)
3735 goto out;
2469ffd7
JF
3736
3737 flags &= ~BRIDGE_FLAGS_MASTER;
e5a55a89
JF
3738 }
3739
2469ffd7
JF
3740 if ((flags & BRIDGE_FLAGS_SELF)) {
3741 if (!dev->netdev_ops->ndo_bridge_setlink)
3742 err = -EOPNOTSUPP;
3743 else
add511b3
RP
3744 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
3745 flags);
02dba438 3746 if (!err) {
2469ffd7 3747 flags &= ~BRIDGE_FLAGS_SELF;
02dba438
RP
3748
3749 /* Generate event to notify upper layer of bridge
3750 * change
3751 */
3752 err = rtnl_bridge_notify(dev);
3753 }
2469ffd7 3754 }
e5a55a89 3755
c38e01b8 3756 if (have_flags)
2469ffd7 3757 memcpy(nla_data(attr), &flags, sizeof(flags));
e5a55a89
JF
3758out:
3759 return err;
3760}
3761
c21ef3e3
DA
3762static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
3763 struct netlink_ext_ack *extack)
407af329
VY
3764{
3765 struct net *net = sock_net(skb->sk);
3766 struct ifinfomsg *ifm;
3767 struct net_device *dev;
3768 struct nlattr *br_spec, *attr = NULL;
3769 int rem, err = -EOPNOTSUPP;
4de8b413 3770 u16 flags = 0;
407af329
VY
3771 bool have_flags = false;
3772
3773 if (nlmsg_len(nlh) < sizeof(*ifm))
3774 return -EINVAL;
3775
3776 ifm = nlmsg_data(nlh);
3777 if (ifm->ifi_family != AF_BRIDGE)
3778 return -EPFNOSUPPORT;
3779
3780 dev = __dev_get_by_index(net, ifm->ifi_index);
3781 if (!dev) {
b88d12e4 3782 NL_SET_ERR_MSG(extack, "unknown ifindex");
407af329
VY
3783 return -ENODEV;
3784 }
3785
3786 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3787 if (br_spec) {
3788 nla_for_each_nested(attr, br_spec, rem) {
3789 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
6e8d1c55
TG
3790 if (nla_len(attr) < sizeof(flags))
3791 return -EINVAL;
3792
407af329
VY
3793 have_flags = true;
3794 flags = nla_get_u16(attr);
3795 break;
3796 }
3797 }
3798 }
3799
407af329
VY
3800 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3801 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3802
3803 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
3804 err = -EOPNOTSUPP;
3805 goto out;
3806 }
3807
add511b3 3808 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
407af329
VY
3809 if (err)
3810 goto out;
3811
3812 flags &= ~BRIDGE_FLAGS_MASTER;
3813 }
3814
3815 if ((flags & BRIDGE_FLAGS_SELF)) {
3816 if (!dev->netdev_ops->ndo_bridge_dellink)
3817 err = -EOPNOTSUPP;
3818 else
add511b3
RP
3819 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
3820 flags);
407af329 3821
02dba438 3822 if (!err) {
407af329 3823 flags &= ~BRIDGE_FLAGS_SELF;
02dba438
RP
3824
3825 /* Generate event to notify upper layer of bridge
3826 * change
3827 */
3828 err = rtnl_bridge_notify(dev);
3829 }
407af329
VY
3830 }
3831
3832 if (have_flags)
3833 memcpy(nla_data(attr), &flags, sizeof(flags));
407af329
VY
3834out:
3835 return err;
3836}
3837
e8872a25
NA
3838static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
3839{
3840 return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
3841 (!idxattr || idxattr == attrid);
3842}
3843
69ae6ad2
NF
3844#define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
3845static int rtnl_get_offload_stats_attr_size(int attr_id)
3846{
3847 switch (attr_id) {
3848 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
3849 return sizeof(struct rtnl_link_stats64);
3850 }
3851
3852 return 0;
3853}
3854
3855static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
3856 int *prividx)
3857{
3858 struct nlattr *attr = NULL;
3859 int attr_id, size;
3860 void *attr_data;
3861 int err;
3862
3863 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
3864 dev->netdev_ops->ndo_get_offload_stats))
3865 return -ENODATA;
3866
3867 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
3868 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
3869 if (attr_id < *prividx)
3870 continue;
3871
3872 size = rtnl_get_offload_stats_attr_size(attr_id);
3873 if (!size)
3874 continue;
3875
3df5b3c6 3876 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
69ae6ad2
NF
3877 continue;
3878
3879 attr = nla_reserve_64bit(skb, attr_id, size,
3880 IFLA_OFFLOAD_XSTATS_UNSPEC);
3881 if (!attr)
3882 goto nla_put_failure;
3883
3884 attr_data = nla_data(attr);
3885 memset(attr_data, 0, size);
3886 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
3887 attr_data);
3888 if (err)
3889 goto get_offload_stats_failure;
3890 }
3891
3892 if (!attr)
3893 return -ENODATA;
3894
3895 *prividx = 0;
3896 return 0;
3897
3898nla_put_failure:
3899 err = -EMSGSIZE;
3900get_offload_stats_failure:
3901 *prividx = attr_id;
3902 return err;
3903}
3904
3905static int rtnl_get_offload_stats_size(const struct net_device *dev)
3906{
3907 int nla_size = 0;
3908 int attr_id;
3909 int size;
3910
3911 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
3912 dev->netdev_ops->ndo_get_offload_stats))
3913 return 0;
3914
3915 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
3916 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
3df5b3c6 3917 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
69ae6ad2
NF
3918 continue;
3919 size = rtnl_get_offload_stats_attr_size(attr_id);
3920 nla_size += nla_total_size_64bit(size);
3921 }
3922
3923 if (nla_size != 0)
3924 nla_size += nla_total_size(0);
3925
3926 return nla_size;
3927}
3928
10c9ead9
RP
3929static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
3930 int type, u32 pid, u32 seq, u32 change,
e8872a25
NA
3931 unsigned int flags, unsigned int filter_mask,
3932 int *idxattr, int *prividx)
10c9ead9
RP
3933{
3934 struct if_stats_msg *ifsm;
3935 struct nlmsghdr *nlh;
3936 struct nlattr *attr;
e8872a25 3937 int s_prividx = *prividx;
69ae6ad2 3938 int err;
10c9ead9
RP
3939
3940 ASSERT_RTNL();
3941
3942 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
3943 if (!nlh)
3944 return -EMSGSIZE;
3945
3946 ifsm = nlmsg_data(nlh);
ce024f42
NA
3947 ifsm->family = PF_UNSPEC;
3948 ifsm->pad1 = 0;
3949 ifsm->pad2 = 0;
10c9ead9
RP
3950 ifsm->ifindex = dev->ifindex;
3951 ifsm->filter_mask = filter_mask;
3952
e8872a25 3953 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
10c9ead9 3954 struct rtnl_link_stats64 *sp;
10c9ead9 3955
58414d32
ND
3956 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
3957 sizeof(struct rtnl_link_stats64),
3958 IFLA_STATS_UNSPEC);
10c9ead9
RP
3959 if (!attr)
3960 goto nla_put_failure;
3961
3962 sp = nla_data(attr);
3963 dev_get_stats(dev, sp);
3964 }
3965
97a47fac
NA
3966 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
3967 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
3968
3969 if (ops && ops->fill_linkxstats) {
97a47fac
NA
3970 *idxattr = IFLA_STATS_LINK_XSTATS;
3971 attr = nla_nest_start(skb,
3972 IFLA_STATS_LINK_XSTATS);
3973 if (!attr)
3974 goto nla_put_failure;
3975
80e73cc5
NA
3976 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
3977 nla_nest_end(skb, attr);
3978 if (err)
3979 goto nla_put_failure;
3980 *idxattr = 0;
3981 }
3982 }
3983
3984 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
3985 *idxattr)) {
3986 const struct rtnl_link_ops *ops = NULL;
3987 const struct net_device *master;
3988
3989 master = netdev_master_upper_dev_get(dev);
3990 if (master)
3991 ops = master->rtnl_link_ops;
3992 if (ops && ops->fill_linkxstats) {
80e73cc5
NA
3993 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
3994 attr = nla_nest_start(skb,
3995 IFLA_STATS_LINK_XSTATS_SLAVE);
3996 if (!attr)
3997 goto nla_put_failure;
3998
3999 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
97a47fac
NA
4000 nla_nest_end(skb, attr);
4001 if (err)
4002 goto nla_put_failure;
4003 *idxattr = 0;
4004 }
4005 }
4006
69ae6ad2
NF
4007 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
4008 *idxattr)) {
4009 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
4010 attr = nla_nest_start(skb, IFLA_STATS_LINK_OFFLOAD_XSTATS);
4011 if (!attr)
4012 goto nla_put_failure;
4013
4014 err = rtnl_get_offload_stats(skb, dev, prividx);
4015 if (err == -ENODATA)
4016 nla_nest_cancel(skb, attr);
4017 else
4018 nla_nest_end(skb, attr);
4019
4020 if (err && err != -ENODATA)
4021 goto nla_put_failure;
4022 *idxattr = 0;
4023 }
4024
aefb4d4a
RS
4025 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
4026 struct rtnl_af_ops *af_ops;
4027
4028 *idxattr = IFLA_STATS_AF_SPEC;
4029 attr = nla_nest_start(skb, IFLA_STATS_AF_SPEC);
4030 if (!attr)
4031 goto nla_put_failure;
4032
5fa85a09
FW
4033 rcu_read_lock();
4034 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
aefb4d4a
RS
4035 if (af_ops->fill_stats_af) {
4036 struct nlattr *af;
4037 int err;
4038
4039 af = nla_nest_start(skb, af_ops->family);
5fa85a09
FW
4040 if (!af) {
4041 rcu_read_unlock();
aefb4d4a 4042 goto nla_put_failure;
5fa85a09 4043 }
aefb4d4a
RS
4044 err = af_ops->fill_stats_af(skb, dev);
4045
5fa85a09 4046 if (err == -ENODATA) {
aefb4d4a 4047 nla_nest_cancel(skb, af);
5fa85a09
FW
4048 } else if (err < 0) {
4049 rcu_read_unlock();
aefb4d4a 4050 goto nla_put_failure;
5fa85a09 4051 }
aefb4d4a
RS
4052
4053 nla_nest_end(skb, af);
4054 }
4055 }
5fa85a09 4056 rcu_read_unlock();
aefb4d4a
RS
4057
4058 nla_nest_end(skb, attr);
4059
4060 *idxattr = 0;
4061 }
4062
10c9ead9
RP
4063 nlmsg_end(skb, nlh);
4064
4065 return 0;
4066
4067nla_put_failure:
e8872a25
NA
4068 /* not a multi message or no progress mean a real error */
4069 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
4070 nlmsg_cancel(skb, nlh);
4071 else
4072 nlmsg_end(skb, nlh);
10c9ead9
RP
4073
4074 return -EMSGSIZE;
4075}
4076
10c9ead9
RP
4077static size_t if_nlmsg_stats_size(const struct net_device *dev,
4078 u32 filter_mask)
4079{
4080 size_t size = 0;
4081
e8872a25 4082 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
10c9ead9
RP
4083 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
4084
97a47fac
NA
4085 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
4086 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
80e73cc5 4087 int attr = IFLA_STATS_LINK_XSTATS;
97a47fac
NA
4088
4089 if (ops && ops->get_linkxstats_size) {
80e73cc5
NA
4090 size += nla_total_size(ops->get_linkxstats_size(dev,
4091 attr));
97a47fac
NA
4092 /* for IFLA_STATS_LINK_XSTATS */
4093 size += nla_total_size(0);
4094 }
4095 }
4096
80e73cc5
NA
4097 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
4098 struct net_device *_dev = (struct net_device *)dev;
4099 const struct rtnl_link_ops *ops = NULL;
4100 const struct net_device *master;
4101
4102 /* netdev_master_upper_dev_get can't take const */
4103 master = netdev_master_upper_dev_get(_dev);
4104 if (master)
4105 ops = master->rtnl_link_ops;
4106 if (ops && ops->get_linkxstats_size) {
4107 int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
4108
4109 size += nla_total_size(ops->get_linkxstats_size(dev,
4110 attr));
4111 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
4112 size += nla_total_size(0);
4113 }
4114 }
4115
69ae6ad2
NF
4116 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
4117 size += rtnl_get_offload_stats_size(dev);
4118
aefb4d4a
RS
4119 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
4120 struct rtnl_af_ops *af_ops;
4121
4122 /* for IFLA_STATS_AF_SPEC */
4123 size += nla_total_size(0);
4124
5fa85a09
FW
4125 rcu_read_lock();
4126 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
aefb4d4a
RS
4127 if (af_ops->get_stats_af_size) {
4128 size += nla_total_size(
4129 af_ops->get_stats_af_size(dev));
4130
4131 /* for AF_* */
4132 size += nla_total_size(0);
4133 }
4134 }
5fa85a09 4135 rcu_read_unlock();
aefb4d4a
RS
4136 }
4137
10c9ead9
RP
4138 return size;
4139}
4140
c21ef3e3
DA
4141static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
4142 struct netlink_ext_ack *extack)
10c9ead9
RP
4143{
4144 struct net *net = sock_net(skb->sk);
10c9ead9 4145 struct net_device *dev = NULL;
e8872a25
NA
4146 int idxattr = 0, prividx = 0;
4147 struct if_stats_msg *ifsm;
10c9ead9
RP
4148 struct sk_buff *nskb;
4149 u32 filter_mask;
4150 int err;
4151
4775cc1f
MK
4152 if (nlmsg_len(nlh) < sizeof(*ifsm))
4153 return -EINVAL;
4154
10c9ead9
RP
4155 ifsm = nlmsg_data(nlh);
4156 if (ifsm->ifindex > 0)
4157 dev = __dev_get_by_index(net, ifsm->ifindex);
4158 else
4159 return -EINVAL;
4160
4161 if (!dev)
4162 return -ENODEV;
4163
4164 filter_mask = ifsm->filter_mask;
4165 if (!filter_mask)
4166 return -EINVAL;
4167
4168 nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
4169 if (!nskb)
4170 return -ENOBUFS;
4171
4172 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
4173 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
e8872a25 4174 0, filter_mask, &idxattr, &prividx);
10c9ead9
RP
4175 if (err < 0) {
4176 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
4177 WARN_ON(err == -EMSGSIZE);
4178 kfree_skb(nskb);
4179 } else {
4180 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
4181 }
4182
4183 return err;
4184}
4185
4186static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
4187{
e8872a25 4188 int h, s_h, err, s_idx, s_idxattr, s_prividx;
10c9ead9 4189 struct net *net = sock_net(skb->sk);
e8872a25 4190 unsigned int flags = NLM_F_MULTI;
10c9ead9 4191 struct if_stats_msg *ifsm;
10c9ead9 4192 struct hlist_head *head;
e8872a25 4193 struct net_device *dev;
10c9ead9 4194 u32 filter_mask = 0;
e8872a25 4195 int idx = 0;
10c9ead9
RP
4196
4197 s_h = cb->args[0];
4198 s_idx = cb->args[1];
e8872a25
NA
4199 s_idxattr = cb->args[2];
4200 s_prividx = cb->args[3];
10c9ead9
RP
4201
4202 cb->seq = net->dev_base_seq;
4203
4775cc1f
MK
4204 if (nlmsg_len(cb->nlh) < sizeof(*ifsm))
4205 return -EINVAL;
4206
10c9ead9
RP
4207 ifsm = nlmsg_data(cb->nlh);
4208 filter_mask = ifsm->filter_mask;
4209 if (!filter_mask)
4210 return -EINVAL;
4211
4212 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4213 idx = 0;
4214 head = &net->dev_index_head[h];
4215 hlist_for_each_entry(dev, head, index_hlist) {
4216 if (idx < s_idx)
4217 goto cont;
4218 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
4219 NETLINK_CB(cb->skb).portid,
4220 cb->nlh->nlmsg_seq, 0,
e8872a25
NA
4221 flags, filter_mask,
4222 &s_idxattr, &s_prividx);
10c9ead9
RP
4223 /* If we ran out of room on the first message,
4224 * we're in trouble
4225 */
4226 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
4227
4228 if (err < 0)
4229 goto out;
e8872a25
NA
4230 s_prividx = 0;
4231 s_idxattr = 0;
10c9ead9
RP
4232 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
4233cont:
4234 idx++;
4235 }
4236 }
4237out:
e8872a25
NA
4238 cb->args[3] = s_prividx;
4239 cb->args[2] = s_idxattr;
10c9ead9
RP
4240 cb->args[1] = idx;
4241 cb->args[0] = h;
4242
4243 return skb->len;
4244}
4245
1da177e4
LT
4246/* Process one rtnetlink message. */
4247
2d4bc933
JB
4248static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
4249 struct netlink_ext_ack *extack)
1da177e4 4250{
3b1e0a65 4251 struct net *net = sock_net(skb->sk);
6853dd48
FW
4252 struct rtnl_link *handlers;
4253 int err = -EOPNOTSUPP;
e2849863 4254 rtnl_doit_func doit;
62256f98 4255 unsigned int flags;
617cfc75 4256 int kind;
1da177e4
LT
4257 int family;
4258 int type;
1da177e4 4259
1da177e4 4260 type = nlh->nlmsg_type;
1da177e4 4261 if (type > RTM_MAX)
038890fe 4262 return -EOPNOTSUPP;
1da177e4
LT
4263
4264 type -= RTM_BASE;
4265
4266 /* All the messages must have at least 1 byte length */
573ce260 4267 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
1da177e4
LT
4268 return 0;
4269
573ce260 4270 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
1da177e4
LT
4271 kind = type&3;
4272
90f62cf3 4273 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
1d00a4eb 4274 return -EPERM;
1da177e4 4275
5c2bb9b6 4276 if (family >= ARRAY_SIZE(rtnl_msg_handlers))
6853dd48
FW
4277 family = PF_UNSPEC;
4278
4279 rcu_read_lock();
4280 handlers = rcu_dereference(rtnl_msg_handlers[family]);
4281 if (!handlers) {
4282 family = PF_UNSPEC;
4283 handlers = rcu_dereference(rtnl_msg_handlers[family]);
4284 }
4285
b8f3ab42 4286 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
97c53cac 4287 struct sock *rtnl;
e2849863 4288 rtnl_dumpit_func dumpit;
c7ac8679 4289 u16 min_dump_alloc = 0;
1da177e4 4290
6853dd48
FW
4291 dumpit = READ_ONCE(handlers[type].dumpit);
4292 if (!dumpit) {
4293 family = PF_UNSPEC;
4294 handlers = rcu_dereference(rtnl_msg_handlers[PF_UNSPEC]);
4295 if (!handlers)
4296 goto err_unlock;
0cc09020 4297
6853dd48
FW
4298 dumpit = READ_ONCE(handlers[type].dumpit);
4299 if (!dumpit)
4300 goto err_unlock;
4301 }
e1fa6d21 4302
019a3169
FW
4303 refcount_inc(&rtnl_msg_handlers_ref[family]);
4304
5c2bb9b6 4305 if (type == RTM_GETLINK - RTM_BASE)
e1fa6d21 4306 min_dump_alloc = rtnl_calcit(skb, nlh);
9ac4a169 4307
6853dd48
FW
4308 rcu_read_unlock();
4309
97c53cac 4310 rtnl = net->rtnl;
80d326fa
PNA
4311 {
4312 struct netlink_dump_control c = {
4313 .dump = dumpit,
4314 .min_dump_alloc = min_dump_alloc,
4315 };
4316 err = netlink_dump_start(rtnl, skb, nlh, &c);
4317 }
019a3169 4318 refcount_dec(&rtnl_msg_handlers_ref[family]);
2907c35f 4319 return err;
1da177e4
LT
4320 }
4321
8caa38b5
FW
4322 doit = READ_ONCE(handlers[type].doit);
4323 if (!doit) {
4324 family = PF_UNSPEC;
4325 handlers = rcu_dereference(rtnl_msg_handlers[family]);
4326 }
4327
62256f98
FW
4328 flags = READ_ONCE(handlers[type].flags);
4329 if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
4330 refcount_inc(&rtnl_msg_handlers_ref[family]);
4331 doit = READ_ONCE(handlers[type].doit);
4332 rcu_read_unlock();
4333 if (doit)
4334 err = doit(skb, nlh, extack);
4335 refcount_dec(&rtnl_msg_handlers_ref[family]);
4336 return err;
4337 }
4338
6853dd48 4339 rcu_read_unlock();
1da177e4 4340
6853dd48
FW
4341 rtnl_lock();
4342 handlers = rtnl_dereference(rtnl_msg_handlers[family]);
4343 if (handlers) {
4344 doit = READ_ONCE(handlers[type].doit);
4345 if (doit)
4346 err = doit(skb, nlh, extack);
4347 }
0cc09020 4348 rtnl_unlock();
0cc09020
FW
4349 return err;
4350
4351err_unlock:
6853dd48 4352 rcu_read_unlock();
0cc09020 4353 return -EOPNOTSUPP;
1da177e4
LT
4354}
4355
cd40b7d3 4356static void rtnetlink_rcv(struct sk_buff *skb)
1da177e4 4357{
cd40b7d3 4358 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
1da177e4
LT
4359}
4360
5f729eaa
JG
4361static int rtnetlink_bind(struct net *net, int group)
4362{
4363 switch (group) {
4364 case RTNLGRP_IPV4_MROUTE_R:
4365 case RTNLGRP_IPV6_MROUTE_R:
4366 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
4367 return -EPERM;
4368 break;
4369 }
4370 return 0;
4371}
4372
1da177e4
LT
4373static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
4374{
351638e7 4375 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
e9dc8653 4376
1da177e4 4377 switch (event) {
5138e86f 4378 case NETDEV_REBOOT:
8a212589 4379 case NETDEV_CHANGEMTU:
3753654e 4380 case NETDEV_CHANGEADDR:
5138e86f
VY
4381 case NETDEV_CHANGENAME:
4382 case NETDEV_FEAT_CHANGE:
4383 case NETDEV_BONDING_FAILOVER:
e6e66594 4384 case NETDEV_POST_TYPE_CHANGE:
5138e86f 4385 case NETDEV_NOTIFY_PEERS:
dc709f37 4386 case NETDEV_CHANGEUPPER:
5138e86f 4387 case NETDEV_RESEND_IGMP:
5138e86f 4388 case NETDEV_CHANGEINFODATA:
ebdcf045 4389 case NETDEV_CHANGE_TX_QUEUE_LEN:
3d3ea5af 4390 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
6621dd29 4391 GFP_KERNEL, NULL);
1da177e4
LT
4392 break;
4393 default:
1da177e4
LT
4394 break;
4395 }
4396 return NOTIFY_DONE;
4397}
4398
4399static struct notifier_block rtnetlink_dev_notifier = {
4400 .notifier_call = rtnetlink_event,
4401};
4402
97c53cac 4403
2c8c1e72 4404static int __net_init rtnetlink_net_init(struct net *net)
97c53cac
DL
4405{
4406 struct sock *sk;
a31f2d17
PNA
4407 struct netlink_kernel_cfg cfg = {
4408 .groups = RTNLGRP_MAX,
4409 .input = rtnetlink_rcv,
4410 .cb_mutex = &rtnl_mutex,
9785e10a 4411 .flags = NL_CFG_F_NONROOT_RECV,
5f729eaa 4412 .bind = rtnetlink_bind,
a31f2d17
PNA
4413 };
4414
9f00d977 4415 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
97c53cac
DL
4416 if (!sk)
4417 return -ENOMEM;
97c53cac
DL
4418 net->rtnl = sk;
4419 return 0;
4420}
4421
2c8c1e72 4422static void __net_exit rtnetlink_net_exit(struct net *net)
97c53cac 4423{
775516bf
DL
4424 netlink_kernel_release(net->rtnl);
4425 net->rtnl = NULL;
97c53cac
DL
4426}
4427
4428static struct pernet_operations rtnetlink_net_ops = {
4429 .init = rtnetlink_net_init,
4430 .exit = rtnetlink_net_exit,
4431};
4432
1da177e4
LT
4433void __init rtnetlink_init(void)
4434{
d38a6512
FW
4435 int i;
4436
4437 for (i = 0; i < ARRAY_SIZE(rtnl_msg_handlers_ref); i++)
4438 refcount_set(&rtnl_msg_handlers_ref[i], 1);
4439
97c53cac 4440 if (register_pernet_subsys(&rtnetlink_net_ops))
1da177e4 4441 panic("rtnetlink_init: cannot initialize rtnetlink\n");
97c53cac 4442
1da177e4 4443 register_netdevice_notifier(&rtnetlink_dev_notifier);
340d17fc 4444
c7ac8679 4445 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
b97bac64
FW
4446 rtnl_dump_ifinfo, 0);
4447 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
4448 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
4449 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
687ad8cc 4450
b97bac64
FW
4451 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
4452 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
4453 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
77162022 4454
b97bac64
FW
4455 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
4456 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
4457 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, 0);
e5a55a89 4458
b97bac64
FW
4459 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
4460 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
4461 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
10c9ead9
RP
4462
4463 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
b97bac64 4464 0);
1da177e4 4465}