ip6mr.c 57.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 *	Linux IPv6 multicast routing support for BSD pim6sd
 *	Based on net/ipv4/ipmr.c.
 *
 *	(c) 2004 Mickael Hoerdt, <hoerdt@clarinet.u-strasbg.fr>
 *		LSIIT Laboratory, Strasbourg, France
 *	(c) 2004 Jean-Philippe Andriot, <jean-philippe.andriot@6WIND.com>
 *		6WIND, Paris, France
 *	Copyright (C)2007,2008 USAGI/WIDE Project
 *		YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
 *
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License
 *	as published by the Free Software Foundation; either version
 *	2 of the License, or (at your option) any later version.
 *
 */

19
#include <linux/uaccess.h>
20 21 22 23 24 25 26 27 28 29 30 31 32 33
#include <linux/types.h>
#include <linux/sched.h>
#include <linux/errno.h>
#include <linux/mm.h>
#include <linux/kernel.h>
#include <linux/fcntl.h>
#include <linux/stat.h>
#include <linux/socket.h>
#include <linux/inet.h>
#include <linux/netdevice.h>
#include <linux/inetdevice.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/init.h>
34
#include <linux/compat.h>
35 36 37 38 39 40 41
#include <net/protocol.h>
#include <linux/skbuff.h>
#include <net/raw.h>
#include <linux/notifier.h>
#include <linux/if_arp.h>
#include <net/checksum.h>
#include <net/netlink.h>
42
#include <net/fib_rules.h>
43 44 45 46

#include <net/ipv6.h>
#include <net/ip6_route.h>
#include <linux/mroute6.h>
47
#include <linux/pim.h>
48 49
#include <net/addrconf.h>
#include <linux/netfilter_ipv6.h>
50
#include <linux/export.h>
D
Dave Jones 已提交
51
#include <net/ip6_checksum.h>
52
#include <linux/netconf.h>
53

54 55 56 57 58
struct ip6mr_rule {
	struct fib_rule		common;
};

struct ip6mr_result {
59
	struct mr_table	*mrt;
60 61
};

62 63 64 65 66 67
/* Big lock, protecting vif table, mrt cache and mroute socket state.
   Note that the changes are semaphored via rtnl_lock.
 */

static DEFINE_RWLOCK(mrt_lock);

68
/* Multicast router control variables */
69 70 71 72 73 74 75 76 77 78 79 80 81 82

/* Special spinlock for queue of unresolved entries */
static DEFINE_SPINLOCK(mfc_unres_lock);

/* We return to original Alan's scheme. Hash table of resolved
   entries is changed only in process context and protected
   with weak lock mrt_lock. Queue of unresolved entries is protected
   with strong spinlock mfc_unres_lock.

   In this case data path is free of exclusive locks at all.
 */

static struct kmem_cache *mrt_cachep __read_mostly;

83 84
static struct mr_table *ip6mr_new_table(struct net *net, u32 id);
static void ip6mr_free_table(struct mr_table *mrt);
85

86
static void ip6_mr_forward(struct net *net, struct mr_table *mrt,
87
			   struct sk_buff *skb, struct mfc6_cache *cache);
88
static int ip6mr_cache_report(struct mr_table *mrt, struct sk_buff *pkt,
89
			      mifi_t mifi, int assert);
90
static void mr6_netlink_event(struct mr_table *mrt, struct mfc6_cache *mfc,
91
			      int cmd);
92
static void mrt6msg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt);
93 94
static int ip6mr_rtm_dumproute(struct sk_buff *skb,
			       struct netlink_callback *cb);
95
static void mroute_clean_tables(struct mr_table *mrt, bool all);
96
static void ipmr_expire_process(struct timer_list *t);
97 98

#ifdef CONFIG_IPV6_MROUTE_MULTIPLE_TABLES
99
#define ip6mr_for_each_table(mrt, net) \
100 101
	list_for_each_entry_rcu(mrt, &net->ipv6.mr6_tables, list)

Y
Yuval Mintz 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
static struct mr_table *ip6mr_mr_table_iter(struct net *net,
					    struct mr_table *mrt)
{
	struct mr_table *ret;

	if (!mrt)
		ret = list_entry_rcu(net->ipv6.mr6_tables.next,
				     struct mr_table, list);
	else
		ret = list_entry_rcu(mrt->list.next,
				     struct mr_table, list);

	if (&ret->list == &net->ipv6.mr6_tables)
		return NULL;
	return ret;
}

119
static struct mr_table *ip6mr_get_table(struct net *net, u32 id)
120
{
121
	struct mr_table *mrt;
122 123 124 125 126 127 128 129

	ip6mr_for_each_table(mrt, net) {
		if (mrt->id == id)
			return mrt;
	}
	return NULL;
}

130
static int ip6mr_fib_lookup(struct net *net, struct flowi6 *flp6,
131
			    struct mr_table **mrt)
132 133
{
	int err;
134 135 136 137 138
	struct ip6mr_result res;
	struct fib_lookup_arg arg = {
		.result = &res,
		.flags = FIB_LOOKUP_NOREF,
	};
139

140 141
	err = fib_rules_lookup(net->ipv6.mr6_rules_ops,
			       flowi6_to_flowi(flp6), 0, &arg);
142 143 144 145 146 147 148 149 150 151
	if (err < 0)
		return err;
	*mrt = res.mrt;
	return 0;
}

static int ip6mr_rule_action(struct fib_rule *rule, struct flowi *flp,
			     int flags, struct fib_lookup_arg *arg)
{
	struct ip6mr_result *res = arg->result;
152
	struct mr_table *mrt;
153 154 155 156 157 158 159 160 161 162 163 164 165 166

	switch (rule->action) {
	case FR_ACT_TO_TBL:
		break;
	case FR_ACT_UNREACHABLE:
		return -ENETUNREACH;
	case FR_ACT_PROHIBIT:
		return -EACCES;
	case FR_ACT_BLACKHOLE:
	default:
		return -EINVAL;
	}

	mrt = ip6mr_get_table(rule->fr_net, rule->table);
167
	if (!mrt)
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
		return -EAGAIN;
	res->mrt = mrt;
	return 0;
}

static int ip6mr_rule_match(struct fib_rule *rule, struct flowi *flp, int flags)
{
	return 1;
}

static const struct nla_policy ip6mr_rule_policy[FRA_MAX + 1] = {
	FRA_GENERIC_POLICY,
};

static int ip6mr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
				struct fib_rule_hdr *frh, struct nlattr **tb)
{
	return 0;
}

static int ip6mr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
			      struct nlattr **tb)
{
	return 1;
}

static int ip6mr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
			   struct fib_rule_hdr *frh)
{
	frh->dst_len = 0;
	frh->src_len = 0;
	frh->tos     = 0;
	return 0;
}

203
static const struct fib_rules_ops __net_initconst ip6mr_rules_ops_template = {
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
	.family		= RTNL_FAMILY_IP6MR,
	.rule_size	= sizeof(struct ip6mr_rule),
	.addr_size	= sizeof(struct in6_addr),
	.action		= ip6mr_rule_action,
	.match		= ip6mr_rule_match,
	.configure	= ip6mr_rule_configure,
	.compare	= ip6mr_rule_compare,
	.fill		= ip6mr_rule_fill,
	.nlgroup	= RTNLGRP_IPV6_RULE,
	.policy		= ip6mr_rule_policy,
	.owner		= THIS_MODULE,
};

static int __net_init ip6mr_rules_init(struct net *net)
{
	struct fib_rules_ops *ops;
220
	struct mr_table *mrt;
221 222 223 224 225 226 227 228 229
	int err;

	ops = fib_rules_register(&ip6mr_rules_ops_template, net);
	if (IS_ERR(ops))
		return PTR_ERR(ops);

	INIT_LIST_HEAD(&net->ipv6.mr6_tables);

	mrt = ip6mr_new_table(net, RT6_TABLE_DFLT);
230
	if (!mrt) {
231 232 233 234 235 236 237 238 239 240 241 242
		err = -ENOMEM;
		goto err1;
	}

	err = fib_default_rule_add(ops, 0x7fff, RT6_TABLE_DFLT, 0);
	if (err < 0)
		goto err2;

	net->ipv6.mr6_rules_ops = ops;
	return 0;

err2:
243
	ip6mr_free_table(mrt);
244 245 246 247 248 249 250
err1:
	fib_rules_unregister(ops);
	return err;
}

static void __net_exit ip6mr_rules_exit(struct net *net)
{
251
	struct mr_table *mrt, *next;
252

253
	rtnl_lock();
E
Eric Dumazet 已提交
254 255
	list_for_each_entry_safe(mrt, next, &net->ipv6.mr6_tables, list) {
		list_del(&mrt->list);
256
		ip6mr_free_table(mrt);
E
Eric Dumazet 已提交
257
	}
258
	fib_rules_unregister(net->ipv6.mr6_rules_ops);
259
	rtnl_unlock();
260
}
Y
Yuval Mintz 已提交
261 262 263 264 265 266 267 268 269 270

static int ip6mr_rules_dump(struct net *net, struct notifier_block *nb)
{
	return fib_rules_dump(net, nb, RTNL_FAMILY_IP6MR);
}

static unsigned int ip6mr_rules_seq_read(struct net *net)
{
	return fib_rules_seq_read(net, RTNL_FAMILY_IP6MR);
}
271 272 273 274 275 276 277

bool ip6mr_rule_default(const struct fib_rule *rule)
{
	return fib_rule_matchall(rule) && rule->action == FR_ACT_TO_TBL &&
	       rule->table == RT6_TABLE_DFLT && !rule->l3mdev;
}
EXPORT_SYMBOL(ip6mr_rule_default);
278 279 280 281
#else
#define ip6mr_for_each_table(mrt, net) \
	for (mrt = net->ipv6.mrt6; mrt; mrt = NULL)

Y
Yuval Mintz 已提交
282 283 284 285 286 287 288 289
static struct mr_table *ip6mr_mr_table_iter(struct net *net,
					    struct mr_table *mrt)
{
	if (!mrt)
		return net->ipv6.mrt6;
	return NULL;
}

290
static struct mr_table *ip6mr_get_table(struct net *net, u32 id)
291 292 293 294
{
	return net->ipv6.mrt6;
}

295
static int ip6mr_fib_lookup(struct net *net, struct flowi6 *flp6,
296
			    struct mr_table **mrt)
297 298 299 300 301 302 303 304 305 306 307 308 309
{
	*mrt = net->ipv6.mrt6;
	return 0;
}

static int __net_init ip6mr_rules_init(struct net *net)
{
	net->ipv6.mrt6 = ip6mr_new_table(net, RT6_TABLE_DFLT);
	return net->ipv6.mrt6 ? 0 : -ENOMEM;
}

static void __net_exit ip6mr_rules_exit(struct net *net)
{
310
	rtnl_lock();
311
	ip6mr_free_table(net->ipv6.mrt6);
312 313
	net->ipv6.mrt6 = NULL;
	rtnl_unlock();
314
}
Y
Yuval Mintz 已提交
315 316 317 318 319 320 321 322 323 324

static int ip6mr_rules_dump(struct net *net, struct notifier_block *nb)
{
	return 0;
}

static unsigned int ip6mr_rules_seq_read(struct net *net)
{
	return 0;
}
325 326
#endif

327 328 329 330 331 332 333 334 335 336 337
static int ip6mr_hash_cmp(struct rhashtable_compare_arg *arg,
			  const void *ptr)
{
	const struct mfc6_cache_cmp_arg *cmparg = arg->key;
	struct mfc6_cache *c = (struct mfc6_cache *)ptr;

	return !ipv6_addr_equal(&c->mf6c_mcastgrp, &cmparg->mf6c_mcastgrp) ||
	       !ipv6_addr_equal(&c->mf6c_origin, &cmparg->mf6c_origin);
}

