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.
12 * Version: $Id: ipmr.c,v 1.65 2001/10/31 21:55:54 davem Exp $
15 * Michael Chastain : Incorrect size of copying.
16 * Alan Cox : Added the cache manager code
17 * Alan Cox : Fixed the clone/copy bug and device race.
18 * Mike McLagan : Routing by source
19 * Malcolm Beattie : Buffer handling fixes.
20 * Alexey Kuznetsov : Double buffer free and other fixes.
21 * SVR Anand : Fixed several multicast bugs and problems.
22 * Alexey Kuznetsov : Status, optimisations and more.
23 * Brad Parker : Better behaviour on mrouted upcall
25 * Carlos Picoto : PIMv1 Support
26 * Pavlin Ivanov Radoslavov: PIMv2 Registers must checksum only PIM header
27 * Relax this requrement to work with older peers.
31 #include <asm/system.h>
32 #include <asm/uaccess.h>
33 #include <linux/types.h>
34 #include <linux/capability.h>
35 #include <linux/errno.h>
36 #include <linux/timer.h>
38 #include <linux/kernel.h>
39 #include <linux/fcntl.h>
40 #include <linux/stat.h>
41 #include <linux/socket.h>
43 #include <linux/inet.h>
44 #include <linux/netdevice.h>
45 #include <linux/inetdevice.h>
46 #include <linux/igmp.h>
47 #include <linux/proc_fs.h>
48 #include <linux/seq_file.h>
49 #include <linux/mroute.h>
50 #include <linux/init.h>
51 #include <linux/if_ether.h>
53 #include <net/protocol.h>
54 #include <linux/skbuff.h>
55 #include <net/route.h>
60 #include <linux/notifier.h>
61 #include <linux/if_arp.h>
62 #include <linux/netfilter_ipv4.h>
64 #include <net/checksum.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("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
= (void*)&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(p
.name
)) != NULL
) {
151 dev
->flags
|= IFF_MULTICAST
;
153 in_dev
= __in_dev_get_rtnl(dev
);
154 if (in_dev
== NULL
&& (in_dev
= inetdev_init(dev
)) == NULL
)
156 in_dev
->cnf
.rp_filter
= 0;
165 /* allow the register to be completed before unregistering. */
169 unregister_netdevice(dev
);
173 #ifdef CONFIG_IP_PIMSM
175 static int reg_vif_num
= -1;
177 static int reg_vif_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
179 read_lock(&mrt_lock
);
180 ((struct net_device_stats
*)netdev_priv(dev
))->tx_bytes
+= skb
->len
;
181 ((struct net_device_stats
*)netdev_priv(dev
))->tx_packets
++;
182 ipmr_cache_report(skb
, reg_vif_num
, IGMPMSG_WHOLEPKT
);
183 read_unlock(&mrt_lock
);
188 static struct net_device_stats
*reg_vif_get_stats(struct net_device
*dev
)
190 return (struct net_device_stats
*)netdev_priv(dev
);
193 static void reg_vif_setup(struct net_device
*dev
)
195 dev
->type
= ARPHRD_PIMREG
;
196 dev
->mtu
= ETH_DATA_LEN
- sizeof(struct iphdr
) - 8;
197 dev
->flags
= IFF_NOARP
;
198 dev
->hard_start_xmit
= reg_vif_xmit
;
199 dev
->get_stats
= reg_vif_get_stats
;
200 dev
->destructor
= free_netdev
;
203 static struct net_device
*ipmr_reg_vif(void)
205 struct net_device
*dev
;
206 struct in_device
*in_dev
;
208 dev
= alloc_netdev(sizeof(struct net_device_stats
), "pimreg",
214 if (register_netdevice(dev
)) {
220 if ((in_dev
= inetdev_init(dev
)) == NULL
)
223 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 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 (skb
->nh
.iph
->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
, 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(vifc
->vifc_lcl_addr
.s_addr
);
419 return -EADDRNOTAVAIL
;
426 if ((in_dev
= __in_dev_get_rtnl(dev
)) == NULL
)
427 return -EADDRNOTAVAIL
;
428 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 (skb
->nh
.iph
->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
- (u8
*)nlh
;
518 nlh
->nlmsg_type
= NLMSG_ERROR
;
519 nlh
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct nlmsgerr
));
520 skb_trim(skb
, nlh
->nlmsg_len
);
522 e
->error
= -EMSGSIZE
;
523 memset(&e
->msg
, 0, sizeof(e
->msg
));
526 rtnl_unicast(skb
, NETLINK_CB(skb
).pid
);
528 ip_mr_forward(skb
, c
, 0);
533 * Bounce a cache query up to mrouted. We could use netlink for this but mrouted
534 * expects the following bizarre scheme.
536 * Called under mrt_lock.
539 static int ipmr_cache_report(struct sk_buff
*pkt
, vifi_t vifi
, int assert)
542 int ihl
= pkt
->nh
.iph
->ihl
<<2;
543 struct igmphdr
*igmp
;
547 #ifdef CONFIG_IP_PIMSM
548 if (assert == IGMPMSG_WHOLEPKT
)
549 skb
= skb_realloc_headroom(pkt
, sizeof(struct iphdr
));
552 skb
= alloc_skb(128, GFP_ATOMIC
);
557 #ifdef CONFIG_IP_PIMSM
558 if (assert == IGMPMSG_WHOLEPKT
) {
559 /* Ugly, but we have no choice with this interface.
560 Duplicate old header, fix ihl, length etc.
561 And all this only to mangle msg->im_msgtype and
562 to set msg->im_mbz to "mbz" :-)
564 msg
= (struct igmpmsg
*)skb_push(skb
, sizeof(struct iphdr
));
565 skb
->nh
.raw
= skb
->h
.raw
= (u8
*)msg
;
566 memcpy(msg
, skb_network_header(pkt
), sizeof(struct iphdr
));
567 msg
->im_msgtype
= IGMPMSG_WHOLEPKT
;
569 msg
->im_vif
= reg_vif_num
;
570 skb
->nh
.iph
->ihl
= sizeof(struct iphdr
) >> 2;
571 skb
->nh
.iph
->tot_len
= htons(ntohs(pkt
->nh
.iph
->tot_len
) + sizeof(struct iphdr
));
580 skb
->nh
.iph
= (struct iphdr
*)skb_put(skb
, ihl
);
581 memcpy(skb
->data
,pkt
->data
,ihl
);
582 skb
->nh
.iph
->protocol
= 0; /* Flag to the kernel this is a route add */
583 msg
= (struct igmpmsg
*)skb
->nh
.iph
;
585 skb
->dst
= dst_clone(pkt
->dst
);
591 igmp
=(struct igmphdr
*)skb_put(skb
,sizeof(struct igmphdr
));
593 msg
->im_msgtype
= assert;
595 skb
->nh
.iph
->tot_len
=htons(skb
->len
); /* Fix the length */
596 skb
->h
.raw
= skb
->nh
.raw
;
599 if (mroute_socket
== NULL
) {
607 if ((ret
=sock_queue_rcv_skb(mroute_socket
,skb
))<0) {
609 printk(KERN_WARNING
"mroute: pending queue full, dropping entries.\n");
617 * Queue a packet for resolution. It gets locked cache entry!
621 ipmr_cache_unresolved(vifi_t vifi
, struct sk_buff
*skb
)
626 spin_lock_bh(&mfc_unres_lock
);
627 for (c
=mfc_unres_queue
; c
; c
=c
->next
) {
628 if (c
->mfc_mcastgrp
== skb
->nh
.iph
->daddr
&&
629 c
->mfc_origin
== skb
->nh
.iph
->saddr
)
635 * Create a new entry if allowable
638 if (atomic_read(&cache_resolve_queue_len
)>=10 ||
639 (c
=ipmr_cache_alloc_unres())==NULL
) {
640 spin_unlock_bh(&mfc_unres_lock
);
647 * Fill in the new cache entry
650 c
->mfc_origin
=skb
->nh
.iph
->saddr
;
651 c
->mfc_mcastgrp
=skb
->nh
.iph
->daddr
;
654 * Reflect first query at mrouted.
656 if ((err
= ipmr_cache_report(skb
, vifi
, IGMPMSG_NOCACHE
))<0) {
657 /* If the report failed throw the cache entry
660 spin_unlock_bh(&mfc_unres_lock
);
662 kmem_cache_free(mrt_cachep
, c
);
667 atomic_inc(&cache_resolve_queue_len
);
668 c
->next
= mfc_unres_queue
;
671 mod_timer(&ipmr_expire_timer
, c
->mfc_un
.unres
.expires
);
675 * See if we can append the packet
677 if (c
->mfc_un
.unres
.unresolved
.qlen
>3) {
681 skb_queue_tail(&c
->mfc_un
.unres
.unresolved
,skb
);
685 spin_unlock_bh(&mfc_unres_lock
);
690 * MFC cache manipulation by user space mroute daemon
693 static int ipmr_mfc_delete(struct mfcctl
*mfc
)
696 struct mfc_cache
*c
, **cp
;
698 line
=MFC_HASH(mfc
->mfcc_mcastgrp
.s_addr
, mfc
->mfcc_origin
.s_addr
);
700 for (cp
=&mfc_cache_array
[line
]; (c
=*cp
) != NULL
; cp
= &c
->next
) {
701 if (c
->mfc_origin
== mfc
->mfcc_origin
.s_addr
&&
702 c
->mfc_mcastgrp
== mfc
->mfcc_mcastgrp
.s_addr
) {
703 write_lock_bh(&mrt_lock
);
705 write_unlock_bh(&mrt_lock
);
707 kmem_cache_free(mrt_cachep
, c
);
714 static int ipmr_mfc_add(struct mfcctl
*mfc
, int mrtsock
)
717 struct mfc_cache
*uc
, *c
, **cp
;
719 line
=MFC_HASH(mfc
->mfcc_mcastgrp
.s_addr
, mfc
->mfcc_origin
.s_addr
);
721 for (cp
=&mfc_cache_array
[line
]; (c
=*cp
) != NULL
; cp
= &c
->next
) {
722 if (c
->mfc_origin
== mfc
->mfcc_origin
.s_addr
&&
723 c
->mfc_mcastgrp
== mfc
->mfcc_mcastgrp
.s_addr
)
728 write_lock_bh(&mrt_lock
);
729 c
->mfc_parent
= mfc
->mfcc_parent
;
730 ipmr_update_thresholds(c
, mfc
->mfcc_ttls
);
732 c
->mfc_flags
|= MFC_STATIC
;
733 write_unlock_bh(&mrt_lock
);
737 if (!MULTICAST(mfc
->mfcc_mcastgrp
.s_addr
))
740 c
=ipmr_cache_alloc();
744 c
->mfc_origin
=mfc
->mfcc_origin
.s_addr
;
745 c
->mfc_mcastgrp
=mfc
->mfcc_mcastgrp
.s_addr
;
746 c
->mfc_parent
=mfc
->mfcc_parent
;
747 ipmr_update_thresholds(c
, mfc
->mfcc_ttls
);
749 c
->mfc_flags
|= MFC_STATIC
;
751 write_lock_bh(&mrt_lock
);
752 c
->next
= mfc_cache_array
[line
];
753 mfc_cache_array
[line
] = c
;
754 write_unlock_bh(&mrt_lock
);
757 * Check to see if we resolved a queued list. If so we
758 * need to send on the frames and tidy up.
760 spin_lock_bh(&mfc_unres_lock
);
761 for (cp
= &mfc_unres_queue
; (uc
=*cp
) != NULL
;
763 if (uc
->mfc_origin
== c
->mfc_origin
&&
764 uc
->mfc_mcastgrp
== c
->mfc_mcastgrp
) {
766 if (atomic_dec_and_test(&cache_resolve_queue_len
))
767 del_timer(&ipmr_expire_timer
);
771 spin_unlock_bh(&mfc_unres_lock
);
774 ipmr_cache_resolve(uc
, c
);
775 kmem_cache_free(mrt_cachep
, uc
);
781 * Close the multicast socket, and clear the vif tables etc
784 static void mroute_clean_tables(struct sock
*sk
)
789 * Shut down all active vif entries
791 for (i
=0; i
<maxvif
; i
++) {
792 if (!(vif_table
[i
].flags
&VIFF_STATIC
))
799 for (i
=0;i
<MFC_LINES
;i
++) {
800 struct mfc_cache
*c
, **cp
;
802 cp
= &mfc_cache_array
[i
];
803 while ((c
= *cp
) != NULL
) {
804 if (c
->mfc_flags
&MFC_STATIC
) {
808 write_lock_bh(&mrt_lock
);
810 write_unlock_bh(&mrt_lock
);
812 kmem_cache_free(mrt_cachep
, c
);
816 if (atomic_read(&cache_resolve_queue_len
) != 0) {
819 spin_lock_bh(&mfc_unres_lock
);
820 while (mfc_unres_queue
!= NULL
) {
822 mfc_unres_queue
= c
->next
;
823 spin_unlock_bh(&mfc_unres_lock
);
825 ipmr_destroy_unres(c
);
827 spin_lock_bh(&mfc_unres_lock
);
829 spin_unlock_bh(&mfc_unres_lock
);
833 static void mrtsock_destruct(struct sock
*sk
)
836 if (sk
== mroute_socket
) {
837 ipv4_devconf
.mc_forwarding
--;
839 write_lock_bh(&mrt_lock
);
841 write_unlock_bh(&mrt_lock
);
843 mroute_clean_tables(sk
);
849 * Socket options and virtual interface manipulation. The whole
850 * virtual interface system is a complete heap, but unfortunately
851 * that's how BSD mrouted happens to think. Maybe one day with a proper
852 * MOSPF/PIM router set up we can clean this up.
855 int ip_mroute_setsockopt(struct sock
*sk
,int optname
,char __user
*optval
,int optlen
)
861 if (optname
!= MRT_INIT
) {
862 if (sk
!= mroute_socket
&& !capable(CAP_NET_ADMIN
))
868 if (sk
->sk_type
!= SOCK_RAW
||
869 inet_sk(sk
)->num
!= IPPROTO_IGMP
)
871 if (optlen
!=sizeof(int))
880 ret
= ip_ra_control(sk
, 1, mrtsock_destruct
);
882 write_lock_bh(&mrt_lock
);
884 write_unlock_bh(&mrt_lock
);
886 ipv4_devconf
.mc_forwarding
++;
891 if (sk
!=mroute_socket
)
893 return ip_ra_control(sk
, 0, NULL
);
896 if (optlen
!=sizeof(vif
))
898 if (copy_from_user(&vif
,optval
,sizeof(vif
)))
900 if (vif
.vifc_vifi
>= MAXVIFS
)
903 if (optname
==MRT_ADD_VIF
) {
904 ret
= vif_add(&vif
, sk
==mroute_socket
);
906 ret
= vif_delete(vif
.vifc_vifi
);
912 * Manipulate the forwarding caches. These live
913 * in a sort of kernel/user symbiosis.
917 if (optlen
!=sizeof(mfc
))
919 if (copy_from_user(&mfc
,optval
, sizeof(mfc
)))
922 if (optname
==MRT_DEL_MFC
)
923 ret
= ipmr_mfc_delete(&mfc
);
925 ret
= ipmr_mfc_add(&mfc
, sk
==mroute_socket
);
929 * Control PIM assert.
934 if (get_user(v
,(int __user
*)optval
))
936 mroute_do_assert
=(v
)?1:0;
939 #ifdef CONFIG_IP_PIMSM
943 if (get_user(v
,(int __user
*)optval
))
948 if (v
!= mroute_do_pim
) {
950 mroute_do_assert
= v
;
951 #ifdef CONFIG_IP_PIMSM_V2
953 ret
= inet_add_protocol(&pim_protocol
,
956 ret
= inet_del_protocol(&pim_protocol
,
967 * Spurious command, or MRT_VERSION which you cannot
976 * Getsock opt support for the multicast routing system.
979 int ip_mroute_getsockopt(struct sock
*sk
,int optname
,char __user
*optval
,int __user
*optlen
)
984 if (optname
!=MRT_VERSION
&&
985 #ifdef CONFIG_IP_PIMSM
991 if (get_user(olr
, optlen
))
994 olr
= min_t(unsigned int, olr
, sizeof(int));
998 if (put_user(olr
,optlen
))
1000 if (optname
==MRT_VERSION
)
1002 #ifdef CONFIG_IP_PIMSM
1003 else if (optname
==MRT_PIM
)
1007 val
=mroute_do_assert
;
1008 if (copy_to_user(optval
,&val
,olr
))
1014 * The IP multicast ioctl support routines.
1017 int ipmr_ioctl(struct sock
*sk
, int cmd
, void __user
*arg
)
1019 struct sioc_sg_req sr
;
1020 struct sioc_vif_req vr
;
1021 struct vif_device
*vif
;
1022 struct mfc_cache
*c
;
1026 if (copy_from_user(&vr
,arg
,sizeof(vr
)))
1028 if (vr
.vifi
>=maxvif
)
1030 read_lock(&mrt_lock
);
1031 vif
=&vif_table
[vr
.vifi
];
1032 if (VIF_EXISTS(vr
.vifi
)) {
1033 vr
.icount
=vif
->pkt_in
;
1034 vr
.ocount
=vif
->pkt_out
;
1035 vr
.ibytes
=vif
->bytes_in
;
1036 vr
.obytes
=vif
->bytes_out
;
1037 read_unlock(&mrt_lock
);
1039 if (copy_to_user(arg
,&vr
,sizeof(vr
)))
1043 read_unlock(&mrt_lock
);
1044 return -EADDRNOTAVAIL
;
1046 if (copy_from_user(&sr
,arg
,sizeof(sr
)))
1049 read_lock(&mrt_lock
);
1050 c
= ipmr_cache_find(sr
.src
.s_addr
, sr
.grp
.s_addr
);
1052 sr
.pktcnt
= c
->mfc_un
.res
.pkt
;
1053 sr
.bytecnt
= c
->mfc_un
.res
.bytes
;
1054 sr
.wrong_if
= c
->mfc_un
.res
.wrong_if
;
1055 read_unlock(&mrt_lock
);
1057 if (copy_to_user(arg
,&sr
,sizeof(sr
)))
1061 read_unlock(&mrt_lock
);
1062 return -EADDRNOTAVAIL
;
1064 return -ENOIOCTLCMD
;
1069 static int ipmr_device_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
1071 struct vif_device
*v
;
1073 if (event
!= NETDEV_UNREGISTER
)
1076 for (ct
=0;ct
<maxvif
;ct
++,v
++) {
1084 static struct notifier_block ip_mr_notifier
={
1085 .notifier_call
= ipmr_device_event
,
1089 * Encapsulate a packet by attaching a valid IPIP header to it.
1090 * This avoids tunnel drivers and other mess and gives us the speed so
1091 * important for multicast video.
1094 static void ip_encap(struct sk_buff
*skb
, __be32 saddr
, __be32 daddr
)
1098 skb_push(skb
, sizeof(struct iphdr
));
1099 skb
->h
.ipiph
= skb
->nh
.iph
;
1100 skb_reset_network_header(skb
);
1104 iph
->tos
= skb
->nh
.iph
->tos
;
1105 iph
->ttl
= skb
->nh
.iph
->ttl
;
1109 iph
->protocol
= IPPROTO_IPIP
;
1111 iph
->tot_len
= htons(skb
->len
);
1112 ip_select_ident(iph
, skb
->dst
, NULL
);
1115 memset(&(IPCB(skb
)->opt
), 0, sizeof(IPCB(skb
)->opt
));
1119 static inline int ipmr_forward_finish(struct sk_buff
*skb
)
1121 struct ip_options
* opt
= &(IPCB(skb
)->opt
);
1123 IP_INC_STATS_BH(IPSTATS_MIB_OUTFORWDATAGRAMS
);
1125 if (unlikely(opt
->optlen
))
1126 ip_forward_options(skb
);
1128 return dst_output(skb
);
1132 * Processing handlers for ipmr_forward
1135 static void ipmr_queue_xmit(struct sk_buff
*skb
, struct mfc_cache
*c
, int vifi
)
1137 struct iphdr
*iph
= skb
->nh
.iph
;
1138 struct vif_device
*vif
= &vif_table
[vifi
];
1139 struct net_device
*dev
;
1143 if (vif
->dev
== NULL
)
1146 #ifdef CONFIG_IP_PIMSM
1147 if (vif
->flags
& VIFF_REGISTER
) {
1149 vif
->bytes_out
+=skb
->len
;
1150 ((struct net_device_stats
*)netdev_priv(vif
->dev
))->tx_bytes
+= skb
->len
;
1151 ((struct net_device_stats
*)netdev_priv(vif
->dev
))->tx_packets
++;
1152 ipmr_cache_report(skb
, vifi
, IGMPMSG_WHOLEPKT
);
1158 if (vif
->flags
&VIFF_TUNNEL
) {
1159 struct flowi fl
= { .oif
= vif
->link
,
1161 { .daddr
= vif
->remote
,
1162 .saddr
= vif
->local
,
1163 .tos
= RT_TOS(iph
->tos
) } },
1164 .proto
= IPPROTO_IPIP
};
1165 if (ip_route_output_key(&rt
, &fl
))
1167 encap
= sizeof(struct iphdr
);
1169 struct flowi fl
= { .oif
= vif
->link
,
1171 { .daddr
= iph
->daddr
,
1172 .tos
= RT_TOS(iph
->tos
) } },
1173 .proto
= IPPROTO_IPIP
};
1174 if (ip_route_output_key(&rt
, &fl
))
1178 dev
= rt
->u
.dst
.dev
;
1180 if (skb
->len
+encap
> dst_mtu(&rt
->u
.dst
) && (ntohs(iph
->frag_off
) & IP_DF
)) {
1181 /* Do not fragment multicasts. Alas, IPv4 does not
1182 allow to send ICMP, so that packets will disappear
1186 IP_INC_STATS_BH(IPSTATS_MIB_FRAGFAILS
);
1191 encap
+= LL_RESERVED_SPACE(dev
) + rt
->u
.dst
.header_len
;
1193 if (skb_cow(skb
, encap
)) {
1199 vif
->bytes_out
+=skb
->len
;
1201 dst_release(skb
->dst
);
1202 skb
->dst
= &rt
->u
.dst
;
1204 ip_decrease_ttl(iph
);
1206 /* FIXME: forward and output firewalls used to be called here.
1207 * What do we do with netfilter? -- RR */
1208 if (vif
->flags
& VIFF_TUNNEL
) {
1209 ip_encap(skb
, vif
->local
, vif
->remote
);
1210 /* FIXME: extra output firewall step used to be here. --RR */
1211 ((struct ip_tunnel
*)netdev_priv(vif
->dev
))->stat
.tx_packets
++;
1212 ((struct ip_tunnel
*)netdev_priv(vif
->dev
))->stat
.tx_bytes
+=skb
->len
;
1215 IPCB(skb
)->flags
|= IPSKB_FORWARDED
;
1218 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1219 * not only before forwarding, but after forwarding on all output
1220 * interfaces. It is clear, if mrouter runs a multicasting
1221 * program, it should receive packets not depending to what interface
1222 * program is joined.
1223 * If we will not make it, the program will have to join on all
1224 * interfaces. On the other hand, multihoming host (or router, but
1225 * not mrouter) cannot join to more than one interface - it will
1226 * result in receiving multiple packets.
1228 NF_HOOK(PF_INET
, NF_IP_FORWARD
, skb
, skb
->dev
, dev
,
1229 ipmr_forward_finish
);
1237 static int ipmr_find_vif(struct net_device
*dev
)
1240 for (ct
=maxvif
-1; ct
>=0; ct
--) {
1241 if (vif_table
[ct
].dev
== dev
)
1247 /* "local" means that we should preserve one skb (for local delivery) */
1249 static int ip_mr_forward(struct sk_buff
*skb
, struct mfc_cache
*cache
, int local
)
1254 vif
= cache
->mfc_parent
;
1255 cache
->mfc_un
.res
.pkt
++;
1256 cache
->mfc_un
.res
.bytes
+= skb
->len
;
1259 * Wrong interface: drop packet and (maybe) send PIM assert.
1261 if (vif_table
[vif
].dev
!= skb
->dev
) {
1264 if (((struct rtable
*)skb
->dst
)->fl
.iif
== 0) {
1265 /* It is our own packet, looped back.
1266 Very complicated situation...
1268 The best workaround until routing daemons will be
1269 fixed is not to redistribute packet, if it was
1270 send through wrong interface. It means, that
1271 multicast applications WILL NOT work for
1272 (S,G), which have default multicast route pointing
1273 to wrong oif. In any case, it is not a good
1274 idea to use multicasting applications on router.
1279 cache
->mfc_un
.res
.wrong_if
++;
1280 true_vifi
= ipmr_find_vif(skb
->dev
);
1282 if (true_vifi
>= 0 && mroute_do_assert
&&
1283 /* pimsm uses asserts, when switching from RPT to SPT,
1284 so that we cannot check that packet arrived on an oif.
1285 It is bad, but otherwise we would need to move pretty
1286 large chunk of pimd to kernel. Ough... --ANK
1288 (mroute_do_pim
|| cache
->mfc_un
.res
.ttls
[true_vifi
] < 255) &&
1290 cache
->mfc_un
.res
.last_assert
+ MFC_ASSERT_THRESH
)) {
1291 cache
->mfc_un
.res
.last_assert
= jiffies
;
1292 ipmr_cache_report(skb
, true_vifi
, IGMPMSG_WRONGVIF
);
1297 vif_table
[vif
].pkt_in
++;
1298 vif_table
[vif
].bytes_in
+=skb
->len
;
1303 for (ct
= cache
->mfc_un
.res
.maxvif
-1; ct
>= cache
->mfc_un
.res
.minvif
; ct
--) {
1304 if (skb
->nh
.iph
->ttl
> cache
->mfc_un
.res
.ttls
[ct
]) {
1306 struct sk_buff
*skb2
= skb_clone(skb
, GFP_ATOMIC
);
1308 ipmr_queue_xmit(skb2
, cache
, psend
);
1315 struct sk_buff
*skb2
= skb_clone(skb
, GFP_ATOMIC
);
1317 ipmr_queue_xmit(skb2
, cache
, psend
);
1319 ipmr_queue_xmit(skb
, cache
, psend
);
1332 * Multicast packets for forwarding arrive here
1335 int ip_mr_input(struct sk_buff
*skb
)
1337 struct mfc_cache
*cache
;
1338 int local
= ((struct rtable
*)skb
->dst
)->rt_flags
&RTCF_LOCAL
;
1340 /* Packet is looped back after forward, it should not be
1341 forwarded second time, but still can be delivered locally.
1343 if (IPCB(skb
)->flags
&IPSKB_FORWARDED
)
1347 if (IPCB(skb
)->opt
.router_alert
) {
1348 if (ip_call_ra_chain(skb
))
1350 } else if (skb
->nh
.iph
->protocol
== IPPROTO_IGMP
){
1351 /* IGMPv1 (and broken IGMPv2 implementations sort of
1352 Cisco IOS <= 11.2(8)) do not put router alert
1353 option to IGMP packets destined to routable
1354 groups. It is very bad, because it means
1355 that we can forward NO IGMP messages.
1357 read_lock(&mrt_lock
);
1358 if (mroute_socket
) {
1360 raw_rcv(mroute_socket
, skb
);
1361 read_unlock(&mrt_lock
);
1364 read_unlock(&mrt_lock
);
1368 read_lock(&mrt_lock
);
1369 cache
= ipmr_cache_find(skb
->nh
.iph
->saddr
, skb
->nh
.iph
->daddr
);
1372 * No usable cache entry
1378 struct sk_buff
*skb2
= skb_clone(skb
, GFP_ATOMIC
);
1379 ip_local_deliver(skb
);
1381 read_unlock(&mrt_lock
);
1387 vif
= ipmr_find_vif(skb
->dev
);
1389 int err
= ipmr_cache_unresolved(vif
, skb
);
1390 read_unlock(&mrt_lock
);
1394 read_unlock(&mrt_lock
);
1399 ip_mr_forward(skb
, cache
, local
);
1401 read_unlock(&mrt_lock
);
1404 return ip_local_deliver(skb
);
1410 return ip_local_deliver(skb
);
1415 #ifdef CONFIG_IP_PIMSM_V1
1417 * Handle IGMP messages of PIMv1
1420 int pim_rcv_v1(struct sk_buff
* skb
)
1422 struct igmphdr
*pim
;
1423 struct iphdr
*encap
;
1424 struct net_device
*reg_dev
= NULL
;
1426 if (!pskb_may_pull(skb
, sizeof(*pim
) + sizeof(*encap
)))
1429 pim
= (struct igmphdr
*)skb
->h
.raw
;
1431 if (!mroute_do_pim
||
1432 skb
->len
< sizeof(*pim
) + sizeof(*encap
) ||
1433 pim
->group
!= PIM_V1_VERSION
|| pim
->code
!= PIM_V1_REGISTER
)
1436 encap
= (struct iphdr
*)(skb
->h
.raw
+ sizeof(struct igmphdr
));
1439 a. packet is really destinted to a multicast group
1440 b. packet is not a NULL-REGISTER
1441 c. packet is not truncated
1443 if (!MULTICAST(encap
->daddr
) ||
1444 encap
->tot_len
== 0 ||
1445 ntohs(encap
->tot_len
) + sizeof(*pim
) > skb
->len
)
1448 read_lock(&mrt_lock
);
1449 if (reg_vif_num
>= 0)
1450 reg_dev
= vif_table
[reg_vif_num
].dev
;
1453 read_unlock(&mrt_lock
);
1455 if (reg_dev
== NULL
)
1458 skb
->mac
.raw
= skb
->nh
.raw
;
1459 skb_pull(skb
, (u8
*)encap
- skb
->data
);
1460 skb_reset_network_header(skb
);
1462 skb
->protocol
= htons(ETH_P_IP
);
1464 skb
->pkt_type
= PACKET_HOST
;
1465 dst_release(skb
->dst
);
1467 ((struct net_device_stats
*)netdev_priv(reg_dev
))->rx_bytes
+= skb
->len
;
1468 ((struct net_device_stats
*)netdev_priv(reg_dev
))->rx_packets
++;
1479 #ifdef CONFIG_IP_PIMSM_V2
1480 static int pim_rcv(struct sk_buff
* skb
)
1482 struct pimreghdr
*pim
;
1483 struct iphdr
*encap
;
1484 struct net_device
*reg_dev
= NULL
;
1486 if (!pskb_may_pull(skb
, sizeof(*pim
) + sizeof(*encap
)))
1489 pim
= (struct pimreghdr
*)skb
->h
.raw
;
1490 if (pim
->type
!= ((PIM_VERSION
<<4)|(PIM_REGISTER
)) ||
1491 (pim
->flags
&PIM_NULL_REGISTER
) ||
1492 (ip_compute_csum((void *)pim
, sizeof(*pim
)) != 0 &&
1493 csum_fold(skb_checksum(skb
, 0, skb
->len
, 0))))
1496 /* check if the inner packet is destined to mcast group */
1497 encap
= (struct iphdr
*)(skb
->h
.raw
+ sizeof(struct pimreghdr
));
1498 if (!MULTICAST(encap
->daddr
) ||
1499 encap
->tot_len
== 0 ||
1500 ntohs(encap
->tot_len
) + sizeof(*pim
) > skb
->len
)
1503 read_lock(&mrt_lock
);
1504 if (reg_vif_num
>= 0)
1505 reg_dev
= vif_table
[reg_vif_num
].dev
;
1508 read_unlock(&mrt_lock
);
1510 if (reg_dev
== NULL
)
1513 skb
->mac
.raw
= skb
->nh
.raw
;
1514 skb_pull(skb
, (u8
*)encap
- skb
->data
);
1515 skb_reset_network_header(skb
);
1517 skb
->protocol
= htons(ETH_P_IP
);
1519 skb
->pkt_type
= PACKET_HOST
;
1520 dst_release(skb
->dst
);
1521 ((struct net_device_stats
*)netdev_priv(reg_dev
))->rx_bytes
+= skb
->len
;
1522 ((struct net_device_stats
*)netdev_priv(reg_dev
))->rx_packets
++;
1535 ipmr_fill_mroute(struct sk_buff
*skb
, struct mfc_cache
*c
, struct rtmsg
*rtm
)
1538 struct rtnexthop
*nhp
;
1539 struct net_device
*dev
= vif_table
[c
->mfc_parent
].dev
;
1541 struct rtattr
*mp_head
;
1544 RTA_PUT(skb
, RTA_IIF
, 4, &dev
->ifindex
);
1546 mp_head
= (struct rtattr
*)skb_put(skb
, RTA_LENGTH(0));
1548 for (ct
= c
->mfc_un
.res
.minvif
; ct
< c
->mfc_un
.res
.maxvif
; ct
++) {
1549 if (c
->mfc_un
.res
.ttls
[ct
] < 255) {
1550 if (skb_tailroom(skb
) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp
)) + 4))
1551 goto rtattr_failure
;
1552 nhp
= (struct rtnexthop
*)skb_put(skb
, RTA_ALIGN(sizeof(*nhp
)));
1553 nhp
->rtnh_flags
= 0;
1554 nhp
->rtnh_hops
= c
->mfc_un
.res
.ttls
[ct
];
1555 nhp
->rtnh_ifindex
= vif_table
[ct
].dev
->ifindex
;
1556 nhp
->rtnh_len
= sizeof(*nhp
);
1559 mp_head
->rta_type
= RTA_MULTIPATH
;
1560 mp_head
->rta_len
= skb
->tail
- (u8
*)mp_head
;
1561 rtm
->rtm_type
= RTN_MULTICAST
;
1565 skb_trim(skb
, b
- skb
->data
);
1569 int ipmr_get_route(struct sk_buff
*skb
, struct rtmsg
*rtm
, int nowait
)
1572 struct mfc_cache
*cache
;
1573 struct rtable
*rt
= (struct rtable
*)skb
->dst
;
1575 read_lock(&mrt_lock
);
1576 cache
= ipmr_cache_find(rt
->rt_src
, rt
->rt_dst
);
1579 struct sk_buff
*skb2
;
1580 struct net_device
*dev
;
1584 read_unlock(&mrt_lock
);
1589 if (dev
== NULL
|| (vif
= ipmr_find_vif(dev
)) < 0) {
1590 read_unlock(&mrt_lock
);
1593 skb2
= skb_clone(skb
, GFP_ATOMIC
);
1595 read_unlock(&mrt_lock
);
1599 skb_push(skb2
, sizeof(struct iphdr
));
1600 skb_reset_network_header(skb2
);
1601 skb2
->nh
.iph
->ihl
= sizeof(struct iphdr
)>>2;
1602 skb2
->nh
.iph
->saddr
= rt
->rt_src
;
1603 skb2
->nh
.iph
->daddr
= rt
->rt_dst
;
1604 skb2
->nh
.iph
->version
= 0;
1605 err
= ipmr_cache_unresolved(vif
, skb2
);
1606 read_unlock(&mrt_lock
);
1610 if (!nowait
&& (rtm
->rtm_flags
&RTM_F_NOTIFY
))
1611 cache
->mfc_flags
|= MFC_NOTIFY
;
1612 err
= ipmr_fill_mroute(skb
, cache
, rtm
);
1613 read_unlock(&mrt_lock
);
1617 #ifdef CONFIG_PROC_FS
1619 * The /proc interfaces to multicast routing /proc/ip_mr_cache /proc/ip_mr_vif
1621 struct ipmr_vif_iter
{
1625 static struct vif_device
*ipmr_vif_seq_idx(struct ipmr_vif_iter
*iter
,
1628 for (iter
->ct
= 0; iter
->ct
< maxvif
; ++iter
->ct
) {
1629 if (!VIF_EXISTS(iter
->ct
))
1632 return &vif_table
[iter
->ct
];
1637 static void *ipmr_vif_seq_start(struct seq_file
*seq
, loff_t
*pos
)
1639 read_lock(&mrt_lock
);
1640 return *pos
? ipmr_vif_seq_idx(seq
->private, *pos
- 1)
1644 static void *ipmr_vif_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
1646 struct ipmr_vif_iter
*iter
= seq
->private;
1649 if (v
== SEQ_START_TOKEN
)
1650 return ipmr_vif_seq_idx(iter
, 0);
1652 while (++iter
->ct
< maxvif
) {
1653 if (!VIF_EXISTS(iter
->ct
))
1655 return &vif_table
[iter
->ct
];
1660 static void ipmr_vif_seq_stop(struct seq_file
*seq
, void *v
)
1662 read_unlock(&mrt_lock
);
1665 static int ipmr_vif_seq_show(struct seq_file
*seq
, void *v
)
1667 if (v
== SEQ_START_TOKEN
) {
1669 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
1671 const struct vif_device
*vif
= v
;
1672 const char *name
= vif
->dev
? vif
->dev
->name
: "none";
1675 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
1677 name
, vif
->bytes_in
, vif
->pkt_in
,
1678 vif
->bytes_out
, vif
->pkt_out
,
1679 vif
->flags
, vif
->local
, vif
->remote
);
1684 static struct seq_operations ipmr_vif_seq_ops
= {
1685 .start
= ipmr_vif_seq_start
,
1686 .next
= ipmr_vif_seq_next
,
1687 .stop
= ipmr_vif_seq_stop
,
1688 .show
= ipmr_vif_seq_show
,
1691 static int ipmr_vif_open(struct inode
*inode
, struct file
*file
)
1693 struct seq_file
*seq
;
1695 struct ipmr_vif_iter
*s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
1700 rc
= seq_open(file
, &ipmr_vif_seq_ops
);
1705 seq
= file
->private_data
;
1715 static const struct file_operations ipmr_vif_fops
= {
1716 .owner
= THIS_MODULE
,
1717 .open
= ipmr_vif_open
,
1719 .llseek
= seq_lseek
,
1720 .release
= seq_release_private
,
1723 struct ipmr_mfc_iter
{
1724 struct mfc_cache
**cache
;
1729 static struct mfc_cache
*ipmr_mfc_seq_idx(struct ipmr_mfc_iter
*it
, loff_t pos
)
1731 struct mfc_cache
*mfc
;
1733 it
->cache
= mfc_cache_array
;
1734 read_lock(&mrt_lock
);
1735 for (it
->ct
= 0; it
->ct
< MFC_LINES
; it
->ct
++)
1736 for (mfc
= mfc_cache_array
[it
->ct
]; mfc
; mfc
= mfc
->next
)
1739 read_unlock(&mrt_lock
);
1741 it
->cache
= &mfc_unres_queue
;
1742 spin_lock_bh(&mfc_unres_lock
);
1743 for (mfc
= mfc_unres_queue
; mfc
; mfc
= mfc
->next
)
1746 spin_unlock_bh(&mfc_unres_lock
);
1753 static void *ipmr_mfc_seq_start(struct seq_file
*seq
, loff_t
*pos
)
1755 struct ipmr_mfc_iter
*it
= seq
->private;
1758 return *pos
? ipmr_mfc_seq_idx(seq
->private, *pos
- 1)
1762 static void *ipmr_mfc_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
1764 struct mfc_cache
*mfc
= v
;
1765 struct ipmr_mfc_iter
*it
= seq
->private;
1769 if (v
== SEQ_START_TOKEN
)
1770 return ipmr_mfc_seq_idx(seq
->private, 0);
1775 if (it
->cache
== &mfc_unres_queue
)
1778 BUG_ON(it
->cache
!= mfc_cache_array
);
1780 while (++it
->ct
< MFC_LINES
) {
1781 mfc
= mfc_cache_array
[it
->ct
];
1786 /* exhausted cache_array, show unresolved */
1787 read_unlock(&mrt_lock
);
1788 it
->cache
= &mfc_unres_queue
;
1791 spin_lock_bh(&mfc_unres_lock
);
1792 mfc
= mfc_unres_queue
;
1797 spin_unlock_bh(&mfc_unres_lock
);
1803 static void ipmr_mfc_seq_stop(struct seq_file
*seq
, void *v
)
1805 struct ipmr_mfc_iter
*it
= seq
->private;
1807 if (it
->cache
== &mfc_unres_queue
)
1808 spin_unlock_bh(&mfc_unres_lock
);
1809 else if (it
->cache
== mfc_cache_array
)
1810 read_unlock(&mrt_lock
);
1813 static int ipmr_mfc_seq_show(struct seq_file
*seq
, void *v
)
1817 if (v
== SEQ_START_TOKEN
) {
1819 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
1821 const struct mfc_cache
*mfc
= v
;
1822 const struct ipmr_mfc_iter
*it
= seq
->private;
1824 seq_printf(seq
, "%08lX %08lX %-3d %8ld %8ld %8ld",
1825 (unsigned long) mfc
->mfc_mcastgrp
,
1826 (unsigned long) mfc
->mfc_origin
,
1828 mfc
->mfc_un
.res
.pkt
,
1829 mfc
->mfc_un
.res
.bytes
,
1830 mfc
->mfc_un
.res
.wrong_if
);
1832 if (it
->cache
!= &mfc_unres_queue
) {
1833 for (n
= mfc
->mfc_un
.res
.minvif
;
1834 n
< mfc
->mfc_un
.res
.maxvif
; n
++ ) {
1836 && mfc
->mfc_un
.res
.ttls
[n
] < 255)
1839 n
, mfc
->mfc_un
.res
.ttls
[n
]);
1842 seq_putc(seq
, '\n');
1847 static struct seq_operations ipmr_mfc_seq_ops
= {
1848 .start
= ipmr_mfc_seq_start
,
1849 .next
= ipmr_mfc_seq_next
,
1850 .stop
= ipmr_mfc_seq_stop
,
1851 .show
= ipmr_mfc_seq_show
,
1854 static int ipmr_mfc_open(struct inode
*inode
, struct file
*file
)
1856 struct seq_file
*seq
;
1858 struct ipmr_mfc_iter
*s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
1863 rc
= seq_open(file
, &ipmr_mfc_seq_ops
);
1867 seq
= file
->private_data
;
1877 static const struct file_operations ipmr_mfc_fops
= {
1878 .owner
= THIS_MODULE
,
1879 .open
= ipmr_mfc_open
,
1881 .llseek
= seq_lseek
,
1882 .release
= seq_release_private
,
1886 #ifdef CONFIG_IP_PIMSM_V2
1887 static struct net_protocol pim_protocol
= {
1894 * Setup for IP multicast routing
1897 void __init
ip_mr_init(void)
1899 mrt_cachep
= kmem_cache_create("ip_mrt_cache",
1900 sizeof(struct mfc_cache
),
1901 0, SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
1903 init_timer(&ipmr_expire_timer
);
1904 ipmr_expire_timer
.function
=ipmr_expire_process
;
1905 register_netdevice_notifier(&ip_mr_notifier
);
1906 #ifdef CONFIG_PROC_FS
1907 proc_net_fops_create("ip_mr_vif", 0, &ipmr_vif_fops
);
1908 proc_net_fops_create("ip_mr_cache", 0, &ipmr_mfc_fops
);