rtnetlink.c 160 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds's avatar
Linus Torvalds committed
2 3 4 5 6 7 8 9 10 11
/*
 * INET		An implementation of the TCP/IP protocol suite for the LINUX
 *		operating system.  INET is implemented using the  BSD Socket
 *		interface as the means of communication with the user level.
 *
 *		Routing netlink socket interface: protocol independent part.
 *
 * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
 *
 *	Fixes:
12
 *	Vitaly E. Lavrov		RTA_OK arithmetic was wrong.
Linus Torvalds's avatar
Linus Torvalds committed
13 14
 */

15
#include <linux/bitops.h>
Linus Torvalds's avatar
Linus Torvalds committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#include <linux/errno.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/socket.h>
#include <linux/kernel.h>
#include <linux/timer.h>
#include <linux/string.h>
#include <linux/sockios.h>
#include <linux/net.h>
#include <linux/fcntl.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include <linux/interrupt.h>
#include <linux/capability.h>
#include <linux/skbuff.h>
#include <linux/init.h>
#include <linux/security.h>
33
#include <linux/mutex.h>
34
#include <linux/if_addr.h>
35
#include <linux/if_bridge.h>
36
#include <linux/if_vlan.h>
37
#include <linux/pci.h>
38
#include <linux/etherdevice.h>
39
#include <linux/bpf.h>
Linus Torvalds's avatar
Linus Torvalds committed
40

41
#include <linux/uaccess.h>
Linus Torvalds's avatar
Linus Torvalds committed
42 43 44 45 46 47 48 49

#include <linux/inet.h>
#include <linux/netdevice.h>
#include <net/ip.h>
#include <net/protocol.h>
#include <net/arp.h>
#include <net/route.h>
#include <net/udp.h>
50
#include <net/tcp.h>
Linus Torvalds's avatar
Linus Torvalds committed
51 52
#include <net/sock.h>
#include <net/pkt_sched.h>
53
#include <net/fib_rules.h>
54
#include <net/rtnetlink.h>
55
#include <net/net_namespace.h>
56
#include <net/devlink.h>
57 58 59
#if IS_ENABLED(CONFIG_IPV6)
#include <net/addrconf.h>
#endif
Linus Torvalds's avatar
Linus Torvalds committed
60

61 62
#include "dev.h"

63
#define RTNL_MAX_TYPE		50
64
#define RTNL_SLAVE_MAX_TYPE	43
Kees Cook's avatar
Kees Cook committed
65

Eric Dumazet's avatar
Eric Dumazet committed
66
struct rtnl_link {
67 68
	rtnl_doit_func		doit;
	rtnl_dumpit_func	dumpit;
69
	struct module		*owner;
70
	unsigned int		flags;
71
	struct rcu_head		rcu;
72 73
};

74
static DEFINE_MUTEX(rtnl_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
75 76 77

void rtnl_lock(void)
{
78
	mutex_lock(&rtnl_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
79
}
Eric Dumazet's avatar
Eric Dumazet committed
80
EXPORT_SYMBOL(rtnl_lock);
Linus Torvalds's avatar
Linus Torvalds committed
81

82 83 84 85 86 87
int rtnl_lock_killable(void)
{
	return mutex_lock_killable(&rtnl_mutex);
}
EXPORT_SYMBOL(rtnl_lock_killable);

88 89 90 91 92 93 94 95 96 97
static struct sk_buff *defer_kfree_skb_list;
void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail)
{
	if (head && tail) {
		tail->next = defer_kfree_skb_list;
		defer_kfree_skb_list = head;
	}
}
EXPORT_SYMBOL(rtnl_kfree_skbs);

98
void __rtnl_unlock(void)
Linus Torvalds's avatar
Linus Torvalds committed
99
{
100 101 102 103
	struct sk_buff *head = defer_kfree_skb_list;

	defer_kfree_skb_list = NULL;

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
	/* Ensure that we didn't actually add any TODO item when __rtnl_unlock()
	 * is used. In some places, e.g. in cfg80211, we have code that will do
	 * something like
	 *   rtnl_lock()
	 *   wiphy_lock()
	 *   ...
	 *   rtnl_unlock()
	 *
	 * and because netdev_run_todo() acquires the RTNL for items on the list
	 * we could cause a situation such as this:
	 * Thread 1			Thread 2
	 *				  rtnl_lock()
	 *				  unregister_netdevice()
	 *				  __rtnl_unlock()
	 * rtnl_lock()
	 * wiphy_lock()
	 * rtnl_unlock()
	 *   netdev_run_todo()
	 *     __rtnl_unlock()
	 *
	 *     // list not empty now
	 *     // because of thread 2
	 *				  rtnl_lock()
	 *     while (!list_empty(...))
	 *       rtnl_lock()
	 *				  wiphy_lock()
	 * **** DEADLOCK ****
	 *
	 * However, usage of __rtnl_unlock() is rare, and so we can ensure that
	 * it's not used in cases where something is added to do the list.
	 */
	WARN_ON(!list_empty(&net_todo_list));

137
	mutex_unlock(&rtnl_mutex);
138 139 140 141 142 143 144 145

	while (head) {
		struct sk_buff *next = head->next;

		kfree_skb(head);
		cond_resched();
		head = next;
	}
Linus Torvalds's avatar
Linus Torvalds committed
146
}
147

Linus Torvalds's avatar
Linus Torvalds committed
148 149
void rtnl_unlock(void)
{
150
	/* This fellow will unlock it for us. */
Linus Torvalds's avatar
Linus Torvalds committed
151 152
	netdev_run_todo();
}
Eric Dumazet's avatar
Eric Dumazet committed
153
EXPORT_SYMBOL(rtnl_unlock);
Linus Torvalds's avatar
Linus Torvalds committed
154

155 156 157 158
int rtnl_trylock(void)
{
	return mutex_trylock(&rtnl_mutex);
}
Eric Dumazet's avatar
Eric Dumazet committed
159
EXPORT_SYMBOL(rtnl_trylock);
160

161 162 163 164
int rtnl_is_locked(void)
{
	return mutex_is_locked(&rtnl_mutex);
}
Eric Dumazet's avatar
Eric Dumazet committed
165
EXPORT_SYMBOL(rtnl_is_locked);
166

167 168 169 170 171 172
bool refcount_dec_and_rtnl_lock(refcount_t *r)
{
	return refcount_dec_and_mutex_lock(r, &rtnl_mutex);
}
EXPORT_SYMBOL(refcount_dec_and_rtnl_lock);

173
#ifdef CONFIG_PROVE_LOCKING
174
bool lockdep_rtnl_is_held(void)
175 176 177 178 179 180
{
	return lockdep_is_held(&rtnl_mutex);
}
EXPORT_SYMBOL(lockdep_rtnl_is_held);
#endif /* #ifdef CONFIG_PROVE_LOCKING */

181
static struct rtnl_link __rcu *__rcu *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196

static inline int rtm_msgindex(int msgtype)
{
	int msgindex = msgtype - RTM_BASE;

	/*
	 * msgindex < 0 implies someone tried to register a netlink
	 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
	 * the message type has not been added to linux/rtnetlink.h
	 */
	BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);

	return msgindex;
}

197 198
static struct rtnl_link *rtnl_get_link(int protocol, int msgtype)
{
199
	struct rtnl_link __rcu **tab;
200 201 202 203 204 205 206 207

	if (protocol >= ARRAY_SIZE(rtnl_msg_handlers))
		protocol = PF_UNSPEC;

	tab = rcu_dereference_rtnl(rtnl_msg_handlers[protocol]);
	if (!tab)
		tab = rcu_dereference_rtnl(rtnl_msg_handlers[PF_UNSPEC]);

208
	return rcu_dereference_rtnl(tab[msgtype]);
209 210
}

211 212 213 214
static int rtnl_register_internal(struct module *owner,
				  int protocol, int msgtype,
				  rtnl_doit_func doit, rtnl_dumpit_func dumpit,
				  unsigned int flags)
215
{
216 217
	struct rtnl_link *link, *old;
	struct rtnl_link __rcu **tab;
218
	int msgindex;
219
	int ret = -ENOBUFS;
220

221
	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
222 223
	msgindex = rtm_msgindex(msgtype);

224
	rtnl_lock();
225
	tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
226
	if (tab == NULL) {
227 228 229
		tab = kcalloc(RTM_NR_MSGTYPES, sizeof(void *), GFP_KERNEL);
		if (!tab)
			goto unlock;
230

231
		/* ensures we see the 0 stores */
232
		rcu_assign_pointer(rtnl_msg_handlers[protocol], tab);
233 234
	}

235 236 237 238 239 240 241 242 243 244 245
	old = rtnl_dereference(tab[msgindex]);
	if (old) {
		link = kmemdup(old, sizeof(*old), GFP_KERNEL);
		if (!link)
			goto unlock;
	} else {
		link = kzalloc(sizeof(*link), GFP_KERNEL);
		if (!link)
			goto unlock;
	}

246 247 248
	WARN_ON(link->owner && link->owner != owner);
	link->owner = owner;

249
	WARN_ON(doit && link->doit && link->doit != doit);
250
	if (doit)
251 252
		link->doit = doit;
	WARN_ON(dumpit && link->dumpit && link->dumpit != dumpit);
253
	if (dumpit)
254
		link->dumpit = dumpit;
255

256 257
	WARN_ON(rtnl_msgtype_kind(msgtype) != RTNL_KIND_DEL &&
		(flags & RTNL_FLAG_BULK_DEL_SUPPORTED));
258 259 260 261 262 263 264 265 266 267
	link->flags |= flags;

	/* publish protocol:msgtype */
	rcu_assign_pointer(tab[msgindex], link);
	ret = 0;
	if (old)
		kfree_rcu(old, rcu);
unlock:
	rtnl_unlock();
	return ret;
268
}
269 270 271 272 273 274 275 276 277

/**
 * rtnl_register_module - Register a rtnetlink message type
 *
 * @owner: module registering the hook (THIS_MODULE)
 * @protocol: Protocol family or PF_UNSPEC
 * @msgtype: rtnetlink message type
 * @doit: Function pointer called for each request message
 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
278
 * @flags: rtnl_link_flags to modify behaviour of doit/dumpit functions
279 280 281 282 283 284 285 286 287 288 289 290 291 292
 *
 * Like rtnl_register, but for use by removable modules.
 */
int rtnl_register_module(struct module *owner,
			 int protocol, int msgtype,
			 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
			 unsigned int flags)
{
	return rtnl_register_internal(owner, protocol, msgtype,
				      doit, dumpit, flags);
}
EXPORT_SYMBOL_GPL(rtnl_register_module);

/**
293
 * rtnl_register - Register a rtnetlink message type
294 295 296 297
 * @protocol: Protocol family or PF_UNSPEC
 * @msgtype: rtnetlink message type
 * @doit: Function pointer called for each request message
 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
298
 * @flags: rtnl_link_flags to modify behaviour of doit/dumpit functions
299 300 301 302 303 304 305 306
 *
 * Registers the specified function pointers (at least one of them has
 * to be non-NULL) to be called whenever a request message for the
 * specified protocol family and message type is received.
 *
 * The special protocol family PF_UNSPEC may be used to define fallback
 * function pointers for the case when no entry for the specific protocol
 * family exists.
307 308
 */
void rtnl_register(int protocol, int msgtype,
309
		   rtnl_doit_func doit, rtnl_dumpit_func dumpit,
310
		   unsigned int flags)
311
{
312 313 314 315 316 317 318
	int err;

	err = rtnl_register_internal(NULL, protocol, msgtype, doit, dumpit,
				     flags);
	if (err)
		pr_err("Unable to register rtnetlink message handler, "
		       "protocol = %d, message type = %d\n", protocol, msgtype);
319 320 321 322 323 324 325 326 327 328 329
}

/**
 * rtnl_unregister - Unregister a rtnetlink message type
 * @protocol: Protocol family or PF_UNSPEC
 * @msgtype: rtnetlink message type
 *
 * Returns 0 on success or a negative error code.
 */
int rtnl_unregister(int protocol, int msgtype)
{
330 331
	struct rtnl_link __rcu **tab;
	struct rtnl_link *link;
332 333
	int msgindex;

334
	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
335 336
	msgindex = rtm_msgindex(msgtype);

337
	rtnl_lock();
338 339
	tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
	if (!tab) {
340
		rtnl_unlock();
341
		return -ENOENT;
342
	}
343

344
	link = rtnl_dereference(tab[msgindex]);
345
	RCU_INIT_POINTER(tab[msgindex], NULL);
346
	rtnl_unlock();
347

348 349
	kfree_rcu(link, rcu);

350 351 352 353 354 355 356 357 358 359 360 361 362
	return 0;
}
EXPORT_SYMBOL_GPL(rtnl_unregister);

/**
 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
 * @protocol : Protocol family or PF_UNSPEC
 *
 * Identical to calling rtnl_unregster() for all registered message types
 * of a certain protocol family.
 */
void rtnl_unregister_all(int protocol)
{
363 364
	struct rtnl_link __rcu **tab;
	struct rtnl_link *link;
365
	int msgindex;
366

367
	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
368

369
	rtnl_lock();
370
	tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
371 372 373 374
	if (!tab) {
		rtnl_unlock();
		return;
	}
375
	RCU_INIT_POINTER(rtnl_msg_handlers[protocol], NULL);
376
	for (msgindex = 0; msgindex < RTM_NR_MSGTYPES; msgindex++) {
377
		link = rtnl_dereference(tab[msgindex]);
378 379 380
		if (!link)
			continue;

381
		RCU_INIT_POINTER(tab[msgindex], NULL);
382 383
		kfree_rcu(link, rcu);
	}
384 385
	rtnl_unlock();

386 387
	synchronize_net();

388
	kfree(tab);
389 390
}
EXPORT_SYMBOL_GPL(rtnl_unregister_all);
Linus Torvalds's avatar
Linus Torvalds committed
391

392 393
static LIST_HEAD(link_ops);

394 395 396 397 398 399 400 401 402 403 404
static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
{
	const struct rtnl_link_ops *ops;

	list_for_each_entry(ops, &link_ops, list) {
		if (!strcmp(ops->kind, kind))
			return ops;
	}
	return NULL;
}

405 406 407 408 409 410 411 412 413 414 415 416
/**
 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
 * @ops: struct rtnl_link_ops * to register
 *
 * The caller must hold the rtnl_mutex. This function should be used
 * by drivers that create devices during module initialization. It
 * must be called before registering the devices.
 *
 * Returns 0 on success or a negative error code.
 */
int __rtnl_link_register(struct rtnl_link_ops *ops)
{
417 418 419
	if (rtnl_link_ops_get(ops->kind))
		return -EEXIST;

420
	/* The check for alloc/setup is here because if ops
421 422 423 424
	 * does not have that filled up, it is not possible
	 * to use the ops for creating device. So do not
	 * fill up dellink as well. That disables rtnl_dellink.
	 */
425
	if ((ops->alloc || ops->setup) && !ops->dellink)
426
		ops->dellink = unregister_netdevice_queue;
427

428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
	list_add_tail(&ops->list, &link_ops);
	return 0;
}
EXPORT_SYMBOL_GPL(__rtnl_link_register);

/**
 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
 * @ops: struct rtnl_link_ops * to register
 *
 * Returns 0 on success or a negative error code.
 */
int rtnl_link_register(struct rtnl_link_ops *ops)
{
	int err;

Kees Cook's avatar
Kees Cook committed
443 444 445 446 447
	/* Sanity-check max sizes to avoid stack buffer overflow. */
	if (WARN_ON(ops->maxtype > RTNL_MAX_TYPE ||
		    ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE))
		return -EINVAL;

448 449 450 451 452 453 454
	rtnl_lock();
	err = __rtnl_link_register(ops);
	rtnl_unlock();
	return err;
}
EXPORT_SYMBOL_GPL(rtnl_link_register);

455 456 457
static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
{
	struct net_device *dev;
458 459
	LIST_HEAD(list_kill);

460
	for_each_netdev(net, dev) {
461 462
		if (dev->rtnl_link_ops == ops)
			ops->dellink(dev, &list_kill);
463
	}
464
	unregister_netdevice_many(&list_kill);
465 466
}

467 468 469 470
/**
 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
 * @ops: struct rtnl_link_ops * to unregister
 *
471 472 473
 * The caller must hold the rtnl_mutex and guarantee net_namespace_list
 * integrity (hold pernet_ops_rwsem for writing to close the race
 * with setup_net() and cleanup_net()).
474 475 476
 */
void __rtnl_link_unregister(struct rtnl_link_ops *ops)
{
477
	struct net *net;
478

479
	for_each_net(net) {
480
		__rtnl_kill_links(net, ops);
481
	}
482 483 484 485
	list_del(&ops->list);
}
EXPORT_SYMBOL_GPL(__rtnl_link_unregister);

486 487 488 489 490 491 492
/* Return with the rtnl_lock held when there are no network
 * devices unregistering in any network namespace.
 */
static void rtnl_lock_unregistering_all(void)
{
	struct net *net;
	bool unregistering;
493
	DEFINE_WAIT_FUNC(wait, woken_wake_function);
494

495
	add_wait_queue(&netdev_unregistering_wq, &wait);
496 497 498
	for (;;) {
		unregistering = false;
		rtnl_lock();
499 500 501
		/* We held write locked pernet_ops_rwsem, and parallel
		 * setup_net() and cleanup_net() are not possible.
		 */
502
		for_each_net(net) {
503
			if (atomic_read(&net->dev_unreg_count) > 0) {
504 505 506 507 508 509 510
				unregistering = true;
				break;
			}
		}
		if (!unregistering)
			break;
		__rtnl_unlock();
511 512

		wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
513
	}
514
	remove_wait_queue(&netdev_unregistering_wq, &wait);
515 516
}

517 518 519 520 521 522
/**
 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
 * @ops: struct rtnl_link_ops * to unregister
 */
void rtnl_link_unregister(struct rtnl_link_ops *ops)
{
Kirill Tkhai's avatar
Kirill Tkhai committed
523
	/* Close the race with setup_net() and cleanup_net() */
524
	down_write(&pernet_ops_rwsem);
525
	rtnl_lock_unregistering_all();
526 527
	__rtnl_link_unregister(ops);
	rtnl_unlock();
528
	up_write(&pernet_ops_rwsem);
529 530 531
}
EXPORT_SYMBOL_GPL(rtnl_link_unregister);

532 533 534 535
static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
{
	struct net_device *master_dev;
	const struct rtnl_link_ops *ops;
536
	size_t size = 0;
537

538 539 540
	rcu_read_lock();

	master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
541
	if (!master_dev)
542 543
		goto out;

544
	ops = master_dev->rtnl_link_ops;
545
	if (!ops || !ops->get_slave_size)
546
		goto out;
547
	/* IFLA_INFO_SLAVE_DATA + nested data */
548
	size = nla_total_size(sizeof(struct nlattr)) +
549
	       ops->get_slave_size(master_dev, dev);
550 551 552 553

out:
	rcu_read_unlock();
	return size;
554 555
}

556 557 558 559 560 561 562 563
static size_t rtnl_link_get_size(const struct net_device *dev)
{
	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
	size_t size;

	if (!ops)
		return 0;

564 565
	size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
	       nla_total_size(strlen(ops->kind) + 1);  /* IFLA_INFO_KIND */
566 567 568

	if (ops->get_size)
		/* IFLA_INFO_DATA + nested data */
569
		size += nla_total_size(sizeof(struct nlattr)) +
570 571 572
			ops->get_size(dev);

	if (ops->get_xstats_size)
573 574
		/* IFLA_INFO_XSTATS */
		size += nla_total_size(ops->get_xstats_size(dev));
575

576 577
	size += rtnl_link_get_slave_info_data_size(dev);

578 579 580
	return size;
}

581 582 583 584 585 586
static LIST_HEAD(rtnl_af_ops);

static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
{
	const struct rtnl_af_ops *ops;

587 588 589
	ASSERT_RTNL();

	list_for_each_entry(ops, &rtnl_af_ops, list) {
590 591 592 593 594 595 596 597 598 599 600 601 602
		if (ops->family == family)
			return ops;
	}

	return NULL;
}

/**
 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
 * @ops: struct rtnl_af_ops * to register
 *
 * Returns 0 on success or a negative error code.
 */
603
void rtnl_af_register(struct rtnl_af_ops *ops)
604 605
{
	rtnl_lock();
606
	list_add_tail_rcu(&ops->list, &rtnl_af_ops);
607 608 609 610 611 612 613 614 615 616 617
	rtnl_unlock();
}
EXPORT_SYMBOL_GPL(rtnl_af_register);

/**
 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
 * @ops: struct rtnl_af_ops * to unregister
 */
void rtnl_af_unregister(struct rtnl_af_ops *ops)
{
	rtnl_lock();
618
	list_del_rcu(&ops->list);
619
	rtnl_unlock();
620 621

	synchronize_rcu();
622 623 624
}
EXPORT_SYMBOL_GPL(rtnl_af_unregister);

625 626
static size_t rtnl_link_get_af_size(const struct net_device *dev,
				    u32 ext_filter_mask)
627 628 629 630 631 632 633
{
	struct rtnl_af_ops *af_ops;
	size_t size;

	/* IFLA_AF_SPEC */
	size = nla_total_size(sizeof(struct nlattr));

634 635
	rcu_read_lock();
	list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
636 637 638
		if (af_ops->get_link_af_size) {
			/* AF_* + nested data */
			size += nla_total_size(sizeof(struct nlattr)) +
639
				af_ops->get_link_af_size(dev, ext_filter_mask);
640 641
		}
	}
642
	rcu_read_unlock();
643 644 645 646

	return size;
}

647
static bool rtnl_have_link_slave_info(const struct net_device *dev)
648
{
649
	struct net_device *master_dev;
650
	bool ret = false;
651

652 653 654
	rcu_read_lock();

	master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
655
	if (master_dev && master_dev->rtnl_link_ops)
656 657 658
		ret = true;
	rcu_read_unlock();
	return ret;
659 660 661 662 663 664 665 666 667
}

static int rtnl_link_slave_info_fill(struct sk_buff *skb,
				     const struct net_device *dev)
{
	struct net_device *master_dev;
	const struct rtnl_link_ops *ops;
	struct nlattr *slave_data;
	int err;
668

669 670 671 672 673 674 675 676 677
	master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
	if (!master_dev)
		return 0;
	ops = master_dev->rtnl_link_ops;
	if (!ops)
		return 0;
	if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
		return -EMSGSIZE;
	if (ops->fill_slave_info) {
678
		slave_data = nla_nest_start_noflag(skb, IFLA_INFO_SLAVE_DATA);
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
		if (!slave_data)
			return -EMSGSIZE;
		err = ops->fill_slave_info(skb, master_dev, dev);
		if (err < 0)
			goto err_cancel_slave_data;
		nla_nest_end(skb, slave_data);
	}
	return 0;

err_cancel_slave_data:
	nla_nest_cancel(skb, slave_data);
	return err;
}

static int rtnl_link_info_fill(struct sk_buff *skb,
			       const struct net_device *dev)
{
	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
	struct nlattr *data;
	int err;

	if (!ops)
		return 0;
702
	if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
703
		return -EMSGSIZE;
704 705 706
	if (ops->fill_xstats) {
		err = ops->fill_xstats(skb, dev);
		if (err < 0)
707
			return err;
708 709
	}
	if (ops->fill_info) {
710
		data = nla_nest_start_noflag(skb, IFLA_INFO_DATA);
711 712
		if (data == NULL)
			return -EMSGSIZE;
713 714 715 716 717 718 719 720 721
		err = ops->fill_info(skb, dev);
		if (err < 0)
			goto err_cancel_data;
		nla_nest_end(skb, data);
	}
	return 0;

err_cancel_data:
	nla_nest_cancel(skb, data);
722 723 724 725 726 727 728 729
	return err;
}

static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
{
	struct nlattr *linkinfo;
	int err = -EMSGSIZE;

730
	linkinfo = nla_nest_start_noflag(skb, IFLA_LINKINFO);
731 732 733 734 735 736 737 738 739 740 741 742 743 744
	if (linkinfo == NULL)
		goto out;

	err = rtnl_link_info_fill(skb, dev);
	if (err < 0)
		goto err_cancel_link;

	err = rtnl_link_slave_info_fill(skb, dev);
	if (err < 0)
		goto err_cancel_link;

	nla_nest_end(skb, linkinfo);
	return 0;

745 746 747 748 749 750
err_cancel_link:
	nla_nest_cancel(skb, linkinfo);
out:
	return err;
}

751
int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
Linus Torvalds's avatar
Linus Torvalds committed
752
{
753
	struct sock *rtnl = net->rtnl;
754 755

	return nlmsg_notify(rtnl, skb, pid, group, echo, GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
756 757
}

758
int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
759
{
760 761
	struct sock *rtnl = net->rtnl;

762 763
	return nlmsg_unicast(rtnl, skb, pid);
}
Eric Dumazet's avatar
Eric Dumazet committed
764
EXPORT_SYMBOL(rtnl_unicast);
765

766
void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
767
		 const struct nlmsghdr *nlh, gfp_t flags)
768
{
769
	struct sock *rtnl = net->rtnl;
770

771
	nlmsg_notify(rtnl, skb, pid, group, nlmsg_report(nlh), flags);
772
}
Eric Dumazet's avatar
Eric Dumazet committed
773
EXPORT_SYMBOL(rtnl_notify);
774

775
void rtnl_set_sk_err(struct net *net, u32 group, int error)
776
{
777 778
	struct sock *rtnl = net->rtnl;

779 780
	netlink_set_err(rtnl, 0, group, error);
}
Eric Dumazet's avatar
Eric Dumazet committed
781
EXPORT_SYMBOL(rtnl_set_sk_err);
782

Linus Torvalds's avatar
Linus Torvalds committed
783 784
int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
{
785 786 787
	struct nlattr *mx;
	int i, valid = 0;

788 789 790 791
	/* nothing is dumped for dst_default_metrics, so just skip the loop */
	if (metrics == dst_default_metrics.metrics)
		return 0;

792
	mx = nla_nest_start_noflag(skb, RTA_METRICS);
793 794 795 796 797
	if (mx == NULL)
		return -ENOBUFS;

	for (i = 0; i < RTAX_MAX; i++) {
		if (metrics[i]) {
798 799 800 801 802 803 804 805
			if (i == RTAX_CC_ALGO - 1) {
				char tmp[TCP_CA_NAME_MAX], *name;

				name = tcp_ca_get_name_by_key(metrics[i], tmp);
				if (!name)
					continue;
				if (nla_put_string(skb, i + 1, name))
					goto nla_put_failure;
806 807 808
			} else if (i == RTAX_FEATURES - 1) {
				u32 user_features = metrics[i] & RTAX_FEATURE_MASK;

809 810
				if (!user_features)
					continue;
811 812 813
				BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
				if (nla_put_u32(skb, i + 1, user_features))
					goto nla_put_failure;
814 815 816 817
			} else {
				if (nla_put_u32(skb, i + 1, metrics[i]))
					goto nla_put_failure;
			}
818 819
			valid++;
		}
Linus Torvalds's avatar
Linus Torvalds committed
820 821
	}

822 823 824 825
	if (!valid) {
		nla_nest_cancel(skb, mx);
		return 0;
	}
826 827 828 829

	return nla_nest_end(skb, mx);

nla_put_failure:
830 831
	nla_nest_cancel(skb, mx);
	return -EMSGSIZE;
Linus Torvalds's avatar
Linus Torvalds committed
832
}
Eric Dumazet's avatar
Eric Dumazet committed
833
EXPORT_SYMBOL(rtnetlink_put_metrics);
Linus Torvalds's avatar
Linus Torvalds committed
834

835
int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
836
		       long expires, u32 error)
