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