blob: c2920a1a6db38732fff6be3690aae2c9fe64cf0d [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 McHardy6bd52142010-05-11 14:40:53 +0200115static int ip6mr_fill_mroute(struct mr6_table *mrt, struct sk_buff *skb,
Patrick McHardyb5aa30b2010-05-11 14:40:50 +0200116 struct mfc6_cache *c, struct rtmsg *rtm);
Patrick McHardy6bd52142010-05-11 14:40:53 +0200117static void mroute_clean_tables(struct mr6_table *mrt);
Patrick McHardyd1db2752010-05-11 14:40:55 +0200118static void ipmr_expire_process(unsigned long arg);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900119
Patrick McHardyd1db2752010-05-11 14:40:55 +0200120#ifdef CONFIG_IPV6_MROUTE_MULTIPLE_TABLES
121#define ip6mr_for_each_table(mrt, met) \
122 list_for_each_entry_rcu(mrt, &net->ipv6.mr6_tables, list)
123
124static struct mr6_table *ip6mr_get_table(struct net *net, u32 id)
125{
126 struct mr6_table *mrt;
127
128 ip6mr_for_each_table(mrt, net) {
129 if (mrt->id == id)
130 return mrt;
131 }
132 return NULL;
133}
134
135static int ip6mr_fib_lookup(struct net *net, struct flowi *flp,
136 struct mr6_table **mrt)
137{
138 struct ip6mr_result res;
139 struct fib_lookup_arg arg = { .result = &res, };
140 int err;
141
142 err = fib_rules_lookup(net->ipv6.mr6_rules_ops, flp, 0, &arg);
143 if (err < 0)
144 return err;
145 *mrt = res.mrt;
146 return 0;
147}
148
149static int ip6mr_rule_action(struct fib_rule *rule, struct flowi *flp,
150 int flags, struct fib_lookup_arg *arg)
151{
152 struct ip6mr_result *res = arg->result;
153 struct mr6_table *mrt;
154
155 switch (rule->action) {
156 case FR_ACT_TO_TBL:
157 break;
158 case FR_ACT_UNREACHABLE:
159 return -ENETUNREACH;
160 case FR_ACT_PROHIBIT:
161 return -EACCES;
162 case FR_ACT_BLACKHOLE:
163 default:
164 return -EINVAL;
165 }
166
167 mrt = ip6mr_get_table(rule->fr_net, rule->table);
168 if (mrt == NULL)
169 return -EAGAIN;
170 res->mrt = mrt;
171 return 0;
172}
173
174static int ip6mr_rule_match(struct fib_rule *rule, struct flowi *flp, int flags)
175{
176 return 1;
177}
178
179static const struct nla_policy ip6mr_rule_policy[FRA_MAX + 1] = {
180 FRA_GENERIC_POLICY,
181};
182
183static int ip6mr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
184 struct fib_rule_hdr *frh, struct nlattr **tb)
185{
186 return 0;
187}
188
189static int ip6mr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
190 struct nlattr **tb)
191{
192 return 1;
193}
194
195static int ip6mr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
196 struct fib_rule_hdr *frh)
197{
198 frh->dst_len = 0;
199 frh->src_len = 0;
200 frh->tos = 0;
201 return 0;
202}
203
204static const struct fib_rules_ops __net_initdata ip6mr_rules_ops_template = {
205 .family = RTNL_FAMILY_IP6MR,
206 .rule_size = sizeof(struct ip6mr_rule),
207 .addr_size = sizeof(struct in6_addr),
208 .action = ip6mr_rule_action,
209 .match = ip6mr_rule_match,
210 .configure = ip6mr_rule_configure,
211 .compare = ip6mr_rule_compare,
212 .default_pref = fib_default_rule_pref,
213 .fill = ip6mr_rule_fill,
214 .nlgroup = RTNLGRP_IPV6_RULE,
215 .policy = ip6mr_rule_policy,
216 .owner = THIS_MODULE,
217};
218
219static int __net_init ip6mr_rules_init(struct net *net)
220{
221 struct fib_rules_ops *ops;
222 struct mr6_table *mrt;
223 int err;
224
225 ops = fib_rules_register(&ip6mr_rules_ops_template, net);
226 if (IS_ERR(ops))
227 return PTR_ERR(ops);
228
229 INIT_LIST_HEAD(&net->ipv6.mr6_tables);
230
231 mrt = ip6mr_new_table(net, RT6_TABLE_DFLT);
232 if (mrt == NULL) {
233 err = -ENOMEM;
234 goto err1;
235 }
236
237 err = fib_default_rule_add(ops, 0x7fff, RT6_TABLE_DFLT, 0);
238 if (err < 0)
239 goto err2;
240
241 net->ipv6.mr6_rules_ops = ops;
242 return 0;
243
244err2:
245 kfree(mrt);
246err1:
247 fib_rules_unregister(ops);
248 return err;
249}
250
251static void __net_exit ip6mr_rules_exit(struct net *net)
252{
253 struct mr6_table *mrt, *next;
254
255 list_for_each_entry_safe(mrt, next, &net->ipv6.mr6_tables, list)
256 ip6mr_free_table(mrt);
257 fib_rules_unregister(net->ipv6.mr6_rules_ops);
258}
259#else
260#define ip6mr_for_each_table(mrt, net) \
261 for (mrt = net->ipv6.mrt6; mrt; mrt = NULL)
262
263static struct mr6_table *ip6mr_get_table(struct net *net, u32 id)
264{
265 return net->ipv6.mrt6;
266}
267
268static int ip6mr_fib_lookup(struct net *net, struct flowi *flp,
269 struct mr6_table **mrt)
270{
271 *mrt = net->ipv6.mrt6;
272 return 0;
273}
274
275static int __net_init ip6mr_rules_init(struct net *net)
276{
277 net->ipv6.mrt6 = ip6mr_new_table(net, RT6_TABLE_DFLT);
278 return net->ipv6.mrt6 ? 0 : -ENOMEM;
279}
280
281static void __net_exit ip6mr_rules_exit(struct net *net)
282{
283 ip6mr_free_table(net->ipv6.mrt6);
284}
285#endif
286
287static struct mr6_table *ip6mr_new_table(struct net *net, u32 id)
288{
289 struct mr6_table *mrt;
290 unsigned int i;
291
292 mrt = ip6mr_get_table(net, id);
293 if (mrt != NULL)
294 return mrt;
295
296 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
297 if (mrt == NULL)
298 return NULL;
299 mrt->id = id;
300 write_pnet(&mrt->net, net);
301
302 /* Forwarding cache */
303 for (i = 0; i < MFC6_LINES; i++)
304 INIT_LIST_HEAD(&mrt->mfc6_cache_array[i]);
305
306 INIT_LIST_HEAD(&mrt->mfc6_unres_queue);
307
308 setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
309 (unsigned long)mrt);
310
311#ifdef CONFIG_IPV6_PIMSM_V2
312 mrt->mroute_reg_vif_num = -1;
313#endif
314#ifdef CONFIG_IPV6_MROUTE_MULTIPLE_TABLES
315 list_add_tail_rcu(&mrt->list, &net->ipv6.mr6_tables);
316#endif
317 return mrt;
318}
319
320static void ip6mr_free_table(struct mr6_table *mrt)
321{
322 del_timer(&mrt->ipmr_expire_timer);
323 mroute_clean_tables(mrt);
324 kfree(mrt);
325}
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900326
327#ifdef CONFIG_PROC_FS
328
329struct ipmr_mfc_iter {
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800330 struct seq_net_private p;
Patrick McHardyd1db2752010-05-11 14:40:55 +0200331 struct mr6_table *mrt;
Patrick McHardyf30a77842010-05-11 14:40:51 +0200332 struct list_head *cache;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900333 int ct;
334};
335
336
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800337static struct mfc6_cache *ipmr_mfc_seq_idx(struct net *net,
338 struct ipmr_mfc_iter *it, loff_t pos)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900339{
Patrick McHardyd1db2752010-05-11 14:40:55 +0200340 struct mr6_table *mrt = it->mrt;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900341 struct mfc6_cache *mfc;
342
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900343 read_lock(&mrt_lock);
Patrick McHardyf30a77842010-05-11 14:40:51 +0200344 for (it->ct = 0; it->ct < MFC6_LINES; it->ct++) {
Patrick McHardy6bd52142010-05-11 14:40:53 +0200345 it->cache = &mrt->mfc6_cache_array[it->ct];
Patrick McHardyf30a77842010-05-11 14:40:51 +0200346 list_for_each_entry(mfc, it->cache, list)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900347 if (pos-- == 0)
348 return mfc;
Patrick McHardyf30a77842010-05-11 14:40:51 +0200349 }
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900350 read_unlock(&mrt_lock);
351
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900352 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy6bd52142010-05-11 14:40:53 +0200353 it->cache = &mrt->mfc6_unres_queue;
Patrick McHardyf30a77842010-05-11 14:40:51 +0200354 list_for_each_entry(mfc, it->cache, list)
Patrick McHardyc476efb2010-05-11 14:40:48 +0200355 if (pos-- == 0)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900356 return mfc;
357 spin_unlock_bh(&mfc_unres_lock);
358
359 it->cache = NULL;
360 return NULL;
361}
362
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900363/*
364 * The /proc interfaces to multicast routing /proc/ip6_mr_cache /proc/ip6_mr_vif
365 */
366
367struct ipmr_vif_iter {
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800368 struct seq_net_private p;
Patrick McHardyd1db2752010-05-11 14:40:55 +0200369 struct mr6_table *mrt;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900370 int ct;
371};
372
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800373static struct mif_device *ip6mr_vif_seq_idx(struct net *net,
374 struct ipmr_vif_iter *iter,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900375 loff_t pos)
376{
Patrick McHardyd1db2752010-05-11 14:40:55 +0200377 struct mr6_table *mrt = iter->mrt;
Patrick McHardy6bd52142010-05-11 14:40:53 +0200378
379 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
380 if (!MIF_EXISTS(mrt, iter->ct))
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900381 continue;
382 if (pos-- == 0)
Patrick McHardy6bd52142010-05-11 14:40:53 +0200383 return &mrt->vif6_table[iter->ct];
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900384 }
385 return NULL;
386}
387
388static void *ip6mr_vif_seq_start(struct seq_file *seq, loff_t *pos)
389 __acquires(mrt_lock)
390{
Patrick McHardyd1db2752010-05-11 14:40:55 +0200391 struct ipmr_vif_iter *iter = seq->private;
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800392 struct net *net = seq_file_net(seq);
Patrick McHardyd1db2752010-05-11 14:40:55 +0200393 struct mr6_table *mrt;
394
395 mrt = ip6mr_get_table(net, RT6_TABLE_DFLT);
396 if (mrt == NULL)
397 return ERR_PTR(-ENOENT);
398
399 iter->mrt = mrt;
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800400
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900401 read_lock(&mrt_lock);
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800402 return *pos ? ip6mr_vif_seq_idx(net, seq->private, *pos - 1)
403 : SEQ_START_TOKEN;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900404}
405
406static void *ip6mr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
407{
408 struct ipmr_vif_iter *iter = seq->private;
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800409 struct net *net = seq_file_net(seq);
Patrick McHardyd1db2752010-05-11 14:40:55 +0200410 struct mr6_table *mrt = iter->mrt;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900411
412 ++*pos;
413 if (v == SEQ_START_TOKEN)
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800414 return ip6mr_vif_seq_idx(net, iter, 0);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900415
Patrick McHardy6bd52142010-05-11 14:40:53 +0200416 while (++iter->ct < mrt->maxvif) {
417 if (!MIF_EXISTS(mrt, iter->ct))
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900418 continue;
Patrick McHardy6bd52142010-05-11 14:40:53 +0200419 return &mrt->vif6_table[iter->ct];
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900420 }
421 return NULL;
422}
423
424static void ip6mr_vif_seq_stop(struct seq_file *seq, void *v)
425 __releases(mrt_lock)
426{
427 read_unlock(&mrt_lock);
428}
429
430static int ip6mr_vif_seq_show(struct seq_file *seq, void *v)
431{
Patrick McHardyd1db2752010-05-11 14:40:55 +0200432 struct ipmr_vif_iter *iter = seq->private;
433 struct mr6_table *mrt = iter->mrt;
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800434
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900435 if (v == SEQ_START_TOKEN) {
436 seq_puts(seq,
437 "Interface BytesIn PktsIn BytesOut PktsOut Flags\n");
438 } else {
439 const struct mif_device *vif = v;
440 const char *name = vif->dev ? vif->dev->name : "none";
441
442 seq_printf(seq,
Al Virod430a222008-06-02 10:59:02 +0100443 "%2td %-10s %8ld %7ld %8ld %7ld %05X\n",
Patrick McHardy6bd52142010-05-11 14:40:53 +0200444 vif - mrt->vif6_table,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900445 name, vif->bytes_in, vif->pkt_in,
446 vif->bytes_out, vif->pkt_out,
447 vif->flags);
448 }
449 return 0;
450}
451
Stephen Hemminger98147d52009-09-01 19:25:02 +0000452static const struct seq_operations ip6mr_vif_seq_ops = {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900453 .start = ip6mr_vif_seq_start,
454 .next = ip6mr_vif_seq_next,
455 .stop = ip6mr_vif_seq_stop,
456 .show = ip6mr_vif_seq_show,
457};
458
459static int ip6mr_vif_open(struct inode *inode, struct file *file)
460{
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800461 return seq_open_net(inode, file, &ip6mr_vif_seq_ops,
462 sizeof(struct ipmr_vif_iter));
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900463}
464
Stephen Hemminger5ca1b992009-09-01 19:25:05 +0000465static const struct file_operations ip6mr_vif_fops = {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900466 .owner = THIS_MODULE,
467 .open = ip6mr_vif_open,
468 .read = seq_read,
469 .llseek = seq_lseek,
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800470 .release = seq_release_net,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900471};
472
473static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
474{
Patrick McHardyd1db2752010-05-11 14:40:55 +0200475 struct ipmr_mfc_iter *it = seq->private;
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800476 struct net *net = seq_file_net(seq);
Patrick McHardyd1db2752010-05-11 14:40:55 +0200477 struct mr6_table *mrt;
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800478
Patrick McHardyd1db2752010-05-11 14:40:55 +0200479 mrt = ip6mr_get_table(net, RT6_TABLE_DFLT);
480 if (mrt == NULL)
481 return ERR_PTR(-ENOENT);
482
483 it->mrt = mrt;
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800484 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
485 : SEQ_START_TOKEN;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900486}
487
488static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
489{
490 struct mfc6_cache *mfc = v;
491 struct ipmr_mfc_iter *it = seq->private;
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800492 struct net *net = seq_file_net(seq);
Patrick McHardyd1db2752010-05-11 14:40:55 +0200493 struct mr6_table *mrt = it->mrt;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900494
495 ++*pos;
496
497 if (v == SEQ_START_TOKEN)
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800498 return ipmr_mfc_seq_idx(net, seq->private, 0);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900499
Patrick McHardyf30a77842010-05-11 14:40:51 +0200500 if (mfc->list.next != it->cache)
501 return list_entry(mfc->list.next, struct mfc6_cache, list);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900502
Patrick McHardy6bd52142010-05-11 14:40:53 +0200503 if (it->cache == &mrt->mfc6_unres_queue)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900504 goto end_of_list;
505
Patrick McHardy6bd52142010-05-11 14:40:53 +0200506 BUG_ON(it->cache != &mrt->mfc6_cache_array[it->ct]);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900507
Benjamin Thery4a6258a2008-12-10 16:24:07 -0800508 while (++it->ct < MFC6_LINES) {
Patrick McHardy6bd52142010-05-11 14:40:53 +0200509 it->cache = &mrt->mfc6_cache_array[it->ct];
Patrick McHardyf30a77842010-05-11 14:40:51 +0200510 if (list_empty(it->cache))
511 continue;
512 return list_first_entry(it->cache, struct mfc6_cache, list);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900513 }
514
515 /* exhausted cache_array, show unresolved */
516 read_unlock(&mrt_lock);
Patrick McHardy6bd52142010-05-11 14:40:53 +0200517 it->cache = &mrt->mfc6_unres_queue;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900518 it->ct = 0;
519
520 spin_lock_bh(&mfc_unres_lock);
Patrick McHardyf30a77842010-05-11 14:40:51 +0200521 if (!list_empty(it->cache))
522 return list_first_entry(it->cache, struct mfc6_cache, list);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900523
524 end_of_list:
525 spin_unlock_bh(&mfc_unres_lock);
526 it->cache = NULL;
527
528 return NULL;
529}
530
531static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
532{
533 struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyd1db2752010-05-11 14:40:55 +0200534 struct mr6_table *mrt = it->mrt;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900535
Patrick McHardy6bd52142010-05-11 14:40:53 +0200536 if (it->cache == &mrt->mfc6_unres_queue)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900537 spin_unlock_bh(&mfc_unres_lock);
Patrick McHardy6bd52142010-05-11 14:40:53 +0200538 else if (it->cache == mrt->mfc6_cache_array)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900539 read_unlock(&mrt_lock);
540}
541
542static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
543{
544 int n;
545
546 if (v == SEQ_START_TOKEN) {
547 seq_puts(seq,
548 "Group "
549 "Origin "
550 "Iif Pkts Bytes Wrong Oifs\n");
551 } else {
552 const struct mfc6_cache *mfc = v;
553 const struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyd1db2752010-05-11 14:40:55 +0200554 struct mr6_table *mrt = it->mrt;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900555
Benjamin Thery999890b2008-12-03 22:22:16 -0800556 seq_printf(seq, "%pI6 %pI6 %-3hd",
Harvey Harrison0c6ce782008-10-28 16:09:23 -0700557 &mfc->mf6c_mcastgrp, &mfc->mf6c_origin,
Benjamin Thery1ea472e2008-12-03 22:21:47 -0800558 mfc->mf6c_parent);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900559
Patrick McHardy6bd52142010-05-11 14:40:53 +0200560 if (it->cache != &mrt->mfc6_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -0800561 seq_printf(seq, " %8lu %8lu %8lu",
562 mfc->mfc_un.res.pkt,
563 mfc->mfc_un.res.bytes,
564 mfc->mfc_un.res.wrong_if);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900565 for (n = mfc->mfc_un.res.minvif;
566 n < mfc->mfc_un.res.maxvif; n++) {
Patrick McHardy6bd52142010-05-11 14:40:53 +0200567 if (MIF_EXISTS(mrt, n) &&
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900568 mfc->mfc_un.res.ttls[n] < 255)
569 seq_printf(seq,
570 " %2d:%-3d",
571 n, mfc->mfc_un.res.ttls[n]);
572 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -0800573 } else {
574 /* unresolved mfc_caches don't contain
575 * pkt, bytes and wrong_if values
576 */
577 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900578 }
579 seq_putc(seq, '\n');
580 }
581 return 0;
582}
583
James Morris88e9d342009-09-22 16:43:43 -0700584static const struct seq_operations ipmr_mfc_seq_ops = {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900585 .start = ipmr_mfc_seq_start,
586 .next = ipmr_mfc_seq_next,
587 .stop = ipmr_mfc_seq_stop,
588 .show = ipmr_mfc_seq_show,
589};
590
591static int ipmr_mfc_open(struct inode *inode, struct file *file)
592{
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800593 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
594 sizeof(struct ipmr_mfc_iter));
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900595}
596
Stephen Hemminger5ca1b992009-09-01 19:25:05 +0000597static const struct file_operations ip6mr_mfc_fops = {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900598 .owner = THIS_MODULE,
599 .open = ipmr_mfc_open,
600 .read = seq_read,
601 .llseek = seq_lseek,
Benjamin Thery8b90fc72008-12-10 16:29:48 -0800602 .release = seq_release_net,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900603};
604#endif
605
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900606#ifdef CONFIG_IPV6_PIMSM_V2
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900607
608static int pim6_rcv(struct sk_buff *skb)
609{
610 struct pimreghdr *pim;
611 struct ipv6hdr *encap;
612 struct net_device *reg_dev = NULL;
Benjamin Thery8229efd2008-12-10 16:30:15 -0800613 struct net *net = dev_net(skb->dev);
Patrick McHardyd1db2752010-05-11 14:40:55 +0200614 struct mr6_table *mrt;
615 struct flowi fl = {
616 .iif = skb->dev->ifindex,
617 .mark = skb->mark,
618 };
619 int reg_vif_num;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900620
621 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(*encap)))
622 goto drop;
623
624 pim = (struct pimreghdr *)skb_transport_header(skb);
625 if (pim->type != ((PIM_VERSION << 4) | PIM_REGISTER) ||
626 (pim->flags & PIM_NULL_REGISTER) ||
Thomas Goff1d6e55f2009-01-27 22:39:59 -0800627 (csum_ipv6_magic(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
628 sizeof(*pim), IPPROTO_PIM,
629 csum_partial((void *)pim, sizeof(*pim), 0)) &&
Al Viroec6b4862008-04-26 22:28:58 -0700630 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900631 goto drop;
632
633 /* check if the inner packet is destined to mcast group */
634 encap = (struct ipv6hdr *)(skb_transport_header(skb) +
635 sizeof(*pim));
636
637 if (!ipv6_addr_is_multicast(&encap->daddr) ||
638 encap->payload_len == 0 ||
639 ntohs(encap->payload_len) + sizeof(*pim) > skb->len)
640 goto drop;
641
Patrick McHardyd1db2752010-05-11 14:40:55 +0200642 if (ip6mr_fib_lookup(net, &fl, &mrt) < 0)
643 goto drop;
644 reg_vif_num = mrt->mroute_reg_vif_num;
645
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900646 read_lock(&mrt_lock);
647 if (reg_vif_num >= 0)
Patrick McHardy6bd52142010-05-11 14:40:53 +0200648 reg_dev = mrt->vif6_table[reg_vif_num].dev;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900649 if (reg_dev)
650 dev_hold(reg_dev);
651 read_unlock(&mrt_lock);
652
653 if (reg_dev == NULL)
654 goto drop;
655
656 skb->mac_header = skb->network_header;
657 skb_pull(skb, (u8 *)encap - skb->data);
658 skb_reset_network_header(skb);
659 skb->dev = reg_dev;
Thomas Goff1d6e55f2009-01-27 22:39:59 -0800660 skb->protocol = htons(ETH_P_IPV6);
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900661 skb->ip_summed = 0;
662 skb->pkt_type = PACKET_HOST;
Eric Dumazetadf30902009-06-02 05:19:30 +0000663 skb_dst_drop(skb);
Pavel Emelyanovdc58c782008-05-21 14:17:54 -0700664 reg_dev->stats.rx_bytes += skb->len;
665 reg_dev->stats.rx_packets++;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900666 nf_reset(skb);
667 netif_rx(skb);
668 dev_put(reg_dev);
669 return 0;
670 drop:
671 kfree_skb(skb);
672 return 0;
673}
674
Alexey Dobriyan41135cc2009-09-14 12:22:28 +0000675static const struct inet6_protocol pim6_protocol = {
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900676 .handler = pim6_rcv,
677};
678
679/* Service routines creating virtual interfaces: PIMREG */
680
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000681static netdev_tx_t reg_vif_xmit(struct sk_buff *skb,
682 struct net_device *dev)
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900683{
Benjamin Thery8229efd2008-12-10 16:30:15 -0800684 struct net *net = dev_net(dev);
Patrick McHardyd1db2752010-05-11 14:40:55 +0200685 struct mr6_table *mrt;
686 struct flowi fl = {
687 .oif = dev->ifindex,
688 .iif = skb->skb_iif,
689 .mark = skb->mark,
690 };
691 int err;
692
693 err = ip6mr_fib_lookup(net, &fl, &mrt);
694 if (err < 0)
695 return err;
Benjamin Thery8229efd2008-12-10 16:30:15 -0800696
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900697 read_lock(&mrt_lock);
Pavel Emelyanovdc58c782008-05-21 14:17:54 -0700698 dev->stats.tx_bytes += skb->len;
699 dev->stats.tx_packets++;
Patrick McHardy6bd52142010-05-11 14:40:53 +0200700 ip6mr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, MRT6MSG_WHOLEPKT);
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900701 read_unlock(&mrt_lock);
702 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000703 return NETDEV_TX_OK;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900704}
705
Stephen Hemminger007c3832008-11-20 20:28:35 -0800706static const struct net_device_ops reg_vif_netdev_ops = {
707 .ndo_start_xmit = reg_vif_xmit,
708};
709
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900710static void reg_vif_setup(struct net_device *dev)
711{
712 dev->type = ARPHRD_PIMREG;
713 dev->mtu = 1500 - sizeof(struct ipv6hdr) - 8;
714 dev->flags = IFF_NOARP;
Stephen Hemminger007c3832008-11-20 20:28:35 -0800715 dev->netdev_ops = &reg_vif_netdev_ops;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900716 dev->destructor = free_netdev;
Tom Goff403dbb92009-06-14 03:16:13 -0700717 dev->features |= NETIF_F_NETNS_LOCAL;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900718}
719
Patrick McHardyd1db2752010-05-11 14:40:55 +0200720static struct net_device *ip6mr_reg_vif(struct net *net, struct mr6_table *mrt)
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900721{
722 struct net_device *dev;
Patrick McHardyd1db2752010-05-11 14:40:55 +0200723 char name[IFNAMSIZ];
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900724
Patrick McHardyd1db2752010-05-11 14:40:55 +0200725 if (mrt->id == RT6_TABLE_DFLT)
726 sprintf(name, "pim6reg");
727 else
728 sprintf(name, "pim6reg%u", mrt->id);
729
730 dev = alloc_netdev(0, name, reg_vif_setup);
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900731 if (dev == NULL)
732 return NULL;
733
Benjamin Thery8229efd2008-12-10 16:30:15 -0800734 dev_net_set(dev, net);
735
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900736 if (register_netdevice(dev)) {
737 free_netdev(dev);
738 return NULL;
739 }
740 dev->iflink = 0;
741
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900742 if (dev_open(dev))
743 goto failure;
744
Wang Chen7af3db72008-07-14 20:54:54 -0700745 dev_hold(dev);
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900746 return dev;
747
748failure:
749 /* allow the register to be completed before unregistering. */
750 rtnl_unlock();
751 rtnl_lock();
752
753 unregister_netdevice(dev);
754 return NULL;
755}
756#endif
757
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900758/*
759 * Delete a VIF entry
760 */
761
Patrick McHardy6bd52142010-05-11 14:40:53 +0200762static int mif6_delete(struct mr6_table *mrt, int vifi, struct list_head *head)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900763{
764 struct mif_device *v;
765 struct net_device *dev;
Thomas Goff1d6e55f2009-01-27 22:39:59 -0800766 struct inet6_dev *in6_dev;
Patrick McHardy6bd52142010-05-11 14:40:53 +0200767
768 if (vifi < 0 || vifi >= mrt->maxvif)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900769 return -EADDRNOTAVAIL;
770
Patrick McHardy6bd52142010-05-11 14:40:53 +0200771 v = &mrt->vif6_table[vifi];
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900772
773 write_lock_bh(&mrt_lock);
774 dev = v->dev;
775 v->dev = NULL;
776
777 if (!dev) {
778 write_unlock_bh(&mrt_lock);
779 return -EADDRNOTAVAIL;
780 }
781
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900782#ifdef CONFIG_IPV6_PIMSM_V2
Patrick McHardy6bd52142010-05-11 14:40:53 +0200783 if (vifi == mrt->mroute_reg_vif_num)
784 mrt->mroute_reg_vif_num = -1;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900785#endif
786
Patrick McHardy6bd52142010-05-11 14:40:53 +0200787 if (vifi + 1 == mrt->maxvif) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900788 int tmp;
789 for (tmp = vifi - 1; tmp >= 0; tmp--) {
Patrick McHardy6bd52142010-05-11 14:40:53 +0200790 if (MIF_EXISTS(mrt, tmp))
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900791 break;
792 }
Patrick McHardy6bd52142010-05-11 14:40:53 +0200793 mrt->maxvif = tmp + 1;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900794 }
795
796 write_unlock_bh(&mrt_lock);
797
798 dev_set_allmulti(dev, -1);
799
Thomas Goff1d6e55f2009-01-27 22:39:59 -0800800 in6_dev = __in6_dev_get(dev);
801 if (in6_dev)
802 in6_dev->cnf.mc_forwarding--;
803
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900804 if (v->flags & MIFF_REGISTER)
Eric Dumazetc871e662009-10-28 04:48:11 +0000805 unregister_netdevice_queue(dev, head);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900806
807 dev_put(dev);
808 return 0;
809}
810
Benjamin Thery58701ad2008-12-10 16:22:34 -0800811static inline void ip6mr_cache_free(struct mfc6_cache *c)
812{
Benjamin Thery58701ad2008-12-10 16:22:34 -0800813 kmem_cache_free(mrt_cachep, c);
814}
815
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900816/* Destroy an unresolved cache entry, killing queued skbs
817 and reporting error to netlink readers.
818 */
819
Patrick McHardy6bd52142010-05-11 14:40:53 +0200820static void ip6mr_destroy_unres(struct mr6_table *mrt, struct mfc6_cache *c)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900821{
Patrick McHardy6bd52142010-05-11 14:40:53 +0200822 struct net *net = read_pnet(&mrt->net);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900823 struct sk_buff *skb;
824
Patrick McHardy6bd52142010-05-11 14:40:53 +0200825 atomic_dec(&mrt->cache_resolve_queue_len);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900826
827 while((skb = skb_dequeue(&c->mfc_un.unres.unresolved)) != NULL) {
828 if (ipv6_hdr(skb)->version == 0) {
829 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct ipv6hdr));
830 nlh->nlmsg_type = NLMSG_ERROR;
831 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
832 skb_trim(skb, nlh->nlmsg_len);
833 ((struct nlmsgerr *)NLMSG_DATA(nlh))->error = -ETIMEDOUT;
Benjamin Thery8229efd2008-12-10 16:30:15 -0800834 rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900835 } else
836 kfree_skb(skb);
837 }
838
Benjamin Thery58701ad2008-12-10 16:22:34 -0800839 ip6mr_cache_free(c);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900840}
841
842
Patrick McHardyc476efb2010-05-11 14:40:48 +0200843/* Timer process for all the unresolved queue. */
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900844
Patrick McHardy6bd52142010-05-11 14:40:53 +0200845static void ipmr_do_expire_process(struct mr6_table *mrt)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900846{
847 unsigned long now = jiffies;
848 unsigned long expires = 10 * HZ;
Patrick McHardyf30a77842010-05-11 14:40:51 +0200849 struct mfc6_cache *c, *next;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900850
Patrick McHardy6bd52142010-05-11 14:40:53 +0200851 list_for_each_entry_safe(c, next, &mrt->mfc6_unres_queue, list) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900852 if (time_after(c->mfc_un.unres.expires, now)) {
853 /* not yet... */
854 unsigned long interval = c->mfc_un.unres.expires - now;
855 if (interval < expires)
856 expires = interval;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900857 continue;
858 }
859
Patrick McHardyf30a77842010-05-11 14:40:51 +0200860 list_del(&c->list);
Patrick McHardy6bd52142010-05-11 14:40:53 +0200861 ip6mr_destroy_unres(mrt, c);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900862 }
863
Patrick McHardy6bd52142010-05-11 14:40:53 +0200864 if (!list_empty(&mrt->mfc6_unres_queue))
865 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900866}
867
Patrick McHardyc476efb2010-05-11 14:40:48 +0200868static void ipmr_expire_process(unsigned long arg)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900869{
Patrick McHardy6bd52142010-05-11 14:40:53 +0200870 struct mr6_table *mrt = (struct mr6_table *)arg;
Patrick McHardyc476efb2010-05-11 14:40:48 +0200871
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900872 if (!spin_trylock(&mfc_unres_lock)) {
Patrick McHardy6bd52142010-05-11 14:40:53 +0200873 mod_timer(&mrt->ipmr_expire_timer, jiffies + 1);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900874 return;
875 }
876
Patrick McHardy6bd52142010-05-11 14:40:53 +0200877 if (!list_empty(&mrt->mfc6_unres_queue))
878 ipmr_do_expire_process(mrt);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900879
880 spin_unlock(&mfc_unres_lock);
881}
882
883/* Fill oifs list. It is called under write locked mrt_lock. */
884
Patrick McHardy6bd52142010-05-11 14:40:53 +0200885static void ip6mr_update_thresholds(struct mr6_table *mrt, struct mfc6_cache *cache,
Patrick McHardyb5aa30b2010-05-11 14:40:50 +0200886 unsigned char *ttls)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900887{
888 int vifi;
889
Rami Rosen6ac7eb02008-04-10 12:40:10 +0300890 cache->mfc_un.res.minvif = MAXMIFS;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900891 cache->mfc_un.res.maxvif = 0;
Rami Rosen6ac7eb02008-04-10 12:40:10 +0300892 memset(cache->mfc_un.res.ttls, 255, MAXMIFS);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900893
Patrick McHardy6bd52142010-05-11 14:40:53 +0200894 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
895 if (MIF_EXISTS(mrt, vifi) &&
Benjamin Thery4e168802008-12-10 16:15:08 -0800896 ttls[vifi] && ttls[vifi] < 255) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900897 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
898 if (cache->mfc_un.res.minvif > vifi)
899 cache->mfc_un.res.minvif = vifi;
900 if (cache->mfc_un.res.maxvif <= vifi)
901 cache->mfc_un.res.maxvif = vifi + 1;
902 }
903 }
904}
905
Patrick McHardy6bd52142010-05-11 14:40:53 +0200906static int mif6_add(struct net *net, struct mr6_table *mrt,
907 struct mif6ctl *vifc, int mrtsock)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900908{
909 int vifi = vifc->mif6c_mifi;
Patrick McHardy6bd52142010-05-11 14:40:53 +0200910 struct mif_device *v = &mrt->vif6_table[vifi];
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900911 struct net_device *dev;
Thomas Goff1d6e55f2009-01-27 22:39:59 -0800912 struct inet6_dev *in6_dev;
Wang Chen5ae7b442008-07-14 20:54:23 -0700913 int err;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900914
915 /* Is vif busy ? */
Patrick McHardy6bd52142010-05-11 14:40:53 +0200916 if (MIF_EXISTS(mrt, vifi))
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900917 return -EADDRINUSE;
918
919 switch (vifc->mif6c_flags) {
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900920#ifdef CONFIG_IPV6_PIMSM_V2
921 case MIFF_REGISTER:
922 /*
923 * Special Purpose VIF in PIM
924 * All the packets will be sent to the daemon
925 */
Patrick McHardy6bd52142010-05-11 14:40:53 +0200926 if (mrt->mroute_reg_vif_num >= 0)
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900927 return -EADDRINUSE;
Patrick McHardyd1db2752010-05-11 14:40:55 +0200928 dev = ip6mr_reg_vif(net, mrt);
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900929 if (!dev)
930 return -ENOBUFS;
Wang Chen5ae7b442008-07-14 20:54:23 -0700931 err = dev_set_allmulti(dev, 1);
932 if (err) {
933 unregister_netdevice(dev);
Wang Chen7af3db72008-07-14 20:54:54 -0700934 dev_put(dev);
Wang Chen5ae7b442008-07-14 20:54:23 -0700935 return err;
936 }
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900937 break;
938#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900939 case 0:
Benjamin Thery8229efd2008-12-10 16:30:15 -0800940 dev = dev_get_by_index(net, vifc->mif6c_pifi);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900941 if (!dev)
942 return -EADDRNOTAVAIL;
Wang Chen5ae7b442008-07-14 20:54:23 -0700943 err = dev_set_allmulti(dev, 1);
Wang Chen7af3db72008-07-14 20:54:54 -0700944 if (err) {
945 dev_put(dev);
Wang Chen5ae7b442008-07-14 20:54:23 -0700946 return err;
Wang Chen7af3db72008-07-14 20:54:54 -0700947 }
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900948 break;
949 default:
950 return -EINVAL;
951 }
952
Thomas Goff1d6e55f2009-01-27 22:39:59 -0800953 in6_dev = __in6_dev_get(dev);
954 if (in6_dev)
955 in6_dev->cnf.mc_forwarding++;
956
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900957 /*
958 * Fill in the VIF structures
959 */
960 v->rate_limit = vifc->vifc_rate_limit;
961 v->flags = vifc->mif6c_flags;
962 if (!mrtsock)
963 v->flags |= VIFF_STATIC;
964 v->threshold = vifc->vifc_threshold;
965 v->bytes_in = 0;
966 v->bytes_out = 0;
967 v->pkt_in = 0;
968 v->pkt_out = 0;
969 v->link = dev->ifindex;
970 if (v->flags & MIFF_REGISTER)
971 v->link = dev->iflink;
972
973 /* And finish update writing critical data */
974 write_lock_bh(&mrt_lock);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900975 v->dev = dev;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900976#ifdef CONFIG_IPV6_PIMSM_V2
977 if (v->flags & MIFF_REGISTER)
Patrick McHardy6bd52142010-05-11 14:40:53 +0200978 mrt->mroute_reg_vif_num = vifi;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900979#endif
Patrick McHardy6bd52142010-05-11 14:40:53 +0200980 if (vifi + 1 > mrt->maxvif)
981 mrt->maxvif = vifi + 1;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900982 write_unlock_bh(&mrt_lock);
983 return 0;
984}
985
Patrick McHardy6bd52142010-05-11 14:40:53 +0200986static struct mfc6_cache *ip6mr_cache_find(struct mr6_table *mrt,
Benjamin Thery8229efd2008-12-10 16:30:15 -0800987 struct in6_addr *origin,
988 struct in6_addr *mcastgrp)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900989{
990 int line = MFC6_HASH(mcastgrp, origin);
991 struct mfc6_cache *c;
992
Patrick McHardy6bd52142010-05-11 14:40:53 +0200993 list_for_each_entry(c, &mrt->mfc6_cache_array[line], list) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900994 if (ipv6_addr_equal(&c->mf6c_origin, origin) &&
995 ipv6_addr_equal(&c->mf6c_mcastgrp, mcastgrp))
Patrick McHardyf30a77842010-05-11 14:40:51 +0200996 return c;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900997 }
Patrick McHardyf30a77842010-05-11 14:40:51 +0200998 return NULL;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900999}
1000
1001/*
1002 * Allocate a multicast cache entry
1003 */
Patrick McHardyb5aa30b2010-05-11 14:40:50 +02001004static struct mfc6_cache *ip6mr_cache_alloc(void)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001005{
Joe Perches36cbac52008-12-03 22:27:25 -08001006 struct mfc6_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001007 if (c == NULL)
1008 return NULL;
Rami Rosen6ac7eb02008-04-10 12:40:10 +03001009 c->mfc_un.res.minvif = MAXMIFS;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001010 return c;
1011}
1012
Patrick McHardyb5aa30b2010-05-11 14:40:50 +02001013static struct mfc6_cache *ip6mr_cache_alloc_unres(void)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001014{
Joe Perches36cbac52008-12-03 22:27:25 -08001015 struct mfc6_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001016 if (c == NULL)
1017 return NULL;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001018 skb_queue_head_init(&c->mfc_un.unres.unresolved);
1019 c->mfc_un.unres.expires = jiffies + 10 * HZ;
1020 return c;
1021}
1022
1023/*
1024 * A cache entry has gone into a resolved state from queued
1025 */
1026
Patrick McHardy6bd52142010-05-11 14:40:53 +02001027static void ip6mr_cache_resolve(struct net *net, struct mr6_table *mrt,
1028 struct mfc6_cache *uc, struct mfc6_cache *c)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001029{
1030 struct sk_buff *skb;
1031
1032 /*
1033 * Play the pending entries through our router
1034 */
1035
1036 while((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
1037 if (ipv6_hdr(skb)->version == 0) {
1038 int err;
1039 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct ipv6hdr));
1040
Patrick McHardy6bd52142010-05-11 14:40:53 +02001041 if (ip6mr_fill_mroute(mrt, skb, c, NLMSG_DATA(nlh)) > 0) {
YOSHIFUJI Hideaki549e0282008-04-05 22:17:39 +09001042 nlh->nlmsg_len = skb_tail_pointer(skb) - (u8 *)nlh;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001043 } else {
1044 nlh->nlmsg_type = NLMSG_ERROR;
1045 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
1046 skb_trim(skb, nlh->nlmsg_len);
1047 ((struct nlmsgerr *)NLMSG_DATA(nlh))->error = -EMSGSIZE;
1048 }
Patrick McHardyb5aa30b2010-05-11 14:40:50 +02001049 err = rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001050 } else
Patrick McHardy6bd52142010-05-11 14:40:53 +02001051 ip6_mr_forward(net, mrt, skb, c);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001052 }
1053}
1054
1055/*
1056 * Bounce a cache query up to pim6sd. We could use netlink for this but pim6sd
1057 * expects the following bizarre scheme.
1058 *
1059 * Called under mrt_lock.
1060 */
1061
Patrick McHardy6bd52142010-05-11 14:40:53 +02001062static int ip6mr_cache_report(struct mr6_table *mrt, struct sk_buff *pkt,
1063 mifi_t mifi, int assert)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001064{
1065 struct sk_buff *skb;
1066 struct mrt6msg *msg;
1067 int ret;
1068
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001069#ifdef CONFIG_IPV6_PIMSM_V2
1070 if (assert == MRT6MSG_WHOLEPKT)
1071 skb = skb_realloc_headroom(pkt, -skb_network_offset(pkt)
1072 +sizeof(*msg));
1073 else
1074#endif
1075 skb = alloc_skb(sizeof(struct ipv6hdr) + sizeof(*msg), GFP_ATOMIC);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001076
1077 if (!skb)
1078 return -ENOBUFS;
1079
1080 /* I suppose that internal messages
1081 * do not require checksums */
1082
1083 skb->ip_summed = CHECKSUM_UNNECESSARY;
1084
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001085#ifdef CONFIG_IPV6_PIMSM_V2
1086 if (assert == MRT6MSG_WHOLEPKT) {
1087 /* Ugly, but we have no choice with this interface.
1088 Duplicate old header, fix length etc.
1089 And all this only to mangle msg->im6_msgtype and
1090 to set msg->im6_mbz to "mbz" :-)
1091 */
1092 skb_push(skb, -skb_network_offset(pkt));
1093
1094 skb_push(skb, sizeof(*msg));
1095 skb_reset_transport_header(skb);
1096 msg = (struct mrt6msg *)skb_transport_header(skb);
1097 msg->im6_mbz = 0;
1098 msg->im6_msgtype = MRT6MSG_WHOLEPKT;
Patrick McHardy6bd52142010-05-11 14:40:53 +02001099 msg->im6_mif = mrt->mroute_reg_vif_num;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001100 msg->im6_pad = 0;
1101 ipv6_addr_copy(&msg->im6_src, &ipv6_hdr(pkt)->saddr);
1102 ipv6_addr_copy(&msg->im6_dst, &ipv6_hdr(pkt)->daddr);
1103
1104 skb->ip_summed = CHECKSUM_UNNECESSARY;
1105 } else
1106#endif
1107 {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001108 /*
1109 * Copy the IP header
1110 */
1111
1112 skb_put(skb, sizeof(struct ipv6hdr));
1113 skb_reset_network_header(skb);
1114 skb_copy_to_linear_data(skb, ipv6_hdr(pkt), sizeof(struct ipv6hdr));
1115
1116 /*
1117 * Add our header
1118 */
1119 skb_put(skb, sizeof(*msg));
1120 skb_reset_transport_header(skb);
1121 msg = (struct mrt6msg *)skb_transport_header(skb);
1122
1123 msg->im6_mbz = 0;
1124 msg->im6_msgtype = assert;
Rami Rosen6ac7eb02008-04-10 12:40:10 +03001125 msg->im6_mif = mifi;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001126 msg->im6_pad = 0;
1127 ipv6_addr_copy(&msg->im6_src, &ipv6_hdr(pkt)->saddr);
1128 ipv6_addr_copy(&msg->im6_dst, &ipv6_hdr(pkt)->daddr);
1129
Eric Dumazetadf30902009-06-02 05:19:30 +00001130 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001131 skb->ip_summed = CHECKSUM_UNNECESSARY;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001132 }
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001133
Patrick McHardy6bd52142010-05-11 14:40:53 +02001134 if (mrt->mroute6_sk == NULL) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001135 kfree_skb(skb);
1136 return -EINVAL;
1137 }
1138
1139 /*
1140 * Deliver to user space multicast routing algorithms
1141 */
Patrick McHardy6bd52142010-05-11 14:40:53 +02001142 ret = sock_queue_rcv_skb(mrt->mroute6_sk, skb);
Benjamin Therybd91b8b2008-12-10 16:07:08 -08001143 if (ret < 0) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001144 if (net_ratelimit())
1145 printk(KERN_WARNING "mroute6: pending queue full, dropping entries.\n");
1146 kfree_skb(skb);
1147 }
1148
1149 return ret;
1150}
1151
1152/*
1153 * Queue a packet for resolution. It gets locked cache entry!
1154 */
1155
1156static int
Patrick McHardy6bd52142010-05-11 14:40:53 +02001157ip6mr_cache_unresolved(struct mr6_table *mrt, mifi_t mifi, struct sk_buff *skb)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001158{
Patrick McHardyf30a77842010-05-11 14:40:51 +02001159 bool found = false;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001160 int err;
1161 struct mfc6_cache *c;
1162
1163 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy6bd52142010-05-11 14:40:53 +02001164 list_for_each_entry(c, &mrt->mfc6_unres_queue, list) {
Patrick McHardyc476efb2010-05-11 14:40:48 +02001165 if (ipv6_addr_equal(&c->mf6c_mcastgrp, &ipv6_hdr(skb)->daddr) &&
Patrick McHardyf30a77842010-05-11 14:40:51 +02001166 ipv6_addr_equal(&c->mf6c_origin, &ipv6_hdr(skb)->saddr)) {
1167 found = true;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001168 break;
Patrick McHardyf30a77842010-05-11 14:40:51 +02001169 }
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001170 }
1171
Patrick McHardyf30a77842010-05-11 14:40:51 +02001172 if (!found) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001173 /*
1174 * Create a new entry if allowable
1175 */
1176
Patrick McHardy6bd52142010-05-11 14:40:53 +02001177 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
Patrick McHardyb5aa30b2010-05-11 14:40:50 +02001178 (c = ip6mr_cache_alloc_unres()) == NULL) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001179 spin_unlock_bh(&mfc_unres_lock);
1180
1181 kfree_skb(skb);
1182 return -ENOBUFS;
1183 }
1184
1185 /*
1186 * Fill in the new cache entry
1187 */
1188 c->mf6c_parent = -1;
1189 c->mf6c_origin = ipv6_hdr(skb)->saddr;
1190 c->mf6c_mcastgrp = ipv6_hdr(skb)->daddr;
1191
1192 /*
1193 * Reflect first query at pim6sd
1194 */
Patrick McHardy6bd52142010-05-11 14:40:53 +02001195 err = ip6mr_cache_report(mrt, skb, mifi, MRT6MSG_NOCACHE);
Benjamin Thery8229efd2008-12-10 16:30:15 -08001196 if (err < 0) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001197 /* If the report failed throw the cache entry
1198 out - Brad Parker
1199 */
1200 spin_unlock_bh(&mfc_unres_lock);
1201
Benjamin Thery58701ad2008-12-10 16:22:34 -08001202 ip6mr_cache_free(c);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001203 kfree_skb(skb);
1204 return err;
1205 }
1206
Patrick McHardy6bd52142010-05-11 14:40:53 +02001207 atomic_inc(&mrt->cache_resolve_queue_len);
1208 list_add(&c->list, &mrt->mfc6_unres_queue);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001209
Patrick McHardy6bd52142010-05-11 14:40:53 +02001210 ipmr_do_expire_process(mrt);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001211 }
1212
1213 /*
1214 * See if we can append the packet
1215 */
1216 if (c->mfc_un.unres.unresolved.qlen > 3) {
1217 kfree_skb(skb);
1218 err = -ENOBUFS;
1219 } else {
1220 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
1221 err = 0;
1222 }
1223
1224 spin_unlock_bh(&mfc_unres_lock);
1225 return err;
1226}
1227
1228/*
1229 * MFC6 cache manipulation by user space
1230 */
1231
Patrick McHardy6bd52142010-05-11 14:40:53 +02001232static int ip6mr_mfc_delete(struct mr6_table *mrt, struct mf6cctl *mfc)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001233{
1234 int line;
Patrick McHardyf30a77842010-05-11 14:40:51 +02001235 struct mfc6_cache *c, *next;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001236
1237 line = MFC6_HASH(&mfc->mf6cc_mcastgrp.sin6_addr, &mfc->mf6cc_origin.sin6_addr);
1238
Patrick McHardy6bd52142010-05-11 14:40:53 +02001239 list_for_each_entry_safe(c, next, &mrt->mfc6_cache_array[line], list) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001240 if (ipv6_addr_equal(&c->mf6c_origin, &mfc->mf6cc_origin.sin6_addr) &&
1241 ipv6_addr_equal(&c->mf6c_mcastgrp, &mfc->mf6cc_mcastgrp.sin6_addr)) {
1242 write_lock_bh(&mrt_lock);
Patrick McHardyf30a77842010-05-11 14:40:51 +02001243 list_del(&c->list);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001244 write_unlock_bh(&mrt_lock);
1245
Benjamin Thery58701ad2008-12-10 16:22:34 -08001246 ip6mr_cache_free(c);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001247 return 0;
1248 }
1249 }
1250 return -ENOENT;
1251}
1252
1253static int ip6mr_device_event(struct notifier_block *this,
1254 unsigned long event, void *ptr)
1255{
1256 struct net_device *dev = ptr;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001257 struct net *net = dev_net(dev);
Patrick McHardyd1db2752010-05-11 14:40:55 +02001258 struct mr6_table *mrt;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001259 struct mif_device *v;
1260 int ct;
Eric Dumazetc871e662009-10-28 04:48:11 +00001261 LIST_HEAD(list);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001262
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001263 if (event != NETDEV_UNREGISTER)
1264 return NOTIFY_DONE;
1265
Patrick McHardyd1db2752010-05-11 14:40:55 +02001266 ip6mr_for_each_table(mrt, net) {
1267 v = &mrt->vif6_table[0];
1268 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1269 if (v->dev == dev)
1270 mif6_delete(mrt, ct, &list);
1271 }
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001272 }
Eric Dumazetc871e662009-10-28 04:48:11 +00001273 unregister_netdevice_many(&list);
1274
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001275 return NOTIFY_DONE;
1276}
1277
1278static struct notifier_block ip6_mr_notifier = {
1279 .notifier_call = ip6mr_device_event
1280};
1281
1282/*
1283 * Setup for IP multicast routing
1284 */
1285
Benjamin Thery4e168802008-12-10 16:15:08 -08001286static int __net_init ip6mr_net_init(struct net *net)
1287{
Patrick McHardyd1db2752010-05-11 14:40:55 +02001288 int err;
Patrick McHardyf30a77842010-05-11 14:40:51 +02001289
Patrick McHardyd1db2752010-05-11 14:40:55 +02001290 err = ip6mr_rules_init(net);
1291 if (err < 0)
Benjamin Thery4e168802008-12-10 16:15:08 -08001292 goto fail;
Benjamin Thery8b90fc72008-12-10 16:29:48 -08001293
1294#ifdef CONFIG_PROC_FS
1295 err = -ENOMEM;
1296 if (!proc_net_fops_create(net, "ip6_mr_vif", 0, &ip6mr_vif_fops))
1297 goto proc_vif_fail;
1298 if (!proc_net_fops_create(net, "ip6_mr_cache", 0, &ip6mr_mfc_fops))
1299 goto proc_cache_fail;
1300#endif
Patrick McHardy6bd52142010-05-11 14:40:53 +02001301
Benjamin Thery4a6258a2008-12-10 16:24:07 -08001302 return 0;
1303
Benjamin Thery8b90fc72008-12-10 16:29:48 -08001304#ifdef CONFIG_PROC_FS
1305proc_cache_fail:
1306 proc_net_remove(net, "ip6_mr_vif");
1307proc_vif_fail:
Patrick McHardyd1db2752010-05-11 14:40:55 +02001308 ip6mr_rules_exit(net);
Benjamin Thery8b90fc72008-12-10 16:29:48 -08001309#endif
Benjamin Thery4e168802008-12-10 16:15:08 -08001310fail:
1311 return err;
1312}
1313
1314static void __net_exit ip6mr_net_exit(struct net *net)
1315{
Benjamin Thery8b90fc72008-12-10 16:29:48 -08001316#ifdef CONFIG_PROC_FS
1317 proc_net_remove(net, "ip6_mr_cache");
1318 proc_net_remove(net, "ip6_mr_vif");
1319#endif
Patrick McHardyd1db2752010-05-11 14:40:55 +02001320 ip6mr_rules_exit(net);
Benjamin Thery4e168802008-12-10 16:15:08 -08001321}
1322
1323static struct pernet_operations ip6mr_net_ops = {
1324 .init = ip6mr_net_init,
1325 .exit = ip6mr_net_exit,
1326};
1327
Wang Chen623d1a12008-07-03 12:13:30 +08001328int __init ip6_mr_init(void)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001329{
Wang Chen623d1a12008-07-03 12:13:30 +08001330 int err;
1331
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001332 mrt_cachep = kmem_cache_create("ip6_mrt_cache",
1333 sizeof(struct mfc6_cache),
1334 0, SLAB_HWCACHE_ALIGN,
1335 NULL);
1336 if (!mrt_cachep)
Wang Chen623d1a12008-07-03 12:13:30 +08001337 return -ENOMEM;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001338
Benjamin Thery4e168802008-12-10 16:15:08 -08001339 err = register_pernet_subsys(&ip6mr_net_ops);
1340 if (err)
1341 goto reg_pernet_fail;
1342
Wang Chen623d1a12008-07-03 12:13:30 +08001343 err = register_netdevice_notifier(&ip6_mr_notifier);
1344 if (err)
1345 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07001346#ifdef CONFIG_IPV6_PIMSM_V2
1347 if (inet6_add_protocol(&pim6_protocol, IPPROTO_PIM) < 0) {
1348 printk(KERN_ERR "ip6_mr_init: can't add PIM protocol\n");
1349 err = -EAGAIN;
1350 goto add_proto_fail;
1351 }
1352#endif
Wang Chen623d1a12008-07-03 12:13:30 +08001353 return 0;
Tom Goff403dbb92009-06-14 03:16:13 -07001354#ifdef CONFIG_IPV6_PIMSM_V2
1355add_proto_fail:
1356 unregister_netdevice_notifier(&ip6_mr_notifier);
1357#endif
Benjamin Thery87b30a62008-11-10 16:34:11 -08001358reg_notif_fail:
Benjamin Thery4e168802008-12-10 16:15:08 -08001359 unregister_pernet_subsys(&ip6mr_net_ops);
1360reg_pernet_fail:
Benjamin Thery87b30a62008-11-10 16:34:11 -08001361 kmem_cache_destroy(mrt_cachep);
Wang Chen623d1a12008-07-03 12:13:30 +08001362 return err;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001363}
1364
Wang Chen623d1a12008-07-03 12:13:30 +08001365void ip6_mr_cleanup(void)
1366{
Wang Chen623d1a12008-07-03 12:13:30 +08001367 unregister_netdevice_notifier(&ip6_mr_notifier);
Benjamin Thery4e168802008-12-10 16:15:08 -08001368 unregister_pernet_subsys(&ip6mr_net_ops);
Wang Chen623d1a12008-07-03 12:13:30 +08001369 kmem_cache_destroy(mrt_cachep);
1370}
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001371
Patrick McHardy6bd52142010-05-11 14:40:53 +02001372static int ip6mr_mfc_add(struct net *net, struct mr6_table *mrt,
1373 struct mf6cctl *mfc, int mrtsock)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001374{
Patrick McHardyf30a77842010-05-11 14:40:51 +02001375 bool found = false;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001376 int line;
Patrick McHardyf30a77842010-05-11 14:40:51 +02001377 struct mfc6_cache *uc, *c;
Rami Rosen6ac7eb02008-04-10 12:40:10 +03001378 unsigned char ttls[MAXMIFS];
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001379 int i;
1380
Patrick McHardya50436f22010-03-17 06:04:14 +00001381 if (mfc->mf6cc_parent >= MAXMIFS)
1382 return -ENFILE;
1383
Rami Rosen6ac7eb02008-04-10 12:40:10 +03001384 memset(ttls, 255, MAXMIFS);
1385 for (i = 0; i < MAXMIFS; i++) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001386 if (IF_ISSET(i, &mfc->mf6cc_ifset))
1387 ttls[i] = 1;
1388
1389 }
1390
1391 line = MFC6_HASH(&mfc->mf6cc_mcastgrp.sin6_addr, &mfc->mf6cc_origin.sin6_addr);
1392
Patrick McHardy6bd52142010-05-11 14:40:53 +02001393 list_for_each_entry(c, &mrt->mfc6_cache_array[line], list) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001394 if (ipv6_addr_equal(&c->mf6c_origin, &mfc->mf6cc_origin.sin6_addr) &&
Patrick McHardyf30a77842010-05-11 14:40:51 +02001395 ipv6_addr_equal(&c->mf6c_mcastgrp, &mfc->mf6cc_mcastgrp.sin6_addr)) {
1396 found = true;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001397 break;
Patrick McHardyf30a77842010-05-11 14:40:51 +02001398 }
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001399 }
1400
Patrick McHardyf30a77842010-05-11 14:40:51 +02001401 if (found) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001402 write_lock_bh(&mrt_lock);
1403 c->mf6c_parent = mfc->mf6cc_parent;
Patrick McHardy6bd52142010-05-11 14:40:53 +02001404 ip6mr_update_thresholds(mrt, c, ttls);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001405 if (!mrtsock)
1406 c->mfc_flags |= MFC_STATIC;
1407 write_unlock_bh(&mrt_lock);
1408 return 0;
1409 }
1410
1411 if (!ipv6_addr_is_multicast(&mfc->mf6cc_mcastgrp.sin6_addr))
1412 return -EINVAL;
1413
Patrick McHardyb5aa30b2010-05-11 14:40:50 +02001414 c = ip6mr_cache_alloc();
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001415 if (c == NULL)
1416 return -ENOMEM;
1417
1418 c->mf6c_origin = mfc->mf6cc_origin.sin6_addr;
1419 c->mf6c_mcastgrp = mfc->mf6cc_mcastgrp.sin6_addr;
1420 c->mf6c_parent = mfc->mf6cc_parent;
Patrick McHardy6bd52142010-05-11 14:40:53 +02001421 ip6mr_update_thresholds(mrt, c, ttls);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001422 if (!mrtsock)
1423 c->mfc_flags |= MFC_STATIC;
1424
1425 write_lock_bh(&mrt_lock);
Patrick McHardy6bd52142010-05-11 14:40:53 +02001426 list_add(&c->list, &mrt->mfc6_cache_array[line]);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001427 write_unlock_bh(&mrt_lock);
1428
1429 /*
1430 * Check to see if we resolved a queued list. If so we
1431 * need to send on the frames and tidy up.
1432 */
Patrick McHardyf30a77842010-05-11 14:40:51 +02001433 found = false;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001434 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy6bd52142010-05-11 14:40:53 +02001435 list_for_each_entry(uc, &mrt->mfc6_unres_queue, list) {
Patrick McHardyc476efb2010-05-11 14:40:48 +02001436 if (ipv6_addr_equal(&uc->mf6c_origin, &c->mf6c_origin) &&
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001437 ipv6_addr_equal(&uc->mf6c_mcastgrp, &c->mf6c_mcastgrp)) {
Patrick McHardyf30a77842010-05-11 14:40:51 +02001438 list_del(&uc->list);
Patrick McHardy6bd52142010-05-11 14:40:53 +02001439 atomic_dec(&mrt->cache_resolve_queue_len);
Patrick McHardyf30a77842010-05-11 14:40:51 +02001440 found = true;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001441 break;
1442 }
1443 }
Patrick McHardy6bd52142010-05-11 14:40:53 +02001444 if (list_empty(&mrt->mfc6_unres_queue))
1445 del_timer(&mrt->ipmr_expire_timer);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001446 spin_unlock_bh(&mfc_unres_lock);
1447
Patrick McHardyf30a77842010-05-11 14:40:51 +02001448 if (found) {
Patrick McHardy6bd52142010-05-11 14:40:53 +02001449 ip6mr_cache_resolve(net, mrt, uc, c);
Benjamin Thery58701ad2008-12-10 16:22:34 -08001450 ip6mr_cache_free(uc);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001451 }
1452 return 0;
1453}
1454
1455/*
1456 * Close the multicast socket, and clear the vif tables etc
1457 */
1458
Patrick McHardy6bd52142010-05-11 14:40:53 +02001459static void mroute_clean_tables(struct mr6_table *mrt)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001460{
1461 int i;
Eric Dumazetc871e662009-10-28 04:48:11 +00001462 LIST_HEAD(list);
Patrick McHardyf30a77842010-05-11 14:40:51 +02001463 struct mfc6_cache *c, *next;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001464
1465 /*
1466 * Shut down all active vif entries
1467 */
Patrick McHardy6bd52142010-05-11 14:40:53 +02001468 for (i = 0; i < mrt->maxvif; i++) {
1469 if (!(mrt->vif6_table[i].flags & VIFF_STATIC))
1470 mif6_delete(mrt, i, &list);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001471 }
Eric Dumazetc871e662009-10-28 04:48:11 +00001472 unregister_netdevice_many(&list);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001473
1474 /*
1475 * Wipe the cache
1476 */
Benjamin Thery4a6258a2008-12-10 16:24:07 -08001477 for (i = 0; i < MFC6_LINES; i++) {
Patrick McHardy6bd52142010-05-11 14:40:53 +02001478 list_for_each_entry_safe(c, next, &mrt->mfc6_cache_array[i], list) {
Patrick McHardyf30a77842010-05-11 14:40:51 +02001479 if (c->mfc_flags & MFC_STATIC)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001480 continue;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001481 write_lock_bh(&mrt_lock);
Patrick McHardyf30a77842010-05-11 14:40:51 +02001482 list_del(&c->list);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001483 write_unlock_bh(&mrt_lock);
1484
Benjamin Thery58701ad2008-12-10 16:22:34 -08001485 ip6mr_cache_free(c);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001486 }
1487 }
1488
Patrick McHardy6bd52142010-05-11 14:40:53 +02001489 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001490 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy6bd52142010-05-11 14:40:53 +02001491 list_for_each_entry_safe(c, next, &mrt->mfc6_unres_queue, list) {
Patrick McHardyf30a77842010-05-11 14:40:51 +02001492 list_del(&c->list);
Patrick McHardy6bd52142010-05-11 14:40:53 +02001493 ip6mr_destroy_unres(mrt, c);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001494 }
1495 spin_unlock_bh(&mfc_unres_lock);
1496 }
1497}
1498
Patrick McHardy6bd52142010-05-11 14:40:53 +02001499static int ip6mr_sk_init(struct mr6_table *mrt, struct sock *sk)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001500{
1501 int err = 0;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001502 struct net *net = sock_net(sk);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001503
1504 rtnl_lock();
1505 write_lock_bh(&mrt_lock);
Patrick McHardy6bd52142010-05-11 14:40:53 +02001506 if (likely(mrt->mroute6_sk == NULL)) {
1507 mrt->mroute6_sk = sk;
Thomas Goff1d6e55f2009-01-27 22:39:59 -08001508 net->ipv6.devconf_all->mc_forwarding++;
1509 }
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001510 else
1511 err = -EADDRINUSE;
1512 write_unlock_bh(&mrt_lock);
1513
1514 rtnl_unlock();
1515
1516 return err;
1517}
1518
1519int ip6mr_sk_done(struct sock *sk)
1520{
Patrick McHardyd1db2752010-05-11 14:40:55 +02001521 int err = -EACCES;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001522 struct net *net = sock_net(sk);
Patrick McHardyd1db2752010-05-11 14:40:55 +02001523 struct mr6_table *mrt;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001524
1525 rtnl_lock();
Patrick McHardyd1db2752010-05-11 14:40:55 +02001526 ip6mr_for_each_table(mrt, net) {
1527 if (sk == mrt->mroute6_sk) {
1528 write_lock_bh(&mrt_lock);
1529 mrt->mroute6_sk = NULL;
1530 net->ipv6.devconf_all->mc_forwarding--;
1531 write_unlock_bh(&mrt_lock);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001532
Patrick McHardyd1db2752010-05-11 14:40:55 +02001533 mroute_clean_tables(mrt);
1534 err = 0;
1535 break;
1536 }
1537 }
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001538 rtnl_unlock();
1539
1540 return err;
1541}
1542
Patrick McHardyd1db2752010-05-11 14:40:55 +02001543struct sock *mroute6_socket(struct net *net, struct sk_buff *skb)
Patrick McHardy6bd52142010-05-11 14:40:53 +02001544{
Patrick McHardyd1db2752010-05-11 14:40:55 +02001545 struct mr6_table *mrt;
1546 struct flowi fl = {
1547 .iif = skb->skb_iif,
1548 .oif = skb->dev->ifindex,
1549 .mark = skb->mark,
1550 };
1551
1552 if (ip6mr_fib_lookup(net, &fl, &mrt) < 0)
1553 return NULL;
Patrick McHardy6bd52142010-05-11 14:40:53 +02001554
1555 return mrt->mroute6_sk;
1556}
1557
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001558/*
1559 * Socket options and virtual interface manipulation. The whole
1560 * virtual interface system is a complete heap, but unfortunately
1561 * that's how BSD mrouted happens to think. Maybe one day with a proper
1562 * MOSPF/PIM router set up we can clean this up.
1563 */
1564
David S. Millerb7058842009-09-30 16:12:20 -07001565int ip6_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001566{
1567 int ret;
1568 struct mif6ctl vif;
1569 struct mf6cctl mfc;
1570 mifi_t mifi;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001571 struct net *net = sock_net(sk);
Patrick McHardyd1db2752010-05-11 14:40:55 +02001572 struct mr6_table *mrt;
1573
1574 mrt = ip6mr_get_table(net, raw6_sk(sk)->ip6mr_table ? : RT6_TABLE_DFLT);
1575 if (mrt == NULL)
1576 return -ENOENT;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001577
1578 if (optname != MRT6_INIT) {
Patrick McHardy6bd52142010-05-11 14:40:53 +02001579 if (sk != mrt->mroute6_sk && !capable(CAP_NET_ADMIN))
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001580 return -EACCES;
1581 }
1582
1583 switch (optname) {
1584 case MRT6_INIT:
1585 if (sk->sk_type != SOCK_RAW ||
Eric Dumazetc720c7e82009-10-15 06:30:45 +00001586 inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001587 return -EOPNOTSUPP;
1588 if (optlen < sizeof(int))
1589 return -EINVAL;
1590
Patrick McHardy6bd52142010-05-11 14:40:53 +02001591 return ip6mr_sk_init(mrt, sk);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001592
1593 case MRT6_DONE:
1594 return ip6mr_sk_done(sk);
1595
1596 case MRT6_ADD_MIF:
1597 if (optlen < sizeof(vif))
1598 return -EINVAL;
1599 if (copy_from_user(&vif, optval, sizeof(vif)))
1600 return -EFAULT;
Rami Rosen6ac7eb02008-04-10 12:40:10 +03001601 if (vif.mif6c_mifi >= MAXMIFS)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001602 return -ENFILE;
1603 rtnl_lock();
Patrick McHardy6bd52142010-05-11 14:40:53 +02001604 ret = mif6_add(net, mrt, &vif, sk == mrt->mroute6_sk);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001605 rtnl_unlock();
1606 return ret;
1607
1608 case MRT6_DEL_MIF:
1609 if (optlen < sizeof(mifi_t))
1610 return -EINVAL;
1611 if (copy_from_user(&mifi, optval, sizeof(mifi_t)))
1612 return -EFAULT;
1613 rtnl_lock();
Patrick McHardy6bd52142010-05-11 14:40:53 +02001614 ret = mif6_delete(mrt, mifi, NULL);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001615 rtnl_unlock();
1616 return ret;
1617
1618 /*
1619 * Manipulate the forwarding caches. These live
1620 * in a sort of kernel/user symbiosis.
1621 */
1622 case MRT6_ADD_MFC:
1623 case MRT6_DEL_MFC:
1624 if (optlen < sizeof(mfc))
1625 return -EINVAL;
1626 if (copy_from_user(&mfc, optval, sizeof(mfc)))
1627 return -EFAULT;
1628 rtnl_lock();
1629 if (optname == MRT6_DEL_MFC)
Patrick McHardy6bd52142010-05-11 14:40:53 +02001630 ret = ip6mr_mfc_delete(mrt, &mfc);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001631 else
Patrick McHardy6bd52142010-05-11 14:40:53 +02001632 ret = ip6mr_mfc_add(net, mrt, &mfc, sk == mrt->mroute6_sk);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001633 rtnl_unlock();
1634 return ret;
1635
1636 /*
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001637 * Control PIM assert (to activate pim will activate assert)
1638 */
1639 case MRT6_ASSERT:
1640 {
1641 int v;
1642 if (get_user(v, (int __user *)optval))
1643 return -EFAULT;
Patrick McHardy6bd52142010-05-11 14:40:53 +02001644 mrt->mroute_do_assert = !!v;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001645 return 0;
1646 }
1647
1648#ifdef CONFIG_IPV6_PIMSM_V2
1649 case MRT6_PIM:
1650 {
YOSHIFUJI Hideakia9f83bf2008-04-10 15:41:28 +09001651 int v;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001652 if (get_user(v, (int __user *)optval))
1653 return -EFAULT;
1654 v = !!v;
1655 rtnl_lock();
1656 ret = 0;
Patrick McHardy6bd52142010-05-11 14:40:53 +02001657 if (v != mrt->mroute_do_pim) {
1658 mrt->mroute_do_pim = v;
1659 mrt->mroute_do_assert = v;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001660 }
1661 rtnl_unlock();
1662 return ret;
1663 }
1664
1665#endif
Patrick McHardyd1db2752010-05-11 14:40:55 +02001666#ifdef CONFIG_IPV6_MROUTE_MULTIPLE_TABLES
1667 case MRT6_TABLE:
1668 {
1669 u32 v;
1670
1671 if (optlen != sizeof(u32))
1672 return -EINVAL;
1673 if (get_user(v, (u32 __user *)optval))
1674 return -EFAULT;
1675 if (sk == mrt->mroute6_sk)
1676 return -EBUSY;
1677
1678 rtnl_lock();
1679 ret = 0;
1680 if (!ip6mr_new_table(net, v))
1681 ret = -ENOMEM;
1682 raw6_sk(sk)->ip6mr_table = v;
1683 rtnl_unlock();
1684 return ret;
1685 }
1686#endif
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001687 /*
Rami Rosen7d120c52008-04-23 14:35:13 +03001688 * Spurious command, or MRT6_VERSION which you cannot
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001689 * set.
1690 */
1691 default:
1692 return -ENOPROTOOPT;
1693 }
1694}
1695
1696/*
1697 * Getsock opt support for the multicast routing system.
1698 */
1699
1700int ip6_mroute_getsockopt(struct sock *sk, int optname, char __user *optval,
1701 int __user *optlen)
1702{
1703 int olr;
1704 int val;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001705 struct net *net = sock_net(sk);
Patrick McHardyd1db2752010-05-11 14:40:55 +02001706 struct mr6_table *mrt;
1707
1708 mrt = ip6mr_get_table(net, raw6_sk(sk)->ip6mr_table ? : RT6_TABLE_DFLT);
1709 if (mrt == NULL)
1710 return -ENOENT;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001711
1712 switch (optname) {
1713 case MRT6_VERSION:
1714 val = 0x0305;
1715 break;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001716#ifdef CONFIG_IPV6_PIMSM_V2
1717 case MRT6_PIM:
Patrick McHardy6bd52142010-05-11 14:40:53 +02001718 val = mrt->mroute_do_pim;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001719 break;
1720#endif
1721 case MRT6_ASSERT:
Patrick McHardy6bd52142010-05-11 14:40:53 +02001722 val = mrt->mroute_do_assert;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001723 break;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001724 default:
1725 return -ENOPROTOOPT;
1726 }
1727
1728 if (get_user(olr, optlen))
1729 return -EFAULT;
1730
1731 olr = min_t(int, olr, sizeof(int));
1732 if (olr < 0)
1733 return -EINVAL;
1734
1735 if (put_user(olr, optlen))
1736 return -EFAULT;
1737 if (copy_to_user(optval, &val, olr))
1738 return -EFAULT;
1739 return 0;
1740}
1741
1742/*
1743 * The IP multicast ioctl support routines.
1744 */
1745
1746int ip6mr_ioctl(struct sock *sk, int cmd, void __user *arg)
1747{
1748 struct sioc_sg_req6 sr;
1749 struct sioc_mif_req6 vr;
1750 struct mif_device *vif;
1751 struct mfc6_cache *c;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001752 struct net *net = sock_net(sk);
Patrick McHardyd1db2752010-05-11 14:40:55 +02001753 struct mr6_table *mrt;
1754
1755 mrt = ip6mr_get_table(net, raw6_sk(sk)->ip6mr_table ? : RT6_TABLE_DFLT);
1756 if (mrt == NULL)
1757 return -ENOENT;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001758
1759 switch (cmd) {
1760 case SIOCGETMIFCNT_IN6:
1761 if (copy_from_user(&vr, arg, sizeof(vr)))
1762 return -EFAULT;
Patrick McHardy6bd52142010-05-11 14:40:53 +02001763 if (vr.mifi >= mrt->maxvif)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001764 return -EINVAL;
1765 read_lock(&mrt_lock);
Patrick McHardy6bd52142010-05-11 14:40:53 +02001766 vif = &mrt->vif6_table[vr.mifi];
1767 if (MIF_EXISTS(mrt, vr.mifi)) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001768 vr.icount = vif->pkt_in;
1769 vr.ocount = vif->pkt_out;
1770 vr.ibytes = vif->bytes_in;
1771 vr.obytes = vif->bytes_out;
1772 read_unlock(&mrt_lock);
1773
1774 if (copy_to_user(arg, &vr, sizeof(vr)))
1775 return -EFAULT;
1776 return 0;
1777 }
1778 read_unlock(&mrt_lock);
1779 return -EADDRNOTAVAIL;
1780 case SIOCGETSGCNT_IN6:
1781 if (copy_from_user(&sr, arg, sizeof(sr)))
1782 return -EFAULT;
1783
1784 read_lock(&mrt_lock);
Patrick McHardy6bd52142010-05-11 14:40:53 +02001785 c = ip6mr_cache_find(mrt, &sr.src.sin6_addr, &sr.grp.sin6_addr);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001786 if (c) {
1787 sr.pktcnt = c->mfc_un.res.pkt;
1788 sr.bytecnt = c->mfc_un.res.bytes;
1789 sr.wrong_if = c->mfc_un.res.wrong_if;
1790 read_unlock(&mrt_lock);
1791
1792 if (copy_to_user(arg, &sr, sizeof(sr)))
1793 return -EFAULT;
1794 return 0;
1795 }
1796 read_unlock(&mrt_lock);
1797 return -EADDRNOTAVAIL;
1798 default:
1799 return -ENOIOCTLCMD;
1800 }
1801}
1802
1803
1804static inline int ip6mr_forward2_finish(struct sk_buff *skb)
1805{
Eric Dumazetadf30902009-06-02 05:19:30 +00001806 IP6_INC_STATS_BH(dev_net(skb_dst(skb)->dev), ip6_dst_idev(skb_dst(skb)),
Denis V. Lunev483a47d2008-10-08 11:09:27 -07001807 IPSTATS_MIB_OUTFORWDATAGRAMS);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001808 return dst_output(skb);
1809}
1810
1811/*
1812 * Processing handlers for ip6mr_forward
1813 */
1814
Patrick McHardy6bd52142010-05-11 14:40:53 +02001815static int ip6mr_forward2(struct net *net, struct mr6_table *mrt,
1816 struct sk_buff *skb, struct mfc6_cache *c, int vifi)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001817{
1818 struct ipv6hdr *ipv6h;
Patrick McHardy6bd52142010-05-11 14:40:53 +02001819 struct mif_device *vif = &mrt->vif6_table[vifi];
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001820 struct net_device *dev;
1821 struct dst_entry *dst;
1822 struct flowi fl;
1823
1824 if (vif->dev == NULL)
1825 goto out_free;
1826
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001827#ifdef CONFIG_IPV6_PIMSM_V2
1828 if (vif->flags & MIFF_REGISTER) {
1829 vif->pkt_out++;
1830 vif->bytes_out += skb->len;
Pavel Emelyanovdc58c782008-05-21 14:17:54 -07001831 vif->dev->stats.tx_bytes += skb->len;
1832 vif->dev->stats.tx_packets++;
Patrick McHardy6bd52142010-05-11 14:40:53 +02001833 ip6mr_cache_report(mrt, skb, vifi, MRT6MSG_WHOLEPKT);
Ilpo Järvinen8da73b72008-12-14 23:15:49 -08001834 goto out_free;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001835 }
1836#endif
1837
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001838 ipv6h = ipv6_hdr(skb);
1839
1840 fl = (struct flowi) {
1841 .oif = vif->link,
1842 .nl_u = { .ip6_u =
1843 { .daddr = ipv6h->daddr, }
1844 }
1845 };
1846
Benjamin Thery8229efd2008-12-10 16:30:15 -08001847 dst = ip6_route_output(net, NULL, &fl);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001848 if (!dst)
1849 goto out_free;
1850
Eric Dumazetadf30902009-06-02 05:19:30 +00001851 skb_dst_drop(skb);
1852 skb_dst_set(skb, dst);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001853
1854 /*
1855 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1856 * not only before forwarding, but after forwarding on all output
1857 * interfaces. It is clear, if mrouter runs a multicasting
1858 * program, it should receive packets not depending to what interface
1859 * program is joined.
1860 * If we will not make it, the program will have to join on all
1861 * interfaces. On the other hand, multihoming host (or router, but
1862 * not mrouter) cannot join to more than one interface - it will
1863 * result in receiving multiple packets.
1864 */
1865 dev = vif->dev;
1866 skb->dev = dev;
1867 vif->pkt_out++;
1868 vif->bytes_out += skb->len;
1869
1870 /* We are about to write */
1871 /* XXX: extension headers? */
1872 if (skb_cow(skb, sizeof(*ipv6h) + LL_RESERVED_SPACE(dev)))
1873 goto out_free;
1874
1875 ipv6h = ipv6_hdr(skb);
1876 ipv6h->hop_limit--;
1877
1878 IP6CB(skb)->flags |= IP6SKB_FORWARDED;
1879
Jan Engelhardtb2e0b382010-03-23 04:09:07 +01001880 return NF_HOOK(NFPROTO_IPV6, NF_INET_FORWARD, skb, skb->dev, dev,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001881 ip6mr_forward2_finish);
1882
1883out_free:
1884 kfree_skb(skb);
1885 return 0;
1886}
1887
Patrick McHardy6bd52142010-05-11 14:40:53 +02001888static int ip6mr_find_vif(struct mr6_table *mrt, struct net_device *dev)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001889{
1890 int ct;
Patrick McHardy6bd52142010-05-11 14:40:53 +02001891
1892 for (ct = mrt->maxvif - 1; ct >= 0; ct--) {
1893 if (mrt->vif6_table[ct].dev == dev)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001894 break;
1895 }
1896 return ct;
1897}
1898
Patrick McHardy6bd52142010-05-11 14:40:53 +02001899static int ip6_mr_forward(struct net *net, struct mr6_table *mrt,
1900 struct sk_buff *skb, struct mfc6_cache *cache)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001901{
1902 int psend = -1;
1903 int vif, ct;
1904
1905 vif = cache->mf6c_parent;
1906 cache->mfc_un.res.pkt++;
1907 cache->mfc_un.res.bytes += skb->len;
1908
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001909 /*
1910 * Wrong interface: drop packet and (maybe) send PIM assert.
1911 */
Patrick McHardy6bd52142010-05-11 14:40:53 +02001912 if (mrt->vif6_table[vif].dev != skb->dev) {
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001913 int true_vifi;
1914
1915 cache->mfc_un.res.wrong_if++;
Patrick McHardy6bd52142010-05-11 14:40:53 +02001916 true_vifi = ip6mr_find_vif(mrt, skb->dev);
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001917
Patrick McHardy6bd52142010-05-11 14:40:53 +02001918 if (true_vifi >= 0 && mrt->mroute_do_assert &&
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001919 /* pimsm uses asserts, when switching from RPT to SPT,
1920 so that we cannot check that packet arrived on an oif.
1921 It is bad, but otherwise we would need to move pretty
1922 large chunk of pimd to kernel. Ough... --ANK
1923 */
Patrick McHardy6bd52142010-05-11 14:40:53 +02001924 (mrt->mroute_do_pim ||
Benjamin Therya21f3f92008-12-10 16:28:44 -08001925 cache->mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001926 time_after(jiffies,
1927 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1928 cache->mfc_un.res.last_assert = jiffies;
Patrick McHardy6bd52142010-05-11 14:40:53 +02001929 ip6mr_cache_report(mrt, skb, true_vifi, MRT6MSG_WRONGMIF);
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001930 }
1931 goto dont_forward;
1932 }
1933
Patrick McHardy6bd52142010-05-11 14:40:53 +02001934 mrt->vif6_table[vif].pkt_in++;
1935 mrt->vif6_table[vif].bytes_in += skb->len;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001936
1937 /*
1938 * Forward the frame
1939 */
1940 for (ct = cache->mfc_un.res.maxvif - 1; ct >= cache->mfc_un.res.minvif; ct--) {
1941 if (ipv6_hdr(skb)->hop_limit > cache->mfc_un.res.ttls[ct]) {
1942 if (psend != -1) {
1943 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1944 if (skb2)
Patrick McHardy6bd52142010-05-11 14:40:53 +02001945 ip6mr_forward2(net, mrt, skb2, cache, psend);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001946 }
1947 psend = ct;
1948 }
1949 }
1950 if (psend != -1) {
Patrick McHardy6bd52142010-05-11 14:40:53 +02001951 ip6mr_forward2(net, mrt, skb, cache, psend);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001952 return 0;
1953 }
1954
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001955dont_forward:
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001956 kfree_skb(skb);
1957 return 0;
1958}
1959
1960
1961/*
1962 * Multicast packets for forwarding arrive here
1963 */
1964
1965int ip6_mr_input(struct sk_buff *skb)
1966{
1967 struct mfc6_cache *cache;
Benjamin Thery8229efd2008-12-10 16:30:15 -08001968 struct net *net = dev_net(skb->dev);
Patrick McHardyd1db2752010-05-11 14:40:55 +02001969 struct mr6_table *mrt;
1970 struct flowi fl = {
1971 .iif = skb->dev->ifindex,
1972 .mark = skb->mark,
1973 };
1974 int err;
1975
1976 err = ip6mr_fib_lookup(net, &fl, &mrt);
1977 if (err < 0)
1978 return err;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001979
1980 read_lock(&mrt_lock);
Patrick McHardy6bd52142010-05-11 14:40:53 +02001981 cache = ip6mr_cache_find(mrt,
Benjamin Thery8229efd2008-12-10 16:30:15 -08001982 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001983
1984 /*
1985 * No usable cache entry
1986 */
1987 if (cache == NULL) {
1988 int vif;
1989
Patrick McHardy6bd52142010-05-11 14:40:53 +02001990 vif = ip6mr_find_vif(mrt, skb->dev);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001991 if (vif >= 0) {
Patrick McHardy6bd52142010-05-11 14:40:53 +02001992 int err = ip6mr_cache_unresolved(mrt, vif, skb);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001993 read_unlock(&mrt_lock);
1994
1995 return err;
1996 }
1997 read_unlock(&mrt_lock);
1998 kfree_skb(skb);
1999 return -ENODEV;
2000 }
2001
Patrick McHardy6bd52142010-05-11 14:40:53 +02002002 ip6_mr_forward(net, mrt, skb, cache);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002003
2004 read_unlock(&mrt_lock);
2005
2006 return 0;
2007}
2008
2009
2010static int
Patrick McHardy6bd52142010-05-11 14:40:53 +02002011ip6mr_fill_mroute(struct mr6_table *mrt, struct sk_buff *skb,
2012 struct mfc6_cache *c, struct rtmsg *rtm)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002013{
2014 int ct;
2015 struct rtnexthop *nhp;
YOSHIFUJI Hideaki549e0282008-04-05 22:17:39 +09002016 u8 *b = skb_tail_pointer(skb);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002017 struct rtattr *mp_head;
2018
Nicolas Dichtel74381892010-03-25 23:45:35 +00002019 /* If cache is unresolved, don't try to parse IIF and OIF */
2020 if (c->mf6c_parent > MAXMIFS)
2021 return -ENOENT;
2022
Patrick McHardy6bd52142010-05-11 14:40:53 +02002023 if (MIF_EXISTS(mrt, c->mf6c_parent))
2024 RTA_PUT(skb, RTA_IIF, 4, &mrt->vif6_table[c->mf6c_parent].dev->ifindex);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002025
2026 mp_head = (struct rtattr *)skb_put(skb, RTA_LENGTH(0));
2027
2028 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
Patrick McHardy6bd52142010-05-11 14:40:53 +02002029 if (MIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002030 if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
2031 goto rtattr_failure;
2032 nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
2033 nhp->rtnh_flags = 0;
2034 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
Patrick McHardy6bd52142010-05-11 14:40:53 +02002035 nhp->rtnh_ifindex = mrt->vif6_table[ct].dev->ifindex;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002036 nhp->rtnh_len = sizeof(*nhp);
2037 }
2038 }
2039 mp_head->rta_type = RTA_MULTIPATH;
YOSHIFUJI Hideaki549e0282008-04-05 22:17:39 +09002040 mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002041 rtm->rtm_type = RTN_MULTICAST;
2042 return 1;
2043
2044rtattr_failure:
2045 nlmsg_trim(skb, b);
2046 return -EMSGSIZE;
2047}
2048
Benjamin Thery8229efd2008-12-10 16:30:15 -08002049int ip6mr_get_route(struct net *net,
2050 struct sk_buff *skb, struct rtmsg *rtm, int nowait)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002051{
2052 int err;
Patrick McHardyd1db2752010-05-11 14:40:55 +02002053 struct mr6_table *mrt;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002054 struct mfc6_cache *cache;
Eric Dumazetadf30902009-06-02 05:19:30 +00002055 struct rt6_info *rt = (struct rt6_info *)skb_dst(skb);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002056
Patrick McHardyd1db2752010-05-11 14:40:55 +02002057 mrt = ip6mr_get_table(net, RT6_TABLE_DFLT);
2058 if (mrt == NULL)
2059 return -ENOENT;
2060
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002061 read_lock(&mrt_lock);
Patrick McHardy6bd52142010-05-11 14:40:53 +02002062 cache = ip6mr_cache_find(mrt, &rt->rt6i_src.addr, &rt->rt6i_dst.addr);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002063
2064 if (!cache) {
2065 struct sk_buff *skb2;
2066 struct ipv6hdr *iph;
2067 struct net_device *dev;
2068 int vif;
2069
2070 if (nowait) {
2071 read_unlock(&mrt_lock);
2072 return -EAGAIN;
2073 }
2074
2075 dev = skb->dev;
Patrick McHardy6bd52142010-05-11 14:40:53 +02002076 if (dev == NULL || (vif = ip6mr_find_vif(mrt, dev)) < 0) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002077 read_unlock(&mrt_lock);
2078 return -ENODEV;
2079 }
2080
2081 /* really correct? */
2082 skb2 = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
2083 if (!skb2) {
2084 read_unlock(&mrt_lock);
2085 return -ENOMEM;
2086 }
2087
2088 skb_reset_transport_header(skb2);
2089
2090 skb_put(skb2, sizeof(struct ipv6hdr));
2091 skb_reset_network_header(skb2);
2092
2093 iph = ipv6_hdr(skb2);
2094 iph->version = 0;
2095 iph->priority = 0;
2096 iph->flow_lbl[0] = 0;
2097 iph->flow_lbl[1] = 0;
2098 iph->flow_lbl[2] = 0;
2099 iph->payload_len = 0;
2100 iph->nexthdr = IPPROTO_NONE;
2101 iph->hop_limit = 0;
2102 ipv6_addr_copy(&iph->saddr, &rt->rt6i_src.addr);
2103 ipv6_addr_copy(&iph->daddr, &rt->rt6i_dst.addr);
2104
Patrick McHardy6bd52142010-05-11 14:40:53 +02002105 err = ip6mr_cache_unresolved(mrt, vif, skb2);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002106 read_unlock(&mrt_lock);
2107
2108 return err;
2109 }
2110
2111 if (!nowait && (rtm->rtm_flags&RTM_F_NOTIFY))
2112 cache->mfc_flags |= MFC_NOTIFY;
2113
Patrick McHardy6bd52142010-05-11 14:40:53 +02002114 err = ip6mr_fill_mroute(mrt, skb, cache, rtm);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09002115 read_unlock(&mrt_lock);
2116 return err;
2117}
2118