rtnl: allow to create device with IFLA_LINK_NETNSID set
[deliverable/linux.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 */
d37512a2 878 + nla_total_size(4) /* IFLA_LINK_NETNSID */
115c9b81
GR
879 + nla_total_size(ext_filter_mask
880 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
881 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
c53864fd 882 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
f8ff182c 883 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
66cae9ed 884 + rtnl_link_get_af_size(dev) /* IFLA_AF_SPEC */
82f28412
JP
885 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
886 + nla_total_size(MAX_PHYS_ITEM_ID_LEN); /* IFLA_PHYS_SWITCH_ID */
339bf98f
TG
887}
888
57b61080
SF
889static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
890{
891 struct nlattr *vf_ports;
892 struct nlattr *vf_port;
893 int vf;
894 int err;
895
896 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
897 if (!vf_ports)
898 return -EMSGSIZE;
899
900 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
901 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
8ca94183
SF
902 if (!vf_port)
903 goto nla_put_failure;
a6574349
DM
904 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
905 goto nla_put_failure;
57b61080 906 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
8ca94183
SF
907 if (err == -EMSGSIZE)
908 goto nla_put_failure;
57b61080 909 if (err) {
57b61080
SF
910 nla_nest_cancel(skb, vf_port);
911 continue;
912 }
913 nla_nest_end(skb, vf_port);
914 }
915
916 nla_nest_end(skb, vf_ports);
917
918 return 0;
8ca94183
SF
919
920nla_put_failure:
921 nla_nest_cancel(skb, vf_ports);
922 return -EMSGSIZE;
57b61080
SF
923}
924
925static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
926{
927 struct nlattr *port_self;
928 int err;
929
930 port_self = nla_nest_start(skb, IFLA_PORT_SELF);
931 if (!port_self)
932 return -EMSGSIZE;
933
934 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
935 if (err) {
936 nla_nest_cancel(skb, port_self);
8ca94183 937 return (err == -EMSGSIZE) ? err : 0;
57b61080
SF
938 }
939
940 nla_nest_end(skb, port_self);
941
942 return 0;
943}
944
c53864fd
DG
945static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
946 u32 ext_filter_mask)
57b61080
SF
947{
948 int err;
949
c53864fd
DG
950 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
951 !(ext_filter_mask & RTEXT_FILTER_VF))
57b61080
SF
952 return 0;
953
954 err = rtnl_port_self_fill(skb, dev);
955 if (err)
956 return err;
957
958 if (dev_num_vf(dev->dev.parent)) {
959 err = rtnl_vf_ports_fill(skb, dev);
960 if (err)
961 return err;
962 }
963
964 return 0;
965}
966
66cae9ed
JP
967static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
968{
969 int err;
02637fce 970 struct netdev_phys_item_id ppid;
66cae9ed
JP
971
972 err = dev_get_phys_port_id(dev, &ppid);
973 if (err) {
974 if (err == -EOPNOTSUPP)
975 return 0;
976 return err;
977 }
978
979 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
980 return -EMSGSIZE;
981
982 return 0;
983}
984
82f28412
JP
985static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
986{
987 int err;
988 struct netdev_phys_item_id psid;
989
990 err = netdev_switch_parent_id_get(dev, &psid);
991 if (err) {
992 if (err == -EOPNOTSUPP)
993 return 0;
994 return err;
995 }
996
997 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, psid.id_len, psid.id))
998 return -EMSGSIZE;
999
1000 return 0;
1001}
1002
b60c5115 1003static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
575c3e2a 1004 int type, u32 pid, u32 seq, u32 change,
115c9b81 1005 unsigned int flags, u32 ext_filter_mask)
b60c5115
TG
1006{
1007 struct ifinfomsg *ifm;
1008 struct nlmsghdr *nlh;
28172739 1009 struct rtnl_link_stats64 temp;
be1f3c2c 1010 const struct rtnl_link_stats64 *stats;
f8ff182c
TG
1011 struct nlattr *attr, *af_spec;
1012 struct rtnl_af_ops *af_ops;
898e5061 1013 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
1da177e4 1014
2907c35f 1015 ASSERT_RTNL();
b60c5115
TG
1016 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1017 if (nlh == NULL)
26932566 1018 return -EMSGSIZE;
1da177e4 1019
b60c5115
TG
1020 ifm = nlmsg_data(nlh);
1021 ifm->ifi_family = AF_UNSPEC;
1022 ifm->__ifi_pad = 0;
1023 ifm->ifi_type = dev->type;
1024 ifm->ifi_index = dev->ifindex;
1025 ifm->ifi_flags = dev_get_flags(dev);
1026 ifm->ifi_change = change;
1027
a6574349
DM
1028 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1029 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1030 nla_put_u8(skb, IFLA_OPERSTATE,
1031 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1032 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1033 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1034 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
edbc0bb3 1035 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
76ff5cc9 1036 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
1d69c2b3 1037#ifdef CONFIG_RPS
76ff5cc9 1038 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
1d69c2b3 1039#endif
a6574349
DM
1040 (dev->ifindex != dev->iflink &&
1041 nla_put_u32(skb, IFLA_LINK, dev->iflink)) ||
898e5061
JP
1042 (upper_dev &&
1043 nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
9a57247f 1044 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
a6574349
DM
1045 (dev->qdisc &&
1046 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1047 (dev->ifalias &&
2d3b479d 1048 nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) ||
1049 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
1050 atomic_read(&dev->carrier_changes)))
a6574349 1051 goto nla_put_failure;
0b815a1a 1052
1da177e4
LT
1053 if (1) {
1054 struct rtnl_link_ifmap map = {
1055 .mem_start = dev->mem_start,
1056 .mem_end = dev->mem_end,
1057 .base_addr = dev->base_addr,
1058 .irq = dev->irq,
1059 .dma = dev->dma,
1060 .port = dev->if_port,
1061 };
a6574349
DM
1062 if (nla_put(skb, IFLA_MAP, sizeof(map), &map))
1063 goto nla_put_failure;
1da177e4
LT
1064 }
1065
1066 if (dev->addr_len) {
a6574349
DM
1067 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1068 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1069 goto nla_put_failure;
1da177e4
LT
1070 }
1071
66cae9ed
JP
1072 if (rtnl_phys_port_id_fill(skb, dev))
1073 goto nla_put_failure;
1074
82f28412
JP
1075 if (rtnl_phys_switch_id_fill(skb, dev))
1076 goto nla_put_failure;
1077
96e74088
PE
1078 attr = nla_reserve(skb, IFLA_STATS,
1079 sizeof(struct rtnl_link_stats));
1080 if (attr == NULL)
1081 goto nla_put_failure;
b60c5115 1082
28172739 1083 stats = dev_get_stats(dev, &temp);
96e74088 1084 copy_rtnl_link_stats(nla_data(attr), stats);
1da177e4 1085
10708f37
JE
1086 attr = nla_reserve(skb, IFLA_STATS64,
1087 sizeof(struct rtnl_link_stats64));
1088 if (attr == NULL)
1089 goto nla_put_failure;
10708f37
JE
1090 copy_rtnl_link_stats64(nla_data(attr), stats);
1091
a6574349
DM
1092 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
1093 nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
1094 goto nla_put_failure;
57b61080 1095
115c9b81
GR
1096 if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent
1097 && (ext_filter_mask & RTEXT_FILTER_VF)) {
ebc08a6f 1098 int i;
ebc08a6f 1099
c02db8c6
CW
1100 struct nlattr *vfinfo, *vf;
1101 int num_vfs = dev_num_vf(dev->dev.parent);
1102
c02db8c6
CW
1103 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1104 if (!vfinfo)
1105 goto nla_put_failure;
1106 for (i = 0; i < num_vfs; i++) {
1107 struct ifla_vf_info ivi;
1108 struct ifla_vf_mac vf_mac;
1109 struct ifla_vf_vlan vf_vlan;
ed616689 1110 struct ifla_vf_rate vf_rate;
c02db8c6 1111 struct ifla_vf_tx_rate vf_tx_rate;
5f8444a3 1112 struct ifla_vf_spoofchk vf_spoofchk;
1d8faf48 1113 struct ifla_vf_link_state vf_linkstate;
5f8444a3
GR
1114
1115 /*
1116 * Not all SR-IOV capable drivers support the
1117 * spoofcheck query. Preset to -1 so the user
1118 * space tool can detect that the driver didn't
1119 * report anything.
1120 */
1121 ivi.spoofchk = -1;
84d73cd3 1122 memset(ivi.mac, 0, sizeof(ivi.mac));
1d8faf48
RE
1123 /* The default value for VF link state is "auto"
1124 * IFLA_VF_LINK_STATE_AUTO which equals zero
1125 */
1126 ivi.linkstate = 0;
ebc08a6f
WM
1127 if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi))
1128 break;
5f8444a3
GR
1129 vf_mac.vf =
1130 vf_vlan.vf =
ed616689 1131 vf_rate.vf =
5f8444a3 1132 vf_tx_rate.vf =
1d8faf48
RE
1133 vf_spoofchk.vf =
1134 vf_linkstate.vf = ivi.vf;
5f8444a3 1135
c02db8c6
CW
1136 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1137 vf_vlan.vlan = ivi.vlan;
1138 vf_vlan.qos = ivi.qos;
ed616689
SC
1139 vf_tx_rate.rate = ivi.max_tx_rate;
1140 vf_rate.min_tx_rate = ivi.min_tx_rate;
1141 vf_rate.max_tx_rate = ivi.max_tx_rate;
5f8444a3 1142 vf_spoofchk.setting = ivi.spoofchk;
1d8faf48 1143 vf_linkstate.link_state = ivi.linkstate;
c02db8c6
CW
1144 vf = nla_nest_start(skb, IFLA_VF_INFO);
1145 if (!vf) {
1146 nla_nest_cancel(skb, vfinfo);
1147 goto nla_put_failure;
1148 }
a6574349
DM
1149 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1150 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
ed616689
SC
1151 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1152 &vf_rate) ||
a6574349
DM
1153 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1154 &vf_tx_rate) ||
1155 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1d8faf48
RE
1156 &vf_spoofchk) ||
1157 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1158 &vf_linkstate))
a6574349 1159 goto nla_put_failure;
c02db8c6 1160 nla_nest_end(skb, vf);
ebc08a6f 1161 }
c02db8c6 1162 nla_nest_end(skb, vfinfo);
ebc08a6f 1163 }
57b61080 1164
c53864fd 1165 if (rtnl_port_fill(skb, dev, ext_filter_mask))
57b61080
SF
1166 goto nla_put_failure;
1167
ba7d49b1 1168 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
38f7b870
PM
1169 if (rtnl_link_fill(skb, dev) < 0)
1170 goto nla_put_failure;
1171 }
1172
d37512a2
ND
1173 if (dev->rtnl_link_ops &&
1174 dev->rtnl_link_ops->get_link_net) {
1175 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1176
1177 if (!net_eq(dev_net(dev), link_net)) {
1178 int id = peernet2id(dev_net(dev), link_net);
1179
1180 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1181 goto nla_put_failure;
1182 }
1183 }
1184
f8ff182c
TG
1185 if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1186 goto nla_put_failure;
1187
1188 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1189 if (af_ops->fill_link_af) {
1190 struct nlattr *af;
1191 int err;
1192
1193 if (!(af = nla_nest_start(skb, af_ops->family)))
1194 goto nla_put_failure;
1195
1196 err = af_ops->fill_link_af(skb, dev);
1197
1198 /*
1199 * Caller may return ENODATA to indicate that there
1200 * was no data to be dumped. This is not an error, it
1201 * means we should trim the attribute header and
1202 * continue.
1203 */
1204 if (err == -ENODATA)
1205 nla_nest_cancel(skb, af);
1206 else if (err < 0)
1207 goto nla_put_failure;
1208
1209 nla_nest_end(skb, af);
1210 }
1211 }
1212
1213 nla_nest_end(skb, af_spec);
1214
053c095a
JB
1215 nlmsg_end(skb, nlh);
1216 return 0;
b60c5115
TG
1217
1218nla_put_failure:
26932566
PM
1219 nlmsg_cancel(skb, nlh);
1220 return -EMSGSIZE;
1da177e4
LT
1221}
1222
f7b12606 1223static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
5176f91e 1224 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
38f7b870
PM
1225 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1226 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
5176f91e 1227 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
da5e0494 1228 [IFLA_MTU] = { .type = NLA_U32 },
76e87306 1229 [IFLA_LINK] = { .type = NLA_U32 },
fbaec0ea 1230 [IFLA_MASTER] = { .type = NLA_U32 },
9a57247f 1231 [IFLA_CARRIER] = { .type = NLA_U8 },
da5e0494
TG
1232 [IFLA_TXQLEN] = { .type = NLA_U32 },
1233 [IFLA_WEIGHT] = { .type = NLA_U32 },
1234 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1235 [IFLA_LINKMODE] = { .type = NLA_U8 },
76e87306 1236 [IFLA_LINKINFO] = { .type = NLA_NESTED },
d8a5ec67 1237 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
f0630529 1238 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
0b815a1a 1239 [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 },
c02db8c6 1240 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
57b61080
SF
1241 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1242 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
f8ff182c 1243 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
115c9b81 1244 [IFLA_EXT_MASK] = { .type = NLA_U32 },
edbc0bb3 1245 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
76ff5cc9
JP
1246 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1247 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
02637fce 1248 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
2d3b479d 1249 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
82f28412 1250 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
317f4810 1251 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
da5e0494
TG
1252};
1253
38f7b870
PM
1254static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1255 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1256 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
ba7d49b1
JP
1257 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1258 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
38f7b870
PM
1259};
1260
c02db8c6
CW
1261static const struct nla_policy ifla_vfinfo_policy[IFLA_VF_INFO_MAX+1] = {
1262 [IFLA_VF_INFO] = { .type = NLA_NESTED },
1263};
1264
1265static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1266 [IFLA_VF_MAC] = { .type = NLA_BINARY,
1267 .len = sizeof(struct ifla_vf_mac) },
1268 [IFLA_VF_VLAN] = { .type = NLA_BINARY,
1269 .len = sizeof(struct ifla_vf_vlan) },
1270 [IFLA_VF_TX_RATE] = { .type = NLA_BINARY,
1271 .len = sizeof(struct ifla_vf_tx_rate) },
48752f65
GR
1272 [IFLA_VF_SPOOFCHK] = { .type = NLA_BINARY,
1273 .len = sizeof(struct ifla_vf_spoofchk) },
ed616689
SC
1274 [IFLA_VF_RATE] = { .type = NLA_BINARY,
1275 .len = sizeof(struct ifla_vf_rate) },
c5b46160
DL
1276 [IFLA_VF_LINK_STATE] = { .type = NLA_BINARY,
1277 .len = sizeof(struct ifla_vf_link_state) },
c02db8c6
CW
1278};
1279
57b61080
SF
1280static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1281 [IFLA_PORT_VF] = { .type = NLA_U32 },
1282 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1283 .len = PORT_PROFILE_MAX },
1284 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1285 .len = sizeof(struct ifla_port_vsi)},
1286 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1287 .len = PORT_UUID_MAX },
1288 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1289 .len = PORT_UUID_MAX },
1290 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1291 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
1292};
1293
f7b12606
JP
1294static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
1295{
1296 struct net *net = sock_net(skb->sk);
1297 int h, s_h;
1298 int idx = 0, s_idx;
1299 struct net_device *dev;
1300 struct hlist_head *head;
1301 struct nlattr *tb[IFLA_MAX+1];
1302 u32 ext_filter_mask = 0;
973462bb 1303 int err;
e5eca6d4 1304 int hdrlen;
f7b12606
JP
1305
1306 s_h = cb->args[0];
1307 s_idx = cb->args[1];
1308
1309 rcu_read_lock();
1310 cb->seq = net->dev_base_seq;
1311
e5eca6d4
MS
1312 /* A hack to preserve kernel<->userspace interface.
1313 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1314 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1315 * what iproute2 < v3.9.0 used.
1316 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1317 * attribute, its netlink message is shorter than struct ifinfomsg.
1318 */
1319 hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1320 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1321
1322 if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
f7b12606
JP
1323
1324 if (tb[IFLA_EXT_MASK])
1325 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1326 }
1327
1328 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1329 idx = 0;
1330 head = &net->dev_index_head[h];
1331 hlist_for_each_entry_rcu(dev, head, index_hlist) {
1332 if (idx < s_idx)
1333 goto cont;
973462bb
DG
1334 err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
1335 NETLINK_CB(cb->skb).portid,
1336 cb->nlh->nlmsg_seq, 0,
1337 NLM_F_MULTI,
1338 ext_filter_mask);
1339 /* If we ran out of room on the first message,
1340 * we're in trouble
1341 */
1342 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
1343
7b46a644 1344 if (err < 0)
f7b12606
JP
1345 goto out;
1346
1347 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1348cont:
1349 idx++;
1350 }
1351 }
1352out:
1353 rcu_read_unlock();
1354 cb->args[1] = idx;
1355 cb->args[0] = h;
1356
1357 return skb->len;
1358}
1359
1360int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len)
1361{
1362 return nla_parse(tb, IFLA_MAX, head, len, ifla_policy);
1363}
1364EXPORT_SYMBOL(rtnl_nla_parse_ifla);
1365
81adee47
EB
1366struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1367{
1368 struct net *net;
1369 /* Examine the link attributes and figure out which
1370 * network namespace we are talking about.
1371 */
1372 if (tb[IFLA_NET_NS_PID])
1373 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
f0630529
EB
1374 else if (tb[IFLA_NET_NS_FD])
1375 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
81adee47
EB
1376 else
1377 net = get_net(src_net);
1378 return net;
1379}
1380EXPORT_SYMBOL(rtnl_link_get_net);
1381
1840bb13
TG
1382static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1383{
1384 if (dev) {
1385 if (tb[IFLA_ADDRESS] &&
1386 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1387 return -EINVAL;
1388
1389 if (tb[IFLA_BROADCAST] &&
1390 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1391 return -EINVAL;
1392 }
1393
cf7afbfe
TG
1394 if (tb[IFLA_AF_SPEC]) {
1395 struct nlattr *af;
1396 int rem, err;
1397
1398 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1399 const struct rtnl_af_ops *af_ops;
1400
1401 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1402 return -EAFNOSUPPORT;
1403
1404 if (!af_ops->set_link_af)
1405 return -EOPNOTSUPP;
1406
1407 if (af_ops->validate_link_af) {
6d3a9a68 1408 err = af_ops->validate_link_af(dev, af);
cf7afbfe
TG
1409 if (err < 0)
1410 return err;
1411 }
1412 }
1413 }
1414
1840bb13
TG
1415 return 0;
1416}
1417
c02db8c6
CW
1418static int do_setvfinfo(struct net_device *dev, struct nlattr *attr)
1419{
1420 int rem, err = -EINVAL;
1421 struct nlattr *vf;
1422 const struct net_device_ops *ops = dev->netdev_ops;
1423
1424 nla_for_each_nested(vf, attr, rem) {
1425 switch (nla_type(vf)) {
1426 case IFLA_VF_MAC: {
1427 struct ifla_vf_mac *ivm;
1428 ivm = nla_data(vf);
1429 err = -EOPNOTSUPP;
1430 if (ops->ndo_set_vf_mac)
1431 err = ops->ndo_set_vf_mac(dev, ivm->vf,
1432 ivm->mac);
1433 break;
1434 }
1435 case IFLA_VF_VLAN: {
1436 struct ifla_vf_vlan *ivv;
1437 ivv = nla_data(vf);
1438 err = -EOPNOTSUPP;
1439 if (ops->ndo_set_vf_vlan)
1440 err = ops->ndo_set_vf_vlan(dev, ivv->vf,
1441 ivv->vlan,
1442 ivv->qos);
1443 break;
1444 }
1445 case IFLA_VF_TX_RATE: {
1446 struct ifla_vf_tx_rate *ivt;
ed616689 1447 struct ifla_vf_info ivf;
c02db8c6
CW
1448 ivt = nla_data(vf);
1449 err = -EOPNOTSUPP;
ed616689
SC
1450 if (ops->ndo_get_vf_config)
1451 err = ops->ndo_get_vf_config(dev, ivt->vf,
1452 &ivf);
1453 if (err)
1454 break;
1455 err = -EOPNOTSUPP;
1456 if (ops->ndo_set_vf_rate)
1457 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1458 ivf.min_tx_rate,
1459 ivt->rate);
1460 break;
1461 }
1462 case IFLA_VF_RATE: {
1463 struct ifla_vf_rate *ivt;
1464 ivt = nla_data(vf);
1465 err = -EOPNOTSUPP;
1466 if (ops->ndo_set_vf_rate)
1467 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1468 ivt->min_tx_rate,
1469 ivt->max_tx_rate);
c02db8c6
CW
1470 break;
1471 }
5f8444a3
GR
1472 case IFLA_VF_SPOOFCHK: {
1473 struct ifla_vf_spoofchk *ivs;
1474 ivs = nla_data(vf);
1475 err = -EOPNOTSUPP;
1476 if (ops->ndo_set_vf_spoofchk)
1477 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
1478 ivs->setting);
1479 break;
1480 }
1d8faf48
RE
1481 case IFLA_VF_LINK_STATE: {
1482 struct ifla_vf_link_state *ivl;
1483 ivl = nla_data(vf);
1484 err = -EOPNOTSUPP;
1485 if (ops->ndo_set_vf_link_state)
1486 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
1487 ivl->link_state);
1488 break;
1489 }
c02db8c6
CW
1490 default:
1491 err = -EINVAL;
1492 break;
1493 }
1494 if (err)
1495 break;
1496 }
1497 return err;
1498}
1499
fbaec0ea
JP
1500static int do_set_master(struct net_device *dev, int ifindex)
1501{
898e5061 1502 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
fbaec0ea
JP
1503 const struct net_device_ops *ops;
1504 int err;
1505
898e5061
JP
1506 if (upper_dev) {
1507 if (upper_dev->ifindex == ifindex)
fbaec0ea 1508 return 0;
898e5061 1509 ops = upper_dev->netdev_ops;
fbaec0ea 1510 if (ops->ndo_del_slave) {
898e5061 1511 err = ops->ndo_del_slave(upper_dev, dev);
fbaec0ea
JP
1512 if (err)
1513 return err;
1514 } else {
1515 return -EOPNOTSUPP;
1516 }
1517 }
1518
1519 if (ifindex) {
898e5061
JP
1520 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
1521 if (!upper_dev)
fbaec0ea 1522 return -EINVAL;
898e5061 1523 ops = upper_dev->netdev_ops;
fbaec0ea 1524 if (ops->ndo_add_slave) {
898e5061 1525 err = ops->ndo_add_slave(upper_dev, dev);
fbaec0ea
JP
1526 if (err)
1527 return err;
1528 } else {
1529 return -EOPNOTSUPP;
1530 }
1531 }
1532 return 0;
1533}
1534
90c325e3 1535#define DO_SETLINK_MODIFIED 0x01
ba998906
ND
1536/* notify flag means notify + modified. */
1537#define DO_SETLINK_NOTIFY 0x03
90f62cf3
EB
1538static int do_setlink(const struct sk_buff *skb,
1539 struct net_device *dev, struct ifinfomsg *ifm,
90c325e3 1540 struct nlattr **tb, char *ifname, int status)
1da177e4 1541{
d314774c 1542 const struct net_device_ops *ops = dev->netdev_ops;
0157f60c 1543 int err;
1da177e4 1544
f0630529 1545 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
81adee47 1546 struct net *net = rtnl_link_get_net(dev_net(dev), tb);
d8a5ec67
EB
1547 if (IS_ERR(net)) {
1548 err = PTR_ERR(net);
1549 goto errout;
1550 }
90f62cf3 1551 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
e0ebde0e 1552 put_net(net);
b51642f6
EB
1553 err = -EPERM;
1554 goto errout;
1555 }
d8a5ec67
EB
1556 err = dev_change_net_namespace(dev, net, ifname);
1557 put_net(net);
1558 if (err)
1559 goto errout;
90c325e3 1560 status |= DO_SETLINK_MODIFIED;
d8a5ec67
EB
1561 }
1562
da5e0494 1563 if (tb[IFLA_MAP]) {
1da177e4
LT
1564 struct rtnl_link_ifmap *u_map;
1565 struct ifmap k_map;
1566
d314774c 1567 if (!ops->ndo_set_config) {
1da177e4 1568 err = -EOPNOTSUPP;
0157f60c 1569 goto errout;
1da177e4
LT
1570 }
1571
1572 if (!netif_device_present(dev)) {
1573 err = -ENODEV;
0157f60c 1574 goto errout;
1da177e4 1575 }
1da177e4 1576
da5e0494 1577 u_map = nla_data(tb[IFLA_MAP]);
1da177e4
LT
1578 k_map.mem_start = (unsigned long) u_map->mem_start;
1579 k_map.mem_end = (unsigned long) u_map->mem_end;
1580 k_map.base_addr = (unsigned short) u_map->base_addr;
1581 k_map.irq = (unsigned char) u_map->irq;
1582 k_map.dma = (unsigned char) u_map->dma;
1583 k_map.port = (unsigned char) u_map->port;
1584
d314774c 1585 err = ops->ndo_set_config(dev, &k_map);
da5e0494 1586 if (err < 0)
0157f60c 1587 goto errout;
1da177e4 1588
ba998906 1589 status |= DO_SETLINK_NOTIFY;
1da177e4
LT
1590 }
1591
da5e0494 1592 if (tb[IFLA_ADDRESS]) {
70f8e78e
DM
1593 struct sockaddr *sa;
1594 int len;
1595
70f8e78e
DM
1596 len = sizeof(sa_family_t) + dev->addr_len;
1597 sa = kmalloc(len, GFP_KERNEL);
1598 if (!sa) {
1599 err = -ENOMEM;
0157f60c 1600 goto errout;
70f8e78e
DM
1601 }
1602 sa->sa_family = dev->type;
da5e0494 1603 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
70f8e78e 1604 dev->addr_len);
e7c3273e 1605 err = dev_set_mac_address(dev, sa);
70f8e78e 1606 kfree(sa);
1da177e4 1607 if (err)
0157f60c 1608 goto errout;
90c325e3 1609 status |= DO_SETLINK_MODIFIED;
1da177e4
LT
1610 }
1611
da5e0494
TG
1612 if (tb[IFLA_MTU]) {
1613 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1614 if (err < 0)
0157f60c 1615 goto errout;
90c325e3 1616 status |= DO_SETLINK_MODIFIED;
1da177e4
LT
1617 }
1618
cbda10fa
VD
1619 if (tb[IFLA_GROUP]) {
1620 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
ba998906 1621 status |= DO_SETLINK_NOTIFY;
cbda10fa
VD
1622 }
1623
da5e0494
TG
1624 /*
1625 * Interface selected by interface index but interface
1626 * name provided implies that a name change has been
1627 * requested.
1628 */
51055be8 1629 if (ifm->ifi_index > 0 && ifname[0]) {
da5e0494
TG
1630 err = dev_change_name(dev, ifname);
1631 if (err < 0)
0157f60c 1632 goto errout;
90c325e3 1633 status |= DO_SETLINK_MODIFIED;
1da177e4
LT
1634 }
1635
0b815a1a
SH
1636 if (tb[IFLA_IFALIAS]) {
1637 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
1638 nla_len(tb[IFLA_IFALIAS]));
1639 if (err < 0)
1640 goto errout;
ba998906 1641 status |= DO_SETLINK_NOTIFY;
0b815a1a
SH
1642 }
1643
da5e0494
TG
1644 if (tb[IFLA_BROADCAST]) {
1645 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
e7c3273e 1646 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
1da177e4
LT
1647 }
1648
83b496e9 1649 if (ifm->ifi_flags || ifm->ifi_change) {
3729d502 1650 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
5f9021cf
JB
1651 if (err < 0)
1652 goto errout;
83b496e9 1653 }
1da177e4 1654
fbaec0ea
JP
1655 if (tb[IFLA_MASTER]) {
1656 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
1657 if (err)
1658 goto errout;
90c325e3 1659 status |= DO_SETLINK_MODIFIED;
fbaec0ea
JP
1660 }
1661
9a57247f
JP
1662 if (tb[IFLA_CARRIER]) {
1663 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
1664 if (err)
1665 goto errout;
90c325e3 1666 status |= DO_SETLINK_MODIFIED;
9a57247f
JP
1667 }
1668
5d1180fc
ND
1669 if (tb[IFLA_TXQLEN]) {
1670 unsigned long value = nla_get_u32(tb[IFLA_TXQLEN]);
1671
1672 if (dev->tx_queue_len ^ value)
ba998906 1673 status |= DO_SETLINK_NOTIFY;
5d1180fc
ND
1674
1675 dev->tx_queue_len = value;
1676 }
b00055aa 1677
da5e0494 1678 if (tb[IFLA_OPERSTATE])
93b2d4a2 1679 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
b00055aa 1680
da5e0494 1681 if (tb[IFLA_LINKMODE]) {
1889b0e7
ND
1682 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
1683
93b2d4a2 1684 write_lock_bh(&dev_base_lock);
1889b0e7 1685 if (dev->link_mode ^ value)
ba998906 1686 status |= DO_SETLINK_NOTIFY;
1889b0e7 1687 dev->link_mode = value;
93b2d4a2 1688 write_unlock_bh(&dev_base_lock);
b00055aa
SR
1689 }
1690
c02db8c6
CW
1691 if (tb[IFLA_VFINFO_LIST]) {
1692 struct nlattr *attr;
1693 int rem;
1694 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
253683bb
DH
1695 if (nla_type(attr) != IFLA_VF_INFO) {
1696 err = -EINVAL;
c02db8c6 1697 goto errout;
253683bb 1698 }
c02db8c6
CW
1699 err = do_setvfinfo(dev, attr);
1700 if (err < 0)
1701 goto errout;
ba998906 1702 status |= DO_SETLINK_NOTIFY;
c02db8c6 1703 }
ebc08a6f 1704 }
1da177e4
LT
1705 err = 0;
1706
57b61080
SF
1707 if (tb[IFLA_VF_PORTS]) {
1708 struct nlattr *port[IFLA_PORT_MAX+1];
1709 struct nlattr *attr;
1710 int vf;
1711 int rem;
1712
1713 err = -EOPNOTSUPP;
1714 if (!ops->ndo_set_vf_port)
1715 goto errout;
1716
1717 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
1718 if (nla_type(attr) != IFLA_VF_PORT)
1719 continue;
1720 err = nla_parse_nested(port, IFLA_PORT_MAX,
1721 attr, ifla_port_policy);
1722 if (err < 0)
1723 goto errout;
1724 if (!port[IFLA_PORT_VF]) {
1725 err = -EOPNOTSUPP;
1726 goto errout;
1727 }
1728 vf = nla_get_u32(port[IFLA_PORT_VF]);
1729 err = ops->ndo_set_vf_port(dev, vf, port);
1730 if (err < 0)
1731 goto errout;
ba998906 1732 status |= DO_SETLINK_NOTIFY;
57b61080
SF
1733 }
1734 }
1735 err = 0;
1736
1737 if (tb[IFLA_PORT_SELF]) {
1738 struct nlattr *port[IFLA_PORT_MAX+1];
1739
1740 err = nla_parse_nested(port, IFLA_PORT_MAX,
1741 tb[IFLA_PORT_SELF], ifla_port_policy);
1742 if (err < 0)
1743 goto errout;
1744
1745 err = -EOPNOTSUPP;
1746 if (ops->ndo_set_vf_port)
1747 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
1748 if (err < 0)
1749 goto errout;
ba998906 1750 status |= DO_SETLINK_NOTIFY;
57b61080 1751 }
f8ff182c
TG
1752
1753 if (tb[IFLA_AF_SPEC]) {
1754 struct nlattr *af;
1755 int rem;
1756
1757 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1758 const struct rtnl_af_ops *af_ops;
1759
1760 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
cf7afbfe 1761 BUG();
f8ff182c 1762
cf7afbfe 1763 err = af_ops->set_link_af(dev, af);
f8ff182c
TG
1764 if (err < 0)
1765 goto errout;
1766
ba998906 1767 status |= DO_SETLINK_NOTIFY;
f8ff182c
TG
1768 }
1769 }
57b61080
SF
1770 err = 0;
1771
0157f60c 1772errout:
ba998906
ND
1773 if (status & DO_SETLINK_MODIFIED) {
1774 if (status & DO_SETLINK_NOTIFY)
1775 netdev_state_change(dev);
1776
1777 if (err < 0)
1778 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",
1779 dev->name);
1780 }
da5e0494 1781
0157f60c
PM
1782 return err;
1783}
1da177e4 1784
661d2967 1785static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
0157f60c 1786{
3b1e0a65 1787 struct net *net = sock_net(skb->sk);
0157f60c
PM
1788 struct ifinfomsg *ifm;
1789 struct net_device *dev;
1790 int err;
1791 struct nlattr *tb[IFLA_MAX+1];
1792 char ifname[IFNAMSIZ];
1793
1794 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1795 if (err < 0)
1796 goto errout;
1797
1798 if (tb[IFLA_IFNAME])
1799 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1800 else
1801 ifname[0] = '\0';
1802
1803 err = -EINVAL;
1804 ifm = nlmsg_data(nlh);
1805 if (ifm->ifi_index > 0)
a3d12891 1806 dev = __dev_get_by_index(net, ifm->ifi_index);
0157f60c 1807 else if (tb[IFLA_IFNAME])
a3d12891 1808 dev = __dev_get_by_name(net, ifname);
0157f60c
PM
1809 else
1810 goto errout;
1811
1812 if (dev == NULL) {
1813 err = -ENODEV;
1814 goto errout;
1815 }
1816
e0d087af
ED
1817 err = validate_linkmsg(dev, tb);
1818 if (err < 0)
a3d12891 1819 goto errout;
0157f60c 1820
90f62cf3 1821 err = do_setlink(skb, dev, ifm, tb, ifname, 0);
da5e0494 1822errout:
1da177e4
LT
1823 return err;
1824}
1825
661d2967 1826static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
38f7b870 1827{
3b1e0a65 1828 struct net *net = sock_net(skb->sk);
38f7b870
PM
1829 const struct rtnl_link_ops *ops;
1830 struct net_device *dev;
1831 struct ifinfomsg *ifm;
1832 char ifname[IFNAMSIZ];
1833 struct nlattr *tb[IFLA_MAX+1];
1834 int err;
226bd341 1835 LIST_HEAD(list_kill);
38f7b870
PM
1836
1837 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1838 if (err < 0)
1839 return err;
1840
1841 if (tb[IFLA_IFNAME])
1842 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1843
1844 ifm = nlmsg_data(nlh);
1845 if (ifm->ifi_index > 0)
881d966b 1846 dev = __dev_get_by_index(net, ifm->ifi_index);
38f7b870 1847 else if (tb[IFLA_IFNAME])
881d966b 1848 dev = __dev_get_by_name(net, ifname);
38f7b870
PM
1849 else
1850 return -EINVAL;
1851
1852 if (!dev)
1853 return -ENODEV;
1854
1855 ops = dev->rtnl_link_ops;
b0ab2fab 1856 if (!ops || !ops->dellink)
38f7b870
PM
1857 return -EOPNOTSUPP;
1858
226bd341
ED
1859 ops->dellink(dev, &list_kill);
1860 unregister_netdevice_many(&list_kill);
38f7b870
PM
1861 return 0;
1862}
1863
3729d502
PM
1864int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
1865{
1866 unsigned int old_flags;
1867 int err;
1868
1869 old_flags = dev->flags;
1870 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
1871 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
1872 if (err < 0)
1873 return err;
1874 }
1875
1876 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
3729d502 1877
a528c219 1878 __dev_notify_flags(dev, old_flags, ~0U);
3729d502
PM
1879 return 0;
1880}
1881EXPORT_SYMBOL(rtnl_configure_link);
1882
c0713563 1883struct net_device *rtnl_create_link(struct net *net,
5517750f
TG
1884 char *ifname, unsigned char name_assign_type,
1885 const struct rtnl_link_ops *ops, struct nlattr *tb[])
e7199288
PE
1886{
1887 int err;
1888 struct net_device *dev;
d40156aa
JP
1889 unsigned int num_tx_queues = 1;
1890 unsigned int num_rx_queues = 1;
e7199288 1891
76ff5cc9
JP
1892 if (tb[IFLA_NUM_TX_QUEUES])
1893 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
1894 else if (ops->get_num_tx_queues)
d40156aa 1895 num_tx_queues = ops->get_num_tx_queues();
76ff5cc9
JP
1896
1897 if (tb[IFLA_NUM_RX_QUEUES])
1898 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
1899 else if (ops->get_num_rx_queues)
d40156aa 1900 num_rx_queues = ops->get_num_rx_queues();
efacb309 1901
e7199288 1902 err = -ENOMEM;
5517750f 1903 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
c835a677 1904 ops->setup, num_tx_queues, num_rx_queues);
e7199288
PE
1905 if (!dev)
1906 goto err;
1907
81adee47
EB
1908 dev_net_set(dev, net);
1909 dev->rtnl_link_ops = ops;
3729d502 1910 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
81adee47 1911
e7199288
PE
1912 if (tb[IFLA_MTU])
1913 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
2afb9b53 1914 if (tb[IFLA_ADDRESS]) {
e7199288
PE
1915 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
1916 nla_len(tb[IFLA_ADDRESS]));
2afb9b53
JP
1917 dev->addr_assign_type = NET_ADDR_SET;
1918 }
e7199288
PE
1919 if (tb[IFLA_BROADCAST])
1920 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
1921 nla_len(tb[IFLA_BROADCAST]));
1922 if (tb[IFLA_TXQLEN])
1923 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1924 if (tb[IFLA_OPERSTATE])
93b2d4a2 1925 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
e7199288
PE
1926 if (tb[IFLA_LINKMODE])
1927 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
ffa934f1
PM
1928 if (tb[IFLA_GROUP])
1929 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
e7199288
PE
1930
1931 return dev;
1932
e7199288
PE
1933err:
1934 return ERR_PTR(err);
1935}
e0d087af 1936EXPORT_SYMBOL(rtnl_create_link);
e7199288 1937
90f62cf3
EB
1938static int rtnl_group_changelink(const struct sk_buff *skb,
1939 struct net *net, int group,
e7ed828f
VD
1940 struct ifinfomsg *ifm,
1941 struct nlattr **tb)
1942{
1943 struct net_device *dev;
1944 int err;
1945
1946 for_each_netdev(net, dev) {
1947 if (dev->group == group) {
90f62cf3 1948 err = do_setlink(skb, dev, ifm, tb, NULL, 0);
e7ed828f
VD
1949 if (err < 0)
1950 return err;
1951 }
1952 }
1953
1954 return 0;
1955}
1956
661d2967 1957static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh)
38f7b870 1958{
3b1e0a65 1959 struct net *net = sock_net(skb->sk);
38f7b870 1960 const struct rtnl_link_ops *ops;
ba7d49b1 1961 const struct rtnl_link_ops *m_ops = NULL;
38f7b870 1962 struct net_device *dev;
ba7d49b1 1963 struct net_device *master_dev = NULL;
38f7b870
PM
1964 struct ifinfomsg *ifm;
1965 char kind[MODULE_NAME_LEN];
1966 char ifname[IFNAMSIZ];
1967 struct nlattr *tb[IFLA_MAX+1];
1968 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
5517750f 1969 unsigned char name_assign_type = NET_NAME_USER;
38f7b870
PM
1970 int err;
1971
95a5afca 1972#ifdef CONFIG_MODULES
38f7b870 1973replay:
8072f085 1974#endif
38f7b870
PM
1975 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1976 if (err < 0)
1977 return err;
1978
1979 if (tb[IFLA_IFNAME])
1980 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1981 else
1982 ifname[0] = '\0';
1983
1984 ifm = nlmsg_data(nlh);
1985 if (ifm->ifi_index > 0)
881d966b 1986 dev = __dev_get_by_index(net, ifm->ifi_index);
e7ed828f
VD
1987 else {
1988 if (ifname[0])
1989 dev = __dev_get_by_name(net, ifname);
e7ed828f
VD
1990 else
1991 dev = NULL;
1992 }
38f7b870 1993
ba7d49b1
JP
1994 if (dev) {
1995 master_dev = netdev_master_upper_dev_get(dev);
1996 if (master_dev)
1997 m_ops = master_dev->rtnl_link_ops;
1998 }
1999
e0d087af
ED
2000 err = validate_linkmsg(dev, tb);
2001 if (err < 0)
1840bb13
TG
2002 return err;
2003
38f7b870
PM
2004 if (tb[IFLA_LINKINFO]) {
2005 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
2006 tb[IFLA_LINKINFO], ifla_info_policy);
2007 if (err < 0)
2008 return err;
2009 } else
2010 memset(linkinfo, 0, sizeof(linkinfo));
2011
2012 if (linkinfo[IFLA_INFO_KIND]) {
2013 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
2014 ops = rtnl_link_ops_get(kind);
2015 } else {
2016 kind[0] = '\0';
2017 ops = NULL;
2018 }
2019
2020 if (1) {
ba7d49b1
JP
2021 struct nlattr *attr[ops ? ops->maxtype + 1 : 0];
2022 struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 0];
2023 struct nlattr **data = NULL;
2024 struct nlattr **slave_data = NULL;
317f4810 2025 struct net *dest_net, *link_net = NULL;
38f7b870
PM
2026
2027 if (ops) {
2028 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
2029 err = nla_parse_nested(attr, ops->maxtype,
2030 linkinfo[IFLA_INFO_DATA],
2031 ops->policy);
2032 if (err < 0)
2033 return err;
2034 data = attr;
2035 }
2036 if (ops->validate) {
2037 err = ops->validate(tb, data);
2038 if (err < 0)
2039 return err;
2040 }
2041 }
2042
ba7d49b1
JP
2043 if (m_ops) {
2044 if (m_ops->slave_maxtype &&
2045 linkinfo[IFLA_INFO_SLAVE_DATA]) {
2046 err = nla_parse_nested(slave_attr,
2047 m_ops->slave_maxtype,
2048 linkinfo[IFLA_INFO_SLAVE_DATA],
2049 m_ops->slave_policy);
2050 if (err < 0)
2051 return err;
2052 slave_data = slave_attr;
2053 }
2054 if (m_ops->slave_validate) {
2055 err = m_ops->slave_validate(tb, slave_data);
2056 if (err < 0)
2057 return err;
2058 }
2059 }
2060
38f7b870 2061 if (dev) {
90c325e3 2062 int status = 0;
38f7b870
PM
2063
2064 if (nlh->nlmsg_flags & NLM_F_EXCL)
2065 return -EEXIST;
2066 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2067 return -EOPNOTSUPP;
2068
2069 if (linkinfo[IFLA_INFO_DATA]) {
2070 if (!ops || ops != dev->rtnl_link_ops ||
2071 !ops->changelink)
2072 return -EOPNOTSUPP;
2073
2074 err = ops->changelink(dev, tb, data);
2075 if (err < 0)
2076 return err;
ba998906 2077 status |= DO_SETLINK_NOTIFY;
38f7b870
PM
2078 }
2079
ba7d49b1
JP
2080 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2081 if (!m_ops || !m_ops->slave_changelink)
2082 return -EOPNOTSUPP;
2083
2084 err = m_ops->slave_changelink(master_dev, dev,
2085 tb, slave_data);
2086 if (err < 0)
2087 return err;
ba998906 2088 status |= DO_SETLINK_NOTIFY;
ba7d49b1
JP
2089 }
2090
90c325e3 2091 return do_setlink(skb, dev, ifm, tb, ifname, status);
38f7b870
PM
2092 }
2093
ffa934f1
PM
2094 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2095 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
90f62cf3 2096 return rtnl_group_changelink(skb, net,
ffa934f1
PM
2097 nla_get_u32(tb[IFLA_GROUP]),
2098 ifm, tb);
38f7b870 2099 return -ENODEV;
ffa934f1 2100 }
38f7b870 2101
0e06877c 2102 if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
38f7b870
PM
2103 return -EOPNOTSUPP;
2104
2105 if (!ops) {
95a5afca 2106#ifdef CONFIG_MODULES
38f7b870
PM
2107 if (kind[0]) {
2108 __rtnl_unlock();
2109 request_module("rtnl-link-%s", kind);
2110 rtnl_lock();
2111 ops = rtnl_link_ops_get(kind);
2112 if (ops)
2113 goto replay;
2114 }
2115#endif
2116 return -EOPNOTSUPP;
2117 }
2118
b0ab2fab
JP
2119 if (!ops->setup)
2120 return -EOPNOTSUPP;
2121
5517750f 2122 if (!ifname[0]) {
38f7b870 2123 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
5517750f
TG
2124 name_assign_type = NET_NAME_ENUM;
2125 }
e7199288 2126
81adee47 2127 dest_net = rtnl_link_get_net(net, tb);
13ad1774
EB
2128 if (IS_ERR(dest_net))
2129 return PTR_ERR(dest_net);
2130
317f4810
ND
2131 if (tb[IFLA_LINK_NETNSID]) {
2132 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
2133
2134 link_net = get_net_ns_by_id(dest_net, id);
2135 if (!link_net) {
2136 err = -EINVAL;
2137 goto out;
2138 }
2139 }
2140
2141 dev = rtnl_create_link(link_net ? : dest_net, ifname,
2142 name_assign_type, ops, tb);
9c7dafbf 2143 if (IS_ERR(dev)) {
e7199288 2144 err = PTR_ERR(dev);
9c7dafbf
PE
2145 goto out;
2146 }
2147
2148 dev->ifindex = ifm->ifi_index;
2149
0e0eee24 2150 if (ops->newlink) {
81adee47 2151 err = ops->newlink(net, dev, tb, data);
0e0eee24 2152 /* Drivers should call free_netdev() in ->destructor
e51fb152
CW
2153 * and unregister it on failure after registration
2154 * so that device could be finally freed in rtnl_unlock.
0e0eee24 2155 */
e51fb152
CW
2156 if (err < 0) {
2157 /* If device is not registered at all, free it now */
2158 if (dev->reg_state == NETREG_UNINITIALIZED)
2159 free_netdev(dev);
0e0eee24 2160 goto out;
e51fb152 2161 }
0e0eee24 2162 } else {
2d85cba2 2163 err = register_netdevice(dev);
0e0eee24
CW
2164 if (err < 0) {
2165 free_netdev(dev);
2166 goto out;
2167 }
fce9b9be 2168 }
3729d502 2169 err = rtnl_configure_link(dev, ifm);
317f4810 2170 if (err < 0) {
3729d502 2171 unregister_netdevice(dev);
317f4810
ND
2172 goto out;
2173 }
2174
2175 if (link_net)
2176 err = dev_change_net_namespace(dev, dest_net, ifname);
3729d502 2177out:
317f4810
ND
2178 if (link_net)
2179 put_net(link_net);
81adee47 2180 put_net(dest_net);
38f7b870
PM
2181 return err;
2182 }
2183}
2184
661d2967 2185static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh)
711e2c33 2186{
3b1e0a65 2187 struct net *net = sock_net(skb->sk);
b60c5115 2188 struct ifinfomsg *ifm;
a3d12891 2189 char ifname[IFNAMSIZ];
b60c5115
TG
2190 struct nlattr *tb[IFLA_MAX+1];
2191 struct net_device *dev = NULL;
2192 struct sk_buff *nskb;
339bf98f 2193 int err;
115c9b81 2194 u32 ext_filter_mask = 0;
711e2c33 2195
b60c5115
TG
2196 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2197 if (err < 0)
9918f230 2198 return err;
b60c5115 2199
a3d12891
ED
2200 if (tb[IFLA_IFNAME])
2201 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2202
115c9b81
GR
2203 if (tb[IFLA_EXT_MASK])
2204 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2205
b60c5115 2206 ifm = nlmsg_data(nlh);
a3d12891
ED
2207 if (ifm->ifi_index > 0)
2208 dev = __dev_get_by_index(net, ifm->ifi_index);
2209 else if (tb[IFLA_IFNAME])
2210 dev = __dev_get_by_name(net, ifname);
2211 else
711e2c33 2212 return -EINVAL;
711e2c33 2213
a3d12891
ED
2214 if (dev == NULL)
2215 return -ENODEV;
2216
115c9b81 2217 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
a3d12891
ED
2218 if (nskb == NULL)
2219 return -ENOBUFS;
b60c5115 2220
15e47304 2221 err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
115c9b81 2222 nlh->nlmsg_seq, 0, 0, ext_filter_mask);
26932566
PM
2223 if (err < 0) {
2224 /* -EMSGSIZE implies BUG in if_nlmsg_size */
2225 WARN_ON(err == -EMSGSIZE);
2226 kfree_skb(nskb);
a3d12891 2227 } else
15e47304 2228 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
711e2c33 2229
b60c5115 2230 return err;
711e2c33 2231}
711e2c33 2232
115c9b81 2233static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
c7ac8679 2234{
115c9b81
GR
2235 struct net *net = sock_net(skb->sk);
2236 struct net_device *dev;
2237 struct nlattr *tb[IFLA_MAX+1];
2238 u32 ext_filter_mask = 0;
2239 u16 min_ifinfo_dump_size = 0;
e5eca6d4
MS
2240 int hdrlen;
2241
2242 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2243 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2244 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
115c9b81 2245
e5eca6d4 2246 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
a4b64fbe
ED
2247 if (tb[IFLA_EXT_MASK])
2248 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2249 }
115c9b81
GR
2250
2251 if (!ext_filter_mask)
2252 return NLMSG_GOODSIZE;
2253 /*
2254 * traverse the list of net devices and compute the minimum
2255 * buffer size based upon the filter mask.
2256 */
2257 list_for_each_entry(dev, &net->dev_base_head, dev_list) {
2258 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
2259 if_nlmsg_size(dev,
2260 ext_filter_mask));
2261 }
2262
c7ac8679
GR
2263 return min_ifinfo_dump_size;
2264}
2265
42bad1da 2266static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
1da177e4
LT
2267{
2268 int idx;
2269 int s_idx = cb->family;
2270
2271 if (s_idx == 0)
2272 s_idx = 1;
25239cee 2273 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
1da177e4
LT
2274 int type = cb->nlh->nlmsg_type-RTM_BASE;
2275 if (idx < s_idx || idx == PF_PACKET)
2276 continue;
e2849863
TG
2277 if (rtnl_msg_handlers[idx] == NULL ||
2278 rtnl_msg_handlers[idx][type].dumpit == NULL)
1da177e4 2279 continue;
0465277f 2280 if (idx > s_idx) {
1da177e4 2281 memset(&cb->args[0], 0, sizeof(cb->args));
0465277f
ND
2282 cb->prev_seq = 0;
2283 cb->seq = 0;
2284 }
e2849863 2285 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
1da177e4
LT
2286 break;
2287 }
2288 cb->family = idx;
2289
2290 return skb->len;
2291}
2292
395eea6c
MB
2293struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
2294 unsigned int change, gfp_t flags)
1da177e4 2295{
c346dca1 2296 struct net *net = dev_net(dev);
1da177e4 2297 struct sk_buff *skb;
0ec6d3f4 2298 int err = -ENOBUFS;
c7ac8679 2299 size_t if_info_size;
1da177e4 2300
7f294054 2301 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
0ec6d3f4
TG
2302 if (skb == NULL)
2303 goto errout;
1da177e4 2304
115c9b81 2305 err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
26932566
PM
2306 if (err < 0) {
2307 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
2308 WARN_ON(err == -EMSGSIZE);
2309 kfree_skb(skb);
2310 goto errout;
2311 }
395eea6c 2312 return skb;
0ec6d3f4
TG
2313errout:
2314 if (err < 0)
4b3da706 2315 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
395eea6c
MB
2316 return NULL;
2317}
2318
2319void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
2320{
2321 struct net *net = dev_net(dev);
2322
2323 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
2324}
2325
2326void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
2327 gfp_t flags)
2328{
2329 struct sk_buff *skb;
2330
2331 skb = rtmsg_ifinfo_build_skb(type, dev, change, flags);
2332 if (skb)
2333 rtmsg_ifinfo_send(skb, dev, flags);
1da177e4 2334}
471cb5a3 2335EXPORT_SYMBOL(rtmsg_ifinfo);
1da177e4 2336
d83b0603
JF
2337static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
2338 struct net_device *dev,
2339 u8 *addr, u32 pid, u32 seq,
1c104a6b
ND
2340 int type, unsigned int flags,
2341 int nlflags)
d83b0603
JF
2342{
2343 struct nlmsghdr *nlh;
2344 struct ndmsg *ndm;
2345
1c104a6b 2346 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
d83b0603
JF
2347 if (!nlh)
2348 return -EMSGSIZE;
2349
2350 ndm = nlmsg_data(nlh);
2351 ndm->ndm_family = AF_BRIDGE;
2352 ndm->ndm_pad1 = 0;
2353 ndm->ndm_pad2 = 0;
2354 ndm->ndm_flags = flags;
2355 ndm->ndm_type = 0;
2356 ndm->ndm_ifindex = dev->ifindex;
2357 ndm->ndm_state = NUD_PERMANENT;
2358
2359 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
2360 goto nla_put_failure;
2361
053c095a
JB
2362 nlmsg_end(skb, nlh);
2363 return 0;
d83b0603
JF
2364
2365nla_put_failure:
2366 nlmsg_cancel(skb, nlh);
2367 return -EMSGSIZE;
2368}
2369
3ff661c3
JF
2370static inline size_t rtnl_fdb_nlmsg_size(void)
2371{
2372 return NLMSG_ALIGN(sizeof(struct ndmsg)) + nla_total_size(ETH_ALEN);
2373}
2374
2375static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, int type)
2376{
2377 struct net *net = dev_net(dev);
2378 struct sk_buff *skb;
2379 int err = -ENOBUFS;
2380
2381 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
2382 if (!skb)
2383 goto errout;
2384
1c104a6b 2385 err = nlmsg_populate_fdb_fill(skb, dev, addr, 0, 0, type, NTF_SELF, 0);
3ff661c3
JF
2386 if (err < 0) {
2387 kfree_skb(skb);
2388 goto errout;
2389 }
2390
2391 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
2392 return;
2393errout:
2394 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
2395}
2396
090096bf
VY
2397/**
2398 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2399 */
2400int ndo_dflt_fdb_add(struct ndmsg *ndm,
2401 struct nlattr *tb[],
2402 struct net_device *dev,
f6f6424b 2403 const unsigned char *addr, u16 vid,
090096bf
VY
2404 u16 flags)
2405{
2406 int err = -EINVAL;
2407
2408 /* If aging addresses are supported device will need to
2409 * implement its own handler for this.
2410 */
2411 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2412 pr_info("%s: FDB only supports static addresses\n", dev->name);
2413 return err;
2414 }
2415
65891fea
OG
2416 if (vid) {
2417 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
2418 return err;
2419 }
2420
090096bf
VY
2421 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2422 err = dev_uc_add_excl(dev, addr);
2423 else if (is_multicast_ether_addr(addr))
2424 err = dev_mc_add_excl(dev, addr);
2425
2426 /* Only return duplicate errors if NLM_F_EXCL is set */
2427 if (err == -EEXIST && !(flags & NLM_F_EXCL))
2428 err = 0;
2429
2430 return err;
2431}
2432EXPORT_SYMBOL(ndo_dflt_fdb_add);
2433
f6f6424b
JP
2434static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid)
2435{
2436 u16 vid = 0;
2437
2438 if (vlan_attr) {
2439 if (nla_len(vlan_attr) != sizeof(u16)) {
2440 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n");
2441 return -EINVAL;
2442 }
2443
2444 vid = nla_get_u16(vlan_attr);
2445
2446 if (!vid || vid >= VLAN_VID_MASK) {
2447 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n",
2448 vid);
2449 return -EINVAL;
2450 }
2451 }
2452 *p_vid = vid;
2453 return 0;
2454}
2455
661d2967 2456static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh)
77162022
JF
2457{
2458 struct net *net = sock_net(skb->sk);
77162022
JF
2459 struct ndmsg *ndm;
2460 struct nlattr *tb[NDA_MAX+1];
2461 struct net_device *dev;
2462 u8 *addr;
f6f6424b 2463 u16 vid;
77162022
JF
2464 int err;
2465
2466 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2467 if (err < 0)
2468 return err;
2469
2470 ndm = nlmsg_data(nlh);
2471 if (ndm->ndm_ifindex == 0) {
2472 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
2473 return -EINVAL;
2474 }
2475
2476 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2477 if (dev == NULL) {
2478 pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
2479 return -ENODEV;
2480 }
2481
2482 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2483 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
2484 return -EINVAL;
2485 }
2486
2487 addr = nla_data(tb[NDA_LLADDR]);
77162022 2488
f6f6424b
JP
2489 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2490 if (err)
2491 return err;
2492
77162022
JF
2493 err = -EOPNOTSUPP;
2494
2495 /* Support fdb on master device the net/bridge default case */
2496 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2497 (dev->priv_flags & IFF_BRIDGE_PORT)) {
898e5061
JP
2498 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2499 const struct net_device_ops *ops = br_dev->netdev_ops;
2500
f6f6424b
JP
2501 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
2502 nlh->nlmsg_flags);
77162022
JF
2503 if (err)
2504 goto out;
2505 else
2506 ndm->ndm_flags &= ~NTF_MASTER;
2507 }
2508
2509 /* Embedded bridge, macvlan, and any other device support */
090096bf
VY
2510 if ((ndm->ndm_flags & NTF_SELF)) {
2511 if (dev->netdev_ops->ndo_fdb_add)
2512 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
f6f6424b 2513 vid,
090096bf
VY
2514 nlh->nlmsg_flags);
2515 else
f6f6424b 2516 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
090096bf 2517 nlh->nlmsg_flags);
77162022 2518
3ff661c3
JF
2519 if (!err) {
2520 rtnl_fdb_notify(dev, addr, RTM_NEWNEIGH);
77162022 2521 ndm->ndm_flags &= ~NTF_SELF;
3ff661c3 2522 }
77162022
JF
2523 }
2524out:
2525 return err;
2526}
2527
090096bf
VY
2528/**
2529 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
2530 */
2531int ndo_dflt_fdb_del(struct ndmsg *ndm,
2532 struct nlattr *tb[],
2533 struct net_device *dev,
f6f6424b 2534 const unsigned char *addr, u16 vid)
090096bf 2535{
c8a89c4a 2536 int err = -EINVAL;
090096bf
VY
2537
2538 /* If aging addresses are supported device will need to
2539 * implement its own handler for this.
2540 */
64535993 2541 if (!(ndm->ndm_state & NUD_PERMANENT)) {
090096bf 2542 pr_info("%s: FDB only supports static addresses\n", dev->name);
c8a89c4a 2543 return err;
090096bf
VY
2544 }
2545
2546 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2547 err = dev_uc_del(dev, addr);
2548 else if (is_multicast_ether_addr(addr))
2549 err = dev_mc_del(dev, addr);
090096bf
VY
2550
2551 return err;
2552}
2553EXPORT_SYMBOL(ndo_dflt_fdb_del);
2554
661d2967 2555static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh)
77162022
JF
2556{
2557 struct net *net = sock_net(skb->sk);
2558 struct ndmsg *ndm;
1690be63 2559 struct nlattr *tb[NDA_MAX+1];
77162022
JF
2560 struct net_device *dev;
2561 int err = -EINVAL;
2562 __u8 *addr;
f6f6424b 2563 u16 vid;
77162022 2564
90f62cf3 2565 if (!netlink_capable(skb, CAP_NET_ADMIN))
1690be63
VY
2566 return -EPERM;
2567
2568 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2569 if (err < 0)
2570 return err;
77162022
JF
2571
2572 ndm = nlmsg_data(nlh);
2573 if (ndm->ndm_ifindex == 0) {
2574 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
2575 return -EINVAL;
2576 }
2577
2578 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2579 if (dev == NULL) {
2580 pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
2581 return -ENODEV;
2582 }
2583
1690be63
VY
2584 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2585 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
2586 return -EINVAL;
2587 }
2588
2589 addr = nla_data(tb[NDA_LLADDR]);
77162022 2590
f6f6424b
JP
2591 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2592 if (err)
2593 return err;
2594
77162022
JF
2595 err = -EOPNOTSUPP;
2596
2597 /* Support fdb on master device the net/bridge default case */
2598 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2599 (dev->priv_flags & IFF_BRIDGE_PORT)) {
898e5061
JP
2600 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2601 const struct net_device_ops *ops = br_dev->netdev_ops;
77162022 2602
898e5061 2603 if (ops->ndo_fdb_del)
f6f6424b 2604 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
77162022
JF
2605
2606 if (err)
2607 goto out;
2608 else
2609 ndm->ndm_flags &= ~NTF_MASTER;
2610 }
2611
2612 /* Embedded bridge, macvlan, and any other device support */
090096bf
VY
2613 if (ndm->ndm_flags & NTF_SELF) {
2614 if (dev->netdev_ops->ndo_fdb_del)
f6f6424b
JP
2615 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
2616 vid);
090096bf 2617 else
f6f6424b 2618 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
77162022 2619
3ff661c3
JF
2620 if (!err) {
2621 rtnl_fdb_notify(dev, addr, RTM_DELNEIGH);
77162022 2622 ndm->ndm_flags &= ~NTF_SELF;
3ff661c3 2623 }
77162022
JF
2624 }
2625out:
2626 return err;
2627}
2628
d83b0603
JF
2629static int nlmsg_populate_fdb(struct sk_buff *skb,
2630 struct netlink_callback *cb,
2631 struct net_device *dev,
2632 int *idx,
2633 struct netdev_hw_addr_list *list)
2634{
2635 struct netdev_hw_addr *ha;
2636 int err;
15e47304 2637 u32 portid, seq;
d83b0603 2638
15e47304 2639 portid = NETLINK_CB(cb->skb).portid;
d83b0603
JF
2640 seq = cb->nlh->nlmsg_seq;
2641
2642 list_for_each_entry(ha, &list->list, list) {
2643 if (*idx < cb->args[0])
2644 goto skip;
2645
2646 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr,
a7a558fe 2647 portid, seq,
1c104a6b
ND
2648 RTM_NEWNEIGH, NTF_SELF,
2649 NLM_F_MULTI);
d83b0603
JF
2650 if (err < 0)
2651 return err;
2652skip:
2653 *idx += 1;
2654 }
2655 return 0;
2656}
2657
2658/**
2c53040f 2659 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
d83b0603
JF
2660 * @nlh: netlink message header
2661 * @dev: netdevice
2662 *
2663 * Default netdevice operation to dump the existing unicast address list.
91f3e7b1 2664 * Returns number of addresses from list put in skb.
d83b0603
JF
2665 */
2666int ndo_dflt_fdb_dump(struct sk_buff *skb,
2667 struct netlink_callback *cb,
2668 struct net_device *dev,
5d5eacb3 2669 struct net_device *filter_dev,
d83b0603
JF
2670 int idx)
2671{
2672 int err;
2673
2674 netif_addr_lock_bh(dev);
2675 err = nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->uc);
2676 if (err)
2677 goto out;
2678 nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->mc);
2679out:
2680 netif_addr_unlock_bh(dev);
2681 return idx;
2682}
2683EXPORT_SYMBOL(ndo_dflt_fdb_dump);
2684
77162022
JF
2685static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
2686{
77162022 2687 struct net_device *dev;
5e6d2435
JHS
2688 struct nlattr *tb[IFLA_MAX+1];
2689 struct net_device *bdev = NULL;
2690 struct net_device *br_dev = NULL;
2691 const struct net_device_ops *ops = NULL;
2692 const struct net_device_ops *cops = NULL;
2693 struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
2694 struct net *net = sock_net(skb->sk);
2695 int brport_idx = 0;
2696 int br_idx = 0;
2697 int idx = 0;
2698
2699 if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
2700 ifla_policy) == 0) {
2701 if (tb[IFLA_MASTER])
2702 br_idx = nla_get_u32(tb[IFLA_MASTER]);
2703 }
2704
2705 brport_idx = ifm->ifi_index;
2706
2707 if (br_idx) {
2708 br_dev = __dev_get_by_index(net, br_idx);
2709 if (!br_dev)
2710 return -ENODEV;
2711
2712 ops = br_dev->netdev_ops;
2713 bdev = br_dev;
2714 }
2715
2716 for_each_netdev(net, dev) {
2717 if (brport_idx && (dev->ifindex != brport_idx))
2718 continue;
2719
2720 if (!br_idx) { /* user did not specify a specific bridge */
2721 if (dev->priv_flags & IFF_BRIDGE_PORT) {
2722 br_dev = netdev_master_upper_dev_get(dev);
2723 cops = br_dev->netdev_ops;
2724 }
2725
2726 bdev = dev;
2727 } else {
2728 if (dev != br_dev &&
2729 !(dev->priv_flags & IFF_BRIDGE_PORT))
2730 continue;
2731
2732 if (br_dev != netdev_master_upper_dev_get(dev) &&
2733 !(dev->priv_flags & IFF_EBRIDGE))
2734 continue;
2735
2736 bdev = br_dev;
2737 cops = ops;
2738 }
77162022 2739
77162022 2740 if (dev->priv_flags & IFF_BRIDGE_PORT) {
5e6d2435
JHS
2741 if (cops && cops->ndo_fdb_dump)
2742 idx = cops->ndo_fdb_dump(skb, cb, br_dev, dev,
2743 idx);
77162022
JF
2744 }
2745
2746 if (dev->netdev_ops->ndo_fdb_dump)
6cb69742 2747 idx = dev->netdev_ops->ndo_fdb_dump(skb, cb, dev, NULL,
5d5eacb3 2748 idx);
6cb69742
HS
2749 else
2750 idx = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx);
5e6d2435
JHS
2751
2752 cops = NULL;
77162022 2753 }
77162022
JF
2754
2755 cb->args[0] = idx;
2756 return skb->len;
2757}
2758
2c3c031c
SF
2759static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
2760 unsigned int attrnum, unsigned int flag)
2761{
2762 if (mask & flag)
2763 return nla_put_u8(skb, attrnum, !!(flags & flag));
2764 return 0;
2765}
2766
815cccbf 2767int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
2c3c031c
SF
2768 struct net_device *dev, u16 mode,
2769 u32 flags, u32 mask)
815cccbf
JF
2770{
2771 struct nlmsghdr *nlh;
2772 struct ifinfomsg *ifm;
2773 struct nlattr *br_afspec;
2c3c031c 2774 struct nlattr *protinfo;
815cccbf 2775 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
898e5061 2776 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
815cccbf
JF
2777
2778 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), NLM_F_MULTI);
2779 if (nlh == NULL)
2780 return -EMSGSIZE;
2781
2782 ifm = nlmsg_data(nlh);
2783 ifm->ifi_family = AF_BRIDGE;
2784 ifm->__ifi_pad = 0;
2785 ifm->ifi_type = dev->type;
2786 ifm->ifi_index = dev->ifindex;
2787 ifm->ifi_flags = dev_get_flags(dev);
2788 ifm->ifi_change = 0;
2789
2790
2791 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
2792 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
2793 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
898e5061
JP
2794 (br_dev &&
2795 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
815cccbf
JF
2796 (dev->addr_len &&
2797 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
2798 (dev->ifindex != dev->iflink &&
2799 nla_put_u32(skb, IFLA_LINK, dev->iflink)))
2800 goto nla_put_failure;
2801
2802 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
2803 if (!br_afspec)
2804 goto nla_put_failure;
2805
1d460b98 2806 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
815cccbf
JF
2807 nla_nest_cancel(skb, br_afspec);
2808 goto nla_put_failure;
2809 }
1d460b98
RP
2810
2811 if (mode != BRIDGE_MODE_UNDEF) {
2812 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
2813 nla_nest_cancel(skb, br_afspec);
2814 goto nla_put_failure;
2815 }
2816 }
815cccbf
JF
2817 nla_nest_end(skb, br_afspec);
2818
2c3c031c
SF
2819 protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
2820 if (!protinfo)
2821 goto nla_put_failure;
2822
2823 if (brport_nla_put_flag(skb, flags, mask,
2824 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
2825 brport_nla_put_flag(skb, flags, mask,
2826 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
2827 brport_nla_put_flag(skb, flags, mask,
2828 IFLA_BRPORT_FAST_LEAVE,
2829 BR_MULTICAST_FAST_LEAVE) ||
2830 brport_nla_put_flag(skb, flags, mask,
2831 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
2832 brport_nla_put_flag(skb, flags, mask,
2833 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
2834 brport_nla_put_flag(skb, flags, mask,
2835 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
2836 brport_nla_put_flag(skb, flags, mask,
2837 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
2838 brport_nla_put_flag(skb, flags, mask,
2839 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
2840 nla_nest_cancel(skb, protinfo);
2841 goto nla_put_failure;
2842 }
2843
2844 nla_nest_end(skb, protinfo);
2845
053c095a
JB
2846 nlmsg_end(skb, nlh);
2847 return 0;
815cccbf
JF
2848nla_put_failure:
2849 nlmsg_cancel(skb, nlh);
2850 return -EMSGSIZE;
2851}
2852EXPORT_SYMBOL(ndo_dflt_bridge_getlink);
2853
e5a55a89
JF
2854static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
2855{
2856 struct net *net = sock_net(skb->sk);
2857 struct net_device *dev;
2858 int idx = 0;
2859 u32 portid = NETLINK_CB(cb->skb).portid;
2860 u32 seq = cb->nlh->nlmsg_seq;
6cbdceeb
VY
2861 u32 filter_mask = 0;
2862
aa68c20f
TG
2863 if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
2864 struct nlattr *extfilt;
2865
2866 extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
2867 IFLA_EXT_MASK);
2868 if (extfilt) {
2869 if (nla_len(extfilt) < sizeof(filter_mask))
2870 return -EINVAL;
2871
2872 filter_mask = nla_get_u32(extfilt);
2873 }
2874 }
e5a55a89
JF
2875
2876 rcu_read_lock();
2877 for_each_netdev_rcu(net, dev) {
2878 const struct net_device_ops *ops = dev->netdev_ops;
898e5061 2879 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
e5a55a89 2880
898e5061 2881 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
25b1e679 2882 if (idx >= cb->args[0] &&
898e5061 2883 br_dev->netdev_ops->ndo_bridge_getlink(
6cbdceeb 2884 skb, portid, seq, dev, filter_mask) < 0)
e5a55a89 2885 break;
25b1e679 2886 idx++;
e5a55a89
JF
2887 }
2888
2889 if (ops->ndo_bridge_getlink) {
25b1e679 2890 if (idx >= cb->args[0] &&
6cbdceeb
VY
2891 ops->ndo_bridge_getlink(skb, portid, seq, dev,
2892 filter_mask) < 0)
e5a55a89 2893 break;
25b1e679 2894 idx++;
e5a55a89
JF
2895 }
2896 }
2897 rcu_read_unlock();
2898 cb->args[0] = idx;
2899
2900 return skb->len;
2901}
2902
2469ffd7
JF
2903static inline size_t bridge_nlmsg_size(void)
2904{
2905 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
2906 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
2907 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
2908 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
2909 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
2910 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
2911 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
2912 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
2913 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
2914 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
2915 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
2916}
2917
02dba438 2918static int rtnl_bridge_notify(struct net_device *dev)
2469ffd7
JF
2919{
2920 struct net *net = dev_net(dev);
2469ffd7
JF
2921 struct sk_buff *skb;
2922 int err = -EOPNOTSUPP;
2923
02dba438
RP
2924 if (!dev->netdev_ops->ndo_bridge_getlink)
2925 return 0;
2926
2469ffd7
JF
2927 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
2928 if (!skb) {
2929 err = -ENOMEM;
2930 goto errout;
2931 }
2932
02dba438
RP
2933 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0);
2934 if (err < 0)
2935 goto errout;
2469ffd7
JF
2936
2937 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
2938 return 0;
2939errout:
2940 WARN_ON(err == -EMSGSIZE);
2941 kfree_skb(skb);
2942 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
2943 return err;
2944}
2945
661d2967 2946static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
e5a55a89
JF
2947{
2948 struct net *net = sock_net(skb->sk);
2949 struct ifinfomsg *ifm;
2950 struct net_device *dev;
2469ffd7
JF
2951 struct nlattr *br_spec, *attr = NULL;
2952 int rem, err = -EOPNOTSUPP;
4de8b413 2953 u16 flags = 0;
c38e01b8 2954 bool have_flags = false;
e5a55a89
JF
2955
2956 if (nlmsg_len(nlh) < sizeof(*ifm))
2957 return -EINVAL;
2958
2959 ifm = nlmsg_data(nlh);
2960 if (ifm->ifi_family != AF_BRIDGE)
2961 return -EPFNOSUPPORT;
2962
2963 dev = __dev_get_by_index(net, ifm->ifi_index);
2964 if (!dev) {
2965 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
2966 return -ENODEV;
2967 }
2968
2469ffd7
JF
2969 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
2970 if (br_spec) {
2971 nla_for_each_nested(attr, br_spec, rem) {
2972 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
6e8d1c55
TG
2973 if (nla_len(attr) < sizeof(flags))
2974 return -EINVAL;
2975
c38e01b8 2976 have_flags = true;
2469ffd7
JF
2977 flags = nla_get_u16(attr);
2978 break;
2979 }
2980 }
2981 }
2982
2983 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
898e5061
JP
2984 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2985
2986 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
2469ffd7
JF
2987 err = -EOPNOTSUPP;
2988 goto out;
2989 }
2990
898e5061 2991 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh);
e5a55a89
JF
2992 if (err)
2993 goto out;
2469ffd7
JF
2994
2995 flags &= ~BRIDGE_FLAGS_MASTER;
e5a55a89
JF
2996 }
2997
2469ffd7
JF
2998 if ((flags & BRIDGE_FLAGS_SELF)) {
2999 if (!dev->netdev_ops->ndo_bridge_setlink)
3000 err = -EOPNOTSUPP;
3001 else
3002 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh);
02dba438 3003 if (!err) {
2469ffd7 3004 flags &= ~BRIDGE_FLAGS_SELF;
02dba438
RP
3005
3006 /* Generate event to notify upper layer of bridge
3007 * change
3008 */
3009 err = rtnl_bridge_notify(dev);
3010 }
2469ffd7 3011 }
e5a55a89 3012
c38e01b8 3013 if (have_flags)
2469ffd7 3014 memcpy(nla_data(attr), &flags, sizeof(flags));
e5a55a89
JF
3015out:
3016 return err;
3017}
3018
661d2967 3019static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
407af329
VY
3020{
3021 struct net *net = sock_net(skb->sk);
3022 struct ifinfomsg *ifm;
3023 struct net_device *dev;
3024 struct nlattr *br_spec, *attr = NULL;
3025 int rem, err = -EOPNOTSUPP;
4de8b413 3026 u16 flags = 0;
407af329
VY
3027 bool have_flags = false;
3028
3029 if (nlmsg_len(nlh) < sizeof(*ifm))
3030 return -EINVAL;
3031
3032 ifm = nlmsg_data(nlh);
3033 if (ifm->ifi_family != AF_BRIDGE)
3034 return -EPFNOSUPPORT;
3035
3036 dev = __dev_get_by_index(net, ifm->ifi_index);
3037 if (!dev) {
3038 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3039 return -ENODEV;
3040 }
3041
3042 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3043 if (br_spec) {
3044 nla_for_each_nested(attr, br_spec, rem) {
3045 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
6e8d1c55
TG
3046 if (nla_len(attr) < sizeof(flags))
3047 return -EINVAL;
3048
407af329
VY
3049 have_flags = true;
3050 flags = nla_get_u16(attr);
3051 break;
3052 }
3053 }
3054 }
3055
407af329
VY
3056 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3057 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3058
3059 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
3060 err = -EOPNOTSUPP;
3061 goto out;
3062 }
3063
3064 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh);
3065 if (err)
3066 goto out;
3067
3068 flags &= ~BRIDGE_FLAGS_MASTER;
3069 }
3070
3071 if ((flags & BRIDGE_FLAGS_SELF)) {
3072 if (!dev->netdev_ops->ndo_bridge_dellink)
3073 err = -EOPNOTSUPP;
3074 else
3075 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh);
3076
02dba438 3077 if (!err) {
407af329 3078 flags &= ~BRIDGE_FLAGS_SELF;
02dba438
RP
3079
3080 /* Generate event to notify upper layer of bridge
3081 * change
3082 */
3083 err = rtnl_bridge_notify(dev);
3084 }
407af329
VY
3085 }
3086
3087 if (have_flags)
3088 memcpy(nla_data(attr), &flags, sizeof(flags));
407af329
VY
3089out:
3090 return err;
3091}
3092
1da177e4
LT
3093/* Process one rtnetlink message. */
3094
1d00a4eb 3095static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
1da177e4 3096{
3b1e0a65 3097 struct net *net = sock_net(skb->sk);
e2849863 3098 rtnl_doit_func doit;
1da177e4 3099 int sz_idx, kind;
1da177e4
LT
3100 int family;
3101 int type;
2907c35f 3102 int err;
1da177e4 3103
1da177e4 3104 type = nlh->nlmsg_type;
1da177e4 3105 if (type > RTM_MAX)
038890fe 3106 return -EOPNOTSUPP;
1da177e4
LT
3107
3108 type -= RTM_BASE;
3109
3110 /* All the messages must have at least 1 byte length */
573ce260 3111 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
1da177e4
LT
3112 return 0;
3113
573ce260 3114 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
1da177e4
LT
3115 sz_idx = type>>2;
3116 kind = type&3;
3117
90f62cf3 3118 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
1d00a4eb 3119 return -EPERM;
1da177e4 3120
b8f3ab42 3121 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
97c53cac 3122 struct sock *rtnl;
e2849863 3123 rtnl_dumpit_func dumpit;
c7ac8679
GR
3124 rtnl_calcit_func calcit;
3125 u16 min_dump_alloc = 0;
1da177e4 3126
e2849863
TG
3127 dumpit = rtnl_get_dumpit(family, type);
3128 if (dumpit == NULL)
038890fe 3129 return -EOPNOTSUPP;
c7ac8679
GR
3130 calcit = rtnl_get_calcit(family, type);
3131 if (calcit)
115c9b81 3132 min_dump_alloc = calcit(skb, nlh);
9ac4a169 3133
2907c35f 3134 __rtnl_unlock();
97c53cac 3135 rtnl = net->rtnl;
80d326fa
PNA
3136 {
3137 struct netlink_dump_control c = {
3138 .dump = dumpit,
3139 .min_dump_alloc = min_dump_alloc,
3140 };
3141 err = netlink_dump_start(rtnl, skb, nlh, &c);
3142 }
2907c35f
ED
3143 rtnl_lock();
3144 return err;
1da177e4
LT
3145 }
3146
e2849863
TG
3147 doit = rtnl_get_doit(family, type);
3148 if (doit == NULL)
038890fe 3149 return -EOPNOTSUPP;
1da177e4 3150
661d2967 3151 return doit(skb, nlh);
1da177e4
LT
3152}
3153
cd40b7d3 3154static void rtnetlink_rcv(struct sk_buff *skb)
1da177e4 3155{
cd40b7d3
DL
3156 rtnl_lock();
3157 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
3158 rtnl_unlock();
1da177e4
LT
3159}
3160
1da177e4
LT
3161static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
3162{
351638e7 3163 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
e9dc8653 3164
1da177e4 3165 switch (event) {
1da177e4
LT
3166 case NETDEV_UP:
3167 case NETDEV_DOWN:
10de05af 3168 case NETDEV_PRE_UP:
d90a909e
EB
3169 case NETDEV_POST_INIT:
3170 case NETDEV_REGISTER:
1da177e4 3171 case NETDEV_CHANGE:
755d0e77 3172 case NETDEV_PRE_TYPE_CHANGE:
1da177e4 3173 case NETDEV_GOING_DOWN:
a2835763 3174 case NETDEV_UNREGISTER:
0115e8e3 3175 case NETDEV_UNREGISTER_FINAL:
ac3d3f81
AW
3176 case NETDEV_RELEASE:
3177 case NETDEV_JOIN:
1da177e4
LT
3178 break;
3179 default:
7f294054 3180 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
1da177e4
LT
3181 break;
3182 }
3183 return NOTIFY_DONE;
3184}
3185
3186static struct notifier_block rtnetlink_dev_notifier = {
3187 .notifier_call = rtnetlink_event,
3188};
3189
97c53cac 3190
2c8c1e72 3191static int __net_init rtnetlink_net_init(struct net *net)
97c53cac
DL
3192{
3193 struct sock *sk;
a31f2d17
PNA
3194 struct netlink_kernel_cfg cfg = {
3195 .groups = RTNLGRP_MAX,
3196 .input = rtnetlink_rcv,
3197 .cb_mutex = &rtnl_mutex,
9785e10a 3198 .flags = NL_CFG_F_NONROOT_RECV,
a31f2d17
PNA
3199 };
3200
9f00d977 3201 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
97c53cac
DL
3202 if (!sk)
3203 return -ENOMEM;
97c53cac
DL
3204 net->rtnl = sk;
3205 return 0;
3206}
3207
2c8c1e72 3208static void __net_exit rtnetlink_net_exit(struct net *net)
97c53cac 3209{
775516bf
DL
3210 netlink_kernel_release(net->rtnl);
3211 net->rtnl = NULL;
97c53cac
DL
3212}
3213
3214static struct pernet_operations rtnetlink_net_ops = {
3215 .init = rtnetlink_net_init,
3216 .exit = rtnetlink_net_exit,
3217};
3218
1da177e4
LT
3219void __init rtnetlink_init(void)
3220{
97c53cac 3221 if (register_pernet_subsys(&rtnetlink_net_ops))
1da177e4 3222 panic("rtnetlink_init: cannot initialize rtnetlink\n");
97c53cac 3223
1da177e4 3224 register_netdevice_notifier(&rtnetlink_dev_notifier);
340d17fc 3225
c7ac8679
GR
3226 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
3227 rtnl_dump_ifinfo, rtnl_calcit);
3228 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
3229 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
3230 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
687ad8cc 3231
c7ac8679
GR
3232 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
3233 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
77162022
JF
3234
3235 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL);
3236 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL);
3237 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL);
e5a55a89
JF
3238
3239 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL);
407af329 3240 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL);
e5a55a89 3241 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL);
1da177e4
LT
3242}
3243
This page took 1.022657 seconds and 5 git commands to generate.