static const struct rhashtable_params ip6mr_rht_params = {
338
	.head_offset = offsetof(struct mr_mfc, mnode),
339 340 341 342 343 344 345 346
	.key_offset = offsetof(struct mfc6_cache, cmparg),
	.key_len = sizeof(struct mfc6_cache_cmp_arg),
	.nelem_hint = 3,
	.locks_mul = 1,
	.obj_cmpfn = ip6mr_hash_cmp,
	.automatic_shrinking = true,
};

347 348 349 350 351 352 353 354
static void ip6mr_new_table_set(struct mr_table *mrt,
				struct net *net)
{
#ifdef CONFIG_IPV6_MROUTE_MULTIPLE_TABLES
	list_add_tail_rcu(&mrt->list, &net->ipv6.mr6_tables);
#endif
}

355 356 357 358 359 360 361 362 363 364
static struct mfc6_cache_cmp_arg ip6mr_mr_table_ops_cmparg_any = {
	.mf6c_origin = IN6ADDR_ANY_INIT,
	.mf6c_mcastgrp = IN6ADDR_ANY_INIT,
};

static struct mr_table_ops ip6mr_mr_table_ops = {
	.rht_params = &ip6mr_rht_params,
	.cmparg_any = &ip6mr_mr_table_ops_cmparg_any,
};

365
static struct mr_table *ip6mr_new_table(struct net *net, u32 id)
366
{
367
	struct mr_table *mrt;
368 369

	mrt = ip6mr_get_table(net, id);
370
	if (mrt)
371 372
		return mrt;

373
	return mr_table_alloc(net, id, &ip6mr_mr_table_ops,
374
			      ipmr_expire_process, ip6mr_new_table_set);
375
}
376

377
static void ip6mr_free_table(struct mr_table *mrt)
378
{
379
	del_timer_sync(&mrt->ipmr_expire_timer);
380
	mroute_clean_tables(mrt, true);
381
	rhltable_destroy(&mrt->mfc_hash);
382 383
	kfree(mrt);
}
384 385

#ifdef CONFIG_PROC_FS
Y
Yuval Mintz 已提交
386 387
/* The /proc interfaces to multicast routing
 * /proc/ip6_mr_cache /proc/ip6_mr_vif
388 389 390 391 392
 */

static void *ip6mr_vif_seq_start(struct seq_file *seq, loff_t *pos)
	__acquires(mrt_lock)
{
393
	struct mr_vif_iter *iter = seq->private;
394
	struct net *net = seq_file_net(seq);
395
	struct mr_table *mrt;
396 397

	mrt = ip6mr_get_table(net, RT6_TABLE_DFLT);
398
	if (!mrt)
399 400 401
		return ERR_PTR(-ENOENT);

	iter->mrt = mrt;
402

403
	read_lock(&mrt_lock);
404
	return mr_vif_seq_start(seq, pos);
405 406 407 408 409 410 411 412 413 414
}

static void ip6mr_vif_seq_stop(struct seq_file *seq, void *v)
	__releases(mrt_lock)
{
	read_unlock(&mrt_lock);
}

static int ip6mr_vif_seq_show(struct seq_file *seq, void *v)
{
415
	struct mr_vif_iter *iter = seq->private;
416
	struct mr_table *mrt = iter->mrt;
417

418 419 420 421
	if (v == SEQ_START_TOKEN) {
		seq_puts(seq,
			 "Interface      BytesIn  PktsIn  BytesOut PktsOut Flags\n");
	} else {
422
		const struct vif_device *vif = v;
423 424 425
		const char *name = vif->dev ? vif->dev->name : "none";

		seq_printf(seq,
A
Al Viro 已提交
426
			   "%2td %-10s %8ld %7ld  %8ld %7ld %05X\n",
427
			   vif - mrt->vif_table,
428 429 430 431 432 433 434
			   name, vif->bytes_in, vif->pkt_in,
			   vif->bytes_out, vif->pkt_out,
			   vif->flags);
	}
	return 0;
}

435
static const struct seq_operations ip6mr_vif_seq_ops = {
436
	.start = ip6mr_vif_seq_start,
437
	.next  = mr_vif_seq_next,
438 439 440 441 442 443
	.stop  = ip6mr_vif_seq_stop,
	.show  = ip6mr_vif_seq_show,
};

static int ip6mr_vif_open(struct inode *inode, struct file *file)
{
444
	return seq_open_net(inode, file, &ip6mr_vif_seq_ops,
445
			    sizeof(struct mr_vif_iter));
446 447
}

448
static const struct file_operations ip6mr_vif_fops = {
449 450 451
	.open    = ip6mr_vif_open,
	.read    = seq_read,
	.llseek  = seq_lseek,
452
	.release = seq_release_net,
453 454 455 456
};

static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
{
457
	struct net *net = seq_file_net(seq);
458
	struct mr_table *mrt;
459

460
	mrt = ip6mr_get_table(net, RT6_TABLE_DFLT);
461
	if (!mrt)
462 463
		return ERR_PTR(-ENOENT);

Y
Yuval Mintz 已提交
464
	return mr_mfc_seq_start(seq, pos, mrt, &mfc_unres_lock);
465 466 467 468 469 470 471 472 473 474 475 476 477
}

static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
{
	int n;

	if (v == SEQ_START_TOKEN) {
		seq_puts(seq,
			 "Group                            "
			 "Origin                           "
			 "Iif      Pkts  Bytes     Wrong  Oifs\n");
	} else {
		const struct mfc6_cache *mfc = v;
Y
Yuval Mintz 已提交
478
		const struct mr_mfc_iter *it = seq->private;
479
		struct mr_table *mrt = it->mrt;
480

481
		seq_printf(seq, "%pI6 %pI6 %-3hd",
482
			   &mfc->mf6c_mcastgrp, &mfc->mf6c_origin,
483
			   mfc->_c.mfc_parent);
484

485
		if (it->cache != &mrt->mfc_unres_queue) {
486
			seq_printf(seq, " %8lu %8lu %8lu",
487 488 489 490 491
				   mfc->_c.mfc_un.res.pkt,
				   mfc->_c.mfc_un.res.bytes,
				   mfc->_c.mfc_un.res.wrong_if);
			for (n = mfc->_c.mfc_un.res.minvif;
			     n < mfc->_c.mfc_un.res.maxvif; n++) {
492
				if (VIF_EXISTS(mrt, n) &&
493
				    mfc->_c.mfc_un.res.ttls[n] < 255)
494
					seq_printf(seq,
495 496
						   " %2d:%-3d", n,
						   mfc->_c.mfc_un.res.ttls[n]);
497
			}
498 499 500 501 502
		} else {
			/* unresolved mfc_caches don't contain
			 * pkt, bytes and wrong_if values
			 */
			seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
503 504 505 506 507 508
		}
		seq_putc(seq, '\n');
	}
	return 0;
}

J
James Morris 已提交
509
static const struct seq_operations ipmr_mfc_seq_ops = {
510
	.start = ipmr_mfc_seq_start,
Y
Yuval Mintz 已提交
511 512
	.next  = mr_mfc_seq_next,
	.stop  = mr_mfc_seq_stop,
513 514 515 516 517
	.show  = ipmr_mfc_seq_show,
};

static int ipmr_mfc_open(struct inode *inode, struct file *file)
{
518
	return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
Y
Yuval Mintz 已提交
519
			    sizeof(struct mr_mfc_iter));
520 521
}

522
static const struct file_operations ip6mr_mfc_fops = {
523 524 525
	.open    = ipmr_mfc_open,
	.read    = seq_read,
	.llseek  = seq_lseek,
526
	.release = seq_release_net,
527 528 529
};
#endif

530 531 532 533 534 535 536
#ifdef CONFIG_IPV6_PIMSM_V2

static int pim6_rcv(struct sk_buff *skb)
{
	struct pimreghdr *pim;
	struct ipv6hdr   *encap;
	struct net_device  *reg_dev = NULL;
537
	struct net *net = dev_net(skb->dev);
538
	struct mr_table *mrt;
539 540 541
	struct flowi6 fl6 = {
		.flowi6_iif	= skb->dev->ifindex,
		.flowi6_mark	= skb->mark,
542 543
	};
	int reg_vif_num;
544 545 546 547 548

	if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(*encap)))
		goto drop;

	pim = (struct pimreghdr *)skb_transport_header(skb);
549
	if (pim->type != ((PIM_VERSION << 4) | PIM_TYPE_REGISTER) ||
550
	    (pim->flags & PIM_NULL_REGISTER) ||
T
Thomas Goff 已提交
551 552 553
	    (csum_ipv6_magic(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
			     sizeof(*pim), IPPROTO_PIM,
			     csum_partial((void *)pim, sizeof(*pim), 0)) &&
554
	     csum_fold(skb_checksum(skb, 0, skb->len, 0))))
555 556 557 558 559 560 561 562 563 564 565
		goto drop;

	/* check if the inner packet is destined to mcast group */
	encap = (struct ipv6hdr *)(skb_transport_header(skb) +
				   sizeof(*pim));

	if (!ipv6_addr_is_multicast(&encap->daddr) ||
	    encap->payload_len == 0 ||
	    ntohs(encap->payload_len) + sizeof(*pim) > skb->len)
		goto drop;

566
	if (ip6mr_fib_lookup(net, &fl6, &mrt) < 0)
567 568 569
		goto drop;
	reg_vif_num = mrt->mroute_reg_vif_num;

570 571
	read_lock(&mrt_lock);
	if (reg_vif_num >= 0)
572
		reg_dev = mrt->vif_table[reg_vif_num].dev;
573 574 575 576
	if (reg_dev)
		dev_hold(reg_dev);
	read_unlock(&mrt_lock);

577
	if (!reg_dev)
578 579 580 581 582
		goto drop;

	skb->mac_header = skb->network_header;
	skb_pull(skb, (u8 *)encap - skb->data);
	skb_reset_network_header(skb);
T
Thomas Goff 已提交
583
	skb->protocol = htons(ETH_P_IPV6);
584
	skb->ip_summed = CHECKSUM_NONE;
585

586
	skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
587

588
	netif_rx(skb);
E
Eric Dumazet 已提交
589

590 591 592 593 594 595 596
	dev_put(reg_dev);
	return 0;
 drop:
	kfree_skb(skb);
	return 0;
}

597
static const struct inet6_protocol pim6_protocol = {
598 599 600 601 602
	.handler	=	pim6_rcv,
};

/* Service routines creating virtual interfaces: PIMREG */

603 604
static netdev_tx_t reg_vif_xmit(struct sk_buff *skb,
				      struct net_device *dev)
605
{
606
	struct net *net = dev_net(dev);
607
	struct mr_table *mrt;
608 609
	struct flowi6 fl6 = {
		.flowi6_oif	= dev->ifindex,
610
		.flowi6_iif	= skb->skb_iif ? : LOOPBACK_IFINDEX,
611
		.flowi6_mark	= skb->mark,
612 613 614
	};
	int err;

615
	err = ip6mr_fib_lookup(net, &fl6, &mrt);
616 617
	if (err < 0) {
		kfree_skb(skb);
618
		return err;
619
	}
620

621
	read_lock(&mrt_lock);
622 623
	dev->stats.tx_bytes += skb->len;
	dev->stats.tx_packets++;
624
	ip6mr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, MRT6MSG_WHOLEPKT);
625 626
	read_unlock(&mrt_lock);
	kfree_skb(skb);
627
	return NETDEV_TX_OK;
628 629
}

630 631 632 633 634
static int reg_vif_get_iflink(const struct net_device *dev)
{
	return 0;
}

635 636
static const struct net_device_ops reg_vif_netdev_ops = {
	.ndo_start_xmit	= reg_vif_xmit,
637
	.ndo_get_iflink = reg_vif_get_iflink,
638 639
};

640 641 642 643 644
static void reg_vif_setup(struct net_device *dev)
{
	dev->type		= ARPHRD_PIMREG;
	dev->mtu		= 1500 - sizeof(struct ipv6hdr) - 8;
	dev->flags		= IFF_NOARP;
645
	dev->netdev_ops		= &reg_vif_netdev_ops;
646
	dev->needs_free_netdev	= true;
T
Tom Goff 已提交
647
	dev->features		|= NETIF_F_NETNS_LOCAL;
648 649
}