837 838 839 840 841 842
{
	struct rta_cacheinfo ci = {
		.rta_error = error,
		.rta_id =  id,
	};

843 844 845
	if (dst) {
		ci.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse);
		ci.rta_used = dst->__use;
846
		ci.rta_clntref = rcuref_read(&dst->__rcuref);
847
	}
848 849
	if (expires) {
		unsigned long clock;
850

851 852 853 854
		clock = jiffies_to_clock_t(abs(expires));
		clock = min_t(unsigned long, clock, INT_MAX);
		ci.rta_expires = (expires > 0) ? clock : -clock;
	}
855 856 857
	return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
}
EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
Linus Torvalds's avatar
Linus Torvalds committed
858

859
static void set_operstate(struct net_device *dev, unsigned char transition)
860 861 862
{
	unsigned char operstate = dev->operstate;

Eric Dumazet's avatar
Eric Dumazet committed
863
	switch (transition) {
864 865
	case IF_OPER_UP:
		if ((operstate == IF_OPER_DORMANT ||
Andrew Lunn's avatar
Andrew Lunn committed
866
		     operstate == IF_OPER_TESTING ||
867
		     operstate == IF_OPER_UNKNOWN) &&
Andrew Lunn's avatar
Andrew Lunn committed
868
		    !netif_dormant(dev) && !netif_testing(dev))
869 870 871
			operstate = IF_OPER_UP;
		break;

Andrew Lunn's avatar
Andrew Lunn committed
872
	case IF_OPER_TESTING:
873
		if (netif_oper_up(dev))
Andrew Lunn's avatar
Andrew Lunn committed
874 875 876
			operstate = IF_OPER_TESTING;
		break;

877
	case IF_OPER_DORMANT:
878
		if (netif_oper_up(dev))
879 880
			operstate = IF_OPER_DORMANT;
		break;
881
	}
882 883

	if (dev->operstate != operstate) {
884
		write_lock(&dev_base_lock);
885
		dev->operstate = operstate;
886
		write_unlock(&dev_base_lock);
887 888
		netdev_state_change(dev);
	}
889 890
}

891 892 893 894 895 896
static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
{
	return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
	       (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
}

897 898 899 900 901 902 903 904
static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
					   const struct ifinfomsg *ifm)
{
	unsigned int flags = ifm->ifi_flags;

	/* bugwards compatibility: ifi_change == 0 is treated as ~0 */
	if (ifm->ifi_change)
		flags = (flags & ifm->ifi_change) |
905
			(rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
906 907 908 909

	return flags;
}

910
static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
911
				 const struct rtnl_link_stats64 *b)
Linus Torvalds's avatar
Linus Torvalds committed
912
{
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
	a->rx_packets = b->rx_packets;
	a->tx_packets = b->tx_packets;
	a->rx_bytes = b->rx_bytes;
	a->tx_bytes = b->tx_bytes;
	a->rx_errors = b->rx_errors;
	a->tx_errors = b->tx_errors;
	a->rx_dropped = b->rx_dropped;
	a->tx_dropped = b->tx_dropped;

	a->multicast = b->multicast;
	a->collisions = b->collisions;

	a->rx_length_errors = b->rx_length_errors;
	a->rx_over_errors = b->rx_over_errors;
	a->rx_crc_errors = b->rx_crc_errors;
	a->rx_frame_errors = b->rx_frame_errors;
	a->rx_fifo_errors = b->rx_fifo_errors;
	a->rx_missed_errors = b->rx_missed_errors;

	a->tx_aborted_errors = b->tx_aborted_errors;
	a->tx_carrier_errors = b->tx_carrier_errors;
	a->tx_fifo_errors = b->tx_fifo_errors;
	a->tx_heartbeat_errors = b->tx_heartbeat_errors;
	a->tx_window_errors = b->tx_window_errors;

	a->rx_compressed = b->rx_compressed;
	a->tx_compressed = b->tx_compressed;
940 941

	a->rx_nohandler = b->rx_nohandler;
942 943
}

944
/* All VF info */
945 946
static inline int rtnl_vfinfo_size(const struct net_device *dev,
				   u32 ext_filter_mask)
947
{
948
	if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
949
		int num_vfs = dev_num_vf(dev->dev.parent);
950
		size_t size = nla_total_size(0);
951
		size += num_vfs *
952 953
			(nla_total_size(0) +
			 nla_total_size(sizeof(struct ifla_vf_mac)) +
954
			 nla_total_size(sizeof(struct ifla_vf_broadcast)) +
955 956
			 nla_total_size(sizeof(struct ifla_vf_vlan)) +
			 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
957 958
			 nla_total_size(MAX_VLAN_LIST_LEN *
					sizeof(struct ifla_vf_vlan_info)) +
959
			 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
960
			 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
Jiri Benc's avatar
Jiri Benc committed
961
			 nla_total_size(sizeof(struct ifla_vf_rate)) +
962
			 nla_total_size(sizeof(struct ifla_vf_link_state)) +
963
			 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
964
			 nla_total_size(sizeof(struct ifla_vf_trust)));
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
		if (~ext_filter_mask & RTEXT_FILTER_SKIP_STATS) {
			size += num_vfs *
				(nla_total_size(0) + /* nest IFLA_VF_STATS */
				 /* IFLA_VF_STATS_RX_PACKETS */
				 nla_total_size_64bit(sizeof(__u64)) +
				 /* IFLA_VF_STATS_TX_PACKETS */
				 nla_total_size_64bit(sizeof(__u64)) +
				 /* IFLA_VF_STATS_RX_BYTES */
				 nla_total_size_64bit(sizeof(__u64)) +
				 /* IFLA_VF_STATS_TX_BYTES */
				 nla_total_size_64bit(sizeof(__u64)) +
				 /* IFLA_VF_STATS_BROADCAST */
				 nla_total_size_64bit(sizeof(__u64)) +
				 /* IFLA_VF_STATS_MULTICAST */
				 nla_total_size_64bit(sizeof(__u64)) +
				 /* IFLA_VF_STATS_RX_DROPPED */
				 nla_total_size_64bit(sizeof(__u64)) +
				 /* IFLA_VF_STATS_TX_DROPPED */
				 nla_total_size_64bit(sizeof(__u64)));
		}
985 986
		return size;
	} else
987 988 989
		return 0;
}

990 991
static size_t rtnl_port_size(const struct net_device *dev,
			     u32 ext_filter_mask)
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
{
	size_t port_size = nla_total_size(4)		/* PORT_VF */
		+ nla_total_size(PORT_PROFILE_MAX)	/* PORT_PROFILE */
		+ nla_total_size(PORT_UUID_MAX)		/* PORT_INSTANCE_UUID */
		+ nla_total_size(PORT_UUID_MAX)		/* PORT_HOST_UUID */
		+ nla_total_size(1)			/* PROT_VDP_REQUEST */
		+ nla_total_size(2);			/* PORT_VDP_RESPONSE */
	size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
	size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
		+ port_size;
	size_t port_self_size = nla_total_size(sizeof(struct nlattr))
		+ port_size;

1005 1006
	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
	    !(ext_filter_mask & RTEXT_FILTER_VF))
1007 1008 1009 1010 1011 1012 1013 1014
		return 0;
	if (dev_num_vf(dev->dev.parent))
		return port_self_size + vf_ports_size +
			vf_port_size * dev_num_vf(dev->dev.parent);
	else
		return port_self_size;
}

David S. Miller's avatar
David S. Miller committed
1015
static size_t rtnl_xdp_size(void)
1016
{
1017
	size_t xdp_size = nla_total_size(0) +	/* nest IFLA_XDP */
1018
			  nla_total_size(1) +	/* XDP_ATTACHED */
1019
			  nla_total_size(4) +	/* XDP_PROG_ID (or 1st mode) */
1020
			  nla_total_size(4);	/* XDP_<mode>_PROG_ID */
1021

David S. Miller's avatar
David S. Miller committed
1022
	return xdp_size;
1023 1024
}

1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
static size_t rtnl_prop_list_size(const struct net_device *dev)
{
	struct netdev_name_node *name_node;
	size_t size;

	if (list_empty(&dev->name_node->list))
		return 0;
	size = nla_total_size(0);
	list_for_each_entry(name_node, &dev->name_node->list, list)
		size += nla_total_size(ALTIFNAMSIZ);
	return size;
}

1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
static size_t rtnl_proto_down_size(const struct net_device *dev)
{
	size_t size = nla_total_size(1);

	if (dev->proto_down_reason)
		size += nla_total_size(0) + nla_total_size(4);

	return size;
}

1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
static size_t rtnl_devlink_port_size(const struct net_device *dev)
{
	size_t size = nla_total_size(0); /* nest IFLA_DEVLINK_PORT */

	if (dev->devlink_port)
		size += devlink_nl_port_handle_size(dev->devlink_port);

	return size;
}

1058 1059
static noinline size_t if_nlmsg_size(const struct net_device *dev,
				     u32 ext_filter_mask)
1060 1061 1062
{
	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
1063
	       + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
1064
	       + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
1065
	       + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
1066
	       + nla_total_size(sizeof(struct rtnl_link_stats))
1067
	       + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
1068 1069 1070 1071 1072 1073 1074
	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
	       + nla_total_size(4) /* IFLA_TXQLEN */
	       + nla_total_size(4) /* IFLA_WEIGHT */
	       + nla_total_size(4) /* IFLA_MTU */
	       + nla_total_size(4) /* IFLA_LINK */
	       + nla_total_size(4) /* IFLA_MASTER */
1075
	       + nla_total_size(1) /* IFLA_CARRIER */
1076
	       + nla_total_size(4) /* IFLA_PROMISCUITY */
1077
	       + nla_total_size(4) /* IFLA_ALLMULTI */
1078 1079
	       + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
	       + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
1080 1081
	       + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
	       + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
1082
	       + nla_total_size(4) /* IFLA_GRO_MAX_SIZE */
1083 1084
	       + nla_total_size(4) /* IFLA_GSO_IPV4_MAX_SIZE */
	       + nla_total_size(4) /* IFLA_GRO_IPV4_MAX_SIZE */
1085 1086
	       + nla_total_size(4) /* IFLA_TSO_MAX_SIZE */
	       + nla_total_size(4) /* IFLA_TSO_MAX_SEGS */
1087
	       + nla_total_size(1) /* IFLA_OPERSTATE */
1088
	       + nla_total_size(1) /* IFLA_LINKMODE */
1089
	       + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
1090
	       + nla_total_size(4) /* IFLA_LINK_NETNSID */
1091
	       + nla_total_size(4) /* IFLA_GROUP */
1092 1093 1094
	       + nla_total_size(ext_filter_mask
			        & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
	       + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
1095
	       + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
1096
	       + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
1097
	       + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
1098
	       + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
1099
	       + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
1100
	       + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
David S. Miller's avatar
David S. Miller committed
1101
	       + rtnl_xdp_size() /* IFLA_XDP */
1102
	       + nla_total_size(4)  /* IFLA_EVENT */
1103
	       + nla_total_size(4)  /* IFLA_NEW_NETNSID */
1104
	       + nla_total_size(4)  /* IFLA_NEW_IFINDEX */
1105
	       + rtnl_proto_down_size(dev)  /* proto down */
1106
	       + nla_total_size(4)  /* IFLA_TARGET_NETNSID */
1107 1108
	       + nla_total_size(4)  /* IFLA_CARRIER_UP_COUNT */
	       + nla_total_size(4)  /* IFLA_CARRIER_DOWN_COUNT */
1109 1110
	       + nla_total_size(4)  /* IFLA_MIN_MTU */
	       + nla_total_size(4)  /* IFLA_MAX_MTU */
1111
	       + rtnl_prop_list_size(dev)
1112
	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_PERM_ADDRESS */
1113
	       + rtnl_devlink_port_size(dev)
1114
	       + 0;
1115 1116
}

1117 1118 1119 1120 1121 1122 1123
static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
{
	struct nlattr *vf_ports;
	struct nlattr *vf_port;
	int vf;
	int err;

1124
	vf_ports = nla_nest_start_noflag(skb, IFLA_VF_PORTS);
1125 1126 1127 1128
	if (!vf_ports)
		return -EMSGSIZE;

	for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
1129
		vf_port = nla_nest_start_noflag(skb, IFLA_VF_PORT);
1130 1131
		if (!vf_port)
			goto nla_put_failure;
1132 1133
		if (nla_put_u32(skb, IFLA_PORT_VF, vf))
			goto nla_put_failure;
1134
		err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
1135 1136
		if (err == -EMSGSIZE)
			goto nla_put_failure;
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
		if (err) {
			nla_nest_cancel(skb, vf_port);
			continue;
		}
		nla_nest_end(skb, vf_port);
	}

	nla_nest_end(skb, vf_ports);

	return 0;
1147 1148 1149 1150

nla_put_failure:
	nla_nest_cancel(skb, vf_ports);
	return -EMSGSIZE;
1151 1152 1153 1154 1155 1156 1157
}

static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
{
	struct nlattr *port_self;
	int err;

1158
	port_self = nla_nest_start_noflag(skb, IFLA_PORT_SELF);
1159 1160 1161 1162 1163 1164
	if (!port_self)
		return -EMSGSIZE;

	err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
	if (err) {
		nla_nest_cancel(skb, port_self);
1165
		return (err == -EMSGSIZE) ? err : 0;
1166 1167 1168 1169 1170 1171 1172
	}

	nla_nest_end(skb, port_self);

	return 0;
}

1173 1174
static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
			  u32 ext_filter_mask)
1175 1176 1177
{
	int err;

1178 1179
	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
	    !(ext_filter_mask & RTEXT_FILTER_VF))
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
		return 0;

	err = rtnl_port_self_fill(skb, dev);
	if (err)
		return err;

	if (dev_num_vf(dev->dev.parent)) {
		err = rtnl_vf_ports_fill(skb, dev);
		if (err)
			return err;
	}

	return 0;
}

1195 1196 1197
static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
{
	int err;
1198
	struct netdev_phys_item_id ppid;
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212

	err = dev_get_phys_port_id(dev, &ppid);
	if (err) {
		if (err == -EOPNOTSUPP)
			return 0;
		return err;
	}

	if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
		return -EMSGSIZE;

	return 0;
}

1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
{
	char name[IFNAMSIZ];
	int err;

	err = dev_get_phys_port_name(dev, name, sizeof(name));
	if (err) {
		if (err == -EOPNOTSUPP)
			return 0;
		return err;
	}

1225
	if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
1226 1227 1228 1229 1230
		return -EMSGSIZE;

	return 0;
}

1231 1232
static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
{
1233
	struct netdev_phys_item_id ppid = { };
1234 1235
	int err;

1236
	err = dev_get_port_parent_id(dev, &ppid, false);
1237 1238 1239 1240 1241 1242
	if (err) {
		if (err == -EOPNOTSUPP)
			return 0;
		return err;
	}

1243
	if (nla_put(skb, IFLA_PHYS_SWITCH_ID, ppid.id_len, ppid.id))
1244 1245 1246 1247 1248
		return -EMSGSIZE;

	return 0;
}

1249 1250 1251
static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
					      struct net_device *dev)
{
1252
	struct rtnl_link_stats64 *sp;
1253
	struct nlattr *attr;
1254

1255 1256
	attr = nla_reserve_64bit(skb, IFLA_STATS64,
				 sizeof(struct rtnl_link_stats64), IFLA_PAD);
1257 1258 1259
	if (!attr)
		return -EMSGSIZE;

1260 1261
	sp = nla_data(attr);
	dev_get_stats(dev, sp);
1262

1263 1264
	attr = nla_reserve(skb, IFLA_STATS,
			   sizeof(struct rtnl_link_stats));
1265 1266 1267
	if (!attr)
		return -EMSGSIZE;

1268
	copy_rtnl_link_stats(nla_data(attr), sp);
1269 1270 1271 1272 1273 1274 1275

	return 0;
}

static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
					       struct net_device *dev,
					       int vfs_num,
1276
					       u32 ext_filter_mask)
1277 1278
{
	struct ifla_vf_rss_query_en vf_rss_query_en;
1279
	struct nlattr *vf, *vfstats, *vfvlanlist;
1280
	struct ifla_vf_link_state vf_linkstate;
1281
	struct ifla_vf_vlan_info vf_vlan_info;
1282 1283 1284 1285 1286 1287 1288
	struct ifla_vf_spoofchk vf_spoofchk;
	struct ifla_vf_tx_rate vf_tx_rate;
	struct ifla_vf_stats vf_stats;
	struct ifla_vf_trust vf_trust;
	struct ifla_vf_vlan vf_vlan;
	struct ifla_vf_rate vf_rate;
	struct ifla_vf_mac vf_mac;
1289
	struct ifla_vf_broadcast vf_broadcast;
1290
	struct ifla_vf_info ivi;
1291 1292
	struct ifla_vf_guid node_guid;
	struct ifla_vf_guid port_guid;
1293

1294 1295
	memset(&ivi, 0, sizeof(ivi));

1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
	/* Not all SR-IOV capable drivers support the
	 * spoofcheck and "RSS query enable" query.  Preset to
	 * -1 so the user space tool can detect that the driver
	 * didn't report anything.
	 */
	ivi.spoofchk = -1;
	ivi.rss_query_en = -1;
	ivi.trusted = -1;
	/* The default value for VF link state is "auto"
	 * IFLA_VF_LINK_STATE_AUTO which equals zero
	 */
	ivi.linkstate = 0;
1308 1309
	/* VLAN Protocol by default is 802.1Q */
	ivi.vlan_proto = htons(ETH_P_8021Q);
1310 1311 1312
	if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
		return 0;

1313
	memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
1314 1315
	memset(&node_guid, 0, sizeof(node_guid));
	memset(&port_guid, 0, sizeof(port_guid));
1316

1317 1318
	vf_mac.vf =
		vf_vlan.vf =
1319
		vf_vlan_info.vf =
1320 1321 1322 1323 1324
		vf_rate.vf =
		vf_tx_rate.vf =
		vf_spoofchk.vf =
		vf_linkstate.vf =
		vf_rss_query_en.vf =
1325 1326 1327
		vf_trust.vf =
		node_guid.vf =
		port_guid.vf = ivi.vf;
1328 1329

	memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1330
	memcpy(vf_broadcast.broadcast, dev->broadcast, dev->addr_len);
1331 1332
	vf_vlan.vlan = ivi.vlan;
	vf_vlan.qos = ivi.qos;
1333 1334 1335
	vf_vlan_info.vlan = ivi.vlan;
	vf_vlan_info.qos = ivi.qos;
	vf_vlan_info.vlan_proto = ivi.vlan_proto;
1336 1337 1338 1339 1340 1341 1342
	vf_tx_rate.rate = ivi.max_tx_rate;
	vf_rate.min_tx_rate = ivi.min_tx_rate;
	vf_rate.max_tx_rate = ivi.max_tx_rate;
	vf_spoofchk.setting = ivi.spoofchk;
	vf_linkstate.link_state = ivi.linkstate;
	vf_rss_query_en.setting = ivi.rss_query_en;
	vf_trust.setting = ivi.trusted;
1343
	vf = nla_nest_start_noflag(skb, IFLA_VF_INFO);
1344
	if (!vf)
1345
		return -EMSGSIZE;
1346
	if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1347
	    nla_put(skb, IFLA_VF_BROADCAST, sizeof(vf_broadcast), &vf_broadcast) ||
1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
	    nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
	    nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
		    &vf_rate) ||
	    nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
		    &vf_tx_rate) ||
	    nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
		    &vf_spoofchk) ||
	    nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
		    &vf_linkstate) ||
	    nla_put(skb, IFLA_VF_RSS_QUERY_EN,
		    sizeof(vf_rss_query_en),
		    &vf_rss_query_en) ||
	    nla_put(skb, IFLA_VF_TRUST,
		    sizeof(vf_trust), &vf_trust))
1362
		goto nla_put_vf_failure;
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372

	if (dev->netdev_ops->ndo_get_vf_guid &&
	    !dev->netdev_ops->ndo_get_vf_guid(dev, vfs_num, &node_guid,
					      &port_guid)) {
		if (nla_put(skb, IFLA_VF_IB_NODE_GUID, sizeof(node_guid),
			    &node_guid) ||
		    nla_put(skb, IFLA_VF_IB_PORT_GUID, sizeof(port_guid),
			    &port_guid))
			goto nla_put_vf_failure;
	}
1373
	vfvlanlist = nla_nest_start_noflag(skb, IFLA_VF_VLAN_LIST);
1374 1375 1376 1377 1378 1379 1380 1381
	if (!vfvlanlist)
		goto nla_put_vf_failure;
	if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
		    &vf_vlan_info)) {
		nla_nest_cancel(skb, vfvlanlist);
		goto nla_put_vf_failure;
	}
	nla_nest_end(skb, vfvlanlist);
1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
	if (~ext_filter_mask & RTEXT_FILTER_SKIP_STATS) {
		memset(&vf_stats, 0, sizeof(vf_stats));
		if (dev->netdev_ops->ndo_get_vf_stats)
			dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
							  &vf_stats);
		vfstats = nla_nest_start_noflag(skb, IFLA_VF_STATS);
		if (!vfstats)
			goto nla_put_vf_failure;
		if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
				      vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
		    nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
				      vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
		    nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
				      vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
		    nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
				      vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
		    nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
				      vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
		    nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
				      vf_stats.multicast, IFLA_VF_STATS_PAD) ||
		    nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED,
				      vf_stats.rx_dropped, IFLA_VF_STATS_PAD) ||
		    nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED,
				      vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) {
			nla_nest_cancel(skb, vfstats);
			goto nla_put_vf_failure;
		}
		nla_nest_end(skb, vfstats);
1410
	}
1411 1412
	nla_nest_end(skb, vf);
	return 0;
1413 1414 1415 1416

nla_put_vf_failure:
	nla_nest_cancel(skb, vf);
	return -EMSGSIZE;
1417 1418
}

1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435
static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb,
					   struct net_device *dev,
					   u32 ext_filter_mask)
{
	struct nlattr *vfinfo;
	int i, num_vfs;

	if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0))
		return 0;

	num_vfs = dev_num_vf(dev->dev.parent);
	if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs))
		return -EMSGSIZE;

	if (!dev->netdev_ops->ndo_get_vf_config)
		return 0;

1436
	vfinfo = nla_nest_start_noflag(skb, IFLA_VFINFO_LIST);
1437 1438 1439 1440
	if (!vfinfo)
		return -EMSGSIZE;

	for (i = 0; i < num_vfs; i++) {
1441 1442
		if (rtnl_fill_vfinfo(skb, dev, i, ext_filter_mask)) {
			nla_nest_cancel(skb, vfinfo);
1443
			return -EMSGSIZE;
1444
		}
1445 1446 1447 1448 1449 1450
	}

	nla_nest_end(skb, vfinfo);
	return 0;
}

1451 1452
static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
{
1453 1454 1455 1456 1457 1458 1459 1460 1461 1462
	struct rtnl_link_ifmap map;

	memset(&map, 0, sizeof(map));
	map.mem_start   = dev->mem_start;
	map.mem_end     = dev->mem_end;
	map.base_addr   = dev->base_addr;
	map.irq         = dev->irq;
	map.dma         = dev->dma;
	map.port        = dev->if_port;

1463
	if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
1464 1465 1466 1467 1468
		return -EMSGSIZE;

	return 0;
}

1469
static u32 rtnl_xdp_prog_skb(struct net_device *dev)
1470
{
1471
	const struct bpf_prog *generic_xdp_prog;
1472 1473 1474

	ASSERT_RTNL();

1475
	generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
1476 1477 1478 1479
	if (!generic_xdp_prog)
		return 0;
	return generic_xdp_prog->aux->id;
}
1480

1481 1482
static u32 rtnl_xdp_prog_drv(struct net_device *dev)
{
1483
	return dev_xdp_prog_id(dev, XDP_MODE_DRV);
1484
}
1485

1486 1487
static u32 rtnl_xdp_prog_hw(struct net_device *dev)
{
1488
	return dev_xdp_prog_id(dev, XDP_MODE_HW);
1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505
}

static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev,
			       u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr,
			       u32 (*get_prog_id)(struct net_device *dev))
{
	u32 curr_id;
	int err;

	curr_id = get_prog_id(dev);
	if (!curr_id)
		return 0;

	*prog_id = curr_id;
	err = nla_put_u32(skb, attr, curr_id);
	if (err)
		return err;
1506

1507 1508 1509 1510
	if (*mode != XDP_ATTACHED_NONE)
		*mode = XDP_ATTACHED_MULTI;
	else
		*mode = tgt_mode;
1511

1512
	return 0;
1513 1514
}

1515 1516 1517
static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
{
	struct nlattr *xdp;
1518
	u32 prog_id;
1519
	int err;
1520
	u8 mode;
1521

1522
	xdp = nla_nest_start_noflag(skb, IFLA_XDP);
1523 1524
	if (!xdp)
		return -EMSGSIZE;
1525

1526 1527
	prog_id = 0;
	mode = XDP_ATTACHED_NONE;
1528 1529 1530
	err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB,
				  IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb);
	if (err)
1531
		goto err_cancel;
1532 1533 1534
	err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV,
				  IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv);
	if (err)
1535
		goto err_cancel;
1536 1537 1538
	err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW,
				  IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw);
	if (err)
1539 1540
		goto err_cancel;

1541
	err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode);
1542 1543 1544
	if (err)
		goto err_cancel;

1545
	if (prog_id && mode != XDP_ATTACHED_MULTI) {
1546 1547 1548 1549 1550
		err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
		if (err)
			goto err_cancel;
	}

1551 1552 1553 1554 1555 1556 1557 1558
	nla_nest_end(skb, xdp);
	return 0;

err_cancel:
	nla_nest_cancel(skb, xdp);
	return err;
}

1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588
static u32 rtnl_get_event(unsigned long event)
{
	u32 rtnl_event_type = IFLA_EVENT_NONE;

	switch (event) {
	case NETDEV_REBOOT:
		rtnl_event_type = IFLA_EVENT_REBOOT;
		break;
	case NETDEV_FEAT_CHANGE:
		rtnl_event_type = IFLA_EVENT_FEATURES;
		break;
	case NETDEV_BONDING_FAILOVER:
		rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
		break;
	case NETDEV_NOTIFY_PEERS:
		rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
		break;
	case NETDEV_RESEND_IGMP:
		rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
		break;
	case NETDEV_CHANGEINFODATA:
		rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
		break;
	default:
		break;
	}

	return rtnl_event_type;
}

1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603
static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
{
	const struct net_device *upper_dev;
	int ret = 0;

	rcu_read_lock();

	upper_dev = netdev_master_upper_dev_get_rcu(dev);
	if (upper_dev)
		ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);

	rcu_read_unlock();
	return ret;
}

1604 1605
static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev,
			  bool force)
1606 1607 1608
{
	int ifindex = dev_get_iflink(dev);

1609 1610
	if (force || dev->ifindex != ifindex)
		return nla_put_u32(skb, IFLA_LINK, ifindex);
1611

1612
	return 0;
1613 1614
}

1615 1616 1617 1618 1619 1620 1621 1622 1623 1624
static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
					      struct net_device *dev)
{
	char buf[IFALIASZ];
	int ret;

	ret = dev_get_alias(dev, buf, sizeof(buf));
	return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
}

1625
static int rtnl_fill_link_netnsid(struct sk_buff *skb,
1626
				  const struct net_device *dev,
1627
				  struct net *src_net, gfp_t gfp)
1628
{
1629 1630
	bool put_iflink = false;

1631 1632 1633 1634
	if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
		struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);

		if (!net_eq(dev_net(dev), link_net)) {
1635
			int id = peernet2id_alloc(src_net, link_net, gfp);
1636 1637 1638

			if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
				return -EMSGSIZE;
1639 1640

			put_iflink = true;
1641 1642 1643
		}
	}

1644
	return nla_put_iflink(skb, dev, put_iflink);
1645 1646
}

