]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/core/rtnetlink.c
dsa: correctly determine the number of switches in a system
[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
1da177e4
LT
19#include <linux/errno.h>
20#include <linux/module.h>
21#include <linux/types.h>
22#include <linux/socket.h>
23#include <linux/kernel.h>
1da177e4
LT
24#include <linux/timer.h>
25#include <linux/string.h>
26#include <linux/sockios.h>
27#include <linux/net.h>
28#include <linux/fcntl.h>
29#include <linux/mm.h>
30#include <linux/slab.h>
31#include <linux/interrupt.h>
32#include <linux/capability.h>
33#include <linux/skbuff.h>
34#include <linux/init.h>
35#include <linux/security.h>
6756ae4b 36#include <linux/mutex.h>
1823730f 37#include <linux/if_addr.h>
77162022 38#include <linux/if_bridge.h>
f6f6424b 39#include <linux/if_vlan.h>
ebc08a6f 40#include <linux/pci.h>
77162022 41#include <linux/etherdevice.h>
1da177e4
LT
42
43#include <asm/uaccess.h>
1da177e4
LT
44
45#include <linux/inet.h>
46#include <linux/netdevice.h>
82f28412 47#include <net/switchdev.h>
1da177e4
LT
48#include <net/ip.h>
49#include <net/protocol.h>
50#include <net/arp.h>
51#include <net/route.h>
52#include <net/udp.h>
53#include <net/sock.h>
54#include <net/pkt_sched.h>
14c0b97d 55#include <net/fib_rules.h>
e2849863 56#include <net/rtnetlink.h>
30ffee84 57#include <net/net_namespace.h>
1da177e4 58
e0d087af 59struct rtnl_link {
e2849863
TG
60 rtnl_doit_func doit;
61 rtnl_dumpit_func dumpit;
c7ac8679 62 rtnl_calcit_func calcit;
e2849863
TG
63};
64
6756ae4b 65static DEFINE_MUTEX(rtnl_mutex);
1da177e4
LT
66
67void rtnl_lock(void)
68{
6756ae4b 69 mutex_lock(&rtnl_mutex);
1da177e4 70}
e0d087af 71EXPORT_SYMBOL(rtnl_lock);
1da177e4 72
6756ae4b 73void __rtnl_unlock(void)
1da177e4 74{
6756ae4b 75 mutex_unlock(&rtnl_mutex);
1da177e4 76}
6756ae4b 77
1da177e4
LT
78void rtnl_unlock(void)
79{
58ec3b4d 80 /* This fellow will unlock it for us. */
1da177e4
LT
81 netdev_run_todo();
82}
e0d087af 83EXPORT_SYMBOL(rtnl_unlock);
1da177e4 84
6756ae4b
SH
85int rtnl_trylock(void)
86{
87 return mutex_trylock(&rtnl_mutex);
88}
e0d087af 89EXPORT_SYMBOL(rtnl_trylock);
6756ae4b 90
c9c1014b
PM
91int rtnl_is_locked(void)
92{
93 return mutex_is_locked(&rtnl_mutex);
94}
e0d087af 95EXPORT_SYMBOL(rtnl_is_locked);
c9c1014b 96
a898def2
PM
97#ifdef CONFIG_PROVE_LOCKING
98int lockdep_rtnl_is_held(void)
99{
100 return lockdep_is_held(&rtnl_mutex);
101}
102EXPORT_SYMBOL(lockdep_rtnl_is_held);
103#endif /* #ifdef CONFIG_PROVE_LOCKING */
104
25239cee 105static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
e2849863
TG
106
107static inline int rtm_msgindex(int msgtype)
108{
109 int msgindex = msgtype - RTM_BASE;
110
111 /*
112 * msgindex < 0 implies someone tried to register a netlink
113 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
114 * the message type has not been added to linux/rtnetlink.h
115 */
116 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
117
118 return msgindex;
119}
120
121static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
122{
123 struct rtnl_link *tab;
124
25239cee 125 if (protocol <= RTNL_FAMILY_MAX)
0f87b1dd
PM
126 tab = rtnl_msg_handlers[protocol];
127 else
128 tab = NULL;
129
51057f2f 130 if (tab == NULL || tab[msgindex].doit == NULL)
e2849863
TG
131 tab = rtnl_msg_handlers[PF_UNSPEC];
132
c80bbeae 133 return tab[msgindex].doit;
e2849863
TG
134}
135
136static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
137{
138 struct rtnl_link *tab;
139
25239cee 140 if (protocol <= RTNL_FAMILY_MAX)
0f87b1dd
PM
141 tab = rtnl_msg_handlers[protocol];
142 else
143 tab = NULL;
144
51057f2f 145 if (tab == NULL || tab[msgindex].dumpit == NULL)
e2849863
TG
146 tab = rtnl_msg_handlers[PF_UNSPEC];
147
c80bbeae 148 return tab[msgindex].dumpit;
e2849863
TG
149}
150
c7ac8679
GR
151static rtnl_calcit_func rtnl_get_calcit(int protocol, int msgindex)
152{
153 struct rtnl_link *tab;
154
155 if (protocol <= RTNL_FAMILY_MAX)
156 tab = rtnl_msg_handlers[protocol];
157 else
158 tab = NULL;
159
160 if (tab == NULL || tab[msgindex].calcit == NULL)
161 tab = rtnl_msg_handlers[PF_UNSPEC];
162
c80bbeae 163 return tab[msgindex].calcit;
c7ac8679
GR
164}
165
e2849863
TG
166/**
167 * __rtnl_register - Register a rtnetlink message type
168 * @protocol: Protocol family or PF_UNSPEC
169 * @msgtype: rtnetlink message type
170 * @doit: Function pointer called for each request message
171 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
c7ac8679 172 * @calcit: Function pointer to calc size of dump message
e2849863
TG
173 *
174 * Registers the specified function pointers (at least one of them has
175 * to be non-NULL) to be called whenever a request message for the
176 * specified protocol family and message type is received.
177 *
178 * The special protocol family PF_UNSPEC may be used to define fallback
179 * function pointers for the case when no entry for the specific protocol
180 * family exists.
181 *
182 * Returns 0 on success or a negative error code.
183 */
184int __rtnl_register(int protocol, int msgtype,
c7ac8679
GR
185 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
186 rtnl_calcit_func calcit)
e2849863
TG
187{
188 struct rtnl_link *tab;
189 int msgindex;
190
25239cee 191 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
e2849863
TG
192 msgindex = rtm_msgindex(msgtype);
193
194 tab = rtnl_msg_handlers[protocol];
195 if (tab == NULL) {
196 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
197 if (tab == NULL)
198 return -ENOBUFS;
199
200 rtnl_msg_handlers[protocol] = tab;
201 }
202
203 if (doit)
204 tab[msgindex].doit = doit;
205
206 if (dumpit)
207 tab[msgindex].dumpit = dumpit;
208
c7ac8679
GR
209 if (calcit)
210 tab[msgindex].calcit = calcit;
211
e2849863
TG
212 return 0;
213}
e2849863
TG
214EXPORT_SYMBOL_GPL(__rtnl_register);
215
216/**
217 * rtnl_register - Register a rtnetlink message type
218 *
219 * Identical to __rtnl_register() but panics on failure. This is useful
220 * as failure of this function is very unlikely, it can only happen due
221 * to lack of memory when allocating the chain to store all message
222 * handlers for a protocol. Meant for use in init functions where lack
25985edc 223 * of memory implies no sense in continuing.
e2849863
TG
224 */
225void rtnl_register(int protocol, int msgtype,
c7ac8679
GR
226 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
227 rtnl_calcit_func calcit)
e2849863 228{
c7ac8679 229 if (__rtnl_register(protocol, msgtype, doit, dumpit, calcit) < 0)
e2849863
TG
230 panic("Unable to register rtnetlink message handler, "
231 "protocol = %d, message type = %d\n",
232 protocol, msgtype);
233}
e2849863
TG
234EXPORT_SYMBOL_GPL(rtnl_register);
235
236/**
237 * rtnl_unregister - Unregister a rtnetlink message type
238 * @protocol: Protocol family or PF_UNSPEC
239 * @msgtype: rtnetlink message type
240 *
241 * Returns 0 on success or a negative error code.
242 */
243int rtnl_unregister(int protocol, int msgtype)
244{
245 int msgindex;
246
25239cee 247 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
e2849863
TG
248 msgindex = rtm_msgindex(msgtype);
249
250 if (rtnl_msg_handlers[protocol] == NULL)
251 return -ENOENT;
252
253 rtnl_msg_handlers[protocol][msgindex].doit = NULL;
254 rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
255
256 return 0;
257}
e2849863
TG
258EXPORT_SYMBOL_GPL(rtnl_unregister);
259
260/**
261 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
262 * @protocol : Protocol family or PF_UNSPEC
263 *
264 * Identical to calling rtnl_unregster() for all registered message types
265 * of a certain protocol family.
266 */
267void rtnl_unregister_all(int protocol)
268{
25239cee 269 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
e2849863
TG
270
271 kfree(rtnl_msg_handlers[protocol]);
272 rtnl_msg_handlers[protocol] = NULL;
273}
e2849863 274EXPORT_SYMBOL_GPL(rtnl_unregister_all);
1da177e4 275
38f7b870
PM
276static LIST_HEAD(link_ops);
277
c63044f0
ED
278static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
279{
280 const struct rtnl_link_ops *ops;
281
282 list_for_each_entry(ops, &link_ops, list) {
283 if (!strcmp(ops->kind, kind))
284 return ops;
285 }
286 return NULL;
287}
288
38f7b870
PM
289/**
290 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
291 * @ops: struct rtnl_link_ops * to register
292 *
293 * The caller must hold the rtnl_mutex. This function should be used
294 * by drivers that create devices during module initialization. It
295 * must be called before registering the devices.
296 *
297 * Returns 0 on success or a negative error code.
298 */
299int __rtnl_link_register(struct rtnl_link_ops *ops)
300{
c63044f0
ED
301 if (rtnl_link_ops_get(ops->kind))
302 return -EEXIST;
303
b0ab2fab
JP
304 /* The check for setup is here because if ops
305 * does not have that filled up, it is not possible
306 * to use the ops for creating device. So do not
307 * fill up dellink as well. That disables rtnl_dellink.
308 */
309 if (ops->setup && !ops->dellink)
23289a37 310 ops->dellink = unregister_netdevice_queue;
2d85cba2 311
38f7b870
PM
312 list_add_tail(&ops->list, &link_ops);
313 return 0;
314}
38f7b870
PM
315EXPORT_SYMBOL_GPL(__rtnl_link_register);
316
317/**
318 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
319 * @ops: struct rtnl_link_ops * to register
320 *
321 * Returns 0 on success or a negative error code.
322 */
323int rtnl_link_register(struct rtnl_link_ops *ops)
324{
325 int err;
326
327 rtnl_lock();
328 err = __rtnl_link_register(ops);
329 rtnl_unlock();
330 return err;
331}
38f7b870
PM
332EXPORT_SYMBOL_GPL(rtnl_link_register);
333
669f87ba
PE
334static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
335{
336 struct net_device *dev;
23289a37
ED
337 LIST_HEAD(list_kill);
338
669f87ba 339 for_each_netdev(net, dev) {
23289a37
ED
340 if (dev->rtnl_link_ops == ops)
341 ops->dellink(dev, &list_kill);
669f87ba 342 }
23289a37 343 unregister_netdevice_many(&list_kill);
669f87ba
PE
344}
345
38f7b870
PM
346/**
347 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
348 * @ops: struct rtnl_link_ops * to unregister
349 *
2d85cba2 350 * The caller must hold the rtnl_mutex.
38f7b870
PM
351 */
352void __rtnl_link_unregister(struct rtnl_link_ops *ops)
353{
881d966b 354 struct net *net;
2d85cba2 355
881d966b 356 for_each_net(net) {
669f87ba 357 __rtnl_kill_links(net, ops);
2d85cba2 358 }
38f7b870
PM
359 list_del(&ops->list);
360}
38f7b870
PM
361EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
362
200b916f
CW
363/* Return with the rtnl_lock held when there are no network
364 * devices unregistering in any network namespace.
365 */
366static void rtnl_lock_unregistering_all(void)
367{
368 struct net *net;
369 bool unregistering;
ff960a73 370 DEFINE_WAIT_FUNC(wait, woken_wake_function);
200b916f 371
ff960a73 372 add_wait_queue(&netdev_unregistering_wq, &wait);
200b916f 373 for (;;) {
200b916f
CW
374 unregistering = false;
375 rtnl_lock();
376 for_each_net(net) {
377 if (net->dev_unreg_count > 0) {
378 unregistering = true;
379 break;
380 }
381 }
382 if (!unregistering)
383 break;
384 __rtnl_unlock();
ff960a73
PZ
385
386 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
200b916f 387 }
ff960a73 388 remove_wait_queue(&netdev_unregistering_wq, &wait);
200b916f
CW
389}
390
38f7b870
PM
391/**
392 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
393 * @ops: struct rtnl_link_ops * to unregister
394 */
395void rtnl_link_unregister(struct rtnl_link_ops *ops)
396{
200b916f
CW
397 /* Close the race with cleanup_net() */
398 mutex_lock(&net_mutex);
399 rtnl_lock_unregistering_all();
38f7b870
PM
400 __rtnl_link_unregister(ops);
401 rtnl_unlock();
200b916f 402 mutex_unlock(&net_mutex);
38f7b870 403}
38f7b870
PM
404EXPORT_SYMBOL_GPL(rtnl_link_unregister);
405
ba7d49b1
JP
406static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
407{
408 struct net_device *master_dev;
409 const struct rtnl_link_ops *ops;
410
411 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
412 if (!master_dev)
413 return 0;
414 ops = master_dev->rtnl_link_ops;
6049f253 415 if (!ops || !ops->get_slave_size)
ba7d49b1
JP
416 return 0;
417 /* IFLA_INFO_SLAVE_DATA + nested data */
418 return nla_total_size(sizeof(struct nlattr)) +
419 ops->get_slave_size(master_dev, dev);
420}
421
38f7b870
PM
422static size_t rtnl_link_get_size(const struct net_device *dev)
423{
424 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
425 size_t size;
426
427 if (!ops)
428 return 0;
429
369cf77a
TG
430 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
431 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
38f7b870
PM
432
433 if (ops->get_size)
434 /* IFLA_INFO_DATA + nested data */
369cf77a 435 size += nla_total_size(sizeof(struct nlattr)) +
38f7b870
PM
436 ops->get_size(dev);
437
438 if (ops->get_xstats_size)
369cf77a
TG
439 /* IFLA_INFO_XSTATS */
440 size += nla_total_size(ops->get_xstats_size(dev));
38f7b870 441
ba7d49b1
JP
442 size += rtnl_link_get_slave_info_data_size(dev);
443
38f7b870
PM
444 return size;
445}
446
f8ff182c
TG
447static LIST_HEAD(rtnl_af_ops);
448
449static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
450{
451 const struct rtnl_af_ops *ops;
452
453 list_for_each_entry(ops, &rtnl_af_ops, list) {
454 if (ops->family == family)
455 return ops;
456 }
457
458 return NULL;
459}
460
f8ff182c
TG
461/**
462 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
463 * @ops: struct rtnl_af_ops * to register
464 *
465 * Returns 0 on success or a negative error code.
466 */
3678a9d8 467void rtnl_af_register(struct rtnl_af_ops *ops)
f8ff182c 468{
f8ff182c 469 rtnl_lock();
3678a9d8 470 list_add_tail(&ops->list, &rtnl_af_ops);
f8ff182c 471 rtnl_unlock();
f8ff182c
TG
472}
473EXPORT_SYMBOL_GPL(rtnl_af_register);
474
475/**
476 * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
477 * @ops: struct rtnl_af_ops * to unregister
478 *
479 * The caller must hold the rtnl_mutex.
480 */
481void __rtnl_af_unregister(struct rtnl_af_ops *ops)
482{
483 list_del(&ops->list);
484}
485EXPORT_SYMBOL_GPL(__rtnl_af_unregister);
486
487/**
488 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
489 * @ops: struct rtnl_af_ops * to unregister
490 */
491void rtnl_af_unregister(struct rtnl_af_ops *ops)
492{
493 rtnl_lock();
494 __rtnl_af_unregister(ops);
495 rtnl_unlock();
496}
497EXPORT_SYMBOL_GPL(rtnl_af_unregister);
498
499static size_t rtnl_link_get_af_size(const struct net_device *dev)
500{
501 struct rtnl_af_ops *af_ops;
502 size_t size;
503
504 /* IFLA_AF_SPEC */
505 size = nla_total_size(sizeof(struct nlattr));
506
507 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
508 if (af_ops->get_link_af_size) {
509 /* AF_* + nested data */
510 size += nla_total_size(sizeof(struct nlattr)) +
511 af_ops->get_link_af_size(dev);
512 }
513 }
514
515 return size;
516}
517
ba7d49b1 518static bool rtnl_have_link_slave_info(const struct net_device *dev)
38f7b870 519{
ba7d49b1 520 struct net_device *master_dev;
38f7b870 521
ba7d49b1 522 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
813f020c 523 if (master_dev && master_dev->rtnl_link_ops)
ba7d49b1
JP
524 return true;
525 return false;
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
LT
624 if (echo)
625 atomic_inc(&skb->users);
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]) {
672 valid++;
a6574349
DM
673 if (nla_put_u32(skb, i+1, metrics[i]))
674 goto nla_put_failure;
2d7202bf 675 }
1da177e4 676 }
1da177e4 677
a57d27fc
DM
678 if (!valid) {
679 nla_nest_cancel(skb, mx);
680 return 0;
681 }
2d7202bf
TG
682
683 return nla_nest_end(skb, mx);
684
685nla_put_failure:
bc3ed28c
TG
686 nla_nest_cancel(skb, mx);
687 return -EMSGSIZE;
1da177e4 688}
e0d087af 689EXPORT_SYMBOL(rtnetlink_put_metrics);
1da177e4 690
e3703b3d 691int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
87a50699 692 long expires, u32 error)
e3703b3d
TG
693{
694 struct rta_cacheinfo ci = {
a399a805 695 .rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse),
e3703b3d
TG
696 .rta_used = dst->__use,
697 .rta_clntref = atomic_read(&(dst->__refcnt)),
698 .rta_error = error,
699 .rta_id = id,
e3703b3d
TG
700 };
701
8253947e
LW
702 if (expires) {
703 unsigned long clock;
e3703b3d 704
8253947e
LW
705 clock = jiffies_to_clock_t(abs(expires));
706 clock = min_t(unsigned long, clock, INT_MAX);
707 ci.rta_expires = (expires > 0) ? clock : -clock;
708 }
e3703b3d
TG
709 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
710}
e3703b3d 711EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
1da177e4 712
93b2d4a2 713static void set_operstate(struct net_device *dev, unsigned char transition)
b00055aa
SR
714{
715 unsigned char operstate = dev->operstate;
716
e0d087af 717 switch (transition) {
b00055aa
SR
718 case IF_OPER_UP:
719 if ((operstate == IF_OPER_DORMANT ||
720 operstate == IF_OPER_UNKNOWN) &&
721 !netif_dormant(dev))
722 operstate = IF_OPER_UP;
723 break;
724
725 case IF_OPER_DORMANT:
726 if (operstate == IF_OPER_UP ||
727 operstate == IF_OPER_UNKNOWN)
728 operstate = IF_OPER_DORMANT;
729 break;
3ff50b79 730 }
b00055aa
SR
731
732 if (dev->operstate != operstate) {
733 write_lock_bh(&dev_base_lock);
734 dev->operstate = operstate;
735 write_unlock_bh(&dev_base_lock);
93b2d4a2
DM
736 netdev_state_change(dev);
737 }
b00055aa
SR
738}
739
b1beb681
JB
740static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
741{
742 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
743 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
744}
745
3729d502
PM
746static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
747 const struct ifinfomsg *ifm)
748{
749 unsigned int flags = ifm->ifi_flags;
750
751 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
752 if (ifm->ifi_change)
753 flags = (flags & ifm->ifi_change) |
b1beb681 754 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
3729d502
PM
755
756 return flags;
757}
758
b60c5115 759static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
be1f3c2c 760 const struct rtnl_link_stats64 *b)
1da177e4 761{
b60c5115
TG
762 a->rx_packets = b->rx_packets;
763 a->tx_packets = b->tx_packets;
764 a->rx_bytes = b->rx_bytes;
765 a->tx_bytes = b->tx_bytes;
766 a->rx_errors = b->rx_errors;
767 a->tx_errors = b->tx_errors;
768 a->rx_dropped = b->rx_dropped;
769 a->tx_dropped = b->tx_dropped;
770
771 a->multicast = b->multicast;
772 a->collisions = b->collisions;
773
774 a->rx_length_errors = b->rx_length_errors;
775 a->rx_over_errors = b->rx_over_errors;
776 a->rx_crc_errors = b->rx_crc_errors;
777 a->rx_frame_errors = b->rx_frame_errors;
778 a->rx_fifo_errors = b->rx_fifo_errors;
779 a->rx_missed_errors = b->rx_missed_errors;
780
781 a->tx_aborted_errors = b->tx_aborted_errors;
782 a->tx_carrier_errors = b->tx_carrier_errors;
783 a->tx_fifo_errors = b->tx_fifo_errors;
784 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
785 a->tx_window_errors = b->tx_window_errors;
786
787 a->rx_compressed = b->rx_compressed;
788 a->tx_compressed = b->tx_compressed;
10708f37
JE
789}
790
be1f3c2c 791static void copy_rtnl_link_stats64(void *v, const struct rtnl_link_stats64 *b)
10708f37 792{
afdcba37 793 memcpy(v, b, sizeof(*b));
10708f37 794}
1da177e4 795
c02db8c6 796/* All VF info */
115c9b81
GR
797static inline int rtnl_vfinfo_size(const struct net_device *dev,
798 u32 ext_filter_mask)
ebc08a6f 799{
115c9b81
GR
800 if (dev->dev.parent && dev_is_pci(dev->dev.parent) &&
801 (ext_filter_mask & RTEXT_FILTER_VF)) {
c02db8c6 802 int num_vfs = dev_num_vf(dev->dev.parent);
045de01a
SF
803 size_t size = nla_total_size(sizeof(struct nlattr));
804 size += nla_total_size(num_vfs * sizeof(struct nlattr));
805 size += num_vfs *
806 (nla_total_size(sizeof(struct ifla_vf_mac)) +
807 nla_total_size(sizeof(struct ifla_vf_vlan)) +
ed616689 808 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
945a3676
JB
809 nla_total_size(sizeof(struct ifla_vf_rate)) +
810 nla_total_size(sizeof(struct ifla_vf_link_state)));
c02db8c6
CW
811 return size;
812 } else
ebc08a6f
WM
813 return 0;
814}
815
c53864fd
DG
816static size_t rtnl_port_size(const struct net_device *dev,
817 u32 ext_filter_mask)
57b61080
SF
818{
819 size_t port_size = nla_total_size(4) /* PORT_VF */
820 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
821 + nla_total_size(sizeof(struct ifla_port_vsi))
822 /* PORT_VSI_TYPE */
823 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
824 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
825 + nla_total_size(1) /* PROT_VDP_REQUEST */
826 + nla_total_size(2); /* PORT_VDP_RESPONSE */
827 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
828 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
829 + port_size;
830 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
831 + port_size;
832
c53864fd
DG
833 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
834 !(ext_filter_mask & RTEXT_FILTER_VF))
57b61080
SF
835 return 0;
836 if (dev_num_vf(dev->dev.parent))
837 return port_self_size + vf_ports_size +
838 vf_port_size * dev_num_vf(dev->dev.parent);
839 else
840 return port_self_size;
841}
842
115c9b81
GR
843static noinline size_t if_nlmsg_size(const struct net_device *dev,
844 u32 ext_filter_mask)
339bf98f
TG
845{
846 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
847 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
0b815a1a 848 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
339bf98f
TG
849 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
850 + nla_total_size(sizeof(struct rtnl_link_ifmap))
851 + nla_total_size(sizeof(struct rtnl_link_stats))
adcfe196 852 + nla_total_size(sizeof(struct rtnl_link_stats64))
339bf98f
TG
853 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
854 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
855 + nla_total_size(4) /* IFLA_TXQLEN */
856 + nla_total_size(4) /* IFLA_WEIGHT */
857 + nla_total_size(4) /* IFLA_MTU */
858 + nla_total_size(4) /* IFLA_LINK */
859 + nla_total_size(4) /* IFLA_MASTER */
9a57247f 860 + nla_total_size(1) /* IFLA_CARRIER */
edbc0bb3 861 + nla_total_size(4) /* IFLA_PROMISCUITY */
76ff5cc9
JP
862 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
863 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
339bf98f 864 + nla_total_size(1) /* IFLA_OPERSTATE */
38f7b870 865 + nla_total_size(1) /* IFLA_LINKMODE */
2d3b479d 866 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
115c9b81
GR
867 + nla_total_size(ext_filter_mask
868 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
869 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
c53864fd 870 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
f8ff182c 871 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
66cae9ed 872 + rtnl_link_get_af_size(dev) /* IFLA_AF_SPEC */
82f28412
JP
873 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
874 + nla_total_size(MAX_PHYS_ITEM_ID_LEN); /* IFLA_PHYS_SWITCH_ID */
339bf98f
TG
875}
876
57b61080
SF
877static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
878{
879 struct nlattr *vf_ports;
880 struct nlattr *vf_port;
881 int vf;
882 int err;
883
884 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
885 if (!vf_ports)
886 return -EMSGSIZE;
887
888 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
889 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
8ca94183
SF
890 if (!vf_port)
891 goto nla_put_failure;
a6574349
DM
892 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
893 goto nla_put_failure;
57b61080 894 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
8ca94183
SF
895 if (err == -EMSGSIZE)
896 goto nla_put_failure;
57b61080 897 if (err) {
57b61080
SF
898 nla_nest_cancel(skb, vf_port);
899 continue;
900 }
901 nla_nest_end(skb, vf_port);
902 }
903
904 nla_nest_end(skb, vf_ports);
905
906 return 0;
8ca94183
SF
907
908nla_put_failure:
909 nla_nest_cancel(skb, vf_ports);
910 return -EMSGSIZE;
57b61080
SF
911}
912
913static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
914{
915 struct nlattr *port_self;
916 int err;
917
918 port_self = nla_nest_start(skb, IFLA_PORT_SELF);
919 if (!port_self)
920 return -EMSGSIZE;
921
922 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
923 if (err) {
924 nla_nest_cancel(skb, port_self);
8ca94183 925 return (err == -EMSGSIZE) ? err : 0;
57b61080
SF
926 }
927
928 nla_nest_end(skb, port_self);
929
930 return 0;
931}
932
c53864fd
DG
933static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
934 u32 ext_filter_mask)
57b61080
SF
935{
936 int err;
937
c53864fd
DG
938 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
939 !(ext_filter_mask & RTEXT_FILTER_VF))
57b61080
SF
940 return 0;
941
942 err = rtnl_port_self_fill(skb, dev);
943 if (err)
944 return err;
945
946 if (dev_num_vf(dev->dev.parent)) {
947 err = rtnl_vf_ports_fill(skb, dev);
948 if (err)
949 return err;
950 }
951
952 return 0;
953}
954
66cae9ed
JP
955static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
956{
957 int err;
02637fce 958 struct netdev_phys_item_id ppid;
66cae9ed
JP
959
960 err = dev_get_phys_port_id(dev, &ppid);
961 if (err) {
962 if (err == -EOPNOTSUPP)
963 return 0;
964 return err;
965 }
966
967 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
968 return -EMSGSIZE;
969
970 return 0;
971}
972
82f28412
JP
973static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
974{
975 int err;
976 struct netdev_phys_item_id psid;
977
978 err = netdev_switch_parent_id_get(dev, &psid);
979 if (err) {
980 if (err == -EOPNOTSUPP)
981 return 0;
982 return err;
983 }
984
985 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, psid.id_len, psid.id))
986 return -EMSGSIZE;
987
988 return 0;
989}
990
b60c5115 991static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
575c3e2a 992 int type, u32 pid, u32 seq, u32 change,
115c9b81 993 unsigned int flags, u32 ext_filter_mask)
b60c5115
TG
994{
995 struct ifinfomsg *ifm;
996 struct nlmsghdr *nlh;
28172739 997 struct rtnl_link_stats64 temp;
be1f3c2c 998 const struct rtnl_link_stats64 *stats;
f8ff182c
TG
999 struct nlattr *attr, *af_spec;
1000 struct rtnl_af_ops *af_ops;
898e5061 1001 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
1da177e4 1002
2907c35f 1003 ASSERT_RTNL();
b60c5115
TG
1004 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1005 if (nlh == NULL)
26932566 1006 return -EMSGSIZE;
1da177e4 1007
b60c5115
TG
1008 ifm = nlmsg_data(nlh);
1009 ifm->ifi_family = AF_UNSPEC;
1010 ifm->__ifi_pad = 0;
1011 ifm->ifi_type = dev->type;
1012 ifm->ifi_index = dev->ifindex;
1013 ifm->ifi_flags = dev_get_flags(dev);
1014 ifm->ifi_change = change;
1015
a6574349
DM
1016 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1017 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1018 nla_put_u8(skb, IFLA_OPERSTATE,
1019 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1020 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1021 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1022 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
edbc0bb3 1023 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
76ff5cc9 1024 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
1d69c2b3 1025#ifdef CONFIG_RPS
76ff5cc9 1026 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
1d69c2b3 1027#endif
a6574349
DM
1028 (dev->ifindex != dev->iflink &&
1029 nla_put_u32(skb, IFLA_LINK, dev->iflink)) ||
898e5061
JP
1030 (upper_dev &&
1031 nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
9a57247f 1032 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
a6574349
DM
1033 (dev->qdisc &&
1034 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1035 (dev->ifalias &&
2d3b479d 1036 nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) ||
1037 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
1038 atomic_read(&dev->carrier_changes)))
a6574349 1039 goto nla_put_failure;
0b815a1a 1040
1da177e4
LT
1041 if (1) {
1042 struct rtnl_link_ifmap map = {
1043 .mem_start = dev->mem_start,
1044 .mem_end = dev->mem_end,
1045 .base_addr = dev->base_addr,
1046 .irq = dev->irq,
1047 .dma = dev->dma,
1048 .port = dev->if_port,
1049 };
a6574349
DM
1050 if (nla_put(skb, IFLA_MAP, sizeof(map), &map))
1051 goto nla_put_failure;
1da177e4
LT
1052 }
1053
1054 if (dev->addr_len) {
a6574349
DM
1055 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1056 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1057 goto nla_put_failure;
1da177e4
LT
1058 }
1059
66cae9ed
JP
1060 if (rtnl_phys_port_id_fill(skb, dev))
1061 goto nla_put_failure;
1062
82f28412
JP
1063 if (rtnl_phys_switch_id_fill(skb, dev))
1064 goto nla_put_failure;
1065
96e74088
PE
1066 attr = nla_reserve(skb, IFLA_STATS,
1067 sizeof(struct rtnl_link_stats));
1068 if (attr == NULL)
1069 goto nla_put_failure;
b60c5115 1070
28172739 1071 stats = dev_get_stats(dev, &temp);
96e74088 1072 copy_rtnl_link_stats(nla_data(attr), stats);
1da177e4 1073
10708f37
JE
1074 attr = nla_reserve(skb, IFLA_STATS64,
1075 sizeof(struct rtnl_link_stats64));
1076 if (attr == NULL)
1077 goto nla_put_failure;
10708f37
JE
1078 copy_rtnl_link_stats64(nla_data(attr), stats);
1079
a6574349
DM
1080 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
1081 nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
1082 goto nla_put_failure;
57b61080 1083
115c9b81
GR
1084 if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent
1085 && (ext_filter_mask & RTEXT_FILTER_VF)) {
ebc08a6f 1086 int i;
ebc08a6f 1087
c02db8c6
CW
1088 struct nlattr *vfinfo, *vf;
1089 int num_vfs = dev_num_vf(dev->dev.parent);
1090
c02db8c6
CW
1091 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1092 if (!vfinfo)
1093 goto nla_put_failure;
1094 for (i = 0; i < num_vfs; i++) {
1095 struct ifla_vf_info ivi;
1096 struct ifla_vf_mac vf_mac;
1097 struct ifla_vf_vlan vf_vlan;
ed616689 1098 struct ifla_vf_rate vf_rate;
c02db8c6 1099 struct ifla_vf_tx_rate vf_tx_rate;
5f8444a3 1100 struct ifla_vf_spoofchk vf_spoofchk;
1d8faf48 1101 struct ifla_vf_link_state vf_linkstate;
5f8444a3
GR
1102
1103 /*
1104 * Not all SR-IOV capable drivers support the
1105 * spoofcheck query. Preset to -1 so the user
1106 * space tool can detect that the driver didn't
1107 * report anything.
1108 */
1109 ivi.spoofchk = -1;
84d73cd3 1110 memset(ivi.mac, 0, sizeof(ivi.mac));
1d8faf48
RE
1111 /* The default value for VF link state is "auto"
1112 * IFLA_VF_LINK_STATE_AUTO which equals zero
1113 */
1114 ivi.linkstate = 0;
ebc08a6f
WM
1115 if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi))
1116 break;
5f8444a3
GR
1117 vf_mac.vf =
1118 vf_vlan.vf =
ed616689 1119 vf_rate.vf =
5f8444a3 1120 vf_tx_rate.vf =
1d8faf48
RE
1121 vf_spoofchk.vf =
1122 vf_linkstate.vf = ivi.vf;
5f8444a3 1123
c02db8c6
CW
1124 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1125 vf_vlan.vlan = ivi.vlan;
1126 vf_vlan.qos = ivi.qos;
ed616689
SC
1127 vf_tx_rate.rate = ivi.max_tx_rate;
1128 vf_rate.min_tx_rate = ivi.min_tx_rate;
1129 vf_rate.max_tx_rate = ivi.max_tx_rate;
5f8444a3 1130 vf_spoofchk.setting = ivi.spoofchk;
1d8faf48 1131 vf_linkstate.link_state = ivi.linkstate;
c02db8c6
CW
1132 vf = nla_nest_start(skb, IFLA_VF_INFO);
1133 if (!vf) {
1134 nla_nest_cancel(skb, vfinfo);
1135 goto nla_put_failure;
1136 }
a6574349
DM
1137 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1138 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
ed616689
SC
1139 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1140 &vf_rate) ||
a6574349
DM
1141 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1142 &vf_tx_rate) ||
1143 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1d8faf48
RE
1144 &vf_spoofchk) ||
1145 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1146 &vf_linkstate))
a6574349 1147 goto nla_put_failure;
c02db8c6 1148 nla_nest_end(skb, vf);
ebc08a6f 1149 }
c02db8c6 1150 nla_nest_end(skb, vfinfo);
ebc08a6f 1151 }
57b61080 1152
c53864fd 1153 if (rtnl_port_fill(skb, dev, ext_filter_mask))
57b61080
SF
1154 goto nla_put_failure;
1155
ba7d49b1 1156 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
38f7b870
PM
1157 if (rtnl_link_fill(skb, dev) < 0)
1158 goto nla_put_failure;
1159 }
1160
f8ff182c
TG
1161 if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1162 goto nla_put_failure;
1163
1164 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1165 if (af_ops->fill_link_af) {
1166 struct nlattr *af;
1167 int err;
1168
1169 if (!(af = nla_nest_start(skb, af_ops->family)))
1170 goto nla_put_failure;
1171
1172 err = af_ops->fill_link_af(skb, dev);
1173
1174 /*
1175 * Caller may return ENODATA to indicate that there
1176 * was no data to be dumped. This is not an error, it
1177 * means we should trim the attribute header and
1178 * continue.
1179 */
1180 if (err == -ENODATA)
1181 nla_nest_cancel(skb, af);
1182 else if (err < 0)
1183 goto nla_put_failure;
1184
1185 nla_nest_end(skb, af);
1186 }
1187 }
1188
1189 nla_nest_end(skb, af_spec);
1190
b60c5115
TG
1191 return nlmsg_end(skb, nlh);
1192
1193nla_put_failure:
26932566
PM
1194 nlmsg_cancel(skb, nlh);
1195 return -EMSGSIZE;
1da177e4
LT
1196}
1197
f7b12606 1198static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
5176f91e 1199 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
38f7b870
PM
1200 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1201 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
5176f91e 1202 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
da5e0494 1203 [IFLA_MTU] = { .type = NLA_U32 },
76e87306 1204 [IFLA_LINK] = { .type = NLA_U32 },
fbaec0ea 1205 [IFLA_MASTER] = { .type = NLA_U32 },
9a57247f 1206 [IFLA_CARRIER] = { .type = NLA_U8 },
da5e0494
TG
1207 [IFLA_TXQLEN] = { .type = NLA_U32 },
1208 [IFLA_WEIGHT] = { .type = NLA_U32 },
1209 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1210 [IFLA_LINKMODE] = { .type = NLA_U8 },
76e87306 1211 [IFLA_LINKINFO] = { .type = NLA_NESTED },
d8a5ec67 1212 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
f0630529 1213 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
0b815a1a 1214 [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 },
c02db8c6 1215 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
57b61080
SF
1216 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1217 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
f8ff182c 1218 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
115c9b81 1219 [IFLA_EXT_MASK] = { .type = NLA_U32 },
edbc0bb3 1220 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
76ff5cc9
JP
1221 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1222 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
02637fce 1223 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
2d3b479d 1224 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
82f28412 1225 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
da5e0494
TG
1226};
1227
38f7b870
PM
1228static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1229 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1230 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
ba7d49b1
JP
1231 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1232 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
38f7b870
PM
1233};
1234
c02db8c6
CW
1235static const struct nla_policy ifla_vfinfo_policy[IFLA_VF_INFO_MAX+1] = {
1236 [IFLA_VF_INFO] = { .type = NLA_NESTED },
1237};
1238
1239static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1240 [IFLA_VF_MAC] = { .type = NLA_BINARY,
1241 .len = sizeof(struct ifla_vf_mac) },
1242 [IFLA_VF_VLAN] = { .type = NLA_BINARY,
1243 .len = sizeof(struct ifla_vf_vlan) },
1244 [IFLA_VF_TX_RATE] = { .type = NLA_BINARY,
1245 .len = sizeof(struct ifla_vf_tx_rate) },
48752f65
GR
1246 [IFLA_VF_SPOOFCHK] = { .type = NLA_BINARY,
1247 .len = sizeof(struct ifla_vf_spoofchk) },
ed616689
SC
1248 [IFLA_VF_RATE] = { .type = NLA_BINARY,
1249 .len = sizeof(struct ifla_vf_rate) },
c5b46160
DL
1250 [IFLA_VF_LINK_STATE] = { .type = NLA_BINARY,
1251 .len = sizeof(struct ifla_vf_link_state) },
c02db8c6
CW
1252};
1253
57b61080
SF
1254static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1255 [IFLA_PORT_VF] = { .type = NLA_U32 },
1256 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1257 .len = PORT_PROFILE_MAX },
1258 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1259 .len = sizeof(struct ifla_port_vsi)},
1260 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1261 .len = PORT_UUID_MAX },
1262 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1263 .len = PORT_UUID_MAX },
1264 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1265 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
1266};
1267
f7b12606
JP
1268static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
1269{
1270 struct net *net = sock_net(skb->sk);
1271 int h, s_h;
1272 int idx = 0, s_idx;
1273 struct net_device *dev;
1274 struct hlist_head *head;
1275 struct nlattr *tb[IFLA_MAX+1];
1276 u32 ext_filter_mask = 0;
973462bb 1277 int err;
e5eca6d4 1278 int hdrlen;
f7b12606
JP
1279
1280 s_h = cb->args[0];
1281 s_idx = cb->args[1];
1282
1283 rcu_read_lock();
1284 cb->seq = net->dev_base_seq;
1285
e5eca6d4
MS
1286 /* A hack to preserve kernel<->userspace interface.
1287 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1288 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1289 * what iproute2 < v3.9.0 used.
1290 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1291 * attribute, its netlink message is shorter than struct ifinfomsg.
1292 */
1293 hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1294 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1295
1296 if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
f7b12606
JP
1297
1298 if (tb[IFLA_EXT_MASK])
1299 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1300 }
1301
1302 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1303 idx = 0;
1304 head = &net->dev_index_head[h];
1305 hlist_for_each_entry_rcu(dev, head, index_hlist) {
1306 if (idx < s_idx)
1307 goto cont;
973462bb
DG
1308 err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
1309 NETLINK_CB(cb->skb).portid,
1310 cb->nlh->nlmsg_seq, 0,
1311 NLM_F_MULTI,
1312 ext_filter_mask);
1313 /* If we ran out of room on the first message,
1314 * we're in trouble
1315 */
1316 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
1317
1318 if (err <= 0)
f7b12606
JP
1319 goto out;
1320
1321 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1322cont:
1323 idx++;
1324 }
1325 }
1326out:
1327 rcu_read_unlock();
1328 cb->args[1] = idx;
1329 cb->args[0] = h;
1330
1331 return skb->len;
1332}
1333
1334int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len)
1335{
1336 return nla_parse(tb, IFLA_MAX, head, len, ifla_policy);
1337}
1338EXPORT_SYMBOL(rtnl_nla_parse_ifla);
1339
81adee47
EB
1340struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1341{
1342 struct net *net;
1343 /* Examine the link attributes and figure out which
1344 * network namespace we are talking about.
1345 */
1346 if (tb[IFLA_NET_NS_PID])
1347 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
f0630529
EB
1348 else if (tb[IFLA_NET_NS_FD])
1349 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
81adee47
EB
1350 else
1351 net = get_net(src_net);
1352 return net;
1353}
1354EXPORT_SYMBOL(rtnl_link_get_net);
1355
1840bb13
TG
1356static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1357{
1358 if (dev) {
1359 if (tb[IFLA_ADDRESS] &&
1360 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1361 return -EINVAL;
1362
1363 if (tb[IFLA_BROADCAST] &&
1364 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1365 return -EINVAL;
1366 }
1367
cf7afbfe
TG
1368 if (tb[IFLA_AF_SPEC]) {
1369 struct nlattr *af;
1370 int rem, err;
1371
1372 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1373 const struct rtnl_af_ops *af_ops;
1374
1375 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1376 return -EAFNOSUPPORT;
1377
1378 if (!af_ops->set_link_af)
1379 return -EOPNOTSUPP;
1380
1381 if (af_ops->validate_link_af) {
6d3a9a68 1382 err = af_ops->validate_link_af(dev, af);
cf7afbfe
TG
1383 if (err < 0)
1384 return err;
1385 }
1386 }
1387 }
1388
1840bb13
TG
1389 return 0;
1390}
1391
c02db8c6
CW
1392static int do_setvfinfo(struct net_device *dev, struct nlattr *attr)
1393{
1394 int rem, err = -EINVAL;
1395 struct nlattr *vf;
1396 const struct net_device_ops *ops = dev->netdev_ops;
1397
1398 nla_for_each_nested(vf, attr, rem) {
1399 switch (nla_type(vf)) {
1400 case IFLA_VF_MAC: {
1401 struct ifla_vf_mac *ivm;
1402 ivm = nla_data(vf);
1403 err = -EOPNOTSUPP;
1404 if (ops->ndo_set_vf_mac)
1405 err = ops->ndo_set_vf_mac(dev, ivm->vf,
1406 ivm->mac);
1407 break;
1408 }
1409 case IFLA_VF_VLAN: {
1410 struct ifla_vf_vlan *ivv;
1411 ivv = nla_data(vf);
1412 err = -EOPNOTSUPP;
1413 if (ops->ndo_set_vf_vlan)
1414 err = ops->ndo_set_vf_vlan(dev, ivv->vf,
1415 ivv->vlan,
1416 ivv->qos);
1417 break;
1418 }
1419 case IFLA_VF_TX_RATE: {
1420 struct ifla_vf_tx_rate *ivt;
ed616689 1421 struct ifla_vf_info ivf;
c02db8c6
CW
1422 ivt = nla_data(vf);
1423 err = -EOPNOTSUPP;
ed616689
SC
1424 if (ops->ndo_get_vf_config)
1425 err = ops->ndo_get_vf_config(dev, ivt->vf,
1426 &ivf);
1427 if (err)
1428 break;
1429 err = -EOPNOTSUPP;
1430 if (ops->ndo_set_vf_rate)
1431 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1432 ivf.min_tx_rate,
1433 ivt->rate);
1434 break;
1435 }
1436 case IFLA_VF_RATE: {
1437 struct ifla_vf_rate *ivt;
1438 ivt = nla_data(vf);
1439 err = -EOPNOTSUPP;
1440 if (ops->ndo_set_vf_rate)
1441 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1442 ivt->min_tx_rate,
1443 ivt->max_tx_rate);
c02db8c6
CW
1444 break;
1445 }
5f8444a3
GR
1446 case IFLA_VF_SPOOFCHK: {
1447 struct ifla_vf_spoofchk *ivs;
1448 ivs = nla_data(vf);
1449 err = -EOPNOTSUPP;
1450 if (ops->ndo_set_vf_spoofchk)
1451 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
1452 ivs->setting);
1453 break;
1454 }
1d8faf48
RE
1455 case IFLA_VF_LINK_STATE: {
1456 struct ifla_vf_link_state *ivl;
1457 ivl = nla_data(vf);
1458 err = -EOPNOTSUPP;
1459 if (ops->ndo_set_vf_link_state)
1460 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
1461 ivl->link_state);
1462 break;
1463 }
c02db8c6
CW
1464 default:
1465 err = -EINVAL;
1466 break;
1467 }
1468 if (err)
1469 break;
1470 }
1471 return err;
1472}
1473
fbaec0ea
JP
1474static int do_set_master(struct net_device *dev, int ifindex)
1475{
898e5061 1476 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
fbaec0ea
JP
1477 const struct net_device_ops *ops;
1478 int err;
1479
898e5061
JP
1480 if (upper_dev) {
1481 if (upper_dev->ifindex == ifindex)
fbaec0ea 1482 return 0;
898e5061 1483 ops = upper_dev->netdev_ops;
fbaec0ea 1484 if (ops->ndo_del_slave) {
898e5061 1485 err = ops->ndo_del_slave(upper_dev, dev);
fbaec0ea
JP
1486 if (err)
1487 return err;
1488 } else {
1489 return -EOPNOTSUPP;
1490 }
1491 }
1492
1493 if (ifindex) {
898e5061
JP
1494 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
1495 if (!upper_dev)
fbaec0ea 1496 return -EINVAL;
898e5061 1497 ops = upper_dev->netdev_ops;
fbaec0ea 1498 if (ops->ndo_add_slave) {
898e5061 1499 err = ops->ndo_add_slave(upper_dev, dev);
fbaec0ea
JP
1500 if (err)
1501 return err;
1502 } else {
1503 return -EOPNOTSUPP;
1504 }
1505 }
1506 return 0;
1507}
1508
90c325e3 1509#define DO_SETLINK_MODIFIED 0x01
ba998906
ND
1510/* notify flag means notify + modified. */
1511#define DO_SETLINK_NOTIFY 0x03
90f62cf3
EB
1512static int do_setlink(const struct sk_buff *skb,
1513 struct net_device *dev, struct ifinfomsg *ifm,
90c325e3 1514 struct nlattr **tb, char *ifname, int status)
1da177e4 1515{
d314774c 1516 const struct net_device_ops *ops = dev->netdev_ops;
0157f60c 1517 int err;
1da177e4 1518
f0630529 1519 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
81adee47 1520 struct net *net = rtnl_link_get_net(dev_net(dev), tb);
d8a5ec67
EB
1521 if (IS_ERR(net)) {
1522 err = PTR_ERR(net);
1523 goto errout;
1524 }
90f62cf3 1525 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
e0ebde0e 1526 put_net(net);
b51642f6
EB
1527 err = -EPERM;
1528 goto errout;
1529 }
d8a5ec67
EB
1530 err = dev_change_net_namespace(dev, net, ifname);
1531 put_net(net);
1532 if (err)
1533 goto errout;
90c325e3 1534 status |= DO_SETLINK_MODIFIED;
d8a5ec67
EB
1535 }
1536
da5e0494 1537 if (tb[IFLA_MAP]) {
1da177e4
LT
1538 struct rtnl_link_ifmap *u_map;
1539 struct ifmap k_map;
1540
d314774c 1541 if (!ops->ndo_set_config) {
1da177e4 1542 err = -EOPNOTSUPP;
0157f60c 1543 goto errout;
1da177e4
LT
1544 }
1545
1546 if (!netif_device_present(dev)) {
1547 err = -ENODEV;
0157f60c 1548 goto errout;
1da177e4 1549 }
1da177e4 1550
da5e0494 1551 u_map = nla_data(tb[IFLA_MAP]);
1da177e4
LT
1552 k_map.mem_start = (unsigned long) u_map->mem_start;
1553 k_map.mem_end = (unsigned long) u_map->mem_end;
1554 k_map.base_addr = (unsigned short) u_map->base_addr;
1555 k_map.irq = (unsigned char) u_map->irq;
1556 k_map.dma = (unsigned char) u_map->dma;
1557 k_map.port = (unsigned char) u_map->port;
1558
d314774c 1559 err = ops->ndo_set_config(dev, &k_map);
da5e0494 1560 if (err < 0)
0157f60c 1561 goto errout;
1da177e4 1562
ba998906 1563 status |= DO_SETLINK_NOTIFY;
1da177e4
LT
1564 }
1565
da5e0494 1566 if (tb[IFLA_ADDRESS]) {
70f8e78e
DM
1567 struct sockaddr *sa;
1568 int len;
1569
70f8e78e
DM
1570 len = sizeof(sa_family_t) + dev->addr_len;
1571 sa = kmalloc(len, GFP_KERNEL);
1572 if (!sa) {
1573 err = -ENOMEM;
0157f60c 1574 goto errout;
70f8e78e
DM
1575 }
1576 sa->sa_family = dev->type;
da5e0494 1577 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
70f8e78e 1578 dev->addr_len);
e7c3273e 1579 err = dev_set_mac_address(dev, sa);
70f8e78e 1580 kfree(sa);
1da177e4 1581 if (err)
0157f60c 1582 goto errout;
90c325e3 1583 status |= DO_SETLINK_MODIFIED;
1da177e4
LT
1584 }
1585
da5e0494
TG
1586 if (tb[IFLA_MTU]) {
1587 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1588 if (err < 0)
0157f60c 1589 goto errout;
90c325e3 1590 status |= DO_SETLINK_MODIFIED;
1da177e4
LT
1591 }
1592
cbda10fa
VD
1593 if (tb[IFLA_GROUP]) {
1594 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
ba998906 1595 status |= DO_SETLINK_NOTIFY;
cbda10fa
VD
1596 }
1597
da5e0494
TG
1598 /*
1599 * Interface selected by interface index but interface
1600 * name provided implies that a name change has been
1601 * requested.
1602 */
51055be8 1603 if (ifm->ifi_index > 0 && ifname[0]) {
da5e0494
TG
1604 err = dev_change_name(dev, ifname);
1605 if (err < 0)
0157f60c 1606 goto errout;
90c325e3 1607 status |= DO_SETLINK_MODIFIED;
1da177e4
LT
1608 }
1609
0b815a1a
SH
1610 if (tb[IFLA_IFALIAS]) {
1611 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
1612 nla_len(tb[IFLA_IFALIAS]));
1613 if (err < 0)
1614 goto errout;
ba998906 1615 status |= DO_SETLINK_NOTIFY;
0b815a1a
SH
1616 }
1617
da5e0494
TG
1618 if (tb[IFLA_BROADCAST]) {
1619 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
e7c3273e 1620 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
1da177e4
LT
1621 }
1622
83b496e9 1623 if (ifm->ifi_flags || ifm->ifi_change) {
3729d502 1624 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
5f9021cf
JB
1625 if (err < 0)
1626 goto errout;
83b496e9 1627 }
1da177e4 1628
fbaec0ea
JP
1629 if (tb[IFLA_MASTER]) {
1630 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
1631 if (err)
1632 goto errout;
90c325e3 1633 status |= DO_SETLINK_MODIFIED;
fbaec0ea
JP
1634 }
1635
9a57247f
JP
1636 if (tb[IFLA_CARRIER]) {
1637 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
1638 if (err)
1639 goto errout;
90c325e3 1640 status |= DO_SETLINK_MODIFIED;
9a57247f
JP
1641 }
1642
5d1180fc
ND
1643 if (tb[IFLA_TXQLEN]) {
1644 unsigned long value = nla_get_u32(tb[IFLA_TXQLEN]);
1645
1646 if (dev->tx_queue_len ^ value)
ba998906 1647 status |= DO_SETLINK_NOTIFY;
5d1180fc
ND
1648
1649 dev->tx_queue_len = value;
1650 }
b00055aa 1651
da5e0494 1652 if (tb[IFLA_OPERSTATE])
93b2d4a2 1653 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
b00055aa 1654
da5e0494 1655 if (tb[IFLA_LINKMODE]) {
1889b0e7
ND
1656 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
1657
93b2d4a2 1658 write_lock_bh(&dev_base_lock);
1889b0e7 1659 if (dev->link_mode ^ value)
ba998906 1660 status |= DO_SETLINK_NOTIFY;
1889b0e7 1661 dev->link_mode = value;
93b2d4a2 1662 write_unlock_bh(&dev_base_lock);
b00055aa
SR
1663 }
1664
c02db8c6
CW
1665 if (tb[IFLA_VFINFO_LIST]) {
1666 struct nlattr *attr;
1667 int rem;
1668 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
253683bb
DH
1669 if (nla_type(attr) != IFLA_VF_INFO) {
1670 err = -EINVAL;
c02db8c6 1671 goto errout;
253683bb 1672 }
c02db8c6
CW
1673 err = do_setvfinfo(dev, attr);
1674 if (err < 0)
1675 goto errout;
ba998906 1676 status |= DO_SETLINK_NOTIFY;
c02db8c6 1677 }
ebc08a6f 1678 }
1da177e4
LT
1679 err = 0;
1680
57b61080
SF
1681 if (tb[IFLA_VF_PORTS]) {
1682 struct nlattr *port[IFLA_PORT_MAX+1];
1683 struct nlattr *attr;
1684 int vf;
1685 int rem;
1686
1687 err = -EOPNOTSUPP;
1688 if (!ops->ndo_set_vf_port)
1689 goto errout;
1690
1691 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
1692 if (nla_type(attr) != IFLA_VF_PORT)
1693 continue;
1694 err = nla_parse_nested(port, IFLA_PORT_MAX,
1695 attr, ifla_port_policy);
1696 if (err < 0)
1697 goto errout;
1698 if (!port[IFLA_PORT_VF]) {
1699 err = -EOPNOTSUPP;
1700 goto errout;
1701 }
1702 vf = nla_get_u32(port[IFLA_PORT_VF]);
1703 err = ops->ndo_set_vf_port(dev, vf, port);
1704 if (err < 0)
1705 goto errout;
ba998906 1706 status |= DO_SETLINK_NOTIFY;
57b61080
SF
1707 }
1708 }
1709 err = 0;
1710
1711 if (tb[IFLA_PORT_SELF]) {
1712 struct nlattr *port[IFLA_PORT_MAX+1];
1713
1714 err = nla_parse_nested(port, IFLA_PORT_MAX,
1715 tb[IFLA_PORT_SELF], ifla_port_policy);
1716 if (err < 0)
1717 goto errout;
1718
1719 err = -EOPNOTSUPP;
1720 if (ops->ndo_set_vf_port)
1721 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
1722 if (err < 0)
1723 goto errout;
ba998906 1724 status |= DO_SETLINK_NOTIFY;
57b61080 1725 }
f8ff182c
TG
1726
1727 if (tb[IFLA_AF_SPEC]) {
1728 struct nlattr *af;
1729 int rem;
1730
1731 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1732 const struct rtnl_af_ops *af_ops;
1733
1734 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
cf7afbfe 1735 BUG();
f8ff182c 1736
cf7afbfe 1737 err = af_ops->set_link_af(dev, af);
f8ff182c
TG
1738 if (err < 0)
1739 goto errout;
1740
ba998906 1741 status |= DO_SETLINK_NOTIFY;
f8ff182c
TG
1742 }
1743 }
57b61080
SF
1744 err = 0;
1745
0157f60c 1746errout:
ba998906
ND
1747 if (status & DO_SETLINK_MODIFIED) {
1748 if (status & DO_SETLINK_NOTIFY)
1749 netdev_state_change(dev);
1750
1751 if (err < 0)
1752 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",
1753 dev->name);
1754 }
da5e0494 1755
0157f60c
PM
1756 return err;
1757}
1da177e4 1758
661d2967 1759static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
0157f60c 1760{
3b1e0a65 1761 struct net *net = sock_net(skb->sk);
0157f60c
PM
1762 struct ifinfomsg *ifm;
1763 struct net_device *dev;
1764 int err;
1765 struct nlattr *tb[IFLA_MAX+1];
1766 char ifname[IFNAMSIZ];
1767
1768 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1769 if (err < 0)
1770 goto errout;
1771
1772 if (tb[IFLA_IFNAME])
1773 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1774 else
1775 ifname[0] = '\0';
1776
1777 err = -EINVAL;
1778 ifm = nlmsg_data(nlh);
1779 if (ifm->ifi_index > 0)
a3d12891 1780 dev = __dev_get_by_index(net, ifm->ifi_index);
0157f60c 1781 else if (tb[IFLA_IFNAME])
a3d12891 1782 dev = __dev_get_by_name(net, ifname);
0157f60c
PM
1783 else
1784 goto errout;
1785
1786 if (dev == NULL) {
1787 err = -ENODEV;
1788 goto errout;
1789 }
1790
e0d087af
ED
1791 err = validate_linkmsg(dev, tb);
1792 if (err < 0)
a3d12891 1793 goto errout;
0157f60c 1794
90f62cf3 1795 err = do_setlink(skb, dev, ifm, tb, ifname, 0);
da5e0494 1796errout:
1da177e4
LT
1797 return err;
1798}
1799
661d2967 1800static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
38f7b870 1801{
3b1e0a65 1802 struct net *net = sock_net(skb->sk);
38f7b870
PM
1803 const struct rtnl_link_ops *ops;
1804 struct net_device *dev;
1805 struct ifinfomsg *ifm;
1806 char ifname[IFNAMSIZ];
1807 struct nlattr *tb[IFLA_MAX+1];
1808 int err;
226bd341 1809 LIST_HEAD(list_kill);
38f7b870
PM
1810
1811 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1812 if (err < 0)
1813 return err;
1814
1815 if (tb[IFLA_IFNAME])
1816 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1817
1818 ifm = nlmsg_data(nlh);
1819 if (ifm->ifi_index > 0)
881d966b 1820 dev = __dev_get_by_index(net, ifm->ifi_index);
38f7b870 1821 else if (tb[IFLA_IFNAME])
881d966b 1822 dev = __dev_get_by_name(net, ifname);
38f7b870
PM
1823 else
1824 return -EINVAL;
1825
1826 if (!dev)
1827 return -ENODEV;
1828
1829 ops = dev->rtnl_link_ops;
b0ab2fab 1830 if (!ops || !ops->dellink)
38f7b870
PM
1831 return -EOPNOTSUPP;
1832
226bd341
ED
1833 ops->dellink(dev, &list_kill);
1834 unregister_netdevice_many(&list_kill);
38f7b870
PM
1835 return 0;
1836}
1837
3729d502
PM
1838int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
1839{
1840 unsigned int old_flags;
1841 int err;
1842
1843 old_flags = dev->flags;
1844 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
1845 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
1846 if (err < 0)
1847 return err;
1848 }
1849
1850 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
3729d502 1851
a528c219 1852 __dev_notify_flags(dev, old_flags, ~0U);
3729d502
PM
1853 return 0;
1854}
1855EXPORT_SYMBOL(rtnl_configure_link);
1856
c0713563 1857struct net_device *rtnl_create_link(struct net *net,
5517750f
TG
1858 char *ifname, unsigned char name_assign_type,
1859 const struct rtnl_link_ops *ops, struct nlattr *tb[])
e7199288
PE
1860{
1861 int err;
1862 struct net_device *dev;
d40156aa
JP
1863 unsigned int num_tx_queues = 1;
1864 unsigned int num_rx_queues = 1;
e7199288 1865
76ff5cc9
JP
1866 if (tb[IFLA_NUM_TX_QUEUES])
1867 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
1868 else if (ops->get_num_tx_queues)
d40156aa 1869 num_tx_queues = ops->get_num_tx_queues();
76ff5cc9
JP
1870
1871 if (tb[IFLA_NUM_RX_QUEUES])
1872 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
1873 else if (ops->get_num_rx_queues)
d40156aa 1874 num_rx_queues = ops->get_num_rx_queues();
efacb309 1875
e7199288 1876 err = -ENOMEM;
5517750f 1877 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
c835a677 1878 ops->setup, num_tx_queues, num_rx_queues);
e7199288
PE
1879 if (!dev)
1880 goto err;
1881
81adee47
EB
1882 dev_net_set(dev, net);
1883 dev->rtnl_link_ops = ops;
3729d502 1884 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
81adee47 1885
e7199288
PE
1886 if (tb[IFLA_MTU])
1887 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
2afb9b53 1888 if (tb[IFLA_ADDRESS]) {
e7199288
PE
1889 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
1890 nla_len(tb[IFLA_ADDRESS]));
2afb9b53
JP
1891 dev->addr_assign_type = NET_ADDR_SET;
1892 }
e7199288
PE
1893 if (tb[IFLA_BROADCAST])
1894 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
1895 nla_len(tb[IFLA_BROADCAST]));
1896 if (tb[IFLA_TXQLEN])
1897 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1898 if (tb[IFLA_OPERSTATE])
93b2d4a2 1899 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
e7199288
PE
1900 if (tb[IFLA_LINKMODE])
1901 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
ffa934f1
PM
1902 if (tb[IFLA_GROUP])
1903 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
e7199288
PE
1904
1905 return dev;
1906
e7199288
PE
1907err:
1908 return ERR_PTR(err);
1909}
e0d087af 1910EXPORT_SYMBOL(rtnl_create_link);
e7199288 1911
90f62cf3
EB
1912static int rtnl_group_changelink(const struct sk_buff *skb,
1913 struct net *net, int group,
e7ed828f
VD
1914 struct ifinfomsg *ifm,
1915 struct nlattr **tb)
1916{
1917 struct net_device *dev;
1918 int err;
1919
1920 for_each_netdev(net, dev) {
1921 if (dev->group == group) {
90f62cf3 1922 err = do_setlink(skb, dev, ifm, tb, NULL, 0);
e7ed828f
VD
1923 if (err < 0)
1924 return err;
1925 }
1926 }
1927
1928 return 0;
1929}
1930
661d2967 1931static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh)
38f7b870 1932{
3b1e0a65 1933 struct net *net = sock_net(skb->sk);
38f7b870 1934 const struct rtnl_link_ops *ops;
ba7d49b1 1935 const struct rtnl_link_ops *m_ops = NULL;
38f7b870 1936 struct net_device *dev;
ba7d49b1 1937 struct net_device *master_dev = NULL;
38f7b870
PM
1938 struct ifinfomsg *ifm;
1939 char kind[MODULE_NAME_LEN];
1940 char ifname[IFNAMSIZ];
1941 struct nlattr *tb[IFLA_MAX+1];
1942 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
5517750f 1943 unsigned char name_assign_type = NET_NAME_USER;
38f7b870
PM
1944 int err;
1945
95a5afca 1946#ifdef CONFIG_MODULES
38f7b870 1947replay:
8072f085 1948#endif
38f7b870
PM
1949 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1950 if (err < 0)
1951 return err;
1952
1953 if (tb[IFLA_IFNAME])
1954 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1955 else
1956 ifname[0] = '\0';
1957
1958 ifm = nlmsg_data(nlh);
1959 if (ifm->ifi_index > 0)
881d966b 1960 dev = __dev_get_by_index(net, ifm->ifi_index);
e7ed828f
VD
1961 else {
1962 if (ifname[0])
1963 dev = __dev_get_by_name(net, ifname);
e7ed828f
VD
1964 else
1965 dev = NULL;
1966 }
38f7b870 1967
ba7d49b1
JP
1968 if (dev) {
1969 master_dev = netdev_master_upper_dev_get(dev);
1970 if (master_dev)
1971 m_ops = master_dev->rtnl_link_ops;
1972 }
1973
e0d087af
ED
1974 err = validate_linkmsg(dev, tb);
1975 if (err < 0)
1840bb13
TG
1976 return err;
1977
38f7b870
PM
1978 if (tb[IFLA_LINKINFO]) {
1979 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
1980 tb[IFLA_LINKINFO], ifla_info_policy);
1981 if (err < 0)
1982 return err;
1983 } else
1984 memset(linkinfo, 0, sizeof(linkinfo));
1985
1986 if (linkinfo[IFLA_INFO_KIND]) {
1987 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
1988 ops = rtnl_link_ops_get(kind);
1989 } else {
1990 kind[0] = '\0';
1991 ops = NULL;
1992 }
1993
1994 if (1) {
ba7d49b1
JP
1995 struct nlattr *attr[ops ? ops->maxtype + 1 : 0];
1996 struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 0];
1997 struct nlattr **data = NULL;
1998 struct nlattr **slave_data = NULL;
81adee47 1999 struct net *dest_net;
38f7b870
PM
2000
2001 if (ops) {
2002 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
2003 err = nla_parse_nested(attr, ops->maxtype,
2004 linkinfo[IFLA_INFO_DATA],
2005 ops->policy);
2006 if (err < 0)
2007 return err;
2008 data = attr;
2009 }
2010 if (ops->validate) {
2011 err = ops->validate(tb, data);
2012 if (err < 0)
2013 return err;
2014 }
2015 }
2016
ba7d49b1
JP
2017 if (m_ops) {
2018 if (m_ops->slave_maxtype &&
2019 linkinfo[IFLA_INFO_SLAVE_DATA]) {
2020 err = nla_parse_nested(slave_attr,
2021 m_ops->slave_maxtype,
2022 linkinfo[IFLA_INFO_SLAVE_DATA],
2023 m_ops->slave_policy);
2024 if (err < 0)
2025 return err;
2026 slave_data = slave_attr;
2027 }
2028 if (m_ops->slave_validate) {
2029 err = m_ops->slave_validate(tb, slave_data);
2030 if (err < 0)
2031 return err;
2032 }
2033 }
2034
38f7b870 2035 if (dev) {
90c325e3 2036 int status = 0;
38f7b870
PM
2037
2038 if (nlh->nlmsg_flags & NLM_F_EXCL)
2039 return -EEXIST;
2040 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2041 return -EOPNOTSUPP;
2042
2043 if (linkinfo[IFLA_INFO_DATA]) {
2044 if (!ops || ops != dev->rtnl_link_ops ||
2045 !ops->changelink)
2046 return -EOPNOTSUPP;
2047
2048 err = ops->changelink(dev, tb, data);
2049 if (err < 0)
2050 return err;
ba998906 2051 status |= DO_SETLINK_NOTIFY;
38f7b870
PM
2052 }
2053
ba7d49b1
JP
2054 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2055 if (!m_ops || !m_ops->slave_changelink)
2056 return -EOPNOTSUPP;
2057
2058 err = m_ops->slave_changelink(master_dev, dev,
2059 tb, slave_data);
2060 if (err < 0)
2061 return err;
ba998906 2062 status |= DO_SETLINK_NOTIFY;
ba7d49b1
JP
2063 }
2064
90c325e3 2065 return do_setlink(skb, dev, ifm, tb, ifname, status);
38f7b870
PM
2066 }
2067
ffa934f1
PM
2068 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2069 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
90f62cf3 2070 return rtnl_group_changelink(skb, net,
ffa934f1
PM
2071 nla_get_u32(tb[IFLA_GROUP]),
2072 ifm, tb);
38f7b870 2073 return -ENODEV;
ffa934f1 2074 }
38f7b870 2075
0e06877c 2076 if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
38f7b870
PM
2077 return -EOPNOTSUPP;
2078
2079 if (!ops) {
95a5afca 2080#ifdef CONFIG_MODULES
38f7b870
PM
2081 if (kind[0]) {
2082 __rtnl_unlock();
2083 request_module("rtnl-link-%s", kind);
2084 rtnl_lock();
2085 ops = rtnl_link_ops_get(kind);
2086 if (ops)
2087 goto replay;
2088 }
2089#endif
2090 return -EOPNOTSUPP;
2091 }
2092
b0ab2fab
JP
2093 if (!ops->setup)
2094 return -EOPNOTSUPP;
2095
5517750f 2096 if (!ifname[0]) {
38f7b870 2097 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
5517750f
TG
2098 name_assign_type = NET_NAME_ENUM;
2099 }
e7199288 2100
81adee47 2101 dest_net = rtnl_link_get_net(net, tb);
13ad1774
EB
2102 if (IS_ERR(dest_net))
2103 return PTR_ERR(dest_net);
2104
5517750f 2105 dev = rtnl_create_link(dest_net, ifname, name_assign_type, ops, tb);
9c7dafbf 2106 if (IS_ERR(dev)) {
e7199288 2107 err = PTR_ERR(dev);
9c7dafbf
PE
2108 goto out;
2109 }
2110
2111 dev->ifindex = ifm->ifi_index;
2112
0e0eee24 2113 if (ops->newlink) {
81adee47 2114 err = ops->newlink(net, dev, tb, data);
0e0eee24 2115 /* Drivers should call free_netdev() in ->destructor
e51fb152
CW
2116 * and unregister it on failure after registration
2117 * so that device could be finally freed in rtnl_unlock.
0e0eee24 2118 */
e51fb152
CW
2119 if (err < 0) {
2120 /* If device is not registered at all, free it now */
2121 if (dev->reg_state == NETREG_UNINITIALIZED)
2122 free_netdev(dev);
0e0eee24 2123 goto out;
e51fb152 2124 }
0e0eee24 2125 } else {
2d85cba2 2126 err = register_netdevice(dev);
0e0eee24
CW
2127 if (err < 0) {
2128 free_netdev(dev);
2129 goto out;
2130 }
fce9b9be 2131 }
3729d502
PM
2132 err = rtnl_configure_link(dev, ifm);
2133 if (err < 0)
2134 unregister_netdevice(dev);
2135out:
81adee47 2136 put_net(dest_net);
38f7b870
PM
2137 return err;
2138 }
2139}
2140
661d2967 2141static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh)
711e2c33 2142{
3b1e0a65 2143 struct net *net = sock_net(skb->sk);
b60c5115 2144 struct ifinfomsg *ifm;
a3d12891 2145 char ifname[IFNAMSIZ];
b60c5115
TG
2146 struct nlattr *tb[IFLA_MAX+1];
2147 struct net_device *dev = NULL;
2148 struct sk_buff *nskb;
339bf98f 2149 int err;
115c9b81 2150 u32 ext_filter_mask = 0;
711e2c33 2151
b60c5115
TG
2152 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2153 if (err < 0)
9918f230 2154 return err;
b60c5115 2155
a3d12891
ED
2156 if (tb[IFLA_IFNAME])
2157 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2158
115c9b81
GR
2159 if (tb[IFLA_EXT_MASK])
2160 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2161
b60c5115 2162 ifm = nlmsg_data(nlh);
a3d12891
ED
2163 if (ifm->ifi_index > 0)
2164 dev = __dev_get_by_index(net, ifm->ifi_index);
2165 else if (tb[IFLA_IFNAME])
2166 dev = __dev_get_by_name(net, ifname);
2167 else
711e2c33 2168 return -EINVAL;
711e2c33 2169
a3d12891
ED
2170 if (dev == NULL)
2171 return -ENODEV;
2172
115c9b81 2173 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
a3d12891
ED
2174 if (nskb == NULL)
2175 return -ENOBUFS;
b60c5115 2176
15e47304 2177 err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
115c9b81 2178 nlh->nlmsg_seq, 0, 0, ext_filter_mask);
26932566
PM
2179 if (err < 0) {
2180 /* -EMSGSIZE implies BUG in if_nlmsg_size */
2181 WARN_ON(err == -EMSGSIZE);
2182 kfree_skb(nskb);
a3d12891 2183 } else
15e47304 2184 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
711e2c33 2185
b60c5115 2186 return err;
711e2c33 2187}
711e2c33 2188
115c9b81 2189static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
c7ac8679 2190{
115c9b81
GR
2191 struct net *net = sock_net(skb->sk);
2192 struct net_device *dev;
2193 struct nlattr *tb[IFLA_MAX+1];
2194 u32 ext_filter_mask = 0;
2195 u16 min_ifinfo_dump_size = 0;
e5eca6d4
MS
2196 int hdrlen;
2197
2198 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2199 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2200 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
115c9b81 2201
e5eca6d4 2202 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
a4b64fbe
ED
2203 if (tb[IFLA_EXT_MASK])
2204 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2205 }
115c9b81
GR
2206
2207 if (!ext_filter_mask)
2208 return NLMSG_GOODSIZE;
2209 /*
2210 * traverse the list of net devices and compute the minimum
2211 * buffer size based upon the filter mask.
2212 */
2213 list_for_each_entry(dev, &net->dev_base_head, dev_list) {
2214 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
2215 if_nlmsg_size(dev,
2216 ext_filter_mask));
2217 }
2218
c7ac8679
GR
2219 return min_ifinfo_dump_size;
2220}
2221
42bad1da 2222static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
1da177e4
LT
2223{
2224 int idx;
2225 int s_idx = cb->family;
2226
2227 if (s_idx == 0)
2228 s_idx = 1;
25239cee 2229 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
1da177e4
LT
2230 int type = cb->nlh->nlmsg_type-RTM_BASE;
2231 if (idx < s_idx || idx == PF_PACKET)
2232 continue;
e2849863
TG
2233 if (rtnl_msg_handlers[idx] == NULL ||
2234 rtnl_msg_handlers[idx][type].dumpit == NULL)
1da177e4 2235 continue;
0465277f 2236 if (idx > s_idx) {
1da177e4 2237 memset(&cb->args[0], 0, sizeof(cb->args));
0465277f
ND
2238 cb->prev_seq = 0;
2239 cb->seq = 0;
2240 }
e2849863 2241 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
1da177e4
LT
2242 break;
2243 }
2244 cb->family = idx;
2245
2246 return skb->len;
2247}
2248
395eea6c
MB
2249struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
2250 unsigned int change, gfp_t flags)
1da177e4 2251{
c346dca1 2252 struct net *net = dev_net(dev);
1da177e4 2253 struct sk_buff *skb;
0ec6d3f4 2254 int err = -ENOBUFS;
c7ac8679 2255 size_t if_info_size;
1da177e4 2256
7f294054 2257 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
0ec6d3f4
TG
2258 if (skb == NULL)
2259 goto errout;
1da177e4 2260
115c9b81 2261 err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
26932566
PM
2262 if (err < 0) {
2263 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
2264 WARN_ON(err == -EMSGSIZE);
2265 kfree_skb(skb);
2266 goto errout;
2267 }
395eea6c 2268 return skb;
0ec6d3f4
TG
2269errout:
2270 if (err < 0)
4b3da706 2271 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
395eea6c
MB
2272 return NULL;
2273}
2274
2275void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
2276{
2277 struct net *net = dev_net(dev);
2278
2279 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
2280}
2281
2282void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
2283 gfp_t flags)
2284{
2285 struct sk_buff *skb;
2286
2287 skb = rtmsg_ifinfo_build_skb(type, dev, change, flags);
2288 if (skb)
2289 rtmsg_ifinfo_send(skb, dev, flags);
1da177e4 2290}
471cb5a3 2291EXPORT_SYMBOL(rtmsg_ifinfo);
1da177e4 2292
d83b0603
JF
2293static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
2294 struct net_device *dev,
2295 u8 *addr, u32 pid, u32 seq,
1c104a6b
ND
2296 int type, unsigned int flags,
2297 int nlflags)
d83b0603
JF
2298{
2299 struct nlmsghdr *nlh;
2300 struct ndmsg *ndm;
2301
1c104a6b 2302 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
d83b0603
JF
2303 if (!nlh)
2304 return -EMSGSIZE;
2305
2306 ndm = nlmsg_data(nlh);
2307 ndm->ndm_family = AF_BRIDGE;
2308 ndm->ndm_pad1 = 0;
2309 ndm->ndm_pad2 = 0;
2310 ndm->ndm_flags = flags;
2311 ndm->ndm_type = 0;
2312 ndm->ndm_ifindex = dev->ifindex;
2313 ndm->ndm_state = NUD_PERMANENT;
2314
2315 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
2316 goto nla_put_failure;
2317
2318 return nlmsg_end(skb, nlh);
2319
2320nla_put_failure:
2321 nlmsg_cancel(skb, nlh);
2322 return -EMSGSIZE;
2323}
2324
3ff661c3
JF
2325static inline size_t rtnl_fdb_nlmsg_size(void)
2326{
2327 return NLMSG_ALIGN(sizeof(struct ndmsg)) + nla_total_size(ETH_ALEN);
2328}
2329
2330static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, int type)
2331{
2332 struct net *net = dev_net(dev);
2333 struct sk_buff *skb;
2334 int err = -ENOBUFS;
2335
2336 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
2337 if (!skb)
2338 goto errout;
2339
1c104a6b 2340 err = nlmsg_populate_fdb_fill(skb, dev, addr, 0, 0, type, NTF_SELF, 0);
3ff661c3
JF
2341 if (err < 0) {
2342 kfree_skb(skb);
2343 goto errout;
2344 }
2345
2346 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
2347 return;
2348errout:
2349 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
2350}
2351
090096bf
VY
2352/**
2353 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2354 */
2355int ndo_dflt_fdb_add(struct ndmsg *ndm,
2356 struct nlattr *tb[],
2357 struct net_device *dev,
f6f6424b 2358 const unsigned char *addr, u16 vid,
090096bf
VY
2359 u16 flags)
2360{
2361 int err = -EINVAL;
2362
2363 /* If aging addresses are supported device will need to
2364 * implement its own handler for this.
2365 */
2366 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2367 pr_info("%s: FDB only supports static addresses\n", dev->name);
2368 return err;
2369 }
2370
65891fea
OG
2371 if (vid) {
2372 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
2373 return err;
2374 }
2375
090096bf
VY
2376 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2377 err = dev_uc_add_excl(dev, addr);
2378 else if (is_multicast_ether_addr(addr))
2379 err = dev_mc_add_excl(dev, addr);
2380
2381 /* Only return duplicate errors if NLM_F_EXCL is set */
2382 if (err == -EEXIST && !(flags & NLM_F_EXCL))
2383 err = 0;
2384
2385 return err;
2386}
2387EXPORT_SYMBOL(ndo_dflt_fdb_add);
2388
f6f6424b
JP
2389static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid)
2390{
2391 u16 vid = 0;
2392
2393 if (vlan_attr) {
2394 if (nla_len(vlan_attr) != sizeof(u16)) {
2395 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n");
2396 return -EINVAL;
2397 }
2398
2399 vid = nla_get_u16(vlan_attr);
2400
2401 if (!vid || vid >= VLAN_VID_MASK) {
2402 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n",
2403 vid);
2404 return -EINVAL;
2405 }
2406 }
2407 *p_vid = vid;
2408 return 0;
2409}
2410
661d2967 2411static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh)
77162022
JF
2412{
2413 struct net *net = sock_net(skb->sk);
77162022
JF
2414 struct ndmsg *ndm;
2415 struct nlattr *tb[NDA_MAX+1];
2416 struct net_device *dev;
2417 u8 *addr;
f6f6424b 2418 u16 vid;
77162022
JF
2419 int err;
2420
2421 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2422 if (err < 0)
2423 return err;
2424
2425 ndm = nlmsg_data(nlh);
2426 if (ndm->ndm_ifindex == 0) {
2427 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
2428 return -EINVAL;
2429 }
2430
2431 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2432 if (dev == NULL) {
2433 pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
2434 return -ENODEV;
2435 }
2436
2437 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2438 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
2439 return -EINVAL;
2440 }
2441
2442 addr = nla_data(tb[NDA_LLADDR]);
77162022 2443
f6f6424b
JP
2444 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2445 if (err)
2446 return err;
2447
77162022
JF
2448 err = -EOPNOTSUPP;
2449
2450 /* Support fdb on master device the net/bridge default case */
2451 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2452 (dev->priv_flags & IFF_BRIDGE_PORT)) {
898e5061
JP
2453 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2454 const struct net_device_ops *ops = br_dev->netdev_ops;
2455
f6f6424b
JP
2456 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
2457 nlh->nlmsg_flags);
77162022
JF
2458 if (err)
2459 goto out;
2460 else
2461 ndm->ndm_flags &= ~NTF_MASTER;
2462 }
2463
2464 /* Embedded bridge, macvlan, and any other device support */
090096bf
VY
2465 if ((ndm->ndm_flags & NTF_SELF)) {
2466 if (dev->netdev_ops->ndo_fdb_add)
2467 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
f6f6424b 2468 vid,
090096bf
VY
2469 nlh->nlmsg_flags);
2470 else
f6f6424b 2471 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
090096bf 2472 nlh->nlmsg_flags);
77162022 2473
3ff661c3
JF
2474 if (!err) {
2475 rtnl_fdb_notify(dev, addr, RTM_NEWNEIGH);
77162022 2476 ndm->ndm_flags &= ~NTF_SELF;
3ff661c3 2477 }
77162022
JF
2478 }
2479out:
2480 return err;
2481}
2482
090096bf
VY
2483/**
2484 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
2485 */
2486int ndo_dflt_fdb_del(struct ndmsg *ndm,
2487 struct nlattr *tb[],
2488 struct net_device *dev,
f6f6424b 2489 const unsigned char *addr, u16 vid)
090096bf 2490{
c8a89c4a 2491 int err = -EINVAL;
090096bf
VY
2492
2493 /* If aging addresses are supported device will need to
2494 * implement its own handler for this.
2495 */
64535993 2496 if (!(ndm->ndm_state & NUD_PERMANENT)) {
090096bf 2497 pr_info("%s: FDB only supports static addresses\n", dev->name);
c8a89c4a 2498 return err;
090096bf
VY
2499 }
2500
2501 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2502 err = dev_uc_del(dev, addr);
2503 else if (is_multicast_ether_addr(addr))
2504 err = dev_mc_del(dev, addr);
090096bf
VY
2505
2506 return err;
2507}
2508EXPORT_SYMBOL(ndo_dflt_fdb_del);
2509
661d2967 2510static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh)
77162022
JF
2511{
2512 struct net *net = sock_net(skb->sk);
2513 struct ndmsg *ndm;
1690be63 2514 struct nlattr *tb[NDA_MAX+1];
77162022
JF
2515 struct net_device *dev;
2516 int err = -EINVAL;
2517 __u8 *addr;
f6f6424b 2518 u16 vid;
77162022 2519
90f62cf3 2520 if (!netlink_capable(skb, CAP_NET_ADMIN))
1690be63
VY
2521 return -EPERM;
2522
2523 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2524 if (err < 0)
2525 return err;
77162022
JF
2526
2527 ndm = nlmsg_data(nlh);
2528 if (ndm->ndm_ifindex == 0) {
2529 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
2530 return -EINVAL;
2531 }
2532
2533 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2534 if (dev == NULL) {
2535 pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
2536 return -ENODEV;
2537 }
2538
1690be63
VY
2539 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2540 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
2541 return -EINVAL;
2542 }
2543
2544 addr = nla_data(tb[NDA_LLADDR]);
77162022 2545
f6f6424b
JP
2546 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2547 if (err)
2548 return err;
2549
77162022
JF
2550 err = -EOPNOTSUPP;
2551
2552 /* Support fdb on master device the net/bridge default case */
2553 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2554 (dev->priv_flags & IFF_BRIDGE_PORT)) {
898e5061
JP
2555 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2556 const struct net_device_ops *ops = br_dev->netdev_ops;
77162022 2557
898e5061 2558 if (ops->ndo_fdb_del)
f6f6424b 2559 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
77162022
JF
2560
2561 if (err)
2562 goto out;
2563 else
2564 ndm->ndm_flags &= ~NTF_MASTER;
2565 }
2566
2567 /* Embedded bridge, macvlan, and any other device support */
090096bf
VY
2568 if (ndm->ndm_flags & NTF_SELF) {
2569 if (dev->netdev_ops->ndo_fdb_del)
f6f6424b
JP
2570 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
2571 vid);
090096bf 2572 else
f6f6424b 2573 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
77162022 2574
3ff661c3
JF
2575 if (!err) {
2576 rtnl_fdb_notify(dev, addr, RTM_DELNEIGH);
77162022 2577 ndm->ndm_flags &= ~NTF_SELF;
3ff661c3 2578 }
77162022
JF
2579 }
2580out:
2581 return err;
2582}
2583
d83b0603
JF
2584static int nlmsg_populate_fdb(struct sk_buff *skb,
2585 struct netlink_callback *cb,
2586 struct net_device *dev,
2587 int *idx,
2588 struct netdev_hw_addr_list *list)
2589{
2590 struct netdev_hw_addr *ha;
2591 int err;
15e47304 2592 u32 portid, seq;
d83b0603 2593
15e47304 2594 portid = NETLINK_CB(cb->skb).portid;
d83b0603
JF
2595 seq = cb->nlh->nlmsg_seq;
2596
2597 list_for_each_entry(ha, &list->list, list) {
2598 if (*idx < cb->args[0])
2599 goto skip;
2600
2601 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr,
a7a558fe 2602 portid, seq,
1c104a6b
ND
2603 RTM_NEWNEIGH, NTF_SELF,
2604 NLM_F_MULTI);
d83b0603
JF
2605 if (err < 0)
2606 return err;
2607skip:
2608 *idx += 1;
2609 }
2610 return 0;
2611}
2612
2613/**
2c53040f 2614 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
d83b0603
JF
2615 * @nlh: netlink message header
2616 * @dev: netdevice
2617 *
2618 * Default netdevice operation to dump the existing unicast address list.
91f3e7b1 2619 * Returns number of addresses from list put in skb.
d83b0603
JF
2620 */
2621int ndo_dflt_fdb_dump(struct sk_buff *skb,
2622 struct netlink_callback *cb,
2623 struct net_device *dev,
5d5eacb3 2624 struct net_device *filter_dev,
d83b0603
JF
2625 int idx)
2626{
2627 int err;
2628
2629 netif_addr_lock_bh(dev);
2630 err = nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->uc);
2631 if (err)
2632 goto out;
2633 nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->mc);
2634out:
2635 netif_addr_unlock_bh(dev);
2636 return idx;
2637}
2638EXPORT_SYMBOL(ndo_dflt_fdb_dump);
2639
77162022
JF
2640static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
2641{
77162022 2642 struct net_device *dev;
5e6d2435
JHS
2643 struct nlattr *tb[IFLA_MAX+1];
2644 struct net_device *bdev = NULL;
2645 struct net_device *br_dev = NULL;
2646 const struct net_device_ops *ops = NULL;
2647 const struct net_device_ops *cops = NULL;
2648 struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
2649 struct net *net = sock_net(skb->sk);
2650 int brport_idx = 0;
2651 int br_idx = 0;
2652 int idx = 0;
2653
2654 if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
2655 ifla_policy) == 0) {
2656 if (tb[IFLA_MASTER])
2657 br_idx = nla_get_u32(tb[IFLA_MASTER]);
2658 }
2659
2660 brport_idx = ifm->ifi_index;
2661
2662 if (br_idx) {
2663 br_dev = __dev_get_by_index(net, br_idx);
2664 if (!br_dev)
2665 return -ENODEV;
2666
2667 ops = br_dev->netdev_ops;
2668 bdev = br_dev;
2669 }
2670
2671 for_each_netdev(net, dev) {
2672 if (brport_idx && (dev->ifindex != brport_idx))
2673 continue;
2674
2675 if (!br_idx) { /* user did not specify a specific bridge */
2676 if (dev->priv_flags & IFF_BRIDGE_PORT) {
2677 br_dev = netdev_master_upper_dev_get(dev);
2678 cops = br_dev->netdev_ops;
2679 }
2680
2681 bdev = dev;
2682 } else {
2683 if (dev != br_dev &&
2684 !(dev->priv_flags & IFF_BRIDGE_PORT))
2685 continue;
2686
2687 if (br_dev != netdev_master_upper_dev_get(dev) &&
2688 !(dev->priv_flags & IFF_EBRIDGE))
2689 continue;
2690
2691 bdev = br_dev;
2692 cops = ops;
2693 }
77162022 2694
77162022 2695 if (dev->priv_flags & IFF_BRIDGE_PORT) {
5e6d2435
JHS
2696 if (cops && cops->ndo_fdb_dump)
2697 idx = cops->ndo_fdb_dump(skb, cb, br_dev, dev,
2698 idx);
77162022
JF
2699 }
2700
5e6d2435 2701 idx = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx);
77162022 2702 if (dev->netdev_ops->ndo_fdb_dump)
5e6d2435 2703 idx = dev->netdev_ops->ndo_fdb_dump(skb, cb, bdev, dev,
5d5eacb3 2704 idx);
5e6d2435
JHS
2705
2706 cops = NULL;
77162022 2707 }
77162022
JF
2708
2709 cb->args[0] = idx;
2710 return skb->len;
2711}
2712
2c3c031c
SF
2713static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
2714 unsigned int attrnum, unsigned int flag)
2715{
2716 if (mask & flag)
2717 return nla_put_u8(skb, attrnum, !!(flags & flag));
2718 return 0;
2719}
2720
815cccbf 2721int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
2c3c031c
SF
2722 struct net_device *dev, u16 mode,
2723 u32 flags, u32 mask)
815cccbf
JF
2724{
2725 struct nlmsghdr *nlh;
2726 struct ifinfomsg *ifm;
2727 struct nlattr *br_afspec;
2c3c031c 2728 struct nlattr *protinfo;
815cccbf 2729 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
898e5061 2730 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
815cccbf
JF
2731
2732 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), NLM_F_MULTI);
2733 if (nlh == NULL)
2734 return -EMSGSIZE;
2735
2736 ifm = nlmsg_data(nlh);
2737 ifm->ifi_family = AF_BRIDGE;
2738 ifm->__ifi_pad = 0;
2739 ifm->ifi_type = dev->type;
2740 ifm->ifi_index = dev->ifindex;
2741 ifm->ifi_flags = dev_get_flags(dev);
2742 ifm->ifi_change = 0;
2743
2744
2745 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
2746 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
2747 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
898e5061
JP
2748 (br_dev &&
2749 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
815cccbf
JF
2750 (dev->addr_len &&
2751 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
2752 (dev->ifindex != dev->iflink &&
2753 nla_put_u32(skb, IFLA_LINK, dev->iflink)))
2754 goto nla_put_failure;
2755
2756 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
2757 if (!br_afspec)
2758 goto nla_put_failure;
2759
1d460b98 2760 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
815cccbf
JF
2761 nla_nest_cancel(skb, br_afspec);
2762 goto nla_put_failure;
2763 }
1d460b98
RP
2764
2765 if (mode != BRIDGE_MODE_UNDEF) {
2766 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
2767 nla_nest_cancel(skb, br_afspec);
2768 goto nla_put_failure;
2769 }
2770 }
815cccbf
JF
2771 nla_nest_end(skb, br_afspec);
2772
2c3c031c
SF
2773 protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
2774 if (!protinfo)
2775 goto nla_put_failure;
2776
2777 if (brport_nla_put_flag(skb, flags, mask,
2778 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
2779 brport_nla_put_flag(skb, flags, mask,
2780 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
2781 brport_nla_put_flag(skb, flags, mask,
2782 IFLA_BRPORT_FAST_LEAVE,
2783 BR_MULTICAST_FAST_LEAVE) ||
2784 brport_nla_put_flag(skb, flags, mask,
2785 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
2786 brport_nla_put_flag(skb, flags, mask,
2787 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
2788 brport_nla_put_flag(skb, flags, mask,
2789 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
2790 brport_nla_put_flag(skb, flags, mask,
2791 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
2792 brport_nla_put_flag(skb, flags, mask,
2793 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
2794 nla_nest_cancel(skb, protinfo);
2795 goto nla_put_failure;
2796 }
2797
2798 nla_nest_end(skb, protinfo);
2799
815cccbf
JF
2800 return nlmsg_end(skb, nlh);
2801nla_put_failure:
2802 nlmsg_cancel(skb, nlh);
2803 return -EMSGSIZE;
2804}
2805EXPORT_SYMBOL(ndo_dflt_bridge_getlink);
2806
e5a55a89
JF
2807static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
2808{
2809 struct net *net = sock_net(skb->sk);
2810 struct net_device *dev;
2811 int idx = 0;
2812 u32 portid = NETLINK_CB(cb->skb).portid;
2813 u32 seq = cb->nlh->nlmsg_seq;
6cbdceeb
VY
2814 u32 filter_mask = 0;
2815
aa68c20f
TG
2816 if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
2817 struct nlattr *extfilt;
2818
2819 extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
2820 IFLA_EXT_MASK);
2821 if (extfilt) {
2822 if (nla_len(extfilt) < sizeof(filter_mask))
2823 return -EINVAL;
2824
2825 filter_mask = nla_get_u32(extfilt);
2826 }
2827 }
e5a55a89
JF
2828
2829 rcu_read_lock();
2830 for_each_netdev_rcu(net, dev) {
2831 const struct net_device_ops *ops = dev->netdev_ops;
898e5061 2832 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
e5a55a89 2833
898e5061 2834 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
25b1e679 2835 if (idx >= cb->args[0] &&
898e5061 2836 br_dev->netdev_ops->ndo_bridge_getlink(
6cbdceeb 2837 skb, portid, seq, dev, filter_mask) < 0)
e5a55a89 2838 break;
25b1e679 2839 idx++;
e5a55a89
JF
2840 }
2841
2842 if (ops->ndo_bridge_getlink) {
25b1e679 2843 if (idx >= cb->args[0] &&
6cbdceeb
VY
2844 ops->ndo_bridge_getlink(skb, portid, seq, dev,
2845 filter_mask) < 0)
e5a55a89 2846 break;
25b1e679 2847 idx++;
e5a55a89
JF
2848 }
2849 }
2850 rcu_read_unlock();
2851 cb->args[0] = idx;
2852
2853 return skb->len;
2854}
2855
2469ffd7
JF
2856static inline size_t bridge_nlmsg_size(void)
2857{
2858 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
2859 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
2860 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
2861 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
2862 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
2863 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
2864 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
2865 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
2866 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
2867 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
2868 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
2869}
2870
2871static int rtnl_bridge_notify(struct net_device *dev, u16 flags)
2872{
2873 struct net *net = dev_net(dev);
898e5061 2874 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2469ffd7
JF
2875 struct sk_buff *skb;
2876 int err = -EOPNOTSUPP;
2877
2878 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
2879 if (!skb) {
2880 err = -ENOMEM;
2881 goto errout;
2882 }
2883
c38e01b8 2884 if ((!flags || (flags & BRIDGE_FLAGS_MASTER)) &&
898e5061 2885 br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
6cbdceeb 2886 err = br_dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0);
c38e01b8
JF
2887 if (err < 0)
2888 goto errout;
2889 }
2469ffd7 2890
c38e01b8
JF
2891 if ((flags & BRIDGE_FLAGS_SELF) &&
2892 dev->netdev_ops->ndo_bridge_getlink) {
6cbdceeb 2893 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0);
c38e01b8
JF
2894 if (err < 0)
2895 goto errout;
2896 }
2469ffd7 2897
59ccaaaa
RP
2898 if (!skb->len)
2899 goto errout;
2900
2469ffd7
JF
2901 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
2902 return 0;
2903errout:
2904 WARN_ON(err == -EMSGSIZE);
2905 kfree_skb(skb);
59ccaaaa
RP
2906 if (err)
2907 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
2469ffd7
JF
2908 return err;
2909}
2910
661d2967 2911static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
e5a55a89
JF
2912{
2913 struct net *net = sock_net(skb->sk);
2914 struct ifinfomsg *ifm;
2915 struct net_device *dev;
2469ffd7
JF
2916 struct nlattr *br_spec, *attr = NULL;
2917 int rem, err = -EOPNOTSUPP;
c38e01b8
JF
2918 u16 oflags, flags = 0;
2919 bool have_flags = false;
e5a55a89
JF
2920
2921 if (nlmsg_len(nlh) < sizeof(*ifm))
2922 return -EINVAL;
2923
2924 ifm = nlmsg_data(nlh);
2925 if (ifm->ifi_family != AF_BRIDGE)
2926 return -EPFNOSUPPORT;
2927
2928 dev = __dev_get_by_index(net, ifm->ifi_index);
2929 if (!dev) {
2930 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
2931 return -ENODEV;
2932 }
2933
2469ffd7
JF
2934 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
2935 if (br_spec) {
2936 nla_for_each_nested(attr, br_spec, rem) {
2937 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
6e8d1c55
TG
2938 if (nla_len(attr) < sizeof(flags))
2939 return -EINVAL;
2940
c38e01b8 2941 have_flags = true;
2469ffd7
JF
2942 flags = nla_get_u16(attr);
2943 break;
2944 }
2945 }
2946 }
2947
c38e01b8
JF
2948 oflags = flags;
2949
2469ffd7 2950 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
898e5061
JP
2951 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2952
2953 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
2469ffd7
JF
2954 err = -EOPNOTSUPP;
2955 goto out;
2956 }
2957
898e5061 2958 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh);
e5a55a89
JF
2959 if (err)
2960 goto out;
2469ffd7
JF
2961
2962 flags &= ~BRIDGE_FLAGS_MASTER;
e5a55a89
JF
2963 }
2964
2469ffd7
JF
2965 if ((flags & BRIDGE_FLAGS_SELF)) {
2966 if (!dev->netdev_ops->ndo_bridge_setlink)
2967 err = -EOPNOTSUPP;
2968 else
2969 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh);
2970
2971 if (!err)
2972 flags &= ~BRIDGE_FLAGS_SELF;
2973 }
e5a55a89 2974
c38e01b8 2975 if (have_flags)
2469ffd7
JF
2976 memcpy(nla_data(attr), &flags, sizeof(flags));
2977 /* Generate event to notify upper layer of bridge change */
2978 if (!err)
c38e01b8 2979 err = rtnl_bridge_notify(dev, oflags);
e5a55a89
JF
2980out:
2981 return err;
2982}
2983
661d2967 2984static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
407af329
VY
2985{
2986 struct net *net = sock_net(skb->sk);
2987 struct ifinfomsg *ifm;
2988 struct net_device *dev;
2989 struct nlattr *br_spec, *attr = NULL;
2990 int rem, err = -EOPNOTSUPP;
2991 u16 oflags, flags = 0;
2992 bool have_flags = false;
2993
2994 if (nlmsg_len(nlh) < sizeof(*ifm))
2995 return -EINVAL;
2996
2997 ifm = nlmsg_data(nlh);
2998 if (ifm->ifi_family != AF_BRIDGE)
2999 return -EPFNOSUPPORT;
3000
3001 dev = __dev_get_by_index(net, ifm->ifi_index);
3002 if (!dev) {
3003 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3004 return -ENODEV;
3005 }
3006
3007 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3008 if (br_spec) {
3009 nla_for_each_nested(attr, br_spec, rem) {
3010 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
6e8d1c55
TG
3011 if (nla_len(attr) < sizeof(flags))
3012 return -EINVAL;
3013
407af329
VY
3014 have_flags = true;
3015 flags = nla_get_u16(attr);
3016 break;
3017 }
3018 }
3019 }
3020
3021 oflags = flags;
3022
3023 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3024 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3025
3026 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
3027 err = -EOPNOTSUPP;
3028 goto out;
3029 }
3030
3031 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh);
3032 if (err)
3033 goto out;
3034
3035 flags &= ~BRIDGE_FLAGS_MASTER;
3036 }
3037
3038 if ((flags & BRIDGE_FLAGS_SELF)) {
3039 if (!dev->netdev_ops->ndo_bridge_dellink)
3040 err = -EOPNOTSUPP;
3041 else
3042 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh);
3043
3044 if (!err)
3045 flags &= ~BRIDGE_FLAGS_SELF;
3046 }
3047
3048 if (have_flags)
3049 memcpy(nla_data(attr), &flags, sizeof(flags));
3050 /* Generate event to notify upper layer of bridge change */
3051 if (!err)
3052 err = rtnl_bridge_notify(dev, oflags);
3053out:
3054 return err;
3055}
3056
1da177e4
LT
3057/* Process one rtnetlink message. */
3058
1d00a4eb 3059static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
1da177e4 3060{
3b1e0a65 3061 struct net *net = sock_net(skb->sk);
e2849863 3062 rtnl_doit_func doit;
1da177e4 3063 int sz_idx, kind;
1da177e4
LT
3064 int family;
3065 int type;
2907c35f 3066 int err;
1da177e4 3067
1da177e4 3068 type = nlh->nlmsg_type;
1da177e4 3069 if (type > RTM_MAX)
038890fe 3070 return -EOPNOTSUPP;
1da177e4
LT
3071
3072 type -= RTM_BASE;
3073
3074 /* All the messages must have at least 1 byte length */
573ce260 3075 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
1da177e4
LT
3076 return 0;
3077
573ce260 3078 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
1da177e4
LT
3079 sz_idx = type>>2;
3080 kind = type&3;
3081
90f62cf3 3082 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
1d00a4eb 3083 return -EPERM;
1da177e4 3084
b8f3ab42 3085 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
97c53cac 3086 struct sock *rtnl;
e2849863 3087 rtnl_dumpit_func dumpit;
c7ac8679
GR
3088 rtnl_calcit_func calcit;
3089 u16 min_dump_alloc = 0;
1da177e4 3090
e2849863
TG
3091 dumpit = rtnl_get_dumpit(family, type);
3092 if (dumpit == NULL)
038890fe 3093 return -EOPNOTSUPP;
c7ac8679
GR
3094 calcit = rtnl_get_calcit(family, type);
3095 if (calcit)
115c9b81 3096 min_dump_alloc = calcit(skb, nlh);
9ac4a169 3097
2907c35f 3098 __rtnl_unlock();
97c53cac 3099 rtnl = net->rtnl;
80d326fa
PNA
3100 {
3101 struct netlink_dump_control c = {
3102 .dump = dumpit,
3103 .min_dump_alloc = min_dump_alloc,
3104 };
3105 err = netlink_dump_start(rtnl, skb, nlh, &c);
3106 }
2907c35f
ED
3107 rtnl_lock();
3108 return err;
1da177e4
LT
3109 }
3110
e2849863
TG
3111 doit = rtnl_get_doit(family, type);
3112 if (doit == NULL)
038890fe 3113 return -EOPNOTSUPP;
1da177e4 3114
661d2967 3115 return doit(skb, nlh);
1da177e4
LT
3116}
3117
cd40b7d3 3118static void rtnetlink_rcv(struct sk_buff *skb)
1da177e4 3119{
cd40b7d3
DL
3120 rtnl_lock();
3121 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
3122 rtnl_unlock();
1da177e4
LT
3123}
3124
1da177e4
LT
3125static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
3126{
351638e7 3127 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
e9dc8653 3128
1da177e4 3129 switch (event) {
1da177e4
LT
3130 case NETDEV_UP:
3131 case NETDEV_DOWN:
10de05af 3132 case NETDEV_PRE_UP:
d90a909e
EB
3133 case NETDEV_POST_INIT:
3134 case NETDEV_REGISTER:
1da177e4 3135 case NETDEV_CHANGE:
755d0e77 3136 case NETDEV_PRE_TYPE_CHANGE:
1da177e4 3137 case NETDEV_GOING_DOWN:
a2835763 3138 case NETDEV_UNREGISTER:
0115e8e3 3139 case NETDEV_UNREGISTER_FINAL:
ac3d3f81
AW
3140 case NETDEV_RELEASE:
3141 case NETDEV_JOIN:
1da177e4
LT
3142 break;
3143 default:
7f294054 3144 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
1da177e4
LT
3145 break;
3146 }
3147 return NOTIFY_DONE;
3148}
3149
3150static struct notifier_block rtnetlink_dev_notifier = {
3151 .notifier_call = rtnetlink_event,
3152};
3153
97c53cac 3154
2c8c1e72 3155static int __net_init rtnetlink_net_init(struct net *net)
97c53cac
DL
3156{
3157 struct sock *sk;
a31f2d17
PNA
3158 struct netlink_kernel_cfg cfg = {
3159 .groups = RTNLGRP_MAX,
3160 .input = rtnetlink_rcv,
3161 .cb_mutex = &rtnl_mutex,
9785e10a 3162 .flags = NL_CFG_F_NONROOT_RECV,
a31f2d17
PNA
3163 };
3164
9f00d977 3165 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
97c53cac
DL
3166 if (!sk)
3167 return -ENOMEM;
97c53cac
DL
3168 net->rtnl = sk;
3169 return 0;
3170}
3171
2c8c1e72 3172static void __net_exit rtnetlink_net_exit(struct net *net)
97c53cac 3173{
775516bf
DL
3174 netlink_kernel_release(net->rtnl);
3175 net->rtnl = NULL;
97c53cac
DL
3176}
3177
3178static struct pernet_operations rtnetlink_net_ops = {
3179 .init = rtnetlink_net_init,
3180 .exit = rtnetlink_net_exit,
3181};
3182
1da177e4
LT
3183void __init rtnetlink_init(void)
3184{
97c53cac 3185 if (register_pernet_subsys(&rtnetlink_net_ops))
1da177e4 3186 panic("rtnetlink_init: cannot initialize rtnetlink\n");
97c53cac 3187
1da177e4 3188 register_netdevice_notifier(&rtnetlink_dev_notifier);
340d17fc 3189
c7ac8679
GR
3190 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
3191 rtnl_dump_ifinfo, rtnl_calcit);
3192 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
3193 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
3194 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
687ad8cc 3195
c7ac8679
GR
3196 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
3197 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
77162022
JF
3198
3199 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL);
3200 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL);
3201 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL);
e5a55a89
JF
3202
3203 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL);
407af329 3204 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL);
e5a55a89 3205 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL);
1da177e4
LT
3206}
3207