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