1647 1648 1649 1650 1651 1652 1653
static int rtnl_fill_link_af(struct sk_buff *skb,
			     const struct net_device *dev,
			     u32 ext_filter_mask)
{
	const struct rtnl_af_ops *af_ops;
	struct nlattr *af_spec;

1654
	af_spec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
1655 1656 1657
	if (!af_spec)
		return -EMSGSIZE;

1658
	list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
1659 1660 1661 1662 1663 1664
		struct nlattr *af;
		int err;

		if (!af_ops->fill_link_af)
			continue;

1665
		af = nla_nest_start_noflag(skb, af_ops->family);
1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687
		if (!af)
			return -EMSGSIZE;

		err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
		/*
		 * Caller may return ENODATA to indicate that there
		 * was no data to be dumped. This is not an error, it
		 * means we should trim the attribute header and
		 * continue.
		 */
		if (err == -ENODATA)
			nla_nest_cancel(skb, af);
		else if (err < 0)
			return -EMSGSIZE;

		nla_nest_end(skb, af);
	}

	nla_nest_end(skb, af_spec);
	return 0;
}

1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723
static int rtnl_fill_alt_ifnames(struct sk_buff *skb,
				 const struct net_device *dev)
{
	struct netdev_name_node *name_node;
	int count = 0;

	list_for_each_entry(name_node, &dev->name_node->list, list) {
		if (nla_put_string(skb, IFLA_ALT_IFNAME, name_node->name))
			return -EMSGSIZE;
		count++;
	}
	return count;
}

static int rtnl_fill_prop_list(struct sk_buff *skb,
			       const struct net_device *dev)
{
	struct nlattr *prop_list;
	int ret;

	prop_list = nla_nest_start(skb, IFLA_PROP_LIST);
	if (!prop_list)
		return -EMSGSIZE;

	ret = rtnl_fill_alt_ifnames(skb, dev);
	if (ret <= 0)
		goto nest_cancel;

	nla_nest_end(skb, prop_list);
	return 0;

nest_cancel:
	nla_nest_cancel(skb, prop_list);
	return ret;
}

1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752
static int rtnl_fill_proto_down(struct sk_buff *skb,
				const struct net_device *dev)
{
	struct nlattr *pr;
	u32 preason;

	if (nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
		goto nla_put_failure;

	preason = dev->proto_down_reason;
	if (!preason)
		return 0;

	pr = nla_nest_start(skb, IFLA_PROTO_DOWN_REASON);
	if (!pr)
		return -EMSGSIZE;

	if (nla_put_u32(skb, IFLA_PROTO_DOWN_REASON_VALUE, preason)) {
		nla_nest_cancel(skb, pr);
		goto nla_put_failure;
	}

	nla_nest_end(skb, pr);
	return 0;

nla_put_failure:
	return -EMSGSIZE;
}

1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776
static int rtnl_fill_devlink_port(struct sk_buff *skb,
				  const struct net_device *dev)
{
	struct nlattr *devlink_port_nest;
	int ret;

	devlink_port_nest = nla_nest_start(skb, IFLA_DEVLINK_PORT);
	if (!devlink_port_nest)
		return -EMSGSIZE;

	if (dev->devlink_port) {
		ret = devlink_nl_port_handle_fill(skb, dev->devlink_port);
		if (ret < 0)
			goto nest_cancel;
	}

	nla_nest_end(skb, devlink_port_nest);
	return 0;

nest_cancel:
	nla_nest_cancel(skb, devlink_port_nest);
	return ret;
}

1777 1778
static int rtnl_fill_ifinfo(struct sk_buff *skb,
			    struct net_device *dev, struct net *src_net,
1779
			    int type, u32 pid, u32 seq, u32 change,
1780
			    unsigned int flags, u32 ext_filter_mask,
1781
			    u32 event, int *new_nsid, int new_ifindex,
1782
			    int tgt_netnsid, gfp_t gfp)
1783 1784 1785
{
	struct ifinfomsg *ifm;
	struct nlmsghdr *nlh;
1786
	struct Qdisc *qdisc;
Linus Torvalds's avatar
Linus Torvalds committed
1787

1788
	ASSERT_RTNL();
1789 1790
	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
	if (nlh == NULL)
1791
		return -EMSGSIZE;
Linus Torvalds's avatar
Linus Torvalds committed
1792

1793 1794 1795 1796 1797 1798 1799 1800
	ifm = nlmsg_data(nlh);
	ifm->ifi_family = AF_UNSPEC;
	ifm->__ifi_pad = 0;
	ifm->ifi_type = dev->type;
	ifm->ifi_index = dev->ifindex;
	ifm->ifi_flags = dev_get_flags(dev);
	ifm->ifi_change = change;

1801
	if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid))
1802 1803
		goto nla_put_failure;

1804
	qdisc = rtnl_dereference(dev->qdisc);
1805 1806 1807 1808 1809 1810
	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
	    nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
	    nla_put_u8(skb, IFLA_OPERSTATE,
		       netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
	    nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1811 1812
	    nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) ||
	    nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) ||
1813
	    nla_put_u32(skb, IFLA_GROUP, dev->group) ||
1814
	    nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
1815
	    nla_put_u32(skb, IFLA_ALLMULTI, dev->allmulti) ||
1816
	    nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
1817 1818
	    nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
	    nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
1819
	    nla_put_u32(skb, IFLA_GRO_MAX_SIZE, dev->gro_max_size) ||
1820 1821
	    nla_put_u32(skb, IFLA_GSO_IPV4_MAX_SIZE, dev->gso_ipv4_max_size) ||
	    nla_put_u32(skb, IFLA_GRO_IPV4_MAX_SIZE, dev->gro_ipv4_max_size) ||
1822 1823
	    nla_put_u32(skb, IFLA_TSO_MAX_SIZE, dev->tso_max_size) ||
	    nla_put_u32(skb, IFLA_TSO_MAX_SEGS, dev->tso_max_segs) ||
1824
#ifdef CONFIG_RPS
1825
	    nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
1826
#endif
1827
	    put_master_ifindex(skb, dev) ||
1828
	    nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
1829 1830
	    (qdisc &&
	     nla_put_string(skb, IFLA_QDISC, qdisc->ops->id)) ||
1831
	    nla_put_ifalias(skb, dev) ||
1832
	    nla_put_u32(skb, IFLA_CARRIER_CHANGES,
1833 1834 1835 1836 1837 1838
			atomic_read(&dev->carrier_up_count) +
			atomic_read(&dev->carrier_down_count)) ||
	    nla_put_u32(skb, IFLA_CARRIER_UP_COUNT,
			atomic_read(&dev->carrier_up_count)) ||
	    nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT,
			atomic_read(&dev->carrier_down_count)))
1839
		goto nla_put_failure;
1840

1841 1842 1843
	if (rtnl_fill_proto_down(skb, dev))
		goto nla_put_failure;

1844 1845 1846 1847 1848
	if (event != IFLA_EVENT_NONE) {
		if (nla_put_u32(skb, IFLA_EVENT, event))
			goto nla_put_failure;
	}

1849 1850
	if (rtnl_fill_link_ifmap(skb, dev))
		goto nla_put_failure;
Linus Torvalds's avatar
Linus Torvalds committed
1851 1852

	if (dev->addr_len) {
1853 1854 1855
		if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
		    nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
			goto nla_put_failure;
Linus Torvalds's avatar
Linus Torvalds committed
1856 1857
	}

1858 1859 1860
	if (rtnl_phys_port_id_fill(skb, dev))
		goto nla_put_failure;

1861 1862 1863
	if (rtnl_phys_port_name_fill(skb, dev))
		goto nla_put_failure;

1864 1865 1866
	if (rtnl_phys_switch_id_fill(skb, dev))
		goto nla_put_failure;

1867
	if (rtnl_fill_stats(skb, dev))
1868 1869
		goto nla_put_failure;

1870
	if (rtnl_fill_vf(skb, dev, ext_filter_mask))
1871
		goto nla_put_failure;
1872

1873
	if (rtnl_port_fill(skb, dev, ext_filter_mask))
1874 1875
		goto nla_put_failure;

1876 1877 1878
	if (rtnl_xdp_fill(skb, dev))
		goto nla_put_failure;

1879
	if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
1880 1881 1882 1883
		if (rtnl_link_fill(skb, dev) < 0)
			goto nla_put_failure;
	}

1884
	if (rtnl_fill_link_netnsid(skb, dev, src_net, gfp))
1885
		goto nla_put_failure;
1886

1887 1888 1889
	if (new_nsid &&
	    nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0)
		goto nla_put_failure;
1890 1891 1892 1893
	if (new_ifindex &&
	    nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0)
		goto nla_put_failure;

1894 1895 1896
	if (memchr_inv(dev->perm_addr, '\0', dev->addr_len) &&
	    nla_put(skb, IFLA_PERM_ADDRESS, dev->addr_len, dev->perm_addr))
		goto nla_put_failure;
1897

1898
	rcu_read_lock();
1899
	if (rtnl_fill_link_af(skb, dev, ext_filter_mask))
1900 1901
		goto nla_put_failure_rcu;
	rcu_read_unlock();
1902

1903 1904 1905
	if (rtnl_fill_prop_list(skb, dev))
		goto nla_put_failure;

1906 1907 1908 1909 1910 1911 1912 1913 1914 1915
	if (dev->dev.parent &&
	    nla_put_string(skb, IFLA_PARENT_DEV_NAME,
			   dev_name(dev->dev.parent)))
		goto nla_put_failure;

	if (dev->dev.parent && dev->dev.parent->bus &&
	    nla_put_string(skb, IFLA_PARENT_DEV_BUS_NAME,
			   dev->dev.parent->bus->name))
		goto nla_put_failure;

1916 1917 1918
	if (rtnl_fill_devlink_port(skb, dev))
		goto nla_put_failure;

1919 1920
	nlmsg_end(skb, nlh);
	return 0;
1921

1922 1923
nla_put_failure_rcu:
	rcu_read_unlock();
1924
nla_put_failure:
1925 1926
	nlmsg_cancel(skb, nlh);
	return -EMSGSIZE;
Linus Torvalds's avatar
Linus Torvalds committed
1927 1928
}

Jiri Pirko's avatar
Jiri Pirko committed
1929
static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1930
	[IFLA_IFNAME]		= { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1931 1932
	[IFLA_ADDRESS]		= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
	[IFLA_BROADCAST]	= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1933
	[IFLA_MAP]		= { .len = sizeof(struct rtnl_link_ifmap) },
1934
	[IFLA_MTU]		= { .type = NLA_U32 },
1935
	[IFLA_LINK]		= { .type = NLA_U32 },
1936
	[IFLA_MASTER]		= { .type = NLA_U32 },
1937
	[IFLA_CARRIER]		= { .type = NLA_U8 },
1938 1939 1940 1941
	[IFLA_TXQLEN]		= { .type = NLA_U32 },
	[IFLA_WEIGHT]		= { .type = NLA_U32 },
	[IFLA_OPERSTATE]	= { .type = NLA_U8 },
	[IFLA_LINKMODE]		= { .type = NLA_U8 },
1942
	[IFLA_LINKINFO]		= { .type = NLA_NESTED },
1943
	[IFLA_NET_NS_PID]	= { .type = NLA_U32 },
1944
	[IFLA_NET_NS_FD]	= { .type = NLA_U32 },
1945 1946 1947 1948
	/* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
	 * allow 0-length string (needed to remove an alias).
	 */
	[IFLA_IFALIAS]	        = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
1949
	[IFLA_VFINFO_LIST]	= {. type = NLA_NESTED },
1950 1951
	[IFLA_VF_PORTS]		= { .type = NLA_NESTED },
	[IFLA_PORT_SELF]	= { .type = NLA_NESTED },
1952
	[IFLA_AF_SPEC]		= { .type = NLA_NESTED },
1953
	[IFLA_EXT_MASK]		= { .type = NLA_U32 },
1954
	[IFLA_PROMISCUITY]	= { .type = NLA_U32 },
1955 1956
	[IFLA_NUM_TX_QUEUES]	= { .type = NLA_U32 },
	[IFLA_NUM_RX_QUEUES]	= { .type = NLA_U32 },
1957 1958
	[IFLA_GSO_MAX_SEGS]	= { .type = NLA_U32 },
	[IFLA_GSO_MAX_SIZE]	= { .type = NLA_U32 },
1959
	[IFLA_PHYS_PORT_ID]	= { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1960
	[IFLA_CARRIER_CHANGES]	= { .type = NLA_U32 },  /* ignored */
1961
	[IFLA_PHYS_SWITCH_ID]	= { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1962
	[IFLA_LINK_NETNSID]	= { .type = NLA_S32 },
1963
	[IFLA_PROTO_DOWN]	= { .type = NLA_U8 },
1964
	[IFLA_XDP]		= { .type = NLA_NESTED },
1965
	[IFLA_EVENT]		= { .type = NLA_U32 },
1966
	[IFLA_GROUP]		= { .type = NLA_U32 },
1967
	[IFLA_TARGET_NETNSID]	= { .type = NLA_S32 },
1968 1969
	[IFLA_CARRIER_UP_COUNT]	= { .type = NLA_U32 },
	[IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 },
1970 1971
	[IFLA_MIN_MTU]		= { .type = NLA_U32 },
	[IFLA_MAX_MTU]		= { .type = NLA_U32 },
1972 1973 1974
	[IFLA_PROP_LIST]	= { .type = NLA_NESTED },
	[IFLA_ALT_IFNAME]	= { .type = NLA_STRING,
				    .len = ALTIFNAMSIZ - 1 },
1975
	[IFLA_PERM_ADDRESS]	= { .type = NLA_REJECT },
1976
	[IFLA_PROTO_DOWN_REASON] = { .type = NLA_NESTED },
1977
	[IFLA_NEW_IFINDEX]	= NLA_POLICY_MIN(NLA_S32, 1),
1978
	[IFLA_PARENT_DEV_NAME]	= { .type = NLA_NUL_STRING },
1979
	[IFLA_GRO_MAX_SIZE]	= { .type = NLA_U32 },
1980 1981
	[IFLA_TSO_MAX_SIZE]	= { .type = NLA_REJECT },
	[IFLA_TSO_MAX_SEGS]	= { .type = NLA_REJECT },
1982
	[IFLA_ALLMULTI]		= { .type = NLA_REJECT },
1983 1984
	[IFLA_GSO_IPV4_MAX_SIZE]	= { .type = NLA_U32 },
	[IFLA_GRO_IPV4_MAX_SIZE]	= { .type = NLA_U32 },
1985 1986
};

1987 1988 1989
static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
	[IFLA_INFO_KIND]	= { .type = NLA_STRING },
	[IFLA_INFO_DATA]	= { .type = NLA_NESTED },
1990 1991
	[IFLA_INFO_SLAVE_KIND]	= { .type = NLA_STRING },
	[IFLA_INFO_SLAVE_DATA]	= { .type = NLA_NESTED },
1992 1993
};

1994
static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1995
	[IFLA_VF_MAC]		= { .len = sizeof(struct ifla_vf_mac) },
1996
	[IFLA_VF_BROADCAST]	= { .type = NLA_REJECT },
1997
	[IFLA_VF_VLAN]		= { .len = sizeof(struct ifla_vf_vlan) },
1998
	[IFLA_VF_VLAN_LIST]     = { .type = NLA_NESTED },
1999 2000 2001 2002
	[IFLA_VF_TX_RATE]	= { .len = sizeof(struct ifla_vf_tx_rate) },
	[IFLA_VF_SPOOFCHK]	= { .len = sizeof(struct ifla_vf_spoofchk) },
	[IFLA_VF_RATE]		= { .len = sizeof(struct ifla_vf_rate) },
	[IFLA_VF_LINK_STATE]	= { .len = sizeof(struct ifla_vf_link_state) },
2003
	[IFLA_VF_RSS_QUERY_EN]	= { .len = sizeof(struct ifla_vf_rss_query_en) },
2004
	[IFLA_VF_STATS]		= { .type = NLA_NESTED },
2005
	[IFLA_VF_TRUST]		= { .len = sizeof(struct ifla_vf_trust) },
2006 2007
	[IFLA_VF_IB_NODE_GUID]	= { .len = sizeof(struct ifla_vf_guid) },
	[IFLA_VF_IB_PORT_GUID]	= { .len = sizeof(struct ifla_vf_guid) },
2008 2009
};

2010 2011 2012 2013 2014 2015 2016 2017 2018 2019
static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
	[IFLA_PORT_VF]		= { .type = NLA_U32 },
	[IFLA_PORT_PROFILE]	= { .type = NLA_STRING,
				    .len = PORT_PROFILE_MAX },
	[IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
				      .len = PORT_UUID_MAX },
	[IFLA_PORT_HOST_UUID]	= { .type = NLA_STRING,
				    .len = PORT_UUID_MAX },
	[IFLA_PORT_REQUEST]	= { .type = NLA_U8, },
	[IFLA_PORT_RESPONSE]	= { .type = NLA_U16, },
2020 2021 2022 2023 2024 2025 2026

	/* Unused, but we need to keep it here since user space could
	 * fill it. It's also broken with regard to NLA_BINARY use in
	 * combination with structs.
	 */
	[IFLA_PORT_VSI_TYPE]	= { .type = NLA_BINARY,
				    .len = sizeof(struct ifla_port_vsi) },
2027 2028
};

2029
static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
2030
	[IFLA_XDP_UNSPEC]	= { .strict_start_type = IFLA_XDP_EXPECTED_FD },
2031
	[IFLA_XDP_FD]		= { .type = NLA_S32 },
2032
	[IFLA_XDP_EXPECTED_FD]	= { .type = NLA_S32 },
2033
	[IFLA_XDP_ATTACHED]	= { .type = NLA_U8 },
2034
	[IFLA_XDP_FLAGS]	= { .type = NLA_U32 },
2035
	[IFLA_XDP_PROG_ID]	= { .type = NLA_U32 },
2036 2037
};

2038 2039 2040 2041 2042
static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
{
	const struct rtnl_link_ops *ops = NULL;
	struct nlattr *linfo[IFLA_INFO_MAX + 1];

2043
	if (nla_parse_nested_deprecated(linfo, IFLA_INFO_MAX, nla, ifla_info_policy, NULL) < 0)
2044 2045 2046 2047 2048
		return NULL;

	if (linfo[IFLA_INFO_KIND]) {
		char kind[MODULE_NAME_LEN];

2049
		nla_strscpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063
		ops = rtnl_link_ops_get(kind);
	}

	return ops;
}

static bool link_master_filtered(struct net_device *dev, int master_idx)
{
	struct net_device *master;

	if (!master_idx)
		return false;

	master = netdev_master_upper_dev_get(dev);
2064 2065 2066 2067 2068 2069 2070

	/* 0 is already used to denote IFLA_MASTER wasn't passed, therefore need
	 * another invalid value for ifindex to denote "no master".
	 */
	if (master_idx == -1)
		return !!master;

2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096
	if (!master || master->ifindex != master_idx)
		return true;

	return false;
}

static bool link_kind_filtered(const struct net_device *dev,
			       const struct rtnl_link_ops *kind_ops)
{
	if (kind_ops && dev->rtnl_link_ops != kind_ops)
		return true;

	return false;
}

static bool link_dump_filtered(struct net_device *dev,
			       int master_idx,
			       const struct rtnl_link_ops *kind_ops)
{
	if (link_master_filtered(dev, master_idx) ||
	    link_kind_filtered(dev, kind_ops))
		return true;

	return false;
}

2097 2098 2099 2100 2101 2102 2103 2104 2105
/**
 * rtnl_get_net_ns_capable - Get netns if sufficiently privileged.
 * @sk: netlink socket
 * @netnsid: network namespace identifier
 *
 * Returns the network namespace identified by netnsid on success or an error
 * pointer on failure.
 */
struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid)
2106 2107 2108
{
	struct net *net;

2109
	net = get_net_ns_by_id(sock_net(sk), netnsid);
2110 2111 2112 2113 2114 2115
	if (!net)
		return ERR_PTR(-EINVAL);

	/* For now, the caller is required to have CAP_NET_ADMIN in
	 * the user namespace owning the target net ns.
	 */
2116
	if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) {
2117 2118 2119 2120 2121
		put_net(net);
		return ERR_PTR(-EACCES);
	}
	return net;
}
2122
EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable);
2123

2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148
static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh,
				      bool strict_check, struct nlattr **tb,
				      struct netlink_ext_ack *extack)
{
	int hdrlen;

	if (strict_check) {
		struct ifinfomsg *ifm;

		if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
			NL_SET_ERR_MSG(extack, "Invalid header for link dump");
			return -EINVAL;
		}

		ifm = nlmsg_data(nlh);
		if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
		    ifm->ifi_change) {
			NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request");
			return -EINVAL;
		}
		if (ifm->ifi_index) {
			NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps");
			return -EINVAL;
		}

2149 2150 2151
		return nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb,
						     IFLA_MAX, ifla_policy,
						     extack);
2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163
	}

	/* A hack to preserve kernel<->userspace interface.
	 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
	 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
	 * what iproute2 < v3.9.0 used.
	 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
	 * attribute, its netlink message is shorter than struct ifinfomsg.
	 */
	hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
		 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);

2164 2165
	return nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy,
				      extack);
2166 2167
}

Jiri Pirko's avatar
Jiri Pirko committed
2168 2169
static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
{
2170 2171
	struct netlink_ext_ack *extack = cb->extack;
	const struct nlmsghdr *nlh = cb->nlh;
Jiri Pirko's avatar
Jiri Pirko committed
2172
	struct net *net = sock_net(skb->sk);
2173
	struct net *tgt_net = net;
Jiri Pirko's avatar
Jiri Pirko committed
2174 2175 2176 2177 2178 2179
	int h, s_h;
	int idx = 0, s_idx;
	struct net_device *dev;
	struct hlist_head *head;
	struct nlattr *tb[IFLA_MAX+1];
	u32 ext_filter_mask = 0;
2180 2181 2182
	const struct rtnl_link_ops *kind_ops = NULL;
	unsigned int flags = NLM_F_MULTI;
	int master_idx = 0;
2183
	int netnsid = -1;
2184
	int err, i;
Jiri Pirko's avatar
Jiri Pirko committed
2185 2186 2187 2188

	s_h = cb->args[0];
	s_idx = cb->args[1];

2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199
	err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack);
	if (err < 0) {
		if (cb->strict_check)
			return err;

		goto walk_entries;
	}

	for (i = 0; i <= IFLA_MAX; ++i) {
		if (!tb[i])
			continue;
2200

2201 2202 2203 2204
		/* new attributes should only be added with strict checking */
		switch (i) {
		case IFLA_TARGET_NETNSID:
			netnsid = nla_get_s32(tb[i]);
2205
			tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid);
2206 2207
			if (IS_ERR(tgt_net)) {
				NL_SET_ERR_MSG(extack, "Invalid target network namespace id");
2208
				return PTR_ERR(tgt_net);
2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224
			}
			break;
		case IFLA_EXT_MASK:
			ext_filter_mask = nla_get_u32(tb[i]);
			break;
		case IFLA_MASTER:
			master_idx = nla_get_u32(tb[i]);
			break;
		case IFLA_LINKINFO:
			kind_ops = linkinfo_to_kind_ops(tb[i]);
			break;
		default:
			if (cb->strict_check) {
				NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request");
				return -EINVAL;
			}
2225
		}
Jiri Pirko's avatar
Jiri Pirko committed
2226 2227
	}

2228 2229 2230 2231
	if (master_idx || kind_ops)
		flags |= NLM_F_DUMP_FILTERED;

walk_entries:
Jiri Pirko's avatar
Jiri Pirko committed
2232 2233
	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
		idx = 0;
2234
		head = &tgt_net->dev_index_head[h];
2235
		hlist_for_each_entry(dev, head, index_hlist) {
2236
			if (link_dump_filtered(dev, master_idx, kind_ops))
2237
				goto cont;
Jiri Pirko's avatar
Jiri Pirko committed
2238 2239
			if (idx < s_idx)
				goto cont;
2240 2241
			err = rtnl_fill_ifinfo(skb, dev, net,
					       RTM_NEWLINK,
2242
					       NETLINK_CB(cb->skb).portid,
2243
					       nlh->nlmsg_seq, 0, flags,
2244
					       ext_filter_mask, 0, NULL, 0,
2245
					       netnsid, GFP_KERNEL);
2246

2247 2248 2249 2250 2251 2252
			if (err < 0) {
				if (likely(skb->len))
					goto out;

				goto out_err;
			}
Jiri Pirko's avatar
Jiri Pirko committed
2253 2254 2255 2256 2257
cont:
			idx++;
		}
	}
out:
2258 2259
	err = skb->len;
out_err:
Jiri Pirko's avatar
Jiri Pirko committed
2260 2261
	cb->args[1] = idx;
	cb->args[0] = h;
2262
	cb->seq = tgt_net->dev_base_seq;
2263
	nl_dump_check_consistent(cb, nlmsg_hdr(skb));
2264 2265
	if (netnsid >= 0)
		put_net(tgt_net);
Jiri Pirko's avatar
Jiri Pirko committed
2266

2267
	return err;
Jiri Pirko's avatar
Jiri Pirko committed
2268 2269
}

2270 2271
int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
			struct netlink_ext_ack *exterr)
Jiri Pirko's avatar
Jiri Pirko committed
2272
{
2273 2274
	return nla_parse_deprecated(tb, IFLA_MAX, head, len, ifla_policy,
				    exterr);
Jiri Pirko's avatar
Jiri Pirko committed
2275 2276 2277
}
EXPORT_SYMBOL(rtnl_nla_parse_ifla);

2278 2279 2280 2281 2282 2283 2284 2285
struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
{
	struct net *net;
	/* Examine the link attributes and figure out which
	 * network namespace we are talking about.
	 */
	if (tb[IFLA_NET_NS_PID])
		net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
2286 2287
	else if (tb[IFLA_NET_NS_FD])
		net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
2288 2289 2290 2291 2292 2293
	else
		net = get_net(src_net);
	return net;
}
EXPORT_SYMBOL(rtnl_link_get_net);

2294 2295 2296 2297 2298
/* Figure out which network namespace we are talking about by
 * examining the link attributes in the following order:
 *
 * 1. IFLA_NET_NS_PID
 * 2. IFLA_NET_NS_FD
2299
 * 3. IFLA_TARGET_NETNSID
2300 2301 2302 2303 2304 2305 2306 2307 2308
 */
static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net,
					       struct nlattr *tb[])
{
	struct net *net;

	if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])
		return rtnl_link_get_net(src_net, tb);

2309
	if (!tb[IFLA_TARGET_NETNSID])
2310 2311
		return get_net(src_net);

2312
	net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID]));
2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336
	if (!net)
		return ERR_PTR(-EINVAL);

	return net;
}

static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb,
					     struct net *src_net,
					     struct nlattr *tb[], int cap)
{
	struct net *net;

	net = rtnl_link_get_net_by_nlattr(src_net, tb);
	if (IS_ERR(net))
		return net;

	if (!netlink_ns_capable(skb, net->user_ns, cap)) {
		put_net(net);
		return ERR_PTR(-EPERM);
	}

	return net;
}

2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352
/* Verify that rtnetlink requests do not pass additional properties
 * potentially referring to different network namespaces.
 */
static int rtnl_ensure_unique_netns(struct nlattr *tb[],
				    struct netlink_ext_ack *extack,
				    bool netns_id_only)
{

	if (netns_id_only) {
		if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD])
			return 0;

		NL_SET_ERR_MSG(extack, "specified netns attribute not supported");
		return -EOPNOTSUPP;
	}

2353
	if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]))
2354 2355
		goto invalid_attr;

2356
	if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD]))
2357 2358
		goto invalid_attr;

2359
	if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID]))
2360 2361 2362 2363 2364 2365 2366 2367 2368
		goto invalid_attr;

	return 0;

invalid_attr:
	NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified");
	return -EINVAL;
}

