]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/core/rtnetlink.c
neighbour: Fixed race condition at tbl->nht
[mirror_ubuntu-bionic-kernel.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>
ebc08a6f 38#include <linux/pci.h>
1da177e4
LT
39
40#include <asm/uaccess.h>
41#include <asm/system.h>
1da177e4
LT
42
43#include <linux/inet.h>
44#include <linux/netdevice.h>
45#include <net/ip.h>
46#include <net/protocol.h>
47#include <net/arp.h>
48#include <net/route.h>
49#include <net/udp.h>
50#include <net/sock.h>
51#include <net/pkt_sched.h>
14c0b97d 52#include <net/fib_rules.h>
e2849863 53#include <net/rtnetlink.h>
30ffee84 54#include <net/net_namespace.h>
1da177e4 55
e0d087af 56struct rtnl_link {
e2849863
TG
57 rtnl_doit_func doit;
58 rtnl_dumpit_func dumpit;
c7ac8679 59 rtnl_calcit_func calcit;
e2849863
TG
60};
61
6756ae4b 62static DEFINE_MUTEX(rtnl_mutex);
c7ac8679 63static u16 min_ifinfo_dump_size;
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
51057f2f 131 return tab ? tab[msgindex].doit : NULL;
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
51057f2f 146 return tab ? tab[msgindex].dumpit : NULL;
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
161 return tab ? tab[msgindex].calcit : NULL;
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
38f7b870
PM
368static size_t rtnl_link_get_size(const struct net_device *dev)
369{
370 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
371 size_t size;
372
373 if (!ops)
374 return 0;
375
369cf77a
TG
376 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
377 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
38f7b870
PM
378
379 if (ops->get_size)
380 /* IFLA_INFO_DATA + nested data */
369cf77a 381 size += nla_total_size(sizeof(struct nlattr)) +
38f7b870
PM
382 ops->get_size(dev);
383
384 if (ops->get_xstats_size)
369cf77a
TG
385 /* IFLA_INFO_XSTATS */
386 size += nla_total_size(ops->get_xstats_size(dev));
38f7b870
PM
387
388 return size;
389}
390
f8ff182c
TG
391static LIST_HEAD(rtnl_af_ops);
392
393static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
394{
395 const struct rtnl_af_ops *ops;
396
397 list_for_each_entry(ops, &rtnl_af_ops, list) {
398 if (ops->family == family)
399 return ops;
400 }
401
402 return NULL;
403}
404
405/**
406 * __rtnl_af_register - Register rtnl_af_ops with rtnetlink.
407 * @ops: struct rtnl_af_ops * to register
408 *
409 * The caller must hold the rtnl_mutex.
410 *
411 * Returns 0 on success or a negative error code.
412 */
413int __rtnl_af_register(struct rtnl_af_ops *ops)
414{
415 list_add_tail(&ops->list, &rtnl_af_ops);
416 return 0;
417}
418EXPORT_SYMBOL_GPL(__rtnl_af_register);
419
420/**
421 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
422 * @ops: struct rtnl_af_ops * to register
423 *
424 * Returns 0 on success or a negative error code.
425 */
426int rtnl_af_register(struct rtnl_af_ops *ops)
427{
428 int err;
429
430 rtnl_lock();
431 err = __rtnl_af_register(ops);
432 rtnl_unlock();
433 return err;
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
38f7b870
PM
480static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
481{
482 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
483 struct nlattr *linkinfo, *data;
484 int err = -EMSGSIZE;
485
486 linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
487 if (linkinfo == NULL)
488 goto out;
489
490 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
491 goto err_cancel_link;
492 if (ops->fill_xstats) {
493 err = ops->fill_xstats(skb, dev);
494 if (err < 0)
495 goto err_cancel_link;
496 }
497 if (ops->fill_info) {
498 data = nla_nest_start(skb, IFLA_INFO_DATA);
499 if (data == NULL)
500 goto err_cancel_link;
501 err = ops->fill_info(skb, dev);
502 if (err < 0)
503 goto err_cancel_data;
504 nla_nest_end(skb, data);
505 }
506
507 nla_nest_end(skb, linkinfo);
508 return 0;
509
510err_cancel_data:
511 nla_nest_cancel(skb, data);
512err_cancel_link:
513 nla_nest_cancel(skb, linkinfo);
514out:
515 return err;
516}
517
db46edc6 518static const int rtm_min[RTM_NR_FAMILIES] =
1da177e4 519{
f90a0a74
TG
520 [RTM_FAM(RTM_NEWLINK)] = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
521 [RTM_FAM(RTM_NEWADDR)] = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
522 [RTM_FAM(RTM_NEWROUTE)] = NLMSG_LENGTH(sizeof(struct rtmsg)),
14c0b97d 523 [RTM_FAM(RTM_NEWRULE)] = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)),
f90a0a74
TG
524 [RTM_FAM(RTM_NEWQDISC)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
525 [RTM_FAM(RTM_NEWTCLASS)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
526 [RTM_FAM(RTM_NEWTFILTER)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
527 [RTM_FAM(RTM_NEWACTION)] = NLMSG_LENGTH(sizeof(struct tcamsg)),
f90a0a74
TG
528 [RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
529 [RTM_FAM(RTM_GETANYCAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
1da177e4
LT
530};
531
db46edc6 532static const int rta_max[RTM_NR_FAMILIES] =
1da177e4 533{
f90a0a74
TG
534 [RTM_FAM(RTM_NEWLINK)] = IFLA_MAX,
535 [RTM_FAM(RTM_NEWADDR)] = IFA_MAX,
536 [RTM_FAM(RTM_NEWROUTE)] = RTA_MAX,
14c0b97d 537 [RTM_FAM(RTM_NEWRULE)] = FRA_MAX,
f90a0a74
TG
538 [RTM_FAM(RTM_NEWQDISC)] = TCA_MAX,
539 [RTM_FAM(RTM_NEWTCLASS)] = TCA_MAX,
540 [RTM_FAM(RTM_NEWTFILTER)] = TCA_MAX,
541 [RTM_FAM(RTM_NEWACTION)] = TCAA_MAX,
1da177e4
LT
542};
543
544void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
545{
546 struct rtattr *rta;
547 int size = RTA_LENGTH(attrlen);
548
e0d087af 549 rta = (struct rtattr *)skb_put(skb, RTA_ALIGN(size));
1da177e4
LT
550 rta->rta_type = attrtype;
551 rta->rta_len = size;
552 memcpy(RTA_DATA(rta), data, attrlen);
b3563c4f 553 memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size);
1da177e4 554}
e0d087af 555EXPORT_SYMBOL(__rta_fill);
1da177e4 556
97c53cac 557int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned group, int echo)
1da177e4 558{
97c53cac 559 struct sock *rtnl = net->rtnl;
1da177e4
LT
560 int err = 0;
561
ac6d439d 562 NETLINK_CB(skb).dst_group = group;
1da177e4
LT
563 if (echo)
564 atomic_inc(&skb->users);
565 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
566 if (echo)
567 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
568 return err;
569}
570
97c53cac 571int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
2942e900 572{
97c53cac
DL
573 struct sock *rtnl = net->rtnl;
574
2942e900
TG
575 return nlmsg_unicast(rtnl, skb, pid);
576}
e0d087af 577EXPORT_SYMBOL(rtnl_unicast);
2942e900 578
1ce85fe4
PNA
579void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
580 struct nlmsghdr *nlh, gfp_t flags)
97676b6b 581{
97c53cac 582 struct sock *rtnl = net->rtnl;
97676b6b
TG
583 int report = 0;
584
585 if (nlh)
586 report = nlmsg_report(nlh);
587
1ce85fe4 588 nlmsg_notify(rtnl, skb, pid, group, report, flags);
97676b6b 589}
e0d087af 590EXPORT_SYMBOL(rtnl_notify);
97676b6b 591
97c53cac 592void rtnl_set_sk_err(struct net *net, u32 group, int error)
97676b6b 593{
97c53cac
DL
594 struct sock *rtnl = net->rtnl;
595
97676b6b
TG
596 netlink_set_err(rtnl, 0, group, error);
597}
e0d087af 598EXPORT_SYMBOL(rtnl_set_sk_err);
97676b6b 599
1da177e4
LT
600int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
601{
2d7202bf
TG
602 struct nlattr *mx;
603 int i, valid = 0;
604
605 mx = nla_nest_start(skb, RTA_METRICS);
606 if (mx == NULL)
607 return -ENOBUFS;
608
609 for (i = 0; i < RTAX_MAX; i++) {
610 if (metrics[i]) {
611 valid++;
612 NLA_PUT_U32(skb, i+1, metrics[i]);
613 }
1da177e4 614 }
1da177e4 615
a57d27fc
DM
616 if (!valid) {
617 nla_nest_cancel(skb, mx);
618 return 0;
619 }
2d7202bf
TG
620
621 return nla_nest_end(skb, mx);
622
623nla_put_failure:
bc3ed28c
TG
624 nla_nest_cancel(skb, mx);
625 return -EMSGSIZE;
1da177e4 626}
e0d087af 627EXPORT_SYMBOL(rtnetlink_put_metrics);
1da177e4 628
e3703b3d
TG
629int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
630 u32 ts, u32 tsage, long expires, u32 error)
631{
632 struct rta_cacheinfo ci = {
633 .rta_lastuse = jiffies_to_clock_t(jiffies - dst->lastuse),
634 .rta_used = dst->__use,
635 .rta_clntref = atomic_read(&(dst->__refcnt)),
636 .rta_error = error,
637 .rta_id = id,
638 .rta_ts = ts,
639 .rta_tsage = tsage,
640 };
641
642 if (expires)
643 ci.rta_expires = jiffies_to_clock_t(expires);
644
645 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
646}
e3703b3d 647EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
1da177e4 648
93b2d4a2 649static void set_operstate(struct net_device *dev, unsigned char transition)
b00055aa
SR
650{
651 unsigned char operstate = dev->operstate;
652
e0d087af 653 switch (transition) {
b00055aa
SR
654 case IF_OPER_UP:
655 if ((operstate == IF_OPER_DORMANT ||
656 operstate == IF_OPER_UNKNOWN) &&
657 !netif_dormant(dev))
658 operstate = IF_OPER_UP;
659 break;
660
661 case IF_OPER_DORMANT:
662 if (operstate == IF_OPER_UP ||
663 operstate == IF_OPER_UNKNOWN)
664 operstate = IF_OPER_DORMANT;
665 break;
3ff50b79 666 }
b00055aa
SR
667
668 if (dev->operstate != operstate) {
669 write_lock_bh(&dev_base_lock);
670 dev->operstate = operstate;
671 write_unlock_bh(&dev_base_lock);
93b2d4a2
DM
672 netdev_state_change(dev);
673 }
b00055aa
SR
674}
675
3729d502
PM
676static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
677 const struct ifinfomsg *ifm)
678{
679 unsigned int flags = ifm->ifi_flags;
680
681 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
682 if (ifm->ifi_change)
683 flags = (flags & ifm->ifi_change) |
684 (dev->flags & ~ifm->ifi_change);
685
686 return flags;
687}
688
b60c5115 689static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
be1f3c2c 690 const struct rtnl_link_stats64 *b)
1da177e4 691{
b60c5115
TG
692 a->rx_packets = b->rx_packets;
693 a->tx_packets = b->tx_packets;
694 a->rx_bytes = b->rx_bytes;
695 a->tx_bytes = b->tx_bytes;
696 a->rx_errors = b->rx_errors;
697 a->tx_errors = b->tx_errors;
698 a->rx_dropped = b->rx_dropped;
699 a->tx_dropped = b->tx_dropped;
700
701 a->multicast = b->multicast;
702 a->collisions = b->collisions;
703
704 a->rx_length_errors = b->rx_length_errors;
705 a->rx_over_errors = b->rx_over_errors;
706 a->rx_crc_errors = b->rx_crc_errors;
707 a->rx_frame_errors = b->rx_frame_errors;
708 a->rx_fifo_errors = b->rx_fifo_errors;
709 a->rx_missed_errors = b->rx_missed_errors;
710
711 a->tx_aborted_errors = b->tx_aborted_errors;
712 a->tx_carrier_errors = b->tx_carrier_errors;
713 a->tx_fifo_errors = b->tx_fifo_errors;
714 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
715 a->tx_window_errors = b->tx_window_errors;
716
717 a->rx_compressed = b->rx_compressed;
718 a->tx_compressed = b->tx_compressed;
10708f37
JE
719}
720
be1f3c2c 721static void copy_rtnl_link_stats64(void *v, const struct rtnl_link_stats64 *b)
10708f37 722{
afdcba37 723 memcpy(v, b, sizeof(*b));
10708f37 724}
1da177e4 725
c02db8c6 726/* All VF info */
ebc08a6f
WM
727static inline int rtnl_vfinfo_size(const struct net_device *dev)
728{
c02db8c6
CW
729 if (dev->dev.parent && dev_is_pci(dev->dev.parent)) {
730
731 int num_vfs = dev_num_vf(dev->dev.parent);
045de01a
SF
732 size_t size = nla_total_size(sizeof(struct nlattr));
733 size += nla_total_size(num_vfs * sizeof(struct nlattr));
734 size += num_vfs *
735 (nla_total_size(sizeof(struct ifla_vf_mac)) +
736 nla_total_size(sizeof(struct ifla_vf_vlan)) +
5f8444a3
GR
737 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
738 nla_total_size(sizeof(struct ifla_vf_spoofchk)));
c02db8c6
CW
739 return size;
740 } else
ebc08a6f
WM
741 return 0;
742}
743
57b61080
SF
744static size_t rtnl_port_size(const struct net_device *dev)
745{
746 size_t port_size = nla_total_size(4) /* PORT_VF */
747 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
748 + nla_total_size(sizeof(struct ifla_port_vsi))
749 /* PORT_VSI_TYPE */
750 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
751 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
752 + nla_total_size(1) /* PROT_VDP_REQUEST */
753 + nla_total_size(2); /* PORT_VDP_RESPONSE */
754 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
755 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
756 + port_size;
757 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
758 + port_size;
759
760 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent)
761 return 0;
762 if (dev_num_vf(dev->dev.parent))
763 return port_self_size + vf_ports_size +
764 vf_port_size * dev_num_vf(dev->dev.parent);
765 else
766 return port_self_size;
767}
768
9e34a5b5 769static noinline size_t if_nlmsg_size(const struct net_device *dev)
339bf98f
TG
770{
771 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
772 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
0b815a1a 773 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
339bf98f
TG
774 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
775 + nla_total_size(sizeof(struct rtnl_link_ifmap))
776 + nla_total_size(sizeof(struct rtnl_link_stats))
adcfe196 777 + nla_total_size(sizeof(struct rtnl_link_stats64))
339bf98f
TG
778 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
779 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
780 + nla_total_size(4) /* IFLA_TXQLEN */
781 + nla_total_size(4) /* IFLA_WEIGHT */
782 + nla_total_size(4) /* IFLA_MTU */
783 + nla_total_size(4) /* IFLA_LINK */
784 + nla_total_size(4) /* IFLA_MASTER */
785 + nla_total_size(1) /* IFLA_OPERSTATE */
38f7b870 786 + nla_total_size(1) /* IFLA_LINKMODE */
ebc08a6f 787 + nla_total_size(4) /* IFLA_NUM_VF */
c02db8c6 788 + rtnl_vfinfo_size(dev) /* IFLA_VFINFO_LIST */
57b61080 789 + rtnl_port_size(dev) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
f8ff182c
TG
790 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
791 + rtnl_link_get_af_size(dev); /* IFLA_AF_SPEC */
339bf98f
TG
792}
793
57b61080
SF
794static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
795{
796 struct nlattr *vf_ports;
797 struct nlattr *vf_port;
798 int vf;
799 int err;
800
801 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
802 if (!vf_ports)
803 return -EMSGSIZE;
804
805 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
806 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
8ca94183
SF
807 if (!vf_port)
808 goto nla_put_failure;
57b61080
SF
809 NLA_PUT_U32(skb, IFLA_PORT_VF, vf);
810 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
8ca94183
SF
811 if (err == -EMSGSIZE)
812 goto nla_put_failure;
57b61080 813 if (err) {
57b61080
SF
814 nla_nest_cancel(skb, vf_port);
815 continue;
816 }
817 nla_nest_end(skb, vf_port);
818 }
819
820 nla_nest_end(skb, vf_ports);
821
822 return 0;
8ca94183
SF
823
824nla_put_failure:
825 nla_nest_cancel(skb, vf_ports);
826 return -EMSGSIZE;
57b61080
SF
827}
828
829static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
830{
831 struct nlattr *port_self;
832 int err;
833
834 port_self = nla_nest_start(skb, IFLA_PORT_SELF);
835 if (!port_self)
836 return -EMSGSIZE;
837
838 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
839 if (err) {
840 nla_nest_cancel(skb, port_self);
8ca94183 841 return (err == -EMSGSIZE) ? err : 0;
57b61080
SF
842 }
843
844 nla_nest_end(skb, port_self);
845
846 return 0;
847}
848
849static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev)
850{
851 int err;
852
853 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent)
854 return 0;
855
856 err = rtnl_port_self_fill(skb, dev);
857 if (err)
858 return err;
859
860 if (dev_num_vf(dev->dev.parent)) {
861 err = rtnl_vf_ports_fill(skb, dev);
862 if (err)
863 return err;
864 }
865
866 return 0;
867}
868
b60c5115 869static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
575c3e2a
PM
870 int type, u32 pid, u32 seq, u32 change,
871 unsigned int flags)
b60c5115
TG
872{
873 struct ifinfomsg *ifm;
874 struct nlmsghdr *nlh;
28172739 875 struct rtnl_link_stats64 temp;
be1f3c2c 876 const struct rtnl_link_stats64 *stats;
f8ff182c
TG
877 struct nlattr *attr, *af_spec;
878 struct rtnl_af_ops *af_ops;
1da177e4 879
2907c35f 880 ASSERT_RTNL();
b60c5115
TG
881 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
882 if (nlh == NULL)
26932566 883 return -EMSGSIZE;
1da177e4 884
b60c5115
TG
885 ifm = nlmsg_data(nlh);
886 ifm->ifi_family = AF_UNSPEC;
887 ifm->__ifi_pad = 0;
888 ifm->ifi_type = dev->type;
889 ifm->ifi_index = dev->ifindex;
890 ifm->ifi_flags = dev_get_flags(dev);
891 ifm->ifi_change = change;
892
893 NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
894 NLA_PUT_U32(skb, IFLA_TXQLEN, dev->tx_queue_len);
b60c5115
TG
895 NLA_PUT_U8(skb, IFLA_OPERSTATE,
896 netif_running(dev) ? dev->operstate : IF_OPER_DOWN);
897 NLA_PUT_U8(skb, IFLA_LINKMODE, dev->link_mode);
898 NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
cbda10fa 899 NLA_PUT_U32(skb, IFLA_GROUP, dev->group);
b60c5115
TG
900
901 if (dev->ifindex != dev->iflink)
902 NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
903
904 if (dev->master)
905 NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex);
1da177e4 906
af356afa
PM
907 if (dev->qdisc)
908 NLA_PUT_STRING(skb, IFLA_QDISC, dev->qdisc->ops->id);
b00055aa 909
0b815a1a
SH
910 if (dev->ifalias)
911 NLA_PUT_STRING(skb, IFLA_IFALIAS, dev->ifalias);
912
1da177e4
LT
913 if (1) {
914 struct rtnl_link_ifmap map = {
915 .mem_start = dev->mem_start,
916 .mem_end = dev->mem_end,
917 .base_addr = dev->base_addr,
918 .irq = dev->irq,
919 .dma = dev->dma,
920 .port = dev->if_port,
921 };
b60c5115 922 NLA_PUT(skb, IFLA_MAP, sizeof(map), &map);
1da177e4
LT
923 }
924
925 if (dev->addr_len) {
b60c5115
TG
926 NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
927 NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
1da177e4
LT
928 }
929
96e74088
PE
930 attr = nla_reserve(skb, IFLA_STATS,
931 sizeof(struct rtnl_link_stats));
932 if (attr == NULL)
933 goto nla_put_failure;
b60c5115 934
28172739 935 stats = dev_get_stats(dev, &temp);
96e74088 936 copy_rtnl_link_stats(nla_data(attr), stats);
1da177e4 937
10708f37
JE
938 attr = nla_reserve(skb, IFLA_STATS64,
939 sizeof(struct rtnl_link_stats64));
940 if (attr == NULL)
941 goto nla_put_failure;
10708f37
JE
942 copy_rtnl_link_stats64(nla_data(attr), stats);
943
57b61080
SF
944 if (dev->dev.parent)
945 NLA_PUT_U32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent));
946
ebc08a6f
WM
947 if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent) {
948 int i;
ebc08a6f 949
c02db8c6
CW
950 struct nlattr *vfinfo, *vf;
951 int num_vfs = dev_num_vf(dev->dev.parent);
952
c02db8c6
CW
953 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
954 if (!vfinfo)
955 goto nla_put_failure;
956 for (i = 0; i < num_vfs; i++) {
957 struct ifla_vf_info ivi;
958 struct ifla_vf_mac vf_mac;
959 struct ifla_vf_vlan vf_vlan;
960 struct ifla_vf_tx_rate vf_tx_rate;
5f8444a3
GR
961 struct ifla_vf_spoofchk vf_spoofchk;
962
963 /*
964 * Not all SR-IOV capable drivers support the
965 * spoofcheck query. Preset to -1 so the user
966 * space tool can detect that the driver didn't
967 * report anything.
968 */
969 ivi.spoofchk = -1;
ebc08a6f
WM
970 if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi))
971 break;
5f8444a3
GR
972 vf_mac.vf =
973 vf_vlan.vf =
974 vf_tx_rate.vf =
975 vf_spoofchk.vf = ivi.vf;
976
c02db8c6
CW
977 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
978 vf_vlan.vlan = ivi.vlan;
979 vf_vlan.qos = ivi.qos;
980 vf_tx_rate.rate = ivi.tx_rate;
5f8444a3 981 vf_spoofchk.setting = ivi.spoofchk;
c02db8c6
CW
982 vf = nla_nest_start(skb, IFLA_VF_INFO);
983 if (!vf) {
984 nla_nest_cancel(skb, vfinfo);
985 goto nla_put_failure;
986 }
987 NLA_PUT(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac);
988 NLA_PUT(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan);
5f8444a3
GR
989 NLA_PUT(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
990 &vf_tx_rate);
991 NLA_PUT(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
992 &vf_spoofchk);
c02db8c6 993 nla_nest_end(skb, vf);
ebc08a6f 994 }
c02db8c6 995 nla_nest_end(skb, vfinfo);
ebc08a6f 996 }
57b61080
SF
997
998 if (rtnl_port_fill(skb, dev))
999 goto nla_put_failure;
1000
38f7b870
PM
1001 if (dev->rtnl_link_ops) {
1002 if (rtnl_link_fill(skb, dev) < 0)
1003 goto nla_put_failure;
1004 }
1005
f8ff182c
TG
1006 if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1007 goto nla_put_failure;
1008
1009 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1010 if (af_ops->fill_link_af) {
1011 struct nlattr *af;
1012 int err;
1013
1014 if (!(af = nla_nest_start(skb, af_ops->family)))
1015 goto nla_put_failure;
1016
1017 err = af_ops->fill_link_af(skb, dev);
1018
1019 /*
1020 * Caller may return ENODATA to indicate that there
1021 * was no data to be dumped. This is not an error, it
1022 * means we should trim the attribute header and
1023 * continue.
1024 */
1025 if (err == -ENODATA)
1026 nla_nest_cancel(skb, af);
1027 else if (err < 0)
1028 goto nla_put_failure;
1029
1030 nla_nest_end(skb, af);
1031 }
1032 }
1033
1034 nla_nest_end(skb, af_spec);
1035
b60c5115
TG
1036 return nlmsg_end(skb, nlh);
1037
1038nla_put_failure:
26932566
PM
1039 nlmsg_cancel(skb, nlh);
1040 return -EMSGSIZE;
1da177e4
LT
1041}
1042
b60c5115 1043static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
1da177e4 1044{
3b1e0a65 1045 struct net *net = sock_net(skb->sk);
7c28bd0b
ED
1046 int h, s_h;
1047 int idx = 0, s_idx;
1da177e4 1048 struct net_device *dev;
7c28bd0b
ED
1049 struct hlist_head *head;
1050 struct hlist_node *node;
1051
1052 s_h = cb->args[0];
1053 s_idx = cb->args[1];
1054
e67f88dd 1055 rcu_read_lock();
4e985ada
TG
1056 cb->seq = net->dev_base_seq;
1057
7c28bd0b
ED
1058 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1059 idx = 0;
1060 head = &net->dev_index_head[h];
e67f88dd 1061 hlist_for_each_entry_rcu(dev, node, head, index_hlist) {
7c28bd0b
ED
1062 if (idx < s_idx)
1063 goto cont;
1064 if (rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
1065 NETLINK_CB(cb->skb).pid,
1066 cb->nlh->nlmsg_seq, 0,
1067 NLM_F_MULTI) <= 0)
1068 goto out;
4e985ada
TG
1069
1070 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
7562f876 1071cont:
7c28bd0b
ED
1072 idx++;
1073 }
1da177e4 1074 }
7c28bd0b 1075out:
e67f88dd 1076 rcu_read_unlock();
7c28bd0b
ED
1077 cb->args[1] = idx;
1078 cb->args[0] = h;
1da177e4
LT
1079
1080 return skb->len;
1081}
1082
e7199288 1083const struct nla_policy ifla_policy[IFLA_MAX+1] = {
5176f91e 1084 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
38f7b870
PM
1085 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1086 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
5176f91e 1087 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
da5e0494 1088 [IFLA_MTU] = { .type = NLA_U32 },
76e87306 1089 [IFLA_LINK] = { .type = NLA_U32 },
fbaec0ea 1090 [IFLA_MASTER] = { .type = NLA_U32 },
da5e0494
TG
1091 [IFLA_TXQLEN] = { .type = NLA_U32 },
1092 [IFLA_WEIGHT] = { .type = NLA_U32 },
1093 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1094 [IFLA_LINKMODE] = { .type = NLA_U8 },
76e87306 1095 [IFLA_LINKINFO] = { .type = NLA_NESTED },
d8a5ec67 1096 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
f0630529 1097 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
0b815a1a 1098 [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 },
c02db8c6 1099 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
57b61080
SF
1100 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1101 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
f8ff182c 1102 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
da5e0494 1103};
e0d087af 1104EXPORT_SYMBOL(ifla_policy);
da5e0494 1105
38f7b870
PM
1106static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1107 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1108 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1109};
1110
c02db8c6
CW
1111static const struct nla_policy ifla_vfinfo_policy[IFLA_VF_INFO_MAX+1] = {
1112 [IFLA_VF_INFO] = { .type = NLA_NESTED },
1113};
1114
1115static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1116 [IFLA_VF_MAC] = { .type = NLA_BINARY,
1117 .len = sizeof(struct ifla_vf_mac) },
1118 [IFLA_VF_VLAN] = { .type = NLA_BINARY,
1119 .len = sizeof(struct ifla_vf_vlan) },
1120 [IFLA_VF_TX_RATE] = { .type = NLA_BINARY,
1121 .len = sizeof(struct ifla_vf_tx_rate) },
1122};
1123
57b61080
SF
1124static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1125 [IFLA_PORT_VF] = { .type = NLA_U32 },
1126 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1127 .len = PORT_PROFILE_MAX },
1128 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1129 .len = sizeof(struct ifla_port_vsi)},
1130 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1131 .len = PORT_UUID_MAX },
1132 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1133 .len = PORT_UUID_MAX },
1134 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1135 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
1136};
1137
81adee47
EB
1138struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1139{
1140 struct net *net;
1141 /* Examine the link attributes and figure out which
1142 * network namespace we are talking about.
1143 */
1144 if (tb[IFLA_NET_NS_PID])
1145 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
f0630529
EB
1146 else if (tb[IFLA_NET_NS_FD])
1147 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
81adee47
EB
1148 else
1149 net = get_net(src_net);
1150 return net;
1151}
1152EXPORT_SYMBOL(rtnl_link_get_net);
1153
1840bb13
TG
1154static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1155{
1156 if (dev) {
1157 if (tb[IFLA_ADDRESS] &&
1158 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1159 return -EINVAL;
1160
1161 if (tb[IFLA_BROADCAST] &&
1162 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1163 return -EINVAL;
1164 }
1165
cf7afbfe
TG
1166 if (tb[IFLA_AF_SPEC]) {
1167 struct nlattr *af;
1168 int rem, err;
1169
1170 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1171 const struct rtnl_af_ops *af_ops;
1172
1173 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1174 return -EAFNOSUPPORT;
1175
1176 if (!af_ops->set_link_af)
1177 return -EOPNOTSUPP;
1178
1179 if (af_ops->validate_link_af) {
6d3a9a68 1180 err = af_ops->validate_link_af(dev, af);
cf7afbfe
TG
1181 if (err < 0)
1182 return err;
1183 }
1184 }
1185 }
1186
1840bb13
TG
1187 return 0;
1188}
1189
c02db8c6
CW
1190static int do_setvfinfo(struct net_device *dev, struct nlattr *attr)
1191{
1192 int rem, err = -EINVAL;
1193 struct nlattr *vf;
1194 const struct net_device_ops *ops = dev->netdev_ops;
1195
1196 nla_for_each_nested(vf, attr, rem) {
1197 switch (nla_type(vf)) {
1198 case IFLA_VF_MAC: {
1199 struct ifla_vf_mac *ivm;
1200 ivm = nla_data(vf);
1201 err = -EOPNOTSUPP;
1202 if (ops->ndo_set_vf_mac)
1203 err = ops->ndo_set_vf_mac(dev, ivm->vf,
1204 ivm->mac);
1205 break;
1206 }
1207 case IFLA_VF_VLAN: {
1208 struct ifla_vf_vlan *ivv;
1209 ivv = nla_data(vf);
1210 err = -EOPNOTSUPP;
1211 if (ops->ndo_set_vf_vlan)
1212 err = ops->ndo_set_vf_vlan(dev, ivv->vf,
1213 ivv->vlan,
1214 ivv->qos);
1215 break;
1216 }
1217 case IFLA_VF_TX_RATE: {
1218 struct ifla_vf_tx_rate *ivt;
1219 ivt = nla_data(vf);
1220 err = -EOPNOTSUPP;
1221 if (ops->ndo_set_vf_tx_rate)
1222 err = ops->ndo_set_vf_tx_rate(dev, ivt->vf,
1223 ivt->rate);
1224 break;
1225 }
5f8444a3
GR
1226 case IFLA_VF_SPOOFCHK: {
1227 struct ifla_vf_spoofchk *ivs;
1228 ivs = nla_data(vf);
1229 err = -EOPNOTSUPP;
1230 if (ops->ndo_set_vf_spoofchk)
1231 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
1232 ivs->setting);
1233 break;
1234 }
c02db8c6
CW
1235 default:
1236 err = -EINVAL;
1237 break;
1238 }
1239 if (err)
1240 break;
1241 }
1242 return err;
1243}
1244
fbaec0ea
JP
1245static int do_set_master(struct net_device *dev, int ifindex)
1246{
1247 struct net_device *master_dev;
1248 const struct net_device_ops *ops;
1249 int err;
1250
1251 if (dev->master) {
1252 if (dev->master->ifindex == ifindex)
1253 return 0;
1254 ops = dev->master->netdev_ops;
1255 if (ops->ndo_del_slave) {
1256 err = ops->ndo_del_slave(dev->master, dev);
1257 if (err)
1258 return err;
1259 } else {
1260 return -EOPNOTSUPP;
1261 }
1262 }
1263
1264 if (ifindex) {
1265 master_dev = __dev_get_by_index(dev_net(dev), ifindex);
1266 if (!master_dev)
1267 return -EINVAL;
1268 ops = master_dev->netdev_ops;
1269 if (ops->ndo_add_slave) {
1270 err = ops->ndo_add_slave(master_dev, dev);
1271 if (err)
1272 return err;
1273 } else {
1274 return -EOPNOTSUPP;
1275 }
1276 }
1277 return 0;
1278}
1279
0157f60c 1280static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm,
38f7b870 1281 struct nlattr **tb, char *ifname, int modified)
1da177e4 1282{
d314774c 1283 const struct net_device_ops *ops = dev->netdev_ops;
38f7b870 1284 int send_addr_notify = 0;
0157f60c 1285 int err;
1da177e4 1286
f0630529 1287 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
81adee47 1288 struct net *net = rtnl_link_get_net(dev_net(dev), tb);
d8a5ec67
EB
1289 if (IS_ERR(net)) {
1290 err = PTR_ERR(net);
1291 goto errout;
1292 }
1293 err = dev_change_net_namespace(dev, net, ifname);
1294 put_net(net);
1295 if (err)
1296 goto errout;
1297 modified = 1;
1298 }
1299
da5e0494 1300 if (tb[IFLA_MAP]) {
1da177e4
LT
1301 struct rtnl_link_ifmap *u_map;
1302 struct ifmap k_map;
1303
d314774c 1304 if (!ops->ndo_set_config) {
1da177e4 1305 err = -EOPNOTSUPP;
0157f60c 1306 goto errout;
1da177e4
LT
1307 }
1308
1309 if (!netif_device_present(dev)) {
1310 err = -ENODEV;
0157f60c 1311 goto errout;
1da177e4 1312 }
1da177e4 1313
da5e0494 1314 u_map = nla_data(tb[IFLA_MAP]);
1da177e4
LT
1315 k_map.mem_start = (unsigned long) u_map->mem_start;
1316 k_map.mem_end = (unsigned long) u_map->mem_end;
1317 k_map.base_addr = (unsigned short) u_map->base_addr;
1318 k_map.irq = (unsigned char) u_map->irq;
1319 k_map.dma = (unsigned char) u_map->dma;
1320 k_map.port = (unsigned char) u_map->port;
1321
d314774c 1322 err = ops->ndo_set_config(dev, &k_map);
da5e0494 1323 if (err < 0)
0157f60c 1324 goto errout;
1da177e4 1325
da5e0494 1326 modified = 1;
1da177e4
LT
1327 }
1328
da5e0494 1329 if (tb[IFLA_ADDRESS]) {
70f8e78e
DM
1330 struct sockaddr *sa;
1331 int len;
1332
d314774c 1333 if (!ops->ndo_set_mac_address) {
1da177e4 1334 err = -EOPNOTSUPP;
0157f60c 1335 goto errout;
1da177e4 1336 }
da5e0494 1337
1da177e4
LT
1338 if (!netif_device_present(dev)) {
1339 err = -ENODEV;
0157f60c 1340 goto errout;
1da177e4 1341 }
1da177e4 1342
70f8e78e
DM
1343 len = sizeof(sa_family_t) + dev->addr_len;
1344 sa = kmalloc(len, GFP_KERNEL);
1345 if (!sa) {
1346 err = -ENOMEM;
0157f60c 1347 goto errout;
70f8e78e
DM
1348 }
1349 sa->sa_family = dev->type;
da5e0494 1350 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
70f8e78e 1351 dev->addr_len);
d314774c 1352 err = ops->ndo_set_mac_address(dev, sa);
70f8e78e 1353 kfree(sa);
1da177e4 1354 if (err)
0157f60c 1355 goto errout;
1da177e4 1356 send_addr_notify = 1;
da5e0494 1357 modified = 1;
1da177e4
LT
1358 }
1359
da5e0494
TG
1360 if (tb[IFLA_MTU]) {
1361 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1362 if (err < 0)
0157f60c 1363 goto errout;
da5e0494 1364 modified = 1;
1da177e4
LT
1365 }
1366
cbda10fa
VD
1367 if (tb[IFLA_GROUP]) {
1368 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1369 modified = 1;
1370 }
1371
da5e0494
TG
1372 /*
1373 * Interface selected by interface index but interface
1374 * name provided implies that a name change has been
1375 * requested.
1376 */
51055be8 1377 if (ifm->ifi_index > 0 && ifname[0]) {
da5e0494
TG
1378 err = dev_change_name(dev, ifname);
1379 if (err < 0)
0157f60c 1380 goto errout;
da5e0494 1381 modified = 1;
1da177e4
LT
1382 }
1383
0b815a1a
SH
1384 if (tb[IFLA_IFALIAS]) {
1385 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
1386 nla_len(tb[IFLA_IFALIAS]));
1387 if (err < 0)
1388 goto errout;
1389 modified = 1;
1390 }
1391
da5e0494
TG
1392 if (tb[IFLA_BROADCAST]) {
1393 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
1394 send_addr_notify = 1;
1da177e4
LT
1395 }
1396
83b496e9 1397 if (ifm->ifi_flags || ifm->ifi_change) {
3729d502 1398 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
5f9021cf
JB
1399 if (err < 0)
1400 goto errout;
83b496e9 1401 }
1da177e4 1402
fbaec0ea
JP
1403 if (tb[IFLA_MASTER]) {
1404 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
1405 if (err)
1406 goto errout;
1407 modified = 1;
1408 }
1409
93b2d4a2
DM
1410 if (tb[IFLA_TXQLEN])
1411 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
b00055aa 1412
da5e0494 1413 if (tb[IFLA_OPERSTATE])
93b2d4a2 1414 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
b00055aa 1415
da5e0494 1416 if (tb[IFLA_LINKMODE]) {
93b2d4a2
DM
1417 write_lock_bh(&dev_base_lock);
1418 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
1419 write_unlock_bh(&dev_base_lock);
b00055aa
SR
1420 }
1421
c02db8c6
CW
1422 if (tb[IFLA_VFINFO_LIST]) {
1423 struct nlattr *attr;
1424 int rem;
1425 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
253683bb
DH
1426 if (nla_type(attr) != IFLA_VF_INFO) {
1427 err = -EINVAL;
c02db8c6 1428 goto errout;
253683bb 1429 }
c02db8c6
CW
1430 err = do_setvfinfo(dev, attr);
1431 if (err < 0)
1432 goto errout;
1433 modified = 1;
1434 }
ebc08a6f 1435 }
1da177e4
LT
1436 err = 0;
1437
57b61080
SF
1438 if (tb[IFLA_VF_PORTS]) {
1439 struct nlattr *port[IFLA_PORT_MAX+1];
1440 struct nlattr *attr;
1441 int vf;
1442 int rem;
1443
1444 err = -EOPNOTSUPP;
1445 if (!ops->ndo_set_vf_port)
1446 goto errout;
1447
1448 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
1449 if (nla_type(attr) != IFLA_VF_PORT)
1450 continue;
1451 err = nla_parse_nested(port, IFLA_PORT_MAX,
1452 attr, ifla_port_policy);
1453 if (err < 0)
1454 goto errout;
1455 if (!port[IFLA_PORT_VF]) {
1456 err = -EOPNOTSUPP;
1457 goto errout;
1458 }
1459 vf = nla_get_u32(port[IFLA_PORT_VF]);
1460 err = ops->ndo_set_vf_port(dev, vf, port);
1461 if (err < 0)
1462 goto errout;
1463 modified = 1;
1464 }
1465 }
1466 err = 0;
1467
1468 if (tb[IFLA_PORT_SELF]) {
1469 struct nlattr *port[IFLA_PORT_MAX+1];
1470
1471 err = nla_parse_nested(port, IFLA_PORT_MAX,
1472 tb[IFLA_PORT_SELF], ifla_port_policy);
1473 if (err < 0)
1474 goto errout;
1475
1476 err = -EOPNOTSUPP;
1477 if (ops->ndo_set_vf_port)
1478 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
1479 if (err < 0)
1480 goto errout;
1481 modified = 1;
1482 }
f8ff182c
TG
1483
1484 if (tb[IFLA_AF_SPEC]) {
1485 struct nlattr *af;
1486 int rem;
1487
1488 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1489 const struct rtnl_af_ops *af_ops;
1490
1491 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
cf7afbfe 1492 BUG();
f8ff182c 1493
cf7afbfe 1494 err = af_ops->set_link_af(dev, af);
f8ff182c
TG
1495 if (err < 0)
1496 goto errout;
1497
1498 modified = 1;
1499 }
1500 }
57b61080
SF
1501 err = 0;
1502
0157f60c 1503errout:
da5e0494
TG
1504 if (err < 0 && modified && net_ratelimit())
1505 printk(KERN_WARNING "A link change request failed with "
25985edc 1506 "some changes committed already. Interface %s may "
da5e0494
TG
1507 "have been left with an inconsistent configuration, "
1508 "please check.\n", dev->name);
1509
1da177e4
LT
1510 if (send_addr_notify)
1511 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
f18da145
SG
1512 min_ifinfo_dump_size = max_t(u16, if_nlmsg_size(dev),
1513 min_ifinfo_dump_size);
1514
0157f60c
PM
1515 return err;
1516}
1da177e4 1517
0157f60c
PM
1518static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1519{
3b1e0a65 1520 struct net *net = sock_net(skb->sk);
0157f60c
PM
1521 struct ifinfomsg *ifm;
1522 struct net_device *dev;
1523 int err;
1524 struct nlattr *tb[IFLA_MAX+1];
1525 char ifname[IFNAMSIZ];
1526
1527 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1528 if (err < 0)
1529 goto errout;
1530
1531 if (tb[IFLA_IFNAME])
1532 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1533 else
1534 ifname[0] = '\0';
1535
1536 err = -EINVAL;
1537 ifm = nlmsg_data(nlh);
1538 if (ifm->ifi_index > 0)
a3d12891 1539 dev = __dev_get_by_index(net, ifm->ifi_index);
0157f60c 1540 else if (tb[IFLA_IFNAME])
a3d12891 1541 dev = __dev_get_by_name(net, ifname);
0157f60c
PM
1542 else
1543 goto errout;
1544
1545 if (dev == NULL) {
1546 err = -ENODEV;
1547 goto errout;
1548 }
1549
e0d087af
ED
1550 err = validate_linkmsg(dev, tb);
1551 if (err < 0)
a3d12891 1552 goto errout;
0157f60c 1553
38f7b870 1554 err = do_setlink(dev, ifm, tb, ifname, 0);
da5e0494 1555errout:
1da177e4
LT
1556 return err;
1557}
1558
38f7b870
PM
1559static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1560{
3b1e0a65 1561 struct net *net = sock_net(skb->sk);
38f7b870
PM
1562 const struct rtnl_link_ops *ops;
1563 struct net_device *dev;
1564 struct ifinfomsg *ifm;
1565 char ifname[IFNAMSIZ];
1566 struct nlattr *tb[IFLA_MAX+1];
1567 int err;
226bd341 1568 LIST_HEAD(list_kill);
38f7b870
PM
1569
1570 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1571 if (err < 0)
1572 return err;
1573
1574 if (tb[IFLA_IFNAME])
1575 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1576
1577 ifm = nlmsg_data(nlh);
1578 if (ifm->ifi_index > 0)
881d966b 1579 dev = __dev_get_by_index(net, ifm->ifi_index);
38f7b870 1580 else if (tb[IFLA_IFNAME])
881d966b 1581 dev = __dev_get_by_name(net, ifname);
38f7b870
PM
1582 else
1583 return -EINVAL;
1584
1585 if (!dev)
1586 return -ENODEV;
1587
1588 ops = dev->rtnl_link_ops;
1589 if (!ops)
1590 return -EOPNOTSUPP;
1591
226bd341
ED
1592 ops->dellink(dev, &list_kill);
1593 unregister_netdevice_many(&list_kill);
1594 list_del(&list_kill);
38f7b870
PM
1595 return 0;
1596}
1597
3729d502
PM
1598int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
1599{
1600 unsigned int old_flags;
1601 int err;
1602
1603 old_flags = dev->flags;
1604 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
1605 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
1606 if (err < 0)
1607 return err;
1608 }
1609
1610 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
1611 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
1612
1613 __dev_notify_flags(dev, old_flags);
1614 return 0;
1615}
1616EXPORT_SYMBOL(rtnl_configure_link);
1617
81adee47
EB
1618struct net_device *rtnl_create_link(struct net *src_net, struct net *net,
1619 char *ifname, const struct rtnl_link_ops *ops, struct nlattr *tb[])
e7199288
PE
1620{
1621 int err;
1622 struct net_device *dev;
2e59af3d
ED
1623 unsigned int num_queues = 1;
1624 unsigned int real_num_queues = 1;
e7199288 1625
2e59af3d 1626 if (ops->get_tx_queues) {
81adee47 1627 err = ops->get_tx_queues(src_net, tb, &num_queues,
e0d087af 1628 &real_num_queues);
2e59af3d
ED
1629 if (err)
1630 goto err;
1631 }
e7199288 1632 err = -ENOMEM;
2e59af3d 1633 dev = alloc_netdev_mq(ops->priv_size, ifname, ops->setup, num_queues);
e7199288
PE
1634 if (!dev)
1635 goto err;
1636
81adee47
EB
1637 dev_net_set(dev, net);
1638 dev->rtnl_link_ops = ops;
3729d502 1639 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
81adee47 1640
e7199288
PE
1641 if (tb[IFLA_MTU])
1642 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
1643 if (tb[IFLA_ADDRESS])
1644 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
1645 nla_len(tb[IFLA_ADDRESS]));
1646 if (tb[IFLA_BROADCAST])
1647 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
1648 nla_len(tb[IFLA_BROADCAST]));
1649 if (tb[IFLA_TXQLEN])
1650 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1651 if (tb[IFLA_OPERSTATE])
93b2d4a2 1652 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
e7199288
PE
1653 if (tb[IFLA_LINKMODE])
1654 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
ffa934f1
PM
1655 if (tb[IFLA_GROUP])
1656 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
e7199288
PE
1657
1658 return dev;
1659
e7199288
PE
1660err:
1661 return ERR_PTR(err);
1662}
e0d087af 1663EXPORT_SYMBOL(rtnl_create_link);
e7199288 1664
e7ed828f
VD
1665static int rtnl_group_changelink(struct net *net, int group,
1666 struct ifinfomsg *ifm,
1667 struct nlattr **tb)
1668{
1669 struct net_device *dev;
1670 int err;
1671
1672 for_each_netdev(net, dev) {
1673 if (dev->group == group) {
1674 err = do_setlink(dev, ifm, tb, NULL, 0);
1675 if (err < 0)
1676 return err;
1677 }
1678 }
1679
1680 return 0;
1681}
1682
38f7b870
PM
1683static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1684{
3b1e0a65 1685 struct net *net = sock_net(skb->sk);
38f7b870
PM
1686 const struct rtnl_link_ops *ops;
1687 struct net_device *dev;
1688 struct ifinfomsg *ifm;
1689 char kind[MODULE_NAME_LEN];
1690 char ifname[IFNAMSIZ];
1691 struct nlattr *tb[IFLA_MAX+1];
1692 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
1693 int err;
1694
95a5afca 1695#ifdef CONFIG_MODULES
38f7b870 1696replay:
8072f085 1697#endif
38f7b870
PM
1698 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1699 if (err < 0)
1700 return err;
1701
1702 if (tb[IFLA_IFNAME])
1703 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1704 else
1705 ifname[0] = '\0';
1706
1707 ifm = nlmsg_data(nlh);
1708 if (ifm->ifi_index > 0)
881d966b 1709 dev = __dev_get_by_index(net, ifm->ifi_index);
e7ed828f
VD
1710 else {
1711 if (ifname[0])
1712 dev = __dev_get_by_name(net, ifname);
e7ed828f
VD
1713 else
1714 dev = NULL;
1715 }
38f7b870 1716
e0d087af
ED
1717 err = validate_linkmsg(dev, tb);
1718 if (err < 0)
1840bb13
TG
1719 return err;
1720
38f7b870
PM
1721 if (tb[IFLA_LINKINFO]) {
1722 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
1723 tb[IFLA_LINKINFO], ifla_info_policy);
1724 if (err < 0)
1725 return err;
1726 } else
1727 memset(linkinfo, 0, sizeof(linkinfo));
1728
1729 if (linkinfo[IFLA_INFO_KIND]) {
1730 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
1731 ops = rtnl_link_ops_get(kind);
1732 } else {
1733 kind[0] = '\0';
1734 ops = NULL;
1735 }
1736
1737 if (1) {
1738 struct nlattr *attr[ops ? ops->maxtype + 1 : 0], **data = NULL;
81adee47 1739 struct net *dest_net;
38f7b870
PM
1740
1741 if (ops) {
1742 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
1743 err = nla_parse_nested(attr, ops->maxtype,
1744 linkinfo[IFLA_INFO_DATA],
1745 ops->policy);
1746 if (err < 0)
1747 return err;
1748 data = attr;
1749 }
1750 if (ops->validate) {
1751 err = ops->validate(tb, data);
1752 if (err < 0)
1753 return err;
1754 }
1755 }
1756
1757 if (dev) {
1758 int modified = 0;
1759
1760 if (nlh->nlmsg_flags & NLM_F_EXCL)
1761 return -EEXIST;
1762 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1763 return -EOPNOTSUPP;
1764
1765 if (linkinfo[IFLA_INFO_DATA]) {
1766 if (!ops || ops != dev->rtnl_link_ops ||
1767 !ops->changelink)
1768 return -EOPNOTSUPP;
1769
1770 err = ops->changelink(dev, tb, data);
1771 if (err < 0)
1772 return err;
1773 modified = 1;
1774 }
1775
1776 return do_setlink(dev, ifm, tb, ifname, modified);
1777 }
1778
ffa934f1
PM
1779 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
1780 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
1781 return rtnl_group_changelink(net,
1782 nla_get_u32(tb[IFLA_GROUP]),
1783 ifm, tb);
38f7b870 1784 return -ENODEV;
ffa934f1 1785 }
38f7b870 1786
3729d502 1787 if (ifm->ifi_index)
38f7b870 1788 return -EOPNOTSUPP;
0e06877c 1789 if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
38f7b870
PM
1790 return -EOPNOTSUPP;
1791
1792 if (!ops) {
95a5afca 1793#ifdef CONFIG_MODULES
38f7b870
PM
1794 if (kind[0]) {
1795 __rtnl_unlock();
1796 request_module("rtnl-link-%s", kind);
1797 rtnl_lock();
1798 ops = rtnl_link_ops_get(kind);
1799 if (ops)
1800 goto replay;
1801 }
1802#endif
1803 return -EOPNOTSUPP;
1804 }
1805
1806 if (!ifname[0])
1807 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
e7199288 1808
81adee47 1809 dest_net = rtnl_link_get_net(net, tb);
13ad1774
EB
1810 if (IS_ERR(dest_net))
1811 return PTR_ERR(dest_net);
1812
81adee47 1813 dev = rtnl_create_link(net, dest_net, ifname, ops, tb);
e7199288
PE
1814
1815 if (IS_ERR(dev))
1816 err = PTR_ERR(dev);
1817 else if (ops->newlink)
81adee47 1818 err = ops->newlink(net, dev, tb, data);
2d85cba2
PM
1819 else
1820 err = register_netdevice(dev);
80032cff
DC
1821
1822 if (err < 0 && !IS_ERR(dev))
38f7b870 1823 free_netdev(dev);
80032cff 1824 if (err < 0)
3729d502 1825 goto out;
81adee47 1826
3729d502
PM
1827 err = rtnl_configure_link(dev, ifm);
1828 if (err < 0)
1829 unregister_netdevice(dev);
1830out:
81adee47 1831 put_net(dest_net);
38f7b870
PM
1832 return err;
1833 }
1834}
1835
b60c5115 1836static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
711e2c33 1837{
3b1e0a65 1838 struct net *net = sock_net(skb->sk);
b60c5115 1839 struct ifinfomsg *ifm;
a3d12891 1840 char ifname[IFNAMSIZ];
b60c5115
TG
1841 struct nlattr *tb[IFLA_MAX+1];
1842 struct net_device *dev = NULL;
1843 struct sk_buff *nskb;
339bf98f 1844 int err;
711e2c33 1845
b60c5115
TG
1846 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1847 if (err < 0)
9918f230 1848 return err;
b60c5115 1849
a3d12891
ED
1850 if (tb[IFLA_IFNAME])
1851 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1852
b60c5115 1853 ifm = nlmsg_data(nlh);
a3d12891
ED
1854 if (ifm->ifi_index > 0)
1855 dev = __dev_get_by_index(net, ifm->ifi_index);
1856 else if (tb[IFLA_IFNAME])
1857 dev = __dev_get_by_name(net, ifname);
1858 else
711e2c33 1859 return -EINVAL;
711e2c33 1860
a3d12891
ED
1861 if (dev == NULL)
1862 return -ENODEV;
1863
38f7b870 1864 nskb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL);
a3d12891
ED
1865 if (nskb == NULL)
1866 return -ENOBUFS;
b60c5115 1867
575c3e2a
PM
1868 err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).pid,
1869 nlh->nlmsg_seq, 0, 0);
26932566
PM
1870 if (err < 0) {
1871 /* -EMSGSIZE implies BUG in if_nlmsg_size */
1872 WARN_ON(err == -EMSGSIZE);
1873 kfree_skb(nskb);
a3d12891
ED
1874 } else
1875 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).pid);
711e2c33 1876
b60c5115 1877 return err;
711e2c33 1878}
711e2c33 1879
c7ac8679
GR
1880static u16 rtnl_calcit(struct sk_buff *skb)
1881{
1882 return min_ifinfo_dump_size;
1883}
1884
42bad1da 1885static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
1da177e4
LT
1886{
1887 int idx;
1888 int s_idx = cb->family;
1889
1890 if (s_idx == 0)
1891 s_idx = 1;
25239cee 1892 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
1da177e4
LT
1893 int type = cb->nlh->nlmsg_type-RTM_BASE;
1894 if (idx < s_idx || idx == PF_PACKET)
1895 continue;
e2849863
TG
1896 if (rtnl_msg_handlers[idx] == NULL ||
1897 rtnl_msg_handlers[idx][type].dumpit == NULL)
1da177e4
LT
1898 continue;
1899 if (idx > s_idx)
1900 memset(&cb->args[0], 0, sizeof(cb->args));
e2849863 1901 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
1da177e4
LT
1902 break;
1903 }
1904 cb->family = idx;
1905
1906 return skb->len;
1907}
1908
1909void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
1910{
c346dca1 1911 struct net *net = dev_net(dev);
1da177e4 1912 struct sk_buff *skb;
0ec6d3f4 1913 int err = -ENOBUFS;
c7ac8679 1914 size_t if_info_size;
1da177e4 1915
c7ac8679 1916 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev)), GFP_KERNEL);
0ec6d3f4
TG
1917 if (skb == NULL)
1918 goto errout;
1da177e4 1919
c7ac8679
GR
1920 min_ifinfo_dump_size = max_t(u16, if_info_size, min_ifinfo_dump_size);
1921
575c3e2a 1922 err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0);
26932566
PM
1923 if (err < 0) {
1924 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
1925 WARN_ON(err == -EMSGSIZE);
1926 kfree_skb(skb);
1927 goto errout;
1928 }
1ce85fe4
PNA
1929 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
1930 return;
0ec6d3f4
TG
1931errout:
1932 if (err < 0)
4b3da706 1933 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
1da177e4
LT
1934}
1935
1da177e4
LT
1936/* Protected by RTNL sempahore. */
1937static struct rtattr **rta_buf;
1938static int rtattr_max;
1939
1940/* Process one rtnetlink message. */
1941
1d00a4eb 1942static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
1da177e4 1943{
3b1e0a65 1944 struct net *net = sock_net(skb->sk);
e2849863 1945 rtnl_doit_func doit;
1da177e4
LT
1946 int sz_idx, kind;
1947 int min_len;
1948 int family;
1949 int type;
2907c35f 1950 int err;
1da177e4 1951
1da177e4 1952 type = nlh->nlmsg_type;
1da177e4 1953 if (type > RTM_MAX)
038890fe 1954 return -EOPNOTSUPP;
1da177e4
LT
1955
1956 type -= RTM_BASE;
1957
1958 /* All the messages must have at least 1 byte length */
1959 if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
1960 return 0;
1961
e0d087af 1962 family = ((struct rtgenmsg *)NLMSG_DATA(nlh))->rtgen_family;
1da177e4
LT
1963 sz_idx = type>>2;
1964 kind = type&3;
1965
fd778461 1966 if (kind != 2 && !capable(CAP_NET_ADMIN))
1d00a4eb 1967 return -EPERM;
1da177e4 1968
b8f3ab42 1969 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
97c53cac 1970 struct sock *rtnl;
e2849863 1971 rtnl_dumpit_func dumpit;
c7ac8679
GR
1972 rtnl_calcit_func calcit;
1973 u16 min_dump_alloc = 0;
1da177e4 1974
e2849863
TG
1975 dumpit = rtnl_get_dumpit(family, type);
1976 if (dumpit == NULL)
038890fe 1977 return -EOPNOTSUPP;
c7ac8679
GR
1978 calcit = rtnl_get_calcit(family, type);
1979 if (calcit)
1980 min_dump_alloc = calcit(skb);
9ac4a169 1981
2907c35f 1982 __rtnl_unlock();
97c53cac 1983 rtnl = net->rtnl;
c7ac8679
GR
1984 err = netlink_dump_start(rtnl, skb, nlh, dumpit,
1985 NULL, min_dump_alloc);
2907c35f
ED
1986 rtnl_lock();
1987 return err;
1da177e4
LT
1988 }
1989
1990 memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
1991
1992 min_len = rtm_min[sz_idx];
1993 if (nlh->nlmsg_len < min_len)
1d00a4eb 1994 return -EINVAL;
1da177e4
LT
1995
1996 if (nlh->nlmsg_len > min_len) {
1997 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
e0d087af 1998 struct rtattr *attr = (void *)nlh + NLMSG_ALIGN(min_len);
1da177e4
LT
1999
2000 while (RTA_OK(attr, attrlen)) {
2001 unsigned flavor = attr->rta_type;
2002 if (flavor) {
2003 if (flavor > rta_max[sz_idx])
1d00a4eb 2004 return -EINVAL;
1da177e4
LT
2005 rta_buf[flavor-1] = attr;
2006 }
2007 attr = RTA_NEXT(attr, attrlen);
2008 }
2009 }
2010
e2849863
TG
2011 doit = rtnl_get_doit(family, type);
2012 if (doit == NULL)
038890fe 2013 return -EOPNOTSUPP;
1da177e4 2014
1d00a4eb 2015 return doit(skb, nlh, (void *)&rta_buf[0]);
1da177e4
LT
2016}
2017
cd40b7d3 2018static void rtnetlink_rcv(struct sk_buff *skb)
1da177e4 2019{
cd40b7d3
DL
2020 rtnl_lock();
2021 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
2022 rtnl_unlock();
1da177e4
LT
2023}
2024
1da177e4
LT
2025static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
2026{
2027 struct net_device *dev = ptr;
e9dc8653 2028
1da177e4 2029 switch (event) {
1da177e4
LT
2030 case NETDEV_UP:
2031 case NETDEV_DOWN:
10de05af 2032 case NETDEV_PRE_UP:
d90a909e
EB
2033 case NETDEV_POST_INIT:
2034 case NETDEV_REGISTER:
1da177e4 2035 case NETDEV_CHANGE:
755d0e77 2036 case NETDEV_PRE_TYPE_CHANGE:
1da177e4 2037 case NETDEV_GOING_DOWN:
a2835763 2038 case NETDEV_UNREGISTER:
d90a909e 2039 case NETDEV_UNREGISTER_BATCH:
ac3d3f81
AW
2040 case NETDEV_RELEASE:
2041 case NETDEV_JOIN:
1da177e4
LT
2042 break;
2043 default:
2044 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
2045 break;
2046 }
2047 return NOTIFY_DONE;
2048}
2049
2050static struct notifier_block rtnetlink_dev_notifier = {
2051 .notifier_call = rtnetlink_event,
2052};
2053
97c53cac 2054
2c8c1e72 2055static int __net_init rtnetlink_net_init(struct net *net)
97c53cac
DL
2056{
2057 struct sock *sk;
2058 sk = netlink_kernel_create(net, NETLINK_ROUTE, RTNLGRP_MAX,
2907c35f 2059 rtnetlink_rcv, &rtnl_mutex, THIS_MODULE);
97c53cac
DL
2060 if (!sk)
2061 return -ENOMEM;
97c53cac
DL
2062 net->rtnl = sk;
2063 return 0;
2064}
2065
2c8c1e72 2066static void __net_exit rtnetlink_net_exit(struct net *net)
97c53cac 2067{
775516bf
DL
2068 netlink_kernel_release(net->rtnl);
2069 net->rtnl = NULL;
97c53cac
DL
2070}
2071
2072static struct pernet_operations rtnetlink_net_ops = {
2073 .init = rtnetlink_net_init,
2074 .exit = rtnetlink_net_exit,
2075};
2076
1da177e4
LT
2077void __init rtnetlink_init(void)
2078{
2079 int i;
2080
2081 rtattr_max = 0;
2082 for (i = 0; i < ARRAY_SIZE(rta_max); i++)
2083 if (rta_max[i] > rtattr_max)
2084 rtattr_max = rta_max[i];
2085 rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
2086 if (!rta_buf)
2087 panic("rtnetlink_init: cannot allocate rta_buf\n");
2088
97c53cac 2089 if (register_pernet_subsys(&rtnetlink_net_ops))
1da177e4 2090 panic("rtnetlink_init: cannot initialize rtnetlink\n");
97c53cac 2091
1da177e4
LT
2092 netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
2093 register_netdevice_notifier(&rtnetlink_dev_notifier);
340d17fc 2094
c7ac8679
GR
2095 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
2096 rtnl_dump_ifinfo, rtnl_calcit);
2097 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
2098 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
2099 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
687ad8cc 2100
c7ac8679
GR
2101 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
2102 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
1da177e4
LT
2103}
2104