650
static struct net_device *ip6mr_reg_vif(struct net *net, struct mr_table *mrt)
651 652
{
	struct net_device *dev;
653 654 655 656 657 658
	char name[IFNAMSIZ];

	if (mrt->id == RT6_TABLE_DFLT)
		sprintf(name, "pim6reg");
	else
		sprintf(name, "pim6reg%u", mrt->id);
659

660
	dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
661
	if (!dev)
662 663
		return NULL;

664 665
	dev_net_set(dev, net);

666 667 668 669 670 671 672 673
	if (register_netdevice(dev)) {
		free_netdev(dev);
		return NULL;
	}

	if (dev_open(dev))
		goto failure;

W
Wang Chen 已提交
674
	dev_hold(dev);
675 676 677 678 679 680 681 682
	return dev;

failure:
	unregister_netdevice(dev);
	return NULL;
}
#endif

Y
Yuval Mintz 已提交
683 684 685 686 687 688 689 690 691
static int call_ip6mr_vif_entry_notifiers(struct net *net,
					  enum fib_event_type event_type,
					  struct vif_device *vif,
					  mifi_t vif_index, u32 tb_id)
{
	return mr_call_vif_notifiers(net, RTNL_FAMILY_IP6MR, event_type,
				     vif, vif_index, tb_id,
				     &net->ipv6.ipmr_seq);
}
692

Y
Yuval Mintz 已提交
693 694 695 696 697 698 699 700 701
static int call_ip6mr_mfc_entry_notifiers(struct net *net,
					  enum fib_event_type event_type,
					  struct mfc6_cache *mfc, u32 tb_id)
{
	return mr_call_mfc_notifiers(net, RTNL_FAMILY_IP6MR, event_type,
				     &mfc->_c, tb_id, &net->ipv6.ipmr_seq);
}

/* Delete a VIF entry */
702
static int mif6_delete(struct mr_table *mrt, int vifi, int notify,
703
		       struct list_head *head)
704
{
705
	struct vif_device *v;
706
	struct net_device *dev;
T
Thomas Goff 已提交
707
	struct inet6_dev *in6_dev;
708 709

	if (vifi < 0 || vifi >= mrt->maxvif)
710 711
		return -EADDRNOTAVAIL;

712
	v = &mrt->vif_table[vifi];
713

Y
Yuval Mintz 已提交
714 715 716 717 718
	if (VIF_EXISTS(mrt, vifi))
		call_ip6mr_vif_entry_notifiers(read_pnet(&mrt->net),
					       FIB_EVENT_VIF_DEL, v, vifi,
					       mrt->id);

719 720 721 722 723 724 725 726 727
	write_lock_bh(&mrt_lock);
	dev = v->dev;
	v->dev = NULL;

	if (!dev) {
		write_unlock_bh(&mrt_lock);
		return -EADDRNOTAVAIL;
	}

728
#ifdef CONFIG_IPV6_PIMSM_V2
729 730
	if (vifi == mrt->mroute_reg_vif_num)
		mrt->mroute_reg_vif_num = -1;
731 732
#endif

733
	if (vifi + 1 == mrt->maxvif) {
734 735
		int tmp;
		for (tmp = vifi - 1; tmp >= 0; tmp--) {
736
			if (VIF_EXISTS(mrt, tmp))
737 738
				break;
		}
739
		mrt->maxvif = tmp + 1;
740 741 742 743 744 745
	}

	write_unlock_bh(&mrt_lock);

	dev_set_allmulti(dev, -1);

T
Thomas Goff 已提交
746
	in6_dev = __in6_dev_get(dev);
747
	if (in6_dev) {
T
Thomas Goff 已提交
748
		in6_dev->cnf.mc_forwarding--;
749
		inet6_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
750 751 752
					     NETCONFA_MC_FORWARDING,
					     dev->ifindex, &in6_dev->cnf);
	}
T
Thomas Goff 已提交
753

754
	if ((v->flags & MIFF_REGISTER) && !notify)
755
		unregister_netdevice_queue(dev, head);
756 757 758 759 760

	dev_put(dev);
	return 0;
}

761
static inline void ip6mr_cache_free_rcu(struct rcu_head *head)
762
{
763
	struct mr_mfc *c = container_of(head, struct mr_mfc, rcu);
764

765
	kmem_cache_free(mrt_cachep, (struct mfc6_cache *)c);
766 767
}

768 769
static inline void ip6mr_cache_free(struct mfc6_cache *c)
{
770
	call_rcu(&c->_c.rcu, ip6mr_cache_free_rcu);
771 772
}

773 774 775 776
/* Destroy an unresolved cache entry, killing queued skbs
   and reporting error to netlink readers.
 */

777
static void ip6mr_destroy_unres(struct mr_table *mrt, struct mfc6_cache *c)
778
{
779
	struct net *net = read_pnet(&mrt->net);
780 781
	struct sk_buff *skb;

782
	atomic_dec(&mrt->cache_resolve_queue_len);
783

784
	while ((skb = skb_dequeue(&c->_c.mfc_un.unres.unresolved)) != NULL) {
785
		if (ipv6_hdr(skb)->version == 0) {
786 787
			struct nlmsghdr *nlh = skb_pull(skb,
							sizeof(struct ipv6hdr));
788
			nlh->nlmsg_type = NLMSG_ERROR;
789
			nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
790
			skb_trim(skb, nlh->nlmsg_len);
791
			((struct nlmsgerr *)nlmsg_data(nlh))->error = -ETIMEDOUT;
792
			rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
793 794 795 796
		} else
			kfree_skb(skb);
	}

797
	ip6mr_cache_free(c);
798 799 800
}


801
/* Timer process for all the unresolved queue. */
802

803
static void ipmr_do_expire_process(struct mr_table *mrt)
804 805 806
{
	unsigned long now = jiffies;
	unsigned long expires = 10 * HZ;
807
	struct mr_mfc *c, *next;
808

809
	list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
810 811 812 813 814 815 816 817
		if (time_after(c->mfc_un.unres.expires, now)) {
			/* not yet... */
			unsigned long interval = c->mfc_un.unres.expires - now;
			if (interval < expires)
				expires = interval;
			continue;
		}

818
		list_del(&c->list);
819 820
		mr6_netlink_event(mrt, (struct mfc6_cache *)c, RTM_DELROUTE);
		ip6mr_destroy_unres(mrt, (struct mfc6_cache *)c);
821 822
	}

823
	if (!list_empty(&mrt->mfc_unres_queue))
824
		mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
825 826
}

827
static void ipmr_expire_process(struct timer_list *t)
828
{
829
	struct mr_table *mrt = from_timer(mrt, t, ipmr_expire_timer);
830

831
	if (!spin_trylock(&mfc_unres_lock)) {
832
		mod_timer(&mrt->ipmr_expire_timer, jiffies + 1);
833 834 835
		return;
	}

836
	if (!list_empty(&mrt->mfc_unres_queue))
837
		ipmr_do_expire_process(mrt);
838 839 840 841 842 843

	spin_unlock(&mfc_unres_lock);
}

/* Fill oifs list. It is called under write locked mrt_lock. */

844
static void ip6mr_update_thresholds(struct mr_table *mrt,
845
				    struct mr_mfc *cache,
846
				    unsigned char *ttls)
847 848 849
{
	int vifi;

850
	cache->mfc_un.res.minvif = MAXMIFS;
851
	cache->mfc_un.res.maxvif = 0;
852
	memset(cache->mfc_un.res.ttls, 255, MAXMIFS);
853

854
	for (vifi = 0; vifi < mrt->maxvif; vifi++) {
855
		if (VIF_EXISTS(mrt, vifi) &&
856
		    ttls[vifi] && ttls[vifi] < 255) {
857 858 859 860 861 862 863
			cache->mfc_un.res.ttls[vifi] = ttls[vifi];
			if (cache->mfc_un.res.minvif > vifi)
				cache->mfc_un.res.minvif = vifi;
			if (cache->mfc_un.res.maxvif <= vifi)
				cache->mfc_un.res.maxvif = vifi + 1;
		}
	}
864
	cache->mfc_un.res.lastuse = jiffies;
865 866
}

867
static int mif6_add(struct net *net, struct mr_table *mrt,
868
		    struct mif6ctl *vifc, int mrtsock)
869 870
{
	int vifi = vifc->mif6c_mifi;
871
	struct vif_device *v = &mrt->vif_table[vifi];
872
	struct net_device *dev;
T
Thomas Goff 已提交
873
	struct inet6_dev *in6_dev;
874
	int err;
875 876

	/* Is vif busy ? */
877
	if (VIF_EXISTS(mrt, vifi))
878 879 880
		return -EADDRINUSE;

	switch (vifc->mif6c_flags) {
881 882 883 884 885 886
#ifdef CONFIG_IPV6_PIMSM_V2
	case MIFF_REGISTER:
		/*
		 * Special Purpose VIF in PIM
		 * All the packets will be sent to the daemon
		 */
887
		if (mrt->mroute_reg_vif_num >= 0)
888
			return -EADDRINUSE;
889
		dev = ip6mr_reg_vif(net, mrt);
890 891
		if (!dev)
			return -ENOBUFS;
892 893 894
		err = dev_set_allmulti(dev, 1);
		if (err) {
			unregister_netdevice(dev);
W
Wang Chen 已提交
895
			dev_put(dev);
896 897
			return err;
		}
898 899
		break;
#endif
900
	case 0:
901
		dev = dev_get_by_index(net, vifc->mif6c_pifi);
902 903
		if (!dev)
			return -EADDRNOTAVAIL;
904
		err = dev_set_allmulti(dev, 1);
W
Wang Chen 已提交
905 906
		if (err) {
			dev_put(dev);
907
			return err;
W
Wang Chen 已提交
908
		}
909 910 911 912 913
		break;
	default:
		return -EINVAL;
	}

T
Thomas Goff 已提交
914
	in6_dev = __in6_dev_get(dev);
915
	if (in6_dev) {
T
Thomas Goff 已提交
916
		in6_dev->cnf.mc_forwarding++;
917
		inet6_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
918 919 920
					     NETCONFA_MC_FORWARDING,
					     dev->ifindex, &in6_dev->cnf);
	}
T
Thomas Goff 已提交
921

922 923 924 925
	/* Fill in the VIF structures */
	vif_device_init(v, dev, vifc->vifc_rate_limit, vifc->vifc_threshold,
			vifc->mif6c_flags | (!mrtsock ? VIFF_STATIC : 0),
			MIFF_REGISTER);
926 927 928 929

	/* And finish update writing critical data */
	write_lock_bh(&mrt_lock);
	v->dev = dev;
930 931
#ifdef CONFIG_IPV6_PIMSM_V2
	if (v->flags & MIFF_REGISTER)
932
		mrt->mroute_reg_vif_num = vifi;
933
#endif
934 935
	if (vifi + 1 > mrt->maxvif)
		mrt->maxvif = vifi + 1;
936
	write_unlock_bh(&mrt_lock);
Y
Yuval Mintz 已提交
937 938
	call_ip6mr_vif_entry_notifiers(net, FIB_EVENT_VIF_ADD,
				       v, vifi, mrt->id);
939 940 941
	return 0;
}

942
static struct mfc6_cache *ip6mr_cache_find(struct mr_table *mrt,
943 944
					   const struct in6_addr *origin,
					   const struct in6_addr *mcastgrp)
945
{
946 947 948 949 950
	struct mfc6_cache_cmp_arg arg = {
		.mf6c_origin = *origin,
		.mf6c_mcastgrp = *mcastgrp,
	};

951
	return mr_mfc_find(mrt, &arg);
952 953 954
}