2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381
static	int rtnl_set_vf_rate(struct net_device *dev, int vf, int min_tx_rate,
			     int max_tx_rate)
{
	const struct net_device_ops *ops = dev->netdev_ops;

	if (!ops->ndo_set_vf_rate)
		return -EOPNOTSUPP;
	if (max_tx_rate && max_tx_rate < min_tx_rate)
		return -EINVAL;

	return ops->ndo_set_vf_rate(dev, vf, min_tx_rate, max_tx_rate);
}

2382 2383
static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[],
			    struct netlink_ext_ack *extack)
2384
{
2385 2386 2387
	if (tb[IFLA_ADDRESS] &&
	    nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
		return -EINVAL;
2388

2389 2390 2391
	if (tb[IFLA_BROADCAST] &&
	    nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
		return -EINVAL;
2392

2393 2394 2395 2396 2397
	if (tb[IFLA_GSO_MAX_SIZE] &&
	    nla_get_u32(tb[IFLA_GSO_MAX_SIZE]) > dev->tso_max_size) {
		NL_SET_ERR_MSG(extack, "too big gso_max_size");
		return -EINVAL;
	}
2398

2399 2400 2401 2402 2403 2404
	if (tb[IFLA_GSO_MAX_SEGS] &&
	    (nla_get_u32(tb[IFLA_GSO_MAX_SEGS]) > GSO_MAX_SEGS ||
	     nla_get_u32(tb[IFLA_GSO_MAX_SEGS]) > dev->tso_max_segs)) {
		NL_SET_ERR_MSG(extack, "too big gso_max_segs");
		return -EINVAL;
	}
2405

2406 2407 2408 2409 2410
	if (tb[IFLA_GRO_MAX_SIZE] &&
	    nla_get_u32(tb[IFLA_GRO_MAX_SIZE]) > GRO_MAX_SIZE) {
		NL_SET_ERR_MSG(extack, "too big gro_max_size");
		return -EINVAL;
	}
2411

2412 2413 2414 2415 2416
	if (tb[IFLA_GSO_IPV4_MAX_SIZE] &&
	    nla_get_u32(tb[IFLA_GSO_IPV4_MAX_SIZE]) > dev->tso_max_size) {
		NL_SET_ERR_MSG(extack, "too big gso_ipv4_max_size");
		return -EINVAL;
	}
2417

2418 2419 2420 2421
	if (tb[IFLA_GRO_IPV4_MAX_SIZE] &&
	    nla_get_u32(tb[IFLA_GRO_IPV4_MAX_SIZE]) > GRO_MAX_SIZE) {
		NL_SET_ERR_MSG(extack, "too big gro_ipv4_max_size");
		return -EINVAL;
2422 2423
	}

2424 2425 2426 2427 2428 2429 2430
	if (tb[IFLA_AF_SPEC]) {
		struct nlattr *af;
		int rem, err;

		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
			const struct rtnl_af_ops *af_ops;

2431
			af_ops = rtnl_af_lookup(nla_type(af));
2432
			if (!af_ops)
2433 2434
				return -EAFNOSUPPORT;

2435
			if (!af_ops->set_link_af)
2436 2437 2438
				return -EOPNOTSUPP;

			if (af_ops->validate_link_af) {
2439
				err = af_ops->validate_link_af(dev, af, extack);
2440
				if (err < 0)
2441 2442 2443 2444 2445
					return err;
			}
		}
	}

2446 2447 2448
	return 0;
}

2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464
static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
				  int guid_type)
{
	const struct net_device_ops *ops = dev->netdev_ops;

	return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
}

static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
{
	if (dev->type != ARPHRD_INFINIBAND)
		return -EOPNOTSUPP;

	return handle_infiniband_guid(dev, ivt, guid_type);
}

2465
static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
2466 2467
{
	const struct net_device_ops *ops = dev->netdev_ops;
2468
	int err = -EINVAL;
2469

2470 2471
	if (tb[IFLA_VF_MAC]) {
		struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
2472

2473 2474
		if (ivm->vf >= INT_MAX)
			return -EINVAL;
2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485
		err = -EOPNOTSUPP;
		if (ops->ndo_set_vf_mac)
			err = ops->ndo_set_vf_mac(dev, ivm->vf,
						  ivm->mac);
		if (err < 0)
			return err;
	}

	if (tb[IFLA_VF_VLAN]) {
		struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);

2486 2487
		if (ivv->vf >= INT_MAX)
			return -EINVAL;
2488 2489 2490
		err = -EOPNOTSUPP;
		if (ops->ndo_set_vf_vlan)
			err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516
						   ivv->qos,
						   htons(ETH_P_8021Q));
		if (err < 0)
			return err;
	}

	if (tb[IFLA_VF_VLAN_LIST]) {
		struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
		struct nlattr *attr;
		int rem, len = 0;

		err = -EOPNOTSUPP;
		if (!ops->ndo_set_vf_vlan)
			return err;

		nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
			if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
			    nla_len(attr) < NLA_HDRLEN) {
				return -EINVAL;
			}
			if (len >= MAX_VLAN_LIST_LEN)
				return -EOPNOTSUPP;
			ivvl[len] = nla_data(attr);

			len++;
		}
2517 2518 2519
		if (len == 0)
			return -EINVAL;

2520 2521
		if (ivvl[0]->vf >= INT_MAX)
			return -EINVAL;
2522 2523
		err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
					   ivvl[0]->qos, ivvl[0]->vlan_proto);
2524 2525
		if (err < 0)
			return err;
2526
	}
2527 2528 2529 2530 2531

	if (tb[IFLA_VF_TX_RATE]) {
		struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
		struct ifla_vf_info ivf;

2532 2533
		if (ivt->vf >= INT_MAX)
			return -EINVAL;
2534 2535 2536 2537 2538 2539
		err = -EOPNOTSUPP;
		if (ops->ndo_get_vf_config)
			err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
		if (err < 0)
			return err;

2540 2541
		err = rtnl_set_vf_rate(dev, ivt->vf,
				       ivf.min_tx_rate, ivt->rate);
2542 2543 2544 2545 2546 2547 2548
		if (err < 0)
			return err;
	}

	if (tb[IFLA_VF_RATE]) {
		struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);

2549 2550
		if (ivt->vf >= INT_MAX)
			return -EINVAL;
2551 2552 2553

		err = rtnl_set_vf_rate(dev, ivt->vf,
				       ivt->min_tx_rate, ivt->max_tx_rate);
2554 2555 2556 2557 2558 2559 2560
		if (err < 0)
			return err;
	}

	if (tb[IFLA_VF_SPOOFCHK]) {
		struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);

2561 2562
		if (ivs->vf >= INT_MAX)
			return -EINVAL;
2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573
		err = -EOPNOTSUPP;
		if (ops->ndo_set_vf_spoofchk)
			err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
						       ivs->setting);
		if (err < 0)
			return err;
	}

	if (tb[IFLA_VF_LINK_STATE]) {
		struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);

2574 2575
		if (ivl->vf >= INT_MAX)
			return -EINVAL;
2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588
		err = -EOPNOTSUPP;
		if (ops->ndo_set_vf_link_state)
			err = ops->ndo_set_vf_link_state(dev, ivl->vf,
							 ivl->link_state);
		if (err < 0)
			return err;
	}

	if (tb[IFLA_VF_RSS_QUERY_EN]) {
		struct ifla_vf_rss_query_en *ivrssq_en;

		err = -EOPNOTSUPP;
		ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
2589 2590
		if (ivrssq_en->vf >= INT_MAX)
			return -EINVAL;
2591 2592 2593 2594 2595 2596 2597
		if (ops->ndo_set_vf_rss_query_en)
			err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
							   ivrssq_en->setting);
		if (err < 0)
			return err;
	}

2598 2599 2600
	if (tb[IFLA_VF_TRUST]) {
		struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);

2601 2602
		if (ivt->vf >= INT_MAX)
			return -EINVAL;
2603 2604 2605 2606 2607 2608 2609
		err = -EOPNOTSUPP;
		if (ops->ndo_set_vf_trust)
			err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
		if (err < 0)
			return err;
	}

2610 2611 2612
	if (tb[IFLA_VF_IB_NODE_GUID]) {
		struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);

2613 2614
		if (ivt->vf >= INT_MAX)
			return -EINVAL;
2615 2616 2617 2618 2619 2620 2621 2622
		if (!ops->ndo_set_vf_guid)
			return -EOPNOTSUPP;
		return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
	}

	if (tb[IFLA_VF_IB_PORT_GUID]) {
		struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);

2623 2624
		if (ivt->vf >= INT_MAX)
			return -EINVAL;
2625 2626 2627 2628 2629 2630
		if (!ops->ndo_set_vf_guid)
			return -EOPNOTSUPP;

		return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
	}

2631 2632 2633
	return err;
}

2634 2635
static int do_set_master(struct net_device *dev, int ifindex,
			 struct netlink_ext_ack *extack)
2636
{
2637
	struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
2638 2639 2640
	const struct net_device_ops *ops;
	int err;

2641 2642
	if (upper_dev) {
		if (upper_dev->ifindex == ifindex)
2643
			return 0;
2644
		ops = upper_dev->netdev_ops;
2645
		if (ops->ndo_del_slave) {
2646
			err = ops->ndo_del_slave(upper_dev, dev);
2647 2648 2649 2650 2651 2652 2653 2654
			if (err)
				return err;
		} else {
			return -EOPNOTSUPP;
		}
	}

	if (ifindex) {
2655 2656
		upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
		if (!upper_dev)
2657
			return -EINVAL;
2658
		ops = upper_dev->netdev_ops;
2659
		if (ops->ndo_add_slave) {
2660
			err = ops->ndo_add_slave(upper_dev, dev, extack);
2661 2662 2663 2664 2665 2666 2667 2668 2669
			if (err)
				return err;
		} else {
			return -EOPNOTSUPP;
		}
	}
	return 0;
}

2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685
static const struct nla_policy ifla_proto_down_reason_policy[IFLA_PROTO_DOWN_REASON_VALUE + 1] = {
	[IFLA_PROTO_DOWN_REASON_MASK]	= { .type = NLA_U32 },
	[IFLA_PROTO_DOWN_REASON_VALUE]	= { .type = NLA_U32 },
};

static int do_set_proto_down(struct net_device *dev,
			     struct nlattr *nl_proto_down,
			     struct nlattr *nl_proto_down_reason,
			     struct netlink_ext_ack *extack)
{
	struct nlattr *pdreason[IFLA_PROTO_DOWN_REASON_MAX + 1];
	unsigned long mask = 0;
	u32 value;
	bool proto_down;
	int err;

2686
	if (!(dev->priv_flags & IFF_CHANGE_PROTO_DOWN)) {
2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715
		NL_SET_ERR_MSG(extack,  "Protodown not supported by device");
		return -EOPNOTSUPP;
	}

	if (nl_proto_down_reason) {
		err = nla_parse_nested_deprecated(pdreason,
						  IFLA_PROTO_DOWN_REASON_MAX,
						  nl_proto_down_reason,
						  ifla_proto_down_reason_policy,
						  NULL);
		if (err < 0)
			return err;

		if (!pdreason[IFLA_PROTO_DOWN_REASON_VALUE]) {
			NL_SET_ERR_MSG(extack, "Invalid protodown reason value");
			return -EINVAL;
		}

		value = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_VALUE]);

		if (pdreason[IFLA_PROTO_DOWN_REASON_MASK])
			mask = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_MASK]);

		dev_change_proto_down_reason(dev, mask, value);
	}

	if (nl_proto_down) {
		proto_down = nla_get_u8(nl_proto_down);

2716
		/* Don't turn off protodown if there are active reasons */
2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729
		if (!proto_down && dev->proto_down_reason) {
			NL_SET_ERR_MSG(extack, "Cannot clear protodown, active reasons");
			return -EBUSY;
		}
		err = dev_change_proto_down(dev,
					    proto_down);
		if (err)
			return err;
	}

	return 0;
}

2730
#define DO_SETLINK_MODIFIED	0x01
2731 2732
/* notify flag means notify + modified. */
#define DO_SETLINK_NOTIFY	0x03
2733 2734
static int do_setlink(const struct sk_buff *skb,
		      struct net_device *dev, struct ifinfomsg *ifm,
2735
		      struct netlink_ext_ack *extack,
2736
		      struct nlattr **tb, int status)
Linus Torvalds's avatar
Linus Torvalds committed
2737
{
2738
	const struct net_device_ops *ops = dev->netdev_ops;
2739
	char ifname[IFNAMSIZ];
2740
	int err;
Linus Torvalds's avatar
Linus Torvalds committed
2741

2742 2743 2744 2745 2746
	if (tb[IFLA_IFNAME])
		nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
	else
		ifname[0] = '\0';

2747
	if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) {
2748
		const char *pat = ifname[0] ? ifname : NULL;
2749 2750 2751 2752 2753
		struct net *net;
		int new_ifindex;

		net = rtnl_link_get_net_capable(skb, dev_net(dev),
						tb, CAP_NET_ADMIN);
2754 2755 2756 2757
		if (IS_ERR(net)) {
			err = PTR_ERR(net);
			goto errout;
		}
2758

2759 2760 2761 2762 2763
		if (tb[IFLA_NEW_IFINDEX])
			new_ifindex = nla_get_s32(tb[IFLA_NEW_IFINDEX]);
		else
			new_ifindex = 0;

2764
		err = __dev_change_net_namespace(dev, net, pat, new_ifindex);
2765 2766 2767
		put_net(net);
		if (err)
			goto errout;
2768
		status |= DO_SETLINK_MODIFIED;
2769 2770
	}

2771
	if (tb[IFLA_MAP]) {
Linus Torvalds's avatar
Linus Torvalds committed
2772 2773 2774
		struct rtnl_link_ifmap *u_map;
		struct ifmap k_map;

2775
		if (!ops->ndo_set_config) {
Linus Torvalds's avatar
Linus Torvalds committed
2776
			err = -EOPNOTSUPP;
2777
			goto errout;
Linus Torvalds's avatar
Linus Torvalds committed
2778 2779 2780 2781
		}

		if (!netif_device_present(dev)) {
			err = -ENODEV;
2782
			goto errout;
Linus Torvalds's avatar
Linus Torvalds committed
2783 2784
		}

2785
		u_map = nla_data(tb[IFLA_MAP]);
Linus Torvalds's avatar
Linus Torvalds committed
2786 2787 2788 2789 2790 2791 2792
		k_map.mem_start = (unsigned long) u_map->mem_start;
		k_map.mem_end = (unsigned long) u_map->mem_end;
		k_map.base_addr = (unsigned short) u_map->base_addr;
		k_map.irq = (unsigned char) u_map->irq;
		k_map.dma = (unsigned char) u_map->dma;
		k_map.port = (unsigned char) u_map->port;

2793
		err = ops->ndo_set_config(dev, &k_map);
2794
		if (err < 0)
2795
			goto errout;
Linus Torvalds's avatar
Linus Torvalds committed
2796

2797
		status |= DO_SETLINK_NOTIFY;
Linus Torvalds's avatar
Linus Torvalds committed
2798 2799
	}

2800
	if (tb[IFLA_ADDRESS]) {
2801 2802 2803
		struct sockaddr *sa;
		int len;

2804 2805
		len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
						  sizeof(*sa));
2806 2807 2808
		sa = kmalloc(len, GFP_KERNEL);
		if (!sa) {
			err = -ENOMEM;
2809
			goto errout;
2810 2811
		}
		sa->sa_family = dev->type;
2812
		memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
2813
		       dev->addr_len);
2814
		err = dev_set_mac_address_user(dev, sa, extack);
2815
		kfree(sa);
Linus Torvalds's avatar
Linus Torvalds committed
2816
		if (err)
2817
			goto errout;
2818
		status |= DO_SETLINK_MODIFIED;
Linus Torvalds's avatar
Linus Torvalds committed
2819 2820
	}

2821
	if (tb[IFLA_MTU]) {
2822
		err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack);
2823
		if (err < 0)
2824
			goto errout;
2825
		status |= DO_SETLINK_MODIFIED;
Linus Torvalds's avatar
Linus Torvalds committed
2826 2827
	}

2828 2829
	if (tb[IFLA_GROUP]) {
		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2830
		status |= DO_SETLINK_NOTIFY;
2831 2832
	}

2833 2834 2835 2836 2837
	/*
	 * Interface selected by interface index but interface
	 * name provided implies that a name change has been
	 * requested.
	 */
2838
	if (ifm->ifi_index > 0 && ifname[0]) {
2839 2840
		err = dev_change_name(dev, ifname);
		if (err < 0)
2841
			goto errout;
2842
		status |= DO_SETLINK_MODIFIED;
Linus Torvalds's avatar
Linus Torvalds committed
2843 2844
	}

2845 2846 2847 2848 2849
	if (tb[IFLA_IFALIAS]) {
		err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
				    nla_len(tb[IFLA_IFALIAS]));
		if (err < 0)
			goto errout;
2850
		status |= DO_SETLINK_NOTIFY;
2851 2852
	}

2853 2854
	if (tb[IFLA_BROADCAST]) {
		nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
2855
		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Linus Torvalds's avatar
Linus Torvalds committed
2856 2857
	}

2858
	if (tb[IFLA_MASTER]) {
2859
		err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
2860 2861
		if (err)
			goto errout;
2862
		status |= DO_SETLINK_MODIFIED;
2863 2864
	}

2865 2866 2867 2868 2869 2870 2871
	if (ifm->ifi_flags || ifm->ifi_change) {
		err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
				       extack);
		if (err < 0)
			goto errout;
	}

2872 2873 2874 2875
	if (tb[IFLA_CARRIER]) {
		err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
		if (err)
			goto errout;
2876
		status |= DO_SETLINK_MODIFIED;
2877 2878
	}

2879
	if (tb[IFLA_TXQLEN]) {
2880
		unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
2881 2882 2883 2884 2885

		err = dev_change_tx_queue_len(dev, value);
		if (err)
			goto errout;
		status |= DO_SETLINK_MODIFIED;
2886
	}
2887

2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900
	if (tb[IFLA_GSO_MAX_SIZE]) {
		u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]);

		if (dev->gso_max_size ^ max_size) {
			netif_set_gso_max_size(dev, max_size);
			status |= DO_SETLINK_MODIFIED;
		}
	}

	if (tb[IFLA_GSO_MAX_SEGS]) {
		u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);

		if (dev->gso_max_segs ^ max_segs) {
2901
			netif_set_gso_max_segs(dev, max_segs);
2902 2903 2904 2905
			status |= DO_SETLINK_MODIFIED;
		}
	}

2906 2907 2908 2909 2910 2911 2912 2913 2914
	if (tb[IFLA_GRO_MAX_SIZE]) {
		u32 gro_max_size = nla_get_u32(tb[IFLA_GRO_MAX_SIZE]);

		if (dev->gro_max_size ^ gro_max_size) {
			netif_set_gro_max_size(dev, gro_max_size);
			status |= DO_SETLINK_MODIFIED;
		}
	}

2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932
	if (tb[IFLA_GSO_IPV4_MAX_SIZE]) {
		u32 max_size = nla_get_u32(tb[IFLA_GSO_IPV4_MAX_SIZE]);

		if (dev->gso_ipv4_max_size ^ max_size) {
			netif_set_gso_ipv4_max_size(dev, max_size);
			status |= DO_SETLINK_MODIFIED;
		}
	}

	if (tb[IFLA_GRO_IPV4_MAX_SIZE]) {
		u32 gro_max_size = nla_get_u32(tb[IFLA_GRO_IPV4_MAX_SIZE]);

		if (dev->gro_ipv4_max_size ^ gro_max_size) {
			netif_set_gro_ipv4_max_size(dev, gro_max_size);
			status |= DO_SETLINK_MODIFIED;
		}
	}

2933
	if (tb[IFLA_OPERSTATE])
2934
		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
2935

2936
	if (tb[IFLA_LINKMODE]) {
2937 2938
		unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);

2939
		write_lock(&dev_base_lock);
2940
		if (dev->link_mode ^ value)
2941
			status |= DO_SETLINK_NOTIFY;
2942
		dev->link_mode = value;
2943
		write_unlock(&dev_base_lock);
2944 2945
	}

2946
	if (tb[IFLA_VFINFO_LIST]) {
2947
		struct nlattr *vfinfo[IFLA_VF_MAX + 1];
2948 2949
		struct nlattr *attr;
		int rem;
2950

2951
		nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
2952 2953
			if (nla_type(attr) != IFLA_VF_INFO ||
			    nla_len(attr) < NLA_HDRLEN) {
2954
				err = -EINVAL;
2955
				goto errout;
2956
			}
2957 2958 2959 2960
			err = nla_parse_nested_deprecated(vfinfo, IFLA_VF_MAX,
							  attr,
							  ifla_vf_policy,
							  NULL);
2961 2962 2963
			if (err < 0)
				goto errout;
			err = do_setvfinfo(dev, vfinfo);
2964 2965
			if (err < 0)
				goto errout;
2966
			status |= DO_SETLINK_NOTIFY;
2967
		}
2968
	}
Linus Torvalds's avatar
Linus Torvalds committed
2969 2970
	err = 0;

2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981
	if (tb[IFLA_VF_PORTS]) {
		struct nlattr *port[IFLA_PORT_MAX+1];
		struct nlattr *attr;
		int vf;
		int rem;

		err = -EOPNOTSUPP;
		if (!ops->ndo_set_vf_port)
			goto errout;

		nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
2982 2983 2984 2985 2986
			if (nla_type(attr) != IFLA_VF_PORT ||
			    nla_len(attr) < NLA_HDRLEN) {
				err = -EINVAL;
				goto errout;
			}
2987 2988 2989 2990
			err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
							  attr,
							  ifla_port_policy,
							  NULL);
2991 2992 2993 2994 2995 2996 2997 2998 2999 3000
			if (err < 0)
				goto errout;
			if (!port[IFLA_PORT_VF]) {
				err = -EOPNOTSUPP;
				goto errout;
			}
			vf = nla_get_u32(port[IFLA_PORT_VF]);
			err = ops->ndo_set_vf_port(dev, vf, port);
			if (err < 0)
				goto errout;
3001
			status |= DO_SETLINK_NOTIFY;
3002 3003 3004 3005 3006 3007 3008
		}
	}
	err = 0;

	if (tb[IFLA_PORT_SELF]) {
		struct nlattr *port[IFLA_PORT_MAX+1];

3009 3010 3011
		err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
						  tb[IFLA_PORT_SELF],
						  ifla_port_policy, NULL);
3012 3013 3014 3015 3016 3017 3018 3019
		if (err < 0)
			goto errout;

		err = -EOPNOTSUPP;
		if (ops->ndo_set_vf_port)
			err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
		if (err < 0)
			goto errout;
3020
		status |= DO_SETLINK_NOTIFY;
3021
	}
3022 3023 3024 3025 3026 3027 3028 3029

	if (tb[IFLA_AF_SPEC]) {
		struct nlattr *af;
		int rem;

		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
			const struct rtnl_af_ops *af_ops;

3030
			BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
3031

3032
			err = af_ops->set_link_af(dev, af, extack);
3033
			if (err < 0)
3034 3035
				goto errout;

3036
			status |= DO_SETLINK_NOTIFY;
3037 3038
		}
	}
3039 3040
	err = 0;

3041 3042 3043
	if (tb[IFLA_PROTO_DOWN] || tb[IFLA_PROTO_DOWN_REASON]) {
		err = do_set_proto_down(dev, tb[IFLA_PROTO_DOWN],
					tb[IFLA_PROTO_DOWN_REASON], extack);
3044 3045 3046 3047 3048
		if (err)
			goto errout;
		status |= DO_SETLINK_NOTIFY;
	}

3049 3050
	if (tb[IFLA_XDP]) {
		struct nlattr *xdp[IFLA_XDP_MAX + 1];
3051
		u32 xdp_flags = 0;
3052

3053 3054 3055
		err = nla_parse_nested_deprecated(xdp, IFLA_XDP_MAX,
						  tb[IFLA_XDP],
						  ifla_xdp_policy, NULL);
3056 3057 3058
		if (err < 0)
			goto errout;

3059
		if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
3060 3061 3062
			err = -EINVAL;
			goto errout;
		}
3063 3064 3065 3066 3067 3068 3069

		if (xdp[IFLA_XDP_FLAGS]) {
			xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
			if (xdp_flags & ~XDP_FLAGS_MASK) {
				err = -EINVAL;
				goto errout;
			}
3070
			if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
3071 3072 3073
				err = -EINVAL;
				goto errout;
			}
3074 3075
		}

3076
		if (xdp[IFLA_XDP_FD]) {
3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087
			int expected_fd = -1;

			if (xdp_flags & XDP_FLAGS_REPLACE) {
				if (!xdp[IFLA_XDP_EXPECTED_FD]) {
					err = -EINVAL;
					goto errout;
				}
				expected_fd =
					nla_get_s32(xdp[IFLA_XDP_EXPECTED_FD]);
			}

3088
			err = dev_change_xdp_fd(dev, extack,
3089
						nla_get_s32(xdp[IFLA_XDP_FD]),
3090
						expected_fd,
3091
						xdp_flags);
3092 3093 3094 3095 3096 3097
			if (err)
				goto errout;
			status |= DO_SETLINK_NOTIFY;
		}
	}

3098
errout:
3099
	if (status & DO_SETLINK_MODIFIED) {
3100
		if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
3101 3102 3103 3104 3105 3106
			netdev_state_change(dev);

		if (err < 0)
			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",
					     dev->name);
	}
3107

3108 3109
	return err;
}
Linus Torvalds's avatar
Linus Torvalds committed
3110

3111
static struct net_device *rtnl_dev_get(struct net *net,
3112 3113 3114 3115 3116 3117 3118 3119 3120 3121
				       struct nlattr *tb[])
{
	char ifname[ALTIFNAMSIZ];

	if (tb[IFLA_IFNAME])
		nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
	else if (tb[IFLA_ALT_IFNAME])
		nla_strscpy(ifname, tb[IFLA_ALT_IFNAME], ALTIFNAMSIZ);
	else
		return NULL;
3122 3123 3124 3125

	return __dev_get_by_name(net, ifname);
}

3126 3127
static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
			struct netlink_ext_ack *extack)
3128
{
3129
	struct net *net = sock_net(skb->sk);
3130 3131 3132 3133 3134
	struct ifinfomsg *ifm;
	struct net_device *dev;
	int err;
	struct nlattr *tb[IFLA_MAX+1];

3135 3136
	err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
				     ifla_policy, extack);
3137 3138 3139
	if (err < 0)
		goto errout;

3140 3141 3142 3143
	err = rtnl_ensure_unique_netns(tb, extack, false);
	if (err < 0)
		goto errout;

3144 3145 3146
	err = -EINVAL;
	ifm = nlmsg_data(nlh);
	if (ifm->ifi_index > 0)
3147
		dev = __dev_get_by_index(net, ifm->ifi_index);
3148
	else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3149
		dev = rtnl_dev_get(net, tb);
3150 3151 3152 3153 3154 3155 3156 3157
	else
		goto errout;

	if (dev == NULL) {
		err = -ENODEV;
		goto errout;
	}

3158 3159 3160 3161
	err = validate_linkmsg(dev, tb, extack);
	if (err < 0)
		goto errout;

3162
	err = do_setlink(skb, dev, ifm, extack, tb, 0);
3163
errout:
Linus Torvalds's avatar
Linus Torvalds committed
3164 3165 3166
	return err;
}

3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202
static int rtnl_group_dellink(const struct net *net, int group)
{
	struct net_device *dev, *aux;
	LIST_HEAD(list_kill);
	bool found = false;

	if (!group)
		return -EPERM;

	for_each_netdev(net, dev) {
		if (dev->group == group) {
			const struct rtnl_link_ops *ops;

			found = true;
			ops = dev->rtnl_link_ops;
			if (!ops || !ops->dellink)
				return -EOPNOTSUPP;
		}
	}

	if (!found)
		return -ENODEV;

	for_each_netdev_safe(net, dev, aux) {
		if (dev->group == group) {
			const struct rtnl_link_ops *ops;

			ops = dev->rtnl_link_ops;
			ops->dellink(dev, &list_kill);
		}
	}
	unregister_netdevice_many(&list_kill);

	return 0;
}

