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