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