/* Look for a (*,G) entry */
955
static struct mfc6_cache *ip6mr_cache_find_any(struct mr_table *mrt,
956 957 958
					       struct in6_addr *mcastgrp,
					       mifi_t mifi)
{
959 960 961 962
	struct mfc6_cache_cmp_arg arg = {
		.mf6c_origin = in6addr_any,
		.mf6c_mcastgrp = *mcastgrp,
	};
963 964

	if (ipv6_addr_any(mcastgrp))
965 966
		return mr_mfc_find_any_parent(mrt, mifi);
	return mr_mfc_find_any(mrt, mifi, &arg);
967 968
}

969 970
/* Look for a (S,G,iif) entry if parent != -1 */
static struct mfc6_cache *
971
ip6mr_cache_find_parent(struct mr_table *mrt,
972 973 974 975 976 977 978 979 980
			const struct in6_addr *origin,
			const struct in6_addr *mcastgrp,
			int parent)
{
	struct mfc6_cache_cmp_arg arg = {
		.mf6c_origin = *origin,
		.mf6c_mcastgrp = *mcastgrp,
	};

981
	return mr_mfc_find_parent(mrt, &arg, parent);
982 983
}

984
/* Allocate a multicast cache entry */
985
static struct mfc6_cache *ip6mr_cache_alloc(void)
986
{
987
	struct mfc6_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
988
	if (!c)
989
		return NULL;
990 991
	c->_c.mfc_un.res.last_assert = jiffies - MFC_ASSERT_THRESH - 1;
	c->_c.mfc_un.res.minvif = MAXMIFS;
992 993 994
	return c;
}

995
static struct mfc6_cache *ip6mr_cache_alloc_unres(void)
996
{
997
	struct mfc6_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
998
	if (!c)
999
		return NULL;
1000 1001
	skb_queue_head_init(&c->_c.mfc_un.unres.unresolved);
	c->_c.mfc_un.unres.expires = jiffies + 10 * HZ;
1002 1003 1004 1005 1006 1007 1008
	return c;
}

/*
 *	A cache entry has gone into a resolved state from queued
 */

1009
static void ip6mr_cache_resolve(struct net *net, struct mr_table *mrt,
1010
				struct mfc6_cache *uc, struct mfc6_cache *c)
1011 1012 1013 1014 1015 1016 1017
{
	struct sk_buff *skb;

	/*
	 *	Play the pending entries through our router
	 */

1018
	while ((skb = __skb_dequeue(&uc->_c.mfc_un.unres.unresolved))) {
1019
		if (ipv6_hdr(skb)->version == 0) {
1020 1021
			struct nlmsghdr *nlh = skb_pull(skb,
							sizeof(struct ipv6hdr));
1022

Y
Yuval Mintz 已提交
1023 1024
			if (mr_fill_mroute(mrt, skb, &c->_c,
					   nlmsg_data(nlh)) > 0) {
1025
				nlh->nlmsg_len = skb_tail_pointer(skb) - (u8 *)nlh;
1026 1027
			} else {
				nlh->nlmsg_type = NLMSG_ERROR;
1028
				nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
1029
				skb_trim(skb, nlh->nlmsg_len);
1030
				((struct nlmsgerr *)nlmsg_data(nlh))->error = -EMSGSIZE;
1031
			}
1032
			rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
1033
		} else
1034
			ip6_mr_forward(net, mrt, skb, c);
1035 1036 1037 1038
	}
}

/*
1039
 *	Bounce a cache query up to pim6sd and netlink.
1040 1041 1042 1043
 *
 *	Called under mrt_lock.
 */

1044
static int ip6mr_cache_report(struct mr_table *mrt, struct sk_buff *pkt,
1045
			      mifi_t mifi, int assert)
1046
{
Y
Yuval Mintz 已提交
1047
	struct sock *mroute6_sk;
1048 1049 1050 1051
	struct sk_buff *skb;
	struct mrt6msg *msg;
	int ret;

1052 1053 1054 1055 1056 1057 1058
#ifdef CONFIG_IPV6_PIMSM_V2
	if (assert == MRT6MSG_WHOLEPKT)
		skb = skb_realloc_headroom(pkt, -skb_network_offset(pkt)
						+sizeof(*msg));
	else
#endif
		skb = alloc_skb(sizeof(struct ipv6hdr) + sizeof(*msg), GFP_ATOMIC);
1059 1060 1061 1062 1063 1064 1065 1066 1067

	if (!skb)
		return -ENOBUFS;

	/* I suppose that internal messages
	 * do not require checksums */

	skb->ip_summed = CHECKSUM_UNNECESSARY;

1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
#ifdef CONFIG_IPV6_PIMSM_V2
	if (assert == MRT6MSG_WHOLEPKT) {
		/* Ugly, but we have no choice with this interface.
		   Duplicate old header, fix length etc.
		   And all this only to mangle msg->im6_msgtype and
		   to set msg->im6_mbz to "mbz" :-)
		 */
		skb_push(skb, -skb_network_offset(pkt));

		skb_push(skb, sizeof(*msg));
		skb_reset_transport_header(skb);
		msg = (struct mrt6msg *)skb_transport_header(skb);
		msg->im6_mbz = 0;
		msg->im6_msgtype = MRT6MSG_WHOLEPKT;
1082
		msg->im6_mif = mrt->mroute_reg_vif_num;
1083
		msg->im6_pad = 0;
A
Alexey Dobriyan 已提交
1084 1085
		msg->im6_src = ipv6_hdr(pkt)->saddr;
		msg->im6_dst = ipv6_hdr(pkt)->daddr;
1086 1087 1088 1089 1090

		skb->ip_summed = CHECKSUM_UNNECESSARY;
	} else
#endif
	{
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
	/*
	 *	Copy the IP header
	 */

	skb_put(skb, sizeof(struct ipv6hdr));
	skb_reset_network_header(skb);
	skb_copy_to_linear_data(skb, ipv6_hdr(pkt), sizeof(struct ipv6hdr));

	/*
	 *	Add our header
	 */
	skb_put(skb, sizeof(*msg));
	skb_reset_transport_header(skb);
	msg = (struct mrt6msg *)skb_transport_header(skb);

	msg->im6_mbz = 0;
	msg->im6_msgtype = assert;
1108
	msg->im6_mif = mifi;
1109
	msg->im6_pad = 0;
A
Alexey Dobriyan 已提交
1110 1111
	msg->im6_src = ipv6_hdr(pkt)->saddr;
	msg->im6_dst = ipv6_hdr(pkt)->daddr;
1112

E
Eric Dumazet 已提交
1113
	skb_dst_set(skb, dst_clone(skb_dst(pkt)));
1114
	skb->ip_summed = CHECKSUM_UNNECESSARY;
1115
	}
1116

Y
Yuval Mintz 已提交
1117
	rcu_read_lock();
1118
	mroute6_sk = rcu_dereference(mrt->mroute_sk);
Y
Yuval Mintz 已提交
1119 1120
	if (!mroute6_sk) {
		rcu_read_unlock();
1121 1122 1123 1124
		kfree_skb(skb);
		return -EINVAL;
	}

1125 1126
	mrt6msg_netlink_event(mrt, skb);

Y
Yuval Mintz 已提交
1127 1128 1129
	/* Deliver to user space multicast routing algorithms */
	ret = sock_queue_rcv_skb(mroute6_sk, skb);
	rcu_read_unlock();
1130
	if (ret < 0) {
1131
		net_warn_ratelimited("mroute6: pending queue full, dropping entries\n");
1132 1133 1134 1135 1136 1137
		kfree_skb(skb);
	}

	return ret;
}

1138 1139 1140
/* Queue a packet for resolution. It gets locked cache entry! */
static int ip6mr_cache_unresolved(struct mr_table *mrt, mifi_t mifi,
				  struct sk_buff *skb)
1141
{
1142
	struct mfc6_cache *c;
1143
	bool found = false;
1144 1145 1146
	int err;

	spin_lock_bh(&mfc_unres_lock);
1147
	list_for_each_entry(c, &mrt->mfc_unres_queue, _c.list) {
1148
		if (ipv6_addr_equal(&c->mf6c_mcastgrp, &ipv6_hdr(skb)->daddr) &&
1149 1150
		    ipv6_addr_equal(&c->mf6c_origin, &ipv6_hdr(skb)->saddr)) {
			found = true;
1151
			break;
1152
		}
1153 1154
	}

1155
	if (!found) {
1156 1157 1158 1159
		/*
		 *	Create a new entry if allowable
		 */

1160
		if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
1161
		    (c = ip6mr_cache_alloc_unres()) == NULL) {
1162 1163 1164 1165 1166 1167
			spin_unlock_bh(&mfc_unres_lock);

			kfree_skb(skb);
			return -ENOBUFS;
		}

1168 1169
		/* Fill in the new cache entry */
		c->_c.mfc_parent = -1;
1170 1171 1172 1173 1174 1175
		c->mf6c_origin = ipv6_hdr(skb)->saddr;
		c->mf6c_mcastgrp = ipv6_hdr(skb)->daddr;

		/*
		 *	Reflect first query at pim6sd
		 */
1176
		err = ip6mr_cache_report(mrt, skb, mifi, MRT6MSG_NOCACHE);
1177
		if (err < 0) {
1178 1179 1180 1181 1182
			/* If the report failed throw the cache entry
			   out - Brad Parker
			 */
			spin_unlock_bh(&mfc_unres_lock);

1183
			ip6mr_cache_free(c);
1184 1185 1186 1187
			kfree_skb(skb);
			return err;
		}

1188
		atomic_inc(&mrt->cache_resolve_queue_len);
1189
		list_add(&c->_c.list, &mrt->mfc_unres_queue);
1190
		mr6_netlink_event(mrt, c, RTM_NEWROUTE);
1191

1192
		ipmr_do_expire_process(mrt);
1193 1194
	}

1195 1196
	/* See if we can append the packet */
	if (c->_c.mfc_un.unres.unresolved.qlen > 3) {
1197 1198 1199
		kfree_skb(skb);
		err = -ENOBUFS;
	} else {
1200
		skb_queue_tail(&c->_c.mfc_un.unres.unresolved, skb);
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
		err = 0;
	}

	spin_unlock_bh(&mfc_unres_lock);
	return err;
}

/*
 *	MFC6 cache manipulation by user space
 */

1212
static int ip6mr_mfc_delete(struct mr_table *mrt, struct mf6cctl *mfc,
1213
			    int parent)
1214
{
1215
	struct mfc6_cache *c;
1216

1217 1218 1219 1220 1221 1222 1223
	/* The entries are added/deleted only under RTNL */
	rcu_read_lock();
	c = ip6mr_cache_find_parent(mrt, &mfc->mf6cc_origin.sin6_addr,
				    &mfc->mf6cc_mcastgrp.sin6_addr, parent);
	rcu_read_unlock();
	if (!c)
		return -ENOENT;
1224 1225
	rhltable_remove(&mrt->mfc_hash, &c->_c.mnode, ip6mr_rht_params);
	list_del_rcu(&c->_c.list);
1226

Y
Yuval Mintz 已提交
1227 1228
	call_ip6mr_mfc_entry_notifiers(read_pnet(&mrt->net),
				       FIB_EVENT_ENTRY_DEL, c, mrt->id);
1229 1230 1231
	mr6_netlink_event(mrt, c, RTM_DELROUTE);
	ip6mr_cache_free(c);
	return 0;
1232 1233 1234 1235 1236
}

static int ip6mr_device_event(struct notifier_block *this,
			      unsigned long event, void *ptr)
{
1237
	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1238
	struct net *net = dev_net(dev);
1239
	struct mr_table *mrt;
1240
	struct vif_device *v;
1241 1242 1243 1244 1245
	int ct;

	if (event != NETDEV_UNREGISTER)
		return NOTIFY_DONE;

1246
	ip6mr_for_each_table(mrt, net) {
1247
		v = &mrt->vif_table[0];
1248 1249
		for (ct = 0; ct < mrt->maxvif; ct++, v++) {
			if (v->dev == dev)
1250
				mif6_delete(mrt, ct, 1, NULL);
1251
		}
1252
	}
1253

1254 1255 1256
	return NOTIFY_DONE;
}

