blob: 9fab274019c0656dd4f01365ac14e46e0f1f764d [file] [log] [blame]
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001/*
2 * Linux IPv6 multicast routing support for BSD pim6sd
3 * Based on net/ipv4/ipmr.c.
4 *
5 * (c) 2004 Mickael Hoerdt, <hoerdt@clarinet.u-strasbg.fr>
6 * LSIIT Laboratory, Strasbourg, France
7 * (c) 2004 Jean-Philippe Andriot, <jean-philippe.andriot@6WIND.com>
8 * 6WIND, Paris, France
9 * Copyright (C)2007,2008 USAGI/WIDE Project
10 * YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
16 *
17 */
18
19#include <asm/system.h>
20#include <asm/uaccess.h>
21#include <linux/types.h>
22#include <linux/sched.h>
23#include <linux/errno.h>
24#include <linux/timer.h>
25#include <linux/mm.h>
26#include <linux/kernel.h>
27#include <linux/fcntl.h>
28#include <linux/stat.h>
29#include <linux/socket.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090030#include <linux/inet.h>
31#include <linux/netdevice.h>
32#include <linux/inetdevice.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090033#include <linux/proc_fs.h>
34#include <linux/seq_file.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090035#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090037#include <net/protocol.h>
38#include <linux/skbuff.h>
39#include <net/sock.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090040#include <net/raw.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090041#include <linux/notifier.h>
42#include <linux/if_arp.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090043#include <net/checksum.h>
44#include <net/netlink.h>
Patrick McHardyd1db2752010-05-11 14:40:55 +020045#include <net/fib_rules.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090046
47#include <net/ipv6.h>
48#include <net/ip6_route.h>
49#include <linux/mroute6.h>
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +090050#include <linux/pim.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090051#include <net/addrconf.h>
52#include <linux/netfilter_ipv6.h>
Dave Jones5d6e4302009-01-31 00:51:49 -080053#include <net/ip6_checksum.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090054
Patrick McHardy6bd52142010-05-11 14:40:53 +020055struct mr6_table {
Patrick McHardyd1db2752010-05-11 14:40:55 +020056 struct list_head list;
Patrick McHardy6bd52142010-05-11 14:40:53 +020057#ifdef CONFIG_NET_NS
58 struct net *net;
59#endif
Patrick McHardyd1db2752010-05-11 14:40:55 +020060 u32 id;
Patrick McHardy6bd52142010-05-11 14:40:53 +020061 struct sock *mroute6_sk;
62 struct timer_list ipmr_expire_timer;
63 struct list_head mfc6_unres_queue;
64 struct list_head mfc6_cache_array[MFC6_LINES];
65 struct mif_device vif6_table[MAXMIFS];
66 int maxvif;
67 atomic_t cache_resolve_queue_len;
68 int mroute_do_assert;
69 int mroute_do_pim;
70#ifdef CONFIG_IPV6_PIMSM_V2
71 int mroute_reg_vif_num;
72#endif
73};
74
Patrick McHardyd1db2752010-05-11 14:40:55 +020075struct ip6mr_rule {
76 struct fib_rule common;
77};
78
79struct ip6mr_result {
80 struct mr6_table *mrt;
81};
82
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090083/* Big lock, protecting vif table, mrt cache and mroute socket state.
84 Note that the changes are semaphored via rtnl_lock.
85 */
86
87static DEFINE_RWLOCK(mrt_lock);
88
89/*
90 * Multicast router control variables
91 */
92
Patrick McHardy6bd52142010-05-11 14:40:53 +020093#define MIF_EXISTS(_mrt, _idx) ((_mrt)->vif6_table[_idx].dev != NULL)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090094
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090095/* Special spinlock for queue of unresolved entries */
96static DEFINE_SPINLOCK(mfc_unres_lock);
97
98/* We return to original Alan's scheme. Hash table of resolved
99 entries is changed only in process context and protected
100 with weak lock mrt_lock. Queue of unresolved entries is protected
101 with strong spinlock mfc_unres_lock.
102
103 In this case data path is free of exclusive locks at all.
104 */
105
106static struct kmem_cache *mrt_cachep __read_mostly;
107
Patrick McHardyd1db2752010-05-11 14:40:55 +0200108static struct mr6_table *ip6mr_new_table(struct net *net, u32 id);
109static void ip6mr_free_table(struct mr6_table *mrt);
110
Patrick McHardy6bd52142010-05-11 14:40:53 +0200111static int ip6_mr_forward(struct net *net, struct mr6_table *mrt,
112 struct sk_buff *skb, struct mfc6_cache *cache);
113static int ip6mr_cache_report(struct mr6_table *mrt, struct sk_buff *pkt,
Benjamin Thery8229efd2008-12-10 16:30:15 -0800114 mifi_t mifi, int assert);
Patrick McHardy5b285ca2010-05-11 14:40:56 +0200115static int __ip6mr_fill_mroute(struct mr6_table *mrt, struct sk_buff *skb,
116 struct mfc6_cache *c, struct rtmsg *rtm);
117static int ip6mr_rtm_dumproute(struct sk_buff *skb,
118 struct netlink_callback *cb);
Patrick McHardy6bd52142010-05-11 14:40:53 +0200119static void mroute_clean_tables(struct mr6_table *mrt);
Patrick McHardyd1db2752010-05-11 14:40:55 +0200120static void ipmr_expire_process(unsigned long arg);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900121
Patrick McHardyd1db2752010-05-11 14:40:55 +0200122#ifdef CONFIG_IPV6_MROUTE_MULTIPLE_TABLES
Eric Dumazet8ffb3352010-06-06 15:34:40 -0700123#define ip6mr_for_each_table(mrt, net) \
Patrick McHardyd1db2752010-05-11 14:40:55 +0200124 list_for_each_entry_rcu(mrt, &net->ipv6.mr6_tables, list)
125
126static struct mr6_table *ip6mr_get_table(struct net *net, u32 id)
127{
128 struct mr6_table *mrt;
129
130 ip6mr_for_each_table(mrt, net) {
131 if (mrt->id == id)
132 return mrt;
133 }
134 return NULL;
135}
136
137static int ip6mr_fib_lookup(struct net *net, struct flowi *flp,
138 struct mr6_table **mrt)
139{
140 struct ip6mr_result res;
141 struct fib_lookup_arg arg = { .result = &res, };
142 int err;
143
144 err = fib_rules_lookup(net->ipv6.mr6_rules_ops, flp, 0, &arg);
145 if (err < 0)
146 return err;
147 *mrt = res.mrt;
148 return 0;
149}
150
151static int ip6mr_rule_action(struct fib_rule *rule, struct flowi *flp,
152 int flags, struct fib_lookup_arg *arg)
153{
154 struct ip6mr_result *res = arg->result;
155 struct mr6_table *mrt;
156
157 switch (rule->action) {
158 case FR_ACT_TO_TBL:
159 break;
160 case FR_ACT_UNREACHABLE:
161 return -ENETUNREACH;
162 case FR_ACT_PROHIBIT:
163 return -EACCES;
164 case FR_ACT_BLACKHOLE:
165 default:
166 return -EINVAL;
167 }
168
169 mrt = ip6mr_get_table(rule->fr_net, rule->table);
170 if (mrt == NULL)
171 return -EAGAIN;
172 res->mrt = mrt;
173 return 0;
174}
175
176static int ip6mr_rule_match(struct fib_rule *rule, struct flowi *flp, int flags)
177{
178 return 1;
179}
180
181static const struct nla_policy ip6mr_rule_policy[FRA_MAX + 1] = {
182 FRA_GENERIC_POLICY,
183};
184
185static int ip6mr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
186 struct fib_rule_hdr *frh, struct nlattr **tb)
187{
188 return 0;
189}
190
191static int ip6mr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
192 struct nlattr **tb)
193{
194 return 1;
195}
196
197static int ip6mr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
198 struct fib_rule_hdr *frh)
199{
200 frh->dst_len = 0;
201 frh->src_len = 0;
202 frh->tos = 0;
203 return 0;
204}
205
206static const struct fib_rules_ops __net_initdata ip6mr_rules_ops_template = {
207 .family = RTNL_FAMILY_IP6MR,
208 .rule_size = sizeof(struct ip6mr_rule),
209 .addr_size = sizeof(struct in6_addr),
210 .action = ip6mr_rule_action,
211 .match = ip6mr_rule_match,
212 .configure = ip6mr_rule_configure,
213 .compare = ip6mr_rule_compare,
214 .default_pref = fib_default_rule_pref,
215 .fill = ip6mr_rule_fill,
216 .nlgroup = RTNLGRP_IPV6_RULE,
217 .policy = ip6mr_rule_policy,
218 .owner = THIS_MODULE,
219};
220
221static int __net_init ip6mr_rules_init(struct net *net)
222{
223 struct fib_rules_ops *ops;
224 struct mr6_table *mrt;
225 int err;
226
227 ops = fib_rules_register(&ip6mr_rules_ops_template, net);
228 if (IS_ERR(ops))
229 return PTR_ERR(ops);
230
231 INIT_LIST_HEAD(&net->ipv6.mr6_tables);
232
233 mrt = ip6mr_new_table(net, RT6_TABLE_DFLT);
234 if (mrt == NULL) {
235 err = -ENOMEM;
236 goto err1;
237 }
238
239 err = fib_default_rule_add(ops, 0x7fff, RT6_TABLE_DFLT, 0);
240 if (err < 0)
241 goto err2;
242
243 net->ipv6.mr6_rules_ops = ops;
244 return 0;
245
246err2:
247 kfree(mrt);
248err1:
249 fib_rules_unregister(ops);
250 return err;
251}
252
253static void __net_exit ip6mr_rules_exit(struct net *net)
254{
255 struct mr6_table *mrt, *next;
256
Eric Dumazet035320d2010-06-06 23:48:40 +0000257 list_for_each_entry_safe(mrt, next, &net->ipv6.mr6_tables, list) {
258 list_del(&mrt->list);
Patrick McHardyd1db2752010-05-11 14:40:55 +0200259 ip6mr_free_table(mrt);
Eric Dumazet035320d2010-06-06 23:48:40 +0000260 }
Patrick McHardyd1db2752010-05-11 14:40:55 +0200261 fib_rules_unregister(net->ipv6.mr6_rules_ops);
262}
263#else
264#define ip6mr_for_each_table(mrt, net) \
265 for (mrt = net->ipv6.mrt6; mrt; mrt = NULL)
266
267static struct mr6_table *ip6mr_get_table(struct net *net, u32 id)
268{
269 return net->ipv6.mrt6;
270}
271
272static int ip6mr_fib_lookup(struct net *net, struct flowi *flp,
273 struct mr6_table **mrt)
274{
275 *mrt = net->ipv6.mrt6;
276 return 0;
277}
278
279static int __net_init ip6mr_rules_init(struct net *net)
280{
281 net->ipv6.mrt6 = ip6mr_new_table(net, RT6_TABLE_DFLT);
282 return net->ipv6.mrt6 ? 0 : -ENOMEM;
283}
284
285static void __net_exit ip6mr_rules_exit(struct net *net)
286{
287 ip6mr_free_table(net->ipv6.mrt6);
288}
289#endif
290
291static struct mr6_table *ip6mr_new_table(struct net *net, u32 id)
292{
293 struct mr6_table *mrt;
294 unsigned int i;
295
296 mrt = ip6mr_get_table(net, id);
297 if (mrt != NULL)
298 return mrt;
299
300 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
301 if (mrt == NULL)
302 return NULL;
303 mrt->id = id;
304 write_pnet(&mrt->net, net);
305
306 /* Forwarding cache */
307 for (i = 0; i < MFC6_LINES; i++)
308 INIT_LIST_HEAD(&mrt->mfc6_cache_array[i]);
309
310 INIT_LIST_HEAD(&mrt->mfc6_unres_queue);
311
312 setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
313 (unsigned long)mrt);
314
315#ifdef CONFIG_IPV6_PIMSM_V2
316 mrt->mroute_reg_vif_num = -1;
317#endif
318#ifdef CONFIG_IPV6_MROUTE_MULTIPLE_TABLES
319 list_add_tail_rcu(&mrt->list, &net->ipv6.mr6_tables);
320#endif
321 return mrt;
322}
323
324static void ip6mr_free_table(struct mr6_table *mrt)
325{
326 del_timer(&mrt->ipmr_expire_timer);
327 mroute_clean_tables(mrt);
328 kfree(mrt);
329}
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900330
331#ifdef CONFIG_PROC_FS
332
333struct ipmr_mfc_iter {
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800334 struct seq_net_private p;
Patrick McHardyd1db2752010-05-11 14:40:55 +0200335 struct mr6_table *mrt;
Patrick McHardyf30a77842010-05-11 14:40:51 +0200336 struct list_head *cache;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900337 int ct;
338};
339
340
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800341static struct mfc6_cache *ipmr_mfc_seq_idx(struct net *net,
342 struct ipmr_mfc_iter *it, loff_t pos)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900343{
Patrick McHardyd1db2752010-05-11 14:40:55 +0200344 struct mr6_table *mrt = it->mrt;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900345 struct mfc6_cache *mfc;
346
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900347 read_lock(&mrt_lock);
Patrick McHardyf30a77842010-05-11 14:40:51 +0200348 for (it->ct = 0; it->ct < MFC6_LINES; it->ct++) {
Patrick McHardy6bd52142010-05-11 14:40:53 +0200349 it->cache = &mrt->mfc6_cache_array[it->ct];
Patrick McHardyf30a77842010-05-11 14:40:51 +0200350 list_for_each_entry(mfc, it->cache, list)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900351 if (pos-- == 0)
352 return mfc;
Patrick McHardyf30a77842010-05-11 14:40:51 +0200353 }
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900354 read_unlock(&mrt_lock);
355
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900356 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy6bd52142010-05-11 14:40:53 +0200357 it->cache = &mrt->mfc6_unres_queue;
Patrick McHardyf30a77842010-05-11 14:40:51 +0200358 list_for_each_entry(mfc, it->cache, list)
Patrick McHardyc476efb2010-05-11 14:40:48 +0200359 if (pos-- == 0)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900360 return mfc;
361 spin_unlock_bh(&mfc_unres_lock);
362
363 it->cache = NULL;
364 return NULL;
365}
366
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900367/*
368 * The /proc interfaces to multicast routing /proc/ip6_mr_cache /proc/ip6_mr_vif
369 */
370
371struct ipmr_vif_iter {
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800372 struct seq_net_private p;
Patrick McHardyd1db2752010-05-11 14:40:55 +0200373 struct mr6_table *mrt;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900374 int ct;
375};
376
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800377static struct mif_device *ip6mr_vif_seq_idx(struct net *net,
378 struct ipmr_vif_iter *iter,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900379 loff_t pos)
380{
Patrick McHardyd1db2752010-05-11 14:40:55 +0200381 struct mr6_table *mrt = iter->mrt;
Patrick McHardy6bd52142010-05-11 14:40:53 +0200382
383 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
384 if (!MIF_EXISTS(mrt, iter->ct))
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900385 continue;
386 if (pos-- == 0)
Patrick McHardy6bd52142010-05-11 14:40:53 +0200387 return &mrt->vif6_table[iter->ct];
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900388 }
389 return NULL;
390}
391
392static void *ip6mr_vif_seq_start(struct seq_file *seq, loff_t *pos)
393 __acquires(mrt_lock)
394{
Patrick McHardyd1db2752010-05-11 14:40:55 +0200395 struct ipmr_vif_iter *iter = seq->private;
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800396 struct net *net = seq_file_net(seq);
Patrick McHardyd1db2752010-05-11 14:40:55 +0200397 struct mr6_table *mrt;
398
399 mrt = ip6mr_get_table(net, RT6_TABLE_DFLT);
400 if (mrt == NULL)
401 return ERR_PTR(-ENOENT);
402
403 iter->mrt = mrt;
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800404
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900405 read_lock(&mrt_lock);
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800406 return *pos ? ip6mr_vif_seq_idx(net, seq->private, *pos - 1)
407 : SEQ_START_TOKEN;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900408}
409
410static void *ip6mr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
411{
412 struct ipmr_vif_iter *iter = seq->private;
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800413 struct net *net = seq_file_net(seq);
Patrick McHardyd1db2752010-05-11 14:40:55 +0200414 struct mr6_table *mrt = iter->mrt;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900415
416 ++*pos;
417 if (v == SEQ_START_TOKEN)
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800418 return ip6mr_vif_seq_idx(net, iter, 0);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900419
Patrick McHardy6bd52142010-05-11 14:40:53 +0200420 while (++iter->ct < mrt->maxvif) {
421 if (!MIF_EXISTS(mrt, iter->ct))
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900422 continue;
Patrick McHardy6bd52142010-05-11 14:40:53 +0200423 return &mrt->vif6_table[iter->ct];
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900424 }
425 return NULL;
426}
427
428static void ip6mr_vif_seq_stop(struct seq_file *seq, void *v)
429 __releases(mrt_lock)
430{
431 read_unlock(&mrt_lock);
432}
433
434static int ip6mr_vif_seq_show(struct seq_file *seq, void *v)
435{
Patrick McHardyd1db2752010-05-11 14:40:55 +0200436 struct ipmr_vif_iter *iter = seq->private;
437 struct mr6_table *mrt = iter->mrt;
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800438
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900439 if (v == SEQ_START_TOKEN) {
440 seq_puts(seq,
441 "Interface BytesIn PktsIn BytesOut PktsOut Flags\n");
442 } else {
443 const struct mif_device *vif = v;
444 const char *name = vif->dev ? vif->dev->name : "none";
445
446 seq_printf(seq,
Al Virod430a222008-06-02 10:59:02 +0100447 "%2td %-10s %8ld %7ld %8ld %7ld %05X\n",
Patrick McHardy6bd52142010-05-11 14:40:53 +0200448 vif - mrt->vif6_table,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900449 name, vif->bytes_in, vif->pkt_in,
450 vif->bytes_out, vif->pkt_out,
451 vif->flags);
452 }
453 return 0;
454}
455
Stephen Hemminger98147d52009-09-01 19:25:02 +0000456static const struct seq_operations ip6mr_vif_seq_ops = {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900457 .start = ip6mr_vif_seq_start,
458 .next = ip6mr_vif_seq_next,
459 .stop = ip6mr_vif_seq_stop,
460 .show = ip6mr_vif_seq_show,
461};
462
463static int ip6mr_vif_open(struct inode *inode, struct file *file)
464{
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800465 return seq_open_net(inode, file, &ip6mr_vif_seq_ops,
466 sizeof(struct ipmr_vif_iter));
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900467}
468
Stephen Hemminger5ca1b992009-09-01 19:25:05 +0000469static const struct file_operations ip6mr_vif_fops = {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900470 .owner = THIS_MODULE,
471 .open = ip6mr_vif_open,
472 .read = seq_read,
473 .llseek = seq_lseek,
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800474 .release = seq_release_net,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900475};
476
477static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
478{
Patrick McHardyd1db2752010-05-11 14:40:55 +0200479 struct ipmr_mfc_iter *it = seq->private;
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800480 struct net *net = seq_file_net(seq);
Patrick McHardyd1db2752010-05-11 14:40:55 +0200481 struct mr6_table *mrt;
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800482
Patrick McHardyd1db2752010-05-11 14:40:55 +0200483 mrt = ip6mr_get_table(net, RT6_TABLE_DFLT);
484 if (mrt == NULL)
485 return ERR_PTR(-ENOENT);
486
487 it->mrt = mrt;
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800488 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
489 : SEQ_START_TOKEN;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900490}
491
492static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
493{
494 struct mfc6_cache *mfc = v;
495 struct ipmr_mfc_iter *it = seq->private;
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800496 struct net *net = seq_file_net(seq);
Patrick McHardyd1db2752010-05-11 14:40:55 +0200497 struct mr6_table *mrt = it->mrt;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900498
499 ++*pos;
500
501 if (v == SEQ_START_TOKEN)
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800502 return ipmr_mfc_seq_idx(net, seq->private, 0);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900503
Patrick McHardyf30a77842010-05-11 14:40:51 +0200504 if (mfc->list.next != it->cache)
505 return list_entry(mfc->list.next, struct mfc6_cache, list);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900506
Patrick McHardy6bd52142010-05-11 14:40:53 +0200507 if (it->cache == &mrt->mfc6_unres_queue)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900508 goto end_of_list;
509
Patrick McHardy6bd52142010-05-11 14:40:53 +0200510 BUG_ON(it->cache != &mrt->mfc6_cache_array[it->ct]);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900511
Benjamin Thery4a6258a2008-12-10 16:24:07 -0800512 while (++it->ct < MFC6_LINES) {
Patrick McHardy6bd52142010-05-11 14:40:53 +0200513 it->cache = &mrt->mfc6_cache_array[it->ct];
Patrick McHardyf30a77842010-05-11 14:40:51 +0200514 if (list_empty(it->cache))
515 continue;
516 return list_first_entry(it->cache, struct mfc6_cache, list);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900517 }
518
519 /* exhausted cache_array, show unresolved */
520 read_unlock(&mrt_lock);
Patrick McHardy6bd52142010-05-11 14:40:53 +0200521 it->cache = &mrt->mfc6_unres_queue;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900522 it->ct = 0;
523
524 spin_lock_bh(&mfc_unres_lock);
Patrick McHardyf30a77842010-05-11 14:40:51 +0200525 if (!list_empty(it->cache))
526 return list_first_entry(it->cache, struct mfc6_cache, list);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900527
528 end_of_list:
529 spin_unlock_bh(&mfc_unres_lock);
530 it->cache = NULL;
531
532 return NULL;
533}
534
535static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
536{
537 struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyd1db2752010-05-11 14:40:55 +0200538 struct mr6_table *mrt = it->mrt;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900539
Patrick McHardy6bd52142010-05-11 14:40:53 +0200540 if (it->cache == &mrt->mfc6_unres_queue)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900541 spin_unlock_bh(&mfc_unres_lock);
Patrick McHardy6bd52142010-05-11 14:40:53 +0200542 else if (it->cache == mrt->mfc6_cache_array)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900543 read_unlock(&mrt_lock);
544}
545
546static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
547{
548 int n;
549
550 if (v == SEQ_START_TOKEN) {
551 seq_puts(seq,
552 "Group "
553 "Origin "
554 "Iif Pkts Bytes Wrong Oifs\n");
555 } else {
556 const struct mfc6_cache *mfc = v;
557 const struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyd1db2752010-05-11 14:40:55 +0200558 struct mr6_table *mrt = it->mrt;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900559
Benjamin Thery999890b2008-12-03 22:22:16 -0800560 seq_printf(seq, "%pI6 %pI6 %-3hd",
Harvey Harrison0c6ce782008-10-28 16:09:23 -0700561 &mfc->mf6c_mcastgrp, &mfc->mf6c_origin,
Benjamin Thery1ea472e2008-12-03 22:21:47 -0800562 mfc->mf6c_parent);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900563
Patrick McHardy6bd52142010-05-11 14:40:53 +0200564 if (it->cache != &mrt->mfc6_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -0800565 seq_printf(seq, " %8lu %8lu %8lu",
566 mfc->mfc_un.res.pkt,
567 mfc->mfc_un.res.bytes,
568 mfc->mfc_un.res.wrong_if);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900569 for (n = mfc->mfc_un.res.minvif;
570 n < mfc->mfc_un.res.maxvif; n++) {
Patrick McHardy6bd52142010-05-11 14:40:53 +0200571 if (MIF_EXISTS(mrt, n) &&
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900572 mfc->mfc_un.res.ttls[n] < 255)
573 seq_printf(seq,
574 " %2d:%-3d",
575 n, mfc->mfc_un.res.ttls[n]);
576 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -0800577 } else {
578 /* unresolved mfc_caches don't contain
579 * pkt, bytes and wrong_if values
580 */
581 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900582 }
583 seq_putc(seq, '\n');
584 }
585 return 0;
586}
587
James Morris88e9d342009-09-22 16:43:43 -0700588static const struct seq_operations ipmr_mfc_seq_ops = {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900589 .start = ipmr_mfc_seq_start,
590 .next = ipmr_mfc_seq_next,
591 .stop = ipmr_mfc_seq_stop,
592 .show = ipmr_mfc_seq_show,
593};
594
595static int ipmr_mfc_open(struct inode *inode, struct file *file)
596{
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800597 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
598 sizeof(struct ipmr_mfc_iter));
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900599}
600
Stephen Hemminger5ca1b992009-09-01 19:25:05 +0000601static const struct file_operations ip6mr_mfc_fops = {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900602 .owner = THIS_MODULE,
603 .open = ipmr_mfc_open,
604 .read = seq_read,
605 .llseek = seq_lseek,
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800606 .release = seq_release_net,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900607};
608#endif
609
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900610#ifdef CONFIG_IPV6_PIMSM_V2
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900611
612static int pim6_rcv(struct sk_buff *skb)
613{
614 struct pimreghdr *pim;
615 struct ipv6hdr *encap;
616 struct net_device *reg_dev = NULL;
Benjamin Thery8229efd2008-12-10 16:30:15 -0800617 struct net *net = dev_net(skb->dev);
Patrick McHardyd1db2752010-05-11 14:40:55 +0200618 struct mr6_table *mrt;
619 struct flowi fl = {
620 .iif = skb->dev->ifindex,
621 .mark = skb->mark,
622 };
623 int reg_vif_num;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900624
625 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(*encap)))
626 goto drop;
627
628 pim = (struct pimreghdr *)skb_transport_header(skb);
629 if (pim->type != ((PIM_VERSION << 4) | PIM_REGISTER) ||
630 (pim->flags & PIM_NULL_REGISTER) ||
Thomas Goff1d6e55f2009-01-27 22:39:59 -0800631 (csum_ipv6_magic(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
632 sizeof(*pim), IPPROTO_PIM,
633 csum_partial((void *)pim, sizeof(*pim), 0)) &&
Al Viroec6b4862008-04-26 22:28:58 -0700634 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900635 goto drop;
636
637 /* check if the inner packet is destined to mcast group */
638 encap = (struct ipv6hdr *)(skb_transport_header(skb) +
639 sizeof(*pim));
640
641 if (!ipv6_addr_is_multicast(&encap->daddr) ||
642 encap->payload_len == 0 ||
643 ntohs(encap->payload_len) + sizeof(*pim) > skb->len)
644 goto drop;
645
Patrick McHardyd1db2752010-05-11 14:40:55 +0200646 if (ip6mr_fib_lookup(net, &fl, &mrt) < 0)
647 goto drop;
648 reg_vif_num = mrt->mroute_reg_vif_num;
649
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900650 read_lock(&mrt_lock);
651 if (reg_vif_num >= 0)
Patrick McHardy6bd52142010-05-11 14:40:53 +0200652 reg_dev = mrt->vif6_table[reg_vif_num].dev;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900653 if (reg_dev)
654 dev_hold(reg_dev);
655 read_unlock(&mrt_lock);
656
657 if (reg_dev == NULL)
658 goto drop;
659
660 skb->mac_header = skb->network_header;
661 skb_pull(skb, (u8 *)encap - skb->data);
662 skb_reset_network_header(skb);
Thomas Goff1d6e55f2009-01-27 22:39:59 -0800663 skb->protocol = htons(ETH_P_IPV6);
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900664 skb->ip_summed = 0;
665 skb->pkt_type = PACKET_HOST;
Eric Dumazetd19d56d2010-05-17 22:36:55 -0700666
667 skb_tunnel_rx(skb, reg_dev);
668
Eric Dumazetcaf586e2010-09-30 21:06:55 +0000669 netif_rx(skb);
Eric Dumazet8990f462010-09-20 00:12:11 +0000670
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900671 dev_put(reg_dev);
672 return 0;
673 drop:
674 kfree_skb(skb);
675 return 0;
676}
677
Alexey Dobriyan41135cc2009-09-14 12:22:28 +0000678static const struct inet6_protocol pim6_protocol = {
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900679 .handler = pim6_rcv,
680};
681
682/* Service routines creating virtual interfaces: PIMREG */
683
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000684static netdev_tx_t reg_vif_xmit(struct sk_buff *skb,
685 struct net_device *dev)
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900686{
Benjamin Thery8229efd2008-12-10 16:30:15 -0800687 struct net *net = dev_net(dev);
Patrick McHardyd1db2752010-05-11 14:40:55 +0200688 struct mr6_table *mrt;
689 struct flowi fl = {
690 .oif = dev->ifindex,
691 .iif = skb->skb_iif,
692 .mark = skb->mark,
693 };
694 int err;
695
696 err = ip6mr_fib_lookup(net, &fl, &mrt);
697 if (err < 0)
698 return err;
Benjamin Thery8229efd2008-12-10 16:30:15 -0800699
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900700 read_lock(&mrt_lock);
Pavel Emelyanovdc58c782008-05-21 14:17:54 -0700701 dev->stats.tx_bytes += skb->len;
702 dev->stats.tx_packets++;
Patrick McHardy6bd52142010-05-11 14:40:53 +0200703 ip6mr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, MRT6MSG_WHOLEPKT);
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900704 read_unlock(&mrt_lock);
705 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000706 return NETDEV_TX_OK;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900707}
708
Stephen Hemminger007c3832008-11-20 20:28:35 -0800709static const struct net_device_ops reg_vif_netdev_ops = {
710 .ndo_start_xmit = reg_vif_xmit,
711};
712
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900713static void reg_vif_setup(struct net_device *dev)
714{
715 dev->type = ARPHRD_PIMREG;
716 dev->mtu = 1500 - sizeof(struct ipv6hdr) - 8;
717 dev->flags = IFF_NOARP;
Stephen Hemminger007c3832008-11-20 20:28:35 -0800718 dev->netdev_ops = &reg_vif_netdev_ops;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900719 dev->destructor = free_netdev;
Tom Goff403dbb92009-06-14 03:16:13 -0700720 dev->features |= NETIF_F_NETNS_LOCAL;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900721}
722
Patrick McHardyd1db2752010-05-11 14:40:55 +0200723static struct net_device *ip6mr_reg_vif(struct net *net, struct mr6_table *mrt)
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900724{
725 struct net_device *dev;
Patrick McHardyd1db2752010-05-11 14:40:55 +0200726 char name[IFNAMSIZ];
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900727
Patrick McHardyd1db2752010-05-11 14:40:55 +0200728 if (mrt->id == RT6_TABLE_DFLT)
729 sprintf(name, "pim6reg");
730 else
731 sprintf(name, "pim6reg%u", mrt->id);
732
733 dev = alloc_netdev(0, name, reg_vif_setup);
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900734 if (dev == NULL)
735 return NULL;
736
Benjamin Thery8229efd2008-12-10 16:30:15 -0800737 dev_net_set(dev, net);
738
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900739 if (register_netdevice(dev)) {
740 free_netdev(dev);
741 return NULL;
742 }
743 dev->iflink = 0;
744
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900745 if (dev_open(dev))
746 goto failure;
747
Wang Chen7af3db72008-07-14 20:54:54 -0700748 dev_hold(dev);
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900749 return dev;
750
751failure:
752 /* allow the register to be completed before unregistering. */
753 rtnl_unlock();
754 rtnl_lock();
755
756 unregister_netdevice(dev);
757 return NULL;
758}
759#endif
760
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900761/*
762 * Delete a VIF entry
763 */
764
Patrick McHardy6bd52142010-05-11 14:40:53 +0200765static int mif6_delete(struct mr6_table *mrt, int vifi, struct list_head *head)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900766{
767 struct mif_device *v;
768 struct net_device *dev;
Thomas Goff1d6e55f2009-01-27 22:39:59 -0800769 struct inet6_dev *in6_dev;
Patrick McHardy6bd52142010-05-11 14:40:53 +0200770
771 if (vifi < 0 || vifi >= mrt->maxvif)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900772 return -EADDRNOTAVAIL;
773
Patrick McHardy6bd52142010-05-11 14:40:53 +0200774 v = &mrt->vif6_table[vifi];
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900775
776 write_lock_bh(&mrt_lock);
777 dev = v->dev;
778 v->dev = NULL;
779
780 if (!dev) {
781 write_unlock_bh(&mrt_lock);
782 return -EADDRNOTAVAIL;
783 }
784
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900785#ifdef CONFIG_IPV6_PIMSM_V2
Patrick McHardy6bd52142010-05-11 14:40:53 +0200786 if (vifi == mrt->mroute_reg_vif_num)
787 mrt->mroute_reg_vif_num = -1;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900788#endif
789
Patrick McHardy6bd52142010-05-11 14:40:53 +0200790 if (vifi + 1 == mrt->maxvif) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900791 int tmp;
792 for (tmp = vifi - 1; tmp >= 0; tmp--) {
Patrick McHardy6bd52142010-05-11 14:40:53 +0200793 if (MIF_EXISTS(mrt, tmp))
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900794 break;
795 }
Patrick McHardy6bd52142010-05-11 14:40:53 +0200796 mrt->maxvif = tmp + 1;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900797 }
798
799 write_unlock_bh(&mrt_lock);
800
801 dev_set_allmulti(dev, -1);
802
Thomas Goff1d6e55f2009-01-27 22:39:59 -0800803 in6_dev = __in6_dev_get(dev);
804 if (in6_dev)
805 in6_dev->cnf.mc_forwarding--;
806
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900807 if (v->flags & MIFF_REGISTER)
Eric Dumazetc871e662009-10-28 04:48:11 +0000808 unregister_netdevice_queue(dev, head);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900809
810 dev_put(dev);
811 return 0;
812}
813
Benjamin Thery58701ad2008-12-10 16:22:34 -0800814static inline void ip6mr_cache_free(struct mfc6_cache *c)
815{
Benjamin Thery58701ad2008-12-10 16:22:34 -0800816 kmem_cache_free(mrt_cachep, c);
817}
818
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900819/* Destroy an unresolved cache entry, killing queued skbs
820 and reporting error to netlink readers.
821 */
822
Patrick McHardy6bd52142010-05-11 14:40:53 +0200823static void ip6mr_destroy_unres(struct mr6_table *mrt, struct mfc6_cache *c)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900824{
Patrick McHardy6bd52142010-05-11 14:40:53 +0200825 struct net *net = read_pnet(&mrt->net);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900826 struct sk_buff *skb;
827
Patrick McHardy6bd52142010-05-11 14:40:53 +0200828 atomic_dec(&mrt->cache_resolve_queue_len);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900829
830 while((skb = skb_dequeue(&c->mfc_un.unres.unresolved)) != NULL) {
831 if (ipv6_hdr(skb)->version == 0) {
832 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct ipv6hdr));
833 nlh->nlmsg_type = NLMSG_ERROR;
834 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
835 skb_trim(skb, nlh->nlmsg_len);
836 ((struct nlmsgerr *)NLMSG_DATA(nlh))->error = -ETIMEDOUT;
Benjamin Thery8229efd2008-12-10 16:30:15 -0800837 rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900838 } else
839 kfree_skb(skb);
840 }
841
Benjamin Thery58701ad2008-12-10 16:22:34 -0800842 ip6mr_cache_free(c);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900843}
844
845
Patrick McHardyc476efb2010-05-11 14:40:48 +0200846/* Timer process for all the unresolved queue. */
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900847
Patrick McHardy6bd52142010-05-11 14:40:53 +0200848static void ipmr_do_expire_process(struct mr6_table *mrt)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900849{
850 unsigned long now = jiffies;
851 unsigned long expires = 10 * HZ;
Patrick McHardyf30a77842010-05-11 14:40:51 +0200852 struct mfc6_cache *c, *next;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900853
Patrick McHardy6bd52142010-05-11 14:40:53 +0200854 list_for_each_entry_safe(c, next, &mrt->mfc6_unres_queue, list) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900855 if (time_after(c->mfc_un.unres.expires, now)) {
856 /* not yet... */
857 unsigned long interval = c->mfc_un.unres.expires - now;
858 if (interval < expires)
859 expires = interval;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900860 continue;
861 }
862
Patrick McHardyf30a77842010-05-11 14:40:51 +0200863 list_del(&c->list);
Patrick McHardy6bd52142010-05-11 14:40:53 +0200864 ip6mr_destroy_unres(mrt, c);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900865 }
866
Patrick McHardy6bd52142010-05-11 14:40:53 +0200867 if (!list_empty(&mrt->mfc6_unres_queue))
868 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900869}
870
Patrick McHardyc476efb2010-05-11 14:40:48 +0200871static void ipmr_expire_process(unsigned long arg)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900872{
Patrick McHardy6bd52142010-05-11 14:40:53 +0200873 struct mr6_table *mrt = (struct mr6_table *)arg;
Patrick McHardyc476efb2010-05-11 14:40:48 +0200874
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900875 if (!spin_trylock(&mfc_unres_lock)) {
Patrick McHardy6bd52142010-05-11 14:40:53 +0200876 mod_timer(&mrt->ipmr_expire_timer, jiffies + 1);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900877 return;
878 }
879
Patrick McHardy6bd52142010-05-11 14:40:53 +0200880 if (!list_empty(&mrt->mfc6_unres_queue))
881 ipmr_do_expire_process(mrt);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900882
883 spin_unlock(&mfc_unres_lock);
884}
885
886/* Fill oifs list. It is called under write locked mrt_lock. */
887
Patrick McHardy6bd52142010-05-11 14:40:53 +0200888static void ip6mr_update_thresholds(struct mr6_table *mrt, struct mfc6_cache *cache,
Patrick McHardyb5aa30b2010-05-11 14:40:50 +0200889 unsigned char *ttls)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900890{
891 int vifi;
892
Rami Rosen6ac7eb02008-04-10 12:40:10 +0300893 cache->mfc_un.res.minvif = MAXMIFS;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900894 cache->mfc_un.res.maxvif = 0;
Rami Rosen6ac7eb02008-04-10 12:40:10 +0300895 memset(cache->mfc_un.res.ttls, 255, MAXMIFS);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900896
Patrick McHardy6bd52142010-05-11 14:40:53 +0200897 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
898 if (MIF_EXISTS(mrt, vifi) &&
Benjamin Thery4e168802008-12-10 16:15:08 -0800899 ttls[vifi] && ttls[vifi] < 255) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900900 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
901 if (cache->mfc_un.res.minvif > vifi)
902 cache->mfc_un.res.minvif = vifi;
903 if (cache->mfc_un.res.maxvif <= vifi)
904 cache->mfc_un.res.maxvif = vifi + 1;
905 }
906 }
907}
908
Patrick McHardy6bd52142010-05-11 14:40:53 +0200909static int mif6_add(struct net *net, struct mr6_table *mrt,
910 struct mif6ctl *vifc, int mrtsock)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900911{
912 int vifi = vifc->mif6c_mifi;
Patrick McHardy6bd52142010-05-11 14:40:53 +0200913 struct mif_device *v = &mrt->vif6_table[vifi];
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900914 struct net_device *dev;
Thomas Goff1d6e55f2009-01-27 22:39:59 -0800915 struct inet6_dev *in6_dev;
Wang Chen5ae7b442008-07-14 20:54:23 -0700916 int err;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900917
918 /* Is vif busy ? */
Patrick McHardy6bd52142010-05-11 14:40:53 +0200919 if (MIF_EXISTS(mrt, vifi))
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900920 return -EADDRINUSE;
921
922 switch (vifc->mif6c_flags) {
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900923#ifdef CONFIG_IPV6_PIMSM_V2
924 case MIFF_REGISTER:
925 /*
926 * Special Purpose VIF in PIM
927 * All the packets will be sent to the daemon
928 */
Patrick McHardy6bd52142010-05-11 14:40:53 +0200929 if (mrt->mroute_reg_vif_num >= 0)
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900930 return -EADDRINUSE;
Patrick McHardyd1db2752010-05-11 14:40:55 +0200931 dev = ip6mr_reg_vif(net, mrt);
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900932 if (!dev)
933 return -ENOBUFS;
Wang Chen5ae7b442008-07-14 20:54:23 -0700934 err = dev_set_allmulti(dev, 1);
935 if (err) {
936 unregister_netdevice(dev);
Wang Chen7af3db72008-07-14 20:54:54 -0700937 dev_put(dev);
Wang Chen5ae7b442008-07-14 20:54:23 -0700938 return err;
939 }
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900940 break;
941#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900942 case 0:
Benjamin Thery8229efd2008-12-10 16:30:15 -0800943 dev = dev_get_by_index(net, vifc->mif6c_pifi);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900944 if (!dev)
945 return -EADDRNOTAVAIL;
Wang Chen5ae7b442008-07-14 20:54:23 -0700946 err = dev_set_allmulti(dev, 1);
Wang Chen7af3db72008-07-14 20:54:54 -0700947 if (err) {
948 dev_put(dev);
Wang Chen5ae7b442008-07-14 20:54:23 -0700949 return err;
Wang Chen7af3db72008-07-14 20:54:54 -0700950 }
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900951 break;
952 default:
953 return -EINVAL;
954 }
955
Thomas Goff1d6e55f2009-01-27 22:39:59 -0800956 in6_dev = __in6_dev_get(dev);
957 if (in6_dev)
958 in6_dev->cnf.mc_forwarding++;
959
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900960 /*
961 * Fill in the VIF structures
962 */
963 v->rate_limit = vifc->vifc_rate_limit;
964 v->flags = vifc->mif6c_flags;
965 if (!mrtsock)
966 v->flags |= VIFF_STATIC;
967 v->threshold = vifc->vifc_threshold;
968 v->bytes_in = 0;
969 v->bytes_out = 0;
970 v->pkt_in = 0;
971 v->pkt_out = 0;
972 v->link = dev->ifindex;
973 if (v->flags & MIFF_REGISTER)
974 v->link = dev->iflink;
975
976 /* And finish update writing critical data */
977 write_lock_bh(&mrt_lock);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900978 v->dev = dev;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900979#ifdef CONFIG_IPV6_PIMSM_V2
980 if (v->flags & MIFF_REGISTER)
Patrick McHardy6bd52142010-05-11 14:40:53 +0200981 mrt->mroute_reg_vif_num = vifi;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900982#endif
Patrick McHardy6bd52142010-05-11 14:40:53 +0200983 if (vifi + 1 > mrt->maxvif)
984 mrt->maxvif = vifi + 1;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900985 write_unlock_bh(&mrt_lock);
986 return 0;
987}
988
Patrick McHardy6bd52142010-05-11 14:40:53 +0200989static struct mfc6_cache *ip6mr_cache_find(struct mr6_table *mrt,
Benjamin Thery8229efd2008-12-10 16:30:15 -0800990 struct in6_addr *origin,
991 struct in6_addr *mcastgrp)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900992{
993 int line = MFC6_HASH(mcastgrp, origin);
994 struct mfc6_cache *c;
995
Patrick McHardy6bd52142010-05-11 14:40:53 +0200996 list_for_each_entry(c, &mrt->mfc6_cache_array[line], list) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900997 if (ipv6_addr_equal(&c->mf6c_origin, origin) &&
998 ipv6_addr_equal(&c->mf6c_mcastgrp, mcastgrp))
Patrick McHardyf30a77842010-05-11 14:40:51 +0200999 return c;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001000 }
Patrick McHardyf30a77842010-05-11 14:40:51 +02001001 return NULL;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001002}
1003
1004/*
1005 * Allocate a multicast cache entry
1006 */
Patrick McHardyb5aa30b2010-05-11 14:40:50 +02001007static struct mfc6_cache *ip6mr_cache_alloc(void)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001008{
Joe Perches36cbac52008-12-03 22:27:25 -08001009 struct mfc6_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001010 if (c == NULL)
1011 return NULL;
Rami Rosen6ac7eb02008-04-10 12:40:10 +03001012 c->mfc_un.res.minvif = MAXMIFS;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001013 return c;
1014}
1015
Patrick McHardyb5aa30b2010-05-11 14:40:50 +02001016static struct mfc6_cache *ip6mr_cache_alloc_unres(void)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001017{
Joe Perches36cbac52008-12-03 22:27:25 -08001018 struct mfc6_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001019 if (c == NULL)
1020 return NULL;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001021 skb_queue_head_init(&c->mfc_un.unres.unresolved);
1022 c->mfc_un.unres.expires = jiffies + 10 * HZ;
1023 return c;
1024}
1025
1026/*
1027 * A cache entry has gone into a resolved state from queued
1028 */
1029
Patrick McHardy6bd52142010-05-11 14:40:53 +02001030static void ip6mr_cache_resolve(struct net *net, struct mr6_table *mrt,
1031 struct mfc6_cache *uc, struct mfc6_cache *c)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001032{
1033 struct sk_buff *skb;
1034
1035 /*
1036 * Play the pending entries through our router
1037 */
1038
1039 while((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
1040 if (ipv6_hdr(skb)->version == 0) {
1041 int err;
1042 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct ipv6hdr));
1043
Patrick McHardy5b285ca2010-05-11 14:40:56 +02001044 if (__ip6mr_fill_mroute(mrt, skb, c, NLMSG_DATA(nlh)) > 0) {
YOSHIFUJI Hideaki549e0282008-04-05 22:17:39 +09001045 nlh->nlmsg_len = skb_tail_pointer(skb) - (u8 *)nlh;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001046 } else {
1047 nlh->nlmsg_type = NLMSG_ERROR;
1048 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
1049 skb_trim(skb, nlh->nlmsg_len);
1050 ((struct nlmsgerr *)NLMSG_DATA(nlh))->error = -EMSGSIZE;
1051 }
Patrick McHardyb5aa30b2010-05-11 14:40:50 +02001052 err = rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001053 } else
Patrick McHardy6bd52142010-05-11 14:40:53 +02001054 ip6_mr_forward(net, mrt, skb, c);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001055 }
1056}
1057
1058/*
1059 * Bounce a cache query up to pim6sd. We could use netlink for this but pim6sd
1060 * expects the following bizarre scheme.
1061 *
1062 * Called under mrt_lock.
1063 */
1064
Patrick McHardy6bd52142010-05-11 14:40:53 +02001065static int ip6mr_cache_report(struct mr6_table *mrt, struct sk_buff *pkt,
1066 mifi_t mifi, int assert)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001067{
1068 struct sk_buff *skb;
1069 struct mrt6msg *msg;
1070 int ret;
1071
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001072#ifdef CONFIG_IPV6_PIMSM_V2
1073 if (assert == MRT6MSG_WHOLEPKT)
1074 skb = skb_realloc_headroom(pkt, -skb_network_offset(pkt)
1075 +sizeof(*msg));
1076 else
1077#endif
1078 skb = alloc_skb(sizeof(struct ipv6hdr) + sizeof(*msg), GFP_ATOMIC);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001079
1080 if (!skb)
1081 return -ENOBUFS;
1082
1083 /* I suppose that internal messages
1084 * do not require checksums */
1085
1086 skb->ip_summed = CHECKSUM_UNNECESSARY;
1087
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001088#ifdef CONFIG_IPV6_PIMSM_V2
1089 if (assert == MRT6MSG_WHOLEPKT) {
1090 /* Ugly, but we have no choice with this interface.
1091 Duplicate old header, fix length etc.
1092 And all this only to mangle msg->im6_msgtype and
1093 to set msg->im6_mbz to "mbz" :-)
1094 */
1095 skb_push(skb, -skb_network_offset(pkt));
1096
1097 skb_push(skb, sizeof(*msg));
1098 skb_reset_transport_header(skb);
1099 msg = (struct mrt6msg *)skb_transport_header(skb);
1100 msg->im6_mbz = 0;
1101 msg->im6_msgtype = MRT6MSG_WHOLEPKT;
Patrick McHardy6bd52142010-05-11 14:40:53 +02001102 msg->im6_mif = mrt->mroute_reg_vif_num;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001103 msg->im6_pad = 0;
1104 ipv6_addr_copy(&msg->im6_src, &ipv6_hdr(pkt)->saddr);
1105 ipv6_addr_copy(&msg->im6_dst, &ipv6_hdr(pkt)->daddr);
1106
1107 skb->ip_summed = CHECKSUM_UNNECESSARY;
1108 } else
1109#endif
1110 {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001111 /*
1112 * Copy the IP header
1113 */
1114
1115 skb_put(skb, sizeof(struct ipv6hdr));
1116 skb_reset_network_header(skb);
1117 skb_copy_to_linear_data(skb, ipv6_hdr(pkt), sizeof(struct ipv6hdr));
1118
1119 /*
1120 * Add our header
1121 */
1122 skb_put(skb, sizeof(*msg));
1123 skb_reset_transport_header(skb);
1124 msg = (struct mrt6msg *)skb_transport_header(skb);
1125
1126 msg->im6_mbz = 0;
1127 msg->im6_msgtype = assert;
Rami Rosen6ac7eb02008-04-10 12:40:10 +03001128 msg->im6_mif = mifi;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001129 msg->im6_pad = 0;
1130 ipv6_addr_copy(&msg->im6_src, &ipv6_hdr(pkt)->saddr);
1131 ipv6_addr_copy(&msg->im6_dst, &ipv6_hdr(pkt)->daddr);
1132
Eric Dumazetadf30902009-06-02 05:19:30 +00001133 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001134 skb->ip_summed = CHECKSUM_UNNECESSARY;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001135 }
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001136
Patrick McHardy6bd52142010-05-11 14:40:53 +02001137 if (mrt->mroute6_sk == NULL) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001138 kfree_skb(skb);
1139 return -EINVAL;
1140 }
1141
1142 /*
1143 * Deliver to user space multicast routing algorithms
1144 */
Patrick McHardy6bd52142010-05-11 14:40:53 +02001145 ret = sock_queue_rcv_skb(mrt->mroute6_sk, skb);
Benjamin Therybd91b8b2008-12-10 16:07:08 -08001146 if (ret < 0) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001147 if (net_ratelimit())
1148 printk(KERN_WARNING "mroute6: pending queue full, dropping entries.\n");
1149 kfree_skb(skb);
1150 }
1151
1152 return ret;
1153}
1154
1155/*
1156 * Queue a packet for resolution. It gets locked cache entry!
1157 */
1158
1159static int
Patrick McHardy6bd52142010-05-11 14:40:53 +02001160ip6mr_cache_unresolved(struct mr6_table *mrt, mifi_t mifi, struct sk_buff *skb)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001161{
Patrick McHardyf30a77842010-05-11 14:40:51 +02001162 bool found = false;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001163 int err;
1164 struct mfc6_cache *c;
1165
1166 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy6bd52142010-05-11 14:40:53 +02001167 list_for_each_entry(c, &mrt->mfc6_unres_queue, list) {
Patrick McHardyc476efb2010-05-11 14:40:48 +02001168 if (ipv6_addr_equal(&c->mf6c_mcastgrp, &ipv6_hdr(skb)->daddr) &&
Patrick McHardyf30a77842010-05-11 14:40:51 +02001169 ipv6_addr_equal(&c->mf6c_origin, &ipv6_hdr(skb)->saddr)) {
1170 found = true;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001171 break;
Patrick McHardyf30a77842010-05-11 14:40:51 +02001172 }
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001173 }
1174
Patrick McHardyf30a77842010-05-11 14:40:51 +02001175 if (!found) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001176 /*
1177 * Create a new entry if allowable
1178 */
1179
Patrick McHardy6bd52142010-05-11 14:40:53 +02001180 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
Patrick McHardyb5aa30b2010-05-11 14:40:50 +02001181 (c = ip6mr_cache_alloc_unres()) == NULL) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001182 spin_unlock_bh(&mfc_unres_lock);
1183
1184 kfree_skb(skb);
1185 return -ENOBUFS;
1186 }
1187
1188 /*
1189 * Fill in the new cache entry
1190 */
1191 c->mf6c_parent = -1;
1192 c->mf6c_origin = ipv6_hdr(skb)->saddr;
1193 c->mf6c_mcastgrp = ipv6_hdr(skb)->daddr;
1194
1195 /*
1196 * Reflect first query at pim6sd
1197 */
Patrick McHardy6bd52142010-05-11 14:40:53 +02001198 err = ip6mr_cache_report(mrt, skb, mifi, MRT6MSG_NOCACHE);
Benjamin Thery8229efd2008-12-10 16:30:15 -08001199 if (err < 0) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001200 /* If the report failed throw the cache entry
1201 out - Brad Parker
1202 */
1203 spin_unlock_bh(&mfc_unres_lock);
1204
Benjamin Thery58701ad2008-12-10 16:22:34 -08001205 ip6mr_cache_free(c);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001206 kfree_skb(skb);
1207 return err;
1208 }
1209
Patrick McHardy6bd52142010-05-11 14:40:53 +02001210 atomic_inc(&mrt->cache_resolve_queue_len);
1211 list_add(&c->list, &mrt->mfc6_unres_queue);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001212
Patrick McHardy6bd52142010-05-11 14:40:53 +02001213 ipmr_do_expire_process(mrt);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001214 }
1215
1216 /*
1217 * See if we can append the packet
1218 */
1219 if (c->mfc_un.unres.unresolved.qlen > 3) {
1220 kfree_skb(skb);
1221 err = -ENOBUFS;
1222 } else {
1223 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
1224 err = 0;
1225 }
1226
1227 spin_unlock_bh(&mfc_unres_lock);
1228 return err;
1229}
1230
1231/*
1232 * MFC6 cache manipulation by user space
1233 */
1234
Patrick McHardy6bd52142010-05-11 14:40:53 +02001235static int ip6mr_mfc_delete(struct mr6_table *mrt, struct mf6cctl *mfc)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001236{
1237 int line;
Patrick McHardyf30a77842010-05-11 14:40:51 +02001238 struct mfc6_cache *c, *next;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001239
1240 line = MFC6_HASH(&mfc->mf6cc_mcastgrp.sin6_addr, &mfc->mf6cc_origin.sin6_addr);
1241
Patrick McHardy6bd52142010-05-11 14:40:53 +02001242 list_for_each_entry_safe(c, next, &mrt->mfc6_cache_array[line], list) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001243 if (ipv6_addr_equal(&c->mf6c_origin, &mfc->mf6cc_origin.sin6_addr) &&
1244 ipv6_addr_equal(&c->mf6c_mcastgrp, &mfc->mf6cc_mcastgrp.sin6_addr)) {
1245 write_lock_bh(&mrt_lock);
Patrick McHardyf30a77842010-05-11 14:40:51 +02001246 list_del(&c->list);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001247 write_unlock_bh(&mrt_lock);
1248
Benjamin Thery58701ad2008-12-10 16:22:34 -08001249 ip6mr_cache_free(c);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001250 return 0;
1251 }
1252 }
1253 return -ENOENT;
1254}
1255
1256static int ip6mr_device_event(struct notifier_block *this,
1257 unsigned long event, void *ptr)
1258{
1259 struct net_device *dev = ptr;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001260 struct net *net = dev_net(dev);
Patrick McHardyd1db2752010-05-11 14:40:55 +02001261 struct mr6_table *mrt;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001262 struct mif_device *v;
1263 int ct;
Eric Dumazetc871e662009-10-28 04:48:11 +00001264 LIST_HEAD(list);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001265
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001266 if (event != NETDEV_UNREGISTER)
1267 return NOTIFY_DONE;
1268
Patrick McHardyd1db2752010-05-11 14:40:55 +02001269 ip6mr_for_each_table(mrt, net) {
1270 v = &mrt->vif6_table[0];
1271 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1272 if (v->dev == dev)
1273 mif6_delete(mrt, ct, &list);
1274 }
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001275 }
Eric Dumazetc871e662009-10-28 04:48:11 +00001276 unregister_netdevice_many(&list);
1277
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001278 return NOTIFY_DONE;
1279}
1280
1281static struct notifier_block ip6_mr_notifier = {
1282 .notifier_call = ip6mr_device_event
1283};
1284
1285/*
1286 * Setup for IP multicast routing
1287 */
1288
Benjamin Thery4e168802008-12-10 16:15:08 -08001289static int __net_init ip6mr_net_init(struct net *net)
1290{
Patrick McHardyd1db2752010-05-11 14:40:55 +02001291 int err;
Patrick McHardyf30a77842010-05-11 14:40:51 +02001292
Patrick McHardyd1db2752010-05-11 14:40:55 +02001293 err = ip6mr_rules_init(net);
1294 if (err < 0)
Benjamin Thery4e168802008-12-10 16:15:08 -08001295 goto fail;
Benjamin Thery8b90fc72008-12-10 16:29:48 -08001296
1297#ifdef CONFIG_PROC_FS
1298 err = -ENOMEM;
1299 if (!proc_net_fops_create(net, "ip6_mr_vif", 0, &ip6mr_vif_fops))
1300 goto proc_vif_fail;
1301 if (!proc_net_fops_create(net, "ip6_mr_cache", 0, &ip6mr_mfc_fops))
1302 goto proc_cache_fail;
1303#endif
Patrick McHardy6bd52142010-05-11 14:40:53 +02001304
Benjamin Thery4a6258a2008-12-10 16:24:07 -08001305 return 0;
1306
Benjamin Thery8b90fc72008-12-10 16:29:48 -08001307#ifdef CONFIG_PROC_FS
1308proc_cache_fail:
1309 proc_net_remove(net, "ip6_mr_vif");
1310proc_vif_fail:
Patrick McHardyd1db2752010-05-11 14:40:55 +02001311 ip6mr_rules_exit(net);
Benjamin Thery8b90fc72008-12-10 16:29:48 -08001312#endif
Benjamin Thery4e168802008-12-10 16:15:08 -08001313fail:
1314 return err;
1315}
1316
1317static void __net_exit ip6mr_net_exit(struct net *net)
1318{
Benjamin Thery8b90fc72008-12-10 16:29:48 -08001319#ifdef CONFIG_PROC_FS
1320 proc_net_remove(net, "ip6_mr_cache");
1321 proc_net_remove(net, "ip6_mr_vif");
1322#endif
Patrick McHardyd1db2752010-05-11 14:40:55 +02001323 ip6mr_rules_exit(net);
Benjamin Thery4e168802008-12-10 16:15:08 -08001324}
1325
1326static struct pernet_operations ip6mr_net_ops = {
1327 .init = ip6mr_net_init,
1328 .exit = ip6mr_net_exit,
1329};
1330
Wang Chen623d1a12008-07-03 12:13:30 +08001331int __init ip6_mr_init(void)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001332{
Wang Chen623d1a12008-07-03 12:13:30 +08001333 int err;
1334
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001335 mrt_cachep = kmem_cache_create("ip6_mrt_cache",
1336 sizeof(struct mfc6_cache),
1337 0, SLAB_HWCACHE_ALIGN,
1338 NULL);
1339 if (!mrt_cachep)
Wang Chen623d1a12008-07-03 12:13:30 +08001340 return -ENOMEM;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001341
Benjamin Thery4e168802008-12-10 16:15:08 -08001342 err = register_pernet_subsys(&ip6mr_net_ops);
1343 if (err)
1344 goto reg_pernet_fail;
1345
Wang Chen623d1a12008-07-03 12:13:30 +08001346 err = register_netdevice_notifier(&ip6_mr_notifier);
1347 if (err)
1348 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07001349#ifdef CONFIG_IPV6_PIMSM_V2
1350 if (inet6_add_protocol(&pim6_protocol, IPPROTO_PIM) < 0) {
1351 printk(KERN_ERR "ip6_mr_init: can't add PIM protocol\n");
1352 err = -EAGAIN;
1353 goto add_proto_fail;
1354 }
1355#endif
Patrick McHardy5b285ca2010-05-11 14:40:56 +02001356 rtnl_register(RTNL_FAMILY_IP6MR, RTM_GETROUTE, NULL, ip6mr_rtm_dumproute);
Wang Chen623d1a12008-07-03 12:13:30 +08001357 return 0;
Tom Goff403dbb92009-06-14 03:16:13 -07001358#ifdef CONFIG_IPV6_PIMSM_V2
1359add_proto_fail:
1360 unregister_netdevice_notifier(&ip6_mr_notifier);
1361#endif
Benjamin Thery87b30a62008-11-10 16:34:11 -08001362reg_notif_fail:
Benjamin Thery4e168802008-12-10 16:15:08 -08001363 unregister_pernet_subsys(&ip6mr_net_ops);
1364reg_pernet_fail:
Benjamin Thery87b30a62008-11-10 16:34:11 -08001365 kmem_cache_destroy(mrt_cachep);
Wang Chen623d1a12008-07-03 12:13:30 +08001366 return err;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001367}
1368
Wang Chen623d1a12008-07-03 12:13:30 +08001369void ip6_mr_cleanup(void)
1370{
Wang Chen623d1a12008-07-03 12:13:30 +08001371 unregister_netdevice_notifier(&ip6_mr_notifier);
Benjamin Thery4e168802008-12-10 16:15:08 -08001372 unregister_pernet_subsys(&ip6mr_net_ops);
Wang Chen623d1a12008-07-03 12:13:30 +08001373 kmem_cache_destroy(mrt_cachep);
1374}
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001375
Patrick McHardy6bd52142010-05-11 14:40:53 +02001376static int ip6mr_mfc_add(struct net *net, struct mr6_table *mrt,
1377 struct mf6cctl *mfc, int mrtsock)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001378{
Patrick McHardyf30a77842010-05-11 14:40:51 +02001379 bool found = false;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001380 int line;
Patrick McHardyf30a77842010-05-11 14:40:51 +02001381 struct mfc6_cache *uc, *c;
Rami Rosen6ac7eb02008-04-10 12:40:10 +03001382 unsigned char ttls[MAXMIFS];
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001383 int i;
1384
Patrick McHardya50436f22010-03-17 06:04:14 +00001385 if (mfc->mf6cc_parent >= MAXMIFS)
1386 return -ENFILE;
1387
Rami Rosen6ac7eb02008-04-10 12:40:10 +03001388 memset(ttls, 255, MAXMIFS);
1389 for (i = 0; i < MAXMIFS; i++) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001390 if (IF_ISSET(i, &mfc->mf6cc_ifset))
1391 ttls[i] = 1;
1392
1393 }
1394
1395 line = MFC6_HASH(&mfc->mf6cc_mcastgrp.sin6_addr, &mfc->mf6cc_origin.sin6_addr);
1396
Patrick McHardy6bd52142010-05-11 14:40:53 +02001397 list_for_each_entry(c, &mrt->mfc6_cache_array[line], list) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001398 if (ipv6_addr_equal(&c->mf6c_origin, &mfc->mf6cc_origin.sin6_addr) &&
Patrick McHardyf30a77842010-05-11 14:40:51 +02001399 ipv6_addr_equal(&c->mf6c_mcastgrp, &mfc->mf6cc_mcastgrp.sin6_addr)) {
1400 found = true;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001401 break;
Patrick McHardyf30a77842010-05-11 14:40:51 +02001402 }
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001403 }
1404
Patrick McHardyf30a77842010-05-11 14:40:51 +02001405 if (found) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001406 write_lock_bh(&mrt_lock);
1407 c->mf6c_parent = mfc->mf6cc_parent;
Patrick McHardy6bd52142010-05-11 14:40:53 +02001408 ip6mr_update_thresholds(mrt, c, ttls);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001409 if (!mrtsock)
1410 c->mfc_flags |= MFC_STATIC;
1411 write_unlock_bh(&mrt_lock);
1412 return 0;
1413 }
1414
1415 if (!ipv6_addr_is_multicast(&mfc->mf6cc_mcastgrp.sin6_addr))
1416 return -EINVAL;
1417
Patrick McHardyb5aa30b2010-05-11 14:40:50 +02001418 c = ip6mr_cache_alloc();
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001419 if (c == NULL)
1420 return -ENOMEM;
1421
1422 c->mf6c_origin = mfc->mf6cc_origin.sin6_addr;
1423 c->mf6c_mcastgrp = mfc->mf6cc_mcastgrp.sin6_addr;
1424 c->mf6c_parent = mfc->mf6cc_parent;
Patrick McHardy6bd52142010-05-11 14:40:53 +02001425 ip6mr_update_thresholds(mrt, c, ttls);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001426 if (!mrtsock)
1427 c->mfc_flags |= MFC_STATIC;
1428
1429 write_lock_bh(&mrt_lock);
Patrick McHardy6bd52142010-05-11 14:40:53 +02001430 list_add(&c->list, &mrt->mfc6_cache_array[line]);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001431 write_unlock_bh(&mrt_lock);
1432
1433 /*
1434 * Check to see if we resolved a queued list. If so we
1435 * need to send on the frames and tidy up.
1436 */
Patrick McHardyf30a77842010-05-11 14:40:51 +02001437 found = false;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001438 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy6bd52142010-05-11 14:40:53 +02001439 list_for_each_entry(uc, &mrt->mfc6_unres_queue, list) {
Patrick McHardyc476efb2010-05-11 14:40:48 +02001440 if (ipv6_addr_equal(&uc->mf6c_origin, &c->mf6c_origin) &&
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001441 ipv6_addr_equal(&uc->mf6c_mcastgrp, &c->mf6c_mcastgrp)) {
Patrick McHardyf30a77842010-05-11 14:40:51 +02001442 list_del(&uc->list);
Patrick McHardy6bd52142010-05-11 14:40:53 +02001443 atomic_dec(&mrt->cache_resolve_queue_len);
Patrick McHardyf30a77842010-05-11 14:40:51 +02001444 found = true;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001445 break;
1446 }
1447 }
Patrick McHardy6bd52142010-05-11 14:40:53 +02001448 if (list_empty(&mrt->mfc6_unres_queue))
1449 del_timer(&mrt->ipmr_expire_timer);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001450 spin_unlock_bh(&mfc_unres_lock);
1451
Patrick McHardyf30a77842010-05-11 14:40:51 +02001452 if (found) {
Patrick McHardy6bd52142010-05-11 14:40:53 +02001453 ip6mr_cache_resolve(net, mrt, uc, c);
Benjamin Thery58701ad2008-12-10 16:22:34 -08001454 ip6mr_cache_free(uc);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001455 }
1456 return 0;
1457}
1458
1459/*
1460 * Close the multicast socket, and clear the vif tables etc
1461 */
1462
Patrick McHardy6bd52142010-05-11 14:40:53 +02001463static void mroute_clean_tables(struct mr6_table *mrt)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001464{
1465 int i;
Eric Dumazetc871e662009-10-28 04:48:11 +00001466 LIST_HEAD(list);
Patrick McHardyf30a77842010-05-11 14:40:51 +02001467 struct mfc6_cache *c, *next;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001468
1469 /*
1470 * Shut down all active vif entries
1471 */
Patrick McHardy6bd52142010-05-11 14:40:53 +02001472 for (i = 0; i < mrt->maxvif; i++) {
1473 if (!(mrt->vif6_table[i].flags & VIFF_STATIC))
1474 mif6_delete(mrt, i, &list);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001475 }
Eric Dumazetc871e662009-10-28 04:48:11 +00001476 unregister_netdevice_many(&list);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001477
1478 /*
1479 * Wipe the cache
1480 */
Benjamin Thery4a6258a2008-12-10 16:24:07 -08001481 for (i = 0; i < MFC6_LINES; i++) {
Patrick McHardy6bd52142010-05-11 14:40:53 +02001482 list_for_each_entry_safe(c, next, &mrt->mfc6_cache_array[i], list) {
Patrick McHardyf30a77842010-05-11 14:40:51 +02001483 if (c->mfc_flags & MFC_STATIC)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001484 continue;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001485 write_lock_bh(&mrt_lock);
Patrick McHardyf30a77842010-05-11 14:40:51 +02001486 list_del(&c->list);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001487 write_unlock_bh(&mrt_lock);
1488
Benjamin Thery58701ad2008-12-10 16:22:34 -08001489 ip6mr_cache_free(c);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001490 }
1491 }
1492
Patrick McHardy6bd52142010-05-11 14:40:53 +02001493 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001494 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy6bd52142010-05-11 14:40:53 +02001495 list_for_each_entry_safe(c, next, &mrt->mfc6_unres_queue, list) {
Patrick McHardyf30a77842010-05-11 14:40:51 +02001496 list_del(&c->list);
Patrick McHardy6bd52142010-05-11 14:40:53 +02001497 ip6mr_destroy_unres(mrt, c);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001498 }
1499 spin_unlock_bh(&mfc_unres_lock);
1500 }
1501}
1502
Patrick McHardy6bd52142010-05-11 14:40:53 +02001503static int ip6mr_sk_init(struct mr6_table *mrt, struct sock *sk)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001504{
1505 int err = 0;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001506 struct net *net = sock_net(sk);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001507
1508 rtnl_lock();
1509 write_lock_bh(&mrt_lock);
Patrick McHardy6bd52142010-05-11 14:40:53 +02001510 if (likely(mrt->mroute6_sk == NULL)) {
1511 mrt->mroute6_sk = sk;
Thomas Goff1d6e55f2009-01-27 22:39:59 -08001512 net->ipv6.devconf_all->mc_forwarding++;
1513 }
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001514 else
1515 err = -EADDRINUSE;
1516 write_unlock_bh(&mrt_lock);
1517
1518 rtnl_unlock();
1519
1520 return err;
1521}
1522
1523int ip6mr_sk_done(struct sock *sk)
1524{
Patrick McHardyd1db2752010-05-11 14:40:55 +02001525 int err = -EACCES;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001526 struct net *net = sock_net(sk);
Patrick McHardyd1db2752010-05-11 14:40:55 +02001527 struct mr6_table *mrt;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001528
1529 rtnl_lock();
Patrick McHardyd1db2752010-05-11 14:40:55 +02001530 ip6mr_for_each_table(mrt, net) {
1531 if (sk == mrt->mroute6_sk) {
1532 write_lock_bh(&mrt_lock);
1533 mrt->mroute6_sk = NULL;
1534 net->ipv6.devconf_all->mc_forwarding--;
1535 write_unlock_bh(&mrt_lock);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001536
Patrick McHardyd1db2752010-05-11 14:40:55 +02001537 mroute_clean_tables(mrt);
1538 err = 0;
1539 break;
1540 }
1541 }
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001542 rtnl_unlock();
1543
1544 return err;
1545}
1546
Patrick McHardyd1db2752010-05-11 14:40:55 +02001547struct sock *mroute6_socket(struct net *net, struct sk_buff *skb)
Patrick McHardy6bd52142010-05-11 14:40:53 +02001548{
Patrick McHardyd1db2752010-05-11 14:40:55 +02001549 struct mr6_table *mrt;
1550 struct flowi fl = {
1551 .iif = skb->skb_iif,
1552 .oif = skb->dev->ifindex,
1553 .mark = skb->mark,
1554 };
1555
1556 if (ip6mr_fib_lookup(net, &fl, &mrt) < 0)
1557 return NULL;
Patrick McHardy6bd52142010-05-11 14:40:53 +02001558
1559 return mrt->mroute6_sk;
1560}
1561
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001562/*
1563 * Socket options and virtual interface manipulation. The whole
1564 * virtual interface system is a complete heap, but unfortunately
1565 * that's how BSD mrouted happens to think. Maybe one day with a proper
1566 * MOSPF/PIM router set up we can clean this up.
1567 */
1568
David S. Millerb7058842009-09-30 16:12:20 -07001569int ip6_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001570{
1571 int ret;
1572 struct mif6ctl vif;
1573 struct mf6cctl mfc;
1574 mifi_t mifi;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001575 struct net *net = sock_net(sk);
Patrick McHardyd1db2752010-05-11 14:40:55 +02001576 struct mr6_table *mrt;
1577
1578 mrt = ip6mr_get_table(net, raw6_sk(sk)->ip6mr_table ? : RT6_TABLE_DFLT);
1579 if (mrt == NULL)
1580 return -ENOENT;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001581
1582 if (optname != MRT6_INIT) {
Patrick McHardy6bd52142010-05-11 14:40:53 +02001583 if (sk != mrt->mroute6_sk && !capable(CAP_NET_ADMIN))
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001584 return -EACCES;
1585 }
1586
1587 switch (optname) {
1588 case MRT6_INIT:
1589 if (sk->sk_type != SOCK_RAW ||
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001590 inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001591 return -EOPNOTSUPP;
1592 if (optlen < sizeof(int))
1593 return -EINVAL;
1594
Patrick McHardy6bd52142010-05-11 14:40:53 +02001595 return ip6mr_sk_init(mrt, sk);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001596
1597 case MRT6_DONE:
1598 return ip6mr_sk_done(sk);
1599
1600 case MRT6_ADD_MIF:
1601 if (optlen < sizeof(vif))
1602 return -EINVAL;
1603 if (copy_from_user(&vif, optval, sizeof(vif)))
1604 return -EFAULT;
Rami Rosen6ac7eb02008-04-10 12:40:10 +03001605 if (vif.mif6c_mifi >= MAXMIFS)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001606 return -ENFILE;
1607 rtnl_lock();
Patrick McHardy6bd52142010-05-11 14:40:53 +02001608 ret = mif6_add(net, mrt, &vif, sk == mrt->mroute6_sk);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001609 rtnl_unlock();
1610 return ret;
1611
1612 case MRT6_DEL_MIF:
1613 if (optlen < sizeof(mifi_t))
1614 return -EINVAL;
1615 if (copy_from_user(&mifi, optval, sizeof(mifi_t)))
1616 return -EFAULT;
1617 rtnl_lock();
Patrick McHardy6bd52142010-05-11 14:40:53 +02001618 ret = mif6_delete(mrt, mifi, NULL);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001619 rtnl_unlock();
1620 return ret;
1621
1622 /*
1623 * Manipulate the forwarding caches. These live
1624 * in a sort of kernel/user symbiosis.
1625 */
1626 case MRT6_ADD_MFC:
1627 case MRT6_DEL_MFC:
1628 if (optlen < sizeof(mfc))
1629 return -EINVAL;
1630 if (copy_from_user(&mfc, optval, sizeof(mfc)))
1631 return -EFAULT;
1632 rtnl_lock();
1633 if (optname == MRT6_DEL_MFC)
Patrick McHardy6bd52142010-05-11 14:40:53 +02001634 ret = ip6mr_mfc_delete(mrt, &mfc);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001635 else
Patrick McHardy6bd52142010-05-11 14:40:53 +02001636 ret = ip6mr_mfc_add(net, mrt, &mfc, sk == mrt->mroute6_sk);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001637 rtnl_unlock();
1638 return ret;
1639
1640 /*
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001641 * Control PIM assert (to activate pim will activate assert)
1642 */
1643 case MRT6_ASSERT:
1644 {
1645 int v;
1646 if (get_user(v, (int __user *)optval))
1647 return -EFAULT;
Patrick McHardy6bd52142010-05-11 14:40:53 +02001648 mrt->mroute_do_assert = !!v;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001649 return 0;
1650 }
1651
1652#ifdef CONFIG_IPV6_PIMSM_V2
1653 case MRT6_PIM:
1654 {
YOSHIFUJI Hideakia9f83bf2008-04-10 15:41:28 +09001655 int v;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001656 if (get_user(v, (int __user *)optval))
1657 return -EFAULT;
1658 v = !!v;
1659 rtnl_lock();
1660 ret = 0;
Patrick McHardy6bd52142010-05-11 14:40:53 +02001661 if (v != mrt->mroute_do_pim) {
1662 mrt->mroute_do_pim = v;
1663 mrt->mroute_do_assert = v;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001664 }
1665 rtnl_unlock();
1666 return ret;
1667 }
1668
1669#endif
Patrick McHardyd1db2752010-05-11 14:40:55 +02001670#ifdef CONFIG_IPV6_MROUTE_MULTIPLE_TABLES
1671 case MRT6_TABLE:
1672 {
1673 u32 v;
1674
1675 if (optlen != sizeof(u32))
1676 return -EINVAL;
1677 if (get_user(v, (u32 __user *)optval))
1678 return -EFAULT;
1679 if (sk == mrt->mroute6_sk)
1680 return -EBUSY;
1681
1682 rtnl_lock();
1683 ret = 0;
1684 if (!ip6mr_new_table(net, v))
1685 ret = -ENOMEM;
1686 raw6_sk(sk)->ip6mr_table = v;
1687 rtnl_unlock();
1688 return ret;
1689 }
1690#endif
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001691 /*
Rami Rosen7d120c52008-04-23 14:35:13 +03001692 * Spurious command, or MRT6_VERSION which you cannot
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001693 * set.
1694 */
1695 default:
1696 return -ENOPROTOOPT;
1697 }
1698}
1699
1700/*
1701 * Getsock opt support for the multicast routing system.
1702 */
1703
1704int ip6_mroute_getsockopt(struct sock *sk, int optname, char __user *optval,
1705 int __user *optlen)
1706{
1707 int olr;
1708 int val;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001709 struct net *net = sock_net(sk);
Patrick McHardyd1db2752010-05-11 14:40:55 +02001710 struct mr6_table *mrt;
1711
1712 mrt = ip6mr_get_table(net, raw6_sk(sk)->ip6mr_table ? : RT6_TABLE_DFLT);
1713 if (mrt == NULL)
1714 return -ENOENT;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001715
1716 switch (optname) {
1717 case MRT6_VERSION:
1718 val = 0x0305;
1719 break;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001720#ifdef CONFIG_IPV6_PIMSM_V2
1721 case MRT6_PIM:
Patrick McHardy6bd52142010-05-11 14:40:53 +02001722 val = mrt->mroute_do_pim;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001723 break;
1724#endif
1725 case MRT6_ASSERT:
Patrick McHardy6bd52142010-05-11 14:40:53 +02001726 val = mrt->mroute_do_assert;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001727 break;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001728 default:
1729 return -ENOPROTOOPT;
1730 }
1731
1732 if (get_user(olr, optlen))
1733 return -EFAULT;
1734
1735 olr = min_t(int, olr, sizeof(int));
1736 if (olr < 0)
1737 return -EINVAL;
1738
1739 if (put_user(olr, optlen))
1740 return -EFAULT;
1741 if (copy_to_user(optval, &val, olr))
1742 return -EFAULT;
1743 return 0;
1744}
1745
1746/*
1747 * The IP multicast ioctl support routines.
1748 */
1749
1750int ip6mr_ioctl(struct sock *sk, int cmd, void __user *arg)
1751{
1752 struct sioc_sg_req6 sr;
1753 struct sioc_mif_req6 vr;
1754 struct mif_device *vif;
1755 struct mfc6_cache *c;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001756 struct net *net = sock_net(sk);
Patrick McHardyd1db2752010-05-11 14:40:55 +02001757 struct mr6_table *mrt;
1758
1759 mrt = ip6mr_get_table(net, raw6_sk(sk)->ip6mr_table ? : RT6_TABLE_DFLT);
1760 if (mrt == NULL)
1761 return -ENOENT;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001762
1763 switch (cmd) {
1764 case SIOCGETMIFCNT_IN6:
1765 if (copy_from_user(&vr, arg, sizeof(vr)))
1766 return -EFAULT;
Patrick McHardy6bd52142010-05-11 14:40:53 +02001767 if (vr.mifi >= mrt->maxvif)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001768 return -EINVAL;
1769 read_lock(&mrt_lock);
Patrick McHardy6bd52142010-05-11 14:40:53 +02001770 vif = &mrt->vif6_table[vr.mifi];
1771 if (MIF_EXISTS(mrt, vr.mifi)) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001772 vr.icount = vif->pkt_in;
1773 vr.ocount = vif->pkt_out;
1774 vr.ibytes = vif->bytes_in;
1775 vr.obytes = vif->bytes_out;
1776 read_unlock(&mrt_lock);
1777
1778 if (copy_to_user(arg, &vr, sizeof(vr)))
1779 return -EFAULT;
1780 return 0;
1781 }
1782 read_unlock(&mrt_lock);
1783 return -EADDRNOTAVAIL;
1784 case SIOCGETSGCNT_IN6:
1785 if (copy_from_user(&sr, arg, sizeof(sr)))
1786 return -EFAULT;
1787
1788 read_lock(&mrt_lock);
Patrick McHardy6bd52142010-05-11 14:40:53 +02001789 c = ip6mr_cache_find(mrt, &sr.src.sin6_addr, &sr.grp.sin6_addr);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001790 if (c) {
1791 sr.pktcnt = c->mfc_un.res.pkt;
1792 sr.bytecnt = c->mfc_un.res.bytes;
1793 sr.wrong_if = c->mfc_un.res.wrong_if;
1794 read_unlock(&mrt_lock);
1795
1796 if (copy_to_user(arg, &sr, sizeof(sr)))
1797 return -EFAULT;
1798 return 0;
1799 }
1800 read_unlock(&mrt_lock);
1801 return -EADDRNOTAVAIL;
1802 default:
1803 return -ENOIOCTLCMD;
1804 }
1805}
1806
1807
1808static inline int ip6mr_forward2_finish(struct sk_buff *skb)
1809{
Eric Dumazetadf30902009-06-02 05:19:30 +00001810 IP6_INC_STATS_BH(dev_net(skb_dst(skb)->dev), ip6_dst_idev(skb_dst(skb)),
Denis V. Lunev483a47d2008-10-08 11:09:27 -07001811 IPSTATS_MIB_OUTFORWDATAGRAMS);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001812 return dst_output(skb);
1813}
1814
1815/*
1816 * Processing handlers for ip6mr_forward
1817 */
1818
Patrick McHardy6bd52142010-05-11 14:40:53 +02001819static int ip6mr_forward2(struct net *net, struct mr6_table *mrt,
1820 struct sk_buff *skb, struct mfc6_cache *c, int vifi)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001821{
1822 struct ipv6hdr *ipv6h;
Patrick McHardy6bd52142010-05-11 14:40:53 +02001823 struct mif_device *vif = &mrt->vif6_table[vifi];
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001824 struct net_device *dev;
1825 struct dst_entry *dst;
1826 struct flowi fl;
1827
1828 if (vif->dev == NULL)
1829 goto out_free;
1830
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001831#ifdef CONFIG_IPV6_PIMSM_V2
1832 if (vif->flags & MIFF_REGISTER) {
1833 vif->pkt_out++;
1834 vif->bytes_out += skb->len;
Pavel Emelyanovdc58c782008-05-21 14:17:54 -07001835 vif->dev->stats.tx_bytes += skb->len;
1836 vif->dev->stats.tx_packets++;
Patrick McHardy6bd52142010-05-11 14:40:53 +02001837 ip6mr_cache_report(mrt, skb, vifi, MRT6MSG_WHOLEPKT);
Ilpo Järvinen8da73b72008-12-14 23:15:49 -08001838 goto out_free;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001839 }
1840#endif
1841
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001842 ipv6h = ipv6_hdr(skb);
1843
1844 fl = (struct flowi) {
1845 .oif = vif->link,
Changli Gao58116622010-11-12 18:43:55 +00001846 .fl6_dst = ipv6h->daddr,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001847 };
1848
Benjamin Thery8229efd2008-12-10 16:30:15 -08001849 dst = ip6_route_output(net, NULL, &fl);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001850 if (!dst)
1851 goto out_free;
1852
Eric Dumazetadf30902009-06-02 05:19:30 +00001853 skb_dst_drop(skb);
1854 skb_dst_set(skb, dst);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001855
1856 /*
1857 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1858 * not only before forwarding, but after forwarding on all output
1859 * interfaces. It is clear, if mrouter runs a multicasting
1860 * program, it should receive packets not depending to what interface
1861 * program is joined.
1862 * If we will not make it, the program will have to join on all
1863 * interfaces. On the other hand, multihoming host (or router, but
1864 * not mrouter) cannot join to more than one interface - it will
1865 * result in receiving multiple packets.
1866 */
1867 dev = vif->dev;
1868 skb->dev = dev;
1869 vif->pkt_out++;
1870 vif->bytes_out += skb->len;
1871
1872 /* We are about to write */
1873 /* XXX: extension headers? */
1874 if (skb_cow(skb, sizeof(*ipv6h) + LL_RESERVED_SPACE(dev)))
1875 goto out_free;
1876
1877 ipv6h = ipv6_hdr(skb);
1878 ipv6h->hop_limit--;
1879
1880 IP6CB(skb)->flags |= IP6SKB_FORWARDED;
1881
Jan Engelhardtb2e0b382010-03-23 04:09:07 +01001882 return NF_HOOK(NFPROTO_IPV6, NF_INET_FORWARD, skb, skb->dev, dev,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001883 ip6mr_forward2_finish);
1884
1885out_free:
1886 kfree_skb(skb);
1887 return 0;
1888}
1889
Patrick McHardy6bd52142010-05-11 14:40:53 +02001890static int ip6mr_find_vif(struct mr6_table *mrt, struct net_device *dev)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001891{
1892 int ct;
Patrick McHardy6bd52142010-05-11 14:40:53 +02001893
1894 for (ct = mrt->maxvif - 1; ct >= 0; ct--) {
1895 if (mrt->vif6_table[ct].dev == dev)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001896 break;
1897 }
1898 return ct;
1899}
1900
Patrick McHardy6bd52142010-05-11 14:40:53 +02001901static int ip6_mr_forward(struct net *net, struct mr6_table *mrt,
1902 struct sk_buff *skb, struct mfc6_cache *cache)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001903{
1904 int psend = -1;
1905 int vif, ct;
1906
1907 vif = cache->mf6c_parent;
1908 cache->mfc_un.res.pkt++;
1909 cache->mfc_un.res.bytes += skb->len;
1910
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001911 /*
1912 * Wrong interface: drop packet and (maybe) send PIM assert.
1913 */
Patrick McHardy6bd52142010-05-11 14:40:53 +02001914 if (mrt->vif6_table[vif].dev != skb->dev) {
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001915 int true_vifi;
1916
1917 cache->mfc_un.res.wrong_if++;
Patrick McHardy6bd52142010-05-11 14:40:53 +02001918 true_vifi = ip6mr_find_vif(mrt, skb->dev);
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001919
Patrick McHardy6bd52142010-05-11 14:40:53 +02001920 if (true_vifi >= 0 && mrt->mroute_do_assert &&
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001921 /* pimsm uses asserts, when switching from RPT to SPT,
1922 so that we cannot check that packet arrived on an oif.
1923 It is bad, but otherwise we would need to move pretty
1924 large chunk of pimd to kernel. Ough... --ANK
1925 */
Patrick McHardy6bd52142010-05-11 14:40:53 +02001926 (mrt->mroute_do_pim ||
Benjamin Therya21f3f92008-12-10 16:28:44 -08001927 cache->mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001928 time_after(jiffies,
1929 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1930 cache->mfc_un.res.last_assert = jiffies;
Patrick McHardy6bd52142010-05-11 14:40:53 +02001931 ip6mr_cache_report(mrt, skb, true_vifi, MRT6MSG_WRONGMIF);
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001932 }
1933 goto dont_forward;
1934 }
1935
Patrick McHardy6bd52142010-05-11 14:40:53 +02001936 mrt->vif6_table[vif].pkt_in++;
1937 mrt->vif6_table[vif].bytes_in += skb->len;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001938
1939 /*
1940 * Forward the frame
1941 */
1942 for (ct = cache->mfc_un.res.maxvif - 1; ct >= cache->mfc_un.res.minvif; ct--) {
1943 if (ipv6_hdr(skb)->hop_limit > cache->mfc_un.res.ttls[ct]) {
1944 if (psend != -1) {
1945 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1946 if (skb2)
Patrick McHardy6bd52142010-05-11 14:40:53 +02001947 ip6mr_forward2(net, mrt, skb2, cache, psend);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001948 }
1949 psend = ct;
1950 }
1951 }
1952 if (psend != -1) {
Patrick McHardy6bd52142010-05-11 14:40:53 +02001953 ip6mr_forward2(net, mrt, skb, cache, psend);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001954 return 0;
1955 }
1956
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001957dont_forward:
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001958 kfree_skb(skb);
1959 return 0;
1960}
1961
1962
1963/*
1964 * Multicast packets for forwarding arrive here
1965 */
1966
1967int ip6_mr_input(struct sk_buff *skb)
1968{
1969 struct mfc6_cache *cache;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001970 struct net *net = dev_net(skb->dev);
Patrick McHardyd1db2752010-05-11 14:40:55 +02001971 struct mr6_table *mrt;
1972 struct flowi fl = {
1973 .iif = skb->dev->ifindex,
1974 .mark = skb->mark,
1975 };
1976 int err;
1977
1978 err = ip6mr_fib_lookup(net, &fl, &mrt);
1979 if (err < 0)
1980 return err;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001981
1982 read_lock(&mrt_lock);
Patrick McHardy6bd52142010-05-11 14:40:53 +02001983 cache = ip6mr_cache_find(mrt,
Benjamin Thery8229efd2008-12-10 16:30:15 -08001984 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001985
1986 /*
1987 * No usable cache entry
1988 */
1989 if (cache == NULL) {
1990 int vif;
1991
Patrick McHardy6bd52142010-05-11 14:40:53 +02001992 vif = ip6mr_find_vif(mrt, skb->dev);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001993 if (vif >= 0) {
Patrick McHardy6bd52142010-05-11 14:40:53 +02001994 int err = ip6mr_cache_unresolved(mrt, vif, skb);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001995 read_unlock(&mrt_lock);
1996
1997 return err;
1998 }
1999 read_unlock(&mrt_lock);
2000 kfree_skb(skb);
2001 return -ENODEV;
2002 }
2003
Patrick McHardy6bd52142010-05-11 14:40:53 +02002004 ip6_mr_forward(net, mrt, skb, cache);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002005
2006 read_unlock(&mrt_lock);
2007
2008 return 0;
2009}
2010
2011
Patrick McHardy5b285ca2010-05-11 14:40:56 +02002012static int __ip6mr_fill_mroute(struct mr6_table *mrt, struct sk_buff *skb,
2013 struct mfc6_cache *c, struct rtmsg *rtm)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002014{
2015 int ct;
2016 struct rtnexthop *nhp;
YOSHIFUJI Hideaki549e0282008-04-05 22:17:39 +09002017 u8 *b = skb_tail_pointer(skb);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002018 struct rtattr *mp_head;
2019
Nicolas Dichtel74381892010-03-25 23:45:35 +00002020 /* If cache is unresolved, don't try to parse IIF and OIF */
Dan Carpentered0f160a2010-05-26 00:38:56 -07002021 if (c->mf6c_parent >= MAXMIFS)
Nicolas Dichtel74381892010-03-25 23:45:35 +00002022 return -ENOENT;
2023
Patrick McHardy6bd52142010-05-11 14:40:53 +02002024 if (MIF_EXISTS(mrt, c->mf6c_parent))
2025 RTA_PUT(skb, RTA_IIF, 4, &mrt->vif6_table[c->mf6c_parent].dev->ifindex);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002026
2027 mp_head = (struct rtattr *)skb_put(skb, RTA_LENGTH(0));
2028
2029 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
Patrick McHardy6bd52142010-05-11 14:40:53 +02002030 if (MIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002031 if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
2032 goto rtattr_failure;
2033 nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
2034 nhp->rtnh_flags = 0;
2035 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
Patrick McHardy6bd52142010-05-11 14:40:53 +02002036 nhp->rtnh_ifindex = mrt->vif6_table[ct].dev->ifindex;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002037 nhp->rtnh_len = sizeof(*nhp);
2038 }
2039 }
2040 mp_head->rta_type = RTA_MULTIPATH;
YOSHIFUJI Hideaki549e0282008-04-05 22:17:39 +09002041 mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002042 rtm->rtm_type = RTN_MULTICAST;
2043 return 1;
2044
2045rtattr_failure:
2046 nlmsg_trim(skb, b);
2047 return -EMSGSIZE;
2048}
2049
Benjamin Thery8229efd2008-12-10 16:30:15 -08002050int ip6mr_get_route(struct net *net,
2051 struct sk_buff *skb, struct rtmsg *rtm, int nowait)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002052{
2053 int err;
Patrick McHardyd1db2752010-05-11 14:40:55 +02002054 struct mr6_table *mrt;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002055 struct mfc6_cache *cache;
Eric Dumazetadf30902009-06-02 05:19:30 +00002056 struct rt6_info *rt = (struct rt6_info *)skb_dst(skb);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002057
Patrick McHardyd1db2752010-05-11 14:40:55 +02002058 mrt = ip6mr_get_table(net, RT6_TABLE_DFLT);
2059 if (mrt == NULL)
2060 return -ENOENT;
2061
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002062 read_lock(&mrt_lock);
Patrick McHardy6bd52142010-05-11 14:40:53 +02002063 cache = ip6mr_cache_find(mrt, &rt->rt6i_src.addr, &rt->rt6i_dst.addr);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002064
2065 if (!cache) {
2066 struct sk_buff *skb2;
2067 struct ipv6hdr *iph;
2068 struct net_device *dev;
2069 int vif;
2070
2071 if (nowait) {
2072 read_unlock(&mrt_lock);
2073 return -EAGAIN;
2074 }
2075
2076 dev = skb->dev;
Patrick McHardy6bd52142010-05-11 14:40:53 +02002077 if (dev == NULL || (vif = ip6mr_find_vif(mrt, dev)) < 0) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002078 read_unlock(&mrt_lock);
2079 return -ENODEV;
2080 }
2081
2082 /* really correct? */
2083 skb2 = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
2084 if (!skb2) {
2085 read_unlock(&mrt_lock);
2086 return -ENOMEM;
2087 }
2088
2089 skb_reset_transport_header(skb2);
2090
2091 skb_put(skb2, sizeof(struct ipv6hdr));
2092 skb_reset_network_header(skb2);
2093
2094 iph = ipv6_hdr(skb2);
2095 iph->version = 0;
2096 iph->priority = 0;
2097 iph->flow_lbl[0] = 0;
2098 iph->flow_lbl[1] = 0;
2099 iph->flow_lbl[2] = 0;
2100 iph->payload_len = 0;
2101 iph->nexthdr = IPPROTO_NONE;
2102 iph->hop_limit = 0;
2103 ipv6_addr_copy(&iph->saddr, &rt->rt6i_src.addr);
2104 ipv6_addr_copy(&iph->daddr, &rt->rt6i_dst.addr);
2105
Patrick McHardy6bd52142010-05-11 14:40:53 +02002106 err = ip6mr_cache_unresolved(mrt, vif, skb2);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002107 read_unlock(&mrt_lock);
2108
2109 return err;
2110 }
2111
2112 if (!nowait && (rtm->rtm_flags&RTM_F_NOTIFY))
2113 cache->mfc_flags |= MFC_NOTIFY;
2114
Patrick McHardy5b285ca2010-05-11 14:40:56 +02002115 err = __ip6mr_fill_mroute(mrt, skb, cache, rtm);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002116 read_unlock(&mrt_lock);
2117 return err;
2118}
2119
Patrick McHardy5b285ca2010-05-11 14:40:56 +02002120static int ip6mr_fill_mroute(struct mr6_table *mrt, struct sk_buff *skb,
2121 u32 pid, u32 seq, struct mfc6_cache *c)
2122{
2123 struct nlmsghdr *nlh;
2124 struct rtmsg *rtm;
2125
2126 nlh = nlmsg_put(skb, pid, seq, RTM_NEWROUTE, sizeof(*rtm), NLM_F_MULTI);
2127 if (nlh == NULL)
2128 return -EMSGSIZE;
2129
2130 rtm = nlmsg_data(nlh);
2131 rtm->rtm_family = RTNL_FAMILY_IPMR;
2132 rtm->rtm_dst_len = 128;
2133 rtm->rtm_src_len = 128;
2134 rtm->rtm_tos = 0;
2135 rtm->rtm_table = mrt->id;
2136 NLA_PUT_U32(skb, RTA_TABLE, mrt->id);
2137 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2138 rtm->rtm_protocol = RTPROT_UNSPEC;
2139 rtm->rtm_flags = 0;
2140
2141 NLA_PUT(skb, RTA_SRC, 16, &c->mf6c_origin);
2142 NLA_PUT(skb, RTA_DST, 16, &c->mf6c_mcastgrp);
2143
2144 if (__ip6mr_fill_mroute(mrt, skb, c, rtm) < 0)
2145 goto nla_put_failure;
2146
2147 return nlmsg_end(skb, nlh);
2148
2149nla_put_failure:
2150 nlmsg_cancel(skb, nlh);
2151 return -EMSGSIZE;
2152}
2153
2154static int ip6mr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2155{
2156 struct net *net = sock_net(skb->sk);
2157 struct mr6_table *mrt;
2158 struct mfc6_cache *mfc;
2159 unsigned int t = 0, s_t;
2160 unsigned int h = 0, s_h;
2161 unsigned int e = 0, s_e;
2162
2163 s_t = cb->args[0];
2164 s_h = cb->args[1];
2165 s_e = cb->args[2];
2166
2167 read_lock(&mrt_lock);
2168 ip6mr_for_each_table(mrt, net) {
2169 if (t < s_t)
2170 goto next_table;
2171 if (t > s_t)
2172 s_h = 0;
2173 for (h = s_h; h < MFC6_LINES; h++) {
2174 list_for_each_entry(mfc, &mrt->mfc6_cache_array[h], list) {
2175 if (e < s_e)
2176 goto next_entry;
2177 if (ip6mr_fill_mroute(mrt, skb,
2178 NETLINK_CB(cb->skb).pid,
2179 cb->nlh->nlmsg_seq,
2180 mfc) < 0)
2181 goto done;
2182next_entry:
2183 e++;
2184 }
2185 e = s_e = 0;
2186 }
2187 s_h = 0;
2188next_table:
2189 t++;
2190 }
2191done:
2192 read_unlock(&mrt_lock);
2193
2194 cb->args[2] = e;
2195 cb->args[1] = h;
2196 cb->args[0] = t;
2197
2198 return skb->len;
2199}