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