3203
int rtnl_delete_link(struct net_device *dev, u32 portid, const struct nlmsghdr *nlh)
3204 3205 3206 3207 3208 3209 3210 3211 3212
{
	const struct rtnl_link_ops *ops;
	LIST_HEAD(list_kill);

	ops = dev->rtnl_link_ops;
	if (!ops || !ops->dellink)
		return -EOPNOTSUPP;

	ops->dellink(dev, &list_kill);
3213
	unregister_netdevice_many_notify(&list_kill, portid, nlh);
3214 3215 3216 3217 3218

	return 0;
}
EXPORT_SYMBOL_GPL(rtnl_delete_link);

3219 3220
static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
			struct netlink_ext_ack *extack)
3221
{
3222
	struct net *net = sock_net(skb->sk);
3223
	u32 portid = NETLINK_CB(skb).portid;
3224 3225
	struct net *tgt_net = net;
	struct net_device *dev = NULL;
3226 3227 3228
	struct ifinfomsg *ifm;
	struct nlattr *tb[IFLA_MAX+1];
	int err;
3229
	int netnsid = -1;
3230

3231 3232
	err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
				     ifla_policy, extack);
3233 3234 3235
	if (err < 0)
		return err;

3236 3237 3238 3239
	err = rtnl_ensure_unique_netns(tb, extack, true);
	if (err < 0)
		return err;

3240 3241
	if (tb[IFLA_TARGET_NETNSID]) {
		netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
3242
		tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
3243 3244 3245 3246 3247
		if (IS_ERR(tgt_net))
			return PTR_ERR(tgt_net);
	}

	err = -EINVAL;
3248 3249
	ifm = nlmsg_data(nlh);
	if (ifm->ifi_index > 0)
3250
		dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
3251
	else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3252
		dev = rtnl_dev_get(net, tb);
3253
	else if (tb[IFLA_GROUP])
3254
		err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP]));
3255
	else
3256
		goto out;
3257

3258
	if (!dev) {
3259
		if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME] || ifm->ifi_index > 0)
3260 3261 3262 3263 3264
			err = -ENODEV;

		goto out;
	}

3265
	err = rtnl_delete_link(dev, portid, nlh);
3266

3267 3268 3269 3270 3271
out:
	if (netnsid >= 0)
		put_net(tgt_net);

	return err;
3272 3273
}

3274 3275
int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm,
			u32 portid, const struct nlmsghdr *nlh)
3276 3277 3278 3279 3280 3281
{
	unsigned int old_flags;
	int err;

	old_flags = dev->flags;
	if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
3282 3283
		err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
					 NULL);
3284 3285 3286 3287
		if (err < 0)
			return err;
	}

3288
	if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) {
3289
		__dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags), portid, nlh);
3290 3291
	} else {
		dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
3292
		__dev_notify_flags(dev, old_flags, ~0U, portid, nlh);
3293
	}
3294 3295 3296 3297
	return 0;
}
EXPORT_SYMBOL(rtnl_configure_link);

3298 3299 3300 3301 3302
struct net_device *rtnl_create_link(struct net *net, const char *ifname,
				    unsigned char name_assign_type,
				    const struct rtnl_link_ops *ops,
				    struct nlattr *tb[],
				    struct netlink_ext_ack *extack)
3303 3304
{
	struct net_device *dev;
3305 3306
	unsigned int num_tx_queues = 1;
	unsigned int num_rx_queues = 1;
3307
	int err;
3308

3309 3310 3311
	if (tb[IFLA_NUM_TX_QUEUES])
		num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
	else if (ops->get_num_tx_queues)
3312
		num_tx_queues = ops->get_num_tx_queues();
3313 3314 3315 3316

	if (tb[IFLA_NUM_RX_QUEUES])
		num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
	else if (ops->get_num_rx_queues)
3317
		num_rx_queues = ops->get_num_rx_queues();
3318

3319 3320
	if (num_tx_queues < 1 || num_tx_queues > 4096) {
		NL_SET_ERR_MSG(extack, "Invalid number of transmit queues");
3321
		return ERR_PTR(-EINVAL);
3322
	}
3323

3324 3325
	if (num_rx_queues < 1 || num_rx_queues > 4096) {
		NL_SET_ERR_MSG(extack, "Invalid number of receive queues");
3326
		return ERR_PTR(-EINVAL);
3327
	}
3328

3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339
	if (ops->alloc) {
		dev = ops->alloc(tb, ifname, name_assign_type,
				 num_tx_queues, num_rx_queues);
		if (IS_ERR(dev))
			return dev;
	} else {
		dev = alloc_netdev_mqs(ops->priv_size, ifname,
				       name_assign_type, ops->setup,
				       num_tx_queues, num_rx_queues);
	}

3340
	if (!dev)
3341
		return ERR_PTR(-ENOMEM);
3342

3343 3344 3345 3346 3347 3348
	err = validate_linkmsg(dev, tb, extack);
	if (err < 0) {
		free_netdev(dev);
		return ERR_PTR(err);
	}

3349 3350
	dev_net_set(dev, net);
	dev->rtnl_link_ops = ops;
3351
	dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
3352

3353 3354 3355 3356 3357 3358 3359 3360 3361 3362
	if (tb[IFLA_MTU]) {
		u32 mtu = nla_get_u32(tb[IFLA_MTU]);

		err = dev_validate_mtu(dev, mtu, extack);
		if (err) {
			free_netdev(dev);
			return ERR_PTR(err);
		}
		dev->mtu = mtu;
	}
3363
	if (tb[IFLA_ADDRESS]) {
3364 3365
		__dev_addr_set(dev, nla_data(tb[IFLA_ADDRESS]),
			       nla_len(tb[IFLA_ADDRESS]));
3366 3367
		dev->addr_assign_type = NET_ADDR_SET;
	}
3368 3369 3370 3371 3372 3373
	if (tb[IFLA_BROADCAST])
		memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
				nla_len(tb[IFLA_BROADCAST]));
	if (tb[IFLA_TXQLEN])
		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
	if (tb[IFLA_OPERSTATE])
3374
		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
3375 3376
	if (tb[IFLA_LINKMODE])
		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
3377 3378
	if (tb[IFLA_GROUP])
		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
3379 3380 3381
	if (tb[IFLA_GSO_MAX_SIZE])
		netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE]));
	if (tb[IFLA_GSO_MAX_SEGS])
3382
		netif_set_gso_max_segs(dev, nla_get_u32(tb[IFLA_GSO_MAX_SEGS]));
3383 3384
	if (tb[IFLA_GRO_MAX_SIZE])
		netif_set_gro_max_size(dev, nla_get_u32(tb[IFLA_GRO_MAX_SIZE]));
3385 3386 3387 3388
	if (tb[IFLA_GSO_IPV4_MAX_SIZE])
		netif_set_gso_ipv4_max_size(dev, nla_get_u32(tb[IFLA_GSO_IPV4_MAX_SIZE]));
	if (tb[IFLA_GRO_IPV4_MAX_SIZE])
		netif_set_gro_ipv4_max_size(dev, nla_get_u32(tb[IFLA_GRO_IPV4_MAX_SIZE]));
3389 3390 3391

	return dev;
}
Eric Dumazet's avatar
Eric Dumazet committed
3392
EXPORT_SYMBOL(rtnl_create_link);
3393

3394 3395
static int rtnl_group_changelink(const struct sk_buff *skb,
		struct net *net, int group,
3396
		struct ifinfomsg *ifm,
3397
		struct netlink_ext_ack *extack,
3398 3399
		struct nlattr **tb)
{
3400
	struct net_device *dev, *aux;
3401 3402
	int err;

3403
	for_each_netdev_safe(net, dev, aux) {
3404
		if (dev->group == group) {
3405 3406 3407
			err = validate_linkmsg(dev, tb, extack);
			if (err < 0)
				return err;
3408
			err = do_setlink(skb, dev, ifm, extack, tb, 0);
3409 3410 3411 3412 3413 3414 3415 3416
			if (err < 0)
				return err;
		}
	}

	return 0;
}

3417 3418
static int rtnl_newlink_create(struct sk_buff *skb, struct ifinfomsg *ifm,
			       const struct rtnl_link_ops *ops,
3419
			       const struct nlmsghdr *nlh,
3420
			       struct nlattr **tb, struct nlattr **data,
3421 3422 3423 3424
			       struct netlink_ext_ack *extack)
{
	unsigned char name_assign_type = NET_NAME_USER;
	struct net *net = sock_net(skb->sk);
3425
	u32 portid = NETLINK_CB(skb).portid;
3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478
	struct net *dest_net, *link_net;
	struct net_device *dev;
	char ifname[IFNAMSIZ];
	int err;

	if (!ops->alloc && !ops->setup)
		return -EOPNOTSUPP;

	if (tb[IFLA_IFNAME]) {
		nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
	} else {
		snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
		name_assign_type = NET_NAME_ENUM;
	}

	dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN);
	if (IS_ERR(dest_net))
		return PTR_ERR(dest_net);

	if (tb[IFLA_LINK_NETNSID]) {
		int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);

		link_net = get_net_ns_by_id(dest_net, id);
		if (!link_net) {
			NL_SET_ERR_MSG(extack, "Unknown network namespace id");
			err =  -EINVAL;
			goto out;
		}
		err = -EPERM;
		if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
			goto out;
	} else {
		link_net = NULL;
	}

	dev = rtnl_create_link(link_net ? : dest_net, ifname,
			       name_assign_type, ops, tb, extack);
	if (IS_ERR(dev)) {
		err = PTR_ERR(dev);
		goto out;
	}

	dev->ifindex = ifm->ifi_index;

	if (ops->newlink)
		err = ops->newlink(link_net ? : net, dev, tb, data, extack);
	else
		err = register_netdevice(dev);
	if (err < 0) {
		free_netdev(dev);
		goto out;
	}

3479
	err = rtnl_configure_link(dev, ifm, portid, nlh);
3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507
	if (err < 0)
		goto out_unregister;
	if (link_net) {
		err = dev_change_net_namespace(dev, dest_net, ifname);
		if (err < 0)
			goto out_unregister;
	}
	if (tb[IFLA_MASTER]) {
		err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
		if (err)
			goto out_unregister;
	}
out:
	if (link_net)
		put_net(link_net);
	put_net(dest_net);
	return err;
out_unregister:
	if (ops->newlink) {
		LIST_HEAD(list_kill);

		ops->dellink(dev, &list_kill);
		unregister_netdevice_many(&list_kill);
	} else {
		unregister_netdevice(dev);
	}
	goto out;
}
3508

3509 3510 3511 3512 3513 3514
struct rtnl_newlink_tbs {
	struct nlattr *tb[IFLA_MAX + 1];
	struct nlattr *attr[RTNL_MAX_TYPE + 1];
	struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1];
};

3515
static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3516 3517
			  struct rtnl_newlink_tbs *tbs,
			  struct netlink_ext_ack *extack)
3518
{
3519
	struct nlattr *linkinfo[IFLA_INFO_MAX + 1];
3520
	struct nlattr ** const tb = tbs->tb;
3521 3522
	const struct rtnl_link_ops *m_ops;
	struct net_device *master_dev;
3523
	struct net *net = sock_net(skb->sk);
3524
	const struct rtnl_link_ops *ops;
3525 3526
	struct nlattr **slave_data;
	char kind[MODULE_NAME_LEN];
3527 3528
	struct net_device *dev;
	struct ifinfomsg *ifm;
3529
	struct nlattr **data;
3530
	bool link_specified;
3531 3532
	int err;

3533
#ifdef CONFIG_MODULES
3534
replay:
3535
#endif
3536 3537
	err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
				     ifla_policy, extack);
3538 3539 3540
	if (err < 0)
		return err;

3541 3542 3543 3544
	err = rtnl_ensure_unique_netns(tb, extack, false);
	if (err < 0)
		return err;

3545
	ifm = nlmsg_data(nlh);
3546 3547
	if (ifm->ifi_index > 0) {
		link_specified = true;
3548
		dev = __dev_get_by_index(net, ifm->ifi_index);
3549 3550
	} else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) {
		link_specified = true;
3551
		dev = rtnl_dev_get(net, tb);
3552 3553
	} else {
		link_specified = false;
3554
		dev = NULL;
3555
	}
3556

3557 3558
	master_dev = NULL;
	m_ops = NULL;
3559 3560 3561 3562 3563 3564
	if (dev) {
		master_dev = netdev_master_upper_dev_get(dev);
		if (master_dev)
			m_ops = master_dev->rtnl_link_ops;
	}

3565
	if (tb[IFLA_LINKINFO]) {
3566 3567 3568
		err = nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX,
						  tb[IFLA_LINKINFO],
						  ifla_info_policy, NULL);
3569 3570 3571 3572 3573 3574
		if (err < 0)
			return err;
	} else
		memset(linkinfo, 0, sizeof(linkinfo));

	if (linkinfo[IFLA_INFO_KIND]) {
3575
		nla_strscpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
3576 3577 3578 3579 3580 3581
		ops = rtnl_link_ops_get(kind);
	} else {
		kind[0] = '\0';
		ops = NULL;
	}

3582 3583 3584 3585
	data = NULL;
	if (ops) {
		if (ops->maxtype > RTNL_MAX_TYPE)
			return -EINVAL;
Kees Cook's avatar
Kees Cook committed
3586

3587
		if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
3588
			err = nla_parse_nested_deprecated(tbs->attr, ops->maxtype,
3589 3590
							  linkinfo[IFLA_INFO_DATA],
							  ops->policy, extack);
3591 3592
			if (err < 0)
				return err;
3593
			data = tbs->attr;
3594
		}
3595 3596 3597 3598 3599 3600
		if (ops->validate) {
			err = ops->validate(tb, data, extack);
			if (err < 0)
				return err;
		}
	}
3601

3602 3603 3604 3605
	slave_data = NULL;
	if (m_ops) {
		if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)
			return -EINVAL;
Kees Cook's avatar
Kees Cook committed
3606

3607 3608
		if (m_ops->slave_maxtype &&
		    linkinfo[IFLA_INFO_SLAVE_DATA]) {
3609
			err = nla_parse_nested_deprecated(tbs->slave_attr,
3610 3611 3612 3613
							  m_ops->slave_maxtype,
							  linkinfo[IFLA_INFO_SLAVE_DATA],
							  m_ops->slave_policy,
							  extack);
3614 3615
			if (err < 0)
				return err;
3616
			slave_data = tbs->slave_attr;
3617
		}
3618
	}
3619

3620 3621
	if (dev) {
		int status = 0;
3622

3623 3624 3625 3626
		if (nlh->nlmsg_flags & NLM_F_EXCL)
			return -EEXIST;
		if (nlh->nlmsg_flags & NLM_F_REPLACE)
			return -EOPNOTSUPP;
3627

3628 3629 3630 3631
		err = validate_linkmsg(dev, tb, extack);
		if (err < 0)
			return err;

3632 3633 3634 3635
		if (linkinfo[IFLA_INFO_DATA]) {
			if (!ops || ops != dev->rtnl_link_ops ||
			    !ops->changelink)
				return -EOPNOTSUPP;
3636

3637 3638 3639 3640 3641
			err = ops->changelink(dev, tb, data, extack);
			if (err < 0)
				return err;
			status |= DO_SETLINK_NOTIFY;
		}
3642

3643 3644 3645
		if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
			if (!m_ops || !m_ops->slave_changelink)
				return -EOPNOTSUPP;
3646

3647 3648 3649 3650 3651
			err = m_ops->slave_changelink(master_dev, dev, tb,
						      slave_data, extack);
			if (err < 0)
				return err;
			status |= DO_SETLINK_NOTIFY;
3652 3653
		}

3654
		return do_setlink(skb, dev, ifm, extack, tb, status);
3655 3656 3657
	}

	if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
3658 3659 3660 3661 3662 3663
		/* No dev found and NLM_F_CREATE not set. Requested dev does not exist,
		 * or it's for a group
		*/
		if (link_specified)
			return -ENODEV;
		if (tb[IFLA_GROUP])
3664
			return rtnl_group_changelink(skb, net,
3665
						nla_get_u32(tb[IFLA_GROUP]),
3666
						ifm, extack, tb);
3667
		return -ENODEV;
3668
	}
3669

3670 3671
	if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
		return -EOPNOTSUPP;
3672

3673
	if (!ops) {
3674
#ifdef CONFIG_MODULES
3675 3676 3677 3678 3679 3680 3681
		if (kind[0]) {
			__rtnl_unlock();
			request_module("rtnl-link-%s", kind);
			rtnl_lock();
			ops = rtnl_link_ops_get(kind);
			if (ops)
				goto replay;
3682
		}
3683 3684 3685 3686
#endif
		NL_SET_ERR_MSG(extack, "Unknown device type");
		return -EOPNOTSUPP;
	}
3687

3688
	return rtnl_newlink_create(skb, ifm, ops, nlh, tb, data, extack);
3689 3690
}

3691 3692 3693
static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
			struct netlink_ext_ack *extack)
{
3694
	struct rtnl_newlink_tbs *tbs;
3695 3696
	int ret;

3697 3698
	tbs = kmalloc(sizeof(*tbs), GFP_KERNEL);
	if (!tbs)
3699 3700
		return -ENOMEM;

3701 3702
	ret = __rtnl_newlink(skb, nlh, tbs, extack);
	kfree(tbs);
3703 3704 3705
	return ret;
}

3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719
static int rtnl_valid_getlink_req(struct sk_buff *skb,
				  const struct nlmsghdr *nlh,
				  struct nlattr **tb,
				  struct netlink_ext_ack *extack)
{
	struct ifinfomsg *ifm;
	int i, err;

	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
		NL_SET_ERR_MSG(extack, "Invalid header for get link");
		return -EINVAL;
	}

	if (!netlink_strict_get_check(skb))
3720 3721
		return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
					      ifla_policy, extack);
3722 3723 3724 3725 3726 3727 3728 3729

	ifm = nlmsg_data(nlh);
	if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
	    ifm->ifi_change) {
		NL_SET_ERR_MSG(extack, "Invalid values in header for get link request");
		return -EINVAL;
	}

3730 3731
	err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFLA_MAX,
					    ifla_policy, extack);
3732 3733 3734 3735 3736 3737 3738 3739 3740
	if (err)
		return err;

	for (i = 0; i <= IFLA_MAX; i++) {
		if (!tb[i])
			continue;

		switch (i) {
		case IFLA_IFNAME:
3741
		case IFLA_ALT_IFNAME:
3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753
		case IFLA_EXT_MASK:
		case IFLA_TARGET_NETNSID:
			break;
		default:
			NL_SET_ERR_MSG(extack, "Unsupported attribute in get link request");
			return -EINVAL;
		}
	}

	return 0;
}

3754 3755
static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
			struct netlink_ext_ack *extack)
3756
{
3757
	struct net *net = sock_net(skb->sk);
3758
	struct net *tgt_net = net;
3759 3760 3761 3762
	struct ifinfomsg *ifm;
	struct nlattr *tb[IFLA_MAX+1];
	struct net_device *dev = NULL;
	struct sk_buff *nskb;
3763
	int netnsid = -1;
3764
	int err;
3765
	u32 ext_filter_mask = 0;
3766

3767
	err = rtnl_valid_getlink_req(skb, nlh, tb, extack);
3768
	if (err < 0)
3769
		return err;
3770

3771 3772 3773 3774
	err = rtnl_ensure_unique_netns(tb, extack, true);
	if (err < 0)
		return err;

3775 3776
	if (tb[IFLA_TARGET_NETNSID]) {
		netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
3777
		tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
3778 3779 3780 3781
		if (IS_ERR(tgt_net))
			return PTR_ERR(tgt_net);
	}

3782 3783 3784
	if (tb[IFLA_EXT_MASK])
		ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);

3785
	err = -EINVAL;
3786
	ifm = nlmsg_data(nlh);
3787
	if (ifm->ifi_index > 0)
3788
		dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
3789
	else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3790
		dev = rtnl_dev_get(tgt_net, tb);
3791
	else
3792
		goto out;
3793

3794
	err = -ENODEV;
3795
	if (dev == NULL)
3796
		goto out;
3797

3798
	err = -ENOBUFS;
3799
	nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
3800
	if (nskb == NULL)
3801
		goto out;
3802

3803 3804 3805
	err = rtnl_fill_ifinfo(nskb, dev, net,
			       RTM_NEWLINK, NETLINK_CB(skb).portid,
			       nlh->nlmsg_seq, 0, 0, ext_filter_mask,
3806
			       0, NULL, 0, netnsid, GFP_KERNEL);
3807 3808 3809 3810
	if (err < 0) {
		/* -EMSGSIZE implies BUG in if_nlmsg_size */
		WARN_ON(err == -EMSGSIZE);
		kfree_skb(nskb);
3811
	} else
3812
		err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
3813 3814 3815
out:
	if (netnsid >= 0)
		put_net(tgt_net);
3816

3817
	return err;
3818 3819
}

3820 3821 3822 3823
static int rtnl_alt_ifname(int cmd, struct net_device *dev, struct nlattr *attr,
			   bool *changed, struct netlink_ext_ack *extack)
{
	char *alt_ifname;
3824
	size_t size;
3825 3826 3827 3828 3829 3830
	int err;

	err = nla_validate(attr, attr->nla_len, IFLA_MAX, ifla_policy, extack);
	if (err)
		return err;

3831 3832 3833 3834 3835 3836 3837 3838 3839 3840
	if (cmd == RTM_NEWLINKPROP) {
		size = rtnl_prop_list_size(dev);
		size += nla_total_size(ALTIFNAMSIZ);
		if (size >= U16_MAX) {
			NL_SET_ERR_MSG(extack,
				       "effective property list too long");
			return -EINVAL;
		}
	}

3841
	alt_ifname = nla_strdup(attr, GFP_KERNEL_ACCOUNT);
3842 3843 3844
	if (!alt_ifname)
		return -ENOMEM;

3845 3846
	if (cmd == RTM_NEWLINKPROP) {
		err = netdev_name_node_alt_create(dev, alt_ifname);
3847 3848
		if (!err)
			alt_ifname = NULL;
3849 3850 3851
	} else if (cmd == RTM_DELLINKPROP) {
		err = netdev_name_node_alt_destroy(dev, alt_ifname);
	} else {
3852 3853
		WARN_ON_ONCE(1);
		err = -EINVAL;
3854 3855
	}

3856 3857 3858 3859
	kfree(alt_ifname);
	if (!err)
		*changed = true;
	return err;
3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881
}

static int rtnl_linkprop(int cmd, struct sk_buff *skb, struct nlmsghdr *nlh,
			 struct netlink_ext_ack *extack)
{
	struct net *net = sock_net(skb->sk);
	struct nlattr *tb[IFLA_MAX + 1];
	struct net_device *dev;
	struct ifinfomsg *ifm;
	bool changed = false;
	struct nlattr *attr;
	int err, rem;

	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
	if (err)
		return err;

	err = rtnl_ensure_unique_netns(tb, extack, true);
	if (err)
		return err;

	ifm = nlmsg_data(nlh);
3882
	if (ifm->ifi_index > 0)
3883
		dev = __dev_get_by_index(net, ifm->ifi_index);
3884
	else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3885
		dev = rtnl_dev_get(net, tb);
3886
	else
3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921
		return -EINVAL;

	if (!dev)
		return -ENODEV;

	if (!tb[IFLA_PROP_LIST])
		return 0;

	nla_for_each_nested(attr, tb[IFLA_PROP_LIST], rem) {
		switch (nla_type(attr)) {
		case IFLA_ALT_IFNAME:
			err = rtnl_alt_ifname(cmd, dev, attr, &changed, extack);
			if (err)
				return err;
			break;
		}
	}

	if (changed)
		netdev_state_change(dev);
	return 0;
}

static int rtnl_newlinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
			    struct netlink_ext_ack *extack)
{
	return rtnl_linkprop(RTM_NEWLINKPROP, skb, nlh, extack);
}

static int rtnl_dellinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
			    struct netlink_ext_ack *extack)
{
	return rtnl_linkprop(RTM_DELLINKPROP, skb, nlh, extack);
}

3922
static u32 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
3923
{
3924
	struct net *net = sock_net(skb->sk);
3925
	size_t min_ifinfo_dump_size = 0;
3926 3927
	struct nlattr *tb[IFLA_MAX+1];
	u32 ext_filter_mask = 0;
3928
	struct net_device *dev;
3929 3930 3931 3932 3933
	int hdrlen;

	/* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
	hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
		 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
3934

3935
	if (nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
3936 3937 3938
		if (tb[IFLA_EXT_MASK])
			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
	}
3939 3940 3941 3942 3943 3944 3945

	if (!ext_filter_mask)
		return NLMSG_GOODSIZE;
	/*
	 * traverse the list of net devices and compute the minimum
	 * buffer size based upon the filter mask.
	 */
3946 3947
	rcu_read_lock();
	for_each_netdev_rcu(net, dev) {
3948 3949
		min_ifinfo_dump_size = max(min_ifinfo_dump_size,
					   if_nlmsg_size(dev, ext_filter_mask));
3950
	}
3951
	rcu_read_unlock();
3952

3953
	return nlmsg_total_size(min_ifinfo_dump_size);
3954 3955
}

Adrian Bunk's avatar
Adrian Bunk committed
3956
static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds's avatar
Linus Torvalds committed
3957 3958 3959
{
	int idx;
	int s_idx = cb->family;
3960
	int type = cb->nlh->nlmsg_type - RTM_BASE;
3961
	int ret = 0;
Linus Torvalds's avatar
Linus Torvalds committed
3962 3963 3964

	if (s_idx == 0)
		s_idx = 1;
3965

3966
	for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
3967
		struct rtnl_link __rcu **tab;
3968
		struct rtnl_link *link;
3969 3970
		rtnl_dumpit_func dumpit;

Linus Torvalds's avatar
Linus Torvalds committed
3971 3972
		if (idx < s_idx || idx == PF_PACKET)
			continue;
3973

3974
		if (type < 0 || type >= RTM_NR_MSGTYPES)
Linus Torvalds's avatar
Linus Torvalds committed
3975
			continue;
3976

3977 3978 3979 3980
		tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]);
		if (!tab)
			continue;

3981
		link = rcu_dereference_rtnl(tab[type]);
3982 3983 3984 3985
		if (!link)
			continue;

		dumpit = link->dumpit;
3986 3987 3988
		if (!dumpit)
			continue;

3989
		if (idx > s_idx) {
Linus Torvalds's avatar
Linus Torvalds committed
3990
			memset(&cb->args[0], 0, sizeof(cb->args));
3991 3992 3993
			cb->prev_seq = 0;
			cb->seq = 0;
		}
3994
		ret = dumpit(skb, cb);
3995
		if (ret)
Linus Torvalds's avatar
Linus Torvalds committed
3996 3997 3998 3999
			break;
	}
	cb->family = idx;

4000
	return skb->len ? : ret;
Linus Torvalds's avatar
Linus Torvalds committed
4001 4002
}

4003
struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
4004
				       unsigned int change,
4005
				       u32 event, gfp_t flags, int *new_nsid,
4006 4007
				       int new_ifindex, u32 portid,
				       const struct nlmsghdr *nlh)
