1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29#include <linux/config.h>
30#include <asm/system.h>
31#include <asm/uaccess.h>
32#include <linux/types.h>
33#include <linux/sched.h>
34#include <linux/errno.h>
35#include <linux/timer.h>
36#include <linux/mm.h>
37#include <linux/kernel.h>
38#include <linux/fcntl.h>
39#include <linux/stat.h>
40#include <linux/socket.h>
41#include <linux/in.h>
42#include <linux/inet.h>
43#include <linux/netdevice.h>
44#include <linux/inetdevice.h>
45#include <linux/igmp.h>
46#include <linux/proc_fs.h>
47#include <linux/mroute.h>
48#include <linux/init.h>
49#include <net/ip.h>
50#include <net/protocol.h>
51#include <linux/skbuff.h>
52#include <net/sock.h>
53#include <net/icmp.h>
54#include <net/udp.h>
55#include <net/raw.h>
56#include <linux/notifier.h>
57#include <linux/if_arp.h>
58#include <linux/ip_fw.h>
59#include <linux/firewall.h>
60#include <net/ipip.h>
61#include <net/checksum.h>
62
63#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
64#define CONFIG_IP_PIMSM 1
65#endif
66
67
68
69
70
71static struct vif_device vif_table[MAXVIFS];
72static unsigned long vifc_map;
73static int maxvif;
74int mroute_do_assert = 0;
75int mroute_do_pim = 0;
76static struct mfc_cache *mfc_cache_array[MFC_LINES];
77int cache_resolve_queue_len = 0;
78
79static int ip_mr_forward(struct sk_buff *skb, struct mfc_cache *cache, int local);
80static int ipmr_cache_report(struct sk_buff *pkt, vifi_t vifi, int assert);
81static int ipmr_fill_mroute(struct sk_buff *skb, struct mfc_cache *c, struct rtmsg *rtm);
82
83extern struct inet_protocol pim_protocol;
84
85static
86struct device *ipmr_new_tunnel(struct vifctl *v)
87{
88 struct device *dev = NULL;
89
90 rtnl_lock();
91 dev = dev_get("tunl0");
92
93 if (dev) {
94 int err;
95 struct ifreq ifr;
96 mm_segment_t oldfs;
97 struct ip_tunnel_parm p;
98 struct in_device *in_dev;
99
100 memset(&p, 0, sizeof(p));
101 p.iph.daddr = v->vifc_rmt_addr.s_addr;
102 p.iph.saddr = v->vifc_lcl_addr.s_addr;
103 p.iph.version = 4;
104 p.iph.ihl = 5;
105 p.iph.protocol = IPPROTO_IPIP;
106 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
107 ifr.ifr_ifru.ifru_data = (void*)&p;
108
109 oldfs = get_fs(); set_fs(KERNEL_DS);
110 err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
111 set_fs(oldfs);
112
113 if (err == 0 && (dev = dev_get(p.name)) != NULL) {
114 dev->flags |= IFF_MULTICAST;
115
116 in_dev = dev->ip_ptr;
117 if (in_dev == NULL && (in_dev = inetdev_init(dev)) == NULL)
118 goto failure;
119 in_dev->cnf.rp_filter = 0;
120
121 if (dev_open(dev))
122 goto failure;
123 }
124 }
125 rtnl_unlock();
126 return dev;
127
128failure:
129 unregister_netdevice(dev);
130 rtnl_unlock();
131 return NULL;
132}
133
134#ifdef CONFIG_IP_PIMSM
135
136static int reg_vif_num = -1;
137static struct device * reg_dev;
138
139static int reg_vif_xmit(struct sk_buff *skb, struct device *dev)
140{
141 ((struct net_device_stats*)dev->priv)->tx_bytes += skb->len;
142 ((struct net_device_stats*)dev->priv)->tx_packets++;
143 ipmr_cache_report(skb, reg_vif_num, IGMPMSG_WHOLEPKT);
144 kfree_skb(skb);
145 return 0;
146}
147
148static struct net_device_stats *reg_vif_get_stats(struct device *dev)
149{
150 return (struct net_device_stats*)dev->priv;
151}
152
153static
154struct device *ipmr_reg_vif(struct vifctl *v)
155{
156 struct device *dev;
157 struct in_device *in_dev;
158 int size;
159
160 size = sizeof(*dev) + IFNAMSIZ + sizeof(struct net_device_stats);
161 dev = kmalloc(size, GFP_KERNEL);
162 if (!dev)
163 return NULL;
164
165 memset(dev, 0, size);
166
167 dev->priv = dev + 1;
168 dev->name = dev->priv + sizeof(struct net_device_stats);
169
170 strcpy(dev->name, "pimreg");
171
172 dev->type = ARPHRD_PIMREG;
173 dev->mtu = 1500 - sizeof(struct iphdr) - 8;
174 dev->flags = IFF_NOARP;
175 dev->hard_start_xmit = reg_vif_xmit;
176 dev->get_stats = reg_vif_get_stats;
177
178 rtnl_lock();
179
180 if (register_netdevice(dev)) {
181 rtnl_unlock();
182 kfree(dev);
183 return NULL;
184 }
185 dev->iflink = 0;
186
187 if ((in_dev = inetdev_init(dev)) == NULL)
188 goto failure;
189
190 in_dev->cnf.rp_filter = 0;
191
192 if (dev_open(dev))
193 goto failure;
194
195 rtnl_unlock();
196 reg_dev = dev;
197 return dev;
198
199failure:
200 unregister_netdevice(dev);
201 rtnl_unlock();
202 kfree(dev);
203 return NULL;
204}
205#endif
206
207
208
209
210
211static int vif_delete(int vifi)
212{
213 struct vif_device *v;
214 struct device *dev;
215 struct in_device *in_dev;
216
217 if (vifi < 0 || vifi >= maxvif || !(vifc_map&(1<<vifi)))
218 return -EADDRNOTAVAIL;
219
220 v = &vif_table[vifi];
221
222 dev = v->dev;
223 v->dev = NULL;
224 vifc_map &= ~(1<<vifi);
225
226 if ((in_dev = dev->ip_ptr) != NULL)
227 in_dev->cnf.mc_forwarding = 0;
228
229 dev_set_allmulti(dev, -1);
230 ip_rt_multicast_event(in_dev);
231
232 if (v->flags&(VIFF_TUNNEL|VIFF_REGISTER)) {
233#ifdef CONFIG_IP_PIMSM
234 if (vifi == reg_vif_num) {
235 reg_vif_num = -1;
236 reg_dev = NULL;
237 }
238#endif
239 unregister_netdevice(dev);
240 if (v->flags&VIFF_REGISTER)
241 kfree(dev);
242 }
243
244 if (vifi+1 == maxvif) {
245 int tmp;
246 for (tmp=vifi-1; tmp>=0; tmp--) {
247 if (vifc_map&(1<<tmp))
248 break;
249 }
250 maxvif = tmp+1;
251 }
252 return 0;
253}
254
255static void ipmr_update_threshoulds(struct mfc_cache *cache, unsigned char *ttls)
256{
257 int vifi;
258
259 start_bh_atomic();
260
261 cache->mfc_minvif = MAXVIFS;
262 cache->mfc_maxvif = 0;
263 memset(cache->mfc_ttls, 255, MAXVIFS);
264
265 for (vifi=0; vifi<maxvif; vifi++) {
266 if (vifc_map&(1<<vifi) && ttls[vifi] && ttls[vifi] < 255) {
267 cache->mfc_ttls[vifi] = ttls[vifi];
268 if (cache->mfc_minvif > vifi)
269 cache->mfc_minvif = vifi;
270 if (cache->mfc_maxvif <= vifi)
271 cache->mfc_maxvif = vifi + 1;
272 }
273 }
274 end_bh_atomic();
275}
276
277
278
279
280
281static void ipmr_cache_delete(struct mfc_cache *cache)
282{
283 struct sk_buff *skb;
284 int line;
285 struct mfc_cache **cp;
286
287
288
289
290
291 line=MFC_HASH(cache->mfc_mcastgrp,cache->mfc_origin);
292 cp=&(mfc_cache_array[line]);
293
294 if(cache->mfc_flags&MFC_QUEUED)
295 del_timer(&cache->mfc_timer);
296
297
298
299
300
301 while(*cp!=NULL)
302 {
303 if(*cp==cache)
304 {
305 *cp=cache->next;
306 break;
307 }
308 cp=&((*cp)->next);
309 }
310
311
312
313
314
315
316 if(cache->mfc_flags&MFC_QUEUED)
317 {
318 cache_resolve_queue_len--;
319 while((skb=skb_dequeue(&cache->mfc_unresolved))) {
320#ifdef CONFIG_RTNETLINK
321 if (skb->nh.iph->version == 0) {
322 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
323 nlh->nlmsg_type = NLMSG_ERROR;
324 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
325 skb_trim(skb, nlh->nlmsg_len);
326 ((struct nlmsgerr*)NLMSG_DATA(nlh))->error = -ETIMEDOUT;
327 netlink_unicast(rtnl, skb, NETLINK_CB(skb).dst_pid, MSG_DONTWAIT);
328 } else
329#endif
330 kfree_skb(skb);
331 }
332 }
333 kfree_s(cache,sizeof(cache));
334}
335
336
337
338
339
340static void ipmr_cache_timer(unsigned long data)
341{
342 struct mfc_cache *cache=(struct mfc_cache *)data;
343 ipmr_cache_delete(cache);
344}
345
346
347
348
349
350static void ipmr_cache_insert(struct mfc_cache *c)
351{
352 int line=MFC_HASH(c->mfc_mcastgrp,c->mfc_origin);
353 c->next=mfc_cache_array[line];
354 mfc_cache_array[line]=c;
355}
356
357
358
359
360
361struct mfc_cache *ipmr_cache_find(__u32 origin, __u32 mcastgrp)
362{
363 int line=MFC_HASH(mcastgrp,origin);
364 struct mfc_cache *cache;
365
366 cache=mfc_cache_array[line];
367 while(cache!=NULL)
368 {
369 if(cache->mfc_origin==origin && cache->mfc_mcastgrp==mcastgrp)
370 return cache;
371 cache=cache->next;
372 }
373 return NULL;
374}
375
376
377
378
379
380static struct mfc_cache *ipmr_cache_alloc(int priority)
381{
382 struct mfc_cache *c=(struct mfc_cache *)kmalloc(sizeof(struct mfc_cache), priority);
383 if(c==NULL)
384 return NULL;
385 memset(c, 0, sizeof(*c));
386 skb_queue_head_init(&c->mfc_unresolved);
387 init_timer(&c->mfc_timer);
388 c->mfc_timer.data=(long)c;
389 c->mfc_timer.function=ipmr_cache_timer;
390 c->mfc_minvif = MAXVIFS;
391 return c;
392}
393
394
395
396
397
398static void ipmr_cache_resolve(struct mfc_cache *cache)
399{
400 struct sk_buff *skb;
401
402 start_bh_atomic();
403
404
405
406
407
408 del_timer(&cache->mfc_timer);
409
410 if (cache->mfc_flags&MFC_QUEUED) {
411 cache->mfc_flags&=~MFC_QUEUED;
412 cache_resolve_queue_len--;
413 }
414
415 end_bh_atomic();
416
417
418
419
420 while((skb=skb_dequeue(&cache->mfc_unresolved))) {
421#ifdef CONFIG_RTNETLINK
422 if (skb->nh.iph->version == 0) {
423 int err;
424 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
425
426 if (ipmr_fill_mroute(skb, cache, NLMSG_DATA(nlh)) > 0) {
427 nlh->nlmsg_len = skb->tail - (u8*)nlh;
428 } else {
429 nlh->nlmsg_type = NLMSG_ERROR;
430 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
431 skb_trim(skb, nlh->nlmsg_len);
432 ((struct nlmsgerr*)NLMSG_DATA(nlh))->error = -EMSGSIZE;
433 }
434 err = netlink_unicast(rtnl, skb, NETLINK_CB(skb).pid, MSG_DONTWAIT);
435 } else
436#endif
437 ip_mr_forward(skb, cache, 0);
438 }
439}
440
441
442
443
444
445
446static int ipmr_cache_report(struct sk_buff *pkt, vifi_t vifi, int assert)
447{
448 struct sk_buff *skb;
449 int ihl = pkt->nh.iph->ihl<<2;
450 struct igmphdr *igmp;
451 struct igmpmsg *msg;
452 int ret;
453
454 if (mroute_socket==NULL)
455 return -EINVAL;
456
457#ifdef CONFIG_IP_PIMSM
458 if (assert == IGMPMSG_WHOLEPKT)
459 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
460 else
461#endif
462 skb = alloc_skb(128, GFP_ATOMIC);
463
464 if(!skb)
465 return -ENOBUFS;
466
467#ifdef CONFIG_IP_PIMSM
468 if (assert == IGMPMSG_WHOLEPKT) {
469
470
471
472
473
474 msg = (struct igmpmsg*)skb_push(skb, sizeof(struct iphdr));
475 skb->nh.raw = skb->h.raw = (u8*)msg;
476 memcpy(msg, pkt->nh.raw, sizeof(struct iphdr));
477 msg->im_msgtype = IGMPMSG_WHOLEPKT;
478 msg->im_mbz = 0;
479 msg->im_vif = reg_vif_num;
480 skb->nh.iph->ihl = sizeof(struct iphdr) >> 2;
481 skb->nh.iph->tot_len = htons(ntohs(pkt->nh.iph->tot_len) + sizeof(struct iphdr));
482 } else
483#endif
484 {
485
486
487
488
489
490 skb->nh.iph = (struct iphdr *)skb_put(skb, ihl);
491 memcpy(skb->data,pkt->data,ihl);
492 skb->nh.iph->protocol = 0;
493 msg = (struct igmpmsg*)skb->nh.iph;
494 msg->im_vif = vifi;
495 skb->dst = dst_clone(pkt->dst);
496
497
498
499
500
501 igmp=(struct igmphdr *)skb_put(skb,sizeof(struct igmphdr));
502 igmp->type =
503 msg->im_msgtype = assert;
504 igmp->code = 0;
505 skb->nh.iph->tot_len=htons(skb->len);
506 skb->h.raw = skb->nh.raw;
507 }
508
509
510
511
512 if ((ret=sock_queue_rcv_skb(mroute_socket,skb))<0) {
513 if (net_ratelimit())
514 printk(KERN_WARNING "mroute: pending queue full, dropping entries.\n");
515 kfree_skb(skb);
516 }
517
518 return ret;
519}
520
521
522
523
524
525static int ipmr_cache_unresolved(struct mfc_cache *cache, vifi_t vifi, struct sk_buff *skb)
526{
527 if(cache==NULL)
528 {
529
530
531
532 if(cache_resolve_queue_len>=10 || (cache=ipmr_cache_alloc(GFP_ATOMIC))==NULL)
533 {
534 kfree_skb(skb);
535 return -ENOBUFS;
536 }
537
538
539
540 cache->mfc_parent=ALL_VIFS;
541 cache->mfc_origin=skb->nh.iph->saddr;
542 cache->mfc_mcastgrp=skb->nh.iph->daddr;
543 cache->mfc_flags=MFC_QUEUED;
544
545
546
547 ipmr_cache_insert(cache);
548 cache_resolve_queue_len++;
549
550
551
552 cache->mfc_timer.expires=jiffies+10*HZ;
553 add_timer(&cache->mfc_timer);
554
555
556
557 if(mroute_socket)
558 {
559
560
561
562
563
564
565 if (ipmr_cache_report(skb, vifi, IGMPMSG_NOCACHE)<0) {
566 ipmr_cache_delete(cache);
567 kfree_skb(skb);
568 return -ENOBUFS;
569 }
570 }
571 }
572
573
574
575 if(cache->mfc_queuelen>3)
576 {
577 kfree_skb(skb);
578 return -ENOBUFS;
579 }
580 cache->mfc_queuelen++;
581 skb_queue_tail(&cache->mfc_unresolved,skb);
582 return 0;
583}
584
585
586
587
588
589int ipmr_mfc_modify(int action, struct mfcctl *mfc)
590{
591 struct mfc_cache *cache;
592
593 if(!MULTICAST(mfc->mfcc_mcastgrp.s_addr))
594 return -EINVAL;
595
596
597
598
599 start_bh_atomic();
600
601 cache=ipmr_cache_find(mfc->mfcc_origin.s_addr,mfc->mfcc_mcastgrp.s_addr);
602
603
604
605
606 if(action==MRT_DEL_MFC)
607 {
608 if(cache)
609 {
610 ipmr_cache_delete(cache);
611 end_bh_atomic();
612 return 0;
613 }
614 end_bh_atomic();
615 return -ENOENT;
616 }
617 if(cache)
618 {
619
620
621
622
623
624 cache->mfc_flags|=MFC_RESOLVED;
625 cache->mfc_parent=mfc->mfcc_parent;
626 ipmr_update_threshoulds(cache, mfc->mfcc_ttls);
627
628
629
630
631
632
633 if(cache->mfc_flags&MFC_QUEUED)
634 ipmr_cache_resolve(cache);
635 end_bh_atomic();
636 return 0;
637 }
638
639
640
641
642
643
644 cache=ipmr_cache_alloc(GFP_ATOMIC);
645 if(cache==NULL)
646 {
647 end_bh_atomic();
648 return -ENOMEM;
649 }
650 cache->mfc_flags=MFC_RESOLVED;
651 cache->mfc_origin=mfc->mfcc_origin.s_addr;
652 cache->mfc_mcastgrp=mfc->mfcc_mcastgrp.s_addr;
653 cache->mfc_parent=mfc->mfcc_parent;
654 ipmr_update_threshoulds(cache, mfc->mfcc_ttls);
655 ipmr_cache_insert(cache);
656 end_bh_atomic();
657 return 0;
658}
659
660static void mrtsock_destruct(struct sock *sk)
661{
662 if (sk == mroute_socket) {
663 ipv4_devconf.mc_forwarding = 0;
664
665 mroute_socket=NULL;
666 synchronize_bh();
667
668 mroute_close(sk);
669 }
670}
671
672
673
674
675
676
677
678
679int ip_mroute_setsockopt(struct sock *sk,int optname,char *optval,int optlen)
680{
681 struct vifctl vif;
682 struct mfcctl mfc;
683
684 if(optname!=MRT_INIT)
685 {
686 if(sk!=mroute_socket)
687 return -EACCES;
688 }
689
690 switch(optname)
691 {
692 case MRT_INIT:
693 if(sk->type!=SOCK_RAW || sk->num!=IPPROTO_IGMP)
694 return -EOPNOTSUPP;
695 if(optlen!=sizeof(int))
696 return -ENOPROTOOPT;
697 {
698 int opt;
699 if (get_user(opt,(int *)optval))
700 return -EFAULT;
701 if (opt != 1)
702 return -ENOPROTOOPT;
703 }
704 if(mroute_socket)
705 return -EADDRINUSE;
706 mroute_socket=sk;
707 ipv4_devconf.mc_forwarding = 1;
708 if (ip_ra_control(sk, 1, mrtsock_destruct) == 0)
709 return 0;
710 mrtsock_destruct(sk);
711 return -EADDRINUSE;
712 case MRT_DONE:
713 return ip_ra_control(sk, 0, NULL);
714 case MRT_ADD_VIF:
715 case MRT_DEL_VIF:
716 if(optlen!=sizeof(vif))
717 return -EINVAL;
718 if (copy_from_user(&vif,optval,sizeof(vif)))
719 return -EFAULT;
720 if(vif.vifc_vifi >= MAXVIFS)
721 return -ENFILE;
722 if(optname==MRT_ADD_VIF)
723 {
724 struct vif_device *v=&vif_table[vif.vifc_vifi];
725 struct device *dev;
726 struct in_device *in_dev;
727
728
729 if (vifc_map&(1<<vif.vifc_vifi))
730 return -EADDRINUSE;
731
732 switch (vif.vifc_flags) {
733#ifdef CONFIG_IP_PIMSM
734 case VIFF_REGISTER:
735
736
737
738
739
740 if (reg_vif_num >= 0)
741 return -EADDRINUSE;
742 reg_vif_num = vif.vifc_vifi;
743 dev = ipmr_reg_vif(&vif);
744 if (!dev) {
745 reg_vif_num = -1;
746 return -ENOBUFS;
747 }
748 break;
749#endif
750 case VIFF_TUNNEL:
751 dev = ipmr_new_tunnel(&vif);
752 if (!dev)
753 return -ENOBUFS;
754 break;
755 case 0:
756 dev=ip_dev_find(vif.vifc_lcl_addr.s_addr);
757 if (!dev)
758 return -EADDRNOTAVAIL;
759 break;
760 default:
761#if 0
762 printk(KERN_DEBUG "ipmr_add_vif: flags %02x\n", vif.vifc_flags);
763#endif
764 return -EINVAL;
765 }
766
767 if ((in_dev = dev->ip_ptr) == NULL)
768 return -EADDRNOTAVAIL;
769 if (in_dev->cnf.mc_forwarding)
770 return -EADDRINUSE;
771 in_dev->cnf.mc_forwarding = 1;
772 dev_set_allmulti(dev, +1);
773 ip_rt_multicast_event(in_dev);
774
775
776
777
778 start_bh_atomic();
779 v->rate_limit=vif.vifc_rate_limit;
780 v->local=vif.vifc_lcl_addr.s_addr;
781 v->remote=vif.vifc_rmt_addr.s_addr;
782 v->flags=vif.vifc_flags;
783 v->threshold=vif.vifc_threshold;
784 v->dev=dev;
785 v->bytes_in = 0;
786 v->bytes_out = 0;
787 v->pkt_in = 0;
788 v->pkt_out = 0;
789 v->link = dev->ifindex;
790 if (vif.vifc_flags&(VIFF_TUNNEL|VIFF_REGISTER))
791 v->link = dev->iflink;
792 vifc_map|=(1<<vif.vifc_vifi);
793 if (vif.vifc_vifi+1 > maxvif)
794 maxvif = vif.vifc_vifi+1;
795 end_bh_atomic();
796 return 0;
797 } else {
798 int ret;
799 rtnl_lock();
800 ret = vif_delete(vif.vifc_vifi);
801 rtnl_unlock();
802 return ret;
803 }
804
805
806
807
808
809 case MRT_ADD_MFC:
810 case MRT_DEL_MFC:
811 if(optlen!=sizeof(mfc))
812 return -EINVAL;
813 if (copy_from_user(&mfc,optval, sizeof(mfc)))
814 return -EFAULT;
815 return ipmr_mfc_modify(optname, &mfc);
816
817
818
819 case MRT_ASSERT:
820 {
821 int v;
822 if(get_user(v,(int *)optval))
823 return -EFAULT;
824 mroute_do_assert=(v)?1:0;
825 return 0;
826 }
827#ifdef CONFIG_IP_PIMSM
828 case MRT_PIM:
829 {
830 int v;
831 if(get_user(v,(int *)optval))
832 return -EFAULT;
833 v = (v)?1:0;
834 if (v != mroute_do_pim) {
835 mroute_do_pim = v;
836 mroute_do_assert = v;
837#ifdef CONFIG_IP_PIMSM_V2
838 if (mroute_do_pim)
839 inet_add_protocol(&pim_protocol);
840 else
841 inet_del_protocol(&pim_protocol);
842#endif
843 }
844 return 0;
845 }
846#endif
847
848
849
850
851 default:
852 return -ENOPROTOOPT;
853 }
854}
855
856
857
858
859
860int ip_mroute_getsockopt(struct sock *sk,int optname,char *optval,int *optlen)
861{
862 int olr;
863 int val;
864
865 if(sk!=mroute_socket)
866 return -EACCES;
867 if(optname!=MRT_VERSION &&
868#ifdef CONFIG_IP_PIMSM
869 optname!=MRT_PIM &&
870#endif
871 optname!=MRT_ASSERT)
872 return -ENOPROTOOPT;
873
874 if(get_user(olr, optlen))
875 return -EFAULT;
876
877 olr=min(olr,sizeof(int));
878 if(put_user(olr,optlen))
879 return -EFAULT;
880 if(optname==MRT_VERSION)
881 val=0x0305;
882#ifdef CONFIG_IP_PIMSM
883 else if(optname==MRT_PIM)
884 val=mroute_do_pim;
885#endif
886 else
887 val=mroute_do_assert;
888 if(copy_to_user(optval,&val,olr))
889 return -EFAULT;
890 return 0;
891}
892
893
894
895
896
897int ipmr_ioctl(struct sock *sk, int cmd, unsigned long arg)
898{
899 struct sioc_sg_req sr;
900 struct sioc_vif_req vr;
901 struct vif_device *vif;
902 struct mfc_cache *c;
903
904 switch(cmd)
905 {
906 case SIOCGETVIFCNT:
907 if (copy_from_user(&vr,(void *)arg,sizeof(vr)))
908 return -EFAULT;
909 if(vr.vifi>=maxvif)
910 return -EINVAL;
911 vif=&vif_table[vr.vifi];
912 if(vifc_map&(1<<vr.vifi))
913 {
914 vr.icount=vif->pkt_in;
915 vr.ocount=vif->pkt_out;
916 vr.ibytes=vif->bytes_in;
917 vr.obytes=vif->bytes_out;
918 if (copy_to_user((void *)arg,&vr,sizeof(vr)))
919 return -EFAULT;
920 return 0;
921 }
922 return -EADDRNOTAVAIL;
923 case SIOCGETSGCNT:
924 if (copy_from_user(&sr,(void *)arg,sizeof(sr)))
925 return -EFAULT;
926 for (c = mfc_cache_array[MFC_HASH(sr.grp.s_addr, sr.src.s_addr)];
927 c; c = c->next) {
928 if (sr.grp.s_addr == c->mfc_mcastgrp &&
929 sr.src.s_addr == c->mfc_origin) {
930 sr.pktcnt = c->mfc_pkt;
931 sr.bytecnt = c->mfc_bytes;
932 sr.wrong_if = c->mfc_wrong_if;
933 if (copy_to_user((void *)arg,&sr,sizeof(sr)))
934 return -EFAULT;
935 return 0;
936 }
937 }
938 return -EADDRNOTAVAIL;
939 default:
940 return -ENOIOCTLCMD;
941 }
942}
943
944
945
946
947
948void mroute_close(struct sock *sk)
949{
950 int i;
951
952
953
954
955 rtnl_lock();
956 for(i=0; i<maxvif; i++)
957 vif_delete(i);
958 rtnl_unlock();
959
960
961
962
963 for(i=0;i<MFC_LINES;i++)
964 {
965 start_bh_atomic();
966 while(mfc_cache_array[i]!=NULL)
967 ipmr_cache_delete(mfc_cache_array[i]);
968 end_bh_atomic();
969 }
970}
971
972static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
973{
974 struct vif_device *v;
975 int ct;
976 if (event != NETDEV_UNREGISTER)
977 return NOTIFY_DONE;
978 v=&vif_table[0];
979 for(ct=0;ct<maxvif;ct++) {
980 if (vifc_map&(1<<ct) && v->dev==ptr)
981 vif_delete(ct);
982 v++;
983 }
984 return NOTIFY_DONE;
985}
986
987
988static struct notifier_block ip_mr_notifier={
989 ipmr_device_event,
990 NULL,
991 0
992};
993
994
995
996
997
998
999
1000static void ip_encap(struct sk_buff *skb, u32 saddr, u32 daddr)
1001{
1002 struct iphdr *iph = (struct iphdr *)skb_push(skb,sizeof(struct iphdr));
1003
1004 iph->version = 4;
1005 iph->tos = skb->nh.iph->tos;
1006 iph->ttl = skb->nh.iph->ttl;
1007 iph->frag_off = 0;
1008 iph->daddr = daddr;
1009 iph->saddr = saddr;
1010 iph->protocol = IPPROTO_IPIP;
1011 iph->ihl = 5;
1012 iph->tot_len = htons(skb->len);
1013 iph->id = htons(ip_id_count++);
1014 ip_send_check(iph);
1015
1016 skb->h.ipiph = skb->nh.iph;
1017 skb->nh.iph = iph;
1018}
1019
1020
1021
1022
1023
1024static void ipmr_queue_xmit(struct sk_buff *skb, struct mfc_cache *c,
1025 int vifi, int last)
1026{
1027 struct iphdr *iph = skb->nh.iph;
1028 struct vif_device *vif = &vif_table[vifi];
1029 struct device *dev;
1030 struct rtable *rt;
1031 int encap = 0;
1032 struct sk_buff *skb2;
1033
1034#ifdef CONFIG_IP_PIMSM
1035 if (vif->flags & VIFF_REGISTER) {
1036 vif->pkt_out++;
1037 vif->bytes_out+=skb->len;
1038 ((struct net_device_stats*)vif->dev->priv)->tx_bytes += skb->len;
1039 ((struct net_device_stats*)vif->dev->priv)->tx_packets++;
1040 ipmr_cache_report(skb, vifi, IGMPMSG_WHOLEPKT);
1041 return;
1042 }
1043#endif
1044
1045 if (vif->flags&VIFF_TUNNEL) {
1046 if (ip_route_output(&rt, vif->remote, vif->local, RT_TOS(iph->tos), vif->link))
1047 return;
1048 encap = sizeof(struct iphdr);
1049 } else {
1050 if (ip_route_output(&rt, iph->daddr, 0, RT_TOS(iph->tos), vif->link))
1051 return;
1052 }
1053
1054 dev = rt->u.dst.dev;
1055
1056 if (skb->len+encap > rt->u.dst.pmtu && (ntohs(iph->frag_off) & IP_DF)) {
1057
1058
1059
1060
1061
1062 ip_statistics.IpFragFails++;
1063 ip_rt_put(rt);
1064 return;
1065 }
1066
1067 encap += dev->hard_header_len;
1068
1069 if (skb_headroom(skb) < encap || skb_cloned(skb) || !last)
1070 skb2 = skb_realloc_headroom(skb, (encap + 15)&~15);
1071 else if (atomic_read(&skb->users) != 1)
1072 skb2 = skb_clone(skb, GFP_ATOMIC);
1073 else {
1074 atomic_inc(&skb->users);
1075 skb2 = skb;
1076 }
1077
1078 if (skb2 == NULL) {
1079 ip_rt_put(rt);
1080 return;
1081 }
1082
1083 vif->pkt_out++;
1084 vif->bytes_out+=skb->len;
1085
1086 dst_release(skb2->dst);
1087 skb2->dst = &rt->u.dst;
1088 iph = skb2->nh.iph;
1089 ip_decrease_ttl(iph);
1090
1091#ifdef CONFIG_FIREWALL
1092 if (call_fw_firewall(PF_INET, vif->dev, skb2->nh.iph, NULL, &skb2) < FW_ACCEPT) {
1093 kfree_skb(skb2);
1094 return;
1095 }
1096 if (call_out_firewall(PF_INET, vif->dev, skb2->nh.iph, NULL, &skb2) < FW_ACCEPT) {
1097 kfree_skb(skb2);
1098 return;
1099 }
1100#endif
1101 if (vif->flags & VIFF_TUNNEL) {
1102 ip_encap(skb2, vif->local, vif->remote);
1103#ifdef CONFIG_FIREWALL
1104
1105
1106
1107 if (call_out_firewall(PF_INET, dev, skb2->nh.iph, NULL, &skb2) < FW_ACCEPT) {
1108 kfree_skb(skb2);
1109 return;
1110 }
1111#endif
1112 ((struct ip_tunnel *)vif->dev->priv)->stat.tx_packets++;
1113 ((struct ip_tunnel *)vif->dev->priv)->stat.tx_bytes+=skb2->len;
1114 }
1115
1116 IPCB(skb2)->flags |= IPSKB_FORWARDED;
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130 if (skb2->len <= rt->u.dst.pmtu)
1131 skb2->dst->output(skb2);
1132 else
1133 ip_fragment(skb2, skb2->dst->output);
1134}
1135
1136int ipmr_find_vif(struct device *dev)
1137{
1138 int ct;
1139 for (ct=0; ct<maxvif; ct++) {
1140 if (vifc_map&(1<<ct) && vif_table[ct].dev == dev)
1141 return ct;
1142 }
1143 return ALL_VIFS;
1144}
1145
1146
1147
1148int ip_mr_forward(struct sk_buff *skb, struct mfc_cache *cache, int local)
1149{
1150 int psend = -1;
1151 int vif, ct;
1152
1153 vif = cache->mfc_parent;
1154 cache->mfc_pkt++;
1155 cache->mfc_bytes += skb->len;
1156
1157
1158
1159
1160 if (vif_table[vif].dev != skb->dev) {
1161 int true_vifi;
1162
1163 if (((struct rtable*)skb->dst)->key.iif == 0) {
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175 goto dont_forward;
1176 }
1177
1178 cache->mfc_wrong_if++;
1179 true_vifi = ipmr_find_vif(skb->dev);
1180
1181 if (true_vifi < MAXVIFS && mroute_do_assert &&
1182
1183
1184
1185
1186
1187 (mroute_do_pim || cache->mfc_ttls[true_vifi] < 255) &&
1188 jiffies - cache->mfc_last_assert > MFC_ASSERT_THRESH) {
1189 cache->mfc_last_assert = jiffies;
1190 ipmr_cache_report(skb, true_vifi, IGMPMSG_WRONGVIF);
1191 }
1192 goto dont_forward;
1193 }
1194
1195 vif_table[vif].pkt_in++;
1196 vif_table[vif].bytes_in+=skb->len;
1197
1198
1199
1200
1201 for (ct = cache->mfc_maxvif-1; ct >= cache->mfc_minvif; ct--) {
1202 if (skb->nh.iph->ttl > cache->mfc_ttls[ct]) {
1203 if (psend != -1)
1204 ipmr_queue_xmit(skb, cache, psend, 0);
1205 psend=ct;
1206 }
1207 }
1208 if (psend != -1)
1209 ipmr_queue_xmit(skb, cache, psend, !local);
1210
1211dont_forward:
1212 if (!local)
1213 kfree_skb(skb);
1214 return 0;
1215}
1216
1217
1218
1219
1220
1221
1222int ip_mr_input(struct sk_buff *skb)
1223{
1224 struct mfc_cache *cache;
1225 int local = ((struct rtable*)skb->dst)->rt_flags&RTCF_LOCAL;
1226
1227
1228
1229
1230 if (IPCB(skb)->flags&IPSKB_FORWARDED)
1231 goto dont_forward;
1232
1233 if (!local) {
1234 if (IPCB(skb)->opt.router_alert) {
1235 if (ip_call_ra_chain(skb))
1236 return 0;
1237 } else if (skb->nh.iph->protocol == IPPROTO_IGMP && mroute_socket) {
1238
1239
1240
1241
1242
1243
1244 raw_rcv(mroute_socket, skb);
1245 return 0;
1246 }
1247 }
1248
1249 cache = ipmr_cache_find(skb->nh.iph->saddr, skb->nh.iph->daddr);
1250
1251
1252
1253
1254
1255 if (cache==NULL || (cache->mfc_flags&MFC_QUEUED)) {
1256 int vif;
1257
1258 if (local) {
1259 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1260 ip_local_deliver(skb);
1261 if (skb2 == NULL)
1262 return -ENOBUFS;
1263 skb = skb2;
1264 }
1265
1266 vif = ipmr_find_vif(skb->dev);
1267 if (vif != ALL_VIFS) {
1268 ipmr_cache_unresolved(cache, vif, skb);
1269 return -EAGAIN;
1270 }
1271 kfree_skb(skb);
1272 return 0;
1273 }
1274
1275 ip_mr_forward(skb, cache, local);
1276
1277 if (local)
1278 return ip_local_deliver(skb);
1279 return 0;
1280
1281dont_forward:
1282 if (local)
1283 return ip_local_deliver(skb);
1284 kfree_skb(skb);
1285 return 0;
1286}
1287
1288#ifdef CONFIG_IP_PIMSM_V1
1289
1290
1291
1292
1293int pim_rcv_v1(struct sk_buff * skb, unsigned short len)
1294{
1295 struct igmphdr *pim = (struct igmphdr*)skb->h.raw;
1296 struct iphdr *encap;
1297
1298 if (!mroute_do_pim ||
1299 len < sizeof(*pim) + sizeof(*encap) ||
1300 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER ||
1301 reg_dev == NULL) {
1302 kfree_skb(skb);
1303 return -EINVAL;
1304 }
1305
1306 encap = (struct iphdr*)(skb->h.raw + sizeof(struct igmphdr));
1307
1308
1309
1310
1311
1312
1313 if (!MULTICAST(encap->daddr) ||
1314 ntohs(encap->tot_len) == 0 ||
1315 ntohs(encap->tot_len) + sizeof(*pim) > len) {
1316 kfree_skb(skb);
1317 return -EINVAL;
1318 }
1319 skb->mac.raw = skb->nh.raw;
1320 skb_pull(skb, (u8*)encap - skb->data);
1321 skb->nh.iph = (struct iphdr *)skb->data;
1322 skb->dev = reg_dev;
1323 memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options));
1324 skb->protocol = __constant_htons(ETH_P_IP);
1325 skb->ip_summed = 0;
1326 skb->pkt_type = PACKET_HOST;
1327 dst_release(skb->dst);
1328 skb->dst = NULL;
1329 ((struct net_device_stats*)reg_dev->priv)->rx_bytes += skb->len;
1330 ((struct net_device_stats*)reg_dev->priv)->rx_packets++;
1331 netif_rx(skb);
1332 return 0;
1333}
1334#endif
1335
1336#ifdef CONFIG_IP_PIMSM_V2
1337int pim_rcv(struct sk_buff * skb, unsigned short len)
1338{
1339 struct pimreghdr *pim = (struct pimreghdr*)skb->h.raw;
1340 struct iphdr *encap;
1341
1342 if (len < sizeof(*pim) + sizeof(*encap) ||
1343 pim->type != ((PIM_VERSION<<4)|(PIM_REGISTER)) ||
1344 (pim->flags&PIM_NULL_REGISTER) ||
1345 reg_dev == NULL ||
1346 ip_compute_csum((void *)pim, len)) {
1347 kfree_skb(skb);
1348 return -EINVAL;
1349 }
1350
1351
1352 encap = (struct iphdr*)(skb->h.raw + sizeof(struct pimreghdr));
1353 if (!MULTICAST(encap->daddr) ||
1354 ntohs(encap->tot_len) == 0 ||
1355 ntohs(encap->tot_len) + sizeof(*pim) > len) {
1356 kfree_skb(skb);
1357 return -EINVAL;
1358 }
1359 skb->mac.raw = skb->nh.raw;
1360 skb_pull(skb, (u8*)encap - skb->data);
1361 skb->nh.iph = (struct iphdr *)skb->data;
1362 skb->dev = reg_dev;
1363 memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options));
1364 skb->protocol = __constant_htons(ETH_P_IP);
1365 skb->ip_summed = 0;
1366 skb->pkt_type = PACKET_HOST;
1367 dst_release(skb->dst);
1368 ((struct net_device_stats*)reg_dev->priv)->rx_bytes += skb->len;
1369 ((struct net_device_stats*)reg_dev->priv)->rx_packets++;
1370 skb->dst = NULL;
1371 netif_rx(skb);
1372 return 0;
1373}
1374#endif
1375
1376#ifdef CONFIG_RTNETLINK
1377
1378static int
1379ipmr_fill_mroute(struct sk_buff *skb, struct mfc_cache *c, struct rtmsg *rtm)
1380{
1381 int ct;
1382 struct rtnexthop *nhp;
1383 struct device *dev = vif_table[c->mfc_parent].dev;
1384 u8 *b = skb->tail;
1385 struct rtattr *mp_head;
1386
1387 if (dev)
1388 RTA_PUT(skb, RTA_IIF, 4, &dev->ifindex);
1389
1390 mp_head = (struct rtattr*)skb_put(skb, RTA_LENGTH(0));
1391
1392 for (ct = c->mfc_minvif; ct < c->mfc_maxvif; ct++) {
1393 if (c->mfc_ttls[ct] < 255) {
1394 if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
1395 goto rtattr_failure;
1396 nhp = (struct rtnexthop*)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
1397 nhp->rtnh_flags = 0;
1398 nhp->rtnh_hops = c->mfc_ttls[ct];
1399 nhp->rtnh_ifindex = vif_table[ct].dev->ifindex;
1400 nhp->rtnh_len = sizeof(*nhp);
1401 }
1402 }
1403 mp_head->rta_type = RTA_MULTIPATH;
1404 mp_head->rta_len = skb->tail - (u8*)mp_head;
1405 rtm->rtm_type = RTN_MULTICAST;
1406 return 1;
1407
1408rtattr_failure:
1409 skb_trim(skb, b - skb->data);
1410 return -EMSGSIZE;
1411}
1412
1413int ipmr_get_route(struct sk_buff *skb, struct rtmsg *rtm, int nowait)
1414{
1415 struct mfc_cache *cache;
1416 struct rtable *rt = (struct rtable*)skb->dst;
1417
1418 start_bh_atomic();
1419 cache = ipmr_cache_find(rt->rt_src, rt->rt_dst);
1420 if (cache==NULL || (cache->mfc_flags&MFC_QUEUED)) {
1421 struct device *dev;
1422 int vif;
1423 int err;
1424
1425 if (nowait) {
1426 end_bh_atomic();
1427 return -EAGAIN;
1428 }
1429
1430 dev = skb->dev;
1431 if (dev == NULL || (vif = ipmr_find_vif(dev)) == ALL_VIFS) {
1432 end_bh_atomic();
1433 return -ENODEV;
1434 }
1435 skb->nh.raw = skb_push(skb, sizeof(struct iphdr));
1436 skb->nh.iph->ihl = sizeof(struct iphdr)>>2;
1437 skb->nh.iph->saddr = rt->rt_src;
1438 skb->nh.iph->daddr = rt->rt_dst;
1439 skb->nh.iph->version = 0;
1440 err = ipmr_cache_unresolved(cache, vif, skb);
1441 end_bh_atomic();
1442 return err;
1443 }
1444
1445
1446
1447 end_bh_atomic();
1448
1449 if (!nowait && (rtm->rtm_flags&RTM_F_NOTIFY))
1450 cache->mfc_flags |= MFC_NOTIFY;
1451 return ipmr_fill_mroute(skb, cache, rtm);
1452}
1453#endif
1454
1455
1456
1457
1458
1459int ipmr_vif_info(char *buffer, char **start, off_t offset, int length, int dummy)
1460{
1461 struct vif_device *vif;
1462 int len=0;
1463 off_t pos=0;
1464 off_t begin=0;
1465 int size;
1466 int ct;
1467
1468 len += sprintf(buffer,
1469 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
1470 pos=len;
1471
1472 for (ct=0;ct<maxvif;ct++)
1473 {
1474 char *name = "none";
1475 vif=&vif_table[ct];
1476 if(!(vifc_map&(1<<ct)))
1477 continue;
1478 if (vif->dev)
1479 name = vif->dev->name;
1480 size = sprintf(buffer+len, "%2d %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
1481 ct, name, vif->bytes_in, vif->pkt_in, vif->bytes_out, vif->pkt_out,
1482 vif->flags, vif->local, vif->remote);
1483 len+=size;
1484 pos+=size;
1485 if(pos<offset)
1486 {
1487 len=0;
1488 begin=pos;
1489 }
1490 if(pos>offset+length)
1491 break;
1492 }
1493
1494 *start=buffer+(offset-begin);
1495 len-=(offset-begin);
1496 if(len>length)
1497 len=length;
1498 return len;
1499}
1500
1501int ipmr_mfc_info(char *buffer, char **start, off_t offset, int length, int dummy)
1502{
1503 struct mfc_cache *mfc;
1504 int len=0;
1505 off_t pos=0;
1506 off_t begin=0;
1507 int size;
1508 int ct;
1509
1510 len += sprintf(buffer,
1511 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
1512 pos=len;
1513
1514 for (ct=0;ct<MFC_LINES;ct++)
1515 {
1516 start_bh_atomic();
1517 mfc=mfc_cache_array[ct];
1518 while(mfc!=NULL)
1519 {
1520 int n;
1521
1522
1523
1524
1525 size = sprintf(buffer+len, "%08lX %08lX %-3d %8ld %8ld %8ld",
1526 (unsigned long)mfc->mfc_mcastgrp,
1527 (unsigned long)mfc->mfc_origin,
1528 mfc->mfc_parent == ALL_VIFS ? -1 : mfc->mfc_parent,
1529 (mfc->mfc_flags & MFC_QUEUED) ? mfc->mfc_unresolved.qlen : mfc->mfc_pkt,
1530 mfc->mfc_bytes,
1531 mfc->mfc_wrong_if);
1532 for(n=mfc->mfc_minvif;n<mfc->mfc_maxvif;n++)
1533 {
1534 if(vifc_map&(1<<n) && mfc->mfc_ttls[n] < 255)
1535 size += sprintf(buffer+len+size, " %2d:%-3d", n, mfc->mfc_ttls[n]);
1536 }
1537 size += sprintf(buffer+len+size, "\n");
1538 len+=size;
1539 pos+=size;
1540 if(pos<offset)
1541 {
1542 len=0;
1543 begin=pos;
1544 }
1545 if(pos>offset+length)
1546 {
1547 end_bh_atomic();
1548 goto done;
1549 }
1550 mfc=mfc->next;
1551 }
1552 end_bh_atomic();
1553 }
1554done:
1555 *start=buffer+(offset-begin);
1556 len-=(offset-begin);
1557 if(len>length)
1558 len=length;
1559 if (len < 0) {
1560 len = 0;
1561 }
1562 return len;
1563}
1564
1565#ifdef CONFIG_PROC_FS
1566static struct proc_dir_entry proc_net_ipmr_vif = {
1567 PROC_NET_IPMR_VIF, 9 ,"ip_mr_vif",
1568 S_IFREG | S_IRUGO, 1, 0, 0,
1569 0, &proc_net_inode_operations,
1570 ipmr_vif_info
1571};
1572static struct proc_dir_entry proc_net_ipmr_mfc = {
1573 PROC_NET_IPMR_MFC, 11 ,"ip_mr_cache",
1574 S_IFREG | S_IRUGO, 1, 0, 0,
1575 0, &proc_net_inode_operations,
1576 ipmr_mfc_info
1577};
1578#endif
1579
1580#ifdef CONFIG_IP_PIMSM_V2
1581struct inet_protocol pim_protocol =
1582{
1583 pim_rcv,
1584 NULL,
1585 NULL,
1586 IPPROTO_PIM,
1587 0,
1588 NULL,
1589 "PIM"
1590};
1591#endif
1592
1593
1594
1595
1596
1597
1598__initfunc(void ip_mr_init(void))
1599{
1600 printk(KERN_INFO "Linux IP multicast router 0.06 plus PIM-SM\n");
1601 register_netdevice_notifier(&ip_mr_notifier);
1602#ifdef CONFIG_PROC_FS
1603 proc_net_register(&proc_net_ipmr_vif);
1604 proc_net_register(&proc_net_ipmr_mfc);
1605#endif
1606}
1607