blob: d1008e6891e7c2de56a89ceb229cbd1ab7c34f31 [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>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090036#include <net/protocol.h>
37#include <linux/skbuff.h>
38#include <net/sock.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090039#include <net/raw.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090040#include <linux/notifier.h>
41#include <linux/if_arp.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090042#include <net/checksum.h>
43#include <net/netlink.h>
44
45#include <net/ipv6.h>
46#include <net/ip6_route.h>
47#include <linux/mroute6.h>
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +090048#include <linux/pim.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090049#include <net/addrconf.h>
50#include <linux/netfilter_ipv6.h>
51
52struct sock *mroute6_socket;
53
54
55/* Big lock, protecting vif table, mrt cache and mroute socket state.
56 Note that the changes are semaphored via rtnl_lock.
57 */
58
59static DEFINE_RWLOCK(mrt_lock);
60
61/*
62 * Multicast router control variables
63 */
64
65static struct mif_device vif6_table[MAXMIFS]; /* Devices */
66static int maxvif;
67
68#define MIF_EXISTS(idx) (vif6_table[idx].dev != NULL)
69
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +090070static int mroute_do_assert; /* Set in PIM assert */
71#ifdef CONFIG_IPV6_PIMSM_V2
72static int mroute_do_pim;
73#else
74#define mroute_do_pim 0
75#endif
76
Rami Rosen6ac7eb02008-04-10 12:40:10 +030077static struct mfc6_cache *mfc6_cache_array[MFC6_LINES]; /* Forwarding cache */
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090078
79static struct mfc6_cache *mfc_unres_queue; /* Queue of unresolved entries */
80static atomic_t cache_resolve_queue_len; /* Size of unresolved */
81
82/* Special spinlock for queue of unresolved entries */
83static DEFINE_SPINLOCK(mfc_unres_lock);
84
85/* We return to original Alan's scheme. Hash table of resolved
86 entries is changed only in process context and protected
87 with weak lock mrt_lock. Queue of unresolved entries is protected
88 with strong spinlock mfc_unres_lock.
89
90 In this case data path is free of exclusive locks at all.
91 */
92
93static struct kmem_cache *mrt_cachep __read_mostly;
94
95static int ip6_mr_forward(struct sk_buff *skb, struct mfc6_cache *cache);
Rami Rosen6ac7eb02008-04-10 12:40:10 +030096static int ip6mr_cache_report(struct sk_buff *pkt, mifi_t mifi, int assert);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090097static int ip6mr_fill_mroute(struct sk_buff *skb, struct mfc6_cache *c, struct rtmsg *rtm);
98
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +090099#ifdef CONFIG_IPV6_PIMSM_V2
100static struct inet6_protocol pim6_protocol;
101#endif
102
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900103static struct timer_list ipmr_expire_timer;
104
105
106#ifdef CONFIG_PROC_FS
107
108struct ipmr_mfc_iter {
109 struct mfc6_cache **cache;
110 int ct;
111};
112
113
114static struct mfc6_cache *ipmr_mfc_seq_idx(struct ipmr_mfc_iter *it, loff_t pos)
115{
116 struct mfc6_cache *mfc;
117
118 it->cache = mfc6_cache_array;
119 read_lock(&mrt_lock);
120 for (it->ct = 0; it->ct < ARRAY_SIZE(mfc6_cache_array); it->ct++)
121 for (mfc = mfc6_cache_array[it->ct]; mfc; mfc = mfc->next)
122 if (pos-- == 0)
123 return mfc;
124 read_unlock(&mrt_lock);
125
126 it->cache = &mfc_unres_queue;
127 spin_lock_bh(&mfc_unres_lock);
128 for (mfc = mfc_unres_queue; mfc; mfc = mfc->next)
129 if (pos-- == 0)
130 return mfc;
131 spin_unlock_bh(&mfc_unres_lock);
132
133 it->cache = NULL;
134 return NULL;
135}
136
137
138
139
140/*
141 * The /proc interfaces to multicast routing /proc/ip6_mr_cache /proc/ip6_mr_vif
142 */
143
144struct ipmr_vif_iter {
145 int ct;
146};
147
148static struct mif_device *ip6mr_vif_seq_idx(struct ipmr_vif_iter *iter,
149 loff_t pos)
150{
151 for (iter->ct = 0; iter->ct < maxvif; ++iter->ct) {
152 if (!MIF_EXISTS(iter->ct))
153 continue;
154 if (pos-- == 0)
155 return &vif6_table[iter->ct];
156 }
157 return NULL;
158}
159
160static void *ip6mr_vif_seq_start(struct seq_file *seq, loff_t *pos)
161 __acquires(mrt_lock)
162{
163 read_lock(&mrt_lock);
164 return (*pos ? ip6mr_vif_seq_idx(seq->private, *pos - 1)
165 : SEQ_START_TOKEN);
166}
167
168static void *ip6mr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
169{
170 struct ipmr_vif_iter *iter = seq->private;
171
172 ++*pos;
173 if (v == SEQ_START_TOKEN)
174 return ip6mr_vif_seq_idx(iter, 0);
175
176 while (++iter->ct < maxvif) {
177 if (!MIF_EXISTS(iter->ct))
178 continue;
179 return &vif6_table[iter->ct];
180 }
181 return NULL;
182}
183
184static void ip6mr_vif_seq_stop(struct seq_file *seq, void *v)
185 __releases(mrt_lock)
186{
187 read_unlock(&mrt_lock);
188}
189
190static int ip6mr_vif_seq_show(struct seq_file *seq, void *v)
191{
192 if (v == SEQ_START_TOKEN) {
193 seq_puts(seq,
194 "Interface BytesIn PktsIn BytesOut PktsOut Flags\n");
195 } else {
196 const struct mif_device *vif = v;
197 const char *name = vif->dev ? vif->dev->name : "none";
198
199 seq_printf(seq,
Al Virod430a222008-06-02 10:59:02 +0100200 "%2td %-10s %8ld %7ld %8ld %7ld %05X\n",
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900201 vif - vif6_table,
202 name, vif->bytes_in, vif->pkt_in,
203 vif->bytes_out, vif->pkt_out,
204 vif->flags);
205 }
206 return 0;
207}
208
209static struct seq_operations ip6mr_vif_seq_ops = {
210 .start = ip6mr_vif_seq_start,
211 .next = ip6mr_vif_seq_next,
212 .stop = ip6mr_vif_seq_stop,
213 .show = ip6mr_vif_seq_show,
214};
215
216static int ip6mr_vif_open(struct inode *inode, struct file *file)
217{
218 return seq_open_private(file, &ip6mr_vif_seq_ops,
219 sizeof(struct ipmr_vif_iter));
220}
221
222static struct file_operations ip6mr_vif_fops = {
223 .owner = THIS_MODULE,
224 .open = ip6mr_vif_open,
225 .read = seq_read,
226 .llseek = seq_lseek,
Benjamin Theryeedd7262008-11-20 04:16:12 -0800227 .release = seq_release_private,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900228};
229
230static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
231{
232 return (*pos ? ipmr_mfc_seq_idx(seq->private, *pos - 1)
233 : SEQ_START_TOKEN);
234}
235
236static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
237{
238 struct mfc6_cache *mfc = v;
239 struct ipmr_mfc_iter *it = seq->private;
240
241 ++*pos;
242
243 if (v == SEQ_START_TOKEN)
244 return ipmr_mfc_seq_idx(seq->private, 0);
245
246 if (mfc->next)
247 return mfc->next;
248
249 if (it->cache == &mfc_unres_queue)
250 goto end_of_list;
251
252 BUG_ON(it->cache != mfc6_cache_array);
253
254 while (++it->ct < ARRAY_SIZE(mfc6_cache_array)) {
255 mfc = mfc6_cache_array[it->ct];
256 if (mfc)
257 return mfc;
258 }
259
260 /* exhausted cache_array, show unresolved */
261 read_unlock(&mrt_lock);
262 it->cache = &mfc_unres_queue;
263 it->ct = 0;
264
265 spin_lock_bh(&mfc_unres_lock);
266 mfc = mfc_unres_queue;
267 if (mfc)
268 return mfc;
269
270 end_of_list:
271 spin_unlock_bh(&mfc_unres_lock);
272 it->cache = NULL;
273
274 return NULL;
275}
276
277static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
278{
279 struct ipmr_mfc_iter *it = seq->private;
280
281 if (it->cache == &mfc_unres_queue)
282 spin_unlock_bh(&mfc_unres_lock);
283 else if (it->cache == mfc6_cache_array)
284 read_unlock(&mrt_lock);
285}
286
287static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
288{
289 int n;
290
291 if (v == SEQ_START_TOKEN) {
292 seq_puts(seq,
293 "Group "
294 "Origin "
295 "Iif Pkts Bytes Wrong Oifs\n");
296 } else {
297 const struct mfc6_cache *mfc = v;
298 const struct ipmr_mfc_iter *it = seq->private;
299
Benjamin Thery999890b2008-12-03 22:22:16 -0800300 seq_printf(seq, "%pI6 %pI6 %-3hd",
Harvey Harrison0c6ce782008-10-28 16:09:23 -0700301 &mfc->mf6c_mcastgrp, &mfc->mf6c_origin,
Benjamin Thery1ea472e2008-12-03 22:21:47 -0800302 mfc->mf6c_parent);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900303
304 if (it->cache != &mfc_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -0800305 seq_printf(seq, " %8lu %8lu %8lu",
306 mfc->mfc_un.res.pkt,
307 mfc->mfc_un.res.bytes,
308 mfc->mfc_un.res.wrong_if);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900309 for (n = mfc->mfc_un.res.minvif;
310 n < mfc->mfc_un.res.maxvif; n++) {
311 if (MIF_EXISTS(n) &&
312 mfc->mfc_un.res.ttls[n] < 255)
313 seq_printf(seq,
314 " %2d:%-3d",
315 n, mfc->mfc_un.res.ttls[n]);
316 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -0800317 } else {
318 /* unresolved mfc_caches don't contain
319 * pkt, bytes and wrong_if values
320 */
321 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900322 }
323 seq_putc(seq, '\n');
324 }
325 return 0;
326}
327
328static struct seq_operations ipmr_mfc_seq_ops = {
329 .start = ipmr_mfc_seq_start,
330 .next = ipmr_mfc_seq_next,
331 .stop = ipmr_mfc_seq_stop,
332 .show = ipmr_mfc_seq_show,
333};
334
335static int ipmr_mfc_open(struct inode *inode, struct file *file)
336{
337 return seq_open_private(file, &ipmr_mfc_seq_ops,
338 sizeof(struct ipmr_mfc_iter));
339}
340
341static struct file_operations ip6mr_mfc_fops = {
342 .owner = THIS_MODULE,
343 .open = ipmr_mfc_open,
344 .read = seq_read,
345 .llseek = seq_lseek,
Benjamin Theryeedd7262008-11-20 04:16:12 -0800346 .release = seq_release_private,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900347};
348#endif
349
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900350#ifdef CONFIG_IPV6_PIMSM_V2
351static int reg_vif_num = -1;
352
353static int pim6_rcv(struct sk_buff *skb)
354{
355 struct pimreghdr *pim;
356 struct ipv6hdr *encap;
357 struct net_device *reg_dev = NULL;
358
359 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(*encap)))
360 goto drop;
361
362 pim = (struct pimreghdr *)skb_transport_header(skb);
363 if (pim->type != ((PIM_VERSION << 4) | PIM_REGISTER) ||
364 (pim->flags & PIM_NULL_REGISTER) ||
365 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
Al Viroec6b4862008-04-26 22:28:58 -0700366 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900367 goto drop;
368
369 /* check if the inner packet is destined to mcast group */
370 encap = (struct ipv6hdr *)(skb_transport_header(skb) +
371 sizeof(*pim));
372
373 if (!ipv6_addr_is_multicast(&encap->daddr) ||
374 encap->payload_len == 0 ||
375 ntohs(encap->payload_len) + sizeof(*pim) > skb->len)
376 goto drop;
377
378 read_lock(&mrt_lock);
379 if (reg_vif_num >= 0)
380 reg_dev = vif6_table[reg_vif_num].dev;
381 if (reg_dev)
382 dev_hold(reg_dev);
383 read_unlock(&mrt_lock);
384
385 if (reg_dev == NULL)
386 goto drop;
387
388 skb->mac_header = skb->network_header;
389 skb_pull(skb, (u8 *)encap - skb->data);
390 skb_reset_network_header(skb);
391 skb->dev = reg_dev;
392 skb->protocol = htons(ETH_P_IP);
393 skb->ip_summed = 0;
394 skb->pkt_type = PACKET_HOST;
395 dst_release(skb->dst);
Pavel Emelyanovdc58c782008-05-21 14:17:54 -0700396 reg_dev->stats.rx_bytes += skb->len;
397 reg_dev->stats.rx_packets++;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900398 skb->dst = NULL;
399 nf_reset(skb);
400 netif_rx(skb);
401 dev_put(reg_dev);
402 return 0;
403 drop:
404 kfree_skb(skb);
405 return 0;
406}
407
408static struct inet6_protocol pim6_protocol = {
409 .handler = pim6_rcv,
410};
411
412/* Service routines creating virtual interfaces: PIMREG */
413
414static int reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
415{
416 read_lock(&mrt_lock);
Pavel Emelyanovdc58c782008-05-21 14:17:54 -0700417 dev->stats.tx_bytes += skb->len;
418 dev->stats.tx_packets++;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900419 ip6mr_cache_report(skb, reg_vif_num, MRT6MSG_WHOLEPKT);
420 read_unlock(&mrt_lock);
421 kfree_skb(skb);
422 return 0;
423}
424
Stephen Hemminger007c3832008-11-20 20:28:35 -0800425static const struct net_device_ops reg_vif_netdev_ops = {
426 .ndo_start_xmit = reg_vif_xmit,
427};
428
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900429static void reg_vif_setup(struct net_device *dev)
430{
431 dev->type = ARPHRD_PIMREG;
432 dev->mtu = 1500 - sizeof(struct ipv6hdr) - 8;
433 dev->flags = IFF_NOARP;
Stephen Hemminger007c3832008-11-20 20:28:35 -0800434 dev->netdev_ops = &reg_vif_netdev_ops;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900435 dev->destructor = free_netdev;
436}
437
438static struct net_device *ip6mr_reg_vif(void)
439{
440 struct net_device *dev;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900441
Pavel Emelyanovdc58c782008-05-21 14:17:54 -0700442 dev = alloc_netdev(0, "pim6reg", reg_vif_setup);
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900443 if (dev == NULL)
444 return NULL;
445
446 if (register_netdevice(dev)) {
447 free_netdev(dev);
448 return NULL;
449 }
450 dev->iflink = 0;
451
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900452 if (dev_open(dev))
453 goto failure;
454
Wang Chen7af3db72008-07-14 20:54:54 -0700455 dev_hold(dev);
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900456 return dev;
457
458failure:
459 /* allow the register to be completed before unregistering. */
460 rtnl_unlock();
461 rtnl_lock();
462
463 unregister_netdevice(dev);
464 return NULL;
465}
466#endif
467
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900468/*
469 * Delete a VIF entry
470 */
471
472static int mif6_delete(int vifi)
473{
474 struct mif_device *v;
475 struct net_device *dev;
476 if (vifi < 0 || vifi >= maxvif)
477 return -EADDRNOTAVAIL;
478
479 v = &vif6_table[vifi];
480
481 write_lock_bh(&mrt_lock);
482 dev = v->dev;
483 v->dev = NULL;
484
485 if (!dev) {
486 write_unlock_bh(&mrt_lock);
487 return -EADDRNOTAVAIL;
488 }
489
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900490#ifdef CONFIG_IPV6_PIMSM_V2
491 if (vifi == reg_vif_num)
492 reg_vif_num = -1;
493#endif
494
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900495 if (vifi + 1 == maxvif) {
496 int tmp;
497 for (tmp = vifi - 1; tmp >= 0; tmp--) {
498 if (MIF_EXISTS(tmp))
499 break;
500 }
501 maxvif = tmp + 1;
502 }
503
504 write_unlock_bh(&mrt_lock);
505
506 dev_set_allmulti(dev, -1);
507
508 if (v->flags & MIFF_REGISTER)
509 unregister_netdevice(dev);
510
511 dev_put(dev);
512 return 0;
513}
514
515/* Destroy an unresolved cache entry, killing queued skbs
516 and reporting error to netlink readers.
517 */
518
519static void ip6mr_destroy_unres(struct mfc6_cache *c)
520{
521 struct sk_buff *skb;
522
523 atomic_dec(&cache_resolve_queue_len);
524
525 while((skb = skb_dequeue(&c->mfc_un.unres.unresolved)) != NULL) {
526 if (ipv6_hdr(skb)->version == 0) {
527 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct ipv6hdr));
528 nlh->nlmsg_type = NLMSG_ERROR;
529 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
530 skb_trim(skb, nlh->nlmsg_len);
531 ((struct nlmsgerr *)NLMSG_DATA(nlh))->error = -ETIMEDOUT;
532 rtnl_unicast(skb, &init_net, NETLINK_CB(skb).pid);
533 } else
534 kfree_skb(skb);
535 }
536
537 kmem_cache_free(mrt_cachep, c);
538}
539
540
541/* Single timer process for all the unresolved queue. */
542
543static void ipmr_do_expire_process(unsigned long dummy)
544{
545 unsigned long now = jiffies;
546 unsigned long expires = 10 * HZ;
547 struct mfc6_cache *c, **cp;
548
549 cp = &mfc_unres_queue;
550
551 while ((c = *cp) != NULL) {
552 if (time_after(c->mfc_un.unres.expires, now)) {
553 /* not yet... */
554 unsigned long interval = c->mfc_un.unres.expires - now;
555 if (interval < expires)
556 expires = interval;
557 cp = &c->next;
558 continue;
559 }
560
561 *cp = c->next;
562 ip6mr_destroy_unres(c);
563 }
564
565 if (atomic_read(&cache_resolve_queue_len))
566 mod_timer(&ipmr_expire_timer, jiffies + expires);
567}
568
569static void ipmr_expire_process(unsigned long dummy)
570{
571 if (!spin_trylock(&mfc_unres_lock)) {
572 mod_timer(&ipmr_expire_timer, jiffies + 1);
573 return;
574 }
575
576 if (atomic_read(&cache_resolve_queue_len))
577 ipmr_do_expire_process(dummy);
578
579 spin_unlock(&mfc_unres_lock);
580}
581
582/* Fill oifs list. It is called under write locked mrt_lock. */
583
584static void ip6mr_update_thresholds(struct mfc6_cache *cache, unsigned char *ttls)
585{
586 int vifi;
587
Rami Rosen6ac7eb02008-04-10 12:40:10 +0300588 cache->mfc_un.res.minvif = MAXMIFS;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900589 cache->mfc_un.res.maxvif = 0;
Rami Rosen6ac7eb02008-04-10 12:40:10 +0300590 memset(cache->mfc_un.res.ttls, 255, MAXMIFS);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900591
592 for (vifi = 0; vifi < maxvif; vifi++) {
593 if (MIF_EXISTS(vifi) && ttls[vifi] && ttls[vifi] < 255) {
594 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
595 if (cache->mfc_un.res.minvif > vifi)
596 cache->mfc_un.res.minvif = vifi;
597 if (cache->mfc_un.res.maxvif <= vifi)
598 cache->mfc_un.res.maxvif = vifi + 1;
599 }
600 }
601}
602
603static int mif6_add(struct mif6ctl *vifc, int mrtsock)
604{
605 int vifi = vifc->mif6c_mifi;
606 struct mif_device *v = &vif6_table[vifi];
607 struct net_device *dev;
Wang Chen5ae7b442008-07-14 20:54:23 -0700608 int err;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900609
610 /* Is vif busy ? */
611 if (MIF_EXISTS(vifi))
612 return -EADDRINUSE;
613
614 switch (vifc->mif6c_flags) {
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900615#ifdef CONFIG_IPV6_PIMSM_V2
616 case MIFF_REGISTER:
617 /*
618 * Special Purpose VIF in PIM
619 * All the packets will be sent to the daemon
620 */
621 if (reg_vif_num >= 0)
622 return -EADDRINUSE;
623 dev = ip6mr_reg_vif();
624 if (!dev)
625 return -ENOBUFS;
Wang Chen5ae7b442008-07-14 20:54:23 -0700626 err = dev_set_allmulti(dev, 1);
627 if (err) {
628 unregister_netdevice(dev);
Wang Chen7af3db72008-07-14 20:54:54 -0700629 dev_put(dev);
Wang Chen5ae7b442008-07-14 20:54:23 -0700630 return err;
631 }
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900632 break;
633#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900634 case 0:
635 dev = dev_get_by_index(&init_net, vifc->mif6c_pifi);
636 if (!dev)
637 return -EADDRNOTAVAIL;
Wang Chen5ae7b442008-07-14 20:54:23 -0700638 err = dev_set_allmulti(dev, 1);
Wang Chen7af3db72008-07-14 20:54:54 -0700639 if (err) {
640 dev_put(dev);
Wang Chen5ae7b442008-07-14 20:54:23 -0700641 return err;
Wang Chen7af3db72008-07-14 20:54:54 -0700642 }
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900643 break;
644 default:
645 return -EINVAL;
646 }
647
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900648 /*
649 * Fill in the VIF structures
650 */
651 v->rate_limit = vifc->vifc_rate_limit;
652 v->flags = vifc->mif6c_flags;
653 if (!mrtsock)
654 v->flags |= VIFF_STATIC;
655 v->threshold = vifc->vifc_threshold;
656 v->bytes_in = 0;
657 v->bytes_out = 0;
658 v->pkt_in = 0;
659 v->pkt_out = 0;
660 v->link = dev->ifindex;
661 if (v->flags & MIFF_REGISTER)
662 v->link = dev->iflink;
663
664 /* And finish update writing critical data */
665 write_lock_bh(&mrt_lock);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900666 v->dev = dev;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900667#ifdef CONFIG_IPV6_PIMSM_V2
668 if (v->flags & MIFF_REGISTER)
669 reg_vif_num = vifi;
670#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900671 if (vifi + 1 > maxvif)
672 maxvif = vifi + 1;
673 write_unlock_bh(&mrt_lock);
674 return 0;
675}
676
677static struct mfc6_cache *ip6mr_cache_find(struct in6_addr *origin, struct in6_addr *mcastgrp)
678{
679 int line = MFC6_HASH(mcastgrp, origin);
680 struct mfc6_cache *c;
681
682 for (c = mfc6_cache_array[line]; c; c = c->next) {
683 if (ipv6_addr_equal(&c->mf6c_origin, origin) &&
684 ipv6_addr_equal(&c->mf6c_mcastgrp, mcastgrp))
685 break;
686 }
687 return c;
688}
689
690/*
691 * Allocate a multicast cache entry
692 */
693static struct mfc6_cache *ip6mr_cache_alloc(void)
694{
Joe Perches36cbac52008-12-03 22:27:25 -0800695 struct mfc6_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900696 if (c == NULL)
697 return NULL;
Rami Rosen6ac7eb02008-04-10 12:40:10 +0300698 c->mfc_un.res.minvif = MAXMIFS;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900699 return c;
700}
701
702static struct mfc6_cache *ip6mr_cache_alloc_unres(void)
703{
Joe Perches36cbac52008-12-03 22:27:25 -0800704 struct mfc6_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900705 if (c == NULL)
706 return NULL;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900707 skb_queue_head_init(&c->mfc_un.unres.unresolved);
708 c->mfc_un.unres.expires = jiffies + 10 * HZ;
709 return c;
710}
711
712/*
713 * A cache entry has gone into a resolved state from queued
714 */
715
716static void ip6mr_cache_resolve(struct mfc6_cache *uc, struct mfc6_cache *c)
717{
718 struct sk_buff *skb;
719
720 /*
721 * Play the pending entries through our router
722 */
723
724 while((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
725 if (ipv6_hdr(skb)->version == 0) {
726 int err;
727 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct ipv6hdr));
728
729 if (ip6mr_fill_mroute(skb, c, NLMSG_DATA(nlh)) > 0) {
YOSHIFUJI Hideaki549e0282008-04-05 22:17:39 +0900730 nlh->nlmsg_len = skb_tail_pointer(skb) - (u8 *)nlh;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900731 } else {
732 nlh->nlmsg_type = NLMSG_ERROR;
733 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
734 skb_trim(skb, nlh->nlmsg_len);
735 ((struct nlmsgerr *)NLMSG_DATA(nlh))->error = -EMSGSIZE;
736 }
737 err = rtnl_unicast(skb, &init_net, NETLINK_CB(skb).pid);
738 } else
739 ip6_mr_forward(skb, c);
740 }
741}
742
743/*
744 * Bounce a cache query up to pim6sd. We could use netlink for this but pim6sd
745 * expects the following bizarre scheme.
746 *
747 * Called under mrt_lock.
748 */
749
Rami Rosen6ac7eb02008-04-10 12:40:10 +0300750static int ip6mr_cache_report(struct sk_buff *pkt, mifi_t mifi, int assert)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900751{
752 struct sk_buff *skb;
753 struct mrt6msg *msg;
754 int ret;
755
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900756#ifdef CONFIG_IPV6_PIMSM_V2
757 if (assert == MRT6MSG_WHOLEPKT)
758 skb = skb_realloc_headroom(pkt, -skb_network_offset(pkt)
759 +sizeof(*msg));
760 else
761#endif
762 skb = alloc_skb(sizeof(struct ipv6hdr) + sizeof(*msg), GFP_ATOMIC);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900763
764 if (!skb)
765 return -ENOBUFS;
766
767 /* I suppose that internal messages
768 * do not require checksums */
769
770 skb->ip_summed = CHECKSUM_UNNECESSARY;
771
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900772#ifdef CONFIG_IPV6_PIMSM_V2
773 if (assert == MRT6MSG_WHOLEPKT) {
774 /* Ugly, but we have no choice with this interface.
775 Duplicate old header, fix length etc.
776 And all this only to mangle msg->im6_msgtype and
777 to set msg->im6_mbz to "mbz" :-)
778 */
779 skb_push(skb, -skb_network_offset(pkt));
780
781 skb_push(skb, sizeof(*msg));
782 skb_reset_transport_header(skb);
783 msg = (struct mrt6msg *)skb_transport_header(skb);
784 msg->im6_mbz = 0;
785 msg->im6_msgtype = MRT6MSG_WHOLEPKT;
786 msg->im6_mif = reg_vif_num;
787 msg->im6_pad = 0;
788 ipv6_addr_copy(&msg->im6_src, &ipv6_hdr(pkt)->saddr);
789 ipv6_addr_copy(&msg->im6_dst, &ipv6_hdr(pkt)->daddr);
790
791 skb->ip_summed = CHECKSUM_UNNECESSARY;
792 } else
793#endif
794 {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900795 /*
796 * Copy the IP header
797 */
798
799 skb_put(skb, sizeof(struct ipv6hdr));
800 skb_reset_network_header(skb);
801 skb_copy_to_linear_data(skb, ipv6_hdr(pkt), sizeof(struct ipv6hdr));
802
803 /*
804 * Add our header
805 */
806 skb_put(skb, sizeof(*msg));
807 skb_reset_transport_header(skb);
808 msg = (struct mrt6msg *)skb_transport_header(skb);
809
810 msg->im6_mbz = 0;
811 msg->im6_msgtype = assert;
Rami Rosen6ac7eb02008-04-10 12:40:10 +0300812 msg->im6_mif = mifi;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900813 msg->im6_pad = 0;
814 ipv6_addr_copy(&msg->im6_src, &ipv6_hdr(pkt)->saddr);
815 ipv6_addr_copy(&msg->im6_dst, &ipv6_hdr(pkt)->daddr);
816
817 skb->dst = dst_clone(pkt->dst);
818 skb->ip_summed = CHECKSUM_UNNECESSARY;
819
820 skb_pull(skb, sizeof(struct ipv6hdr));
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900821 }
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900822
823 if (mroute6_socket == NULL) {
824 kfree_skb(skb);
825 return -EINVAL;
826 }
827
828 /*
829 * Deliver to user space multicast routing algorithms
830 */
831 if ((ret = sock_queue_rcv_skb(mroute6_socket, skb)) < 0) {
832 if (net_ratelimit())
833 printk(KERN_WARNING "mroute6: pending queue full, dropping entries.\n");
834 kfree_skb(skb);
835 }
836
837 return ret;
838}
839
840/*
841 * Queue a packet for resolution. It gets locked cache entry!
842 */
843
844static int
Rami Rosen6ac7eb02008-04-10 12:40:10 +0300845ip6mr_cache_unresolved(mifi_t mifi, struct sk_buff *skb)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900846{
847 int err;
848 struct mfc6_cache *c;
849
850 spin_lock_bh(&mfc_unres_lock);
851 for (c = mfc_unres_queue; c; c = c->next) {
852 if (ipv6_addr_equal(&c->mf6c_mcastgrp, &ipv6_hdr(skb)->daddr) &&
853 ipv6_addr_equal(&c->mf6c_origin, &ipv6_hdr(skb)->saddr))
854 break;
855 }
856
857 if (c == NULL) {
858 /*
859 * Create a new entry if allowable
860 */
861
862 if (atomic_read(&cache_resolve_queue_len) >= 10 ||
863 (c = ip6mr_cache_alloc_unres()) == NULL) {
864 spin_unlock_bh(&mfc_unres_lock);
865
866 kfree_skb(skb);
867 return -ENOBUFS;
868 }
869
870 /*
871 * Fill in the new cache entry
872 */
873 c->mf6c_parent = -1;
874 c->mf6c_origin = ipv6_hdr(skb)->saddr;
875 c->mf6c_mcastgrp = ipv6_hdr(skb)->daddr;
876
877 /*
878 * Reflect first query at pim6sd
879 */
Rami Rosen6ac7eb02008-04-10 12:40:10 +0300880 if ((err = ip6mr_cache_report(skb, mifi, MRT6MSG_NOCACHE)) < 0) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900881 /* If the report failed throw the cache entry
882 out - Brad Parker
883 */
884 spin_unlock_bh(&mfc_unres_lock);
885
886 kmem_cache_free(mrt_cachep, c);
887 kfree_skb(skb);
888 return err;
889 }
890
891 atomic_inc(&cache_resolve_queue_len);
892 c->next = mfc_unres_queue;
893 mfc_unres_queue = c;
894
895 ipmr_do_expire_process(1);
896 }
897
898 /*
899 * See if we can append the packet
900 */
901 if (c->mfc_un.unres.unresolved.qlen > 3) {
902 kfree_skb(skb);
903 err = -ENOBUFS;
904 } else {
905 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
906 err = 0;
907 }
908
909 spin_unlock_bh(&mfc_unres_lock);
910 return err;
911}
912
913/*
914 * MFC6 cache manipulation by user space
915 */
916
917static int ip6mr_mfc_delete(struct mf6cctl *mfc)
918{
919 int line;
920 struct mfc6_cache *c, **cp;
921
922 line = MFC6_HASH(&mfc->mf6cc_mcastgrp.sin6_addr, &mfc->mf6cc_origin.sin6_addr);
923
924 for (cp = &mfc6_cache_array[line]; (c = *cp) != NULL; cp = &c->next) {
925 if (ipv6_addr_equal(&c->mf6c_origin, &mfc->mf6cc_origin.sin6_addr) &&
926 ipv6_addr_equal(&c->mf6c_mcastgrp, &mfc->mf6cc_mcastgrp.sin6_addr)) {
927 write_lock_bh(&mrt_lock);
928 *cp = c->next;
929 write_unlock_bh(&mrt_lock);
930
931 kmem_cache_free(mrt_cachep, c);
932 return 0;
933 }
934 }
935 return -ENOENT;
936}
937
938static int ip6mr_device_event(struct notifier_block *this,
939 unsigned long event, void *ptr)
940{
941 struct net_device *dev = ptr;
942 struct mif_device *v;
943 int ct;
944
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -0700945 if (!net_eq(dev_net(dev), &init_net))
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900946 return NOTIFY_DONE;
947
948 if (event != NETDEV_UNREGISTER)
949 return NOTIFY_DONE;
950
951 v = &vif6_table[0];
952 for (ct = 0; ct < maxvif; ct++, v++) {
953 if (v->dev == dev)
954 mif6_delete(ct);
955 }
956 return NOTIFY_DONE;
957}
958
959static struct notifier_block ip6_mr_notifier = {
960 .notifier_call = ip6mr_device_event
961};
962
963/*
964 * Setup for IP multicast routing
965 */
966
Wang Chen623d1a12008-07-03 12:13:30 +0800967int __init ip6_mr_init(void)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900968{
Wang Chen623d1a12008-07-03 12:13:30 +0800969 int err;
970
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900971 mrt_cachep = kmem_cache_create("ip6_mrt_cache",
972 sizeof(struct mfc6_cache),
973 0, SLAB_HWCACHE_ALIGN,
974 NULL);
975 if (!mrt_cachep)
Wang Chen623d1a12008-07-03 12:13:30 +0800976 return -ENOMEM;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900977
978 setup_timer(&ipmr_expire_timer, ipmr_expire_process, 0);
Wang Chen623d1a12008-07-03 12:13:30 +0800979 err = register_netdevice_notifier(&ip6_mr_notifier);
980 if (err)
981 goto reg_notif_fail;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900982#ifdef CONFIG_PROC_FS
Wang Chen623d1a12008-07-03 12:13:30 +0800983 err = -ENOMEM;
984 if (!proc_net_fops_create(&init_net, "ip6_mr_vif", 0, &ip6mr_vif_fops))
985 goto proc_vif_fail;
986 if (!proc_net_fops_create(&init_net, "ip6_mr_cache",
987 0, &ip6mr_mfc_fops))
988 goto proc_cache_fail;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900989#endif
Wang Chen623d1a12008-07-03 12:13:30 +0800990 return 0;
Wang Chen623d1a12008-07-03 12:13:30 +0800991#ifdef CONFIG_PROC_FS
Wang Chen623d1a12008-07-03 12:13:30 +0800992proc_cache_fail:
993 proc_net_remove(&init_net, "ip6_mr_vif");
Benjamin Thery87b30a62008-11-10 16:34:11 -0800994proc_vif_fail:
995 unregister_netdevice_notifier(&ip6_mr_notifier);
Wang Chen623d1a12008-07-03 12:13:30 +0800996#endif
Benjamin Thery87b30a62008-11-10 16:34:11 -0800997reg_notif_fail:
998 del_timer(&ipmr_expire_timer);
999 kmem_cache_destroy(mrt_cachep);
Wang Chen623d1a12008-07-03 12:13:30 +08001000 return err;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001001}
1002
Wang Chen623d1a12008-07-03 12:13:30 +08001003void ip6_mr_cleanup(void)
1004{
1005#ifdef CONFIG_PROC_FS
1006 proc_net_remove(&init_net, "ip6_mr_cache");
1007 proc_net_remove(&init_net, "ip6_mr_vif");
1008#endif
1009 unregister_netdevice_notifier(&ip6_mr_notifier);
1010 del_timer(&ipmr_expire_timer);
1011 kmem_cache_destroy(mrt_cachep);
1012}
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001013
1014static int ip6mr_mfc_add(struct mf6cctl *mfc, int mrtsock)
1015{
1016 int line;
1017 struct mfc6_cache *uc, *c, **cp;
Rami Rosen6ac7eb02008-04-10 12:40:10 +03001018 unsigned char ttls[MAXMIFS];
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001019 int i;
1020
Rami Rosen6ac7eb02008-04-10 12:40:10 +03001021 memset(ttls, 255, MAXMIFS);
1022 for (i = 0; i < MAXMIFS; i++) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001023 if (IF_ISSET(i, &mfc->mf6cc_ifset))
1024 ttls[i] = 1;
1025
1026 }
1027
1028 line = MFC6_HASH(&mfc->mf6cc_mcastgrp.sin6_addr, &mfc->mf6cc_origin.sin6_addr);
1029
1030 for (cp = &mfc6_cache_array[line]; (c = *cp) != NULL; cp = &c->next) {
1031 if (ipv6_addr_equal(&c->mf6c_origin, &mfc->mf6cc_origin.sin6_addr) &&
1032 ipv6_addr_equal(&c->mf6c_mcastgrp, &mfc->mf6cc_mcastgrp.sin6_addr))
1033 break;
1034 }
1035
1036 if (c != NULL) {
1037 write_lock_bh(&mrt_lock);
1038 c->mf6c_parent = mfc->mf6cc_parent;
1039 ip6mr_update_thresholds(c, ttls);
1040 if (!mrtsock)
1041 c->mfc_flags |= MFC_STATIC;
1042 write_unlock_bh(&mrt_lock);
1043 return 0;
1044 }
1045
1046 if (!ipv6_addr_is_multicast(&mfc->mf6cc_mcastgrp.sin6_addr))
1047 return -EINVAL;
1048
1049 c = ip6mr_cache_alloc();
1050 if (c == NULL)
1051 return -ENOMEM;
1052
1053 c->mf6c_origin = mfc->mf6cc_origin.sin6_addr;
1054 c->mf6c_mcastgrp = mfc->mf6cc_mcastgrp.sin6_addr;
1055 c->mf6c_parent = mfc->mf6cc_parent;
1056 ip6mr_update_thresholds(c, ttls);
1057 if (!mrtsock)
1058 c->mfc_flags |= MFC_STATIC;
1059
1060 write_lock_bh(&mrt_lock);
1061 c->next = mfc6_cache_array[line];
1062 mfc6_cache_array[line] = c;
1063 write_unlock_bh(&mrt_lock);
1064
1065 /*
1066 * Check to see if we resolved a queued list. If so we
1067 * need to send on the frames and tidy up.
1068 */
1069 spin_lock_bh(&mfc_unres_lock);
1070 for (cp = &mfc_unres_queue; (uc = *cp) != NULL;
1071 cp = &uc->next) {
1072 if (ipv6_addr_equal(&uc->mf6c_origin, &c->mf6c_origin) &&
1073 ipv6_addr_equal(&uc->mf6c_mcastgrp, &c->mf6c_mcastgrp)) {
1074 *cp = uc->next;
1075 if (atomic_dec_and_test(&cache_resolve_queue_len))
1076 del_timer(&ipmr_expire_timer);
1077 break;
1078 }
1079 }
1080 spin_unlock_bh(&mfc_unres_lock);
1081
1082 if (uc) {
1083 ip6mr_cache_resolve(uc, c);
1084 kmem_cache_free(mrt_cachep, uc);
1085 }
1086 return 0;
1087}
1088
1089/*
1090 * Close the multicast socket, and clear the vif tables etc
1091 */
1092
1093static void mroute_clean_tables(struct sock *sk)
1094{
1095 int i;
1096
1097 /*
1098 * Shut down all active vif entries
1099 */
1100 for (i = 0; i < maxvif; i++) {
1101 if (!(vif6_table[i].flags & VIFF_STATIC))
1102 mif6_delete(i);
1103 }
1104
1105 /*
1106 * Wipe the cache
1107 */
1108 for (i = 0; i < ARRAY_SIZE(mfc6_cache_array); i++) {
1109 struct mfc6_cache *c, **cp;
1110
1111 cp = &mfc6_cache_array[i];
1112 while ((c = *cp) != NULL) {
1113 if (c->mfc_flags & MFC_STATIC) {
1114 cp = &c->next;
1115 continue;
1116 }
1117 write_lock_bh(&mrt_lock);
1118 *cp = c->next;
1119 write_unlock_bh(&mrt_lock);
1120
1121 kmem_cache_free(mrt_cachep, c);
1122 }
1123 }
1124
1125 if (atomic_read(&cache_resolve_queue_len) != 0) {
1126 struct mfc6_cache *c;
1127
1128 spin_lock_bh(&mfc_unres_lock);
1129 while (mfc_unres_queue != NULL) {
1130 c = mfc_unres_queue;
1131 mfc_unres_queue = c->next;
1132 spin_unlock_bh(&mfc_unres_lock);
1133
1134 ip6mr_destroy_unres(c);
1135
1136 spin_lock_bh(&mfc_unres_lock);
1137 }
1138 spin_unlock_bh(&mfc_unres_lock);
1139 }
1140}
1141
1142static int ip6mr_sk_init(struct sock *sk)
1143{
1144 int err = 0;
1145
1146 rtnl_lock();
1147 write_lock_bh(&mrt_lock);
1148 if (likely(mroute6_socket == NULL))
1149 mroute6_socket = sk;
1150 else
1151 err = -EADDRINUSE;
1152 write_unlock_bh(&mrt_lock);
1153
1154 rtnl_unlock();
1155
1156 return err;
1157}
1158
1159int ip6mr_sk_done(struct sock *sk)
1160{
1161 int err = 0;
1162
1163 rtnl_lock();
1164 if (sk == mroute6_socket) {
1165 write_lock_bh(&mrt_lock);
1166 mroute6_socket = NULL;
1167 write_unlock_bh(&mrt_lock);
1168
1169 mroute_clean_tables(sk);
1170 } else
1171 err = -EACCES;
1172 rtnl_unlock();
1173
1174 return err;
1175}
1176
1177/*
1178 * Socket options and virtual interface manipulation. The whole
1179 * virtual interface system is a complete heap, but unfortunately
1180 * that's how BSD mrouted happens to think. Maybe one day with a proper
1181 * MOSPF/PIM router set up we can clean this up.
1182 */
1183
1184int ip6_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, int optlen)
1185{
1186 int ret;
1187 struct mif6ctl vif;
1188 struct mf6cctl mfc;
1189 mifi_t mifi;
1190
1191 if (optname != MRT6_INIT) {
1192 if (sk != mroute6_socket && !capable(CAP_NET_ADMIN))
1193 return -EACCES;
1194 }
1195
1196 switch (optname) {
1197 case MRT6_INIT:
1198 if (sk->sk_type != SOCK_RAW ||
1199 inet_sk(sk)->num != IPPROTO_ICMPV6)
1200 return -EOPNOTSUPP;
1201 if (optlen < sizeof(int))
1202 return -EINVAL;
1203
1204 return ip6mr_sk_init(sk);
1205
1206 case MRT6_DONE:
1207 return ip6mr_sk_done(sk);
1208
1209 case MRT6_ADD_MIF:
1210 if (optlen < sizeof(vif))
1211 return -EINVAL;
1212 if (copy_from_user(&vif, optval, sizeof(vif)))
1213 return -EFAULT;
Rami Rosen6ac7eb02008-04-10 12:40:10 +03001214 if (vif.mif6c_mifi >= MAXMIFS)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001215 return -ENFILE;
1216 rtnl_lock();
1217 ret = mif6_add(&vif, sk == mroute6_socket);
1218 rtnl_unlock();
1219 return ret;
1220
1221 case MRT6_DEL_MIF:
1222 if (optlen < sizeof(mifi_t))
1223 return -EINVAL;
1224 if (copy_from_user(&mifi, optval, sizeof(mifi_t)))
1225 return -EFAULT;
1226 rtnl_lock();
1227 ret = mif6_delete(mifi);
1228 rtnl_unlock();
1229 return ret;
1230
1231 /*
1232 * Manipulate the forwarding caches. These live
1233 * in a sort of kernel/user symbiosis.
1234 */
1235 case MRT6_ADD_MFC:
1236 case MRT6_DEL_MFC:
1237 if (optlen < sizeof(mfc))
1238 return -EINVAL;
1239 if (copy_from_user(&mfc, optval, sizeof(mfc)))
1240 return -EFAULT;
1241 rtnl_lock();
1242 if (optname == MRT6_DEL_MFC)
1243 ret = ip6mr_mfc_delete(&mfc);
1244 else
1245 ret = ip6mr_mfc_add(&mfc, sk == mroute6_socket);
1246 rtnl_unlock();
1247 return ret;
1248
1249 /*
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001250 * Control PIM assert (to activate pim will activate assert)
1251 */
1252 case MRT6_ASSERT:
1253 {
1254 int v;
1255 if (get_user(v, (int __user *)optval))
1256 return -EFAULT;
1257 mroute_do_assert = !!v;
1258 return 0;
1259 }
1260
1261#ifdef CONFIG_IPV6_PIMSM_V2
1262 case MRT6_PIM:
1263 {
YOSHIFUJI Hideakia9f83bf2008-04-10 15:41:28 +09001264 int v;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001265 if (get_user(v, (int __user *)optval))
1266 return -EFAULT;
1267 v = !!v;
1268 rtnl_lock();
1269 ret = 0;
1270 if (v != mroute_do_pim) {
1271 mroute_do_pim = v;
1272 mroute_do_assert = v;
1273 if (mroute_do_pim)
1274 ret = inet6_add_protocol(&pim6_protocol,
1275 IPPROTO_PIM);
1276 else
1277 ret = inet6_del_protocol(&pim6_protocol,
1278 IPPROTO_PIM);
1279 if (ret < 0)
1280 ret = -EAGAIN;
1281 }
1282 rtnl_unlock();
1283 return ret;
1284 }
1285
1286#endif
1287 /*
Rami Rosen7d120c52008-04-23 14:35:13 +03001288 * Spurious command, or MRT6_VERSION which you cannot
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001289 * set.
1290 */
1291 default:
1292 return -ENOPROTOOPT;
1293 }
1294}
1295
1296/*
1297 * Getsock opt support for the multicast routing system.
1298 */
1299
1300int ip6_mroute_getsockopt(struct sock *sk, int optname, char __user *optval,
1301 int __user *optlen)
1302{
1303 int olr;
1304 int val;
1305
1306 switch (optname) {
1307 case MRT6_VERSION:
1308 val = 0x0305;
1309 break;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001310#ifdef CONFIG_IPV6_PIMSM_V2
1311 case MRT6_PIM:
1312 val = mroute_do_pim;
1313 break;
1314#endif
1315 case MRT6_ASSERT:
1316 val = mroute_do_assert;
1317 break;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001318 default:
1319 return -ENOPROTOOPT;
1320 }
1321
1322 if (get_user(olr, optlen))
1323 return -EFAULT;
1324
1325 olr = min_t(int, olr, sizeof(int));
1326 if (olr < 0)
1327 return -EINVAL;
1328
1329 if (put_user(olr, optlen))
1330 return -EFAULT;
1331 if (copy_to_user(optval, &val, olr))
1332 return -EFAULT;
1333 return 0;
1334}
1335
1336/*
1337 * The IP multicast ioctl support routines.
1338 */
1339
1340int ip6mr_ioctl(struct sock *sk, int cmd, void __user *arg)
1341{
1342 struct sioc_sg_req6 sr;
1343 struct sioc_mif_req6 vr;
1344 struct mif_device *vif;
1345 struct mfc6_cache *c;
1346
1347 switch (cmd) {
1348 case SIOCGETMIFCNT_IN6:
1349 if (copy_from_user(&vr, arg, sizeof(vr)))
1350 return -EFAULT;
1351 if (vr.mifi >= maxvif)
1352 return -EINVAL;
1353 read_lock(&mrt_lock);
1354 vif = &vif6_table[vr.mifi];
1355 if (MIF_EXISTS(vr.mifi)) {
1356 vr.icount = vif->pkt_in;
1357 vr.ocount = vif->pkt_out;
1358 vr.ibytes = vif->bytes_in;
1359 vr.obytes = vif->bytes_out;
1360 read_unlock(&mrt_lock);
1361
1362 if (copy_to_user(arg, &vr, sizeof(vr)))
1363 return -EFAULT;
1364 return 0;
1365 }
1366 read_unlock(&mrt_lock);
1367 return -EADDRNOTAVAIL;
1368 case SIOCGETSGCNT_IN6:
1369 if (copy_from_user(&sr, arg, sizeof(sr)))
1370 return -EFAULT;
1371
1372 read_lock(&mrt_lock);
1373 c = ip6mr_cache_find(&sr.src.sin6_addr, &sr.grp.sin6_addr);
1374 if (c) {
1375 sr.pktcnt = c->mfc_un.res.pkt;
1376 sr.bytecnt = c->mfc_un.res.bytes;
1377 sr.wrong_if = c->mfc_un.res.wrong_if;
1378 read_unlock(&mrt_lock);
1379
1380 if (copy_to_user(arg, &sr, sizeof(sr)))
1381 return -EFAULT;
1382 return 0;
1383 }
1384 read_unlock(&mrt_lock);
1385 return -EADDRNOTAVAIL;
1386 default:
1387 return -ENOIOCTLCMD;
1388 }
1389}
1390
1391
1392static inline int ip6mr_forward2_finish(struct sk_buff *skb)
1393{
Denis V. Lunev483a47d2008-10-08 11:09:27 -07001394 IP6_INC_STATS_BH(dev_net(skb->dst->dev), ip6_dst_idev(skb->dst),
1395 IPSTATS_MIB_OUTFORWDATAGRAMS);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001396 return dst_output(skb);
1397}
1398
1399/*
1400 * Processing handlers for ip6mr_forward
1401 */
1402
1403static int ip6mr_forward2(struct sk_buff *skb, struct mfc6_cache *c, int vifi)
1404{
1405 struct ipv6hdr *ipv6h;
1406 struct mif_device *vif = &vif6_table[vifi];
1407 struct net_device *dev;
1408 struct dst_entry *dst;
1409 struct flowi fl;
1410
1411 if (vif->dev == NULL)
1412 goto out_free;
1413
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001414#ifdef CONFIG_IPV6_PIMSM_V2
1415 if (vif->flags & MIFF_REGISTER) {
1416 vif->pkt_out++;
1417 vif->bytes_out += skb->len;
Pavel Emelyanovdc58c782008-05-21 14:17:54 -07001418 vif->dev->stats.tx_bytes += skb->len;
1419 vif->dev->stats.tx_packets++;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001420 ip6mr_cache_report(skb, vifi, MRT6MSG_WHOLEPKT);
1421 kfree_skb(skb);
1422 return 0;
1423 }
1424#endif
1425
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001426 ipv6h = ipv6_hdr(skb);
1427
1428 fl = (struct flowi) {
1429 .oif = vif->link,
1430 .nl_u = { .ip6_u =
1431 { .daddr = ipv6h->daddr, }
1432 }
1433 };
1434
1435 dst = ip6_route_output(&init_net, NULL, &fl);
1436 if (!dst)
1437 goto out_free;
1438
1439 dst_release(skb->dst);
1440 skb->dst = dst;
1441
1442 /*
1443 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1444 * not only before forwarding, but after forwarding on all output
1445 * interfaces. It is clear, if mrouter runs a multicasting
1446 * program, it should receive packets not depending to what interface
1447 * program is joined.
1448 * If we will not make it, the program will have to join on all
1449 * interfaces. On the other hand, multihoming host (or router, but
1450 * not mrouter) cannot join to more than one interface - it will
1451 * result in receiving multiple packets.
1452 */
1453 dev = vif->dev;
1454 skb->dev = dev;
1455 vif->pkt_out++;
1456 vif->bytes_out += skb->len;
1457
1458 /* We are about to write */
1459 /* XXX: extension headers? */
1460 if (skb_cow(skb, sizeof(*ipv6h) + LL_RESERVED_SPACE(dev)))
1461 goto out_free;
1462
1463 ipv6h = ipv6_hdr(skb);
1464 ipv6h->hop_limit--;
1465
1466 IP6CB(skb)->flags |= IP6SKB_FORWARDED;
1467
1468 return NF_HOOK(PF_INET6, NF_INET_FORWARD, skb, skb->dev, dev,
1469 ip6mr_forward2_finish);
1470
1471out_free:
1472 kfree_skb(skb);
1473 return 0;
1474}
1475
1476static int ip6mr_find_vif(struct net_device *dev)
1477{
1478 int ct;
1479 for (ct = maxvif - 1; ct >= 0; ct--) {
1480 if (vif6_table[ct].dev == dev)
1481 break;
1482 }
1483 return ct;
1484}
1485
1486static int ip6_mr_forward(struct sk_buff *skb, struct mfc6_cache *cache)
1487{
1488 int psend = -1;
1489 int vif, ct;
1490
1491 vif = cache->mf6c_parent;
1492 cache->mfc_un.res.pkt++;
1493 cache->mfc_un.res.bytes += skb->len;
1494
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001495 /*
1496 * Wrong interface: drop packet and (maybe) send PIM assert.
1497 */
1498 if (vif6_table[vif].dev != skb->dev) {
1499 int true_vifi;
1500
1501 cache->mfc_un.res.wrong_if++;
1502 true_vifi = ip6mr_find_vif(skb->dev);
1503
1504 if (true_vifi >= 0 && mroute_do_assert &&
1505 /* pimsm uses asserts, when switching from RPT to SPT,
1506 so that we cannot check that packet arrived on an oif.
1507 It is bad, but otherwise we would need to move pretty
1508 large chunk of pimd to kernel. Ough... --ANK
1509 */
1510 (mroute_do_pim || cache->mfc_un.res.ttls[true_vifi] < 255) &&
1511 time_after(jiffies,
1512 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1513 cache->mfc_un.res.last_assert = jiffies;
1514 ip6mr_cache_report(skb, true_vifi, MRT6MSG_WRONGMIF);
1515 }
1516 goto dont_forward;
1517 }
1518
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001519 vif6_table[vif].pkt_in++;
1520 vif6_table[vif].bytes_in += skb->len;
1521
1522 /*
1523 * Forward the frame
1524 */
1525 for (ct = cache->mfc_un.res.maxvif - 1; ct >= cache->mfc_un.res.minvif; ct--) {
1526 if (ipv6_hdr(skb)->hop_limit > cache->mfc_un.res.ttls[ct]) {
1527 if (psend != -1) {
1528 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1529 if (skb2)
1530 ip6mr_forward2(skb2, cache, psend);
1531 }
1532 psend = ct;
1533 }
1534 }
1535 if (psend != -1) {
1536 ip6mr_forward2(skb, cache, psend);
1537 return 0;
1538 }
1539
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001540dont_forward:
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001541 kfree_skb(skb);
1542 return 0;
1543}
1544
1545
1546/*
1547 * Multicast packets for forwarding arrive here
1548 */
1549
1550int ip6_mr_input(struct sk_buff *skb)
1551{
1552 struct mfc6_cache *cache;
1553
1554 read_lock(&mrt_lock);
1555 cache = ip6mr_cache_find(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr);
1556
1557 /*
1558 * No usable cache entry
1559 */
1560 if (cache == NULL) {
1561 int vif;
1562
1563 vif = ip6mr_find_vif(skb->dev);
1564 if (vif >= 0) {
1565 int err = ip6mr_cache_unresolved(vif, skb);
1566 read_unlock(&mrt_lock);
1567
1568 return err;
1569 }
1570 read_unlock(&mrt_lock);
1571 kfree_skb(skb);
1572 return -ENODEV;
1573 }
1574
1575 ip6_mr_forward(skb, cache);
1576
1577 read_unlock(&mrt_lock);
1578
1579 return 0;
1580}
1581
1582
1583static int
1584ip6mr_fill_mroute(struct sk_buff *skb, struct mfc6_cache *c, struct rtmsg *rtm)
1585{
1586 int ct;
1587 struct rtnexthop *nhp;
1588 struct net_device *dev = vif6_table[c->mf6c_parent].dev;
YOSHIFUJI Hideaki549e0282008-04-05 22:17:39 +09001589 u8 *b = skb_tail_pointer(skb);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001590 struct rtattr *mp_head;
1591
1592 if (dev)
1593 RTA_PUT(skb, RTA_IIF, 4, &dev->ifindex);
1594
1595 mp_head = (struct rtattr *)skb_put(skb, RTA_LENGTH(0));
1596
1597 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
1598 if (c->mfc_un.res.ttls[ct] < 255) {
1599 if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
1600 goto rtattr_failure;
1601 nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
1602 nhp->rtnh_flags = 0;
1603 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
1604 nhp->rtnh_ifindex = vif6_table[ct].dev->ifindex;
1605 nhp->rtnh_len = sizeof(*nhp);
1606 }
1607 }
1608 mp_head->rta_type = RTA_MULTIPATH;
YOSHIFUJI Hideaki549e0282008-04-05 22:17:39 +09001609 mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001610 rtm->rtm_type = RTN_MULTICAST;
1611 return 1;
1612
1613rtattr_failure:
1614 nlmsg_trim(skb, b);
1615 return -EMSGSIZE;
1616}
1617
1618int ip6mr_get_route(struct sk_buff *skb, struct rtmsg *rtm, int nowait)
1619{
1620 int err;
1621 struct mfc6_cache *cache;
1622 struct rt6_info *rt = (struct rt6_info *)skb->dst;
1623
1624 read_lock(&mrt_lock);
1625 cache = ip6mr_cache_find(&rt->rt6i_src.addr, &rt->rt6i_dst.addr);
1626
1627 if (!cache) {
1628 struct sk_buff *skb2;
1629 struct ipv6hdr *iph;
1630 struct net_device *dev;
1631 int vif;
1632
1633 if (nowait) {
1634 read_unlock(&mrt_lock);
1635 return -EAGAIN;
1636 }
1637
1638 dev = skb->dev;
1639 if (dev == NULL || (vif = ip6mr_find_vif(dev)) < 0) {
1640 read_unlock(&mrt_lock);
1641 return -ENODEV;
1642 }
1643
1644 /* really correct? */
1645 skb2 = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
1646 if (!skb2) {
1647 read_unlock(&mrt_lock);
1648 return -ENOMEM;
1649 }
1650
1651 skb_reset_transport_header(skb2);
1652
1653 skb_put(skb2, sizeof(struct ipv6hdr));
1654 skb_reset_network_header(skb2);
1655
1656 iph = ipv6_hdr(skb2);
1657 iph->version = 0;
1658 iph->priority = 0;
1659 iph->flow_lbl[0] = 0;
1660 iph->flow_lbl[1] = 0;
1661 iph->flow_lbl[2] = 0;
1662 iph->payload_len = 0;
1663 iph->nexthdr = IPPROTO_NONE;
1664 iph->hop_limit = 0;
1665 ipv6_addr_copy(&iph->saddr, &rt->rt6i_src.addr);
1666 ipv6_addr_copy(&iph->daddr, &rt->rt6i_dst.addr);
1667
1668 err = ip6mr_cache_unresolved(vif, skb2);
1669 read_unlock(&mrt_lock);
1670
1671 return err;
1672 }
1673
1674 if (!nowait && (rtm->rtm_flags&RTM_F_NOTIFY))
1675 cache->mfc_flags |= MFC_NOTIFY;
1676
1677 err = ip6mr_fill_mroute(skb, cache, rtm);
1678 read_unlock(&mrt_lock);
1679 return err;
1680}
1681