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 requirement 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>
66 #include <net/fib_rules.h>
68 #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
69 #define CONFIG_IP_PIMSM 1
73 struct list_head list
;
78 struct sock __rcu
*mroute_sk
;
79 struct timer_list ipmr_expire_timer
;
80 struct list_head mfc_unres_queue
;
81 struct list_head mfc_cache_array
[MFC_LINES
];
82 struct vif_device vif_table
[MAXVIFS
];
84 atomic_t cache_resolve_queue_len
;
87 #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
88 int mroute_reg_vif_num
;
93 struct fib_rule common
;
100 /* Big lock, protecting vif table, mrt cache and mroute socket state.
101 * Note that the changes are semaphored via rtnl_lock.
104 static DEFINE_RWLOCK(mrt_lock
);
107 * Multicast router control variables
110 #define VIF_EXISTS(_mrt, _idx) ((_mrt)->vif_table[_idx].dev != NULL)
112 /* Special spinlock for queue of unresolved entries */
113 static DEFINE_SPINLOCK(mfc_unres_lock
);
115 /* We return to original Alan's scheme. Hash table of resolved
116 * entries is changed only in process context and protected
117 * with weak lock mrt_lock. Queue of unresolved entries is protected
118 * with strong spinlock mfc_unres_lock.
120 * In this case data path is free of exclusive locks at all.
123 static struct kmem_cache
*mrt_cachep __read_mostly
;
125 static struct mr_table
*ipmr_new_table(struct net
*net
, u32 id
);
126 static int ip_mr_forward(struct net
*net
, struct mr_table
*mrt
,
127 struct sk_buff
*skb
, struct mfc_cache
*cache
,
129 static int ipmr_cache_report(struct mr_table
*mrt
,
130 struct sk_buff
*pkt
, vifi_t vifi
, int assert);
131 static int __ipmr_fill_mroute(struct mr_table
*mrt
, struct sk_buff
*skb
,
132 struct mfc_cache
*c
, struct rtmsg
*rtm
);
133 static void ipmr_expire_process(unsigned long arg
);
135 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
136 #define ipmr_for_each_table(mrt, net) \
137 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
139 static struct mr_table
*ipmr_get_table(struct net
*net
, u32 id
)
141 struct mr_table
*mrt
;
143 ipmr_for_each_table(mrt
, net
) {
150 static int ipmr_fib_lookup(struct net
*net
, struct flowi
*flp
,
151 struct mr_table
**mrt
)
153 struct ipmr_result res
;
154 struct fib_lookup_arg arg
= { .result
= &res
, };
157 err
= fib_rules_lookup(net
->ipv4
.mr_rules_ops
, flp
, 0, &arg
);
164 static int ipmr_rule_action(struct fib_rule
*rule
, struct flowi
*flp
,
165 int flags
, struct fib_lookup_arg
*arg
)
167 struct ipmr_result
*res
= arg
->result
;
168 struct mr_table
*mrt
;
170 switch (rule
->action
) {
173 case FR_ACT_UNREACHABLE
:
175 case FR_ACT_PROHIBIT
:
177 case FR_ACT_BLACKHOLE
:
182 mrt
= ipmr_get_table(rule
->fr_net
, rule
->table
);
189 static int ipmr_rule_match(struct fib_rule
*rule
, struct flowi
*fl
, int flags
)
194 static const struct nla_policy ipmr_rule_policy
[FRA_MAX
+ 1] = {
198 static int ipmr_rule_configure(struct fib_rule
*rule
, struct sk_buff
*skb
,
199 struct fib_rule_hdr
*frh
, struct nlattr
**tb
)
204 static int ipmr_rule_compare(struct fib_rule
*rule
, struct fib_rule_hdr
*frh
,
210 static int ipmr_rule_fill(struct fib_rule
*rule
, struct sk_buff
*skb
,
211 struct fib_rule_hdr
*frh
)
219 static const struct fib_rules_ops __net_initdata ipmr_rules_ops_template
= {
220 .family
= RTNL_FAMILY_IPMR
,
221 .rule_size
= sizeof(struct ipmr_rule
),
222 .addr_size
= sizeof(u32
),
223 .action
= ipmr_rule_action
,
224 .match
= ipmr_rule_match
,
225 .configure
= ipmr_rule_configure
,
226 .compare
= ipmr_rule_compare
,
227 .default_pref
= fib_default_rule_pref
,
228 .fill
= ipmr_rule_fill
,
229 .nlgroup
= RTNLGRP_IPV4_RULE
,
230 .policy
= ipmr_rule_policy
,
231 .owner
= THIS_MODULE
,
234 static int __net_init
ipmr_rules_init(struct net
*net
)
236 struct fib_rules_ops
*ops
;
237 struct mr_table
*mrt
;
240 ops
= fib_rules_register(&ipmr_rules_ops_template
, net
);
244 INIT_LIST_HEAD(&net
->ipv4
.mr_tables
);
246 mrt
= ipmr_new_table(net
, RT_TABLE_DEFAULT
);
252 err
= fib_default_rule_add(ops
, 0x7fff, RT_TABLE_DEFAULT
, 0);
256 net
->ipv4
.mr_rules_ops
= ops
;
262 fib_rules_unregister(ops
);
266 static void __net_exit
ipmr_rules_exit(struct net
*net
)
268 struct mr_table
*mrt
, *next
;
270 list_for_each_entry_safe(mrt
, next
, &net
->ipv4
.mr_tables
, list
) {
271 list_del(&mrt
->list
);
274 fib_rules_unregister(net
->ipv4
.mr_rules_ops
);
277 #define ipmr_for_each_table(mrt, net) \
278 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
280 static struct mr_table
*ipmr_get_table(struct net
*net
, u32 id
)
282 return net
->ipv4
.mrt
;
285 static int ipmr_fib_lookup(struct net
*net
, struct flowi
*flp
,
286 struct mr_table
**mrt
)
288 *mrt
= net
->ipv4
.mrt
;
292 static int __net_init
ipmr_rules_init(struct net
*net
)
294 net
->ipv4
.mrt
= ipmr_new_table(net
, RT_TABLE_DEFAULT
);
295 return net
->ipv4
.mrt
? 0 : -ENOMEM
;
298 static void __net_exit
ipmr_rules_exit(struct net
*net
)
300 kfree(net
->ipv4
.mrt
);
304 static struct mr_table
*ipmr_new_table(struct net
*net
, u32 id
)
306 struct mr_table
*mrt
;
309 mrt
= ipmr_get_table(net
, id
);
313 mrt
= kzalloc(sizeof(*mrt
), GFP_KERNEL
);
316 write_pnet(&mrt
->net
, net
);
319 /* Forwarding cache */
320 for (i
= 0; i
< MFC_LINES
; i
++)
321 INIT_LIST_HEAD(&mrt
->mfc_cache_array
[i
]);
323 INIT_LIST_HEAD(&mrt
->mfc_unres_queue
);
325 setup_timer(&mrt
->ipmr_expire_timer
, ipmr_expire_process
,
328 #ifdef CONFIG_IP_PIMSM
329 mrt
->mroute_reg_vif_num
= -1;
331 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
332 list_add_tail_rcu(&mrt
->list
, &net
->ipv4
.mr_tables
);
337 /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
339 static void ipmr_del_tunnel(struct net_device
*dev
, struct vifctl
*v
)
341 struct net
*net
= dev_net(dev
);
345 dev
= __dev_get_by_name(net
, "tunl0");
347 const struct net_device_ops
*ops
= dev
->netdev_ops
;
349 struct ip_tunnel_parm p
;
351 memset(&p
, 0, sizeof(p
));
352 p
.iph
.daddr
= v
->vifc_rmt_addr
.s_addr
;
353 p
.iph
.saddr
= v
->vifc_lcl_addr
.s_addr
;
356 p
.iph
.protocol
= IPPROTO_IPIP
;
357 sprintf(p
.name
, "dvmrp%d", v
->vifc_vifi
);
358 ifr
.ifr_ifru
.ifru_data
= (__force
void __user
*)&p
;
360 if (ops
->ndo_do_ioctl
) {
361 mm_segment_t oldfs
= get_fs();
364 ops
->ndo_do_ioctl(dev
, &ifr
, SIOCDELTUNNEL
);
371 struct net_device
*ipmr_new_tunnel(struct net
*net
, struct vifctl
*v
)
373 struct net_device
*dev
;
375 dev
= __dev_get_by_name(net
, "tunl0");
378 const struct net_device_ops
*ops
= dev
->netdev_ops
;
381 struct ip_tunnel_parm p
;
382 struct in_device
*in_dev
;
384 memset(&p
, 0, sizeof(p
));
385 p
.iph
.daddr
= v
->vifc_rmt_addr
.s_addr
;
386 p
.iph
.saddr
= v
->vifc_lcl_addr
.s_addr
;
389 p
.iph
.protocol
= IPPROTO_IPIP
;
390 sprintf(p
.name
, "dvmrp%d", v
->vifc_vifi
);
391 ifr
.ifr_ifru
.ifru_data
= (__force
void __user
*)&p
;
393 if (ops
->ndo_do_ioctl
) {
394 mm_segment_t oldfs
= get_fs();
397 err
= ops
->ndo_do_ioctl(dev
, &ifr
, SIOCADDTUNNEL
);
405 (dev
= __dev_get_by_name(net
, p
.name
)) != NULL
) {
406 dev
->flags
|= IFF_MULTICAST
;
408 in_dev
= __in_dev_get_rtnl(dev
);
412 ipv4_devconf_setall(in_dev
);
413 IPV4_DEVCONF(in_dev
->cnf
, RP_FILTER
) = 0;
423 /* allow the register to be completed before unregistering. */
427 unregister_netdevice(dev
);
431 #ifdef CONFIG_IP_PIMSM
433 static netdev_tx_t
reg_vif_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
435 struct net
*net
= dev_net(dev
);
436 struct mr_table
*mrt
;
444 err
= ipmr_fib_lookup(net
, &fl
, &mrt
);
450 read_lock(&mrt_lock
);
451 dev
->stats
.tx_bytes
+= skb
->len
;
452 dev
->stats
.tx_packets
++;
453 ipmr_cache_report(mrt
, skb
, mrt
->mroute_reg_vif_num
, IGMPMSG_WHOLEPKT
);
454 read_unlock(&mrt_lock
);
459 static const struct net_device_ops reg_vif_netdev_ops
= {
460 .ndo_start_xmit
= reg_vif_xmit
,
463 static void reg_vif_setup(struct net_device
*dev
)
465 dev
->type
= ARPHRD_PIMREG
;
466 dev
->mtu
= ETH_DATA_LEN
- sizeof(struct iphdr
) - 8;
467 dev
->flags
= IFF_NOARP
;
468 dev
->netdev_ops
= ®_vif_netdev_ops
,
469 dev
->destructor
= free_netdev
;
470 dev
->features
|= NETIF_F_NETNS_LOCAL
;
473 static struct net_device
*ipmr_reg_vif(struct net
*net
, struct mr_table
*mrt
)
475 struct net_device
*dev
;
476 struct in_device
*in_dev
;
479 if (mrt
->id
== RT_TABLE_DEFAULT
)
480 sprintf(name
, "pimreg");
482 sprintf(name
, "pimreg%u", mrt
->id
);
484 dev
= alloc_netdev(0, name
, reg_vif_setup
);
489 dev_net_set(dev
, net
);
491 if (register_netdevice(dev
)) {
498 in_dev
= __in_dev_get_rcu(dev
);
504 ipv4_devconf_setall(in_dev
);
505 IPV4_DEVCONF(in_dev
->cnf
, RP_FILTER
) = 0;
516 /* allow the register to be completed before unregistering. */
520 unregister_netdevice(dev
);
527 * @notify: Set to 1, if the caller is a notifier_call
530 static int vif_delete(struct mr_table
*mrt
, int vifi
, int notify
,
531 struct list_head
*head
)
533 struct vif_device
*v
;
534 struct net_device
*dev
;
535 struct in_device
*in_dev
;
537 if (vifi
< 0 || vifi
>= mrt
->maxvif
)
538 return -EADDRNOTAVAIL
;
540 v
= &mrt
->vif_table
[vifi
];
542 write_lock_bh(&mrt_lock
);
547 write_unlock_bh(&mrt_lock
);
548 return -EADDRNOTAVAIL
;
551 #ifdef CONFIG_IP_PIMSM
552 if (vifi
== mrt
->mroute_reg_vif_num
)
553 mrt
->mroute_reg_vif_num
= -1;
556 if (vifi
+ 1 == mrt
->maxvif
) {
559 for (tmp
= vifi
- 1; tmp
>= 0; tmp
--) {
560 if (VIF_EXISTS(mrt
, tmp
))
566 write_unlock_bh(&mrt_lock
);
568 dev_set_allmulti(dev
, -1);
570 in_dev
= __in_dev_get_rtnl(dev
);
572 IPV4_DEVCONF(in_dev
->cnf
, MC_FORWARDING
)--;
573 ip_rt_multicast_event(in_dev
);
576 if (v
->flags
& (VIFF_TUNNEL
| VIFF_REGISTER
) && !notify
)
577 unregister_netdevice_queue(dev
, head
);
583 static void ipmr_cache_free_rcu(struct rcu_head
*head
)
585 struct mfc_cache
*c
= container_of(head
, struct mfc_cache
, rcu
);
587 kmem_cache_free(mrt_cachep
, c
);
590 static inline void ipmr_cache_free(struct mfc_cache
*c
)
592 call_rcu(&c
->rcu
, ipmr_cache_free_rcu
);
595 /* Destroy an unresolved cache entry, killing queued skbs
596 * and reporting error to netlink readers.
599 static void ipmr_destroy_unres(struct mr_table
*mrt
, struct mfc_cache
*c
)
601 struct net
*net
= read_pnet(&mrt
->net
);
605 atomic_dec(&mrt
->cache_resolve_queue_len
);
607 while ((skb
= skb_dequeue(&c
->mfc_un
.unres
.unresolved
))) {
608 if (ip_hdr(skb
)->version
== 0) {
609 struct nlmsghdr
*nlh
= (struct nlmsghdr
*)skb_pull(skb
, sizeof(struct iphdr
));
610 nlh
->nlmsg_type
= NLMSG_ERROR
;
611 nlh
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct nlmsgerr
));
612 skb_trim(skb
, nlh
->nlmsg_len
);
614 e
->error
= -ETIMEDOUT
;
615 memset(&e
->msg
, 0, sizeof(e
->msg
));
617 rtnl_unicast(skb
, net
, NETLINK_CB(skb
).pid
);
627 /* Timer process for the unresolved queue. */
629 static void ipmr_expire_process(unsigned long arg
)
631 struct mr_table
*mrt
= (struct mr_table
*)arg
;
633 unsigned long expires
;
634 struct mfc_cache
*c
, *next
;
636 if (!spin_trylock(&mfc_unres_lock
)) {
637 mod_timer(&mrt
->ipmr_expire_timer
, jiffies
+HZ
/10);
641 if (list_empty(&mrt
->mfc_unres_queue
))
647 list_for_each_entry_safe(c
, next
, &mrt
->mfc_unres_queue
, list
) {
648 if (time_after(c
->mfc_un
.unres
.expires
, now
)) {
649 unsigned long interval
= c
->mfc_un
.unres
.expires
- now
;
650 if (interval
< expires
)
656 ipmr_destroy_unres(mrt
, c
);
659 if (!list_empty(&mrt
->mfc_unres_queue
))
660 mod_timer(&mrt
->ipmr_expire_timer
, jiffies
+ expires
);
663 spin_unlock(&mfc_unres_lock
);
666 /* Fill oifs list. It is called under write locked mrt_lock. */
668 static void ipmr_update_thresholds(struct mr_table
*mrt
, struct mfc_cache
*cache
,
673 cache
->mfc_un
.res
.minvif
= MAXVIFS
;
674 cache
->mfc_un
.res
.maxvif
= 0;
675 memset(cache
->mfc_un
.res
.ttls
, 255, MAXVIFS
);
677 for (vifi
= 0; vifi
< mrt
->maxvif
; vifi
++) {
678 if (VIF_EXISTS(mrt
, vifi
) &&
679 ttls
[vifi
] && ttls
[vifi
] < 255) {
680 cache
->mfc_un
.res
.ttls
[vifi
] = ttls
[vifi
];
681 if (cache
->mfc_un
.res
.minvif
> vifi
)
682 cache
->mfc_un
.res
.minvif
= vifi
;
683 if (cache
->mfc_un
.res
.maxvif
<= vifi
)
684 cache
->mfc_un
.res
.maxvif
= vifi
+ 1;
689 static int vif_add(struct net
*net
, struct mr_table
*mrt
,
690 struct vifctl
*vifc
, int mrtsock
)
692 int vifi
= vifc
->vifc_vifi
;
693 struct vif_device
*v
= &mrt
->vif_table
[vifi
];
694 struct net_device
*dev
;
695 struct in_device
*in_dev
;
699 if (VIF_EXISTS(mrt
, vifi
))
702 switch (vifc
->vifc_flags
) {
703 #ifdef CONFIG_IP_PIMSM
706 * Special Purpose VIF in PIM
707 * All the packets will be sent to the daemon
709 if (mrt
->mroute_reg_vif_num
>= 0)
711 dev
= ipmr_reg_vif(net
, mrt
);
714 err
= dev_set_allmulti(dev
, 1);
716 unregister_netdevice(dev
);
723 dev
= ipmr_new_tunnel(net
, vifc
);
726 err
= dev_set_allmulti(dev
, 1);
728 ipmr_del_tunnel(dev
, vifc
);
734 case VIFF_USE_IFINDEX
:
736 if (vifc
->vifc_flags
== VIFF_USE_IFINDEX
) {
737 dev
= dev_get_by_index(net
, vifc
->vifc_lcl_ifindex
);
738 if (dev
&& __in_dev_get_rtnl(dev
) == NULL
) {
740 return -EADDRNOTAVAIL
;
743 dev
= ip_dev_find(net
, vifc
->vifc_lcl_addr
.s_addr
);
746 return -EADDRNOTAVAIL
;
747 err
= dev_set_allmulti(dev
, 1);
757 in_dev
= __in_dev_get_rtnl(dev
);
760 return -EADDRNOTAVAIL
;
762 IPV4_DEVCONF(in_dev
->cnf
, MC_FORWARDING
)++;
763 ip_rt_multicast_event(in_dev
);
765 /* Fill in the VIF structures */
767 v
->rate_limit
= vifc
->vifc_rate_limit
;
768 v
->local
= vifc
->vifc_lcl_addr
.s_addr
;
769 v
->remote
= vifc
->vifc_rmt_addr
.s_addr
;
770 v
->flags
= vifc
->vifc_flags
;
772 v
->flags
|= VIFF_STATIC
;
773 v
->threshold
= vifc
->vifc_threshold
;
778 v
->link
= dev
->ifindex
;
779 if (v
->flags
& (VIFF_TUNNEL
| VIFF_REGISTER
))
780 v
->link
= dev
->iflink
;
782 /* And finish update writing critical data */
783 write_lock_bh(&mrt_lock
);
785 #ifdef CONFIG_IP_PIMSM
786 if (v
->flags
& VIFF_REGISTER
)
787 mrt
->mroute_reg_vif_num
= vifi
;
789 if (vifi
+1 > mrt
->maxvif
)
790 mrt
->maxvif
= vifi
+1;
791 write_unlock_bh(&mrt_lock
);
795 /* called with rcu_read_lock() */
796 static struct mfc_cache
*ipmr_cache_find(struct mr_table
*mrt
,
800 int line
= MFC_HASH(mcastgrp
, origin
);
803 list_for_each_entry_rcu(c
, &mrt
->mfc_cache_array
[line
], list
) {
804 if (c
->mfc_origin
== origin
&& c
->mfc_mcastgrp
== mcastgrp
)
811 * Allocate a multicast cache entry
813 static struct mfc_cache
*ipmr_cache_alloc(void)
815 struct mfc_cache
*c
= kmem_cache_zalloc(mrt_cachep
, GFP_KERNEL
);
818 c
->mfc_un
.res
.minvif
= MAXVIFS
;
822 static struct mfc_cache
*ipmr_cache_alloc_unres(void)
824 struct mfc_cache
*c
= kmem_cache_zalloc(mrt_cachep
, GFP_ATOMIC
);
827 skb_queue_head_init(&c
->mfc_un
.unres
.unresolved
);
828 c
->mfc_un
.unres
.expires
= jiffies
+ 10*HZ
;
834 * A cache entry has gone into a resolved state from queued
837 static void ipmr_cache_resolve(struct net
*net
, struct mr_table
*mrt
,
838 struct mfc_cache
*uc
, struct mfc_cache
*c
)
843 /* Play the pending entries through our router */
845 while ((skb
= __skb_dequeue(&uc
->mfc_un
.unres
.unresolved
))) {
846 if (ip_hdr(skb
)->version
== 0) {
847 struct nlmsghdr
*nlh
= (struct nlmsghdr
*)skb_pull(skb
, sizeof(struct iphdr
));
849 if (__ipmr_fill_mroute(mrt
, skb
, c
, NLMSG_DATA(nlh
)) > 0) {
850 nlh
->nlmsg_len
= skb_tail_pointer(skb
) -
853 nlh
->nlmsg_type
= NLMSG_ERROR
;
854 nlh
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct nlmsgerr
));
855 skb_trim(skb
, nlh
->nlmsg_len
);
857 e
->error
= -EMSGSIZE
;
858 memset(&e
->msg
, 0, sizeof(e
->msg
));
861 rtnl_unicast(skb
, net
, NETLINK_CB(skb
).pid
);
863 ip_mr_forward(net
, mrt
, skb
, c
, 0);
869 * Bounce a cache query up to mrouted. We could use netlink for this but mrouted
870 * expects the following bizarre scheme.
872 * Called under mrt_lock.
875 static int ipmr_cache_report(struct mr_table
*mrt
,
876 struct sk_buff
*pkt
, vifi_t vifi
, int assert)
879 const int ihl
= ip_hdrlen(pkt
);
880 struct igmphdr
*igmp
;
882 struct sock
*mroute_sk
;
885 #ifdef CONFIG_IP_PIMSM
886 if (assert == IGMPMSG_WHOLEPKT
)
887 skb
= skb_realloc_headroom(pkt
, sizeof(struct iphdr
));
890 skb
= alloc_skb(128, GFP_ATOMIC
);
895 #ifdef CONFIG_IP_PIMSM
896 if (assert == IGMPMSG_WHOLEPKT
) {
897 /* Ugly, but we have no choice with this interface.
898 * Duplicate old header, fix ihl, length etc.
899 * And all this only to mangle msg->im_msgtype and
900 * to set msg->im_mbz to "mbz" :-)
902 skb_push(skb
, sizeof(struct iphdr
));
903 skb_reset_network_header(skb
);
904 skb_reset_transport_header(skb
);
905 msg
= (struct igmpmsg
*)skb_network_header(skb
);
906 memcpy(msg
, skb_network_header(pkt
), sizeof(struct iphdr
));
907 msg
->im_msgtype
= IGMPMSG_WHOLEPKT
;
909 msg
->im_vif
= mrt
->mroute_reg_vif_num
;
910 ip_hdr(skb
)->ihl
= sizeof(struct iphdr
) >> 2;
911 ip_hdr(skb
)->tot_len
= htons(ntohs(ip_hdr(pkt
)->tot_len
) +
912 sizeof(struct iphdr
));
917 /* Copy the IP header */
919 skb
->network_header
= skb
->tail
;
921 skb_copy_to_linear_data(skb
, pkt
->data
, ihl
);
922 ip_hdr(skb
)->protocol
= 0; /* Flag to the kernel this is a route add */
923 msg
= (struct igmpmsg
*)skb_network_header(skb
);
925 skb_dst_set(skb
, dst_clone(skb_dst(pkt
)));
929 igmp
= (struct igmphdr
*)skb_put(skb
, sizeof(struct igmphdr
));
931 msg
->im_msgtype
= assert;
933 ip_hdr(skb
)->tot_len
= htons(skb
->len
); /* Fix the length */
934 skb
->transport_header
= skb
->network_header
;
938 mroute_sk
= rcu_dereference(mrt
->mroute_sk
);
939 if (mroute_sk
== NULL
) {
945 /* Deliver to mrouted */
947 ret
= sock_queue_rcv_skb(mroute_sk
, skb
);
951 printk(KERN_WARNING
"mroute: pending queue full, dropping entries.\n");
959 * Queue a packet for resolution. It gets locked cache entry!
963 ipmr_cache_unresolved(struct mr_table
*mrt
, vifi_t vifi
, struct sk_buff
*skb
)
968 const struct iphdr
*iph
= ip_hdr(skb
);
970 spin_lock_bh(&mfc_unres_lock
);
971 list_for_each_entry(c
, &mrt
->mfc_unres_queue
, list
) {
972 if (c
->mfc_mcastgrp
== iph
->daddr
&&
973 c
->mfc_origin
== iph
->saddr
) {
980 /* Create a new entry if allowable */
982 if (atomic_read(&mrt
->cache_resolve_queue_len
) >= 10 ||
983 (c
= ipmr_cache_alloc_unres()) == NULL
) {
984 spin_unlock_bh(&mfc_unres_lock
);
990 /* Fill in the new cache entry */
993 c
->mfc_origin
= iph
->saddr
;
994 c
->mfc_mcastgrp
= iph
->daddr
;
996 /* Reflect first query at mrouted. */
998 err
= ipmr_cache_report(mrt
, skb
, vifi
, IGMPMSG_NOCACHE
);
1000 /* If the report failed throw the cache entry
1003 spin_unlock_bh(&mfc_unres_lock
);
1010 atomic_inc(&mrt
->cache_resolve_queue_len
);
1011 list_add(&c
->list
, &mrt
->mfc_unres_queue
);
1013 if (atomic_read(&mrt
->cache_resolve_queue_len
) == 1)
1014 mod_timer(&mrt
->ipmr_expire_timer
, c
->mfc_un
.unres
.expires
);
1017 /* See if we can append the packet */
1019 if (c
->mfc_un
.unres
.unresolved
.qlen
> 3) {
1023 skb_queue_tail(&c
->mfc_un
.unres
.unresolved
, skb
);
1027 spin_unlock_bh(&mfc_unres_lock
);
1032 * MFC cache manipulation by user space mroute daemon
1035 static int ipmr_mfc_delete(struct mr_table
*mrt
, struct mfcctl
*mfc
)
1038 struct mfc_cache
*c
, *next
;
1040 line
= MFC_HASH(mfc
->mfcc_mcastgrp
.s_addr
, mfc
->mfcc_origin
.s_addr
);
1042 list_for_each_entry_safe(c
, next
, &mrt
->mfc_cache_array
[line
], list
) {
1043 if (c
->mfc_origin
== mfc
->mfcc_origin
.s_addr
&&
1044 c
->mfc_mcastgrp
== mfc
->mfcc_mcastgrp
.s_addr
) {
1045 list_del_rcu(&c
->list
);
1054 static int ipmr_mfc_add(struct net
*net
, struct mr_table
*mrt
,
1055 struct mfcctl
*mfc
, int mrtsock
)
1059 struct mfc_cache
*uc
, *c
;
1061 if (mfc
->mfcc_parent
>= MAXVIFS
)
1064 line
= MFC_HASH(mfc
->mfcc_mcastgrp
.s_addr
, mfc
->mfcc_origin
.s_addr
);
1066 list_for_each_entry(c
, &mrt
->mfc_cache_array
[line
], list
) {
1067 if (c
->mfc_origin
== mfc
->mfcc_origin
.s_addr
&&
1068 c
->mfc_mcastgrp
== mfc
->mfcc_mcastgrp
.s_addr
) {
1075 write_lock_bh(&mrt_lock
);
1076 c
->mfc_parent
= mfc
->mfcc_parent
;
1077 ipmr_update_thresholds(mrt
, c
, mfc
->mfcc_ttls
);
1079 c
->mfc_flags
|= MFC_STATIC
;
1080 write_unlock_bh(&mrt_lock
);
1084 if (!ipv4_is_multicast(mfc
->mfcc_mcastgrp
.s_addr
))
1087 c
= ipmr_cache_alloc();
1091 c
->mfc_origin
= mfc
->mfcc_origin
.s_addr
;
1092 c
->mfc_mcastgrp
= mfc
->mfcc_mcastgrp
.s_addr
;
1093 c
->mfc_parent
= mfc
->mfcc_parent
;
1094 ipmr_update_thresholds(mrt
, c
, mfc
->mfcc_ttls
);
1096 c
->mfc_flags
|= MFC_STATIC
;
1098 list_add_rcu(&c
->list
, &mrt
->mfc_cache_array
[line
]);
1101 * Check to see if we resolved a queued list. If so we
1102 * need to send on the frames and tidy up.
1105 spin_lock_bh(&mfc_unres_lock
);
1106 list_for_each_entry(uc
, &mrt
->mfc_unres_queue
, list
) {
1107 if (uc
->mfc_origin
== c
->mfc_origin
&&
1108 uc
->mfc_mcastgrp
== c
->mfc_mcastgrp
) {
1109 list_del(&uc
->list
);
1110 atomic_dec(&mrt
->cache_resolve_queue_len
);
1115 if (list_empty(&mrt
->mfc_unres_queue
))
1116 del_timer(&mrt
->ipmr_expire_timer
);
1117 spin_unlock_bh(&mfc_unres_lock
);
1120 ipmr_cache_resolve(net
, mrt
, uc
, c
);
1121 ipmr_cache_free(uc
);
1127 * Close the multicast socket, and clear the vif tables etc
1130 static void mroute_clean_tables(struct mr_table
*mrt
)
1134 struct mfc_cache
*c
, *next
;
1136 /* Shut down all active vif entries */
1138 for (i
= 0; i
< mrt
->maxvif
; i
++) {
1139 if (!(mrt
->vif_table
[i
].flags
& VIFF_STATIC
))
1140 vif_delete(mrt
, i
, 0, &list
);
1142 unregister_netdevice_many(&list
);
1144 /* Wipe the cache */
1146 for (i
= 0; i
< MFC_LINES
; i
++) {
1147 list_for_each_entry_safe(c
, next
, &mrt
->mfc_cache_array
[i
], list
) {
1148 if (c
->mfc_flags
& MFC_STATIC
)
1150 list_del_rcu(&c
->list
);
1155 if (atomic_read(&mrt
->cache_resolve_queue_len
) != 0) {
1156 spin_lock_bh(&mfc_unres_lock
);
1157 list_for_each_entry_safe(c
, next
, &mrt
->mfc_unres_queue
, list
) {
1159 ipmr_destroy_unres(mrt
, c
);
1161 spin_unlock_bh(&mfc_unres_lock
);
1165 /* called from ip_ra_control(), before an RCU grace period,
1166 * we dont need to call synchronize_rcu() here
1168 static void mrtsock_destruct(struct sock
*sk
)
1170 struct net
*net
= sock_net(sk
);
1171 struct mr_table
*mrt
;
1174 ipmr_for_each_table(mrt
, net
) {
1175 if (sk
== rtnl_dereference(mrt
->mroute_sk
)) {
1176 IPV4_DEVCONF_ALL(net
, MC_FORWARDING
)--;
1177 rcu_assign_pointer(mrt
->mroute_sk
, NULL
);
1178 mroute_clean_tables(mrt
);
1185 * Socket options and virtual interface manipulation. The whole
1186 * virtual interface system is a complete heap, but unfortunately
1187 * that's how BSD mrouted happens to think. Maybe one day with a proper
1188 * MOSPF/PIM router set up we can clean this up.
1191 int ip_mroute_setsockopt(struct sock
*sk
, int optname
, char __user
*optval
, unsigned int optlen
)
1196 struct net
*net
= sock_net(sk
);
1197 struct mr_table
*mrt
;
1199 mrt
= ipmr_get_table(net
, raw_sk(sk
)->ipmr_table
? : RT_TABLE_DEFAULT
);
1203 if (optname
!= MRT_INIT
) {
1204 if (sk
!= rcu_dereference_raw(mrt
->mroute_sk
) &&
1205 !capable(CAP_NET_ADMIN
))
1211 if (sk
->sk_type
!= SOCK_RAW
||
1212 inet_sk(sk
)->inet_num
!= IPPROTO_IGMP
)
1214 if (optlen
!= sizeof(int))
1215 return -ENOPROTOOPT
;
1218 if (rtnl_dereference(mrt
->mroute_sk
)) {
1223 ret
= ip_ra_control(sk
, 1, mrtsock_destruct
);
1225 rcu_assign_pointer(mrt
->mroute_sk
, sk
);
1226 IPV4_DEVCONF_ALL(net
, MC_FORWARDING
)++;
1231 if (sk
!= rcu_dereference_raw(mrt
->mroute_sk
))
1233 return ip_ra_control(sk
, 0, NULL
);
1236 if (optlen
!= sizeof(vif
))
1238 if (copy_from_user(&vif
, optval
, sizeof(vif
)))
1240 if (vif
.vifc_vifi
>= MAXVIFS
)
1243 if (optname
== MRT_ADD_VIF
) {
1244 ret
= vif_add(net
, mrt
, &vif
,
1245 sk
== rtnl_dereference(mrt
->mroute_sk
));
1247 ret
= vif_delete(mrt
, vif
.vifc_vifi
, 0, NULL
);
1253 * Manipulate the forwarding caches. These live
1254 * in a sort of kernel/user symbiosis.
1258 if (optlen
!= sizeof(mfc
))
1260 if (copy_from_user(&mfc
, optval
, sizeof(mfc
)))
1263 if (optname
== MRT_DEL_MFC
)
1264 ret
= ipmr_mfc_delete(mrt
, &mfc
);
1266 ret
= ipmr_mfc_add(net
, mrt
, &mfc
,
1267 sk
== rtnl_dereference(mrt
->mroute_sk
));
1271 * Control PIM assert.
1276 if (get_user(v
, (int __user
*)optval
))
1278 mrt
->mroute_do_assert
= (v
) ? 1 : 0;
1281 #ifdef CONFIG_IP_PIMSM
1286 if (get_user(v
, (int __user
*)optval
))
1292 if (v
!= mrt
->mroute_do_pim
) {
1293 mrt
->mroute_do_pim
= v
;
1294 mrt
->mroute_do_assert
= v
;
1300 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
1305 if (optlen
!= sizeof(u32
))
1307 if (get_user(v
, (u32 __user
*)optval
))
1312 if (sk
== rtnl_dereference(mrt
->mroute_sk
)) {
1315 if (!ipmr_new_table(net
, v
))
1317 raw_sk(sk
)->ipmr_table
= v
;
1324 * Spurious command, or MRT_VERSION which you cannot
1328 return -ENOPROTOOPT
;
1333 * Getsock opt support for the multicast routing system.
1336 int ip_mroute_getsockopt(struct sock
*sk
, int optname
, char __user
*optval
, int __user
*optlen
)
1340 struct net
*net
= sock_net(sk
);
1341 struct mr_table
*mrt
;
1343 mrt
= ipmr_get_table(net
, raw_sk(sk
)->ipmr_table
? : RT_TABLE_DEFAULT
);
1347 if (optname
!= MRT_VERSION
&&
1348 #ifdef CONFIG_IP_PIMSM
1349 optname
!= MRT_PIM
&&
1351 optname
!= MRT_ASSERT
)
1352 return -ENOPROTOOPT
;
1354 if (get_user(olr
, optlen
))
1357 olr
= min_t(unsigned int, olr
, sizeof(int));
1361 if (put_user(olr
, optlen
))
1363 if (optname
== MRT_VERSION
)
1365 #ifdef CONFIG_IP_PIMSM
1366 else if (optname
== MRT_PIM
)
1367 val
= mrt
->mroute_do_pim
;
1370 val
= mrt
->mroute_do_assert
;
1371 if (copy_to_user(optval
, &val
, olr
))
1377 * The IP multicast ioctl support routines.
1380 int ipmr_ioctl(struct sock
*sk
, int cmd
, void __user
*arg
)
1382 struct sioc_sg_req sr
;
1383 struct sioc_vif_req vr
;
1384 struct vif_device
*vif
;
1385 struct mfc_cache
*c
;
1386 struct net
*net
= sock_net(sk
);
1387 struct mr_table
*mrt
;
1389 mrt
= ipmr_get_table(net
, raw_sk(sk
)->ipmr_table
? : RT_TABLE_DEFAULT
);
1395 if (copy_from_user(&vr
, arg
, sizeof(vr
)))
1397 if (vr
.vifi
>= mrt
->maxvif
)
1399 read_lock(&mrt_lock
);
1400 vif
= &mrt
->vif_table
[vr
.vifi
];
1401 if (VIF_EXISTS(mrt
, vr
.vifi
)) {
1402 vr
.icount
= vif
->pkt_in
;
1403 vr
.ocount
= vif
->pkt_out
;
1404 vr
.ibytes
= vif
->bytes_in
;
1405 vr
.obytes
= vif
->bytes_out
;
1406 read_unlock(&mrt_lock
);
1408 if (copy_to_user(arg
, &vr
, sizeof(vr
)))
1412 read_unlock(&mrt_lock
);
1413 return -EADDRNOTAVAIL
;
1415 if (copy_from_user(&sr
, arg
, sizeof(sr
)))
1419 c
= ipmr_cache_find(mrt
, sr
.src
.s_addr
, sr
.grp
.s_addr
);
1421 sr
.pktcnt
= c
->mfc_un
.res
.pkt
;
1422 sr
.bytecnt
= c
->mfc_un
.res
.bytes
;
1423 sr
.wrong_if
= c
->mfc_un
.res
.wrong_if
;
1426 if (copy_to_user(arg
, &sr
, sizeof(sr
)))
1431 return -EADDRNOTAVAIL
;
1433 return -ENOIOCTLCMD
;
1438 static int ipmr_device_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
1440 struct net_device
*dev
= ptr
;
1441 struct net
*net
= dev_net(dev
);
1442 struct mr_table
*mrt
;
1443 struct vif_device
*v
;
1447 if (event
!= NETDEV_UNREGISTER
)
1450 ipmr_for_each_table(mrt
, net
) {
1451 v
= &mrt
->vif_table
[0];
1452 for (ct
= 0; ct
< mrt
->maxvif
; ct
++, v
++) {
1454 vif_delete(mrt
, ct
, 1, &list
);
1457 unregister_netdevice_many(&list
);
1462 static struct notifier_block ip_mr_notifier
= {
1463 .notifier_call
= ipmr_device_event
,
1467 * Encapsulate a packet by attaching a valid IPIP header to it.
1468 * This avoids tunnel drivers and other mess and gives us the speed so
1469 * important for multicast video.
1472 static void ip_encap(struct sk_buff
*skb
, __be32 saddr
, __be32 daddr
)
1475 struct iphdr
*old_iph
= ip_hdr(skb
);
1477 skb_push(skb
, sizeof(struct iphdr
));
1478 skb
->transport_header
= skb
->network_header
;
1479 skb_reset_network_header(skb
);
1483 iph
->tos
= old_iph
->tos
;
1484 iph
->ttl
= old_iph
->ttl
;
1488 iph
->protocol
= IPPROTO_IPIP
;
1490 iph
->tot_len
= htons(skb
->len
);
1491 ip_select_ident(iph
, skb_dst(skb
), NULL
);
1494 memset(&(IPCB(skb
)->opt
), 0, sizeof(IPCB(skb
)->opt
));
1498 static inline int ipmr_forward_finish(struct sk_buff
*skb
)
1500 struct ip_options
*opt
= &(IPCB(skb
)->opt
);
1502 IP_INC_STATS_BH(dev_net(skb_dst(skb
)->dev
), IPSTATS_MIB_OUTFORWDATAGRAMS
);
1504 if (unlikely(opt
->optlen
))
1505 ip_forward_options(skb
);
1507 return dst_output(skb
);
1511 * Processing handlers for ipmr_forward
1514 static void ipmr_queue_xmit(struct net
*net
, struct mr_table
*mrt
,
1515 struct sk_buff
*skb
, struct mfc_cache
*c
, int vifi
)
1517 const struct iphdr
*iph
= ip_hdr(skb
);
1518 struct vif_device
*vif
= &mrt
->vif_table
[vifi
];
1519 struct net_device
*dev
;
1523 if (vif
->dev
== NULL
)
1526 #ifdef CONFIG_IP_PIMSM
1527 if (vif
->flags
& VIFF_REGISTER
) {
1529 vif
->bytes_out
+= skb
->len
;
1530 vif
->dev
->stats
.tx_bytes
+= skb
->len
;
1531 vif
->dev
->stats
.tx_packets
++;
1532 ipmr_cache_report(mrt
, skb
, vifi
, IGMPMSG_WHOLEPKT
);
1537 if (vif
->flags
& VIFF_TUNNEL
) {
1542 .daddr
= vif
->remote
,
1543 .saddr
= vif
->local
,
1544 .tos
= RT_TOS(iph
->tos
)
1547 .proto
= IPPROTO_IPIP
1550 if (ip_route_output_key(net
, &rt
, &fl
))
1552 encap
= sizeof(struct iphdr
);
1558 .daddr
= iph
->daddr
,
1559 .tos
= RT_TOS(iph
->tos
)
1562 .proto
= IPPROTO_IPIP
1565 if (ip_route_output_key(net
, &rt
, &fl
))
1571 if (skb
->len
+encap
> dst_mtu(&rt
->dst
) && (ntohs(iph
->frag_off
) & IP_DF
)) {
1572 /* Do not fragment multicasts. Alas, IPv4 does not
1573 * allow to send ICMP, so that packets will disappear
1577 IP_INC_STATS_BH(dev_net(dev
), IPSTATS_MIB_FRAGFAILS
);
1582 encap
+= LL_RESERVED_SPACE(dev
) + rt
->dst
.header_len
;
1584 if (skb_cow(skb
, encap
)) {
1590 vif
->bytes_out
+= skb
->len
;
1593 skb_dst_set(skb
, &rt
->dst
);
1594 ip_decrease_ttl(ip_hdr(skb
));
1596 /* FIXME: forward and output firewalls used to be called here.
1597 * What do we do with netfilter? -- RR
1599 if (vif
->flags
& VIFF_TUNNEL
) {
1600 ip_encap(skb
, vif
->local
, vif
->remote
);
1601 /* FIXME: extra output firewall step used to be here. --RR */
1602 vif
->dev
->stats
.tx_packets
++;
1603 vif
->dev
->stats
.tx_bytes
+= skb
->len
;
1606 IPCB(skb
)->flags
|= IPSKB_FORWARDED
;
1609 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1610 * not only before forwarding, but after forwarding on all output
1611 * interfaces. It is clear, if mrouter runs a multicasting
1612 * program, it should receive packets not depending to what interface
1613 * program is joined.
1614 * If we will not make it, the program will have to join on all
1615 * interfaces. On the other hand, multihoming host (or router, but
1616 * not mrouter) cannot join to more than one interface - it will
1617 * result in receiving multiple packets.
1619 NF_HOOK(NFPROTO_IPV4
, NF_INET_FORWARD
, skb
, skb
->dev
, dev
,
1620 ipmr_forward_finish
);
1627 static int ipmr_find_vif(struct mr_table
*mrt
, struct net_device
*dev
)
1631 for (ct
= mrt
->maxvif
-1; ct
>= 0; ct
--) {
1632 if (mrt
->vif_table
[ct
].dev
== dev
)
1638 /* "local" means that we should preserve one skb (for local delivery) */
1640 static int ip_mr_forward(struct net
*net
, struct mr_table
*mrt
,
1641 struct sk_buff
*skb
, struct mfc_cache
*cache
,
1647 vif
= cache
->mfc_parent
;
1648 cache
->mfc_un
.res
.pkt
++;
1649 cache
->mfc_un
.res
.bytes
+= skb
->len
;
1652 * Wrong interface: drop packet and (maybe) send PIM assert.
1654 if (mrt
->vif_table
[vif
].dev
!= skb
->dev
) {
1657 if (skb_rtable(skb
)->fl
.iif
== 0) {
1658 /* It is our own packet, looped back.
1659 * Very complicated situation...
1661 * The best workaround until routing daemons will be
1662 * fixed is not to redistribute packet, if it was
1663 * send through wrong interface. It means, that
1664 * multicast applications WILL NOT work for
1665 * (S,G), which have default multicast route pointing
1666 * to wrong oif. In any case, it is not a good
1667 * idea to use multicasting applications on router.
1672 cache
->mfc_un
.res
.wrong_if
++;
1673 true_vifi
= ipmr_find_vif(mrt
, skb
->dev
);
1675 if (true_vifi
>= 0 && mrt
->mroute_do_assert
&&
1676 /* pimsm uses asserts, when switching from RPT to SPT,
1677 * so that we cannot check that packet arrived on an oif.
1678 * It is bad, but otherwise we would need to move pretty
1679 * large chunk of pimd to kernel. Ough... --ANK
1681 (mrt
->mroute_do_pim
||
1682 cache
->mfc_un
.res
.ttls
[true_vifi
] < 255) &&
1684 cache
->mfc_un
.res
.last_assert
+ MFC_ASSERT_THRESH
)) {
1685 cache
->mfc_un
.res
.last_assert
= jiffies
;
1686 ipmr_cache_report(mrt
, skb
, true_vifi
, IGMPMSG_WRONGVIF
);
1691 mrt
->vif_table
[vif
].pkt_in
++;
1692 mrt
->vif_table
[vif
].bytes_in
+= skb
->len
;
1697 for (ct
= cache
->mfc_un
.res
.maxvif
- 1;
1698 ct
>= cache
->mfc_un
.res
.minvif
; ct
--) {
1699 if (ip_hdr(skb
)->ttl
> cache
->mfc_un
.res
.ttls
[ct
]) {
1701 struct sk_buff
*skb2
= skb_clone(skb
, GFP_ATOMIC
);
1704 ipmr_queue_xmit(net
, mrt
, skb2
, cache
,
1712 struct sk_buff
*skb2
= skb_clone(skb
, GFP_ATOMIC
);
1715 ipmr_queue_xmit(net
, mrt
, skb2
, cache
, psend
);
1717 ipmr_queue_xmit(net
, mrt
, skb
, cache
, psend
);
1730 * Multicast packets for forwarding arrive here
1731 * Called with rcu_read_lock();
1734 int ip_mr_input(struct sk_buff
*skb
)
1736 struct mfc_cache
*cache
;
1737 struct net
*net
= dev_net(skb
->dev
);
1738 int local
= skb_rtable(skb
)->rt_flags
& RTCF_LOCAL
;
1739 struct mr_table
*mrt
;
1742 /* Packet is looped back after forward, it should not be
1743 * forwarded second time, but still can be delivered locally.
1745 if (IPCB(skb
)->flags
& IPSKB_FORWARDED
)
1748 err
= ipmr_fib_lookup(net
, &skb_rtable(skb
)->fl
, &mrt
);
1755 if (IPCB(skb
)->opt
.router_alert
) {
1756 if (ip_call_ra_chain(skb
))
1758 } else if (ip_hdr(skb
)->protocol
== IPPROTO_IGMP
) {
1759 /* IGMPv1 (and broken IGMPv2 implementations sort of
1760 * Cisco IOS <= 11.2(8)) do not put router alert
1761 * option to IGMP packets destined to routable
1762 * groups. It is very bad, because it means
1763 * that we can forward NO IGMP messages.
1765 struct sock
*mroute_sk
;
1767 mroute_sk
= rcu_dereference(mrt
->mroute_sk
);
1770 raw_rcv(mroute_sk
, skb
);
1776 /* already under rcu_read_lock() */
1777 cache
= ipmr_cache_find(mrt
, ip_hdr(skb
)->saddr
, ip_hdr(skb
)->daddr
);
1780 * No usable cache entry
1782 if (cache
== NULL
) {
1786 struct sk_buff
*skb2
= skb_clone(skb
, GFP_ATOMIC
);
1787 ip_local_deliver(skb
);
1793 read_lock(&mrt_lock
);
1794 vif
= ipmr_find_vif(mrt
, skb
->dev
);
1796 int err2
= ipmr_cache_unresolved(mrt
, vif
, skb
);
1797 read_unlock(&mrt_lock
);
1801 read_unlock(&mrt_lock
);
1806 read_lock(&mrt_lock
);
1807 ip_mr_forward(net
, mrt
, skb
, cache
, local
);
1808 read_unlock(&mrt_lock
);
1811 return ip_local_deliver(skb
);
1817 return ip_local_deliver(skb
);
1822 #ifdef CONFIG_IP_PIMSM
1823 /* called with rcu_read_lock() */
1824 static int __pim_rcv(struct mr_table
*mrt
, struct sk_buff
*skb
,
1825 unsigned int pimlen
)
1827 struct net_device
*reg_dev
= NULL
;
1828 struct iphdr
*encap
;
1830 encap
= (struct iphdr
*)(skb_transport_header(skb
) + pimlen
);
1833 * a. packet is really sent to a multicast group
1834 * b. packet is not a NULL-REGISTER
1835 * c. packet is not truncated
1837 if (!ipv4_is_multicast(encap
->daddr
) ||
1838 encap
->tot_len
== 0 ||
1839 ntohs(encap
->tot_len
) + pimlen
> skb
->len
)
1842 read_lock(&mrt_lock
);
1843 if (mrt
->mroute_reg_vif_num
>= 0)
1844 reg_dev
= mrt
->vif_table
[mrt
->mroute_reg_vif_num
].dev
;
1845 read_unlock(&mrt_lock
);
1847 if (reg_dev
== NULL
)
1850 skb
->mac_header
= skb
->network_header
;
1851 skb_pull(skb
, (u8
*)encap
- skb
->data
);
1852 skb_reset_network_header(skb
);
1853 skb
->protocol
= htons(ETH_P_IP
);
1854 skb
->ip_summed
= CHECKSUM_NONE
;
1855 skb
->pkt_type
= PACKET_HOST
;
1857 skb_tunnel_rx(skb
, reg_dev
);
1861 return NET_RX_SUCCESS
;
1865 #ifdef CONFIG_IP_PIMSM_V1
1867 * Handle IGMP messages of PIMv1
1870 int pim_rcv_v1(struct sk_buff
*skb
)
1872 struct igmphdr
*pim
;
1873 struct net
*net
= dev_net(skb
->dev
);
1874 struct mr_table
*mrt
;
1876 if (!pskb_may_pull(skb
, sizeof(*pim
) + sizeof(struct iphdr
)))
1879 pim
= igmp_hdr(skb
);
1881 if (ipmr_fib_lookup(net
, &skb_rtable(skb
)->fl
, &mrt
) < 0)
1884 if (!mrt
->mroute_do_pim
||
1885 pim
->group
!= PIM_V1_VERSION
|| pim
->code
!= PIM_V1_REGISTER
)
1888 if (__pim_rcv(mrt
, skb
, sizeof(*pim
))) {
1896 #ifdef CONFIG_IP_PIMSM_V2
1897 static int pim_rcv(struct sk_buff
*skb
)
1899 struct pimreghdr
*pim
;
1900 struct net
*net
= dev_net(skb
->dev
);
1901 struct mr_table
*mrt
;
1903 if (!pskb_may_pull(skb
, sizeof(*pim
) + sizeof(struct iphdr
)))
1906 pim
= (struct pimreghdr
*)skb_transport_header(skb
);
1907 if (pim
->type
!= ((PIM_VERSION
<< 4) | (PIM_REGISTER
)) ||
1908 (pim
->flags
& PIM_NULL_REGISTER
) ||
1909 (ip_compute_csum((void *)pim
, sizeof(*pim
)) != 0 &&
1910 csum_fold(skb_checksum(skb
, 0, skb
->len
, 0))))
1913 if (ipmr_fib_lookup(net
, &skb_rtable(skb
)->fl
, &mrt
) < 0)
1916 if (__pim_rcv(mrt
, skb
, sizeof(*pim
))) {
1924 static int __ipmr_fill_mroute(struct mr_table
*mrt
, struct sk_buff
*skb
,
1925 struct mfc_cache
*c
, struct rtmsg
*rtm
)
1928 struct rtnexthop
*nhp
;
1929 u8
*b
= skb_tail_pointer(skb
);
1930 struct rtattr
*mp_head
;
1932 /* If cache is unresolved, don't try to parse IIF and OIF */
1933 if (c
->mfc_parent
>= MAXVIFS
)
1936 if (VIF_EXISTS(mrt
, c
->mfc_parent
))
1937 RTA_PUT(skb
, RTA_IIF
, 4, &mrt
->vif_table
[c
->mfc_parent
].dev
->ifindex
);
1939 mp_head
= (struct rtattr
*)skb_put(skb
, RTA_LENGTH(0));
1941 for (ct
= c
->mfc_un
.res
.minvif
; ct
< c
->mfc_un
.res
.maxvif
; ct
++) {
1942 if (VIF_EXISTS(mrt
, ct
) && c
->mfc_un
.res
.ttls
[ct
] < 255) {
1943 if (skb_tailroom(skb
) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp
)) + 4))
1944 goto rtattr_failure
;
1945 nhp
= (struct rtnexthop
*)skb_put(skb
, RTA_ALIGN(sizeof(*nhp
)));
1946 nhp
->rtnh_flags
= 0;
1947 nhp
->rtnh_hops
= c
->mfc_un
.res
.ttls
[ct
];
1948 nhp
->rtnh_ifindex
= mrt
->vif_table
[ct
].dev
->ifindex
;
1949 nhp
->rtnh_len
= sizeof(*nhp
);
1952 mp_head
->rta_type
= RTA_MULTIPATH
;
1953 mp_head
->rta_len
= skb_tail_pointer(skb
) - (u8
*)mp_head
;
1954 rtm
->rtm_type
= RTN_MULTICAST
;
1962 int ipmr_get_route(struct net
*net
,
1963 struct sk_buff
*skb
, struct rtmsg
*rtm
, int nowait
)
1966 struct mr_table
*mrt
;
1967 struct mfc_cache
*cache
;
1968 struct rtable
*rt
= skb_rtable(skb
);
1970 mrt
= ipmr_get_table(net
, RT_TABLE_DEFAULT
);
1975 cache
= ipmr_cache_find(mrt
, rt
->rt_src
, rt
->rt_dst
);
1977 if (cache
== NULL
) {
1978 struct sk_buff
*skb2
;
1980 struct net_device
*dev
;
1989 read_lock(&mrt_lock
);
1991 vif
= ipmr_find_vif(mrt
, dev
);
1993 read_unlock(&mrt_lock
);
1997 skb2
= skb_clone(skb
, GFP_ATOMIC
);
1999 read_unlock(&mrt_lock
);
2004 skb_push(skb2
, sizeof(struct iphdr
));
2005 skb_reset_network_header(skb2
);
2007 iph
->ihl
= sizeof(struct iphdr
) >> 2;
2008 iph
->saddr
= rt
->rt_src
;
2009 iph
->daddr
= rt
->rt_dst
;
2011 err
= ipmr_cache_unresolved(mrt
, vif
, skb2
);
2012 read_unlock(&mrt_lock
);
2017 read_lock(&mrt_lock
);
2018 if (!nowait
&& (rtm
->rtm_flags
& RTM_F_NOTIFY
))
2019 cache
->mfc_flags
|= MFC_NOTIFY
;
2020 err
= __ipmr_fill_mroute(mrt
, skb
, cache
, rtm
);
2021 read_unlock(&mrt_lock
);
2026 static int ipmr_fill_mroute(struct mr_table
*mrt
, struct sk_buff
*skb
,
2027 u32 pid
, u32 seq
, struct mfc_cache
*c
)
2029 struct nlmsghdr
*nlh
;
2032 nlh
= nlmsg_put(skb
, pid
, seq
, RTM_NEWROUTE
, sizeof(*rtm
), NLM_F_MULTI
);
2036 rtm
= nlmsg_data(nlh
);
2037 rtm
->rtm_family
= RTNL_FAMILY_IPMR
;
2038 rtm
->rtm_dst_len
= 32;
2039 rtm
->rtm_src_len
= 32;
2041 rtm
->rtm_table
= mrt
->id
;
2042 NLA_PUT_U32(skb
, RTA_TABLE
, mrt
->id
);
2043 rtm
->rtm_type
= RTN_MULTICAST
;
2044 rtm
->rtm_scope
= RT_SCOPE_UNIVERSE
;
2045 rtm
->rtm_protocol
= RTPROT_UNSPEC
;
2048 NLA_PUT_BE32(skb
, RTA_SRC
, c
->mfc_origin
);
2049 NLA_PUT_BE32(skb
, RTA_DST
, c
->mfc_mcastgrp
);
2051 if (__ipmr_fill_mroute(mrt
, skb
, c
, rtm
) < 0)
2052 goto nla_put_failure
;
2054 return nlmsg_end(skb
, nlh
);
2057 nlmsg_cancel(skb
, nlh
);
2061 static int ipmr_rtm_dumproute(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2063 struct net
*net
= sock_net(skb
->sk
);
2064 struct mr_table
*mrt
;
2065 struct mfc_cache
*mfc
;
2066 unsigned int t
= 0, s_t
;
2067 unsigned int h
= 0, s_h
;
2068 unsigned int e
= 0, s_e
;
2075 ipmr_for_each_table(mrt
, net
) {
2080 for (h
= s_h
; h
< MFC_LINES
; h
++) {
2081 list_for_each_entry_rcu(mfc
, &mrt
->mfc_cache_array
[h
], list
) {
2084 if (ipmr_fill_mroute(mrt
, skb
,
2085 NETLINK_CB(cb
->skb
).pid
,
2108 #ifdef CONFIG_PROC_FS
2110 * The /proc interfaces to multicast routing :
2111 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
2113 struct ipmr_vif_iter
{
2114 struct seq_net_private p
;
2115 struct mr_table
*mrt
;
2119 static struct vif_device
*ipmr_vif_seq_idx(struct net
*net
,
2120 struct ipmr_vif_iter
*iter
,
2123 struct mr_table
*mrt
= iter
->mrt
;
2125 for (iter
->ct
= 0; iter
->ct
< mrt
->maxvif
; ++iter
->ct
) {
2126 if (!VIF_EXISTS(mrt
, iter
->ct
))
2129 return &mrt
->vif_table
[iter
->ct
];
2134 static void *ipmr_vif_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2135 __acquires(mrt_lock
)
2137 struct ipmr_vif_iter
*iter
= seq
->private;
2138 struct net
*net
= seq_file_net(seq
);
2139 struct mr_table
*mrt
;
2141 mrt
= ipmr_get_table(net
, RT_TABLE_DEFAULT
);
2143 return ERR_PTR(-ENOENT
);
2147 read_lock(&mrt_lock
);
2148 return *pos
? ipmr_vif_seq_idx(net
, seq
->private, *pos
- 1)
2152 static void *ipmr_vif_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2154 struct ipmr_vif_iter
*iter
= seq
->private;
2155 struct net
*net
= seq_file_net(seq
);
2156 struct mr_table
*mrt
= iter
->mrt
;
2159 if (v
== SEQ_START_TOKEN
)
2160 return ipmr_vif_seq_idx(net
, iter
, 0);
2162 while (++iter
->ct
< mrt
->maxvif
) {
2163 if (!VIF_EXISTS(mrt
, iter
->ct
))
2165 return &mrt
->vif_table
[iter
->ct
];
2170 static void ipmr_vif_seq_stop(struct seq_file
*seq
, void *v
)
2171 __releases(mrt_lock
)
2173 read_unlock(&mrt_lock
);
2176 static int ipmr_vif_seq_show(struct seq_file
*seq
, void *v
)
2178 struct ipmr_vif_iter
*iter
= seq
->private;
2179 struct mr_table
*mrt
= iter
->mrt
;
2181 if (v
== SEQ_START_TOKEN
) {
2183 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2185 const struct vif_device
*vif
= v
;
2186 const char *name
= vif
->dev
? vif
->dev
->name
: "none";
2189 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
2190 vif
- mrt
->vif_table
,
2191 name
, vif
->bytes_in
, vif
->pkt_in
,
2192 vif
->bytes_out
, vif
->pkt_out
,
2193 vif
->flags
, vif
->local
, vif
->remote
);
2198 static const struct seq_operations ipmr_vif_seq_ops
= {
2199 .start
= ipmr_vif_seq_start
,
2200 .next
= ipmr_vif_seq_next
,
2201 .stop
= ipmr_vif_seq_stop
,
2202 .show
= ipmr_vif_seq_show
,
2205 static int ipmr_vif_open(struct inode
*inode
, struct file
*file
)
2207 return seq_open_net(inode
, file
, &ipmr_vif_seq_ops
,
2208 sizeof(struct ipmr_vif_iter
));
2211 static const struct file_operations ipmr_vif_fops
= {
2212 .owner
= THIS_MODULE
,
2213 .open
= ipmr_vif_open
,
2215 .llseek
= seq_lseek
,
2216 .release
= seq_release_net
,
2219 struct ipmr_mfc_iter
{
2220 struct seq_net_private p
;
2221 struct mr_table
*mrt
;
2222 struct list_head
*cache
;
2227 static struct mfc_cache
*ipmr_mfc_seq_idx(struct net
*net
,
2228 struct ipmr_mfc_iter
*it
, loff_t pos
)
2230 struct mr_table
*mrt
= it
->mrt
;
2231 struct mfc_cache
*mfc
;
2234 for (it
->ct
= 0; it
->ct
< MFC_LINES
; it
->ct
++) {
2235 it
->cache
= &mrt
->mfc_cache_array
[it
->ct
];
2236 list_for_each_entry_rcu(mfc
, it
->cache
, list
)
2242 spin_lock_bh(&mfc_unres_lock
);
2243 it
->cache
= &mrt
->mfc_unres_queue
;
2244 list_for_each_entry(mfc
, it
->cache
, list
)
2247 spin_unlock_bh(&mfc_unres_lock
);
2254 static void *ipmr_mfc_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2256 struct ipmr_mfc_iter
*it
= seq
->private;
2257 struct net
*net
= seq_file_net(seq
);
2258 struct mr_table
*mrt
;
2260 mrt
= ipmr_get_table(net
, RT_TABLE_DEFAULT
);
2262 return ERR_PTR(-ENOENT
);
2267 return *pos
? ipmr_mfc_seq_idx(net
, seq
->private, *pos
- 1)
2271 static void *ipmr_mfc_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2273 struct mfc_cache
*mfc
= v
;
2274 struct ipmr_mfc_iter
*it
= seq
->private;
2275 struct net
*net
= seq_file_net(seq
);
2276 struct mr_table
*mrt
= it
->mrt
;
2280 if (v
== SEQ_START_TOKEN
)
2281 return ipmr_mfc_seq_idx(net
, seq
->private, 0);
2283 if (mfc
->list
.next
!= it
->cache
)
2284 return list_entry(mfc
->list
.next
, struct mfc_cache
, list
);
2286 if (it
->cache
== &mrt
->mfc_unres_queue
)
2289 BUG_ON(it
->cache
!= &mrt
->mfc_cache_array
[it
->ct
]);
2291 while (++it
->ct
< MFC_LINES
) {
2292 it
->cache
= &mrt
->mfc_cache_array
[it
->ct
];
2293 if (list_empty(it
->cache
))
2295 return list_first_entry(it
->cache
, struct mfc_cache
, list
);
2298 /* exhausted cache_array, show unresolved */
2300 it
->cache
= &mrt
->mfc_unres_queue
;
2303 spin_lock_bh(&mfc_unres_lock
);
2304 if (!list_empty(it
->cache
))
2305 return list_first_entry(it
->cache
, struct mfc_cache
, list
);
2308 spin_unlock_bh(&mfc_unres_lock
);
2314 static void ipmr_mfc_seq_stop(struct seq_file
*seq
, void *v
)
2316 struct ipmr_mfc_iter
*it
= seq
->private;
2317 struct mr_table
*mrt
= it
->mrt
;
2319 if (it
->cache
== &mrt
->mfc_unres_queue
)
2320 spin_unlock_bh(&mfc_unres_lock
);
2321 else if (it
->cache
== &mrt
->mfc_cache_array
[it
->ct
])
2325 static int ipmr_mfc_seq_show(struct seq_file
*seq
, void *v
)
2329 if (v
== SEQ_START_TOKEN
) {
2331 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2333 const struct mfc_cache
*mfc
= v
;
2334 const struct ipmr_mfc_iter
*it
= seq
->private;
2335 const struct mr_table
*mrt
= it
->mrt
;
2337 seq_printf(seq
, "%08X %08X %-3hd",
2338 (__force u32
) mfc
->mfc_mcastgrp
,
2339 (__force u32
) mfc
->mfc_origin
,
2342 if (it
->cache
!= &mrt
->mfc_unres_queue
) {
2343 seq_printf(seq
, " %8lu %8lu %8lu",
2344 mfc
->mfc_un
.res
.pkt
,
2345 mfc
->mfc_un
.res
.bytes
,
2346 mfc
->mfc_un
.res
.wrong_if
);
2347 for (n
= mfc
->mfc_un
.res
.minvif
;
2348 n
< mfc
->mfc_un
.res
.maxvif
; n
++) {
2349 if (VIF_EXISTS(mrt
, n
) &&
2350 mfc
->mfc_un
.res
.ttls
[n
] < 255)
2353 n
, mfc
->mfc_un
.res
.ttls
[n
]);
2356 /* unresolved mfc_caches don't contain
2357 * pkt, bytes and wrong_if values
2359 seq_printf(seq
, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
2361 seq_putc(seq
, '\n');
2366 static const struct seq_operations ipmr_mfc_seq_ops
= {
2367 .start
= ipmr_mfc_seq_start
,
2368 .next
= ipmr_mfc_seq_next
,
2369 .stop
= ipmr_mfc_seq_stop
,
2370 .show
= ipmr_mfc_seq_show
,
2373 static int ipmr_mfc_open(struct inode
*inode
, struct file
*file
)
2375 return seq_open_net(inode
, file
, &ipmr_mfc_seq_ops
,
2376 sizeof(struct ipmr_mfc_iter
));
2379 static const struct file_operations ipmr_mfc_fops
= {
2380 .owner
= THIS_MODULE
,
2381 .open
= ipmr_mfc_open
,
2383 .llseek
= seq_lseek
,
2384 .release
= seq_release_net
,
2388 #ifdef CONFIG_IP_PIMSM_V2
2389 static const struct net_protocol pim_protocol
= {
2397 * Setup for IP multicast routing
2399 static int __net_init
ipmr_net_init(struct net
*net
)
2403 err
= ipmr_rules_init(net
);
2407 #ifdef CONFIG_PROC_FS
2409 if (!proc_net_fops_create(net
, "ip_mr_vif", 0, &ipmr_vif_fops
))
2411 if (!proc_net_fops_create(net
, "ip_mr_cache", 0, &ipmr_mfc_fops
))
2412 goto proc_cache_fail
;
2416 #ifdef CONFIG_PROC_FS
2418 proc_net_remove(net
, "ip_mr_vif");
2420 ipmr_rules_exit(net
);
2426 static void __net_exit
ipmr_net_exit(struct net
*net
)
2428 #ifdef CONFIG_PROC_FS
2429 proc_net_remove(net
, "ip_mr_cache");
2430 proc_net_remove(net
, "ip_mr_vif");
2432 ipmr_rules_exit(net
);
2435 static struct pernet_operations ipmr_net_ops
= {
2436 .init
= ipmr_net_init
,
2437 .exit
= ipmr_net_exit
,
2440 int __init
ip_mr_init(void)
2444 mrt_cachep
= kmem_cache_create("ip_mrt_cache",
2445 sizeof(struct mfc_cache
),
2446 0, SLAB_HWCACHE_ALIGN
| SLAB_PANIC
,
2451 err
= register_pernet_subsys(&ipmr_net_ops
);
2453 goto reg_pernet_fail
;
2455 err
= register_netdevice_notifier(&ip_mr_notifier
);
2457 goto reg_notif_fail
;
2458 #ifdef CONFIG_IP_PIMSM_V2
2459 if (inet_add_protocol(&pim_protocol
, IPPROTO_PIM
) < 0) {
2460 printk(KERN_ERR
"ip_mr_init: can't add PIM protocol\n");
2462 goto add_proto_fail
;
2465 rtnl_register(RTNL_FAMILY_IPMR
, RTM_GETROUTE
, NULL
, ipmr_rtm_dumproute
);
2468 #ifdef CONFIG_IP_PIMSM_V2
2470 unregister_netdevice_notifier(&ip_mr_notifier
);
2473 unregister_pernet_subsys(&ipmr_net_ops
);
2475 kmem_cache_destroy(mrt_cachep
);