2 * IP multicast routing support for mrouted 3.6/3.8
4 * (c) 1995 Alan Cox, <alan@lxorguk.ukuu.org.uk>
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 <linux/slab.h>
51 #include <net/net_namespace.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>
65 #include <net/netlink.h>
67 #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
68 #define CONFIG_IP_PIMSM 1
71 /* Big lock, protecting vif table, mrt cache and mroute socket state.
72 Note that the changes are semaphored via rtnl_lock.
75 static DEFINE_RWLOCK(mrt_lock);
78 * Multicast router control variables
81 #define VIF_EXISTS(_net, _idx) ((_net)->ipv4.vif_table[_idx].dev != NULL)
83 /* Special spinlock for queue of unresolved entries */
84 static DEFINE_SPINLOCK(mfc_unres_lock);
86 /* We return to original Alan's scheme. Hash table of resolved
87 entries is changed only in process context and protected
88 with weak lock mrt_lock. Queue of unresolved entries is protected
89 with strong spinlock mfc_unres_lock.
91 In this case data path is free of exclusive locks at all.
94 static struct kmem_cache *mrt_cachep __read_mostly;
96 static int ip_mr_forward(struct sk_buff *skb, struct mfc_cache *cache, int local);
97 static int ipmr_cache_report(struct net *net,
98 struct sk_buff *pkt, vifi_t vifi, int assert);
99 static int ipmr_fill_mroute(struct sk_buff *skb, struct mfc_cache *c, struct rtmsg *rtm);
101 /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
103 static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
105 struct net *net = dev_net(dev);
109 dev = __dev_get_by_name(net, "tunl0");
111 const struct net_device_ops *ops = dev->netdev_ops;
113 struct ip_tunnel_parm p;
115 memset(&p, 0, sizeof(p));
116 p.iph.daddr = v->vifc_rmt_addr.s_addr;
117 p.iph.saddr = v->vifc_lcl_addr.s_addr;
120 p.iph.protocol = IPPROTO_IPIP;
121 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
122 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
124 if (ops->ndo_do_ioctl) {
125 mm_segment_t oldfs = get_fs();
128 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
135 struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
137 struct net_device *dev;
139 dev = __dev_get_by_name(net, "tunl0");
142 const struct net_device_ops *ops = dev->netdev_ops;
145 struct ip_tunnel_parm p;
146 struct in_device *in_dev;
148 memset(&p, 0, sizeof(p));
149 p.iph.daddr = v->vifc_rmt_addr.s_addr;
150 p.iph.saddr = v->vifc_lcl_addr.s_addr;
153 p.iph.protocol = IPPROTO_IPIP;
154 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
155 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
157 if (ops->ndo_do_ioctl) {
158 mm_segment_t oldfs = get_fs();
161 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
169 (dev = __dev_get_by_name(net, p.name)) != NULL) {
170 dev->flags |= IFF_MULTICAST;
172 in_dev = __in_dev_get_rtnl(dev);
176 ipv4_devconf_setall(in_dev);
177 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
187 /* allow the register to be completed before unregistering. */
191 unregister_netdevice(dev);
195 #ifdef CONFIG_IP_PIMSM
197 static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
199 struct net *net = dev_net(dev);
201 read_lock(&mrt_lock);
202 dev->stats.tx_bytes += skb->len;
203 dev->stats.tx_packets++;
204 ipmr_cache_report(net, skb, net->ipv4.mroute_reg_vif_num,
206 read_unlock(&mrt_lock);
211 static const struct net_device_ops reg_vif_netdev_ops = {
212 .ndo_start_xmit = reg_vif_xmit,
215 static void reg_vif_setup(struct net_device *dev)
217 dev->type = ARPHRD_PIMREG;
218 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
219 dev->flags = IFF_NOARP;
220 dev->netdev_ops = ®_vif_netdev_ops,
221 dev->destructor = free_netdev;
222 dev->features |= NETIF_F_NETNS_LOCAL;
225 static struct net_device *ipmr_reg_vif(struct net *net)
227 struct net_device *dev;
228 struct in_device *in_dev;
230 dev = alloc_netdev(0, "pimreg", reg_vif_setup);
235 dev_net_set(dev, net);
237 if (register_netdevice(dev)) {
244 if ((in_dev = __in_dev_get_rcu(dev)) == NULL) {
249 ipv4_devconf_setall(in_dev);
250 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
261 /* allow the register to be completed before unregistering. */
265 unregister_netdevice(dev);
272 * @notify: Set to 1, if the caller is a notifier_call
275 static int vif_delete(struct net *net, int vifi, int notify,
276 struct list_head *head)
278 struct vif_device *v;
279 struct net_device *dev;
280 struct in_device *in_dev;
282 if (vifi < 0 || vifi >= net->ipv4.maxvif)
283 return -EADDRNOTAVAIL;
285 v = &net->ipv4.vif_table[vifi];
287 write_lock_bh(&mrt_lock);
292 write_unlock_bh(&mrt_lock);
293 return -EADDRNOTAVAIL;
296 #ifdef CONFIG_IP_PIMSM
297 if (vifi == net->ipv4.mroute_reg_vif_num)
298 net->ipv4.mroute_reg_vif_num = -1;
301 if (vifi+1 == net->ipv4.maxvif) {
303 for (tmp=vifi-1; tmp>=0; tmp--) {
304 if (VIF_EXISTS(net, tmp))
307 net->ipv4.maxvif = tmp+1;
310 write_unlock_bh(&mrt_lock);
312 dev_set_allmulti(dev, -1);
314 if ((in_dev = __in_dev_get_rtnl(dev)) != NULL) {
315 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
316 ip_rt_multicast_event(in_dev);
319 if (v->flags&(VIFF_TUNNEL|VIFF_REGISTER) && !notify)
320 unregister_netdevice_queue(dev, head);
326 static inline void ipmr_cache_free(struct mfc_cache *c)
328 release_net(mfc_net(c));
329 kmem_cache_free(mrt_cachep, c);
332 /* Destroy an unresolved cache entry, killing queued skbs
333 and reporting error to netlink readers.
336 static void ipmr_destroy_unres(struct mfc_cache *c)
340 struct net *net = mfc_net(c);
342 atomic_dec(&net->ipv4.cache_resolve_queue_len);
344 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
345 if (ip_hdr(skb)->version == 0) {
346 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
347 nlh->nlmsg_type = NLMSG_ERROR;
348 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
349 skb_trim(skb, nlh->nlmsg_len);
351 e->error = -ETIMEDOUT;
352 memset(&e->msg, 0, sizeof(e->msg));
354 rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
363 /* Timer process for the unresolved queue. */
365 static void ipmr_expire_process(unsigned long arg)
367 struct net *net = (struct net *)arg;
369 unsigned long expires;
370 struct mfc_cache *c, **cp;
372 if (!spin_trylock(&mfc_unres_lock)) {
373 mod_timer(&net->ipv4.ipmr_expire_timer, jiffies+HZ/10);
377 if (net->ipv4.mfc_unres_queue == NULL)
382 cp = &net->ipv4.mfc_unres_queue;
384 while ((c=*cp) != NULL) {
385 if (time_after(c->mfc_un.unres.expires, now)) {
386 unsigned long interval = c->mfc_un.unres.expires - now;
387 if (interval < expires)
395 ipmr_destroy_unres(c);
398 if (net->ipv4.mfc_unres_queue != NULL)
399 mod_timer(&net->ipv4.ipmr_expire_timer, jiffies + expires);
402 spin_unlock(&mfc_unres_lock);
405 /* Fill oifs list. It is called under write locked mrt_lock. */
407 static void ipmr_update_thresholds(struct mfc_cache *cache, unsigned char *ttls)
410 struct net *net = mfc_net(cache);
412 cache->mfc_un.res.minvif = MAXVIFS;
413 cache->mfc_un.res.maxvif = 0;
414 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
416 for (vifi = 0; vifi < net->ipv4.maxvif; vifi++) {
417 if (VIF_EXISTS(net, vifi) &&
418 ttls[vifi] && ttls[vifi] < 255) {
419 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
420 if (cache->mfc_un.res.minvif > vifi)
421 cache->mfc_un.res.minvif = vifi;
422 if (cache->mfc_un.res.maxvif <= vifi)
423 cache->mfc_un.res.maxvif = vifi + 1;
428 static int vif_add(struct net *net, struct vifctl *vifc, int mrtsock)
430 int vifi = vifc->vifc_vifi;
431 struct vif_device *v = &net->ipv4.vif_table[vifi];
432 struct net_device *dev;
433 struct in_device *in_dev;
437 if (VIF_EXISTS(net, vifi))
440 switch (vifc->vifc_flags) {
441 #ifdef CONFIG_IP_PIMSM
444 * Special Purpose VIF in PIM
445 * All the packets will be sent to the daemon
447 if (net->ipv4.mroute_reg_vif_num >= 0)
449 dev = ipmr_reg_vif(net);
452 err = dev_set_allmulti(dev, 1);
454 unregister_netdevice(dev);
461 dev = ipmr_new_tunnel(net, vifc);
464 err = dev_set_allmulti(dev, 1);
466 ipmr_del_tunnel(dev, vifc);
472 case VIFF_USE_IFINDEX:
474 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
475 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
476 if (dev && dev->ip_ptr == NULL) {
478 return -EADDRNOTAVAIL;
481 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
484 return -EADDRNOTAVAIL;
485 err = dev_set_allmulti(dev, 1);
495 if ((in_dev = __in_dev_get_rtnl(dev)) == NULL) {
497 return -EADDRNOTAVAIL;
499 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
500 ip_rt_multicast_event(in_dev);
503 * Fill in the VIF structures
505 v->rate_limit = vifc->vifc_rate_limit;
506 v->local = vifc->vifc_lcl_addr.s_addr;
507 v->remote = vifc->vifc_rmt_addr.s_addr;
508 v->flags = vifc->vifc_flags;
510 v->flags |= VIFF_STATIC;
511 v->threshold = vifc->vifc_threshold;
516 v->link = dev->ifindex;
517 if (v->flags&(VIFF_TUNNEL|VIFF_REGISTER))
518 v->link = dev->iflink;
520 /* And finish update writing critical data */
521 write_lock_bh(&mrt_lock);
523 #ifdef CONFIG_IP_PIMSM
524 if (v->flags&VIFF_REGISTER)
525 net->ipv4.mroute_reg_vif_num = vifi;
527 if (vifi+1 > net->ipv4.maxvif)
528 net->ipv4.maxvif = vifi+1;
529 write_unlock_bh(&mrt_lock);
533 static struct mfc_cache *ipmr_cache_find(struct net *net,
537 int line = MFC_HASH(mcastgrp, origin);
540 for (c = net->ipv4.mfc_cache_array[line]; c; c = c->next) {
541 if (c->mfc_origin==origin && c->mfc_mcastgrp==mcastgrp)
548 * Allocate a multicast cache entry
550 static struct mfc_cache *ipmr_cache_alloc(struct net *net)
552 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
555 c->mfc_un.res.minvif = MAXVIFS;
560 static struct mfc_cache *ipmr_cache_alloc_unres(struct net *net)
562 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
565 skb_queue_head_init(&c->mfc_un.unres.unresolved);
566 c->mfc_un.unres.expires = jiffies + 10*HZ;
572 * A cache entry has gone into a resolved state from queued
575 static void ipmr_cache_resolve(struct mfc_cache *uc, struct mfc_cache *c)
581 * Play the pending entries through our router
584 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
585 if (ip_hdr(skb)->version == 0) {
586 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
588 if (ipmr_fill_mroute(skb, c, NLMSG_DATA(nlh)) > 0) {
589 nlh->nlmsg_len = (skb_tail_pointer(skb) -
592 nlh->nlmsg_type = NLMSG_ERROR;
593 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
594 skb_trim(skb, nlh->nlmsg_len);
596 e->error = -EMSGSIZE;
597 memset(&e->msg, 0, sizeof(e->msg));
600 rtnl_unicast(skb, mfc_net(c), NETLINK_CB(skb).pid);
602 ip_mr_forward(skb, c, 0);
607 * Bounce a cache query up to mrouted. We could use netlink for this but mrouted
608 * expects the following bizarre scheme.
610 * Called under mrt_lock.
613 static int ipmr_cache_report(struct net *net,
614 struct sk_buff *pkt, vifi_t vifi, int assert)
617 const int ihl = ip_hdrlen(pkt);
618 struct igmphdr *igmp;
622 #ifdef CONFIG_IP_PIMSM
623 if (assert == IGMPMSG_WHOLEPKT)
624 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
627 skb = alloc_skb(128, GFP_ATOMIC);
632 #ifdef CONFIG_IP_PIMSM
633 if (assert == IGMPMSG_WHOLEPKT) {
634 /* Ugly, but we have no choice with this interface.
635 Duplicate old header, fix ihl, length etc.
636 And all this only to mangle msg->im_msgtype and
637 to set msg->im_mbz to "mbz" :-)
639 skb_push(skb, sizeof(struct iphdr));
640 skb_reset_network_header(skb);
641 skb_reset_transport_header(skb);
642 msg = (struct igmpmsg *)skb_network_header(skb);
643 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
644 msg->im_msgtype = IGMPMSG_WHOLEPKT;
646 msg->im_vif = net->ipv4.mroute_reg_vif_num;
647 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
648 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
649 sizeof(struct iphdr));
658 skb->network_header = skb->tail;
660 skb_copy_to_linear_data(skb, pkt->data, ihl);
661 ip_hdr(skb)->protocol = 0; /* Flag to the kernel this is a route add */
662 msg = (struct igmpmsg *)skb_network_header(skb);
664 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
670 igmp=(struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
672 msg->im_msgtype = assert;
674 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
675 skb->transport_header = skb->network_header;
678 if (net->ipv4.mroute_sk == NULL) {
686 ret = sock_queue_rcv_skb(net->ipv4.mroute_sk, skb);
689 printk(KERN_WARNING "mroute: pending queue full, dropping entries.\n");
697 * Queue a packet for resolution. It gets locked cache entry!
701 ipmr_cache_unresolved(struct net *net, vifi_t vifi, struct sk_buff *skb)
705 const struct iphdr *iph = ip_hdr(skb);
707 spin_lock_bh(&mfc_unres_lock);
708 for (c=net->ipv4.mfc_unres_queue; c; c=c->next) {
709 if (c->mfc_mcastgrp == iph->daddr &&
710 c->mfc_origin == iph->saddr)
716 * Create a new entry if allowable
719 if (atomic_read(&net->ipv4.cache_resolve_queue_len) >= 10 ||
720 (c = ipmr_cache_alloc_unres(net)) == NULL) {
721 spin_unlock_bh(&mfc_unres_lock);
728 * Fill in the new cache entry
731 c->mfc_origin = iph->saddr;
732 c->mfc_mcastgrp = iph->daddr;
735 * Reflect first query at mrouted.
737 err = ipmr_cache_report(net, skb, vifi, IGMPMSG_NOCACHE);
739 /* If the report failed throw the cache entry
742 spin_unlock_bh(&mfc_unres_lock);
749 atomic_inc(&net->ipv4.cache_resolve_queue_len);
750 c->next = net->ipv4.mfc_unres_queue;
751 net->ipv4.mfc_unres_queue = c;
753 mod_timer(&net->ipv4.ipmr_expire_timer, c->mfc_un.unres.expires);
757 * See if we can append the packet
759 if (c->mfc_un.unres.unresolved.qlen>3) {
763 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
767 spin_unlock_bh(&mfc_unres_lock);
772 * MFC cache manipulation by user space mroute daemon
775 static int ipmr_mfc_delete(struct net *net, struct mfcctl *mfc)
778 struct mfc_cache *c, **cp;
780 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
782 for (cp = &net->ipv4.mfc_cache_array[line];
783 (c = *cp) != NULL; cp = &c->next) {
784 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
785 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
786 write_lock_bh(&mrt_lock);
788 write_unlock_bh(&mrt_lock);
797 static int ipmr_mfc_add(struct net *net, struct mfcctl *mfc, int mrtsock)
800 struct mfc_cache *uc, *c, **cp;
802 if (mfc->mfcc_parent >= MAXVIFS)
805 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
807 for (cp = &net->ipv4.mfc_cache_array[line];
808 (c = *cp) != NULL; cp = &c->next) {
809 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
810 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr)
815 write_lock_bh(&mrt_lock);
816 c->mfc_parent = mfc->mfcc_parent;
817 ipmr_update_thresholds(c, mfc->mfcc_ttls);
819 c->mfc_flags |= MFC_STATIC;
820 write_unlock_bh(&mrt_lock);
824 if (!ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
827 c = ipmr_cache_alloc(net);
831 c->mfc_origin = mfc->mfcc_origin.s_addr;
832 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
833 c->mfc_parent = mfc->mfcc_parent;
834 ipmr_update_thresholds(c, mfc->mfcc_ttls);
836 c->mfc_flags |= MFC_STATIC;
838 write_lock_bh(&mrt_lock);
839 c->next = net->ipv4.mfc_cache_array[line];
840 net->ipv4.mfc_cache_array[line] = c;
841 write_unlock_bh(&mrt_lock);
844 * Check to see if we resolved a queued list. If so we
845 * need to send on the frames and tidy up.
847 spin_lock_bh(&mfc_unres_lock);
848 for (cp = &net->ipv4.mfc_unres_queue; (uc=*cp) != NULL;
850 if (uc->mfc_origin == c->mfc_origin &&
851 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
853 atomic_dec(&net->ipv4.cache_resolve_queue_len);
857 if (net->ipv4.mfc_unres_queue == NULL)
858 del_timer(&net->ipv4.ipmr_expire_timer);
859 spin_unlock_bh(&mfc_unres_lock);
862 ipmr_cache_resolve(uc, c);
869 * Close the multicast socket, and clear the vif tables etc
872 static void mroute_clean_tables(struct net *net)
878 * Shut down all active vif entries
880 for (i = 0; i < net->ipv4.maxvif; i++) {
881 if (!(net->ipv4.vif_table[i].flags&VIFF_STATIC))
882 vif_delete(net, i, 0, &list);
884 unregister_netdevice_many(&list);
889 for (i=0; i<MFC_LINES; i++) {
890 struct mfc_cache *c, **cp;
892 cp = &net->ipv4.mfc_cache_array[i];
893 while ((c = *cp) != NULL) {
894 if (c->mfc_flags&MFC_STATIC) {
898 write_lock_bh(&mrt_lock);
900 write_unlock_bh(&mrt_lock);
906 if (atomic_read(&net->ipv4.cache_resolve_queue_len) != 0) {
907 struct mfc_cache *c, **cp;
909 spin_lock_bh(&mfc_unres_lock);
910 cp = &net->ipv4.mfc_unres_queue;
911 while ((c = *cp) != NULL) {
913 ipmr_destroy_unres(c);
915 spin_unlock_bh(&mfc_unres_lock);
919 static void mrtsock_destruct(struct sock *sk)
921 struct net *net = sock_net(sk);
924 if (sk == net->ipv4.mroute_sk) {
925 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
927 write_lock_bh(&mrt_lock);
928 net->ipv4.mroute_sk = NULL;
929 write_unlock_bh(&mrt_lock);
931 mroute_clean_tables(net);
937 * Socket options and virtual interface manipulation. The whole
938 * virtual interface system is a complete heap, but unfortunately
939 * that's how BSD mrouted happens to think. Maybe one day with a proper
940 * MOSPF/PIM router set up we can clean this up.
943 int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
948 struct net *net = sock_net(sk);
950 if (optname != MRT_INIT) {
951 if (sk != net->ipv4.mroute_sk && !capable(CAP_NET_ADMIN))
957 if (sk->sk_type != SOCK_RAW ||
958 inet_sk(sk)->inet_num != IPPROTO_IGMP)
960 if (optlen != sizeof(int))
964 if (net->ipv4.mroute_sk) {
969 ret = ip_ra_control(sk, 1, mrtsock_destruct);
971 write_lock_bh(&mrt_lock);
972 net->ipv4.mroute_sk = sk;
973 write_unlock_bh(&mrt_lock);
975 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
980 if (sk != net->ipv4.mroute_sk)
982 return ip_ra_control(sk, 0, NULL);
985 if (optlen != sizeof(vif))
987 if (copy_from_user(&vif, optval, sizeof(vif)))
989 if (vif.vifc_vifi >= MAXVIFS)
992 if (optname == MRT_ADD_VIF) {
993 ret = vif_add(net, &vif, sk == net->ipv4.mroute_sk);
995 ret = vif_delete(net, vif.vifc_vifi, 0, NULL);
1001 * Manipulate the forwarding caches. These live
1002 * in a sort of kernel/user symbiosis.
1006 if (optlen != sizeof(mfc))
1008 if (copy_from_user(&mfc, optval, sizeof(mfc)))
1011 if (optname == MRT_DEL_MFC)
1012 ret = ipmr_mfc_delete(net, &mfc);
1014 ret = ipmr_mfc_add(net, &mfc, sk == net->ipv4.mroute_sk);
1018 * Control PIM assert.
1023 if (get_user(v,(int __user *)optval))
1025 net->ipv4.mroute_do_assert = (v) ? 1 : 0;
1028 #ifdef CONFIG_IP_PIMSM
1033 if (get_user(v,(int __user *)optval))
1039 if (v != net->ipv4.mroute_do_pim) {
1040 net->ipv4.mroute_do_pim = v;
1041 net->ipv4.mroute_do_assert = v;
1048 * Spurious command, or MRT_VERSION which you cannot
1052 return -ENOPROTOOPT;
1057 * Getsock opt support for the multicast routing system.
1060 int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
1064 struct net *net = sock_net(sk);
1066 if (optname != MRT_VERSION &&
1067 #ifdef CONFIG_IP_PIMSM
1070 optname!=MRT_ASSERT)
1071 return -ENOPROTOOPT;
1073 if (get_user(olr, optlen))
1076 olr = min_t(unsigned int, olr, sizeof(int));
1080 if (put_user(olr, optlen))
1082 if (optname == MRT_VERSION)
1084 #ifdef CONFIG_IP_PIMSM
1085 else if (optname == MRT_PIM)
1086 val = net->ipv4.mroute_do_pim;
1089 val = net->ipv4.mroute_do_assert;
1090 if (copy_to_user(optval, &val, olr))
1096 * The IP multicast ioctl support routines.
1099 int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1101 struct sioc_sg_req sr;
1102 struct sioc_vif_req vr;
1103 struct vif_device *vif;
1104 struct mfc_cache *c;
1105 struct net *net = sock_net(sk);
1109 if (copy_from_user(&vr, arg, sizeof(vr)))
1111 if (vr.vifi >= net->ipv4.maxvif)
1113 read_lock(&mrt_lock);
1114 vif = &net->ipv4.vif_table[vr.vifi];
1115 if (VIF_EXISTS(net, vr.vifi)) {
1116 vr.icount = vif->pkt_in;
1117 vr.ocount = vif->pkt_out;
1118 vr.ibytes = vif->bytes_in;
1119 vr.obytes = vif->bytes_out;
1120 read_unlock(&mrt_lock);
1122 if (copy_to_user(arg, &vr, sizeof(vr)))
1126 read_unlock(&mrt_lock);
1127 return -EADDRNOTAVAIL;
1129 if (copy_from_user(&sr, arg, sizeof(sr)))
1132 read_lock(&mrt_lock);
1133 c = ipmr_cache_find(net, sr.src.s_addr, sr.grp.s_addr);
1135 sr.pktcnt = c->mfc_un.res.pkt;
1136 sr.bytecnt = c->mfc_un.res.bytes;
1137 sr.wrong_if = c->mfc_un.res.wrong_if;
1138 read_unlock(&mrt_lock);
1140 if (copy_to_user(arg, &sr, sizeof(sr)))
1144 read_unlock(&mrt_lock);
1145 return -EADDRNOTAVAIL;
1147 return -ENOIOCTLCMD;
1152 static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1154 struct net_device *dev = ptr;
1155 struct net *net = dev_net(dev);
1156 struct vif_device *v;
1160 if (event != NETDEV_UNREGISTER)
1162 v = &net->ipv4.vif_table[0];
1163 for (ct = 0; ct < net->ipv4.maxvif; ct++, v++) {
1165 vif_delete(net, ct, 1, &list);
1167 unregister_netdevice_many(&list);
1172 static struct notifier_block ip_mr_notifier = {
1173 .notifier_call = ipmr_device_event,
1177 * Encapsulate a packet by attaching a valid IPIP header to it.
1178 * This avoids tunnel drivers and other mess and gives us the speed so
1179 * important for multicast video.
1182 static void ip_encap(struct sk_buff *skb, __be32 saddr, __be32 daddr)
1185 struct iphdr *old_iph = ip_hdr(skb);
1187 skb_push(skb, sizeof(struct iphdr));
1188 skb->transport_header = skb->network_header;
1189 skb_reset_network_header(skb);
1193 iph->tos = old_iph->tos;
1194 iph->ttl = old_iph->ttl;
1198 iph->protocol = IPPROTO_IPIP;
1200 iph->tot_len = htons(skb->len);
1201 ip_select_ident(iph, skb_dst(skb), NULL);
1204 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1208 static inline int ipmr_forward_finish(struct sk_buff *skb)
1210 struct ip_options * opt = &(IPCB(skb)->opt);
1212 IP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTFORWDATAGRAMS);
1214 if (unlikely(opt->optlen))
1215 ip_forward_options(skb);
1217 return dst_output(skb);
1221 * Processing handlers for ipmr_forward
1224 static void ipmr_queue_xmit(struct sk_buff *skb, struct mfc_cache *c, int vifi)
1226 struct net *net = mfc_net(c);
1227 const struct iphdr *iph = ip_hdr(skb);
1228 struct vif_device *vif = &net->ipv4.vif_table[vifi];
1229 struct net_device *dev;
1233 if (vif->dev == NULL)
1236 #ifdef CONFIG_IP_PIMSM
1237 if (vif->flags & VIFF_REGISTER) {
1239 vif->bytes_out += skb->len;
1240 vif->dev->stats.tx_bytes += skb->len;
1241 vif->dev->stats.tx_packets++;
1242 ipmr_cache_report(net, skb, vifi, IGMPMSG_WHOLEPKT);
1247 if (vif->flags&VIFF_TUNNEL) {
1248 struct flowi fl = { .oif = vif->link,
1250 { .daddr = vif->remote,
1251 .saddr = vif->local,
1252 .tos = RT_TOS(iph->tos) } },
1253 .proto = IPPROTO_IPIP };
1254 if (ip_route_output_key(net, &rt, &fl))
1256 encap = sizeof(struct iphdr);
1258 struct flowi fl = { .oif = vif->link,
1260 { .daddr = iph->daddr,
1261 .tos = RT_TOS(iph->tos) } },
1262 .proto = IPPROTO_IPIP };
1263 if (ip_route_output_key(net, &rt, &fl))
1267 dev = rt->u.dst.dev;
1269 if (skb->len+encap > dst_mtu(&rt->u.dst) && (ntohs(iph->frag_off) & IP_DF)) {
1270 /* Do not fragment multicasts. Alas, IPv4 does not
1271 allow to send ICMP, so that packets will disappear
1275 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
1280 encap += LL_RESERVED_SPACE(dev) + rt->u.dst.header_len;
1282 if (skb_cow(skb, encap)) {
1288 vif->bytes_out += skb->len;
1291 skb_dst_set(skb, &rt->u.dst);
1292 ip_decrease_ttl(ip_hdr(skb));
1294 /* FIXME: forward and output firewalls used to be called here.
1295 * What do we do with netfilter? -- RR */
1296 if (vif->flags & VIFF_TUNNEL) {
1297 ip_encap(skb, vif->local, vif->remote);
1298 /* FIXME: extra output firewall step used to be here. --RR */
1299 vif->dev->stats.tx_packets++;
1300 vif->dev->stats.tx_bytes += skb->len;
1303 IPCB(skb)->flags |= IPSKB_FORWARDED;
1306 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1307 * not only before forwarding, but after forwarding on all output
1308 * interfaces. It is clear, if mrouter runs a multicasting
1309 * program, it should receive packets not depending to what interface
1310 * program is joined.
1311 * If we will not make it, the program will have to join on all
1312 * interfaces. On the other hand, multihoming host (or router, but
1313 * not mrouter) cannot join to more than one interface - it will
1314 * result in receiving multiple packets.
1316 NF_HOOK(PF_INET, NF_INET_FORWARD, skb, skb->dev, dev,
1317 ipmr_forward_finish);
1325 static int ipmr_find_vif(struct net_device *dev)
1327 struct net *net = dev_net(dev);
1329 for (ct = net->ipv4.maxvif-1; ct >= 0; ct--) {
1330 if (net->ipv4.vif_table[ct].dev == dev)
1336 /* "local" means that we should preserve one skb (for local delivery) */
1338 static int ip_mr_forward(struct sk_buff *skb, struct mfc_cache *cache, int local)
1342 struct net *net = mfc_net(cache);
1344 vif = cache->mfc_parent;
1345 cache->mfc_un.res.pkt++;
1346 cache->mfc_un.res.bytes += skb->len;
1349 * Wrong interface: drop packet and (maybe) send PIM assert.
1351 if (net->ipv4.vif_table[vif].dev != skb->dev) {
1354 if (skb_rtable(skb)->fl.iif == 0) {
1355 /* It is our own packet, looped back.
1356 Very complicated situation...
1358 The best workaround until routing daemons will be
1359 fixed is not to redistribute packet, if it was
1360 send through wrong interface. It means, that
1361 multicast applications WILL NOT work for
1362 (S,G), which have default multicast route pointing
1363 to wrong oif. In any case, it is not a good
1364 idea to use multicasting applications on router.
1369 cache->mfc_un.res.wrong_if++;
1370 true_vifi = ipmr_find_vif(skb->dev);
1372 if (true_vifi >= 0 && net->ipv4.mroute_do_assert &&
1373 /* pimsm uses asserts, when switching from RPT to SPT,
1374 so that we cannot check that packet arrived on an oif.
1375 It is bad, but otherwise we would need to move pretty
1376 large chunk of pimd to kernel. Ough... --ANK
1378 (net->ipv4.mroute_do_pim ||
1379 cache->mfc_un.res.ttls[true_vifi] < 255) &&
1381 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1382 cache->mfc_un.res.last_assert = jiffies;
1383 ipmr_cache_report(net, skb, true_vifi, IGMPMSG_WRONGVIF);
1388 net->ipv4.vif_table[vif].pkt_in++;
1389 net->ipv4.vif_table[vif].bytes_in += skb->len;
1394 for (ct = cache->mfc_un.res.maxvif-1; ct >= cache->mfc_un.res.minvif; ct--) {
1395 if (ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
1397 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1399 ipmr_queue_xmit(skb2, cache, psend);
1406 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1408 ipmr_queue_xmit(skb2, cache, psend);
1410 ipmr_queue_xmit(skb, cache, psend);
1423 * Multicast packets for forwarding arrive here
1426 int ip_mr_input(struct sk_buff *skb)
1428 struct mfc_cache *cache;
1429 struct net *net = dev_net(skb->dev);
1430 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
1432 /* Packet is looped back after forward, it should not be
1433 forwarded second time, but still can be delivered locally.
1435 if (IPCB(skb)->flags&IPSKB_FORWARDED)
1439 if (IPCB(skb)->opt.router_alert) {
1440 if (ip_call_ra_chain(skb))
1442 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP){
1443 /* IGMPv1 (and broken IGMPv2 implementations sort of
1444 Cisco IOS <= 11.2(8)) do not put router alert
1445 option to IGMP packets destined to routable
1446 groups. It is very bad, because it means
1447 that we can forward NO IGMP messages.
1449 read_lock(&mrt_lock);
1450 if (net->ipv4.mroute_sk) {
1452 raw_rcv(net->ipv4.mroute_sk, skb);
1453 read_unlock(&mrt_lock);
1456 read_unlock(&mrt_lock);
1460 read_lock(&mrt_lock);
1461 cache = ipmr_cache_find(net, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
1464 * No usable cache entry
1466 if (cache == NULL) {
1470 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1471 ip_local_deliver(skb);
1473 read_unlock(&mrt_lock);
1479 vif = ipmr_find_vif(skb->dev);
1481 int err = ipmr_cache_unresolved(net, vif, skb);
1482 read_unlock(&mrt_lock);
1486 read_unlock(&mrt_lock);
1491 ip_mr_forward(skb, cache, local);
1493 read_unlock(&mrt_lock);
1496 return ip_local_deliver(skb);
1502 return ip_local_deliver(skb);
1507 #ifdef CONFIG_IP_PIMSM
1508 static int __pim_rcv(struct sk_buff *skb, unsigned int pimlen)
1510 struct net_device *reg_dev = NULL;
1511 struct iphdr *encap;
1512 struct net *net = dev_net(skb->dev);
1514 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
1517 a. packet is really destinted to a multicast group
1518 b. packet is not a NULL-REGISTER
1519 c. packet is not truncated
1521 if (!ipv4_is_multicast(encap->daddr) ||
1522 encap->tot_len == 0 ||
1523 ntohs(encap->tot_len) + pimlen > skb->len)
1526 read_lock(&mrt_lock);
1527 if (net->ipv4.mroute_reg_vif_num >= 0)
1528 reg_dev = net->ipv4.vif_table[net->ipv4.mroute_reg_vif_num].dev;
1531 read_unlock(&mrt_lock);
1533 if (reg_dev == NULL)
1536 skb->mac_header = skb->network_header;
1537 skb_pull(skb, (u8*)encap - skb->data);
1538 skb_reset_network_header(skb);
1540 skb->protocol = htons(ETH_P_IP);
1542 skb->pkt_type = PACKET_HOST;
1544 reg_dev->stats.rx_bytes += skb->len;
1545 reg_dev->stats.rx_packets++;
1554 #ifdef CONFIG_IP_PIMSM_V1
1556 * Handle IGMP messages of PIMv1
1559 int pim_rcv_v1(struct sk_buff * skb)
1561 struct igmphdr *pim;
1562 struct net *net = dev_net(skb->dev);
1564 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
1567 pim = igmp_hdr(skb);
1569 if (!net->ipv4.mroute_do_pim ||
1570 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
1573 if (__pim_rcv(skb, sizeof(*pim))) {
1581 #ifdef CONFIG_IP_PIMSM_V2
1582 static int pim_rcv(struct sk_buff * skb)
1584 struct pimreghdr *pim;
1586 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
1589 pim = (struct pimreghdr *)skb_transport_header(skb);
1590 if (pim->type != ((PIM_VERSION<<4)|(PIM_REGISTER)) ||
1591 (pim->flags&PIM_NULL_REGISTER) ||
1592 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
1593 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
1596 if (__pim_rcv(skb, sizeof(*pim))) {
1605 ipmr_fill_mroute(struct sk_buff *skb, struct mfc_cache *c, struct rtmsg *rtm)
1608 struct rtnexthop *nhp;
1609 struct net *net = mfc_net(c);
1610 u8 *b = skb_tail_pointer(skb);
1611 struct rtattr *mp_head;
1613 /* If cache is unresolved, don't try to parse IIF and OIF */
1614 if (c->mfc_parent > MAXVIFS)
1617 if (VIF_EXISTS(net, c->mfc_parent))
1618 RTA_PUT(skb, RTA_IIF, 4, &net->ipv4.vif_table[c->mfc_parent].dev->ifindex);
1620 mp_head = (struct rtattr *)skb_put(skb, RTA_LENGTH(0));
1622 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
1623 if (VIF_EXISTS(net, ct) && c->mfc_un.res.ttls[ct] < 255) {
1624 if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
1625 goto rtattr_failure;
1626 nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
1627 nhp->rtnh_flags = 0;
1628 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
1629 nhp->rtnh_ifindex = net->ipv4.vif_table[ct].dev->ifindex;
1630 nhp->rtnh_len = sizeof(*nhp);
1633 mp_head->rta_type = RTA_MULTIPATH;
1634 mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head;
1635 rtm->rtm_type = RTN_MULTICAST;
1643 int ipmr_get_route(struct net *net,
1644 struct sk_buff *skb, struct rtmsg *rtm, int nowait)
1647 struct mfc_cache *cache;
1648 struct rtable *rt = skb_rtable(skb);
1650 read_lock(&mrt_lock);
1651 cache = ipmr_cache_find(net, rt->rt_src, rt->rt_dst);
1653 if (cache == NULL) {
1654 struct sk_buff *skb2;
1656 struct net_device *dev;
1660 read_unlock(&mrt_lock);
1665 if (dev == NULL || (vif = ipmr_find_vif(dev)) < 0) {
1666 read_unlock(&mrt_lock);
1669 skb2 = skb_clone(skb, GFP_ATOMIC);
1671 read_unlock(&mrt_lock);
1675 skb_push(skb2, sizeof(struct iphdr));
1676 skb_reset_network_header(skb2);
1678 iph->ihl = sizeof(struct iphdr) >> 2;
1679 iph->saddr = rt->rt_src;
1680 iph->daddr = rt->rt_dst;
1682 err = ipmr_cache_unresolved(net, vif, skb2);
1683 read_unlock(&mrt_lock);
1687 if (!nowait && (rtm->rtm_flags&RTM_F_NOTIFY))
1688 cache->mfc_flags |= MFC_NOTIFY;
1689 err = ipmr_fill_mroute(skb, cache, rtm);
1690 read_unlock(&mrt_lock);
1694 #ifdef CONFIG_PROC_FS
1696 * The /proc interfaces to multicast routing /proc/ip_mr_cache /proc/ip_mr_vif
1698 struct ipmr_vif_iter {
1699 struct seq_net_private p;
1703 static struct vif_device *ipmr_vif_seq_idx(struct net *net,
1704 struct ipmr_vif_iter *iter,
1707 for (iter->ct = 0; iter->ct < net->ipv4.maxvif; ++iter->ct) {
1708 if (!VIF_EXISTS(net, iter->ct))
1711 return &net->ipv4.vif_table[iter->ct];
1716 static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
1717 __acquires(mrt_lock)
1719 struct net *net = seq_file_net(seq);
1721 read_lock(&mrt_lock);
1722 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
1726 static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1728 struct ipmr_vif_iter *iter = seq->private;
1729 struct net *net = seq_file_net(seq);
1732 if (v == SEQ_START_TOKEN)
1733 return ipmr_vif_seq_idx(net, iter, 0);
1735 while (++iter->ct < net->ipv4.maxvif) {
1736 if (!VIF_EXISTS(net, iter->ct))
1738 return &net->ipv4.vif_table[iter->ct];
1743 static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
1744 __releases(mrt_lock)
1746 read_unlock(&mrt_lock);
1749 static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
1751 struct net *net = seq_file_net(seq);
1753 if (v == SEQ_START_TOKEN) {
1755 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
1757 const struct vif_device *vif = v;
1758 const char *name = vif->dev ? vif->dev->name : "none";
1761 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
1762 vif - net->ipv4.vif_table,
1763 name, vif->bytes_in, vif->pkt_in,
1764 vif->bytes_out, vif->pkt_out,
1765 vif->flags, vif->local, vif->remote);
1770 static const struct seq_operations ipmr_vif_seq_ops = {
1771 .start = ipmr_vif_seq_start,
1772 .next = ipmr_vif_seq_next,
1773 .stop = ipmr_vif_seq_stop,
1774 .show = ipmr_vif_seq_show,
1777 static int ipmr_vif_open(struct inode *inode, struct file *file)
1779 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
1780 sizeof(struct ipmr_vif_iter));
1783 static const struct file_operations ipmr_vif_fops = {
1784 .owner = THIS_MODULE,
1785 .open = ipmr_vif_open,
1787 .llseek = seq_lseek,
1788 .release = seq_release_net,
1791 struct ipmr_mfc_iter {
1792 struct seq_net_private p;
1793 struct mfc_cache **cache;
1798 static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
1799 struct ipmr_mfc_iter *it, loff_t pos)
1801 struct mfc_cache *mfc;
1803 it->cache = net->ipv4.mfc_cache_array;
1804 read_lock(&mrt_lock);
1805 for (it->ct = 0; it->ct < MFC_LINES; it->ct++)
1806 for (mfc = net->ipv4.mfc_cache_array[it->ct];
1807 mfc; mfc = mfc->next)
1810 read_unlock(&mrt_lock);
1812 it->cache = &net->ipv4.mfc_unres_queue;
1813 spin_lock_bh(&mfc_unres_lock);
1814 for (mfc = net->ipv4.mfc_unres_queue; mfc; mfc = mfc->next)
1817 spin_unlock_bh(&mfc_unres_lock);
1824 static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
1826 struct ipmr_mfc_iter *it = seq->private;
1827 struct net *net = seq_file_net(seq);
1831 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
1835 static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1837 struct mfc_cache *mfc = v;
1838 struct ipmr_mfc_iter *it = seq->private;
1839 struct net *net = seq_file_net(seq);
1843 if (v == SEQ_START_TOKEN)
1844 return ipmr_mfc_seq_idx(net, seq->private, 0);
1849 if (it->cache == &net->ipv4.mfc_unres_queue)
1852 BUG_ON(it->cache != net->ipv4.mfc_cache_array);
1854 while (++it->ct < MFC_LINES) {
1855 mfc = net->ipv4.mfc_cache_array[it->ct];
1860 /* exhausted cache_array, show unresolved */
1861 read_unlock(&mrt_lock);
1862 it->cache = &net->ipv4.mfc_unres_queue;
1865 spin_lock_bh(&mfc_unres_lock);
1866 mfc = net->ipv4.mfc_unres_queue;
1871 spin_unlock_bh(&mfc_unres_lock);
1877 static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
1879 struct ipmr_mfc_iter *it = seq->private;
1880 struct net *net = seq_file_net(seq);
1882 if (it->cache == &net->ipv4.mfc_unres_queue)
1883 spin_unlock_bh(&mfc_unres_lock);
1884 else if (it->cache == net->ipv4.mfc_cache_array)
1885 read_unlock(&mrt_lock);
1888 static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
1891 struct net *net = seq_file_net(seq);
1893 if (v == SEQ_START_TOKEN) {
1895 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
1897 const struct mfc_cache *mfc = v;
1898 const struct ipmr_mfc_iter *it = seq->private;
1900 seq_printf(seq, "%08lX %08lX %-3hd",
1901 (unsigned long) mfc->mfc_mcastgrp,
1902 (unsigned long) mfc->mfc_origin,
1905 if (it->cache != &net->ipv4.mfc_unres_queue) {
1906 seq_printf(seq, " %8lu %8lu %8lu",
1907 mfc->mfc_un.res.pkt,
1908 mfc->mfc_un.res.bytes,
1909 mfc->mfc_un.res.wrong_if);
1910 for (n = mfc->mfc_un.res.minvif;
1911 n < mfc->mfc_un.res.maxvif; n++ ) {
1912 if (VIF_EXISTS(net, n) &&
1913 mfc->mfc_un.res.ttls[n] < 255)
1916 n, mfc->mfc_un.res.ttls[n]);
1919 /* unresolved mfc_caches don't contain
1920 * pkt, bytes and wrong_if values
1922 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
1924 seq_putc(seq, '\n');
1929 static const struct seq_operations ipmr_mfc_seq_ops = {
1930 .start = ipmr_mfc_seq_start,
1931 .next = ipmr_mfc_seq_next,
1932 .stop = ipmr_mfc_seq_stop,
1933 .show = ipmr_mfc_seq_show,
1936 static int ipmr_mfc_open(struct inode *inode, struct file *file)
1938 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
1939 sizeof(struct ipmr_mfc_iter));
1942 static const struct file_operations ipmr_mfc_fops = {
1943 .owner = THIS_MODULE,
1944 .open = ipmr_mfc_open,
1946 .llseek = seq_lseek,
1947 .release = seq_release_net,
1951 #ifdef CONFIG_IP_PIMSM_V2
1952 static const struct net_protocol pim_protocol = {
1960 * Setup for IP multicast routing
1962 static int __net_init ipmr_net_init(struct net *net)
1966 net->ipv4.vif_table = kcalloc(MAXVIFS, sizeof(struct vif_device),
1968 if (!net->ipv4.vif_table) {
1973 /* Forwarding cache */
1974 net->ipv4.mfc_cache_array = kcalloc(MFC_LINES,
1975 sizeof(struct mfc_cache *),
1977 if (!net->ipv4.mfc_cache_array) {
1979 goto fail_mfc_cache;
1982 setup_timer(&net->ipv4.ipmr_expire_timer, ipmr_expire_process,
1983 (unsigned long)net);
1985 #ifdef CONFIG_IP_PIMSM
1986 net->ipv4.mroute_reg_vif_num = -1;
1989 #ifdef CONFIG_PROC_FS
1991 if (!proc_net_fops_create(net, "ip_mr_vif", 0, &ipmr_vif_fops))
1993 if (!proc_net_fops_create(net, "ip_mr_cache", 0, &ipmr_mfc_fops))
1994 goto proc_cache_fail;
1998 #ifdef CONFIG_PROC_FS
2000 proc_net_remove(net, "ip_mr_vif");
2002 kfree(net->ipv4.mfc_cache_array);
2005 kfree(net->ipv4.vif_table);
2010 static void __net_exit ipmr_net_exit(struct net *net)
2012 #ifdef CONFIG_PROC_FS
2013 proc_net_remove(net, "ip_mr_cache");
2014 proc_net_remove(net, "ip_mr_vif");
2016 kfree(net->ipv4.mfc_cache_array);
2017 kfree(net->ipv4.vif_table);
2020 static struct pernet_operations ipmr_net_ops = {
2021 .init = ipmr_net_init,
2022 .exit = ipmr_net_exit,
2025 int __init ip_mr_init(void)
2029 mrt_cachep = kmem_cache_create("ip_mrt_cache",
2030 sizeof(struct mfc_cache),
2031 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2036 err = register_pernet_subsys(&ipmr_net_ops);
2038 goto reg_pernet_fail;
2040 err = register_netdevice_notifier(&ip_mr_notifier);
2042 goto reg_notif_fail;
2043 #ifdef CONFIG_IP_PIMSM_V2
2044 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
2045 printk(KERN_ERR "ip_mr_init: can't add PIM protocol\n");
2047 goto add_proto_fail;
2052 #ifdef CONFIG_IP_PIMSM_V2
2054 unregister_netdevice_notifier(&ip_mr_notifier);
2057 unregister_pernet_subsys(&ipmr_net_ops);
2059 kmem_cache_destroy(mrt_cachep);