Linus Torvalds's avatar
Linus Torvalds committed
4008
{
4009
	struct net *net = dev_net(dev);
Linus Torvalds's avatar
Linus Torvalds committed
4010
	struct sk_buff *skb;
4011
	int err = -ENOBUFS;
4012
	u32 seq = 0;
Linus Torvalds's avatar
Linus Torvalds committed
4013

4014
	skb = nlmsg_new(if_nlmsg_size(dev, 0), flags);
4015 4016
	if (skb == NULL)
		goto errout;
Linus Torvalds's avatar
Linus Torvalds committed
4017

4018 4019 4020 4021 4022
	if (nlmsg_report(nlh))
		seq = nlmsg_seq(nlh);
	else
		portid = 0;

4023
	err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
4024
			       type, portid, seq, change, 0, 0, event,
4025
			       new_nsid, new_ifindex, -1, flags);
4026 4027 4028 4029 4030 4031
	if (err < 0) {
		/* -EMSGSIZE implies BUG in if_nlmsg_size() */
		WARN_ON(err == -EMSGSIZE);
		kfree_skb(skb);
		goto errout;
	}
4032
	return skb;
4033 4034
errout:
	if (err < 0)
4035
		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
4036 4037 4038
	return NULL;
}

4039 4040
void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags,
		       u32 portid, const struct nlmsghdr *nlh)
4041 4042 4043
{
	struct net *net = dev_net(dev);

4044
	rtnl_notify(skb, net, portid, RTNLGRP_LINK, nlh, flags);
4045 4046
}

4047 4048
static void rtmsg_ifinfo_event(int type, struct net_device *dev,
			       unsigned int change, u32 event,
4049 4050
			       gfp_t flags, int *new_nsid, int new_ifindex,
			       u32 portid, const struct nlmsghdr *nlh)
4051 4052 4053
{
	struct sk_buff *skb;

4054 4055 4056
	if (dev->reg_state != NETREG_REGISTERED)
		return;

4057
	skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid,
4058
				     new_ifindex, portid, nlh);
4059
	if (skb)
4060
		rtmsg_ifinfo_send(skb, dev, flags, portid, nlh);
Linus Torvalds's avatar
Linus Torvalds committed
4061
}
4062 4063

void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
4064
		  gfp_t flags, u32 portid, const struct nlmsghdr *nlh)
4065
{
4066
	rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
4067
			   NULL, 0, portid, nlh);
4068
}
Linus Torvalds's avatar
Linus Torvalds committed
4069

4070
void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
4071
			 gfp_t flags, int *new_nsid, int new_ifindex)
4072 4073
{
	rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
4074
			   new_nsid, new_ifindex, 0, NULL);
4075 4076
}

4077 4078
static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
				   struct net_device *dev,
4079
				   u8 *addr, u16 vid, u32 pid, u32 seq,
4080
				   int type, unsigned int flags,
4081
				   int nlflags, u16 ndm_state)
4082 4083 4084 4085
{
	struct nlmsghdr *nlh;
	struct ndmsg *ndm;

4086
	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
4087 4088 4089 4090 4091 4092 4093 4094 4095 4096
	if (!nlh)
		return -EMSGSIZE;

	ndm = nlmsg_data(nlh);
	ndm->ndm_family  = AF_BRIDGE;
	ndm->ndm_pad1	 = 0;
	ndm->ndm_pad2    = 0;
	ndm->ndm_flags	 = flags;
	ndm->ndm_type	 = 0;
	ndm->ndm_ifindex = dev->ifindex;
4097
	ndm->ndm_state   = ndm_state;
4098

4099
	if (nla_put(skb, NDA_LLADDR, dev->addr_len, addr))
4100
		goto nla_put_failure;
4101 4102 4103
	if (vid)
		if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
			goto nla_put_failure;
4104

4105 4106
	nlmsg_end(skb, nlh);
	return 0;
4107 4108 4109 4110 4111 4112

nla_put_failure:
	nlmsg_cancel(skb, nlh);
	return -EMSGSIZE;
}

4113
static inline size_t rtnl_fdb_nlmsg_size(const struct net_device *dev)
4114
{
4115
	return NLMSG_ALIGN(sizeof(struct ndmsg)) +
4116
	       nla_total_size(dev->addr_len) +	/* NDA_LLADDR */
4117 4118
	       nla_total_size(sizeof(u16)) +	/* NDA_VLAN */
	       0;
4119 4120
}

4121 4122
static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
			    u16 ndm_state)
4123 4124 4125 4126 4127
{
	struct net *net = dev_net(dev);
	struct sk_buff *skb;
	int err = -ENOBUFS;

4128
	skb = nlmsg_new(rtnl_fdb_nlmsg_size(dev), GFP_ATOMIC);
4129 4130 4131
	if (!skb)
		goto errout;

4132
	err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
4133
				      0, 0, type, NTF_SELF, 0, ndm_state);
4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144
	if (err < 0) {
		kfree_skb(skb);
		goto errout;
	}

	rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
	return;
errout:
	rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
}

4145
/*
4146 4147 4148 4149 4150
 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
 */
int ndo_dflt_fdb_add(struct ndmsg *ndm,
		     struct nlattr *tb[],
		     struct net_device *dev,
4151
		     const unsigned char *addr, u16 vid,
4152 4153 4154 4155 4156 4157 4158 4159
		     u16 flags)
{
	int err = -EINVAL;

	/* If aging addresses are supported device will need to
	 * implement its own handler for this.
	 */
	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
4160
		netdev_info(dev, "default FDB implementation only supports local addresses\n");
4161 4162 4163
		return err;
	}

4164 4165 4166 4167 4168
	if (tb[NDA_FLAGS_EXT]) {
		netdev_info(dev, "invalid flags given to default FDB implementation\n");
		return err;
	}

4169
	if (vid) {
4170
		netdev_info(dev, "vlans aren't supported yet for dev_uc|mc_add()\n");
4171 4172 4173
		return err;
	}

4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186
	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
		err = dev_uc_add_excl(dev, addr);
	else if (is_multicast_ether_addr(addr))
		err = dev_mc_add_excl(dev, addr);

	/* Only return duplicate errors if NLM_F_EXCL is set */
	if (err == -EEXIST && !(flags & NLM_F_EXCL))
		err = 0;

	return err;
}
EXPORT_SYMBOL(ndo_dflt_fdb_add);

4187 4188
static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
			 struct netlink_ext_ack *extack)
4189 4190 4191 4192 4193
{
	u16 vid = 0;

	if (vlan_attr) {
		if (nla_len(vlan_attr) != sizeof(u16)) {
4194
			NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
4195 4196 4197 4198 4199 4200
			return -EINVAL;
		}

		vid = nla_get_u16(vlan_attr);

		if (!vid || vid >= VLAN_VID_MASK) {
4201
			NL_SET_ERR_MSG(extack, "invalid vlan id");
4202 4203 4204 4205 4206 4207 4208
			return -EINVAL;
		}
	}
	*p_vid = vid;
	return 0;
}

4209 4210
static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
			struct netlink_ext_ack *extack)
4211 4212 4213 4214 4215 4216
{
	struct net *net = sock_net(skb->sk);
	struct ndmsg *ndm;
	struct nlattr *tb[NDA_MAX+1];
	struct net_device *dev;
	u8 *addr;
4217
	u16 vid;
4218 4219
	int err;

4220 4221
	err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
				     extack);
4222 4223 4224 4225 4226
	if (err < 0)
		return err;

	ndm = nlmsg_data(nlh);
	if (ndm->ndm_ifindex == 0) {
4227
		NL_SET_ERR_MSG(extack, "invalid ifindex");
4228 4229 4230 4231 4232
		return -EINVAL;
	}

	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
	if (dev == NULL) {
4233
		NL_SET_ERR_MSG(extack, "unknown ifindex");
4234 4235 4236 4237
		return -ENODEV;
	}

	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
4238
		NL_SET_ERR_MSG(extack, "invalid address");
4239 4240 4241
		return -EINVAL;
	}

4242 4243 4244 4245 4246
	if (dev->type != ARPHRD_ETHER) {
		NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices");
		return -EINVAL;
	}

4247 4248
	addr = nla_data(tb[NDA_LLADDR]);

4249
	err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
4250 4251 4252
	if (err)
		return err;

4253 4254 4255 4256
	err = -EOPNOTSUPP;

	/* Support fdb on master device the net/bridge default case */
	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
4257
	    netif_is_bridge_port(dev)) {
4258 4259 4260
		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
		const struct net_device_ops *ops = br_dev->netdev_ops;

4261
		err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
4262
				       nlh->nlmsg_flags, extack);
4263 4264 4265 4266 4267 4268 4269
		if (err)
			goto out;
		else
			ndm->ndm_flags &= ~NTF_MASTER;
	}

	/* Embedded bridge, macvlan, and any other device support */
4270 4271 4272
	if ((ndm->ndm_flags & NTF_SELF)) {
		if (dev->netdev_ops->ndo_fdb_add)
			err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
4273
							   vid,
4274 4275
							   nlh->nlmsg_flags,
							   extack);
4276
		else
4277
			err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
4278
					       nlh->nlmsg_flags);
4279

4280
		if (!err) {
4281 4282
			rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
					ndm->ndm_state);
4283
			ndm->ndm_flags &= ~NTF_SELF;
4284
		}
4285 4286 4287 4288 4289
	}
out:
	return err;
}

4290
/*
4291 4292 4293 4294 4295
 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
 */
int ndo_dflt_fdb_del(struct ndmsg *ndm,
		     struct nlattr *tb[],
		     struct net_device *dev,
4296
		     const unsigned char *addr, u16 vid)
4297
{
4298
	int err = -EINVAL;
4299 4300 4301 4302

	/* If aging addresses are supported device will need to
	 * implement its own handler for this.
	 */
4303
	if (!(ndm->ndm_state & NUD_PERMANENT)) {
4304
		netdev_info(dev, "default FDB implementation only supports local addresses\n");
4305
		return err;
4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316
	}

	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
		err = dev_uc_del(dev, addr);
	else if (is_multicast_ether_addr(addr))
		err = dev_mc_del(dev, addr);

	return err;
}
EXPORT_SYMBOL(ndo_dflt_fdb_del);

4317 4318 4319
static const struct nla_policy fdb_del_bulk_policy[NDA_MAX + 1] = {
	[NDA_VLAN]	= { .type = NLA_U16 },
	[NDA_IFINDEX]	= NLA_POLICY_MIN(NLA_S32, 1),
4320 4321
	[NDA_NDM_STATE_MASK]	= { .type = NLA_U16  },
	[NDA_NDM_FLAGS_MASK]	= { .type = NLA_U8 },
4322 4323
};

4324 4325
static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
			struct netlink_ext_ack *extack)
4326
{
4327
	bool del_bulk = !!(nlh->nlmsg_flags & NLM_F_BULK);
4328
	struct net *net = sock_net(skb->sk);
4329
	const struct net_device_ops *ops;
4330
	struct ndmsg *ndm;
4331
	struct nlattr *tb[NDA_MAX+1];
4332
	struct net_device *dev;
4333
	__u8 *addr = NULL;
4334
	int err;
4335
	u16 vid;
4336

4337
	if (!netlink_capable(skb, CAP_NET_ADMIN))
4338 4339
		return -EPERM;

4340 4341 4342 4343 4344 4345 4346
	if (!del_bulk) {
		err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX,
					     NULL, extack);
	} else {
		err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX,
				  fdb_del_bulk_policy, extack);
	}
4347 4348
	if (err < 0)
		return err;
4349 4350 4351

	ndm = nlmsg_data(nlh);
	if (ndm->ndm_ifindex == 0) {
4352
		NL_SET_ERR_MSG(extack, "invalid ifindex");
4353 4354 4355 4356 4357
		return -EINVAL;
	}

	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
	if (dev == NULL) {
4358
		NL_SET_ERR_MSG(extack, "unknown ifindex");
4359 4360 4361
		return -ENODEV;
	}

4362 4363 4364 4365 4366 4367
	if (!del_bulk) {
		if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
			NL_SET_ERR_MSG(extack, "invalid address");
			return -EINVAL;
		}
		addr = nla_data(tb[NDA_LLADDR]);
4368 4369
	}

4370 4371 4372 4373 4374
	if (dev->type != ARPHRD_ETHER) {
		NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices");
		return -EINVAL;
	}

4375
	err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
4376 4377 4378
	if (err)
		return err;

4379 4380 4381 4382
	err = -EOPNOTSUPP;

	/* Support fdb on master device the net/bridge default case */
	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
4383
	    netif_is_bridge_port(dev)) {
4384
		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4385

4386 4387 4388
		ops = br_dev->netdev_ops;
		if (!del_bulk) {
			if (ops->ndo_fdb_del)
4389
				err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid, extack);
4390 4391 4392 4393 4394
		} else {
			if (ops->ndo_fdb_del_bulk)
				err = ops->ndo_fdb_del_bulk(ndm, tb, dev, vid,
							    extack);
		}
4395 4396 4397 4398 4399 4400 4401 4402

		if (err)
			goto out;
		else
			ndm->ndm_flags &= ~NTF_MASTER;
	}

	/* Embedded bridge, macvlan, and any other device support */
4403
	if (ndm->ndm_flags & NTF_SELF) {
4404 4405 4406
		ops = dev->netdev_ops;
		if (!del_bulk) {
			if (ops->ndo_fdb_del)
4407
				err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid, extack);
4408 4409 4410 4411 4412 4413 4414 4415 4416
			else
				err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
		} else {
			/* in case err was cleared by NTF_MASTER call */
			err = -EOPNOTSUPP;
			if (ops->ndo_fdb_del_bulk)
				err = ops->ndo_fdb_del_bulk(ndm, tb, dev, vid,
							    extack);
		}
4417

4418
		if (!err) {
4419 4420 4421
			if (!del_bulk)
				rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
						ndm->ndm_state);
4422
			ndm->ndm_flags &= ~NTF_SELF;
4423
		}
4424 4425 4426 4427 4428
	}
out:
	return err;
}

4429 4430 4431 4432 4433 4434 4435 4436
static int nlmsg_populate_fdb(struct sk_buff *skb,
			      struct netlink_callback *cb,
			      struct net_device *dev,
			      int *idx,
			      struct netdev_hw_addr_list *list)
{
	struct netdev_hw_addr *ha;
	int err;
4437
	u32 portid, seq;
4438

4439
	portid = NETLINK_CB(cb->skb).portid;
4440 4441 4442
	seq = cb->nlh->nlmsg_seq;

	list_for_each_entry(ha, &list->list, list) {
4443
		if (*idx < cb->args[2])
4444 4445
			goto skip;

4446
		err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
4447
					      portid, seq,
4448
					      RTM_NEWNEIGH, NTF_SELF,
4449
					      NLM_F_MULTI, NUD_PERMANENT);
4450 4451 4452 4453 4454 4455 4456 4457 4458
		if (err < 0)
			return err;
skip:
		*idx += 1;
	}
	return 0;
}

/**
4459
 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
4460 4461
 * @skb: socket buffer to store message in
 * @cb: netlink callback
4462
 * @dev: netdevice
4463 4464
 * @filter_dev: ignored
 * @idx: the number of FDB table entries dumped is added to *@idx
4465 4466
 *
 * Default netdevice operation to dump the existing unicast address list.
4467
 * Returns number of addresses from list put in skb.
4468 4469 4470 4471
 */
int ndo_dflt_fdb_dump(struct sk_buff *skb,
		      struct netlink_callback *cb,
		      struct net_device *dev,
4472
		      struct net_device *filter_dev,
4473
		      int *idx)
4474 4475 4476
{
	int err;

4477 4478 4479
	if (dev->type != ARPHRD_ETHER)
		return -EINVAL;

4480
	netif_addr_lock_bh(dev);
4481
	err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
4482 4483
	if (err)
		goto out;
4484
	err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
4485 4486
out:
	netif_addr_unlock_bh(dev);
4487
	return err;
4488 4489 4490
}
EXPORT_SYMBOL(ndo_dflt_fdb_dump);

4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506
static int valid_fdb_dump_strict(const struct nlmsghdr *nlh,
				 int *br_idx, int *brport_idx,
				 struct netlink_ext_ack *extack)
{
	struct nlattr *tb[NDA_MAX + 1];
	struct ndmsg *ndm;
	int err, i;

	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
		NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request");
		return -EINVAL;
	}

	ndm = nlmsg_data(nlh);
	if (ndm->ndm_pad1  || ndm->ndm_pad2  || ndm->ndm_state ||
	    ndm->ndm_flags || ndm->ndm_type) {
4507
		NL_SET_ERR_MSG(extack, "Invalid values in header for fdb dump request");
4508 4509 4510
		return -EINVAL;
	}

4511 4512
	err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
					    NDA_MAX, NULL, extack);
4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544
	if (err < 0)
		return err;

	*brport_idx = ndm->ndm_ifindex;
	for (i = 0; i <= NDA_MAX; ++i) {
		if (!tb[i])
			continue;

		switch (i) {
		case NDA_IFINDEX:
			if (nla_len(tb[i]) != sizeof(u32)) {
				NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request");
				return -EINVAL;
			}
			*brport_idx = nla_get_u32(tb[NDA_IFINDEX]);
			break;
		case NDA_MASTER:
			if (nla_len(tb[i]) != sizeof(u32)) {
				NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request");
				return -EINVAL;
			}
			*br_idx = nla_get_u32(tb[NDA_MASTER]);
			break;
		default:
			NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request");
			return -EINVAL;
		}
	}

	return 0;
}

4545 4546 4547
static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh,
				 int *br_idx, int *brport_idx,
				 struct netlink_ext_ack *extack)
4548
{
4549
	struct nlattr *tb[IFLA_MAX+1];
4550
	int err;
4551

4552 4553 4554 4555 4556 4557 4558
	/* A hack to preserve kernel<->userspace interface.
	 * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0.
	 * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails.
	 * So, check for ndmsg with an optional u32 attribute (not used here).
	 * Fortunately these sizes don't conflict with the size of ifinfomsg
	 * with an optional attribute.
	 */
4559 4560
	if (nlmsg_len(nlh) != sizeof(struct ndmsg) &&
	    (nlmsg_len(nlh) != sizeof(struct ndmsg) +
4561
	     nla_attr_size(sizeof(u32)))) {
4562 4563
		struct ifinfomsg *ifm;

4564 4565 4566
		err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
					     tb, IFLA_MAX, ifla_policy,
					     extack);
4567 4568 4569 4570
		if (err < 0) {
			return -EINVAL;
		} else if (err == 0) {
			if (tb[IFLA_MASTER])
4571
				*br_idx = nla_get_u32(tb[IFLA_MASTER]);
4572
		}
4573

4574
		ifm = nlmsg_data(nlh);
4575
		*brport_idx = ifm->ifi_index;
4576
	}
4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594
	return 0;
}

static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
{
	struct net_device *dev;
	struct net_device *br_dev = NULL;
	const struct net_device_ops *ops = NULL;
	const struct net_device_ops *cops = NULL;
	struct net *net = sock_net(skb->sk);
	struct hlist_head *head;
	int brport_idx = 0;
	int br_idx = 0;
	int h, s_h;
	int idx = 0, s_idx;
	int err = 0;
	int fidx = 0;

4595 4596 4597 4598 4599 4600
	if (cb->strict_check)
		err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx,
					    cb->extack);
	else
		err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx,
					    cb->extack);
4601 4602
	if (err < 0)
		return err;
4603 4604 4605 4606 4607 4608 4609 4610 4611

	if (br_idx) {
		br_dev = __dev_get_by_index(net, br_idx);
		if (!br_dev)
			return -ENODEV;

		ops = br_dev->netdev_ops;
	}

4612 4613
	s_h = cb->args[0];
	s_idx = cb->args[1];
4614

4615 4616 4617 4618
	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
		idx = 0;
		head = &net->dev_index_head[h];
		hlist_for_each_entry(dev, head, index_hlist) {
4619

4620
			if (brport_idx && (dev->ifindex != brport_idx))
4621 4622
				continue;

4623
			if (!br_idx) { /* user did not specify a specific bridge */
4624
				if (netif_is_bridge_port(dev)) {
4625 4626 4627 4628 4629
					br_dev = netdev_master_upper_dev_get(dev);
					cops = br_dev->netdev_ops;
				}
			} else {
				if (dev != br_dev &&
4630
				    !netif_is_bridge_port(dev))
4631
					continue;
4632

4633
				if (br_dev != netdev_master_upper_dev_get(dev) &&
4634
				    !netif_is_bridge_master(dev))
4635 4636 4637
					continue;
				cops = ops;
			}
4638

4639 4640
			if (idx < s_idx)
				goto cont;
4641

4642
			if (netif_is_bridge_port(dev)) {
4643 4644 4645 4646 4647 4648 4649 4650
				if (cops && cops->ndo_fdb_dump) {
					err = cops->ndo_fdb_dump(skb, cb,
								br_dev, dev,
								&fidx);
					if (err == -EMSGSIZE)
						goto out;
				}
			}
4651

4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669
			if (dev->netdev_ops->ndo_fdb_dump)
				err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
								    dev, NULL,
								    &fidx);
			else
				err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
							&fidx);
			if (err == -EMSGSIZE)
				goto out;

			cops = NULL;

			/* reset fdb offset to 0 for rest of the interfaces */
			cb->args[2] = 0;
			fidx = 0;
cont:
			idx++;
		}
4670 4671
	}

4672 4673 4674 4675 4676
out:
	cb->args[0] = h;
	cb->args[1] = idx;
	cb->args[2] = fidx;

4677 4678 4679
	return skb->len;
}

4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704
static int valid_fdb_get_strict(const struct nlmsghdr *nlh,
				struct nlattr **tb, u8 *ndm_flags,
				int *br_idx, int *brport_idx, u8 **addr,
				u16 *vid, struct netlink_ext_ack *extack)
{
	struct ndmsg *ndm;
	int err, i;

	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
		NL_SET_ERR_MSG(extack, "Invalid header for fdb get request");
		return -EINVAL;
	}

	ndm = nlmsg_data(nlh);
	if (ndm->ndm_pad1  || ndm->ndm_pad2  || ndm->ndm_state ||
	    ndm->ndm_type) {
		NL_SET_ERR_MSG(extack, "Invalid values in header for fdb get request");
		return -EINVAL;
	}

	if (ndm->ndm_flags & ~(NTF_MASTER | NTF_SELF)) {
		NL_SET_ERR_MSG(extack, "Invalid flags in header for fdb get request");
		return -EINVAL;
	}

4705 4706
	err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
					    NDA_MAX, nda_policy, extack);
4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762
	if (err < 0)
		return err;

	*ndm_flags = ndm->ndm_flags;
	*brport_idx = ndm->ndm_ifindex;
	for (i = 0; i <= NDA_MAX; ++i) {
		if (!tb[i])
			continue;

		switch (i) {
		case NDA_MASTER:
			*br_idx = nla_get_u32(tb[i]);
			break;
		case NDA_LLADDR:
			if (nla_len(tb[i]) != ETH_ALEN) {
				NL_SET_ERR_MSG(extack, "Invalid address in fdb get request");
				return -EINVAL;
			}
			*addr = nla_data(tb[i]);
			break;
		case NDA_VLAN:
			err = fdb_vid_parse(tb[i], vid, extack);
			if (err)
				return err;
			break;
		case NDA_VNI:
			break;
		default:
			NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb get request");
			return -EINVAL;
		}
	}

	return 0;
}

static int rtnl_fdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh,
			struct netlink_ext_ack *extack)
{
	struct net_device *dev = NULL, *br_dev = NULL;
	const struct net_device_ops *ops = NULL;
	struct net *net = sock_net(in_skb->sk);
	struct nlattr *tb[NDA_MAX + 1];
	struct sk_buff *skb;
	int brport_idx = 0;
	u8 ndm_flags = 0;
	int br_idx = 0;
	u8 *addr = NULL;
	u16 vid = 0;
	int err;

	err = valid_fdb_get_strict(nlh, tb, &ndm_flags, &br_idx,
				   &brport_idx, &addr, &vid, extack);
	if (err < 0)
		return err;

4763 4764 4765 4766 4767
	if (!addr) {
		NL_SET_ERR_MSG(extack, "Missing lookup address for fdb get request");
		return -EINVAL;
	}

4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791
	if (brport_idx) {
		dev = __dev_get_by_index(net, brport_idx);
		if (!dev) {
			NL_SET_ERR_MSG(extack, "Unknown device ifindex");
			return -ENODEV;
		}
	}

	if (br_idx) {
		if (dev) {
			NL_SET_ERR_MSG(extack, "Master and device are mutually exclusive");
			return -EINVAL;
		}

		br_dev = __dev_get_by_index(net, br_idx);
		if (!br_dev) {
			NL_SET_ERR_MSG(extack, "Invalid master ifindex");
			return -EINVAL;
		}
		ops = br_dev->netdev_ops;
	}

	if (dev) {
		if (!ndm_flags || (ndm_flags & NTF_MASTER)) {
4792
			if (!netif_is_bridge_port(dev)) {
4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838
				NL_SET_ERR_MSG(extack, "Device is not a bridge port");
				return -EINVAL;
			}
			br_dev = netdev_master_upper_dev_get(dev);
			if (!br_dev) {
				NL_SET_ERR_MSG(extack, "Master of device not found");
				return -EINVAL;
			}
			ops = br_dev->netdev_ops;
		} else {
			if (!(ndm_flags & NTF_SELF)) {
				NL_SET_ERR_MSG(extack, "Missing NTF_SELF");
				return -EINVAL;
			}
			ops = dev->netdev_ops;
		}
	}

	if (!br_dev && !dev) {
		NL_SET_ERR_MSG(extack, "No device specified");
		return -ENODEV;
	}

	if (!ops || !ops->ndo_fdb_get) {
		NL_SET_ERR_MSG(extack, "Fdb get operation not supported by device");
		return -EOPNOTSUPP;
	}

	skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
	if (!skb)
		return -ENOBUFS;

	if (br_dev)
		dev = br_dev;
	err = ops->ndo_fdb_get(skb, tb, dev, addr, vid,
			       NETLINK_CB(in_skb).portid,
			       nlh->nlmsg_seq, extack);
	if (err)
		goto out;

	return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
out:
	kfree_skb(skb);
	return err;
}

4839 4840 4841 4842 4843 4844 4845 4846
static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
			       unsigned int attrnum, unsigned int flag)
{
	if (mask & flag)
		return nla_put_u8(skb, attrnum, !!(flags & flag));
	return 0;
}

4847
int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
4848
			    struct net_device *dev, u16 mode,
4849 4850 4851 4852 4853
			    u32 flags, u32 mask, int nlflags,
			    u32 filter_mask,
			    int (*vlan_fill)(struct sk_buff *skb,
					     struct net_device *dev,
					     u32 filter_mask))