Y
Yuval Mintz 已提交
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
static unsigned int ip6mr_seq_read(struct net *net)
{
	ASSERT_RTNL();

	return net->ipv6.ipmr_seq + ip6mr_rules_seq_read(net);
}

static int ip6mr_dump(struct net *net, struct notifier_block *nb)
{
	return mr_dump(net, nb, RTNL_FAMILY_IP6MR, ip6mr_rules_dump,
		       ip6mr_mr_table_iter, &mrt_lock);
}

1270 1271 1272 1273
static struct notifier_block ip6_mr_notifier = {
	.notifier_call = ip6mr_device_event
};

Y
Yuval Mintz 已提交
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
static const struct fib_notifier_ops ip6mr_notifier_ops_template = {
	.family		= RTNL_FAMILY_IP6MR,
	.fib_seq_read	= ip6mr_seq_read,
	.fib_dump	= ip6mr_dump,
	.owner		= THIS_MODULE,
};

static int __net_init ip6mr_notifier_init(struct net *net)
{
	struct fib_notifier_ops *ops;

	net->ipv6.ipmr_seq = 0;
1286

Y
Yuval Mintz 已提交
1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
	ops = fib_notifier_ops_register(&ip6mr_notifier_ops_template, net);
	if (IS_ERR(ops))
		return PTR_ERR(ops);

	net->ipv6.ip6mr_notifier_ops = ops;

	return 0;
}

static void __net_exit ip6mr_notifier_exit(struct net *net)
{
	fib_notifier_ops_unregister(net->ipv6.ip6mr_notifier_ops);
	net->ipv6.ip6mr_notifier_ops = NULL;
}

/* Setup for IP multicast routing */
1303 1304
static int __net_init ip6mr_net_init(struct net *net)
{
1305
	int err;
1306

Y
Yuval Mintz 已提交
1307 1308 1309 1310
	err = ip6mr_notifier_init(net);
	if (err)
		return err;

1311 1312
	err = ip6mr_rules_init(net);
	if (err < 0)
Y
Yuval Mintz 已提交
1313
		goto ip6mr_rules_fail;
1314 1315 1316

#ifdef CONFIG_PROC_FS
	err = -ENOMEM;
1317
	if (!proc_create("ip6_mr_vif", 0, net->proc_net, &ip6mr_vif_fops))
1318
		goto proc_vif_fail;
1319
	if (!proc_create("ip6_mr_cache", 0, net->proc_net, &ip6mr_mfc_fops))
1320 1321
		goto proc_cache_fail;
#endif
1322

1323 1324
	return 0;

1325 1326
#ifdef CONFIG_PROC_FS
proc_cache_fail:
1327
	remove_proc_entry("ip6_mr_vif", net->proc_net);
1328
proc_vif_fail:
1329
	ip6mr_rules_exit(net);
1330
#endif
Y
Yuval Mintz 已提交
1331 1332
ip6mr_rules_fail:
	ip6mr_notifier_exit(net);
1333 1334 1335 1336 1337
	return err;
}

static void __net_exit ip6mr_net_exit(struct net *net)
{
1338
#ifdef CONFIG_PROC_FS
1339 1340
	remove_proc_entry("ip6_mr_cache", net->proc_net);
	remove_proc_entry("ip6_mr_vif", net->proc_net);
1341
#endif
1342
	ip6mr_rules_exit(net);
Y
Yuval Mintz 已提交
1343
	ip6mr_notifier_exit(net);
1344 1345 1346 1347 1348
}

static struct pernet_operations ip6mr_net_ops = {
	.init = ip6mr_net_init,
	.exit = ip6mr_net_exit,
K
Kirill Tkhai 已提交
1349
	.async = true,
1350 1351
};

W
Wang Chen 已提交
1352
int __init ip6_mr_init(void)
1353
{
W
Wang Chen 已提交
1354 1355
	int err;

1356 1357 1358 1359 1360
	mrt_cachep = kmem_cache_create("ip6_mrt_cache",
				       sizeof(struct mfc6_cache),
				       0, SLAB_HWCACHE_ALIGN,
				       NULL);
	if (!mrt_cachep)
W
Wang Chen 已提交
1361
		return -ENOMEM;
1362

1363 1364 1365 1366
	err = register_pernet_subsys(&ip6mr_net_ops);
	if (err)
		goto reg_pernet_fail;

W
Wang Chen 已提交
1367 1368 1369
	err = register_netdevice_notifier(&ip6_mr_notifier);
	if (err)
		goto reg_notif_fail;
T
Tom Goff 已提交
1370 1371
#ifdef CONFIG_IPV6_PIMSM_V2
	if (inet6_add_protocol(&pim6_protocol, IPPROTO_PIM) < 0) {
1372
		pr_err("%s: can't add PIM protocol\n", __func__);
T
Tom Goff 已提交
1373 1374 1375 1376
		err = -EAGAIN;
		goto add_proto_fail;
	}
#endif
1377 1378 1379 1380 1381
	err = rtnl_register_module(THIS_MODULE, RTNL_FAMILY_IP6MR, RTM_GETROUTE,
				   NULL, ip6mr_rtm_dumproute, 0);
	if (err == 0)
		return 0;

T
Tom Goff 已提交
1382
#ifdef CONFIG_IPV6_PIMSM_V2
1383
	inet6_del_protocol(&pim6_protocol, IPPROTO_PIM);
T
Tom Goff 已提交
1384 1385 1386
add_proto_fail:
	unregister_netdevice_notifier(&ip6_mr_notifier);
#endif
B
Benjamin Thery 已提交
1387
reg_notif_fail:
1388 1389
	unregister_pernet_subsys(&ip6mr_net_ops);
reg_pernet_fail:
B
Benjamin Thery 已提交
1390
	kmem_cache_destroy(mrt_cachep);
W
Wang Chen 已提交
1391
	return err;
1392 1393
}

W
Wang Chen 已提交
1394 1395
void ip6_mr_cleanup(void)
{
1396 1397 1398 1399
	rtnl_unregister(RTNL_FAMILY_IP6MR, RTM_GETROUTE);
#ifdef CONFIG_IPV6_PIMSM_V2
	inet6_del_protocol(&pim6_protocol, IPPROTO_PIM);
#endif
W
Wang Chen 已提交
1400
	unregister_netdevice_notifier(&ip6_mr_notifier);
1401
	unregister_pernet_subsys(&ip6mr_net_ops);
W
Wang Chen 已提交
1402 1403
	kmem_cache_destroy(mrt_cachep);
}
1404

1405
static int ip6mr_mfc_add(struct net *net, struct mr_table *mrt,
1406
			 struct mf6cctl *mfc, int mrtsock, int parent)
1407
{
1408
	unsigned char ttls[MAXMIFS];
1409
	struct mfc6_cache *uc, *c;
1410
	struct mr_mfc *_uc;
1411 1412
	bool found;
	int i, err;
1413

1414 1415 1416
	if (mfc->mf6cc_parent >= MAXMIFS)
		return -ENFILE;

1417 1418
	memset(ttls, 255, MAXMIFS);
	for (i = 0; i < MAXMIFS; i++) {
1419 1420 1421 1422
		if (IF_ISSET(i, &mfc->mf6cc_ifset))
			ttls[i] = 1;
	}

1423 1424 1425 1426 1427 1428
	/* The entries are added/deleted only under RTNL */
	rcu_read_lock();
	c = ip6mr_cache_find_parent(mrt, &mfc->mf6cc_origin.sin6_addr,
				    &mfc->mf6cc_mcastgrp.sin6_addr, parent);
	rcu_read_unlock();
	if (c) {
1429
		write_lock_bh(&mrt_lock);
1430 1431
		c->_c.mfc_parent = mfc->mf6cc_parent;
		ip6mr_update_thresholds(mrt, &c->_c, ttls);
1432
		if (!mrtsock)
1433
			c->_c.mfc_flags |= MFC_STATIC;
1434
		write_unlock_bh(&mrt_lock);
Y
Yuval Mintz 已提交
1435 1436
		call_ip6mr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE,
					       c, mrt->id);
1437
		mr6_netlink_event(mrt, c, RTM_NEWROUTE);
1438 1439 1440
		return 0;
	}

1441 1442
	if (!ipv6_addr_any(&mfc->mf6cc_mcastgrp.sin6_addr) &&
	    !ipv6_addr_is_multicast(&mfc->mf6cc_mcastgrp.sin6_addr))
1443 1444
		return -EINVAL;

1445
	c = ip6mr_cache_alloc();
1446
	if (!c)
1447 1448 1449 1450
		return -ENOMEM;

	c->mf6c_origin = mfc->mf6cc_origin.sin6_addr;
	c->mf6c_mcastgrp = mfc->mf6cc_mcastgrp.sin6_addr;
1451 1452
	c->_c.mfc_parent = mfc->mf6cc_parent;
	ip6mr_update_thresholds(mrt, &c->_c, ttls);
1453
	if (!mrtsock)
1454
		c->_c.mfc_flags |= MFC_STATIC;
1455

1456
	err = rhltable_insert_key(&mrt->mfc_hash, &c->cmparg, &c->_c.mnode,
1457 1458 1459 1460 1461 1462
				  ip6mr_rht_params);
	if (err) {
		pr_err("ip6mr: rhtable insert error %d\n", err);
		ip6mr_cache_free(c);
		return err;
	}
1463
	list_add_tail_rcu(&c->_c.list, &mrt->mfc_cache_list);
1464

1465 1466
	/* Check to see if we resolved a queued list. If so we
	 * need to send on the frames and tidy up.
1467
	 */
1468
	found = false;
1469
	spin_lock_bh(&mfc_unres_lock);
1470 1471
	list_for_each_entry(_uc, &mrt->mfc_unres_queue, list) {
		uc = (struct mfc6_cache *)_uc;
1472
		if (ipv6_addr_equal(&uc->mf6c_origin, &c->mf6c_origin) &&
1473
		    ipv6_addr_equal(&uc->mf6c_mcastgrp, &c->mf6c_mcastgrp)) {
1474
			list_del(&_uc->list);
1475
			atomic_dec(&mrt->cache_resolve_queue_len);
1476
			found = true;
1477 1478 1479
			break;
		}
	}
1480
	if (list_empty(&mrt->mfc_unres_queue))
1481
		del_timer(&mrt->ipmr_expire_timer);
1482 1483
	spin_unlock_bh(&mfc_unres_lock);

1484
	if (found) {
1485
		ip6mr_cache_resolve(net, mrt, uc, c);
1486
		ip6mr_cache_free(uc);
1487
	}
Y
Yuval Mintz 已提交
1488 1489
	call_ip6mr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_ADD,
				       c, mrt->id);
1490
	mr6_netlink_event(mrt, c, RTM_NEWROUTE);
1491 1492 1493 1494 1495 1496 1497
	return 0;
}

/*
 *	Close the multicast socket, and clear the vif tables etc
 */

1498
static void mroute_clean_tables(struct mr_table *mrt, bool all)
1499
{
1500
	struct mr_mfc *c, *tmp;
1501
	LIST_HEAD(list);
1502
	int i;
1503

1504
	/* Shut down all active vif entries */
1505
	for (i = 0; i < mrt->maxvif; i++) {
1506
		if (!all && (mrt->vif_table[i].flags & VIFF_STATIC))
1507
			continue;
1508
		mif6_delete(mrt, i, 0, &list);
1509
	}
1510
	unregister_netdevice_many(&list);
1511

1512
	/* Wipe the cache */
1513
	list_for_each_entry_safe(c, tmp, &mrt->mfc_cache_list, list) {
1514 1515
		if (!all && (c->mfc_flags & MFC_STATIC))
			continue;
1516
		rhltable_remove(&mrt->mfc_hash, &c->mnode, ip6mr_rht_params);
1517
		list_del_rcu(&c->list);
1518 1519
		mr6_netlink_event(mrt, (struct mfc6_cache *)c, RTM_DELROUTE);
		ip6mr_cache_free((struct mfc6_cache *)c);
1520 1521
	}

1522
	if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
1523
		spin_lock_bh(&mfc_unres_lock);
1524
		list_for_each_entry_safe(c, tmp, &mrt->mfc_unres_queue, list) {
1525
			list_del(&c->list);
Y
Yuval Mintz 已提交
1526 1527 1528 1529
			call_ip6mr_mfc_entry_notifiers(read_pnet(&mrt->net),
						       FIB_EVENT_ENTRY_DEL,
						       (struct mfc6_cache *)c,
						       mrt->id);
1530 1531 1532
			mr6_netlink_event(mrt, (struct mfc6_cache *)c,
					  RTM_DELROUTE);
			ip6mr_destroy_unres(mrt, (struct mfc6_cache *)c);
1533 1534 1535 1536 1537
		}
		spin_unlock_bh(&mfc_unres_lock);
	}
}

