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