4854 4855 4856 4857
{
	struct nlmsghdr *nlh;
	struct ifinfomsg *ifm;
	struct nlattr *br_afspec;
4858
	struct nlattr *protinfo;
4859
	u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
4860
	struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4861
	int err = 0;
4862

4863
	nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878
	if (nlh == NULL)
		return -EMSGSIZE;

	ifm = nlmsg_data(nlh);
	ifm->ifi_family = AF_BRIDGE;
	ifm->__ifi_pad = 0;
	ifm->ifi_type = dev->type;
	ifm->ifi_index = dev->ifindex;
	ifm->ifi_flags = dev_get_flags(dev);
	ifm->ifi_change = 0;


	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
	    nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
4879 4880
	    (br_dev &&
	     nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
4881 4882
	    (dev->addr_len &&
	     nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
4883 4884
	    (dev->ifindex != dev_get_iflink(dev) &&
	     nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
4885 4886
		goto nla_put_failure;

4887
	br_afspec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
4888 4889 4890
	if (!br_afspec)
		goto nla_put_failure;

Roopa Prabhu's avatar
Roopa Prabhu committed
4891
	if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
4892 4893 4894
		nla_nest_cancel(skb, br_afspec);
		goto nla_put_failure;
	}
Roopa Prabhu's avatar
Roopa Prabhu committed
4895 4896 4897 4898 4899 4900 4901

	if (mode != BRIDGE_MODE_UNDEF) {
		if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
			nla_nest_cancel(skb, br_afspec);
			goto nla_put_failure;
		}
	}
4902 4903 4904 4905 4906 4907 4908
	if (vlan_fill) {
		err = vlan_fill(skb, dev, filter_mask);
		if (err) {
			nla_nest_cancel(skb, br_afspec);
			goto nla_put_failure;
		}
	}
4909 4910
	nla_nest_end(skb, br_afspec);

4911
	protinfo = nla_nest_start(skb, IFLA_PROTINFO);
4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930
	if (!protinfo)
		goto nla_put_failure;

	if (brport_nla_put_flag(skb, flags, mask,
				IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
	    brport_nla_put_flag(skb, flags, mask,
				IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
	    brport_nla_put_flag(skb, flags, mask,
				IFLA_BRPORT_FAST_LEAVE,
				BR_MULTICAST_FAST_LEAVE) ||
	    brport_nla_put_flag(skb, flags, mask,
				IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
	    brport_nla_put_flag(skb, flags, mask,
				IFLA_BRPORT_LEARNING, BR_LEARNING) ||
	    brport_nla_put_flag(skb, flags, mask,
				IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
	    brport_nla_put_flag(skb, flags, mask,
				IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
	    brport_nla_put_flag(skb, flags, mask,
4931 4932 4933 4934 4935
				IFLA_BRPORT_PROXYARP, BR_PROXYARP) ||
	    brport_nla_put_flag(skb, flags, mask,
				IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD) ||
	    brport_nla_put_flag(skb, flags, mask,
				IFLA_BRPORT_BCAST_FLOOD, BR_BCAST_FLOOD)) {
4936 4937 4938 4939 4940 4941
		nla_nest_cancel(skb, protinfo);
		goto nla_put_failure;
	}

	nla_nest_end(skb, protinfo);

4942 4943
	nlmsg_end(skb, nlh);
	return 0;
4944 4945
nla_put_failure:
	nlmsg_cancel(skb, nlh);
4946
	return err ? err : -EMSGSIZE;
4947
}
4948
EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
4949

4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971
static int valid_bridge_getlink_req(const struct nlmsghdr *nlh,
				    bool strict_check, u32 *filter_mask,
				    struct netlink_ext_ack *extack)
{
	struct nlattr *tb[IFLA_MAX+1];
	int err, i;

	if (strict_check) {
		struct ifinfomsg *ifm;

		if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
			NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump");
			return -EINVAL;
		}

		ifm = nlmsg_data(nlh);
		if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
		    ifm->ifi_change || ifm->ifi_index) {
			NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request");
			return -EINVAL;
		}

4972 4973 4974 4975
		err = nlmsg_parse_deprecated_strict(nlh,
						    sizeof(struct ifinfomsg),
						    tb, IFLA_MAX, ifla_policy,
						    extack);
4976
	} else {
4977 4978 4979
		err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
					     tb, IFLA_MAX, ifla_policy,
					     extack);
4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003
	}
	if (err < 0)
		return err;

	/* new attributes should only be added with strict checking */
	for (i = 0; i <= IFLA_MAX; ++i) {
		if (!tb[i])
			continue;

		switch (i) {
		case IFLA_EXT_MASK:
			*filter_mask = nla_get_u32(tb[i]);
			break;
		default:
			if (strict_check) {
				NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request");
				return -EINVAL;
			}
		}
	}

	return 0;
}

5004 5005
static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
{
5006
	const struct nlmsghdr *nlh = cb->nlh;
5007 5008 5009 5010
	struct net *net = sock_net(skb->sk);
	struct net_device *dev;
	int idx = 0;
	u32 portid = NETLINK_CB(cb->skb).portid;
5011
	u32 seq = nlh->nlmsg_seq;
5012
	u32 filter_mask = 0;
5013
	int err;
5014

5015 5016 5017 5018
	err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask,
				       cb->extack);
	if (err < 0 && cb->strict_check)
		return err;
5019 5020 5021 5022

	rcu_read_lock();
	for_each_netdev_rcu(net, dev) {
		const struct net_device_ops *ops = dev->netdev_ops;
5023
		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
5024

5025
		if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
5026 5027 5028 5029
			if (idx >= cb->args[0]) {
				err = br_dev->netdev_ops->ndo_bridge_getlink(
						skb, portid, seq, dev,
						filter_mask, NLM_F_MULTI);
5030 5031 5032 5033 5034 5035
				if (err < 0 && err != -EOPNOTSUPP) {
					if (likely(skb->len))
						break;

					goto out_err;
				}
5036
			}
5037
			idx++;
5038 5039 5040
		}

		if (ops->ndo_bridge_getlink) {
5041 5042 5043 5044 5045
			if (idx >= cb->args[0]) {
				err = ops->ndo_bridge_getlink(skb, portid,
							      seq, dev,
							      filter_mask,
							      NLM_F_MULTI);
5046 5047 5048 5049 5050 5051
				if (err < 0 && err != -EOPNOTSUPP) {
					if (likely(skb->len))
						break;

					goto out_err;
				}
5052
			}
5053
			idx++;
5054 5055
		}
	}
5056 5057
	err = skb->len;
out_err:
5058 5059 5060
	rcu_read_unlock();
	cb->args[0] = idx;

5061
	return err;
5062 5063
}

5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078
static inline size_t bridge_nlmsg_size(void)
{
	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
		+ nla_total_size(IFNAMSIZ)	/* IFLA_IFNAME */
		+ nla_total_size(MAX_ADDR_LEN)	/* IFLA_ADDRESS */
		+ nla_total_size(sizeof(u32))	/* IFLA_MASTER */
		+ nla_total_size(sizeof(u32))	/* IFLA_MTU */
		+ nla_total_size(sizeof(u32))	/* IFLA_LINK */
		+ nla_total_size(sizeof(u32))	/* IFLA_OPERSTATE */
		+ nla_total_size(sizeof(u8))	/* IFLA_PROTINFO */
		+ nla_total_size(sizeof(struct nlattr))	/* IFLA_AF_SPEC */
		+ nla_total_size(sizeof(u16))	/* IFLA_BRIDGE_FLAGS */
		+ nla_total_size(sizeof(u16));	/* IFLA_BRIDGE_MODE */
}

5079
static int rtnl_bridge_notify(struct net_device *dev)
5080 5081 5082 5083 5084
{
	struct net *net = dev_net(dev);
	struct sk_buff *skb;
	int err = -EOPNOTSUPP;

5085 5086 5087
	if (!dev->netdev_ops->ndo_bridge_getlink)
		return 0;

5088 5089 5090 5091 5092 5093
	skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
	if (!skb) {
		err = -ENOMEM;
		goto errout;
	}

5094
	err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
5095 5096
	if (err < 0)
		goto errout;
5097

5098 5099 5100 5101 5102
	/* Notification info is only filled for bridge ports, not the bridge
	 * device itself. Therefore, a zero notification length is valid and
	 * should not result in an error.
	 */
	if (!skb->len)
5103 5104
		goto errout;

5105 5106 5107 5108 5109
	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
	return 0;
errout:
	WARN_ON(err == -EMSGSIZE);
	kfree_skb(skb);
5110 5111
	if (err)
		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
5112 5113 5114
	return err;
}

5115 5116
static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
			       struct netlink_ext_ack *extack)
5117 5118 5119 5120
{
	struct net *net = sock_net(skb->sk);
	struct ifinfomsg *ifm;
	struct net_device *dev;
5121 5122
	struct nlattr *br_spec, *attr = NULL;
	int rem, err = -EOPNOTSUPP;
5123
	u16 flags = 0;
5124
	bool have_flags = false;
5125 5126 5127 5128 5129 5130 5131 5132 5133 5134

	if (nlmsg_len(nlh) < sizeof(*ifm))
		return -EINVAL;

	ifm = nlmsg_data(nlh);
	if (ifm->ifi_family != AF_BRIDGE)
		return -EPFNOSUPPORT;

	dev = __dev_get_by_index(net, ifm->ifi_index);
	if (!dev) {
5135
		NL_SET_ERR_MSG(extack, "unknown ifindex");
5136 5137 5138
		return -ENODEV;
	}

5139 5140 5141 5142
	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
	if (br_spec) {
		nla_for_each_nested(attr, br_spec, rem) {
			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
5143 5144 5145
				if (nla_len(attr) < sizeof(flags))
					return -EINVAL;

5146
				have_flags = true;
5147 5148 5149 5150 5151 5152 5153
				flags = nla_get_u16(attr);
				break;
			}
		}
	}

	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
5154 5155 5156
		struct net_device *br_dev = netdev_master_upper_dev_get(dev);

		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
5157 5158 5159 5160
			err = -EOPNOTSUPP;
			goto out;
		}

5161 5162
		err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags,
							     extack);
5163 5164
		if (err)
			goto out;
5165 5166

		flags &= ~BRIDGE_FLAGS_MASTER;
5167 5168
	}

5169 5170 5171 5172
	if ((flags & BRIDGE_FLAGS_SELF)) {
		if (!dev->netdev_ops->ndo_bridge_setlink)
			err = -EOPNOTSUPP;
		else
5173
			err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
5174 5175
								  flags,
								  extack);
5176
		if (!err) {
5177
			flags &= ~BRIDGE_FLAGS_SELF;
5178 5179 5180 5181 5182 5183

			/* Generate event to notify upper layer of bridge
			 * change
			 */
			err = rtnl_bridge_notify(dev);
		}
5184
	}
5185

5186
	if (have_flags)
5187
		memcpy(nla_data(attr), &flags, sizeof(flags));
5188 5189 5190 5191
out:
	return err;
}

5192 5193
static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
			       struct netlink_ext_ack *extack)
5194 5195 5196 5197 5198 5199
{
	struct net *net = sock_net(skb->sk);
	struct ifinfomsg *ifm;
	struct net_device *dev;
	struct nlattr *br_spec, *attr = NULL;
	int rem, err = -EOPNOTSUPP;
5200
	u16 flags = 0;
5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211
	bool have_flags = false;

	if (nlmsg_len(nlh) < sizeof(*ifm))
		return -EINVAL;

	ifm = nlmsg_data(nlh);
	if (ifm->ifi_family != AF_BRIDGE)
		return -EPFNOSUPPORT;

	dev = __dev_get_by_index(net, ifm->ifi_index);
	if (!dev) {
5212
		NL_SET_ERR_MSG(extack, "unknown ifindex");
5213 5214 5215 5216 5217 5218 5219
		return -ENODEV;
	}

	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
	if (br_spec) {
		nla_for_each_nested(attr, br_spec, rem) {
			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
5220 5221 5222
				if (nla_len(attr) < sizeof(flags))
					return -EINVAL;

5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237
				have_flags = true;
				flags = nla_get_u16(attr);
				break;
			}
		}
	}

	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
		struct net_device *br_dev = netdev_master_upper_dev_get(dev);

		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
			err = -EOPNOTSUPP;
			goto out;
		}

5238
		err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
5239 5240 5241 5242 5243 5244 5245 5246 5247 5248
		if (err)
			goto out;

		flags &= ~BRIDGE_FLAGS_MASTER;
	}

	if ((flags & BRIDGE_FLAGS_SELF)) {
		if (!dev->netdev_ops->ndo_bridge_dellink)
			err = -EOPNOTSUPP;
		else
5249 5250
			err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
								  flags);
5251

5252
		if (!err) {
5253
			flags &= ~BRIDGE_FLAGS_SELF;
5254 5255 5256 5257 5258 5259

			/* Generate event to notify upper layer of bridge
			 * change
			 */
			err = rtnl_bridge_notify(dev);
		}
5260 5261 5262 5263 5264 5265 5266 5267
	}

	if (have_flags)
		memcpy(nla_data(attr), &flags, sizeof(flags));
out:
	return err;
}

5268 5269 5270 5271 5272 5273
static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
{
	return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
	       (!idxattr || idxattr == attrid);
}

5274 5275
static bool
rtnl_offload_xstats_have_ndo(const struct net_device *dev, int attr_id)
5276
{
5277 5278 5279 5280 5281
	return dev->netdev_ops &&
	       dev->netdev_ops->ndo_has_offload_stats &&
	       dev->netdev_ops->ndo_get_offload_stats &&
	       dev->netdev_ops->ndo_has_offload_stats(dev, attr_id);
}
5282

5283 5284 5285 5286 5287
static unsigned int
rtnl_offload_xstats_get_size_ndo(const struct net_device *dev, int attr_id)
{
	return rtnl_offload_xstats_have_ndo(dev, attr_id) ?
	       sizeof(struct rtnl_link_stats64) : 0;
5288 5289
}

5290 5291 5292
static int
rtnl_offload_xstats_fill_ndo(struct net_device *dev, int attr_id,
			     struct sk_buff *skb)
5293
{
5294
	unsigned int size = rtnl_offload_xstats_get_size_ndo(dev, attr_id);
5295 5296 5297 5298
	struct nlattr *attr = NULL;
	void *attr_data;
	int err;

5299
	if (!size)
5300 5301
		return -ENODATA;

5302 5303 5304 5305
	attr = nla_reserve_64bit(skb, attr_id, size,
				 IFLA_OFFLOAD_XSTATS_UNSPEC);
	if (!attr)
		return -EMSGSIZE;
5306

5307 5308
	attr_data = nla_data(attr);
	memset(attr_data, 0, size);
5309

5310 5311 5312
	err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev, attr_data);
	if (err)
		return err;
5313

5314 5315
	return 0;
}
5316

5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413
static unsigned int
rtnl_offload_xstats_get_size_stats(const struct net_device *dev,
				   enum netdev_offload_xstats_type type)
{
	bool enabled = netdev_offload_xstats_enabled(dev, type);

	return enabled ? sizeof(struct rtnl_hw_stats64) : 0;
}

struct rtnl_offload_xstats_request_used {
	bool request;
	bool used;
};

static int
rtnl_offload_xstats_get_stats(struct net_device *dev,
			      enum netdev_offload_xstats_type type,
			      struct rtnl_offload_xstats_request_used *ru,
			      struct rtnl_hw_stats64 *stats,
			      struct netlink_ext_ack *extack)
{
	bool request;
	bool used;
	int err;

	request = netdev_offload_xstats_enabled(dev, type);
	if (!request) {
		used = false;
		goto out;
	}

	err = netdev_offload_xstats_get(dev, type, stats, &used, extack);
	if (err)
		return err;

out:
	if (ru) {
		ru->request = request;
		ru->used = used;
	}
	return 0;
}

static int
rtnl_offload_xstats_fill_hw_s_info_one(struct sk_buff *skb, int attr_id,
				       struct rtnl_offload_xstats_request_used *ru)
{
	struct nlattr *nest;

	nest = nla_nest_start(skb, attr_id);
	if (!nest)
		return -EMSGSIZE;

	if (nla_put_u8(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO_REQUEST, ru->request))
		goto nla_put_failure;

	if (nla_put_u8(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO_USED, ru->used))
		goto nla_put_failure;

	nla_nest_end(skb, nest);
	return 0;

nla_put_failure:
	nla_nest_cancel(skb, nest);
	return -EMSGSIZE;
}

static int
rtnl_offload_xstats_fill_hw_s_info(struct sk_buff *skb, struct net_device *dev,
				   struct netlink_ext_ack *extack)
{
	enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3;
	struct rtnl_offload_xstats_request_used ru_l3;
	struct nlattr *nest;
	int err;

	err = rtnl_offload_xstats_get_stats(dev, t_l3, &ru_l3, NULL, extack);
	if (err)
		return err;

	nest = nla_nest_start(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO);
	if (!nest)
		return -EMSGSIZE;

	if (rtnl_offload_xstats_fill_hw_s_info_one(skb,
						   IFLA_OFFLOAD_XSTATS_L3_STATS,
						   &ru_l3))
		goto nla_put_failure;

	nla_nest_end(skb, nest);
	return 0;

nla_put_failure:
	nla_nest_cancel(skb, nest);
	return -EMSGSIZE;
}

5414
static int rtnl_offload_xstats_fill(struct sk_buff *skb, struct net_device *dev,
5415 5416
				    int *prividx, u32 off_filter_mask,
				    struct netlink_ext_ack *extack)
5417
{
5418 5419 5420
	enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3;
	int attr_id_hw_s_info = IFLA_OFFLOAD_XSTATS_HW_S_INFO;
	int attr_id_l3_stats = IFLA_OFFLOAD_XSTATS_L3_STATS;
5421 5422 5423 5424
	int attr_id_cpu_hit = IFLA_OFFLOAD_XSTATS_CPU_HIT;
	bool have_data = false;
	int err;

5425 5426 5427
	if (*prividx <= attr_id_cpu_hit &&
	    (off_filter_mask &
	     IFLA_STATS_FILTER_BIT(attr_id_cpu_hit))) {
5428 5429 5430 5431 5432 5433 5434
		err = rtnl_offload_xstats_fill_ndo(dev, attr_id_cpu_hit, skb);
		if (!err) {
			have_data = true;
		} else if (err != -ENODATA) {
			*prividx = attr_id_cpu_hit;
			return err;
		}
5435 5436
	}

5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456
	if (*prividx <= attr_id_hw_s_info &&
	    (off_filter_mask & IFLA_STATS_FILTER_BIT(attr_id_hw_s_info))) {
		*prividx = attr_id_hw_s_info;

		err = rtnl_offload_xstats_fill_hw_s_info(skb, dev, extack);
		if (err)
			return err;

		have_data = true;
		*prividx = 0;
	}

	if (*prividx <= attr_id_l3_stats &&
	    (off_filter_mask & IFLA_STATS_FILTER_BIT(attr_id_l3_stats))) {
		unsigned int size_l3;
		struct nlattr *attr;

		*prividx = attr_id_l3_stats;

		size_l3 = rtnl_offload_xstats_get_size_stats(dev, t_l3);
5457 5458
		if (!size_l3)
			goto skip_l3_stats;
5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469
		attr = nla_reserve_64bit(skb, attr_id_l3_stats, size_l3,
					 IFLA_OFFLOAD_XSTATS_UNSPEC);
		if (!attr)
			return -EMSGSIZE;

		err = rtnl_offload_xstats_get_stats(dev, t_l3, NULL,
						    nla_data(attr), extack);
		if (err)
			return err;

		have_data = true;
5470
skip_l3_stats:
5471 5472 5473
		*prividx = 0;
	}

5474
	if (!have_data)
5475 5476 5477 5478 5479 5480
		return -ENODATA;

	*prividx = 0;
	return 0;
}

5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505
static unsigned int
rtnl_offload_xstats_get_size_hw_s_info_one(const struct net_device *dev,
					   enum netdev_offload_xstats_type type)
{
	bool enabled = netdev_offload_xstats_enabled(dev, type);

	return nla_total_size(0) +
		/* IFLA_OFFLOAD_XSTATS_HW_S_INFO_REQUEST */
		nla_total_size(sizeof(u8)) +
		/* IFLA_OFFLOAD_XSTATS_HW_S_INFO_USED */
		(enabled ? nla_total_size(sizeof(u8)) : 0) +
		0;
}

static unsigned int
rtnl_offload_xstats_get_size_hw_s_info(const struct net_device *dev)
{
	enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3;

	return nla_total_size(0) +
		/* IFLA_OFFLOAD_XSTATS_L3_STATS */
		rtnl_offload_xstats_get_size_hw_s_info_one(dev, t_l3) +
		0;
}

5506 5507
static int rtnl_offload_xstats_get_size(const struct net_device *dev,
					u32 off_filter_mask)
5508
{
5509
	enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3;
5510
	int attr_id_cpu_hit = IFLA_OFFLOAD_XSTATS_CPU_HIT;
5511 5512 5513
	int nla_size = 0;
	int size;

5514 5515 5516 5517 5518
	if (off_filter_mask &
	    IFLA_STATS_FILTER_BIT(attr_id_cpu_hit)) {
		size = rtnl_offload_xstats_get_size_ndo(dev, attr_id_cpu_hit);
		nla_size += nla_total_size_64bit(size);
	}
5519

5520 5521 5522 5523 5524 5525 5526 5527 5528 5529
	if (off_filter_mask &
	    IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO))
		nla_size += rtnl_offload_xstats_get_size_hw_s_info(dev);

	if (off_filter_mask &
	    IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_L3_STATS)) {
		size = rtnl_offload_xstats_get_size_stats(dev, t_l3);
		nla_size += nla_total_size_64bit(size);
	}

5530 5531 5532 5533 5534 5535
	if (nla_size != 0)
		nla_size += nla_total_size(0);

	return nla_size;
}

5536 5537 5538 5539 5540 5541 5542
struct rtnl_stats_dump_filters {
	/* mask[0] filters outer attributes. Then individual nests have their
	 * filtering mask at the index of the nested attribute.
	 */
	u32 mask[IFLA_STATS_MAX + 1];
};

5543 5544
static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
			       int type, u32 pid, u32 seq, u32 change,
5545 5546
			       unsigned int flags,
			       const struct rtnl_stats_dump_filters *filters,
5547 5548
			       int *idxattr, int *prividx,
			       struct netlink_ext_ack *extack)
5549
{
5550
	unsigned int filter_mask = filters->mask[0];
5551 5552 5553
	struct if_stats_msg *ifsm;
	struct nlmsghdr *nlh;
	struct nlattr *attr;
5554
	int s_prividx = *prividx;
5555
	int err;
5556 5557 5558 5559 5560 5561 5562 5563

	ASSERT_RTNL();

	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
	if (!nlh)
		return -EMSGSIZE;

	ifsm = nlmsg_data(nlh);
5564 5565 5566
	ifsm->family = PF_UNSPEC;
	ifsm->pad1 = 0;
	ifsm->pad2 = 0;
5567 5568 5569
	ifsm->ifindex = dev->ifindex;
	ifsm->filter_mask = filter_mask;

5570
	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
5571 5572
		struct rtnl_link_stats64 *sp;

5573 5574 5575
		attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
					 sizeof(struct rtnl_link_stats64),
					 IFLA_STATS_UNSPEC);
5576 5577
		if (!attr) {
			err = -EMSGSIZE;
5578
			goto nla_put_failure;
5579
		}
5580 5581 5582 5583 5584

		sp = nla_data(attr);
		dev_get_stats(dev, sp);
	}

5585 5586 5587 5588 5589
	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
		const struct rtnl_link_ops *ops = dev->rtnl_link_ops;

		if (ops && ops->fill_linkxstats) {
			*idxattr = IFLA_STATS_LINK_XSTATS;
5590 5591
			attr = nla_nest_start_noflag(skb,
						     IFLA_STATS_LINK_XSTATS);
5592 5593
			if (!attr) {
				err = -EMSGSIZE;
5594
				goto nla_put_failure;
5595
			}
5596

5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614
			err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
			nla_nest_end(skb, attr);
			if (err)
				goto nla_put_failure;
			*idxattr = 0;
		}
	}

	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
			     *idxattr)) {
		const struct rtnl_link_ops *ops = NULL;
		const struct net_device *master;

		master = netdev_master_upper_dev_get(dev);
		if (master)
			ops = master->rtnl_link_ops;
		if (ops && ops->fill_linkxstats) {
			*idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
5615 5616
			attr = nla_nest_start_noflag(skb,
						     IFLA_STATS_LINK_XSTATS_SLAVE);
5617 5618
			if (!attr) {
				err = -EMSGSIZE;
5619
				goto nla_put_failure;
5620
			}
5621 5622

			err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
5623 5624 5625 5626 5627 5628 5629
			nla_nest_end(skb, attr);
			if (err)
				goto nla_put_failure;
			*idxattr = 0;
		}
	}

5630 5631
	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
			     *idxattr)) {
5632 5633 5634
		u32 off_filter_mask;

		off_filter_mask = filters->mask[IFLA_STATS_LINK_OFFLOAD_XSTATS];
5635
		*idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
5636 5637
		attr = nla_nest_start_noflag(skb,
					     IFLA_STATS_LINK_OFFLOAD_XSTATS);
5638 5639
		if (!attr) {
			err = -EMSGSIZE;
5640
			goto nla_put_failure;
5641
		}
5642

5643
		err = rtnl_offload_xstats_fill(skb, dev, prividx,
5644
					       off_filter_mask, extack);
5645 5646 5647 5648 5649 5650 5651 5652 5653 5654
		if (err == -ENODATA)
			nla_nest_cancel(skb, attr);
		else
			nla_nest_end(skb, attr);

		if (err && err != -ENODATA)
			goto nla_put_failure;
		*idxattr = 0;
	}

5655 5656 5657 5658
	if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
		struct rtnl_af_ops *af_ops;

		*idxattr = IFLA_STATS_AF_SPEC;
5659
		attr = nla_nest_start_noflag(skb, IFLA_STATS_AF_SPEC);
5660 5661
		if (!attr) {
			err = -EMSGSIZE;
5662
			goto nla_put_failure;
5663
		}
5664

5665 5666
		rcu_read_lock();
		list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
5667 5668 5669
			if (af_ops->fill_stats_af) {
				struct nlattr *af;

5670 5671
				af = nla_nest_start_noflag(skb,
							   af_ops->family);
5672 5673
				if (!af) {
					rcu_read_unlock();
5674
					err = -EMSGSIZE;
5675
					goto nla_put_failure;
5676
				}
5677 5678
				err = af_ops->fill_stats_af(skb, dev);

5679
				if (err == -ENODATA) {
5680
					nla_nest_cancel(skb, af);
5681 5682
				} else if (err < 0) {
					rcu_read_unlock();
5683
					goto nla_put_failure;
5684
				}
5685 5686 5687 5688

				nla_nest_end(skb, af);
			}
		}
5689
		rcu_read_unlock();
5690 5691 5692 5693 5694 5695

		nla_nest_end(skb, attr);

		*idxattr = 0;
	}

5696 5697 5698 5699 5700
	nlmsg_end(skb, nlh);

	return 0;

nla_put_failure:
5701 5702 5703 5704 5705
	/* not a multi message or no progress mean a real error */
	if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
		nlmsg_cancel(skb, nlh);
	else
		nlmsg_end(skb, nlh);
5706

5707
	return err;
5708 5709 5710
}

static size_t if_nlmsg_stats_size(const struct net_device *dev,
5711
				  const struct rtnl_stats_dump_filters *filters)
5712
{
5713
	size_t size = NLMSG_ALIGN(sizeof(struct if_stats_msg));
5714
	unsigned int filter_mask = filters->mask[0];
5715

5716
	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
5717 5718
		size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));

5719 5720
	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
		const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
5721
		int attr = IFLA_STATS_LINK_XSTATS;
5722 5723

		if (ops && ops->get_linkxstats_size) {
5724 5725
			size += nla_total_size(ops->get_linkxstats_size(dev,
									attr));
5726 5727 5728 5729 5730
			/* for IFLA_STATS_LINK_XSTATS */
			size += nla_total_size(0);
		}
	}

5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749
	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
		struct net_device *_dev = (struct net_device *)dev;
		const struct rtnl_link_ops *ops = NULL;
		const struct net_device *master;

		/* netdev_master_upper_dev_get can't take const */
		master = netdev_master_upper_dev_get(_dev);
		if (master)
			ops = master->rtnl_link_ops;
		if (ops && ops->get_linkxstats_size) {
			int attr = IFLA_STATS_LINK_XSTATS_SLAVE;

			size += nla_total_size(ops->get_linkxstats_size(dev,
									attr));
			/* for IFLA_STATS_LINK_XSTATS_SLAVE */
			size += nla_total_size(0);
		}
	}

5750 5751 5752 5753 5754 5755
	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0)) {
		u32 off_filter_mask;

		off_filter_mask = filters->mask[IFLA_STATS_LINK_OFFLOAD_XSTATS];
		size += rtnl_offload_xstats_get_size(dev, off_filter_mask);
	}
5756

5757 5758 5759 5760 5761 5762
	if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
		struct rtnl_af_ops *af_ops;

		/* for IFLA_STATS_AF_SPEC */
		size += nla_total_size(0);