1538
static int ip6mr_sk_init(struct mr_table *mrt, struct sock *sk)
1539 1540
{
	int err = 0;
1541
	struct net *net = sock_net(sk);
1542 1543 1544

	rtnl_lock();
	write_lock_bh(&mrt_lock);
1545
	if (rtnl_dereference(mrt->mroute_sk)) {
1546
		err = -EADDRINUSE;
Y
Yuval Mintz 已提交
1547
	} else {
1548
		rcu_assign_pointer(mrt->mroute_sk, sk);
1549
		sock_set_flag(sk, SOCK_RCU_FREE);
Y
Yuval Mintz 已提交
1550
		net->ipv6.devconf_all->mc_forwarding++;
1551
	}
1552 1553
	write_unlock_bh(&mrt_lock);

1554
	if (!err)
1555 1556
		inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
					     NETCONFA_MC_FORWARDING,
1557 1558
					     NETCONFA_IFINDEX_ALL,
					     net->ipv6.devconf_all);
1559 1560 1561 1562 1563 1564 1565
	rtnl_unlock();

	return err;
}

int ip6mr_sk_done(struct sock *sk)
{
1566
	int err = -EACCES;
1567
	struct net *net = sock_net(sk);
1568
	struct mr_table *mrt;
1569

1570 1571 1572 1573
	if (sk->sk_type != SOCK_RAW ||
	    inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
		return err;

1574
	rtnl_lock();
1575
	ip6mr_for_each_table(mrt, net) {
1576
		if (sk == rtnl_dereference(mrt->mroute_sk)) {
1577
			write_lock_bh(&mrt_lock);
1578
			RCU_INIT_POINTER(mrt->mroute_sk, NULL);
1579 1580 1581 1582
			/* Note that mroute_sk had SOCK_RCU_FREE set,
			 * so the RCU grace period before sk freeing
			 * is guaranteed by sk_destruct()
			 */
1583
			net->ipv6.devconf_all->mc_forwarding--;
1584
			write_unlock_bh(&mrt_lock);
1585
			inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
1586 1587 1588
						     NETCONFA_MC_FORWARDING,
						     NETCONFA_IFINDEX_ALL,
						     net->ipv6.devconf_all);
1589

1590
			mroute_clean_tables(mrt, false);
1591 1592 1593 1594
			err = 0;
			break;
		}
	}
1595 1596 1597 1598 1599
	rtnl_unlock();

	return err;
}

Y
Yuval Mintz 已提交
1600
bool mroute6_is_socket(struct net *net, struct sk_buff *skb)
1601
{
1602
	struct mr_table *mrt;
1603
	struct flowi6 fl6 = {
1604
		.flowi6_iif	= skb->skb_iif ? : LOOPBACK_IFINDEX,
1605 1606
		.flowi6_oif	= skb->dev->ifindex,
		.flowi6_mark	= skb->mark,
1607 1608
	};

1609
	if (ip6mr_fib_lookup(net, &fl6, &mrt) < 0)
1610
		return NULL;
1611

1612
	return rcu_access_pointer(mrt->mroute_sk);
1613
}
Y
Yuval Mintz 已提交
1614
EXPORT_SYMBOL(mroute6_is_socket);
1615

1616 1617 1618 1619 1620 1621 1622
/*
 *	Socket options and virtual interface manipulation. The whole
 *	virtual interface system is a complete heap, but unfortunately
 *	that's how BSD mrouted happens to think. Maybe one day with a proper
 *	MOSPF/PIM router set up we can clean this up.
 */

