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