5763 5764
		rcu_read_lock();
		list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
5765 5766 5767 5768 5769 5770 5771 5772
			if (af_ops->get_stats_af_size) {
				size += nla_total_size(
					af_ops->get_stats_af_size(dev));

				/* for AF_* */
				size += nla_total_size(0);
			}
		}
5773
		rcu_read_unlock();
5774 5775
	}

5776 5777 5778
	return size;
}

5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792
#define RTNL_STATS_OFFLOAD_XSTATS_VALID ((1 << __IFLA_OFFLOAD_XSTATS_MAX) - 1)

static const struct nla_policy
rtnl_stats_get_policy_filters[IFLA_STATS_MAX + 1] = {
	[IFLA_STATS_LINK_OFFLOAD_XSTATS] =
		    NLA_POLICY_MASK(NLA_U32, RTNL_STATS_OFFLOAD_XSTATS_VALID),
};

static const struct nla_policy
rtnl_stats_get_policy[IFLA_STATS_GETSET_MAX + 1] = {
	[IFLA_STATS_GET_FILTERS] =
		    NLA_POLICY_NESTED(rtnl_stats_get_policy_filters),
};

5793 5794
static const struct nla_policy
ifla_stats_set_policy[IFLA_STATS_GETSET_MAX + 1] = {
5795
	[IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS] = NLA_POLICY_MAX(NLA_U8, 1),
5796 5797
};

5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851
static int rtnl_stats_get_parse_filters(struct nlattr *ifla_filters,
					struct rtnl_stats_dump_filters *filters,
					struct netlink_ext_ack *extack)
{
	struct nlattr *tb[IFLA_STATS_MAX + 1];
	int err;
	int at;

	err = nla_parse_nested(tb, IFLA_STATS_MAX, ifla_filters,
			       rtnl_stats_get_policy_filters, extack);
	if (err < 0)
		return err;

	for (at = 1; at <= IFLA_STATS_MAX; at++) {
		if (tb[at]) {
			if (!(filters->mask[0] & IFLA_STATS_FILTER_BIT(at))) {
				NL_SET_ERR_MSG(extack, "Filtered attribute not enabled in filter_mask");
				return -EINVAL;
			}
			filters->mask[at] = nla_get_u32(tb[at]);
		}
	}

	return 0;
}

static int rtnl_stats_get_parse(const struct nlmsghdr *nlh,
				u32 filter_mask,
				struct rtnl_stats_dump_filters *filters,
				struct netlink_ext_ack *extack)
{
	struct nlattr *tb[IFLA_STATS_GETSET_MAX + 1];
	int err;
	int i;

	filters->mask[0] = filter_mask;
	for (i = 1; i < ARRAY_SIZE(filters->mask); i++)
		filters->mask[i] = -1U;

	err = nlmsg_parse(nlh, sizeof(struct if_stats_msg), tb,
			  IFLA_STATS_GETSET_MAX, rtnl_stats_get_policy, extack);
	if (err < 0)
		return err;

	if (tb[IFLA_STATS_GET_FILTERS]) {
		err = rtnl_stats_get_parse_filters(tb[IFLA_STATS_GET_FILTERS],
						   filters, extack);
		if (err)
			return err;
	}

	return 0;
}

5852 5853 5854 5855 5856
static int rtnl_valid_stats_req(const struct nlmsghdr *nlh, bool strict_check,
				bool is_dump, struct netlink_ext_ack *extack)
{
	struct if_stats_msg *ifsm;

5857
	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifsm))) {
5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873
		NL_SET_ERR_MSG(extack, "Invalid header for stats dump");
		return -EINVAL;
	}

	if (!strict_check)
		return 0;

	ifsm = nlmsg_data(nlh);

	/* only requests using strict checks can pass data to influence
	 * the dump. The legacy exception is filter_mask.
	 */
	if (ifsm->pad1 || ifsm->pad2 || (is_dump && ifsm->ifindex)) {
		NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request");
		return -EINVAL;
	}
5874 5875 5876 5877
	if (ifsm->filter_mask >= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX + 1)) {
		NL_SET_ERR_MSG(extack, "Invalid stats requested through filter mask");
		return -EINVAL;
	}
5878 5879 5880 5881

	return 0;
}

5882 5883
static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
			  struct netlink_ext_ack *extack)
5884
{
5885
	struct rtnl_stats_dump_filters filters;
5886 5887
	struct net *net = sock_net(skb->sk);
	struct net_device *dev = NULL;
5888 5889
	int idxattr = 0, prividx = 0;
	struct if_stats_msg *ifsm;
5890 5891 5892
	struct sk_buff *nskb;
	int err;

5893 5894 5895 5896
	err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb),
				   false, extack);
	if (err)
		return err;
5897

5898 5899 5900 5901 5902 5903 5904 5905 5906
	ifsm = nlmsg_data(nlh);
	if (ifsm->ifindex > 0)
		dev = __dev_get_by_index(net, ifsm->ifindex);
	else
		return -EINVAL;

	if (!dev)
		return -ENODEV;

5907
	if (!ifsm->filter_mask) {
5908
		NL_SET_ERR_MSG(extack, "Filter mask must be set for stats get");
5909
		return -EINVAL;
5910
	}
5911

5912 5913 5914 5915 5916
	err = rtnl_stats_get_parse(nlh, ifsm->filter_mask, &filters, extack);
	if (err)
		return err;

	nskb = nlmsg_new(if_nlmsg_stats_size(dev, &filters), GFP_KERNEL);
5917 5918 5919 5920 5921
	if (!nskb)
		return -ENOBUFS;

	err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
				  NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
5922
				  0, &filters, &idxattr, &prividx, extack);
5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935
	if (err < 0) {
		/* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
		WARN_ON(err == -EMSGSIZE);
		kfree_skb(nskb);
	} else {
		err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
	}

	return err;
}

static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
{
5936
	struct netlink_ext_ack *extack = cb->extack;
5937
	int h, s_h, err, s_idx, s_idxattr, s_prividx;
5938
	struct rtnl_stats_dump_filters filters;
5939
	struct net *net = sock_net(skb->sk);
5940
	unsigned int flags = NLM_F_MULTI;
5941 5942
	struct if_stats_msg *ifsm;
	struct hlist_head *head;
5943 5944
	struct net_device *dev;
	int idx = 0;
5945 5946 5947

	s_h = cb->args[0];
	s_idx = cb->args[1];
5948 5949
	s_idxattr = cb->args[2];
	s_prividx = cb->args[3];
5950 5951 5952

	cb->seq = net->dev_base_seq;

5953 5954 5955
	err = rtnl_valid_stats_req(cb->nlh, cb->strict_check, true, extack);
	if (err)
		return err;
5956

5957
	ifsm = nlmsg_data(cb->nlh);
5958
	if (!ifsm->filter_mask) {
5959
		NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump");
5960
		return -EINVAL;
5961
	}
5962

5963 5964 5965 5966 5967
	err = rtnl_stats_get_parse(cb->nlh, ifsm->filter_mask, &filters,
				   extack);
	if (err)
		return err;

5968 5969 5970 5971 5972 5973 5974 5975 5976
	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
		idx = 0;
		head = &net->dev_index_head[h];
		hlist_for_each_entry(dev, head, index_hlist) {
			if (idx < s_idx)
				goto cont;
			err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
						  NETLINK_CB(cb->skb).portid,
						  cb->nlh->nlmsg_seq, 0,
5977
						  flags, &filters,
5978 5979
						  &s_idxattr, &s_prividx,
						  extack);
5980 5981 5982 5983 5984 5985 5986
			/* If we ran out of room on the first message,
			 * we're in trouble
			 */
			WARN_ON((err == -EMSGSIZE) && (skb->len == 0));

			if (err < 0)
				goto out;
5987 5988
			s_prividx = 0;
			s_idxattr = 0;
5989 5990 5991 5992 5993 5994
			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
cont:
			idx++;
		}
	}
out:
5995 5996
	cb->args[3] = s_prividx;
	cb->args[2] = s_idxattr;
5997 5998 5999 6000 6001 6002
	cb->args[1] = idx;
	cb->args[0] = h;

	return skb->len;
}

6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037
void rtnl_offload_xstats_notify(struct net_device *dev)
{
	struct rtnl_stats_dump_filters response_filters = {};
	struct net *net = dev_net(dev);
	int idxattr = 0, prividx = 0;
	struct sk_buff *skb;
	int err = -ENOBUFS;

	ASSERT_RTNL();

	response_filters.mask[0] |=
		IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_OFFLOAD_XSTATS);
	response_filters.mask[IFLA_STATS_LINK_OFFLOAD_XSTATS] |=
		IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO);

	skb = nlmsg_new(if_nlmsg_stats_size(dev, &response_filters),
			GFP_KERNEL);
	if (!skb)
		goto errout;

	err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS, 0, 0, 0, 0,
				  &response_filters, &idxattr, &prividx, NULL);
	if (err < 0) {
		kfree_skb(skb);
		goto errout;
	}

	rtnl_notify(skb, net, 0, RTNLGRP_STATS, NULL, GFP_KERNEL);
	return;

errout:
	rtnl_set_sk_err(net, RTNLGRP_STATS, err);
}
EXPORT_SYMBOL(rtnl_offload_xstats_notify);

6038 6039 6040
static int rtnl_stats_set(struct sk_buff *skb, struct nlmsghdr *nlh,
			  struct netlink_ext_ack *extack)
{
6041
	enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3;
6042 6043 6044 6045 6046
	struct rtnl_stats_dump_filters response_filters = {};
	struct nlattr *tb[IFLA_STATS_GETSET_MAX + 1];
	struct net *net = sock_net(skb->sk);
	struct net_device *dev = NULL;
	struct if_stats_msg *ifsm;
6047
	bool notify = false;
6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078
	int err;

	err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb),
				   false, extack);
	if (err)
		return err;

	ifsm = nlmsg_data(nlh);
	if (ifsm->family != AF_UNSPEC) {
		NL_SET_ERR_MSG(extack, "Address family should be AF_UNSPEC");
		return -EINVAL;
	}

	if (ifsm->ifindex > 0)
		dev = __dev_get_by_index(net, ifsm->ifindex);
	else
		return -EINVAL;

	if (!dev)
		return -ENODEV;

	if (ifsm->filter_mask) {
		NL_SET_ERR_MSG(extack, "Filter mask must be 0 for stats set");
		return -EINVAL;
	}

	err = nlmsg_parse(nlh, sizeof(*ifsm), tb, IFLA_STATS_GETSET_MAX,
			  ifla_stats_set_policy, extack);
	if (err < 0)
		return err;

6079 6080
	if (tb[IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS]) {
		u8 req = nla_get_u8(tb[IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS]);
6081

6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095
		if (req)
			err = netdev_offload_xstats_enable(dev, t_l3, extack);
		else
			err = netdev_offload_xstats_disable(dev, t_l3);

		if (!err)
			notify = true;
		else if (err != -EALREADY)
			return err;

		response_filters.mask[0] |=
			IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_OFFLOAD_XSTATS);
		response_filters.mask[IFLA_STATS_LINK_OFFLOAD_XSTATS] |=
			IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO);
6096 6097
	}

6098 6099 6100 6101
	if (notify)
		rtnl_offload_xstats_notify(dev);

	return 0;
6102 6103
}

6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189
static int rtnl_mdb_valid_dump_req(const struct nlmsghdr *nlh,
				   struct netlink_ext_ack *extack)
{
	struct br_port_msg *bpm;

	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*bpm))) {
		NL_SET_ERR_MSG(extack, "Invalid header for mdb dump request");
		return -EINVAL;
	}

	bpm = nlmsg_data(nlh);
	if (bpm->ifindex) {
		NL_SET_ERR_MSG(extack, "Filtering by device index is not supported for mdb dump request");
		return -EINVAL;
	}
	if (nlmsg_attrlen(nlh, sizeof(*bpm))) {
		NL_SET_ERR_MSG(extack, "Invalid data after header in mdb dump request");
		return -EINVAL;
	}

	return 0;
}

struct rtnl_mdb_dump_ctx {
	long idx;
};

static int rtnl_mdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
{
	struct rtnl_mdb_dump_ctx *ctx = (void *)cb->ctx;
	struct net *net = sock_net(skb->sk);
	struct net_device *dev;
	int idx, s_idx;
	int err;

	NL_ASSERT_DUMP_CTX_FITS(struct rtnl_mdb_dump_ctx);

	if (cb->strict_check) {
		err = rtnl_mdb_valid_dump_req(cb->nlh, cb->extack);
		if (err)
			return err;
	}

	s_idx = ctx->idx;
	idx = 0;

	for_each_netdev(net, dev) {
		if (idx < s_idx)
			goto skip;
		if (!dev->netdev_ops->ndo_mdb_dump)
			goto skip;

		err = dev->netdev_ops->ndo_mdb_dump(dev, skb, cb);
		if (err == -EMSGSIZE)
			goto out;
		/* Moving on to next device, reset markers and sequence
		 * counters since they are all maintained per-device.
		 */
		memset(cb->ctx, 0, sizeof(cb->ctx));
		cb->prev_seq = 0;
		cb->seq = 0;
skip:
		idx++;
	}

out:
	ctx->idx = idx;
	return skb->len;
}

static int rtnl_validate_mdb_entry(const struct nlattr *attr,
				   struct netlink_ext_ack *extack)
{
	struct br_mdb_entry *entry = nla_data(attr);

	if (nla_len(attr) != sizeof(struct br_mdb_entry)) {
		NL_SET_ERR_MSG_ATTR(extack, attr, "Invalid attribute length");
		return -EINVAL;
	}

	if (entry->ifindex == 0) {
		NL_SET_ERR_MSG(extack, "Zero entry ifindex is not allowed");
		return -EINVAL;
	}

	if (entry->addr.proto == htons(ETH_P_IP)) {
6190 6191 6192
		if (!ipv4_is_multicast(entry->addr.u.ip4) &&
		    !ipv4_is_zeronet(entry->addr.u.ip4)) {
			NL_SET_ERR_MSG(extack, "IPv4 entry group address is not multicast or 0.0.0.0");
6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314
			return -EINVAL;
		}
		if (ipv4_is_local_multicast(entry->addr.u.ip4)) {
			NL_SET_ERR_MSG(extack, "IPv4 entry group address is local multicast");
			return -EINVAL;
		}
#if IS_ENABLED(CONFIG_IPV6)
	} else if (entry->addr.proto == htons(ETH_P_IPV6)) {
		if (ipv6_addr_is_ll_all_nodes(&entry->addr.u.ip6)) {
			NL_SET_ERR_MSG(extack, "IPv6 entry group address is link-local all nodes");
			return -EINVAL;
		}
#endif
	} else if (entry->addr.proto == 0) {
		/* L2 mdb */
		if (!is_multicast_ether_addr(entry->addr.u.mac_addr)) {
			NL_SET_ERR_MSG(extack, "L2 entry group is not multicast");
			return -EINVAL;
		}
	} else {
		NL_SET_ERR_MSG(extack, "Unknown entry protocol");
		return -EINVAL;
	}

	if (entry->state != MDB_PERMANENT && entry->state != MDB_TEMPORARY) {
		NL_SET_ERR_MSG(extack, "Unknown entry state");
		return -EINVAL;
	}
	if (entry->vid >= VLAN_VID_MASK) {
		NL_SET_ERR_MSG(extack, "Invalid entry VLAN id");
		return -EINVAL;
	}

	return 0;
}

static const struct nla_policy mdba_policy[MDBA_SET_ENTRY_MAX + 1] = {
	[MDBA_SET_ENTRY_UNSPEC] = { .strict_start_type = MDBA_SET_ENTRY_ATTRS + 1 },
	[MDBA_SET_ENTRY] = NLA_POLICY_VALIDATE_FN(NLA_BINARY,
						  rtnl_validate_mdb_entry,
						  sizeof(struct br_mdb_entry)),
	[MDBA_SET_ENTRY_ATTRS] = { .type = NLA_NESTED },
};

static int rtnl_mdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
			struct netlink_ext_ack *extack)
{
	struct nlattr *tb[MDBA_SET_ENTRY_MAX + 1];
	struct net *net = sock_net(skb->sk);
	struct br_port_msg *bpm;
	struct net_device *dev;
	int err;

	err = nlmsg_parse_deprecated(nlh, sizeof(*bpm), tb,
				     MDBA_SET_ENTRY_MAX, mdba_policy, extack);
	if (err)
		return err;

	bpm = nlmsg_data(nlh);
	if (!bpm->ifindex) {
		NL_SET_ERR_MSG(extack, "Invalid ifindex");
		return -EINVAL;
	}

	dev = __dev_get_by_index(net, bpm->ifindex);
	if (!dev) {
		NL_SET_ERR_MSG(extack, "Device doesn't exist");
		return -ENODEV;
	}

	if (NL_REQ_ATTR_CHECK(extack, NULL, tb, MDBA_SET_ENTRY)) {
		NL_SET_ERR_MSG(extack, "Missing MDBA_SET_ENTRY attribute");
		return -EINVAL;
	}

	if (!dev->netdev_ops->ndo_mdb_add) {
		NL_SET_ERR_MSG(extack, "Device does not support MDB operations");
		return -EOPNOTSUPP;
	}

	return dev->netdev_ops->ndo_mdb_add(dev, tb, nlh->nlmsg_flags, extack);
}

static int rtnl_mdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
			struct netlink_ext_ack *extack)
{
	struct nlattr *tb[MDBA_SET_ENTRY_MAX + 1];
	struct net *net = sock_net(skb->sk);
	struct br_port_msg *bpm;
	struct net_device *dev;
	int err;

	err = nlmsg_parse_deprecated(nlh, sizeof(*bpm), tb,
				     MDBA_SET_ENTRY_MAX, mdba_policy, extack);
	if (err)
		return err;

	bpm = nlmsg_data(nlh);
	if (!bpm->ifindex) {
		NL_SET_ERR_MSG(extack, "Invalid ifindex");
		return -EINVAL;
	}

	dev = __dev_get_by_index(net, bpm->ifindex);
	if (!dev) {
		NL_SET_ERR_MSG(extack, "Device doesn't exist");
		return -ENODEV;
	}

	if (NL_REQ_ATTR_CHECK(extack, NULL, tb, MDBA_SET_ENTRY)) {
		NL_SET_ERR_MSG(extack, "Missing MDBA_SET_ENTRY attribute");
		return -EINVAL;
	}

	if (!dev->netdev_ops->ndo_mdb_del) {
		NL_SET_ERR_MSG(extack, "Device does not support MDB operations");
		return -EOPNOTSUPP;
	}

	return dev->netdev_ops->ndo_mdb_del(dev, tb, extack);
}

Linus Torvalds's avatar
Linus Torvalds committed
6315 6316
/* Process one rtnetlink message. */

6317 6318
static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
			     struct netlink_ext_ack *extack)
Linus Torvalds's avatar
Linus Torvalds committed
6319
{
6320
	struct net *net = sock_net(skb->sk);
6321
	struct rtnl_link *link;
6322
	enum rtnl_kinds kind;
6323
	struct module *owner;
6324
	int err = -EOPNOTSUPP;
6325
	rtnl_doit_func doit;
6326
	unsigned int flags;
Linus Torvalds's avatar
Linus Torvalds committed
6327 6328 6329 6330 6331
	int family;
	int type;

	type = nlh->nlmsg_type;
	if (type > RTM_MAX)
6332
		return -EOPNOTSUPP;
Linus Torvalds's avatar
Linus Torvalds committed
6333 6334 6335 6336

	type -= RTM_BASE;

	/* All the messages must have at least 1 byte length */
6337
	if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
Linus Torvalds's avatar
Linus Torvalds committed
6338 6339
		return 0;

6340
	family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
6341
	kind = rtnl_msgtype_kind(type);
Linus Torvalds's avatar
Linus Torvalds committed
6342

6343
	if (kind != RTNL_KIND_GET && !netlink_net_capable(skb, CAP_NET_ADMIN))
6344
		return -EPERM;
Linus Torvalds's avatar
Linus Torvalds committed
6345

6346
	rcu_read_lock();
6347
	if (kind == RTNL_KIND_GET && (nlh->nlmsg_flags & NLM_F_DUMP)) {
6348
		struct sock *rtnl;
6349
		rtnl_dumpit_func dumpit;
6350
		u32 min_dump_alloc = 0;
Linus Torvalds's avatar
Linus Torvalds committed
6351

6352 6353
		link = rtnl_get_link(family, type);
		if (!link || !link->dumpit) {
6354
			family = PF_UNSPEC;
6355 6356
			link = rtnl_get_link(family, type);
			if (!link || !link->dumpit)
6357 6358
				goto err_unlock;
		}
6359
		owner = link->owner;
6360
		dumpit = link->dumpit;
6361

6362
		if (type == RTM_GETLINK - RTM_BASE)
6363
			min_dump_alloc = rtnl_calcit(skb, nlh);
6364

6365 6366 6367 6368 6369
		err = 0;
		/* need to do this before rcu_read_unlock() */
		if (!try_module_get(owner))
			err = -EPROTONOSUPPORT;

6370 6371
		rcu_read_unlock();

6372
		rtnl = net->rtnl;
6373
		if (err == 0) {
6374 6375 6376
			struct netlink_dump_control c = {
				.dump		= dumpit,
				.min_dump_alloc	= min_dump_alloc,
6377
				.module		= owner,
6378 6379
			};
			err = netlink_dump_start(rtnl, skb, nlh, &c);
6380 6381 6382 6383
			/* netlink_dump_start() will keep a reference on
			 * module if dump is still in progress.
			 */
			module_put(owner);
6384
		}
6385
		return err;
Linus Torvalds's avatar
Linus Torvalds committed
6386 6387
	}

6388 6389
	link = rtnl_get_link(family, type);
	if (!link || !link->doit) {
6390
		family = PF_UNSPEC;
6391 6392 6393
		link = rtnl_get_link(PF_UNSPEC, type);
		if (!link || !link->doit)
			goto out_unlock;
6394 6395
	}

6396 6397 6398 6399 6400 6401
	owner = link->owner;
	if (!try_module_get(owner)) {
		err = -EPROTONOSUPPORT;
		goto out_unlock;
	}

6402
	flags = link->flags;
6403 6404 6405
	if (kind == RTNL_KIND_DEL && (nlh->nlmsg_flags & NLM_F_BULK) &&
	    !(flags & RTNL_FLAG_BULK_DEL_SUPPORTED)) {
		NL_SET_ERR_MSG(extack, "Bulk delete is not supported");
6406
		module_put(owner);
6407 6408 6409
		goto err_unlock;
	}

6410
	if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
6411
		doit = link->doit;
6412 6413 6414
		rcu_read_unlock();
		if (doit)
			err = doit(skb, nlh, extack);
6415
		module_put(owner);
6416 6417
		return err;
	}
6418
	rcu_read_unlock();
Linus Torvalds's avatar
Linus Torvalds committed
6419

6420
	rtnl_lock();
6421 6422 6423
	link = rtnl_get_link(family, type);
	if (link && link->doit)
		err = link->doit(skb, nlh, extack);
6424
	rtnl_unlock();
6425

6426 6427
	module_put(owner);

6428 6429 6430 6431
	return err;

out_unlock:
	rcu_read_unlock();
6432 6433 6434
	return err;

err_unlock:
6435
	rcu_read_unlock();
6436
	return -EOPNOTSUPP;
Linus Torvalds's avatar
Linus Torvalds committed
6437 6438
}

6439
static void rtnetlink_rcv(struct sk_buff *skb)
Linus Torvalds's avatar
Linus Torvalds committed
6440
{
6441
	netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
Linus Torvalds's avatar
Linus Torvalds committed
6442 6443
}

6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455
static int rtnetlink_bind(struct net *net, int group)
{
	switch (group) {
	case RTNLGRP_IPV4_MROUTE_R:
	case RTNLGRP_IPV6_MROUTE_R:
		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
			return -EPERM;
		break;
	}
	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
6456 6457
static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
{
6458
	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
6459

Linus Torvalds's avatar
Linus Torvalds committed
6460
	switch (event) {
6461
	case NETDEV_REBOOT:
6462
	case NETDEV_CHANGEMTU:
6463
	case NETDEV_CHANGEADDR:
6464 6465 6466
	case NETDEV_CHANGENAME:
	case NETDEV_FEAT_CHANGE:
	case NETDEV_BONDING_FAILOVER:
6467
	case NETDEV_POST_TYPE_CHANGE:
6468
	case NETDEV_NOTIFY_PEERS:
6469
	case NETDEV_CHANGEUPPER:
6470 6471
	case NETDEV_RESEND_IGMP:
	case NETDEV_CHANGEINFODATA:
6472
	case NETDEV_CHANGELOWERSTATE:
6473
	case NETDEV_CHANGE_TX_QUEUE_LEN:
6474
		rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
6475
				   GFP_KERNEL, NULL, 0, 0, NULL);
Linus Torvalds's avatar
Linus Torvalds committed
6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486
		break;
	default:
		break;
	}
	return NOTIFY_DONE;
}

static struct notifier_block rtnetlink_dev_notifier = {
	.notifier_call	= rtnetlink_event,
};

6487

6488
static int __net_init rtnetlink_net_init(struct net *net)
6489 6490
{
	struct sock *sk;
6491 6492 6493 6494
	struct netlink_kernel_cfg cfg = {
		.groups		= RTNLGRP_MAX,
		.input		= rtnetlink_rcv,
		.cb_mutex	= &rtnl_mutex,
6495
		.flags		= NL_CFG_F_NONROOT_RECV,
6496
		.bind		= rtnetlink_bind,
6497 6498
	};

6499
	sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
6500 6501 6502 6503 6504 6505
	if (!sk)
		return -ENOMEM;
	net->rtnl = sk;
	return 0;
}

6506
static void __net_exit rtnetlink_net_exit(struct net *net)
6507
{
6508 6509
	netlink_kernel_release(net->rtnl);
	net->rtnl = NULL;
6510 6511 6512 6513 6514 6515 6516
}

static struct pernet_operations rtnetlink_net_ops = {
	.init = rtnetlink_net_init,
	.exit = rtnetlink_net_exit,
};

Linus Torvalds's avatar
Linus Torvalds committed
6517 6518
void __init rtnetlink_init(void)
{
6519
	if (register_pernet_subsys(&rtnetlink_net_ops))
Linus Torvalds's avatar
Linus Torvalds committed
6520
		panic("rtnetlink_init: cannot initialize rtnetlink\n");
6521

Linus Torvalds's avatar
Linus Torvalds committed
6522
	register_netdevice_notifier(&rtnetlink_dev_notifier);
6523

6524
	rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
6525 6526 6527 6528
		      rtnl_dump_ifinfo, 0);
	rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
	rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
	rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
6529

6530 6531 6532
	rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
	rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
	rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
6533

6534 6535 6536
	rtnl_register(PF_UNSPEC, RTM_NEWLINKPROP, rtnl_newlinkprop, NULL, 0);
	rtnl_register(PF_UNSPEC, RTM_DELLINKPROP, rtnl_dellinkprop, NULL, 0);

6537
	rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
6538 6539
	rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL,
		      RTNL_FLAG_BULK_DEL_SUPPORTED);
6540
	rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0);
6541

6542 6543 6544
	rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
	rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
	rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
6545 6546

	rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
6547
		      0);
6548
	rtnl_register(PF_UNSPEC, RTM_SETSTATS, rtnl_stats_set, NULL, 0);
6549 6550 6551 6552

	rtnl_register(PF_BRIDGE, RTM_GETMDB, NULL, rtnl_mdb_dump, 0);
	rtnl_register(PF_BRIDGE, RTM_NEWMDB, rtnl_mdb_add, NULL, 0);
	rtnl_register(PF_BRIDGE, RTM_DELMDB, rtnl_mdb_del, NULL, 0);
Linus Torvalds's avatar
Linus Torvalds committed
6553
}