1623
int ip6_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
1624
{
1625
	int ret, parent = 0;
1626 1627 1628
	struct mif6ctl vif;
	struct mf6cctl mfc;
	mifi_t mifi;
1629
	struct net *net = sock_net(sk);
1630
	struct mr_table *mrt;
1631

1632 1633 1634 1635
	if (sk->sk_type != SOCK_RAW ||
	    inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
		return -EOPNOTSUPP;

1636
	mrt = ip6mr_get_table(net, raw6_sk(sk)->ip6mr_table ? : RT6_TABLE_DFLT);
1637
	if (!mrt)
1638
		return -ENOENT;
1639 1640

	if (optname != MRT6_INIT) {
1641
		if (sk != rcu_access_pointer(mrt->mroute_sk) &&
Y
Yuval Mintz 已提交
1642
		    !ns_capable(net->user_ns, CAP_NET_ADMIN))
1643 1644 1645 1646 1647 1648 1649 1650
			return -EACCES;
	}

	switch (optname) {
	case MRT6_INIT:
		if (optlen < sizeof(int))
			return -EINVAL;

1651
		return ip6mr_sk_init(mrt, sk);
1652 1653 1654 1655 1656 1657 1658 1659 1660

	case MRT6_DONE:
		return ip6mr_sk_done(sk);

	case MRT6_ADD_MIF:
		if (optlen < sizeof(vif))
			return -EINVAL;
		if (copy_from_user(&vif, optval, sizeof(vif)))
			return -EFAULT;
1661
		if (vif.mif6c_mifi >= MAXMIFS)
1662 1663
			return -ENFILE;
		rtnl_lock();
Y
Yuval Mintz 已提交
1664
		ret = mif6_add(net, mrt, &vif,
1665
			       sk == rtnl_dereference(mrt->mroute_sk));
1666 1667 1668 1669 1670 1671 1672 1673 1674
		rtnl_unlock();
		return ret;

	case MRT6_DEL_MIF:
		if (optlen < sizeof(mifi_t))
			return -EINVAL;
		if (copy_from_user(&mifi, optval, sizeof(mifi_t)))
			return -EFAULT;
		rtnl_lock();
1675
		ret = mif6_delete(mrt, mifi, 0, NULL);
1676 1677 1678 1679 1680 1681 1682 1683 1684
		rtnl_unlock();
		return ret;

	/*
	 *	Manipulate the forwarding caches. These live
	 *	in a sort of kernel/user symbiosis.
	 */
	case MRT6_ADD_MFC:
	case MRT6_DEL_MFC:
1685
		parent = -1;
1686
		/* fall through */
1687 1688
	case MRT6_ADD_MFC_PROXY:
	case MRT6_DEL_MFC_PROXY:
1689 1690 1691 1692
		if (optlen < sizeof(mfc))
			return -EINVAL;
		if (copy_from_user(&mfc, optval, sizeof(mfc)))
			return -EFAULT;
1693 1694
		if (parent == 0)
			parent = mfc.mf6cc_parent;
1695
		rtnl_lock();
1696 1697
		if (optname == MRT6_DEL_MFC || optname == MRT6_DEL_MFC_PROXY)
			ret = ip6mr_mfc_delete(mrt, &mfc, parent);
1698
		else
1699
			ret = ip6mr_mfc_add(net, mrt, &mfc,
Y
Yuval Mintz 已提交
1700
					    sk ==
1701
					    rtnl_dereference(mrt->mroute_sk),
Y
Yuval Mintz 已提交
1702
					    parent);
1703 1704 1705
		rtnl_unlock();
		return ret;

1706 1707 1708 1709 1710 1711
	/*
	 *	Control PIM assert (to activate pim will activate assert)
	 */
	case MRT6_ASSERT:
	{
		int v;
1712 1713 1714

		if (optlen != sizeof(v))
			return -EINVAL;
1715 1716
		if (get_user(v, (int __user *)optval))
			return -EFAULT;
1717
		mrt->mroute_do_assert = v;
1718 1719 1720 1721 1722 1723
		return 0;
	}

#ifdef CONFIG_IPV6_PIMSM_V2
	case MRT6_PIM:
	{
1724
		int v;
1725 1726 1727

		if (optlen != sizeof(v))
			return -EINVAL;
1728 1729 1730 1731 1732
		if (get_user(v, (int __user *)optval))
			return -EFAULT;
		v = !!v;
		rtnl_lock();
		ret = 0;
1733 1734 1735
		if (v != mrt->mroute_do_pim) {
			mrt->mroute_do_pim = v;
			mrt->mroute_do_assert = v;
1736 1737 1738 1739 1740
		}
		rtnl_unlock();
		return ret;
	}

1741 1742 1743 1744 1745 1746 1747 1748 1749 1750
#endif
#ifdef CONFIG_IPV6_MROUTE_MULTIPLE_TABLES
	case MRT6_TABLE:
	{
		u32 v;

		if (optlen != sizeof(u32))
			return -EINVAL;
		if (get_user(v, (u32 __user *)optval))
			return -EFAULT;
1751 1752 1753
		/* "pim6reg%u" should not exceed 16 bytes (IFNAMSIZ) */
		if (v != RT_TABLE_DEFAULT && v >= 100000000)
			return -EINVAL;
1754
		if (sk == rcu_access_pointer(mrt->mroute_sk))
1755 1756 1757 1758 1759 1760 1761 1762 1763 1764
			return -EBUSY;

		rtnl_lock();
		ret = 0;
		if (!ip6mr_new_table(net, v))
			ret = -ENOMEM;
		raw6_sk(sk)->ip6mr_table = v;
		rtnl_unlock();
		return ret;
	}
1765
#endif
1766
	/*
1767
	 *	Spurious command, or MRT6_VERSION which you cannot
1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783
	 *	set.
	 */
	default:
		return -ENOPROTOOPT;
	}
}

/*
 *	Getsock opt support for the multicast routing system.
 */

int ip6_mroute_getsockopt(struct sock *sk, int optname, char __user *optval,
			  int __user *optlen)
{
	int olr;
	int val;
1784
	struct net *net = sock_net(sk);
1785
	struct mr_table *mrt;
1786

1787 1788 1789 1790
	if (sk->sk_type != SOCK_RAW ||
	    inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
		return -EOPNOTSUPP;

1791
	mrt = ip6mr_get_table(net, raw6_sk(sk)->ip6mr_table ? : RT6_TABLE_DFLT);
1792
	if (!mrt)
1793
		return -ENOENT;
1794 1795 1796 1797 1798

	switch (optname) {
	case MRT6_VERSION:
		val = 0x0305;
		break;
1799 1800
#ifdef CONFIG_IPV6_PIMSM_V2
	case MRT6_PIM:
1801
		val = mrt->mroute_do_pim;
1802 1803 1804
		break;
#endif
	case MRT6_ASSERT:
1805
		val = mrt->mroute_do_assert;
1806
		break;
1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832
	default:
		return -ENOPROTOOPT;
	}

	if (get_user(olr, optlen))
		return -EFAULT;

	olr = min_t(int, olr, sizeof(int));
	if (olr < 0)
		return -EINVAL;

	if (put_user(olr, optlen))
		return -EFAULT;
	if (copy_to_user(optval, &val, olr))
		return -EFAULT;
	return 0;
}

/*
 *	The IP multicast ioctl support routines.
 */

int ip6mr_ioctl(struct sock *sk, int cmd, void __user *arg)
{
	struct sioc_sg_req6 sr;
	struct sioc_mif_req6 vr;
1833
	struct vif_device *vif;
1834
	struct mfc6_cache *c;
1835
	struct net *net = sock_net(sk);
1836
	struct mr_table *mrt;
1837 1838

	mrt = ip6mr_get_table(net, raw6_sk(sk)->ip6mr_table ? : RT6_TABLE_DFLT);
1839
	if (!mrt)
1840
		return -ENOENT;
1841 1842 1843 1844 1845

	switch (cmd) {
	case SIOCGETMIFCNT_IN6:
		if (copy_from_user(&vr, arg, sizeof(vr)))
			return -EFAULT;
1846
		if (vr.mifi >= mrt->maxvif)
1847 1848
			return -EINVAL;
		read_lock(&mrt_lock);
1849 1850
		vif = &mrt->vif_table[vr.mifi];
		if (VIF_EXISTS(mrt, vr.mifi)) {
1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866
			vr.icount = vif->pkt_in;
			vr.ocount = vif->pkt_out;
			vr.ibytes = vif->bytes_in;
			vr.obytes = vif->bytes_out;
			read_unlock(&mrt_lock);

			if (copy_to_user(arg, &vr, sizeof(vr)))
				return -EFAULT;
			return 0;
		}
		read_unlock(&mrt_lock);
		return -EADDRNOTAVAIL;
	case SIOCGETSGCNT_IN6:
		if (copy_from_user(&sr, arg, sizeof(sr)))
			return -EFAULT;

1867
		rcu_read_lock();
1868
		c = ip6mr_cache_find(mrt, &sr.src.sin6_addr, &sr.grp.sin6_addr);
1869
		if (c) {
1870 1871 1872
			sr.pktcnt = c->_c.mfc_un.res.pkt;
			sr.bytecnt = c->_c.mfc_un.res.bytes;
			sr.wrong_if = c->_c.mfc_un.res.wrong_if;
1873
			rcu_read_unlock();
1874 1875 1876 1877 1878

			if (copy_to_user(arg, &sr, sizeof(sr)))
				return -EFAULT;
			return 0;
		}
1879
		rcu_read_unlock();
1880 1881 1882 1883 1884 1885
		return -EADDRNOTAVAIL;
	default:
		return -ENOIOCTLCMD;
	}
}

1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906
#ifdef CONFIG_COMPAT
struct compat_sioc_sg_req6 {
	struct sockaddr_in6 src;
	struct sockaddr_in6 grp;
	compat_ulong_t pktcnt;
	compat_ulong_t bytecnt;
	compat_ulong_t wrong_if;
};

struct compat_sioc_mif_req6 {
	mifi_t	mifi;
	compat_ulong_t icount;
	compat_ulong_t ocount;
	compat_ulong_t ibytes;
	compat_ulong_t obytes;
};

int ip6mr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
{
	struct compat_sioc_sg_req6 sr;
	struct compat_sioc_mif_req6 vr;
1907
	struct vif_device *vif;
1908 1909
	struct mfc6_cache *c;
	struct net *net = sock_net(sk);
1910
	struct mr_table *mrt;
1911 1912

	mrt = ip6mr_get_table(net, raw6_sk(sk)->ip6mr_table ? : RT6_TABLE_DFLT);
1913
	if (!mrt)
1914 1915 1916 1917 1918 1919 1920 1921 1922
		return -ENOENT;

	switch (cmd) {
	case SIOCGETMIFCNT_IN6:
		if (copy_from_user(&vr, arg, sizeof(vr)))
			return -EFAULT;
		if (vr.mifi >= mrt->maxvif)
			return -EINVAL;
		read_lock(&mrt_lock);
1923 1924
		vif = &mrt->vif_table[vr.mifi];
		if (VIF_EXISTS(mrt, vr.mifi)) {
1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940
			vr.icount = vif->pkt_in;
			vr.ocount = vif->pkt_out;
			vr.ibytes = vif->bytes_in;
			vr.obytes = vif->bytes_out;
			read_unlock(&mrt_lock);

			if (copy_to_user(arg, &vr, sizeof(vr)))
				return -EFAULT;
			return 0;
		}
		read_unlock(&mrt_lock);
		return -EADDRNOTAVAIL;
	case SIOCGETSGCNT_IN6:
		if (copy_from_user(&sr, arg, sizeof(sr)))
			return -EFAULT;

1941
		rcu_read_lock();
1942 1943
		c = ip6mr_cache_find(mrt, &sr.src.sin6_addr, &sr.grp.sin6_addr);
		if (c) {
1944 1945 1946
			sr.pktcnt = c->_c.mfc_un.res.pkt;
			sr.bytecnt = c->_c.mfc_un.res.bytes;
			sr.wrong_if = c->_c.mfc_un.res.wrong_if;
1947
			rcu_read_unlock();
1948 1949 1950 1951 1952

			if (copy_to_user(arg, &sr, sizeof(sr)))
				return -EFAULT;
			return 0;
		}
1953
		rcu_read_unlock();
1954 1955 1956 1957 1958 1959
		return -EADDRNOTAVAIL;
	default:
		return -ENOIOCTLCMD;
	}
}
#endif
1960

1961
static inline int ip6mr_forward2_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
1962
{
E
Eric Dumazet 已提交
1963 1964 1965 1966
	__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
			IPSTATS_MIB_OUTFORWDATAGRAMS);
	__IP6_ADD_STATS(net, ip6_dst_idev(skb_dst(skb)),
			IPSTATS_MIB_OUTOCTETS, skb->len);
1967
	return dst_output(net, sk, skb);
1968 1969 1970 1971 1972 1973
}

/*
 *	Processing handlers for ip6mr_forward
 */

1974
static int ip6mr_forward2(struct net *net, struct mr_table *mrt,
1975
			  struct sk_buff *skb, struct mfc6_cache *c, int vifi)
1976 1977
{
	struct ipv6hdr *ipv6h;
1978
	struct vif_device *vif = &mrt->vif_table[vifi];
1979 1980
	struct net_device *dev;
	struct dst_entry *dst;
1981
	struct flowi6 fl6;
1982

1983
	if (!vif->dev)
1984 1985
		goto out_free;

1986 1987 1988 1989
#ifdef CONFIG_IPV6_PIMSM_V2
	if (vif->flags & MIFF_REGISTER) {
		vif->pkt_out++;
		vif->bytes_out += skb->len;
1990 1991
		vif->dev->stats.tx_bytes += skb->len;
		vif->dev->stats.tx_packets++;
1992
		ip6mr_cache_report(mrt, skb, vifi, MRT6MSG_WHOLEPKT);
1993
		goto out_free;
1994 1995 1996
	}
#endif

1997 1998
	ipv6h = ipv6_hdr(skb);

1999 2000 2001
	fl6 = (struct flowi6) {
		.flowi6_oif = vif->link,
		.daddr = ipv6h->daddr,
2002 2003
	};

2004
	dst = ip6_route_output(net, NULL, &fl6);
2005 2006
	if (dst->error) {
		dst_release(dst);
2007
		goto out_free;
2008
	}
2009

E
Eric Dumazet 已提交
2010 2011
	skb_dst_drop(skb);
	skb_dst_set(skb, dst);
2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038

	/*
	 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
	 * not only before forwarding, but after forwarding on all output
	 * interfaces. It is clear, if mrouter runs a multicasting
	 * program, it should receive packets not depending to what interface
	 * program is joined.
	 * If we will not make it, the program will have to join on all
	 * interfaces. On the other hand, multihoming host (or router, but
	 * not mrouter) cannot join to more than one interface - it will
	 * result in receiving multiple packets.
	 */
	dev = vif->dev;
	skb->dev = dev;
	vif->pkt_out++;
	vif->bytes_out += skb->len;

	/* We are about to write */
	/* XXX: extension headers? */
	if (skb_cow(skb, sizeof(*ipv6h) + LL_RESERVED_SPACE(dev)))
		goto out_free;

	ipv6h = ipv6_hdr(skb);
	ipv6h->hop_limit--;

	IP6CB(skb)->flags |= IP6SKB_FORWARDED;

2039 2040
	return NF_HOOK(NFPROTO_IPV6, NF_INET_FORWARD,
		       net, NULL, skb, skb->dev, dev,
2041 2042 2043 2044 2045 2046 2047
		       ip6mr_forward2_finish);

out_free:
	kfree_skb(skb);
	return 0;
}

2048
static int ip6mr_find_vif(struct mr_table *mrt, struct net_device *dev)
2049 2050
{
	int ct;
2051 2052

	for (ct = mrt->maxvif - 1; ct >= 0; ct--) {
2053
		if (mrt->vif_table[ct].dev == dev)
2054 2055 2056 2057 2058
			break;
	}
	return ct;
}

2059
static void ip6_mr_forward(struct net *net, struct mr_table *mrt,
2060
			   struct sk_buff *skb, struct mfc6_cache *c)
2061 2062 2063
{
	int psend = -1;
	int vif, ct;
2064
	int true_vifi = ip6mr_find_vif(mrt, skb->dev);
2065

2066 2067 2068 2069
	vif = c->_c.mfc_parent;
	c->_c.mfc_un.res.pkt++;
	c->_c.mfc_un.res.bytes += skb->len;
	c->_c.mfc_un.res.lastuse = jiffies;
2070

2071
	if (ipv6_addr_any(&c->mf6c_origin) && true_vifi >= 0) {
2072 2073
		struct mfc6_cache *cache_proxy;

2074
		/* For an (*,G) entry, we only check that the incoming
2075 2076
		 * interface is part of the static tree.
		 */
2077
		rcu_read_lock();
2078
		cache_proxy = mr_mfc_find_any_parent(mrt, vif);
2079
		if (cache_proxy &&
2080
		    cache_proxy->_c.mfc_un.res.ttls[true_vifi] < 255) {
2081
			rcu_read_unlock();
2082
			goto forward;
2083 2084
		}
		rcu_read_unlock();
2085 2086
	}

2087 2088 2089
	/*
	 * Wrong interface: drop packet and (maybe) send PIM assert.
	 */
2090
	if (mrt->vif_table[vif].dev != skb->dev) {
2091
		c->_c.mfc_un.res.wrong_if++;
2092

2093
		if (true_vifi >= 0 && mrt->mroute_do_assert &&
2094 2095 2096 2097 2098
		    /* pimsm uses asserts, when switching from RPT to SPT,
		       so that we cannot check that packet arrived on an oif.
		       It is bad, but otherwise we would need to move pretty
		       large chunk of pimd to kernel. Ough... --ANK
		     */
2099
		    (mrt->mroute_do_pim ||
2100
		     c->_c.mfc_un.res.ttls[true_vifi] < 255) &&
2101
		    time_after(jiffies,
2102 2103 2104
			       c->_c.mfc_un.res.last_assert +
			       MFC_ASSERT_THRESH)) {
			c->_c.mfc_un.res.last_assert = jiffies;
2105
			ip6mr_cache_report(mrt, skb, true_vifi, MRT6MSG_WRONGMIF);
2106 2107 2108 2109
		}
		goto dont_forward;
	}

2110
forward:
2111 2112
	mrt->vif_table[vif].pkt_in++;
	mrt->vif_table[vif].bytes_in += skb->len;
2113 2114 2115 2116

	/*
	 *	Forward the frame
	 */
2117 2118
	if (ipv6_addr_any(&c->mf6c_origin) &&
	    ipv6_addr_any(&c->mf6c_mcastgrp)) {
2119
		if (true_vifi >= 0 &&
2120
		    true_vifi != c->_c.mfc_parent &&
2121
		    ipv6_hdr(skb)->hop_limit >
2122
				c->_c.mfc_un.res.ttls[c->_c.mfc_parent]) {
2123 2124 2125 2126
			/* It's an (*,*) entry and the packet is not coming from
			 * the upstream: forward the packet to the upstream
			 * only.
			 */
2127
			psend = c->_c.mfc_parent;
2128 2129 2130 2131
			goto last_forward;
		}
		goto dont_forward;
	}
2132 2133
	for (ct = c->_c.mfc_un.res.maxvif - 1;
	     ct >= c->_c.mfc_un.res.minvif; ct--) {
2134
		/* For (*,G) entry, don't forward to the incoming interface */
2135 2136
		if ((!ipv6_addr_any(&c->mf6c_origin) || ct != true_vifi) &&
		    ipv6_hdr(skb)->hop_limit > c->_c.mfc_un.res.ttls[ct]) {
2137 2138 2139
			if (psend != -1) {
				struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
				if (skb2)
2140 2141
					ip6mr_forward2(net, mrt, skb2,
						       c, psend);
2142 2143 2144 2145
			}
			psend = ct;
		}
	}
2146
last_forward:
2147
	if (psend != -1) {
2148
		ip6mr_forward2(net, mrt, skb, c, psend);
2149
		return;
2150 2151
	}

2152
dont_forward:
2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163
	kfree_skb(skb);
}


/*
 *	Multicast packets for forwarding arrive here
 */

int ip6_mr_input(struct sk_buff *skb)
{
	struct mfc6_cache *cache;
2164
	struct net *net = dev_net(skb->dev);
2165
	struct mr_table *mrt;
2166 2167 2168
	struct flowi6 fl6 = {
		.flowi6_iif	= skb->dev->ifindex,
		.flowi6_mark	= skb->mark,
2169 2170 2171
	};
	int err;

2172
	err = ip6mr_fib_lookup(net, &fl6, &mrt);
2173 2174
	if (err < 0) {
		kfree_skb(skb);
2175
		return err;
2176
	}
2177 2178

	read_lock(&mrt_lock);
2179
	cache = ip6mr_cache_find(mrt,
2180
				 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr);
2181
	if (!cache) {
2182 2183 2184 2185 2186 2187 2188
		int vif = ip6mr_find_vif(mrt, skb->dev);

		if (vif >= 0)
			cache = ip6mr_cache_find_any(mrt,
						     &ipv6_hdr(skb)->daddr,
						     vif);
	}
2189 2190 2191 2192

	/*
	 *	No usable cache entry
	 */
2193
	if (!cache) {
2194 2195
		int vif;

2196
		vif = ip6mr_find_vif(mrt, skb->dev);
2197
		if (vif >= 0) {
2198
			int err = ip6mr_cache_unresolved(mrt, vif, skb);
2199 2200 2201 2202 2203 2204 2205 2206 2207
			read_unlock(&mrt_lock);

			return err;
		}
		read_unlock(&mrt_lock);
		kfree_skb(skb);
		return -ENODEV;
	}

2208
	ip6_mr_forward(net, mrt, skb, cache);
2209 2210 2211 2212 2213 2214

	read_unlock(&mrt_lock);

	return 0;
}

2215
int ip6mr_get_route(struct net *net, struct sk_buff *skb, struct rtmsg *rtm,
2216
		    u32 portid)
2217 2218
{
	int err;
2219
	struct mr_table *mrt;
2220
	struct mfc6_cache *cache;
E
Eric Dumazet 已提交
2221
	struct rt6_info *rt = (struct rt6_info *)skb_dst(skb);
2222

2223
	mrt = ip6mr_get_table(net, RT6_TABLE_DFLT);
2224
	if (!mrt)
2225 2226
		return -ENOENT;

2227
	read_lock(&mrt_lock);
2228
	cache = ip6mr_cache_find(mrt, &rt->rt6i_src.addr, &rt->rt6i_dst.addr);
2229 2230 2231 2232 2233 2234 2235
	if (!cache && skb->dev) {
		int vif = ip6mr_find_vif(mrt, skb->dev);

		if (vif >= 0)
			cache = ip6mr_cache_find_any(mrt, &rt->rt6i_dst.addr,
						     vif);
	}
2236 2237 2238 2239 2240 2241 2242 2243

	if (!cache) {
		struct sk_buff *skb2;
		struct ipv6hdr *iph;
		struct net_device *dev;
		int vif;

		dev = skb->dev;
2244
		if (!dev || (vif = ip6mr_find_vif(mrt, dev)) < 0) {
2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255
			read_unlock(&mrt_lock);
			return -ENODEV;
		}

		/* really correct? */
		skb2 = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
		if (!skb2) {
			read_unlock(&mrt_lock);
			return -ENOMEM;
		}

2256
		NETLINK_CB(skb2).portid = portid;
2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270
		skb_reset_transport_header(skb2);

		skb_put(skb2, sizeof(struct ipv6hdr));
		skb_reset_network_header(skb2);

		iph = ipv6_hdr(skb2);
		iph->version = 0;
		iph->priority = 0;
		iph->flow_lbl[0] = 0;
		iph->flow_lbl[1] = 0;
		iph->flow_lbl[2] = 0;
		iph->payload_len = 0;
		iph->nexthdr = IPPROTO_NONE;
		iph->hop_limit = 0;
A
Alexey Dobriyan 已提交
2271 2272
		iph->saddr = rt->rt6i_src.addr;
		iph->daddr = rt->rt6i_dst.addr;
2273

2274
		err = ip6mr_cache_unresolved(mrt, vif, skb2);
2275 2276 2277 2278 2279
		read_unlock(&mrt_lock);

		return err;
	}

Y
Yuval Mintz 已提交
2280
	err = mr_fill_mroute(mrt, skb, &cache->_c, rtm);
2281 2282 2283 2284
	read_unlock(&mrt_lock);
	return err;
}

2285
static int ip6mr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2286 2287
			     u32 portid, u32 seq, struct mfc6_cache *c, int cmd,
			     int flags)
