blob: dfba9fd0c248842a2b023c415e88e2c068fb32d0 [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
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700300 seq_printf(seq, "%pI6 %pI6 %-3d %8ld %8ld %8ld",
Harvey Harrison0c6ce782008-10-28 16:09:23 -0700301 &mfc->mf6c_mcastgrp, &mfc->mf6c_origin,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900302 mfc->mf6c_parent,
303 mfc->mfc_un.res.pkt,
304 mfc->mfc_un.res.bytes,
305 mfc->mfc_un.res.wrong_if);
306
307 if (it->cache != &mfc_unres_queue) {
308 for (n = mfc->mfc_un.res.minvif;
309 n < mfc->mfc_un.res.maxvif; n++) {
310 if (MIF_EXISTS(n) &&
311 mfc->mfc_un.res.ttls[n] < 255)
312 seq_printf(seq,
313 " %2d:%-3d",
314 n, mfc->mfc_un.res.ttls[n]);
315 }
316 }
317 seq_putc(seq, '\n');
318 }
319 return 0;
320}
321
322static struct seq_operations ipmr_mfc_seq_ops = {
323 .start = ipmr_mfc_seq_start,
324 .next = ipmr_mfc_seq_next,
325 .stop = ipmr_mfc_seq_stop,
326 .show = ipmr_mfc_seq_show,
327};
328
329static int ipmr_mfc_open(struct inode *inode, struct file *file)
330{
331 return seq_open_private(file, &ipmr_mfc_seq_ops,
332 sizeof(struct ipmr_mfc_iter));
333}
334
335static struct file_operations ip6mr_mfc_fops = {
336 .owner = THIS_MODULE,
337 .open = ipmr_mfc_open,
338 .read = seq_read,
339 .llseek = seq_lseek,
Benjamin Theryeedd7262008-11-20 04:16:12 -0800340 .release = seq_release_private,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900341};
342#endif
343
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900344#ifdef CONFIG_IPV6_PIMSM_V2
345static int reg_vif_num = -1;
346
347static int pim6_rcv(struct sk_buff *skb)
348{
349 struct pimreghdr *pim;
350 struct ipv6hdr *encap;
351 struct net_device *reg_dev = NULL;
352
353 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(*encap)))
354 goto drop;
355
356 pim = (struct pimreghdr *)skb_transport_header(skb);
357 if (pim->type != ((PIM_VERSION << 4) | PIM_REGISTER) ||
358 (pim->flags & PIM_NULL_REGISTER) ||
359 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
Al Viroec6b4862008-04-26 22:28:58 -0700360 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900361 goto drop;
362
363 /* check if the inner packet is destined to mcast group */
364 encap = (struct ipv6hdr *)(skb_transport_header(skb) +
365 sizeof(*pim));
366
367 if (!ipv6_addr_is_multicast(&encap->daddr) ||
368 encap->payload_len == 0 ||
369 ntohs(encap->payload_len) + sizeof(*pim) > skb->len)
370 goto drop;
371
372 read_lock(&mrt_lock);
373 if (reg_vif_num >= 0)
374 reg_dev = vif6_table[reg_vif_num].dev;
375 if (reg_dev)
376 dev_hold(reg_dev);
377 read_unlock(&mrt_lock);
378
379 if (reg_dev == NULL)
380 goto drop;
381
382 skb->mac_header = skb->network_header;
383 skb_pull(skb, (u8 *)encap - skb->data);
384 skb_reset_network_header(skb);
385 skb->dev = reg_dev;
386 skb->protocol = htons(ETH_P_IP);
387 skb->ip_summed = 0;
388 skb->pkt_type = PACKET_HOST;
389 dst_release(skb->dst);
Pavel Emelyanovdc58c782008-05-21 14:17:54 -0700390 reg_dev->stats.rx_bytes += skb->len;
391 reg_dev->stats.rx_packets++;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900392 skb->dst = NULL;
393 nf_reset(skb);
394 netif_rx(skb);
395 dev_put(reg_dev);
396 return 0;
397 drop:
398 kfree_skb(skb);
399 return 0;
400}
401
402static struct inet6_protocol pim6_protocol = {
403 .handler = pim6_rcv,
404};
405
406/* Service routines creating virtual interfaces: PIMREG */
407
408static int reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
409{
410 read_lock(&mrt_lock);
Pavel Emelyanovdc58c782008-05-21 14:17:54 -0700411 dev->stats.tx_bytes += skb->len;
412 dev->stats.tx_packets++;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900413 ip6mr_cache_report(skb, reg_vif_num, MRT6MSG_WHOLEPKT);
414 read_unlock(&mrt_lock);
415 kfree_skb(skb);
416 return 0;
417}
418
Stephen Hemminger007c3832008-11-20 20:28:35 -0800419static const struct net_device_ops reg_vif_netdev_ops = {
420 .ndo_start_xmit = reg_vif_xmit,
421};
422
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900423static void reg_vif_setup(struct net_device *dev)
424{
425 dev->type = ARPHRD_PIMREG;
426 dev->mtu = 1500 - sizeof(struct ipv6hdr) - 8;
427 dev->flags = IFF_NOARP;
Stephen Hemminger007c3832008-11-20 20:28:35 -0800428 dev->netdev_ops = &reg_vif_netdev_ops;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900429 dev->destructor = free_netdev;
430}
431
432static struct net_device *ip6mr_reg_vif(void)
433{
434 struct net_device *dev;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900435
Pavel Emelyanovdc58c782008-05-21 14:17:54 -0700436 dev = alloc_netdev(0, "pim6reg", reg_vif_setup);
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900437 if (dev == NULL)
438 return NULL;
439
440 if (register_netdevice(dev)) {
441 free_netdev(dev);
442 return NULL;
443 }
444 dev->iflink = 0;
445
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900446 if (dev_open(dev))
447 goto failure;
448
Wang Chen7af3db72008-07-14 20:54:54 -0700449 dev_hold(dev);
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900450 return dev;
451
452failure:
453 /* allow the register to be completed before unregistering. */
454 rtnl_unlock();
455 rtnl_lock();
456
457 unregister_netdevice(dev);
458 return NULL;
459}
460#endif
461
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900462/*
463 * Delete a VIF entry
464 */
465
466static int mif6_delete(int vifi)
467{
468 struct mif_device *v;
469 struct net_device *dev;
470 if (vifi < 0 || vifi >= maxvif)
471 return -EADDRNOTAVAIL;
472
473 v = &vif6_table[vifi];
474
475 write_lock_bh(&mrt_lock);
476 dev = v->dev;
477 v->dev = NULL;
478
479 if (!dev) {
480 write_unlock_bh(&mrt_lock);
481 return -EADDRNOTAVAIL;
482 }
483
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900484#ifdef CONFIG_IPV6_PIMSM_V2
485 if (vifi == reg_vif_num)
486 reg_vif_num = -1;
487#endif
488
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900489 if (vifi + 1 == maxvif) {
490 int tmp;
491 for (tmp = vifi - 1; tmp >= 0; tmp--) {
492 if (MIF_EXISTS(tmp))
493 break;
494 }
495 maxvif = tmp + 1;
496 }
497
498 write_unlock_bh(&mrt_lock);
499
500 dev_set_allmulti(dev, -1);
501
502 if (v->flags & MIFF_REGISTER)
503 unregister_netdevice(dev);
504
505 dev_put(dev);
506 return 0;
507}
508
509/* Destroy an unresolved cache entry, killing queued skbs
510 and reporting error to netlink readers.
511 */
512
513static void ip6mr_destroy_unres(struct mfc6_cache *c)
514{
515 struct sk_buff *skb;
516
517 atomic_dec(&cache_resolve_queue_len);
518
519 while((skb = skb_dequeue(&c->mfc_un.unres.unresolved)) != NULL) {
520 if (ipv6_hdr(skb)->version == 0) {
521 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct ipv6hdr));
522 nlh->nlmsg_type = NLMSG_ERROR;
523 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
524 skb_trim(skb, nlh->nlmsg_len);
525 ((struct nlmsgerr *)NLMSG_DATA(nlh))->error = -ETIMEDOUT;
526 rtnl_unicast(skb, &init_net, NETLINK_CB(skb).pid);
527 } else
528 kfree_skb(skb);
529 }
530
531 kmem_cache_free(mrt_cachep, c);
532}
533
534
535/* Single timer process for all the unresolved queue. */
536
537static void ipmr_do_expire_process(unsigned long dummy)
538{
539 unsigned long now = jiffies;
540 unsigned long expires = 10 * HZ;
541 struct mfc6_cache *c, **cp;
542
543 cp = &mfc_unres_queue;
544
545 while ((c = *cp) != NULL) {
546 if (time_after(c->mfc_un.unres.expires, now)) {
547 /* not yet... */
548 unsigned long interval = c->mfc_un.unres.expires - now;
549 if (interval < expires)
550 expires = interval;
551 cp = &c->next;
552 continue;
553 }
554
555 *cp = c->next;
556 ip6mr_destroy_unres(c);
557 }
558
559 if (atomic_read(&cache_resolve_queue_len))
560 mod_timer(&ipmr_expire_timer, jiffies + expires);
561}
562
563static void ipmr_expire_process(unsigned long dummy)
564{
565 if (!spin_trylock(&mfc_unres_lock)) {
566 mod_timer(&ipmr_expire_timer, jiffies + 1);
567 return;
568 }
569
570 if (atomic_read(&cache_resolve_queue_len))
571 ipmr_do_expire_process(dummy);
572
573 spin_unlock(&mfc_unres_lock);
574}
575
576/* Fill oifs list. It is called under write locked mrt_lock. */
577
578static void ip6mr_update_thresholds(struct mfc6_cache *cache, unsigned char *ttls)
579{
580 int vifi;
581
Rami Rosen6ac7eb02008-04-10 12:40:10 +0300582 cache->mfc_un.res.minvif = MAXMIFS;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900583 cache->mfc_un.res.maxvif = 0;
Rami Rosen6ac7eb02008-04-10 12:40:10 +0300584 memset(cache->mfc_un.res.ttls, 255, MAXMIFS);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900585
586 for (vifi = 0; vifi < maxvif; vifi++) {
587 if (MIF_EXISTS(vifi) && ttls[vifi] && ttls[vifi] < 255) {
588 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
589 if (cache->mfc_un.res.minvif > vifi)
590 cache->mfc_un.res.minvif = vifi;
591 if (cache->mfc_un.res.maxvif <= vifi)
592 cache->mfc_un.res.maxvif = vifi + 1;
593 }
594 }
595}
596
597static int mif6_add(struct mif6ctl *vifc, int mrtsock)
598{
599 int vifi = vifc->mif6c_mifi;
600 struct mif_device *v = &vif6_table[vifi];
601 struct net_device *dev;
Wang Chen5ae7b442008-07-14 20:54:23 -0700602 int err;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900603
604 /* Is vif busy ? */
605 if (MIF_EXISTS(vifi))
606 return -EADDRINUSE;
607
608 switch (vifc->mif6c_flags) {
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900609#ifdef CONFIG_IPV6_PIMSM_V2
610 case MIFF_REGISTER:
611 /*
612 * Special Purpose VIF in PIM
613 * All the packets will be sent to the daemon
614 */
615 if (reg_vif_num >= 0)
616 return -EADDRINUSE;
617 dev = ip6mr_reg_vif();
618 if (!dev)
619 return -ENOBUFS;
Wang Chen5ae7b442008-07-14 20:54:23 -0700620 err = dev_set_allmulti(dev, 1);
621 if (err) {
622 unregister_netdevice(dev);
Wang Chen7af3db72008-07-14 20:54:54 -0700623 dev_put(dev);
Wang Chen5ae7b442008-07-14 20:54:23 -0700624 return err;
625 }
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900626 break;
627#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900628 case 0:
629 dev = dev_get_by_index(&init_net, vifc->mif6c_pifi);
630 if (!dev)
631 return -EADDRNOTAVAIL;
Wang Chen5ae7b442008-07-14 20:54:23 -0700632 err = dev_set_allmulti(dev, 1);
Wang Chen7af3db72008-07-14 20:54:54 -0700633 if (err) {
634 dev_put(dev);
Wang Chen5ae7b442008-07-14 20:54:23 -0700635 return err;
Wang Chen7af3db72008-07-14 20:54:54 -0700636 }
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900637 break;
638 default:
639 return -EINVAL;
640 }
641
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900642 /*
643 * Fill in the VIF structures
644 */
645 v->rate_limit = vifc->vifc_rate_limit;
646 v->flags = vifc->mif6c_flags;
647 if (!mrtsock)
648 v->flags |= VIFF_STATIC;
649 v->threshold = vifc->vifc_threshold;
650 v->bytes_in = 0;
651 v->bytes_out = 0;
652 v->pkt_in = 0;
653 v->pkt_out = 0;
654 v->link = dev->ifindex;
655 if (v->flags & MIFF_REGISTER)
656 v->link = dev->iflink;
657
658 /* And finish update writing critical data */
659 write_lock_bh(&mrt_lock);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900660 v->dev = dev;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900661#ifdef CONFIG_IPV6_PIMSM_V2
662 if (v->flags & MIFF_REGISTER)
663 reg_vif_num = vifi;
664#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900665 if (vifi + 1 > maxvif)
666 maxvif = vifi + 1;
667 write_unlock_bh(&mrt_lock);
668 return 0;
669}
670
671static struct mfc6_cache *ip6mr_cache_find(struct in6_addr *origin, struct in6_addr *mcastgrp)
672{
673 int line = MFC6_HASH(mcastgrp, origin);
674 struct mfc6_cache *c;
675
676 for (c = mfc6_cache_array[line]; c; c = c->next) {
677 if (ipv6_addr_equal(&c->mf6c_origin, origin) &&
678 ipv6_addr_equal(&c->mf6c_mcastgrp, mcastgrp))
679 break;
680 }
681 return c;
682}
683
684/*
685 * Allocate a multicast cache entry
686 */
687static struct mfc6_cache *ip6mr_cache_alloc(void)
688{
689 struct mfc6_cache *c = kmem_cache_alloc(mrt_cachep, GFP_KERNEL);
690 if (c == NULL)
691 return NULL;
692 memset(c, 0, sizeof(*c));
Rami Rosen6ac7eb02008-04-10 12:40:10 +0300693 c->mfc_un.res.minvif = MAXMIFS;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900694 return c;
695}
696
697static struct mfc6_cache *ip6mr_cache_alloc_unres(void)
698{
699 struct mfc6_cache *c = kmem_cache_alloc(mrt_cachep, GFP_ATOMIC);
700 if (c == NULL)
701 return NULL;
702 memset(c, 0, sizeof(*c));
703 skb_queue_head_init(&c->mfc_un.unres.unresolved);
704 c->mfc_un.unres.expires = jiffies + 10 * HZ;
705 return c;
706}
707
708/*
709 * A cache entry has gone into a resolved state from queued
710 */
711
712static void ip6mr_cache_resolve(struct mfc6_cache *uc, struct mfc6_cache *c)
713{
714 struct sk_buff *skb;
715
716 /*
717 * Play the pending entries through our router
718 */
719
720 while((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
721 if (ipv6_hdr(skb)->version == 0) {
722 int err;
723 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct ipv6hdr));
724
725 if (ip6mr_fill_mroute(skb, c, NLMSG_DATA(nlh)) > 0) {
YOSHIFUJI Hideaki549e0282008-04-05 22:17:39 +0900726 nlh->nlmsg_len = skb_tail_pointer(skb) - (u8 *)nlh;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900727 } else {
728 nlh->nlmsg_type = NLMSG_ERROR;
729 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
730 skb_trim(skb, nlh->nlmsg_len);
731 ((struct nlmsgerr *)NLMSG_DATA(nlh))->error = -EMSGSIZE;
732 }
733 err = rtnl_unicast(skb, &init_net, NETLINK_CB(skb).pid);
734 } else
735 ip6_mr_forward(skb, c);
736 }
737}
738
739/*
740 * Bounce a cache query up to pim6sd. We could use netlink for this but pim6sd
741 * expects the following bizarre scheme.
742 *
743 * Called under mrt_lock.
744 */
745
Rami Rosen6ac7eb02008-04-10 12:40:10 +0300746static int ip6mr_cache_report(struct sk_buff *pkt, mifi_t mifi, int assert)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900747{
748 struct sk_buff *skb;
749 struct mrt6msg *msg;
750 int ret;
751
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900752#ifdef CONFIG_IPV6_PIMSM_V2
753 if (assert == MRT6MSG_WHOLEPKT)
754 skb = skb_realloc_headroom(pkt, -skb_network_offset(pkt)
755 +sizeof(*msg));
756 else
757#endif
758 skb = alloc_skb(sizeof(struct ipv6hdr) + sizeof(*msg), GFP_ATOMIC);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900759
760 if (!skb)
761 return -ENOBUFS;
762
763 /* I suppose that internal messages
764 * do not require checksums */
765
766 skb->ip_summed = CHECKSUM_UNNECESSARY;
767
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900768#ifdef CONFIG_IPV6_PIMSM_V2
769 if (assert == MRT6MSG_WHOLEPKT) {
770 /* Ugly, but we have no choice with this interface.
771 Duplicate old header, fix length etc.
772 And all this only to mangle msg->im6_msgtype and
773 to set msg->im6_mbz to "mbz" :-)
774 */
775 skb_push(skb, -skb_network_offset(pkt));
776
777 skb_push(skb, sizeof(*msg));
778 skb_reset_transport_header(skb);
779 msg = (struct mrt6msg *)skb_transport_header(skb);
780 msg->im6_mbz = 0;
781 msg->im6_msgtype = MRT6MSG_WHOLEPKT;
782 msg->im6_mif = reg_vif_num;
783 msg->im6_pad = 0;
784 ipv6_addr_copy(&msg->im6_src, &ipv6_hdr(pkt)->saddr);
785 ipv6_addr_copy(&msg->im6_dst, &ipv6_hdr(pkt)->daddr);
786
787 skb->ip_summed = CHECKSUM_UNNECESSARY;
788 } else
789#endif
790 {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900791 /*
792 * Copy the IP header
793 */
794
795 skb_put(skb, sizeof(struct ipv6hdr));
796 skb_reset_network_header(skb);
797 skb_copy_to_linear_data(skb, ipv6_hdr(pkt), sizeof(struct ipv6hdr));
798
799 /*
800 * Add our header
801 */
802 skb_put(skb, sizeof(*msg));
803 skb_reset_transport_header(skb);
804 msg = (struct mrt6msg *)skb_transport_header(skb);
805
806 msg->im6_mbz = 0;
807 msg->im6_msgtype = assert;
Rami Rosen6ac7eb02008-04-10 12:40:10 +0300808 msg->im6_mif = mifi;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900809 msg->im6_pad = 0;
810 ipv6_addr_copy(&msg->im6_src, &ipv6_hdr(pkt)->saddr);
811 ipv6_addr_copy(&msg->im6_dst, &ipv6_hdr(pkt)->daddr);
812
813 skb->dst = dst_clone(pkt->dst);
814 skb->ip_summed = CHECKSUM_UNNECESSARY;
815
816 skb_pull(skb, sizeof(struct ipv6hdr));
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +0900817 }
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900818
819 if (mroute6_socket == NULL) {
820 kfree_skb(skb);
821 return -EINVAL;
822 }
823
824 /*
825 * Deliver to user space multicast routing algorithms
826 */
827 if ((ret = sock_queue_rcv_skb(mroute6_socket, skb)) < 0) {
828 if (net_ratelimit())
829 printk(KERN_WARNING "mroute6: pending queue full, dropping entries.\n");
830 kfree_skb(skb);
831 }
832
833 return ret;
834}
835
836/*
837 * Queue a packet for resolution. It gets locked cache entry!
838 */
839
840static int
Rami Rosen6ac7eb02008-04-10 12:40:10 +0300841ip6mr_cache_unresolved(mifi_t mifi, struct sk_buff *skb)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900842{
843 int err;
844 struct mfc6_cache *c;
845
846 spin_lock_bh(&mfc_unres_lock);
847 for (c = mfc_unres_queue; c; c = c->next) {
848 if (ipv6_addr_equal(&c->mf6c_mcastgrp, &ipv6_hdr(skb)->daddr) &&
849 ipv6_addr_equal(&c->mf6c_origin, &ipv6_hdr(skb)->saddr))
850 break;
851 }
852
853 if (c == NULL) {
854 /*
855 * Create a new entry if allowable
856 */
857
858 if (atomic_read(&cache_resolve_queue_len) >= 10 ||
859 (c = ip6mr_cache_alloc_unres()) == NULL) {
860 spin_unlock_bh(&mfc_unres_lock);
861
862 kfree_skb(skb);
863 return -ENOBUFS;
864 }
865
866 /*
867 * Fill in the new cache entry
868 */
869 c->mf6c_parent = -1;
870 c->mf6c_origin = ipv6_hdr(skb)->saddr;
871 c->mf6c_mcastgrp = ipv6_hdr(skb)->daddr;
872
873 /*
874 * Reflect first query at pim6sd
875 */
Rami Rosen6ac7eb02008-04-10 12:40:10 +0300876 if ((err = ip6mr_cache_report(skb, mifi, MRT6MSG_NOCACHE)) < 0) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900877 /* If the report failed throw the cache entry
878 out - Brad Parker
879 */
880 spin_unlock_bh(&mfc_unres_lock);
881
882 kmem_cache_free(mrt_cachep, c);
883 kfree_skb(skb);
884 return err;
885 }
886
887 atomic_inc(&cache_resolve_queue_len);
888 c->next = mfc_unres_queue;
889 mfc_unres_queue = c;
890
891 ipmr_do_expire_process(1);
892 }
893
894 /*
895 * See if we can append the packet
896 */
897 if (c->mfc_un.unres.unresolved.qlen > 3) {
898 kfree_skb(skb);
899 err = -ENOBUFS;
900 } else {
901 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
902 err = 0;
903 }
904
905 spin_unlock_bh(&mfc_unres_lock);
906 return err;
907}
908
909/*
910 * MFC6 cache manipulation by user space
911 */
912
913static int ip6mr_mfc_delete(struct mf6cctl *mfc)
914{
915 int line;
916 struct mfc6_cache *c, **cp;
917
918 line = MFC6_HASH(&mfc->mf6cc_mcastgrp.sin6_addr, &mfc->mf6cc_origin.sin6_addr);
919
920 for (cp = &mfc6_cache_array[line]; (c = *cp) != NULL; cp = &c->next) {
921 if (ipv6_addr_equal(&c->mf6c_origin, &mfc->mf6cc_origin.sin6_addr) &&
922 ipv6_addr_equal(&c->mf6c_mcastgrp, &mfc->mf6cc_mcastgrp.sin6_addr)) {
923 write_lock_bh(&mrt_lock);
924 *cp = c->next;
925 write_unlock_bh(&mrt_lock);
926
927 kmem_cache_free(mrt_cachep, c);
928 return 0;
929 }
930 }
931 return -ENOENT;
932}
933
934static int ip6mr_device_event(struct notifier_block *this,
935 unsigned long event, void *ptr)
936{
937 struct net_device *dev = ptr;
938 struct mif_device *v;
939 int ct;
940
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -0700941 if (!net_eq(dev_net(dev), &init_net))
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900942 return NOTIFY_DONE;
943
944 if (event != NETDEV_UNREGISTER)
945 return NOTIFY_DONE;
946
947 v = &vif6_table[0];
948 for (ct = 0; ct < maxvif; ct++, v++) {
949 if (v->dev == dev)
950 mif6_delete(ct);
951 }
952 return NOTIFY_DONE;
953}
954
955static struct notifier_block ip6_mr_notifier = {
956 .notifier_call = ip6mr_device_event
957};
958
959/*
960 * Setup for IP multicast routing
961 */
962
Wang Chen623d1a12008-07-03 12:13:30 +0800963int __init ip6_mr_init(void)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900964{
Wang Chen623d1a12008-07-03 12:13:30 +0800965 int err;
966
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900967 mrt_cachep = kmem_cache_create("ip6_mrt_cache",
968 sizeof(struct mfc6_cache),
969 0, SLAB_HWCACHE_ALIGN,
970 NULL);
971 if (!mrt_cachep)
Wang Chen623d1a12008-07-03 12:13:30 +0800972 return -ENOMEM;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900973
974 setup_timer(&ipmr_expire_timer, ipmr_expire_process, 0);
Wang Chen623d1a12008-07-03 12:13:30 +0800975 err = register_netdevice_notifier(&ip6_mr_notifier);
976 if (err)
977 goto reg_notif_fail;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900978#ifdef CONFIG_PROC_FS
Wang Chen623d1a12008-07-03 12:13:30 +0800979 err = -ENOMEM;
980 if (!proc_net_fops_create(&init_net, "ip6_mr_vif", 0, &ip6mr_vif_fops))
981 goto proc_vif_fail;
982 if (!proc_net_fops_create(&init_net, "ip6_mr_cache",
983 0, &ip6mr_mfc_fops))
984 goto proc_cache_fail;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900985#endif
Wang Chen623d1a12008-07-03 12:13:30 +0800986 return 0;
Wang Chen623d1a12008-07-03 12:13:30 +0800987#ifdef CONFIG_PROC_FS
Wang Chen623d1a12008-07-03 12:13:30 +0800988proc_cache_fail:
989 proc_net_remove(&init_net, "ip6_mr_vif");
Benjamin Thery87b30a62008-11-10 16:34:11 -0800990proc_vif_fail:
991 unregister_netdevice_notifier(&ip6_mr_notifier);
Wang Chen623d1a12008-07-03 12:13:30 +0800992#endif
Benjamin Thery87b30a62008-11-10 16:34:11 -0800993reg_notif_fail:
994 del_timer(&ipmr_expire_timer);
995 kmem_cache_destroy(mrt_cachep);
Wang Chen623d1a12008-07-03 12:13:30 +0800996 return err;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900997}
998
Wang Chen623d1a12008-07-03 12:13:30 +0800999void ip6_mr_cleanup(void)
1000{
1001#ifdef CONFIG_PROC_FS
1002 proc_net_remove(&init_net, "ip6_mr_cache");
1003 proc_net_remove(&init_net, "ip6_mr_vif");
1004#endif
1005 unregister_netdevice_notifier(&ip6_mr_notifier);
1006 del_timer(&ipmr_expire_timer);
1007 kmem_cache_destroy(mrt_cachep);
1008}
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001009
1010static int ip6mr_mfc_add(struct mf6cctl *mfc, int mrtsock)
1011{
1012 int line;
1013 struct mfc6_cache *uc, *c, **cp;
Rami Rosen6ac7eb02008-04-10 12:40:10 +03001014 unsigned char ttls[MAXMIFS];
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001015 int i;
1016
Rami Rosen6ac7eb02008-04-10 12:40:10 +03001017 memset(ttls, 255, MAXMIFS);
1018 for (i = 0; i < MAXMIFS; i++) {
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001019 if (IF_ISSET(i, &mfc->mf6cc_ifset))
1020 ttls[i] = 1;
1021
1022 }
1023
1024 line = MFC6_HASH(&mfc->mf6cc_mcastgrp.sin6_addr, &mfc->mf6cc_origin.sin6_addr);
1025
1026 for (cp = &mfc6_cache_array[line]; (c = *cp) != NULL; cp = &c->next) {
1027 if (ipv6_addr_equal(&c->mf6c_origin, &mfc->mf6cc_origin.sin6_addr) &&
1028 ipv6_addr_equal(&c->mf6c_mcastgrp, &mfc->mf6cc_mcastgrp.sin6_addr))
1029 break;
1030 }
1031
1032 if (c != NULL) {
1033 write_lock_bh(&mrt_lock);
1034 c->mf6c_parent = mfc->mf6cc_parent;
1035 ip6mr_update_thresholds(c, ttls);
1036 if (!mrtsock)
1037 c->mfc_flags |= MFC_STATIC;
1038 write_unlock_bh(&mrt_lock);
1039 return 0;
1040 }
1041
1042 if (!ipv6_addr_is_multicast(&mfc->mf6cc_mcastgrp.sin6_addr))
1043 return -EINVAL;
1044
1045 c = ip6mr_cache_alloc();
1046 if (c == NULL)
1047 return -ENOMEM;
1048
1049 c->mf6c_origin = mfc->mf6cc_origin.sin6_addr;
1050 c->mf6c_mcastgrp = mfc->mf6cc_mcastgrp.sin6_addr;
1051 c->mf6c_parent = mfc->mf6cc_parent;
1052 ip6mr_update_thresholds(c, ttls);
1053 if (!mrtsock)
1054 c->mfc_flags |= MFC_STATIC;
1055
1056 write_lock_bh(&mrt_lock);
1057 c->next = mfc6_cache_array[line];
1058 mfc6_cache_array[line] = c;
1059 write_unlock_bh(&mrt_lock);
1060
1061 /*
1062 * Check to see if we resolved a queued list. If so we
1063 * need to send on the frames and tidy up.
1064 */
1065 spin_lock_bh(&mfc_unres_lock);
1066 for (cp = &mfc_unres_queue; (uc = *cp) != NULL;
1067 cp = &uc->next) {
1068 if (ipv6_addr_equal(&uc->mf6c_origin, &c->mf6c_origin) &&
1069 ipv6_addr_equal(&uc->mf6c_mcastgrp, &c->mf6c_mcastgrp)) {
1070 *cp = uc->next;
1071 if (atomic_dec_and_test(&cache_resolve_queue_len))
1072 del_timer(&ipmr_expire_timer);
1073 break;
1074 }
1075 }
1076 spin_unlock_bh(&mfc_unres_lock);
1077
1078 if (uc) {
1079 ip6mr_cache_resolve(uc, c);
1080 kmem_cache_free(mrt_cachep, uc);
1081 }
1082 return 0;
1083}
1084
1085/*
1086 * Close the multicast socket, and clear the vif tables etc
1087 */
1088
1089static void mroute_clean_tables(struct sock *sk)
1090{
1091 int i;
1092
1093 /*
1094 * Shut down all active vif entries
1095 */
1096 for (i = 0; i < maxvif; i++) {
1097 if (!(vif6_table[i].flags & VIFF_STATIC))
1098 mif6_delete(i);
1099 }
1100
1101 /*
1102 * Wipe the cache
1103 */
1104 for (i = 0; i < ARRAY_SIZE(mfc6_cache_array); i++) {
1105 struct mfc6_cache *c, **cp;
1106
1107 cp = &mfc6_cache_array[i];
1108 while ((c = *cp) != NULL) {
1109 if (c->mfc_flags & MFC_STATIC) {
1110 cp = &c->next;
1111 continue;
1112 }
1113 write_lock_bh(&mrt_lock);
1114 *cp = c->next;
1115 write_unlock_bh(&mrt_lock);
1116
1117 kmem_cache_free(mrt_cachep, c);
1118 }
1119 }
1120
1121 if (atomic_read(&cache_resolve_queue_len) != 0) {
1122 struct mfc6_cache *c;
1123
1124 spin_lock_bh(&mfc_unres_lock);
1125 while (mfc_unres_queue != NULL) {
1126 c = mfc_unres_queue;
1127 mfc_unres_queue = c->next;
1128 spin_unlock_bh(&mfc_unres_lock);
1129
1130 ip6mr_destroy_unres(c);
1131
1132 spin_lock_bh(&mfc_unres_lock);
1133 }
1134 spin_unlock_bh(&mfc_unres_lock);
1135 }
1136}
1137
1138static int ip6mr_sk_init(struct sock *sk)
1139{
1140 int err = 0;
1141
1142 rtnl_lock();
1143 write_lock_bh(&mrt_lock);
1144 if (likely(mroute6_socket == NULL))
1145 mroute6_socket = sk;
1146 else
1147 err = -EADDRINUSE;
1148 write_unlock_bh(&mrt_lock);
1149
1150 rtnl_unlock();
1151
1152 return err;
1153}
1154
1155int ip6mr_sk_done(struct sock *sk)
1156{
1157 int err = 0;
1158
1159 rtnl_lock();
1160 if (sk == mroute6_socket) {
1161 write_lock_bh(&mrt_lock);
1162 mroute6_socket = NULL;
1163 write_unlock_bh(&mrt_lock);
1164
1165 mroute_clean_tables(sk);
1166 } else
1167 err = -EACCES;
1168 rtnl_unlock();
1169
1170 return err;
1171}
1172
1173/*
1174 * Socket options and virtual interface manipulation. The whole
1175 * virtual interface system is a complete heap, but unfortunately
1176 * that's how BSD mrouted happens to think. Maybe one day with a proper
1177 * MOSPF/PIM router set up we can clean this up.
1178 */
1179
1180int ip6_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, int optlen)
1181{
1182 int ret;
1183 struct mif6ctl vif;
1184 struct mf6cctl mfc;
1185 mifi_t mifi;
1186
1187 if (optname != MRT6_INIT) {
1188 if (sk != mroute6_socket && !capable(CAP_NET_ADMIN))
1189 return -EACCES;
1190 }
1191
1192 switch (optname) {
1193 case MRT6_INIT:
1194 if (sk->sk_type != SOCK_RAW ||
1195 inet_sk(sk)->num != IPPROTO_ICMPV6)
1196 return -EOPNOTSUPP;
1197 if (optlen < sizeof(int))
1198 return -EINVAL;
1199
1200 return ip6mr_sk_init(sk);
1201
1202 case MRT6_DONE:
1203 return ip6mr_sk_done(sk);
1204
1205 case MRT6_ADD_MIF:
1206 if (optlen < sizeof(vif))
1207 return -EINVAL;
1208 if (copy_from_user(&vif, optval, sizeof(vif)))
1209 return -EFAULT;
Rami Rosen6ac7eb02008-04-10 12:40:10 +03001210 if (vif.mif6c_mifi >= MAXMIFS)
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001211 return -ENFILE;
1212 rtnl_lock();
1213 ret = mif6_add(&vif, sk == mroute6_socket);
1214 rtnl_unlock();
1215 return ret;
1216
1217 case MRT6_DEL_MIF:
1218 if (optlen < sizeof(mifi_t))
1219 return -EINVAL;
1220 if (copy_from_user(&mifi, optval, sizeof(mifi_t)))
1221 return -EFAULT;
1222 rtnl_lock();
1223 ret = mif6_delete(mifi);
1224 rtnl_unlock();
1225 return ret;
1226
1227 /*
1228 * Manipulate the forwarding caches. These live
1229 * in a sort of kernel/user symbiosis.
1230 */
1231 case MRT6_ADD_MFC:
1232 case MRT6_DEL_MFC:
1233 if (optlen < sizeof(mfc))
1234 return -EINVAL;
1235 if (copy_from_user(&mfc, optval, sizeof(mfc)))
1236 return -EFAULT;
1237 rtnl_lock();
1238 if (optname == MRT6_DEL_MFC)
1239 ret = ip6mr_mfc_delete(&mfc);
1240 else
1241 ret = ip6mr_mfc_add(&mfc, sk == mroute6_socket);
1242 rtnl_unlock();
1243 return ret;
1244
1245 /*
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001246 * Control PIM assert (to activate pim will activate assert)
1247 */
1248 case MRT6_ASSERT:
1249 {
1250 int v;
1251 if (get_user(v, (int __user *)optval))
1252 return -EFAULT;
1253 mroute_do_assert = !!v;
1254 return 0;
1255 }
1256
1257#ifdef CONFIG_IPV6_PIMSM_V2
1258 case MRT6_PIM:
1259 {
YOSHIFUJI Hideakia9f83bf2008-04-10 15:41:28 +09001260 int v;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001261 if (get_user(v, (int __user *)optval))
1262 return -EFAULT;
1263 v = !!v;
1264 rtnl_lock();
1265 ret = 0;
1266 if (v != mroute_do_pim) {
1267 mroute_do_pim = v;
1268 mroute_do_assert = v;
1269 if (mroute_do_pim)
1270 ret = inet6_add_protocol(&pim6_protocol,
1271 IPPROTO_PIM);
1272 else
1273 ret = inet6_del_protocol(&pim6_protocol,
1274 IPPROTO_PIM);
1275 if (ret < 0)
1276 ret = -EAGAIN;
1277 }
1278 rtnl_unlock();
1279 return ret;
1280 }
1281
1282#endif
1283 /*
Rami Rosen7d120c52008-04-23 14:35:13 +03001284 * Spurious command, or MRT6_VERSION which you cannot
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001285 * set.
1286 */
1287 default:
1288 return -ENOPROTOOPT;
1289 }
1290}
1291
1292/*
1293 * Getsock opt support for the multicast routing system.
1294 */
1295
1296int ip6_mroute_getsockopt(struct sock *sk, int optname, char __user *optval,
1297 int __user *optlen)
1298{
1299 int olr;
1300 int val;
1301
1302 switch (optname) {
1303 case MRT6_VERSION:
1304 val = 0x0305;
1305 break;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001306#ifdef CONFIG_IPV6_PIMSM_V2
1307 case MRT6_PIM:
1308 val = mroute_do_pim;
1309 break;
1310#endif
1311 case MRT6_ASSERT:
1312 val = mroute_do_assert;
1313 break;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001314 default:
1315 return -ENOPROTOOPT;
1316 }
1317
1318 if (get_user(olr, optlen))
1319 return -EFAULT;
1320
1321 olr = min_t(int, olr, sizeof(int));
1322 if (olr < 0)
1323 return -EINVAL;
1324
1325 if (put_user(olr, optlen))
1326 return -EFAULT;
1327 if (copy_to_user(optval, &val, olr))
1328 return -EFAULT;
1329 return 0;
1330}
1331
1332/*
1333 * The IP multicast ioctl support routines.
1334 */
1335
1336int ip6mr_ioctl(struct sock *sk, int cmd, void __user *arg)
1337{
1338 struct sioc_sg_req6 sr;
1339 struct sioc_mif_req6 vr;
1340 struct mif_device *vif;
1341 struct mfc6_cache *c;
1342
1343 switch (cmd) {
1344 case SIOCGETMIFCNT_IN6:
1345 if (copy_from_user(&vr, arg, sizeof(vr)))
1346 return -EFAULT;
1347 if (vr.mifi >= maxvif)
1348 return -EINVAL;
1349 read_lock(&mrt_lock);
1350 vif = &vif6_table[vr.mifi];
1351 if (MIF_EXISTS(vr.mifi)) {
1352 vr.icount = vif->pkt_in;
1353 vr.ocount = vif->pkt_out;
1354 vr.ibytes = vif->bytes_in;
1355 vr.obytes = vif->bytes_out;
1356 read_unlock(&mrt_lock);
1357
1358 if (copy_to_user(arg, &vr, sizeof(vr)))
1359 return -EFAULT;
1360 return 0;
1361 }
1362 read_unlock(&mrt_lock);
1363 return -EADDRNOTAVAIL;
1364 case SIOCGETSGCNT_IN6:
1365 if (copy_from_user(&sr, arg, sizeof(sr)))
1366 return -EFAULT;
1367
1368 read_lock(&mrt_lock);
1369 c = ip6mr_cache_find(&sr.src.sin6_addr, &sr.grp.sin6_addr);
1370 if (c) {
1371 sr.pktcnt = c->mfc_un.res.pkt;
1372 sr.bytecnt = c->mfc_un.res.bytes;
1373 sr.wrong_if = c->mfc_un.res.wrong_if;
1374 read_unlock(&mrt_lock);
1375
1376 if (copy_to_user(arg, &sr, sizeof(sr)))
1377 return -EFAULT;
1378 return 0;
1379 }
1380 read_unlock(&mrt_lock);
1381 return -EADDRNOTAVAIL;
1382 default:
1383 return -ENOIOCTLCMD;
1384 }
1385}
1386
1387
1388static inline int ip6mr_forward2_finish(struct sk_buff *skb)
1389{
Denis V. Lunev483a47d2008-10-08 11:09:27 -07001390 IP6_INC_STATS_BH(dev_net(skb->dst->dev), ip6_dst_idev(skb->dst),
1391 IPSTATS_MIB_OUTFORWDATAGRAMS);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001392 return dst_output(skb);
1393}
1394
1395/*
1396 * Processing handlers for ip6mr_forward
1397 */
1398
1399static int ip6mr_forward2(struct sk_buff *skb, struct mfc6_cache *c, int vifi)
1400{
1401 struct ipv6hdr *ipv6h;
1402 struct mif_device *vif = &vif6_table[vifi];
1403 struct net_device *dev;
1404 struct dst_entry *dst;
1405 struct flowi fl;
1406
1407 if (vif->dev == NULL)
1408 goto out_free;
1409
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001410#ifdef CONFIG_IPV6_PIMSM_V2
1411 if (vif->flags & MIFF_REGISTER) {
1412 vif->pkt_out++;
1413 vif->bytes_out += skb->len;
Pavel Emelyanovdc58c782008-05-21 14:17:54 -07001414 vif->dev->stats.tx_bytes += skb->len;
1415 vif->dev->stats.tx_packets++;
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001416 ip6mr_cache_report(skb, vifi, MRT6MSG_WHOLEPKT);
1417 kfree_skb(skb);
1418 return 0;
1419 }
1420#endif
1421
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001422 ipv6h = ipv6_hdr(skb);
1423
1424 fl = (struct flowi) {
1425 .oif = vif->link,
1426 .nl_u = { .ip6_u =
1427 { .daddr = ipv6h->daddr, }
1428 }
1429 };
1430
1431 dst = ip6_route_output(&init_net, NULL, &fl);
1432 if (!dst)
1433 goto out_free;
1434
1435 dst_release(skb->dst);
1436 skb->dst = dst;
1437
1438 /*
1439 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1440 * not only before forwarding, but after forwarding on all output
1441 * interfaces. It is clear, if mrouter runs a multicasting
1442 * program, it should receive packets not depending to what interface
1443 * program is joined.
1444 * If we will not make it, the program will have to join on all
1445 * interfaces. On the other hand, multihoming host (or router, but
1446 * not mrouter) cannot join to more than one interface - it will
1447 * result in receiving multiple packets.
1448 */
1449 dev = vif->dev;
1450 skb->dev = dev;
1451 vif->pkt_out++;
1452 vif->bytes_out += skb->len;
1453
1454 /* We are about to write */
1455 /* XXX: extension headers? */
1456 if (skb_cow(skb, sizeof(*ipv6h) + LL_RESERVED_SPACE(dev)))
1457 goto out_free;
1458
1459 ipv6h = ipv6_hdr(skb);
1460 ipv6h->hop_limit--;
1461
1462 IP6CB(skb)->flags |= IP6SKB_FORWARDED;
1463
1464 return NF_HOOK(PF_INET6, NF_INET_FORWARD, skb, skb->dev, dev,
1465 ip6mr_forward2_finish);
1466
1467out_free:
1468 kfree_skb(skb);
1469 return 0;
1470}
1471
1472static int ip6mr_find_vif(struct net_device *dev)
1473{
1474 int ct;
1475 for (ct = maxvif - 1; ct >= 0; ct--) {
1476 if (vif6_table[ct].dev == dev)
1477 break;
1478 }
1479 return ct;
1480}
1481
1482static int ip6_mr_forward(struct sk_buff *skb, struct mfc6_cache *cache)
1483{
1484 int psend = -1;
1485 int vif, ct;
1486
1487 vif = cache->mf6c_parent;
1488 cache->mfc_un.res.pkt++;
1489 cache->mfc_un.res.bytes += skb->len;
1490
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001491 /*
1492 * Wrong interface: drop packet and (maybe) send PIM assert.
1493 */
1494 if (vif6_table[vif].dev != skb->dev) {
1495 int true_vifi;
1496
1497 cache->mfc_un.res.wrong_if++;
1498 true_vifi = ip6mr_find_vif(skb->dev);
1499
1500 if (true_vifi >= 0 && mroute_do_assert &&
1501 /* pimsm uses asserts, when switching from RPT to SPT,
1502 so that we cannot check that packet arrived on an oif.
1503 It is bad, but otherwise we would need to move pretty
1504 large chunk of pimd to kernel. Ough... --ANK
1505 */
1506 (mroute_do_pim || cache->mfc_un.res.ttls[true_vifi] < 255) &&
1507 time_after(jiffies,
1508 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1509 cache->mfc_un.res.last_assert = jiffies;
1510 ip6mr_cache_report(skb, true_vifi, MRT6MSG_WRONGMIF);
1511 }
1512 goto dont_forward;
1513 }
1514
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001515 vif6_table[vif].pkt_in++;
1516 vif6_table[vif].bytes_in += skb->len;
1517
1518 /*
1519 * Forward the frame
1520 */
1521 for (ct = cache->mfc_un.res.maxvif - 1; ct >= cache->mfc_un.res.minvif; ct--) {
1522 if (ipv6_hdr(skb)->hop_limit > cache->mfc_un.res.ttls[ct]) {
1523 if (psend != -1) {
1524 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1525 if (skb2)
1526 ip6mr_forward2(skb2, cache, psend);
1527 }
1528 psend = ct;
1529 }
1530 }
1531 if (psend != -1) {
1532 ip6mr_forward2(skb, cache, psend);
1533 return 0;
1534 }
1535
YOSHIFUJI Hideaki14fb64e2008-04-03 09:22:54 +09001536dont_forward:
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001537 kfree_skb(skb);
1538 return 0;
1539}
1540
1541
1542/*
1543 * Multicast packets for forwarding arrive here
1544 */
1545
1546int ip6_mr_input(struct sk_buff *skb)
1547{
1548 struct mfc6_cache *cache;
1549
1550 read_lock(&mrt_lock);
1551 cache = ip6mr_cache_find(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr);
1552
1553 /*
1554 * No usable cache entry
1555 */
1556 if (cache == NULL) {
1557 int vif;
1558
1559 vif = ip6mr_find_vif(skb->dev);
1560 if (vif >= 0) {
1561 int err = ip6mr_cache_unresolved(vif, skb);
1562 read_unlock(&mrt_lock);
1563
1564 return err;
1565 }
1566 read_unlock(&mrt_lock);
1567 kfree_skb(skb);
1568 return -ENODEV;
1569 }
1570
1571 ip6_mr_forward(skb, cache);
1572
1573 read_unlock(&mrt_lock);
1574
1575 return 0;
1576}
1577
1578
1579static int
1580ip6mr_fill_mroute(struct sk_buff *skb, struct mfc6_cache *c, struct rtmsg *rtm)
1581{
1582 int ct;
1583 struct rtnexthop *nhp;
1584 struct net_device *dev = vif6_table[c->mf6c_parent].dev;
YOSHIFUJI Hideaki549e0282008-04-05 22:17:39 +09001585 u8 *b = skb_tail_pointer(skb);
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001586 struct rtattr *mp_head;
1587
1588 if (dev)
1589 RTA_PUT(skb, RTA_IIF, 4, &dev->ifindex);
1590
1591 mp_head = (struct rtattr *)skb_put(skb, RTA_LENGTH(0));
1592
1593 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
1594 if (c->mfc_un.res.ttls[ct] < 255) {
1595 if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
1596 goto rtattr_failure;
1597 nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
1598 nhp->rtnh_flags = 0;
1599 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
1600 nhp->rtnh_ifindex = vif6_table[ct].dev->ifindex;
1601 nhp->rtnh_len = sizeof(*nhp);
1602 }
1603 }
1604 mp_head->rta_type = RTA_MULTIPATH;
YOSHIFUJI Hideaki549e0282008-04-05 22:17:39 +09001605 mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09001606 rtm->rtm_type = RTN_MULTICAST;
1607 return 1;
1608
1609rtattr_failure:
1610 nlmsg_trim(skb, b);
1611 return -EMSGSIZE;
1612}
1613
1614int ip6mr_get_route(struct sk_buff *skb, struct rtmsg *rtm, int nowait)
1615{
1616 int err;
1617 struct mfc6_cache *cache;
1618 struct rt6_info *rt = (struct rt6_info *)skb->dst;
1619
1620 read_lock(&mrt_lock);
1621 cache = ip6mr_cache_find(&rt->rt6i_src.addr, &rt->rt6i_dst.addr);
1622
1623 if (!cache) {
1624 struct sk_buff *skb2;
1625 struct ipv6hdr *iph;
1626 struct net_device *dev;
1627 int vif;
1628
1629 if (nowait) {
1630 read_unlock(&mrt_lock);
1631 return -EAGAIN;
1632 }
1633
1634 dev = skb->dev;
1635 if (dev == NULL || (vif = ip6mr_find_vif(dev)) < 0) {
1636 read_unlock(&mrt_lock);
1637 return -ENODEV;
1638 }
1639
1640 /* really correct? */
1641 skb2 = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
1642 if (!skb2) {
1643 read_unlock(&mrt_lock);
1644 return -ENOMEM;
1645 }
1646
1647 skb_reset_transport_header(skb2);
1648
1649 skb_put(skb2, sizeof(struct ipv6hdr));
1650 skb_reset_network_header(skb2);
1651
1652 iph = ipv6_hdr(skb2);
1653 iph->version = 0;
1654 iph->priority = 0;
1655 iph->flow_lbl[0] = 0;
1656 iph->flow_lbl[1] = 0;
1657 iph->flow_lbl[2] = 0;
1658 iph->payload_len = 0;
1659 iph->nexthdr = IPPROTO_NONE;
1660 iph->hop_limit = 0;
1661 ipv6_addr_copy(&iph->saddr, &rt->rt6i_src.addr);
1662 ipv6_addr_copy(&iph->daddr, &rt->rt6i_dst.addr);
1663
1664 err = ip6mr_cache_unresolved(vif, skb2);
1665 read_unlock(&mrt_lock);
1666
1667 return err;
1668 }
1669
1670 if (!nowait && (rtm->rtm_flags&RTM_F_NOTIFY))
1671 cache->mfc_flags |= MFC_NOTIFY;
1672
1673 err = ip6mr_fill_mroute(skb, cache, rtm);
1674 read_unlock(&mrt_lock);
1675 return err;
1676}
1677