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