2288 2289 2290
{
	struct nlmsghdr *nlh;
	struct rtmsg *rtm;
2291
	int err;
2292

2293
	nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
2294
	if (!nlh)
2295 2296 2297
		return -EMSGSIZE;

	rtm = nlmsg_data(nlh);
2298
	rtm->rtm_family   = RTNL_FAMILY_IP6MR;
2299 2300 2301 2302
	rtm->rtm_dst_len  = 128;
	rtm->rtm_src_len  = 128;
	rtm->rtm_tos      = 0;
	rtm->rtm_table    = mrt->id;
D
David S. Miller 已提交
2303 2304
	if (nla_put_u32(skb, RTA_TABLE, mrt->id))
		goto nla_put_failure;
2305
	rtm->rtm_type = RTN_MULTICAST;
2306
	rtm->rtm_scope    = RT_SCOPE_UNIVERSE;
2307
	if (c->_c.mfc_flags & MFC_STATIC)
2308 2309 2310
		rtm->rtm_protocol = RTPROT_STATIC;
	else
		rtm->rtm_protocol = RTPROT_MROUTED;
2311 2312
	rtm->rtm_flags    = 0;

2313 2314
	if (nla_put_in6_addr(skb, RTA_SRC, &c->mf6c_origin) ||
	    nla_put_in6_addr(skb, RTA_DST, &c->mf6c_mcastgrp))
D
David S. Miller 已提交
2315
		goto nla_put_failure;
Y
Yuval Mintz 已提交
2316
	err = mr_fill_mroute(mrt, skb, &c->_c, rtm);
2317 2318
	/* do not break the dump if cache is unresolved */
	if (err < 0 && err != -ENOENT)
2319 2320
		goto nla_put_failure;

2321 2322
	nlmsg_end(skb, nlh);
	return 0;
2323 2324 2325 2326 2327 2328

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

Y
Yuval Mintz 已提交
2329 2330 2331 2332 2333 2334 2335 2336
static int _ip6mr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
			      u32 portid, u32 seq, struct mr_mfc *c,
			      int cmd, int flags)
{
	return ip6mr_fill_mroute(mrt, skb, portid, seq, (struct mfc6_cache *)c,
				 cmd, flags);
}

2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351
static int mr6_msgsize(bool unresolved, int maxvif)
{
	size_t len =
		NLMSG_ALIGN(sizeof(struct rtmsg))
		+ nla_total_size(4)	/* RTA_TABLE */
		+ nla_total_size(sizeof(struct in6_addr))	/* RTA_SRC */
		+ nla_total_size(sizeof(struct in6_addr))	/* RTA_DST */
		;

	if (!unresolved)
		len = len
		      + nla_total_size(4)	/* RTA_IIF */
		      + nla_total_size(0)	/* RTA_MULTIPATH */
		      + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
						/* RTA_MFC_STATS */
2352
		      + nla_total_size_64bit(sizeof(struct rta_mfc_stats))
2353 2354 2355 2356 2357
		;

	return len;
}

2358
static void mr6_netlink_event(struct mr_table *mrt, struct mfc6_cache *mfc,
2359 2360 2361 2362 2363 2364
			      int cmd)
{
	struct net *net = read_pnet(&mrt->net);
	struct sk_buff *skb;
	int err = -ENOBUFS;

2365
	skb = nlmsg_new(mr6_msgsize(mfc->_c.mfc_parent >= MAXMIFS, mrt->maxvif),
2366
			GFP_ATOMIC);
2367
	if (!skb)
2368 2369
		goto errout;

2370
	err = ip6mr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382
	if (err < 0)
		goto errout;

	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_MROUTE, NULL, GFP_ATOMIC);
	return;

errout:
	kfree_skb(skb);
	if (err < 0)
		rtnl_set_sk_err(net, RTNLGRP_IPV6_MROUTE, err);
}

2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399
static size_t mrt6msg_netlink_msgsize(size_t payloadlen)
{
	size_t len =
		NLMSG_ALIGN(sizeof(struct rtgenmsg))
		+ nla_total_size(1)	/* IP6MRA_CREPORT_MSGTYPE */
		+ nla_total_size(4)	/* IP6MRA_CREPORT_MIF_ID */
					/* IP6MRA_CREPORT_SRC_ADDR */
		+ nla_total_size(sizeof(struct in6_addr))
					/* IP6MRA_CREPORT_DST_ADDR */
		+ nla_total_size(sizeof(struct in6_addr))
					/* IP6MRA_CREPORT_PKT */
		+ nla_total_size(payloadlen)
		;

	return len;
}

2400
static void mrt6msg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt)
2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447
{
	struct net *net = read_pnet(&mrt->net);
	struct nlmsghdr *nlh;
	struct rtgenmsg *rtgenm;
	struct mrt6msg *msg;
	struct sk_buff *skb;
	struct nlattr *nla;
	int payloadlen;

	payloadlen = pkt->len - sizeof(struct mrt6msg);
	msg = (struct mrt6msg *)skb_transport_header(pkt);

	skb = nlmsg_new(mrt6msg_netlink_msgsize(payloadlen), GFP_ATOMIC);
	if (!skb)
		goto errout;

	nlh = nlmsg_put(skb, 0, 0, RTM_NEWCACHEREPORT,
			sizeof(struct rtgenmsg), 0);
	if (!nlh)
		goto errout;
	rtgenm = nlmsg_data(nlh);
	rtgenm->rtgen_family = RTNL_FAMILY_IP6MR;
	if (nla_put_u8(skb, IP6MRA_CREPORT_MSGTYPE, msg->im6_msgtype) ||
	    nla_put_u32(skb, IP6MRA_CREPORT_MIF_ID, msg->im6_mif) ||
	    nla_put_in6_addr(skb, IP6MRA_CREPORT_SRC_ADDR,
			     &msg->im6_src) ||
	    nla_put_in6_addr(skb, IP6MRA_CREPORT_DST_ADDR,
			     &msg->im6_dst))
		goto nla_put_failure;

	nla = nla_reserve(skb, IP6MRA_CREPORT_PKT, payloadlen);
	if (!nla || skb_copy_bits(pkt, sizeof(struct mrt6msg),
				  nla_data(nla), payloadlen))
		goto nla_put_failure;

	nlmsg_end(skb, nlh);

	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_MROUTE_R, NULL, GFP_ATOMIC);
	return;

nla_put_failure:
	nlmsg_cancel(skb, nlh);
errout:
	kfree_skb(skb);
	rtnl_set_sk_err(net, RTNLGRP_IPV6_MROUTE_R, -ENOBUFS);
}

2448 2449
static int ip6mr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
{
Y
Yuval Mintz 已提交
2450 2451
	return mr_rtm_dumproute(skb, cb, ip6mr_mr_table_iter,
				_ip6mr_fill_mroute, &mfc_unres_lock);
2452
}