2 * IP multicast routing support for mrouted 3.6/3.8
4 * (c) 1995 Alan Cox, <alan@redhat.com>
5 * Linux Consultancy and Custom Driver Development
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
13 * Michael Chastain : Incorrect size of copying.
14 * Alan Cox : Added the cache manager code
15 * Alan Cox : Fixed the clone/copy bug and device race.
16 * Mike McLagan : Routing by source
17 * Malcolm Beattie : Buffer handling fixes.
18 * Alexey Kuznetsov : Double buffer free and other fixes.
19 * SVR Anand : Fixed several multicast bugs and problems.
20 * Alexey Kuznetsov : Status, optimisations and more.
21 * Brad Parker : Better behaviour on mrouted upcall
23 * Carlos Picoto : PIMv1 Support
24 * Pavlin Ivanov Radoslavov: PIMv2 Registers must checksum only PIM header
25 * Relax this requrement to work with older peers.
29 #include <asm/system.h>
30 #include <asm/uaccess.h>
31 #include <linux/types.h>
32 #include <linux/capability.h>
33 #include <linux/errno.h>
34 #include <linux/timer.h>
36 #include <linux/kernel.h>
37 #include <linux/fcntl.h>
38 #include <linux/stat.h>
39 #include <linux/socket.h>
41 #include <linux/inet.h>
42 #include <linux/netdevice.h>
43 #include <linux/inetdevice.h>
44 #include <linux/igmp.h>
45 #include <linux/proc_fs.h>
46 #include <linux/seq_file.h>
47 #include <linux/mroute.h>
48 #include <linux/init.h>
49 #include <linux/if_ether.h>
50 #include <net/net_namespace.h>
52 #include <net/protocol.h>
53 #include <linux/skbuff.h>
54 #include <net/route.h>
59 #include <linux/notifier.h>
60 #include <linux/if_arp.h>
61 #include <linux/netfilter_ipv4.h>
63 #include <net/checksum.h>
64 #include <net/netlink.h>
66 #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
67 #define CONFIG_IP_PIMSM 1
70 static struct sock *mroute_socket;
73 /* Big lock, protecting vif table, mrt cache and mroute socket state.
74 Note that the changes are semaphored via rtnl_lock.
77 static DEFINE_RWLOCK(mrt_lock);
80 * Multicast router control variables
83 static struct vif_device vif_table[MAXVIFS]; /* Devices */
86 #define VIF_EXISTS(idx) (vif_table[idx].dev != NULL)
88 static int mroute_do_assert; /* Set in PIM assert */
89 static int mroute_do_pim;
91 static struct mfc_cache *mfc_cache_array[MFC_LINES]; /* Forwarding cache */
93 static struct mfc_cache *mfc_unres_queue; /* Queue of unresolved entries */
94 static atomic_t cache_resolve_queue_len; /* Size of unresolved */
96 /* Special spinlock for queue of unresolved entries */
97 static DEFINE_SPINLOCK(mfc_unres_lock);
99 /* We return to original Alan's scheme. Hash table of resolved
100 entries is changed only in process context and protected
101 with weak lock mrt_lock. Queue of unresolved entries is protected
102 with strong spinlock mfc_unres_lock.
104 In this case data path is free of exclusive locks at all.
107 static struct kmem_cache *mrt_cachep __read_mostly;
109 static int ip_mr_forward(struct sk_buff *skb, struct mfc_cache *cache, int local);
110 static int ipmr_cache_report(struct sk_buff *pkt, vifi_t vifi, int assert);
111 static int ipmr_fill_mroute(struct sk_buff *skb, struct mfc_cache *c, struct rtmsg *rtm);
113 #ifdef CONFIG_IP_PIMSM_V2
114 static struct net_protocol pim_protocol;
117 static struct timer_list ipmr_expire_timer;
119 /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
122 struct net_device *ipmr_new_tunnel(struct vifctl *v)
124 struct net_device *dev;
126 dev = __dev_get_by_name(&init_net, "tunl0");
132 struct ip_tunnel_parm p;
133 struct in_device *in_dev;
135 memset(&p, 0, sizeof(p));
136 p.iph.daddr = v->vifc_rmt_addr.s_addr;
137 p.iph.saddr = v->vifc_lcl_addr.s_addr;
140 p.iph.protocol = IPPROTO_IPIP;
141 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
142 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
144 oldfs = get_fs(); set_fs(KERNEL_DS);
145 err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
150 if (err == 0 && (dev = __dev_get_by_name(&init_net, p.name)) != NULL) {
151 dev->flags |= IFF_MULTICAST;
153 in_dev = __in_dev_get_rtnl(dev);
157 ipv4_devconf_setall(in_dev);
158 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
167 /* allow the register to be completed before unregistering. */
171 unregister_netdevice(dev);
175 #ifdef CONFIG_IP_PIMSM
177 static int reg_vif_num = -1;
179 static int reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
181 read_lock(&mrt_lock);
182 dev->stats.tx_bytes += skb->len;
183 dev->stats.tx_packets++;
184 ipmr_cache_report(skb, reg_vif_num, IGMPMSG_WHOLEPKT);
185 read_unlock(&mrt_lock);
190 static void reg_vif_setup(struct net_device *dev)
192 dev->type = ARPHRD_PIMREG;
193 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
194 dev->flags = IFF_NOARP;
195 dev->hard_start_xmit = reg_vif_xmit;
196 dev->destructor = free_netdev;
199 static struct net_device *ipmr_reg_vif(void)
201 struct net_device *dev;
202 struct in_device *in_dev;
204 dev = alloc_netdev(0, "pimreg", reg_vif_setup);
209 if (register_netdevice(dev)) {
216 if ((in_dev = __in_dev_get_rcu(dev)) == NULL) {
221 ipv4_devconf_setall(in_dev);
222 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
231 /* allow the register to be completed before unregistering. */
235 unregister_netdevice(dev);
244 static int vif_delete(int vifi)
246 struct vif_device *v;
247 struct net_device *dev;
248 struct in_device *in_dev;
250 if (vifi < 0 || vifi >= maxvif)
251 return -EADDRNOTAVAIL;
253 v = &vif_table[vifi];
255 write_lock_bh(&mrt_lock);
260 write_unlock_bh(&mrt_lock);
261 return -EADDRNOTAVAIL;
264 #ifdef CONFIG_IP_PIMSM
265 if (vifi == reg_vif_num)
269 if (vifi+1 == maxvif) {
271 for (tmp=vifi-1; tmp>=0; tmp--) {
278 write_unlock_bh(&mrt_lock);
280 dev_set_allmulti(dev, -1);
282 if ((in_dev = __in_dev_get_rtnl(dev)) != NULL) {
283 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
284 ip_rt_multicast_event(in_dev);
287 if (v->flags&(VIFF_TUNNEL|VIFF_REGISTER))
288 unregister_netdevice(dev);
294 /* Destroy an unresolved cache entry, killing queued skbs
295 and reporting error to netlink readers.
298 static void ipmr_destroy_unres(struct mfc_cache *c)
303 atomic_dec(&cache_resolve_queue_len);
305 while ((skb=skb_dequeue(&c->mfc_un.unres.unresolved))) {
306 if (ip_hdr(skb)->version == 0) {
307 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
308 nlh->nlmsg_type = NLMSG_ERROR;
309 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
310 skb_trim(skb, nlh->nlmsg_len);
312 e->error = -ETIMEDOUT;
313 memset(&e->msg, 0, sizeof(e->msg));
315 rtnl_unicast(skb, &init_net, NETLINK_CB(skb).pid);
320 kmem_cache_free(mrt_cachep, c);
324 /* Single timer process for all the unresolved queue. */
326 static void ipmr_expire_process(unsigned long dummy)
329 unsigned long expires;
330 struct mfc_cache *c, **cp;
332 if (!spin_trylock(&mfc_unres_lock)) {
333 mod_timer(&ipmr_expire_timer, jiffies+HZ/10);
337 if (atomic_read(&cache_resolve_queue_len) == 0)
342 cp = &mfc_unres_queue;
344 while ((c=*cp) != NULL) {
345 if (time_after(c->mfc_un.unres.expires, now)) {
346 unsigned long interval = c->mfc_un.unres.expires - now;
347 if (interval < expires)
355 ipmr_destroy_unres(c);
358 if (atomic_read(&cache_resolve_queue_len))
359 mod_timer(&ipmr_expire_timer, jiffies + expires);
362 spin_unlock(&mfc_unres_lock);
365 /* Fill oifs list. It is called under write locked mrt_lock. */
367 static void ipmr_update_thresholds(struct mfc_cache *cache, unsigned char *ttls)
371 cache->mfc_un.res.minvif = MAXVIFS;
372 cache->mfc_un.res.maxvif = 0;
373 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
375 for (vifi=0; vifi<maxvif; vifi++) {
376 if (VIF_EXISTS(vifi) && ttls[vifi] && ttls[vifi] < 255) {
377 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
378 if (cache->mfc_un.res.minvif > vifi)
379 cache->mfc_un.res.minvif = vifi;
380 if (cache->mfc_un.res.maxvif <= vifi)
381 cache->mfc_un.res.maxvif = vifi + 1;
386 static int vif_add(struct vifctl *vifc, int mrtsock)
388 int vifi = vifc->vifc_vifi;
389 struct vif_device *v = &vif_table[vifi];
390 struct net_device *dev;
391 struct in_device *in_dev;
394 if (VIF_EXISTS(vifi))
397 switch (vifc->vifc_flags) {
398 #ifdef CONFIG_IP_PIMSM
401 * Special Purpose VIF in PIM
402 * All the packets will be sent to the daemon
404 if (reg_vif_num >= 0)
406 dev = ipmr_reg_vif();
412 dev = ipmr_new_tunnel(vifc);
417 dev = ip_dev_find(&init_net, vifc->vifc_lcl_addr.s_addr);
419 return -EADDRNOTAVAIL;
426 if ((in_dev = __in_dev_get_rtnl(dev)) == NULL)
427 return -EADDRNOTAVAIL;
428 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
429 dev_set_allmulti(dev, +1);
430 ip_rt_multicast_event(in_dev);
433 * Fill in the VIF structures
435 v->rate_limit=vifc->vifc_rate_limit;
436 v->local=vifc->vifc_lcl_addr.s_addr;
437 v->remote=vifc->vifc_rmt_addr.s_addr;
438 v->flags=vifc->vifc_flags;
440 v->flags |= VIFF_STATIC;
441 v->threshold=vifc->vifc_threshold;
446 v->link = dev->ifindex;
447 if (v->flags&(VIFF_TUNNEL|VIFF_REGISTER))
448 v->link = dev->iflink;
450 /* And finish update writing critical data */
451 write_lock_bh(&mrt_lock);
454 #ifdef CONFIG_IP_PIMSM
455 if (v->flags&VIFF_REGISTER)
460 write_unlock_bh(&mrt_lock);
464 static struct mfc_cache *ipmr_cache_find(__be32 origin, __be32 mcastgrp)
466 int line=MFC_HASH(mcastgrp,origin);
469 for (c=mfc_cache_array[line]; c; c = c->next) {
470 if (c->mfc_origin==origin && c->mfc_mcastgrp==mcastgrp)
477 * Allocate a multicast cache entry
479 static struct mfc_cache *ipmr_cache_alloc(void)
481 struct mfc_cache *c=kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
484 c->mfc_un.res.minvif = MAXVIFS;
488 static struct mfc_cache *ipmr_cache_alloc_unres(void)
490 struct mfc_cache *c=kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
493 skb_queue_head_init(&c->mfc_un.unres.unresolved);
494 c->mfc_un.unres.expires = jiffies + 10*HZ;
499 * A cache entry has gone into a resolved state from queued
502 static void ipmr_cache_resolve(struct mfc_cache *uc, struct mfc_cache *c)
508 * Play the pending entries through our router
511 while ((skb=__skb_dequeue(&uc->mfc_un.unres.unresolved))) {
512 if (ip_hdr(skb)->version == 0) {
513 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
515 if (ipmr_fill_mroute(skb, c, NLMSG_DATA(nlh)) > 0) {
516 nlh->nlmsg_len = (skb_tail_pointer(skb) -
519 nlh->nlmsg_type = NLMSG_ERROR;
520 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
521 skb_trim(skb, nlh->nlmsg_len);
523 e->error = -EMSGSIZE;
524 memset(&e->msg, 0, sizeof(e->msg));
527 rtnl_unicast(skb, &init_net, NETLINK_CB(skb).pid);
529 ip_mr_forward(skb, c, 0);
534 * Bounce a cache query up to mrouted. We could use netlink for this but mrouted
535 * expects the following bizarre scheme.
537 * Called under mrt_lock.
540 static int ipmr_cache_report(struct sk_buff *pkt, vifi_t vifi, int assert)
543 const int ihl = ip_hdrlen(pkt);
544 struct igmphdr *igmp;
548 #ifdef CONFIG_IP_PIMSM
549 if (assert == IGMPMSG_WHOLEPKT)
550 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
553 skb = alloc_skb(128, GFP_ATOMIC);
558 #ifdef CONFIG_IP_PIMSM
559 if (assert == IGMPMSG_WHOLEPKT) {
560 /* Ugly, but we have no choice with this interface.
561 Duplicate old header, fix ihl, length etc.
562 And all this only to mangle msg->im_msgtype and
563 to set msg->im_mbz to "mbz" :-)
565 skb_push(skb, sizeof(struct iphdr));
566 skb_reset_network_header(skb);
567 skb_reset_transport_header(skb);
568 msg = (struct igmpmsg *)skb_network_header(skb);
569 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
570 msg->im_msgtype = IGMPMSG_WHOLEPKT;
572 msg->im_vif = reg_vif_num;
573 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
574 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
575 sizeof(struct iphdr));
584 skb->network_header = skb->tail;
586 skb_copy_to_linear_data(skb, pkt->data, ihl);
587 ip_hdr(skb)->protocol = 0; /* Flag to the kernel this is a route add */
588 msg = (struct igmpmsg *)skb_network_header(skb);
590 skb->dst = dst_clone(pkt->dst);
596 igmp=(struct igmphdr *)skb_put(skb,sizeof(struct igmphdr));
598 msg->im_msgtype = assert;
600 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
601 skb->transport_header = skb->network_header;
604 if (mroute_socket == NULL) {
612 if ((ret=sock_queue_rcv_skb(mroute_socket,skb))<0) {
614 printk(KERN_WARNING "mroute: pending queue full, dropping entries.\n");
622 * Queue a packet for resolution. It gets locked cache entry!
626 ipmr_cache_unresolved(vifi_t vifi, struct sk_buff *skb)
630 const struct iphdr *iph = ip_hdr(skb);
632 spin_lock_bh(&mfc_unres_lock);
633 for (c=mfc_unres_queue; c; c=c->next) {
634 if (c->mfc_mcastgrp == iph->daddr &&
635 c->mfc_origin == iph->saddr)
641 * Create a new entry if allowable
644 if (atomic_read(&cache_resolve_queue_len)>=10 ||
645 (c=ipmr_cache_alloc_unres())==NULL) {
646 spin_unlock_bh(&mfc_unres_lock);
653 * Fill in the new cache entry
656 c->mfc_origin = iph->saddr;
657 c->mfc_mcastgrp = iph->daddr;
660 * Reflect first query at mrouted.
662 if ((err = ipmr_cache_report(skb, vifi, IGMPMSG_NOCACHE))<0) {
663 /* If the report failed throw the cache entry
666 spin_unlock_bh(&mfc_unres_lock);
668 kmem_cache_free(mrt_cachep, c);
673 atomic_inc(&cache_resolve_queue_len);
674 c->next = mfc_unres_queue;
677 mod_timer(&ipmr_expire_timer, c->mfc_un.unres.expires);
681 * See if we can append the packet
683 if (c->mfc_un.unres.unresolved.qlen>3) {
687 skb_queue_tail(&c->mfc_un.unres.unresolved,skb);
691 spin_unlock_bh(&mfc_unres_lock);
696 * MFC cache manipulation by user space mroute daemon
699 static int ipmr_mfc_delete(struct mfcctl *mfc)
702 struct mfc_cache *c, **cp;
704 line=MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
706 for (cp=&mfc_cache_array[line]; (c=*cp) != NULL; cp = &c->next) {
707 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
708 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
709 write_lock_bh(&mrt_lock);
711 write_unlock_bh(&mrt_lock);
713 kmem_cache_free(mrt_cachep, c);
720 static int ipmr_mfc_add(struct mfcctl *mfc, int mrtsock)
723 struct mfc_cache *uc, *c, **cp;
725 line=MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
727 for (cp=&mfc_cache_array[line]; (c=*cp) != NULL; cp = &c->next) {
728 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
729 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr)
734 write_lock_bh(&mrt_lock);
735 c->mfc_parent = mfc->mfcc_parent;
736 ipmr_update_thresholds(c, mfc->mfcc_ttls);
738 c->mfc_flags |= MFC_STATIC;
739 write_unlock_bh(&mrt_lock);
743 if (!ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
746 c=ipmr_cache_alloc();
750 c->mfc_origin=mfc->mfcc_origin.s_addr;
751 c->mfc_mcastgrp=mfc->mfcc_mcastgrp.s_addr;
752 c->mfc_parent=mfc->mfcc_parent;
753 ipmr_update_thresholds(c, mfc->mfcc_ttls);
755 c->mfc_flags |= MFC_STATIC;
757 write_lock_bh(&mrt_lock);
758 c->next = mfc_cache_array[line];
759 mfc_cache_array[line] = c;
760 write_unlock_bh(&mrt_lock);
763 * Check to see if we resolved a queued list. If so we
764 * need to send on the frames and tidy up.
766 spin_lock_bh(&mfc_unres_lock);
767 for (cp = &mfc_unres_queue; (uc=*cp) != NULL;
769 if (uc->mfc_origin == c->mfc_origin &&
770 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
772 if (atomic_dec_and_test(&cache_resolve_queue_len))
773 del_timer(&ipmr_expire_timer);
777 spin_unlock_bh(&mfc_unres_lock);
780 ipmr_cache_resolve(uc, c);
781 kmem_cache_free(mrt_cachep, uc);
787 * Close the multicast socket, and clear the vif tables etc
790 static void mroute_clean_tables(struct sock *sk)
795 * Shut down all active vif entries
797 for (i=0; i<maxvif; i++) {
798 if (!(vif_table[i].flags&VIFF_STATIC))
805 for (i=0;i<MFC_LINES;i++) {
806 struct mfc_cache *c, **cp;
808 cp = &mfc_cache_array[i];
809 while ((c = *cp) != NULL) {
810 if (c->mfc_flags&MFC_STATIC) {
814 write_lock_bh(&mrt_lock);
816 write_unlock_bh(&mrt_lock);
818 kmem_cache_free(mrt_cachep, c);
822 if (atomic_read(&cache_resolve_queue_len) != 0) {
825 spin_lock_bh(&mfc_unres_lock);
826 while (mfc_unres_queue != NULL) {
828 mfc_unres_queue = c->next;
829 spin_unlock_bh(&mfc_unres_lock);
831 ipmr_destroy_unres(c);
833 spin_lock_bh(&mfc_unres_lock);
835 spin_unlock_bh(&mfc_unres_lock);
839 static void mrtsock_destruct(struct sock *sk)
842 if (sk == mroute_socket) {
843 IPV4_DEVCONF_ALL(sock_net(sk), MC_FORWARDING)--;
845 write_lock_bh(&mrt_lock);
847 write_unlock_bh(&mrt_lock);
849 mroute_clean_tables(sk);
855 * Socket options and virtual interface manipulation. The whole
856 * virtual interface system is a complete heap, but unfortunately
857 * that's how BSD mrouted happens to think. Maybe one day with a proper
858 * MOSPF/PIM router set up we can clean this up.
861 int ip_mroute_setsockopt(struct sock *sk,int optname,char __user *optval,int optlen)
867 if (optname != MRT_INIT) {
868 if (sk != mroute_socket && !capable(CAP_NET_ADMIN))
874 if (sk->sk_type != SOCK_RAW ||
875 inet_sk(sk)->num != IPPROTO_IGMP)
877 if (optlen!=sizeof(int))
886 ret = ip_ra_control(sk, 1, mrtsock_destruct);
888 write_lock_bh(&mrt_lock);
890 write_unlock_bh(&mrt_lock);
892 IPV4_DEVCONF_ALL(sock_net(sk), MC_FORWARDING)++;
897 if (sk!=mroute_socket)
899 return ip_ra_control(sk, 0, NULL);
902 if (optlen!=sizeof(vif))
904 if (copy_from_user(&vif,optval,sizeof(vif)))
906 if (vif.vifc_vifi >= MAXVIFS)
909 if (optname==MRT_ADD_VIF) {
910 ret = vif_add(&vif, sk==mroute_socket);
912 ret = vif_delete(vif.vifc_vifi);
918 * Manipulate the forwarding caches. These live
919 * in a sort of kernel/user symbiosis.
923 if (optlen!=sizeof(mfc))
925 if (copy_from_user(&mfc,optval, sizeof(mfc)))
928 if (optname==MRT_DEL_MFC)
929 ret = ipmr_mfc_delete(&mfc);
931 ret = ipmr_mfc_add(&mfc, sk==mroute_socket);
935 * Control PIM assert.
940 if (get_user(v,(int __user *)optval))
942 mroute_do_assert=(v)?1:0;
945 #ifdef CONFIG_IP_PIMSM
950 if (get_user(v,(int __user *)optval))
956 if (v != mroute_do_pim) {
958 mroute_do_assert = v;
959 #ifdef CONFIG_IP_PIMSM_V2
961 ret = inet_add_protocol(&pim_protocol,
964 ret = inet_del_protocol(&pim_protocol,
975 * Spurious command, or MRT_VERSION which you cannot
984 * Getsock opt support for the multicast routing system.
987 int ip_mroute_getsockopt(struct sock *sk,int optname,char __user *optval,int __user *optlen)
992 if (optname!=MRT_VERSION &&
993 #ifdef CONFIG_IP_PIMSM
999 if (get_user(olr, optlen))
1002 olr = min_t(unsigned int, olr, sizeof(int));
1006 if (put_user(olr,optlen))
1008 if (optname==MRT_VERSION)
1010 #ifdef CONFIG_IP_PIMSM
1011 else if (optname==MRT_PIM)
1015 val=mroute_do_assert;
1016 if (copy_to_user(optval,&val,olr))
1022 * The IP multicast ioctl support routines.
1025 int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1027 struct sioc_sg_req sr;
1028 struct sioc_vif_req vr;
1029 struct vif_device *vif;
1030 struct mfc_cache *c;
1034 if (copy_from_user(&vr,arg,sizeof(vr)))
1036 if (vr.vifi>=maxvif)
1038 read_lock(&mrt_lock);
1039 vif=&vif_table[vr.vifi];
1040 if (VIF_EXISTS(vr.vifi)) {
1041 vr.icount=vif->pkt_in;
1042 vr.ocount=vif->pkt_out;
1043 vr.ibytes=vif->bytes_in;
1044 vr.obytes=vif->bytes_out;
1045 read_unlock(&mrt_lock);
1047 if (copy_to_user(arg,&vr,sizeof(vr)))
1051 read_unlock(&mrt_lock);
1052 return -EADDRNOTAVAIL;
1054 if (copy_from_user(&sr,arg,sizeof(sr)))
1057 read_lock(&mrt_lock);
1058 c = ipmr_cache_find(sr.src.s_addr, sr.grp.s_addr);
1060 sr.pktcnt = c->mfc_un.res.pkt;
1061 sr.bytecnt = c->mfc_un.res.bytes;
1062 sr.wrong_if = c->mfc_un.res.wrong_if;
1063 read_unlock(&mrt_lock);
1065 if (copy_to_user(arg,&sr,sizeof(sr)))
1069 read_unlock(&mrt_lock);
1070 return -EADDRNOTAVAIL;
1072 return -ENOIOCTLCMD;
1077 static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1079 struct net_device *dev = ptr;
1080 struct vif_device *v;
1083 if (dev_net(dev) != &init_net)
1086 if (event != NETDEV_UNREGISTER)
1089 for (ct=0;ct<maxvif;ct++,v++) {
1097 static struct notifier_block ip_mr_notifier={
1098 .notifier_call = ipmr_device_event,
1102 * Encapsulate a packet by attaching a valid IPIP header to it.
1103 * This avoids tunnel drivers and other mess and gives us the speed so
1104 * important for multicast video.
1107 static void ip_encap(struct sk_buff *skb, __be32 saddr, __be32 daddr)
1110 struct iphdr *old_iph = ip_hdr(skb);
1112 skb_push(skb, sizeof(struct iphdr));
1113 skb->transport_header = skb->network_header;
1114 skb_reset_network_header(skb);
1118 iph->tos = old_iph->tos;
1119 iph->ttl = old_iph->ttl;
1123 iph->protocol = IPPROTO_IPIP;
1125 iph->tot_len = htons(skb->len);
1126 ip_select_ident(iph, skb->dst, NULL);
1129 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1133 static inline int ipmr_forward_finish(struct sk_buff *skb)
1135 struct ip_options * opt = &(IPCB(skb)->opt);
1137 IP_INC_STATS_BH(IPSTATS_MIB_OUTFORWDATAGRAMS);
1139 if (unlikely(opt->optlen))
1140 ip_forward_options(skb);
1142 return dst_output(skb);
1146 * Processing handlers for ipmr_forward
1149 static void ipmr_queue_xmit(struct sk_buff *skb, struct mfc_cache *c, int vifi)
1151 const struct iphdr *iph = ip_hdr(skb);
1152 struct vif_device *vif = &vif_table[vifi];
1153 struct net_device *dev;
1157 if (vif->dev == NULL)
1160 #ifdef CONFIG_IP_PIMSM
1161 if (vif->flags & VIFF_REGISTER) {
1163 vif->bytes_out+=skb->len;
1164 vif->dev->stats.tx_bytes += skb->len;
1165 vif->dev->stats.tx_packets++;
1166 ipmr_cache_report(skb, vifi, IGMPMSG_WHOLEPKT);
1172 if (vif->flags&VIFF_TUNNEL) {
1173 struct flowi fl = { .oif = vif->link,
1175 { .daddr = vif->remote,
1176 .saddr = vif->local,
1177 .tos = RT_TOS(iph->tos) } },
1178 .proto = IPPROTO_IPIP };
1179 if (ip_route_output_key(&init_net, &rt, &fl))
1181 encap = sizeof(struct iphdr);
1183 struct flowi fl = { .oif = vif->link,
1185 { .daddr = iph->daddr,
1186 .tos = RT_TOS(iph->tos) } },
1187 .proto = IPPROTO_IPIP };
1188 if (ip_route_output_key(&init_net, &rt, &fl))
1192 dev = rt->u.dst.dev;
1194 if (skb->len+encap > dst_mtu(&rt->u.dst) && (ntohs(iph->frag_off) & IP_DF)) {
1195 /* Do not fragment multicasts. Alas, IPv4 does not
1196 allow to send ICMP, so that packets will disappear
1200 IP_INC_STATS_BH(IPSTATS_MIB_FRAGFAILS);
1205 encap += LL_RESERVED_SPACE(dev) + rt->u.dst.header_len;
1207 if (skb_cow(skb, encap)) {
1213 vif->bytes_out+=skb->len;
1215 dst_release(skb->dst);
1216 skb->dst = &rt->u.dst;
1217 ip_decrease_ttl(ip_hdr(skb));
1219 /* FIXME: forward and output firewalls used to be called here.
1220 * What do we do with netfilter? -- RR */
1221 if (vif->flags & VIFF_TUNNEL) {
1222 ip_encap(skb, vif->local, vif->remote);
1223 /* FIXME: extra output firewall step used to be here. --RR */
1224 vif->dev->stats.tx_packets++;
1225 vif->dev->stats.tx_bytes += skb->len;
1228 IPCB(skb)->flags |= IPSKB_FORWARDED;
1231 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1232 * not only before forwarding, but after forwarding on all output
1233 * interfaces. It is clear, if mrouter runs a multicasting
1234 * program, it should receive packets not depending to what interface
1235 * program is joined.
1236 * If we will not make it, the program will have to join on all
1237 * interfaces. On the other hand, multihoming host (or router, but
1238 * not mrouter) cannot join to more than one interface - it will
1239 * result in receiving multiple packets.
1241 NF_HOOK(PF_INET, NF_INET_FORWARD, skb, skb->dev, dev,
1242 ipmr_forward_finish);
1250 static int ipmr_find_vif(struct net_device *dev)
1253 for (ct=maxvif-1; ct>=0; ct--) {
1254 if (vif_table[ct].dev == dev)
1260 /* "local" means that we should preserve one skb (for local delivery) */
1262 static int ip_mr_forward(struct sk_buff *skb, struct mfc_cache *cache, int local)
1267 vif = cache->mfc_parent;
1268 cache->mfc_un.res.pkt++;
1269 cache->mfc_un.res.bytes += skb->len;
1272 * Wrong interface: drop packet and (maybe) send PIM assert.
1274 if (vif_table[vif].dev != skb->dev) {
1277 if (skb->rtable->fl.iif == 0) {
1278 /* It is our own packet, looped back.
1279 Very complicated situation...
1281 The best workaround until routing daemons will be
1282 fixed is not to redistribute packet, if it was
1283 send through wrong interface. It means, that
1284 multicast applications WILL NOT work for
1285 (S,G), which have default multicast route pointing
1286 to wrong oif. In any case, it is not a good
1287 idea to use multicasting applications on router.
1292 cache->mfc_un.res.wrong_if++;
1293 true_vifi = ipmr_find_vif(skb->dev);
1295 if (true_vifi >= 0 && mroute_do_assert &&
1296 /* pimsm uses asserts, when switching from RPT to SPT,
1297 so that we cannot check that packet arrived on an oif.
1298 It is bad, but otherwise we would need to move pretty
1299 large chunk of pimd to kernel. Ough... --ANK
1301 (mroute_do_pim || cache->mfc_un.res.ttls[true_vifi] < 255) &&
1303 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1304 cache->mfc_un.res.last_assert = jiffies;
1305 ipmr_cache_report(skb, true_vifi, IGMPMSG_WRONGVIF);
1310 vif_table[vif].pkt_in++;
1311 vif_table[vif].bytes_in+=skb->len;
1316 for (ct = cache->mfc_un.res.maxvif-1; ct >= cache->mfc_un.res.minvif; ct--) {
1317 if (ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
1319 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1321 ipmr_queue_xmit(skb2, cache, psend);
1328 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1330 ipmr_queue_xmit(skb2, cache, psend);
1332 ipmr_queue_xmit(skb, cache, psend);
1345 * Multicast packets for forwarding arrive here
1348 int ip_mr_input(struct sk_buff *skb)
1350 struct mfc_cache *cache;
1351 int local = skb->rtable->rt_flags&RTCF_LOCAL;
1353 /* Packet is looped back after forward, it should not be
1354 forwarded second time, but still can be delivered locally.
1356 if (IPCB(skb)->flags&IPSKB_FORWARDED)
1360 if (IPCB(skb)->opt.router_alert) {
1361 if (ip_call_ra_chain(skb))
1363 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP){
1364 /* IGMPv1 (and broken IGMPv2 implementations sort of
1365 Cisco IOS <= 11.2(8)) do not put router alert
1366 option to IGMP packets destined to routable
1367 groups. It is very bad, because it means
1368 that we can forward NO IGMP messages.
1370 read_lock(&mrt_lock);
1371 if (mroute_socket) {
1373 raw_rcv(mroute_socket, skb);
1374 read_unlock(&mrt_lock);
1377 read_unlock(&mrt_lock);
1381 read_lock(&mrt_lock);
1382 cache = ipmr_cache_find(ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
1385 * No usable cache entry
1391 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1392 ip_local_deliver(skb);
1394 read_unlock(&mrt_lock);
1400 vif = ipmr_find_vif(skb->dev);
1402 int err = ipmr_cache_unresolved(vif, skb);
1403 read_unlock(&mrt_lock);
1407 read_unlock(&mrt_lock);
1412 ip_mr_forward(skb, cache, local);
1414 read_unlock(&mrt_lock);
1417 return ip_local_deliver(skb);
1423 return ip_local_deliver(skb);
1428 #ifdef CONFIG_IP_PIMSM_V1
1430 * Handle IGMP messages of PIMv1
1433 int pim_rcv_v1(struct sk_buff * skb)
1435 struct igmphdr *pim;
1436 struct iphdr *encap;
1437 struct net_device *reg_dev = NULL;
1439 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(*encap)))
1442 pim = igmp_hdr(skb);
1444 if (!mroute_do_pim ||
1445 skb->len < sizeof(*pim) + sizeof(*encap) ||
1446 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
1449 encap = (struct iphdr *)(skb_transport_header(skb) +
1450 sizeof(struct igmphdr));
1453 a. packet is really destinted to a multicast group
1454 b. packet is not a NULL-REGISTER
1455 c. packet is not truncated
1457 if (!ipv4_is_multicast(encap->daddr) ||
1458 encap->tot_len == 0 ||
1459 ntohs(encap->tot_len) + sizeof(*pim) > skb->len)
1462 read_lock(&mrt_lock);
1463 if (reg_vif_num >= 0)
1464 reg_dev = vif_table[reg_vif_num].dev;
1467 read_unlock(&mrt_lock);
1469 if (reg_dev == NULL)
1472 skb->mac_header = skb->network_header;
1473 skb_pull(skb, (u8*)encap - skb->data);
1474 skb_reset_network_header(skb);
1476 skb->protocol = htons(ETH_P_IP);
1478 skb->pkt_type = PACKET_HOST;
1479 dst_release(skb->dst);
1481 reg_dev->stats.rx_bytes += skb->len;
1482 reg_dev->stats.rx_packets++;
1493 #ifdef CONFIG_IP_PIMSM_V2
1494 static int pim_rcv(struct sk_buff * skb)
1496 struct pimreghdr *pim;
1497 struct iphdr *encap;
1498 struct net_device *reg_dev = NULL;
1500 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(*encap)))
1503 pim = (struct pimreghdr *)skb_transport_header(skb);
1504 if (pim->type != ((PIM_VERSION<<4)|(PIM_REGISTER)) ||
1505 (pim->flags&PIM_NULL_REGISTER) ||
1506 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
1507 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
1510 /* check if the inner packet is destined to mcast group */
1511 encap = (struct iphdr *)(skb_transport_header(skb) +
1512 sizeof(struct pimreghdr));
1513 if (!ipv4_is_multicast(encap->daddr) ||
1514 encap->tot_len == 0 ||
1515 ntohs(encap->tot_len) + sizeof(*pim) > skb->len)
1518 read_lock(&mrt_lock);
1519 if (reg_vif_num >= 0)
1520 reg_dev = vif_table[reg_vif_num].dev;
1523 read_unlock(&mrt_lock);
1525 if (reg_dev == NULL)
1528 skb->mac_header = skb->network_header;
1529 skb_pull(skb, (u8*)encap - skb->data);
1530 skb_reset_network_header(skb);
1532 skb->protocol = htons(ETH_P_IP);
1534 skb->pkt_type = PACKET_HOST;
1535 dst_release(skb->dst);
1536 reg_dev->stats.rx_bytes += skb->len;
1537 reg_dev->stats.rx_packets++;
1550 ipmr_fill_mroute(struct sk_buff *skb, struct mfc_cache *c, struct rtmsg *rtm)
1553 struct rtnexthop *nhp;
1554 struct net_device *dev = vif_table[c->mfc_parent].dev;
1555 u8 *b = skb_tail_pointer(skb);
1556 struct rtattr *mp_head;
1559 RTA_PUT(skb, RTA_IIF, 4, &dev->ifindex);
1561 mp_head = (struct rtattr*)skb_put(skb, RTA_LENGTH(0));
1563 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
1564 if (c->mfc_un.res.ttls[ct] < 255) {
1565 if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
1566 goto rtattr_failure;
1567 nhp = (struct rtnexthop*)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
1568 nhp->rtnh_flags = 0;
1569 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
1570 nhp->rtnh_ifindex = vif_table[ct].dev->ifindex;
1571 nhp->rtnh_len = sizeof(*nhp);
1574 mp_head->rta_type = RTA_MULTIPATH;
1575 mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head;
1576 rtm->rtm_type = RTN_MULTICAST;
1584 int ipmr_get_route(struct sk_buff *skb, struct rtmsg *rtm, int nowait)
1587 struct mfc_cache *cache;
1588 struct rtable *rt = skb->rtable;
1590 read_lock(&mrt_lock);
1591 cache = ipmr_cache_find(rt->rt_src, rt->rt_dst);
1594 struct sk_buff *skb2;
1596 struct net_device *dev;
1600 read_unlock(&mrt_lock);
1605 if (dev == NULL || (vif = ipmr_find_vif(dev)) < 0) {
1606 read_unlock(&mrt_lock);
1609 skb2 = skb_clone(skb, GFP_ATOMIC);
1611 read_unlock(&mrt_lock);
1615 skb_push(skb2, sizeof(struct iphdr));
1616 skb_reset_network_header(skb2);
1618 iph->ihl = sizeof(struct iphdr) >> 2;
1619 iph->saddr = rt->rt_src;
1620 iph->daddr = rt->rt_dst;
1622 err = ipmr_cache_unresolved(vif, skb2);
1623 read_unlock(&mrt_lock);
1627 if (!nowait && (rtm->rtm_flags&RTM_F_NOTIFY))
1628 cache->mfc_flags |= MFC_NOTIFY;
1629 err = ipmr_fill_mroute(skb, cache, rtm);
1630 read_unlock(&mrt_lock);
1634 #ifdef CONFIG_PROC_FS
1636 * The /proc interfaces to multicast routing /proc/ip_mr_cache /proc/ip_mr_vif
1638 struct ipmr_vif_iter {
1642 static struct vif_device *ipmr_vif_seq_idx(struct ipmr_vif_iter *iter,
1645 for (iter->ct = 0; iter->ct < maxvif; ++iter->ct) {
1646 if (!VIF_EXISTS(iter->ct))
1649 return &vif_table[iter->ct];
1654 static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
1655 __acquires(mrt_lock)
1657 read_lock(&mrt_lock);
1658 return *pos ? ipmr_vif_seq_idx(seq->private, *pos - 1)
1662 static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1664 struct ipmr_vif_iter *iter = seq->private;
1667 if (v == SEQ_START_TOKEN)
1668 return ipmr_vif_seq_idx(iter, 0);
1670 while (++iter->ct < maxvif) {
1671 if (!VIF_EXISTS(iter->ct))
1673 return &vif_table[iter->ct];
1678 static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
1679 __releases(mrt_lock)
1681 read_unlock(&mrt_lock);
1684 static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
1686 if (v == SEQ_START_TOKEN) {
1688 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
1690 const struct vif_device *vif = v;
1691 const char *name = vif->dev ? vif->dev->name : "none";
1694 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
1696 name, vif->bytes_in, vif->pkt_in,
1697 vif->bytes_out, vif->pkt_out,
1698 vif->flags, vif->local, vif->remote);
1703 static const struct seq_operations ipmr_vif_seq_ops = {
1704 .start = ipmr_vif_seq_start,
1705 .next = ipmr_vif_seq_next,
1706 .stop = ipmr_vif_seq_stop,
1707 .show = ipmr_vif_seq_show,
1710 static int ipmr_vif_open(struct inode *inode, struct file *file)
1712 return seq_open_private(file, &ipmr_vif_seq_ops,
1713 sizeof(struct ipmr_vif_iter));
1716 static const struct file_operations ipmr_vif_fops = {
1717 .owner = THIS_MODULE,
1718 .open = ipmr_vif_open,
1720 .llseek = seq_lseek,
1721 .release = seq_release_private,
1724 struct ipmr_mfc_iter {
1725 struct mfc_cache **cache;
1730 static struct mfc_cache *ipmr_mfc_seq_idx(struct ipmr_mfc_iter *it, loff_t pos)
1732 struct mfc_cache *mfc;
1734 it->cache = mfc_cache_array;
1735 read_lock(&mrt_lock);
1736 for (it->ct = 0; it->ct < MFC_LINES; it->ct++)
1737 for (mfc = mfc_cache_array[it->ct]; mfc; mfc = mfc->next)
1740 read_unlock(&mrt_lock);
1742 it->cache = &mfc_unres_queue;
1743 spin_lock_bh(&mfc_unres_lock);
1744 for (mfc = mfc_unres_queue; mfc; mfc = mfc->next)
1747 spin_unlock_bh(&mfc_unres_lock);
1754 static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
1756 struct ipmr_mfc_iter *it = seq->private;
1759 return *pos ? ipmr_mfc_seq_idx(seq->private, *pos - 1)
1763 static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1765 struct mfc_cache *mfc = v;
1766 struct ipmr_mfc_iter *it = seq->private;
1770 if (v == SEQ_START_TOKEN)
1771 return ipmr_mfc_seq_idx(seq->private, 0);
1776 if (it->cache == &mfc_unres_queue)
1779 BUG_ON(it->cache != mfc_cache_array);
1781 while (++it->ct < MFC_LINES) {
1782 mfc = mfc_cache_array[it->ct];
1787 /* exhausted cache_array, show unresolved */
1788 read_unlock(&mrt_lock);
1789 it->cache = &mfc_unres_queue;
1792 spin_lock_bh(&mfc_unres_lock);
1793 mfc = mfc_unres_queue;
1798 spin_unlock_bh(&mfc_unres_lock);
1804 static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
1806 struct ipmr_mfc_iter *it = seq->private;
1808 if (it->cache == &mfc_unres_queue)
1809 spin_unlock_bh(&mfc_unres_lock);
1810 else if (it->cache == mfc_cache_array)
1811 read_unlock(&mrt_lock);
1814 static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
1818 if (v == SEQ_START_TOKEN) {
1820 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
1822 const struct mfc_cache *mfc = v;
1823 const struct ipmr_mfc_iter *it = seq->private;
1825 seq_printf(seq, "%08lX %08lX %-3d %8ld %8ld %8ld",
1826 (unsigned long) mfc->mfc_mcastgrp,
1827 (unsigned long) mfc->mfc_origin,
1829 mfc->mfc_un.res.pkt,
1830 mfc->mfc_un.res.bytes,
1831 mfc->mfc_un.res.wrong_if);
1833 if (it->cache != &mfc_unres_queue) {
1834 for (n = mfc->mfc_un.res.minvif;
1835 n < mfc->mfc_un.res.maxvif; n++ ) {
1837 && mfc->mfc_un.res.ttls[n] < 255)
1840 n, mfc->mfc_un.res.ttls[n]);
1843 seq_putc(seq, '\n');
1848 static const struct seq_operations ipmr_mfc_seq_ops = {
1849 .start = ipmr_mfc_seq_start,
1850 .next = ipmr_mfc_seq_next,
1851 .stop = ipmr_mfc_seq_stop,
1852 .show = ipmr_mfc_seq_show,
1855 static int ipmr_mfc_open(struct inode *inode, struct file *file)
1857 return seq_open_private(file, &ipmr_mfc_seq_ops,
1858 sizeof(struct ipmr_mfc_iter));
1861 static const struct file_operations ipmr_mfc_fops = {
1862 .owner = THIS_MODULE,
1863 .open = ipmr_mfc_open,
1865 .llseek = seq_lseek,
1866 .release = seq_release_private,
1870 #ifdef CONFIG_IP_PIMSM_V2
1871 static struct net_protocol pim_protocol = {
1878 * Setup for IP multicast routing
1881 void __init ip_mr_init(void)
1883 mrt_cachep = kmem_cache_create("ip_mrt_cache",
1884 sizeof(struct mfc_cache),
1885 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
1887 setup_timer(&ipmr_expire_timer, ipmr_expire_process, 0);
1888 register_netdevice_notifier(&ip_mr_notifier);
1889 #ifdef CONFIG_PROC_FS
1890 proc_net_fops_create(&init_net, "ip_mr_vif", 0, &ipmr_vif_fops);
1891 proc_net_fops_create(&init_net, "ip_mr_cache", 0, &ipmr_mfc_fops);